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 static java.util.Objects.requireNonNull;
20 
21 import android.annotation.NonNull;
22 import android.annotation.Nullable;
23 import android.annotation.SystemApi;
24 import android.os.Parcel;
25 import android.os.Parcelable;
26 
27 /**
28  * Holds application info migration data payload. Used to backfill application info for apps.
29  *
30  * @hide
31  */
32 @SystemApi
33 public final class AppInfoMigrationPayload extends MigrationPayload implements Parcelable {
34 
35     @NonNull
36     public static final Creator<AppInfoMigrationPayload> CREATOR =
37             new Creator<>() {
38                 @Override
39                 public AppInfoMigrationPayload createFromParcel(Parcel in) {
40                     in.readInt(); // Skip the type
41                     return new AppInfoMigrationPayload(in);
42                 }
43 
44                 @Override
45                 public AppInfoMigrationPayload[] newArray(int size) {
46                     return new AppInfoMigrationPayload[size];
47                 }
48             };
49 
50     private final String mPackageName;
51     private final String mAppName;
52     private final byte[] mAppIcon;
53 
54     @SuppressWarnings("NullAway") // TODO(b/317029272): fix this suppression
AppInfoMigrationPayload( @onNull String packageName, @NonNull String appName, @Nullable byte[] appIcon)55     private AppInfoMigrationPayload(
56             @NonNull String packageName, @NonNull String appName, @Nullable byte[] appIcon) {
57         mPackageName = packageName;
58         mAppName = appName;
59         mAppIcon = appIcon;
60     }
61 
AppInfoMigrationPayload(@onNull Parcel in)62     AppInfoMigrationPayload(@NonNull Parcel in) {
63         mPackageName = in.readString();
64         mAppName = in.readString();
65         mAppIcon = in.createByteArray();
66     }
67 
68     @Override
writeToParcel(@onNull Parcel dest, int flags)69     public void writeToParcel(@NonNull Parcel dest, int flags) {
70         dest.writeInt(TYPE_APP_INFO);
71 
72         dest.writeString(mPackageName);
73         dest.writeString(mAppName);
74         dest.writeByteArray(mAppIcon);
75     }
76 
77     @Override
describeContents()78     public int describeContents() {
79         return 0;
80     }
81 
82     /** Returns package name of this app info payload. */
83     @NonNull
getPackageName()84     public String getPackageName() {
85         return mPackageName;
86     }
87 
88     /** Returns application name of this app info payload. */
89     @NonNull
getAppName()90     public String getAppName() {
91         return mAppName;
92     }
93 
94     /**
95      * Returns icon bitmap encoded as a byte array. The icon is decoded using {@link
96      * android.graphics.BitmapFactory#decodeByteArray(byte[], int, int)}.
97      */
98     @Nullable
getAppIcon()99     public byte[] getAppIcon() {
100         return mAppIcon;
101     }
102 
103     /** Builder for {@link AppInfoMigrationPayload}. */
104     public static final class Builder {
105         private String mPackageName;
106         private String mAppName;
107         private byte[] mAppIcon;
108 
109         @SuppressWarnings("NullAway.Init") // TODO(b/317029272): fix this suppression
Builder(@onNull String packageName, @NonNull String appName)110         public Builder(@NonNull String packageName, @NonNull String appName) {
111             requireNonNull(packageName);
112             requireNonNull(appName);
113 
114             mPackageName = packageName;
115             mAppName = appName;
116         }
117 
118         /** Sets the value for {@link AppInfoMigrationPayload#getPackageName()}. */
119         @NonNull
setPackageName(@onNull String packageName)120         public Builder setPackageName(@NonNull String packageName) {
121             requireNonNull(packageName);
122             mPackageName = packageName;
123             return this;
124         }
125 
126         /** Sets the value for {@link AppInfoMigrationPayload#getAppName()}. */
127         @NonNull
setAppName(@onNull String appName)128         public Builder setAppName(@NonNull String appName) {
129             requireNonNull(appName);
130             mAppName = appName;
131             return this;
132         }
133 
134         /** Sets the value for {@link AppInfoMigrationPayload#getAppIcon()}. */
135         @SuppressWarnings("NullAway") // TODO(b/317029272): fix this suppression
136         @NonNull
setAppIcon(@ullable byte[] appIcon)137         public Builder setAppIcon(@Nullable byte[] appIcon) {
138             mAppIcon = appIcon;
139             return this;
140         }
141 
142         /**
143          * Creates a new instance of {@link AppInfoMigrationPayload} with the specified arguments.
144          */
145         @NonNull
build()146         public AppInfoMigrationPayload build() {
147             return new AppInfoMigrationPayload(mPackageName, mAppName, mAppIcon);
148         }
149     }
150 }
151