1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.health.connect.migration;
18 
19 import android.annotation.NonNull;
20 import android.annotation.SystemApi;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23 
24 /**
25  * A base class for migration payloads. There is no need extend this class, use existing subclasses
26  * instead. <br>
27  * <br>
28  *
29  * <p>Steps when adding a new type:
30  *
31  * <ul>
32  *   <li>Create a new class, make sure it extends {@code MigrationPayload}.
33  *   <li>Handle the new class in {@link MigrationPayload#CREATOR}.
34  *   <li>Handle the new class in {@link MigrationEntity#writeToParcel(android.os.Parcel, int)} and
35  *       in {@link MigrationEntity}'s constructor.
36  *   <li>Handle the new class in {@link
37  *       com.android.server.healthconnect.migration.DataMigrationManager}
38  * </ul>
39  *
40  * Refer to existing subclasses for details.
41  *
42  * @hide
43  */
44 @SuppressWarnings({"ParcelNotFinal", "ParcelCreator"}) // Can be only extended internally
45 @SystemApi
46 public abstract class MigrationPayload implements Parcelable {
47 
48     static final int TYPE_PACKAGE_PERMISSIONS = 1;
49     static final int TYPE_RECORD = 2;
50     static final int TYPE_APP_INFO = 3;
51     static final int TYPE_PRIORITY = 4;
52     static final int TYPE_METADATA = 5;
53 
54     @NonNull
55     public static final Parcelable.Creator<MigrationPayload> CREATOR =
56             new Creator<>() {
57                 @Override
58                 public MigrationPayload createFromParcel(Parcel source) {
59                     final int type = source.readInt();
60                     switch (type) {
61                         case TYPE_PACKAGE_PERMISSIONS:
62                             return new PermissionMigrationPayload(source);
63                         case TYPE_RECORD:
64                             return new RecordMigrationPayload(source);
65                         case TYPE_APP_INFO:
66                             return new AppInfoMigrationPayload(source);
67                         case TYPE_PRIORITY:
68                             return new PriorityMigrationPayload(source);
69                         case TYPE_METADATA:
70                             return new MetadataMigrationPayload(source);
71                         default:
72                             throw new IllegalStateException("Unexpected payload type: " + type);
73                     }
74                 }
75 
76                 @Override
77                 public MigrationPayload[] newArray(int size) {
78                     return new MigrationPayload[size];
79                 }
80             };
81 
82     /** Package-private constructor - instances and custom subclasses are not allowed. */
MigrationPayload()83     MigrationPayload() {}
84 
85     @Override
describeContents()86     public int describeContents() {
87         return 0;
88     }
89 }
90