1 /*
2  * Copyright (C) 2015 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.content.pm;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.annotation.SystemApi;
22 import android.graphics.drawable.Drawable;
23 import android.os.Parcel;
24 import android.os.Parcelable;
25 
26 /**
27  * This class represents the state of an instant app. Instant apps can
28  * be installed or uninstalled. If the app is installed you can call
29  * {@link #getApplicationInfo()} to get the app info, otherwise this
30  * class provides APIs to get basic app info for showing it in the UI,
31  * such as permissions, label, package name.
32  *
33  * @hide
34  */
35 @SystemApi
36 public final class InstantAppInfo implements Parcelable {
37     private final ApplicationInfo mApplicationInfo;
38 
39     private final String mPackageName;
40     private final CharSequence mLabelText;
41 
42     private final String[] mRequestedPermissions;
43     private final String[] mGrantedPermissions;
44 
InstantAppInfo(ApplicationInfo appInfo, String[] requestedPermissions, String[] grantedPermissions)45     public InstantAppInfo(ApplicationInfo appInfo,
46             String[] requestedPermissions, String[] grantedPermissions) {
47         mApplicationInfo = appInfo;
48         mPackageName = null;
49         mLabelText = null;
50         mRequestedPermissions = requestedPermissions;
51         mGrantedPermissions = grantedPermissions;
52     }
53 
InstantAppInfo(String packageName, CharSequence label, String[] requestedPermissions, String[] grantedPermissions)54     public InstantAppInfo(String packageName, CharSequence label,
55             String[] requestedPermissions, String[] grantedPermissions) {
56         mApplicationInfo = null;
57         mPackageName = packageName;
58         mLabelText = label;
59         mRequestedPermissions = requestedPermissions;
60         mGrantedPermissions = grantedPermissions;
61     }
62 
InstantAppInfo(Parcel parcel)63     private InstantAppInfo(Parcel parcel) {
64         mPackageName = parcel.readString();
65         mLabelText = parcel.readCharSequence();
66         mRequestedPermissions = parcel.readStringArray();
67         mGrantedPermissions = parcel.createStringArray();
68         mApplicationInfo = parcel.readParcelable(null);
69     }
70 
71     /**
72      * @return The application info if the app is installed,
73      *     <code>null</code> otherwise,
74      */
getApplicationInfo()75     public @Nullable ApplicationInfo getApplicationInfo() {
76         return mApplicationInfo;
77     }
78 
79     /**
80      * @return The package name.
81      */
getPackageName()82     public @NonNull String getPackageName() {
83         if (mApplicationInfo != null) {
84             return mApplicationInfo.packageName;
85         }
86         return mPackageName;
87     }
88 
89     /**
90      * @param packageManager Package manager for loading resources.
91      * @return Loads the label if the app is installed or returns the cached one otherwise.
92      */
loadLabel(@onNull PackageManager packageManager)93     public @NonNull CharSequence loadLabel(@NonNull PackageManager packageManager) {
94         if (mApplicationInfo != null) {
95             return mApplicationInfo.loadLabel(packageManager);
96         }
97         return mLabelText;
98     }
99 
100     /**
101      * @param packageManager Package manager for loading resources.
102      * @return Loads the icon if the app is installed or returns the cached one otherwise.
103      */
loadIcon(@onNull PackageManager packageManager)104     public @NonNull Drawable loadIcon(@NonNull PackageManager packageManager) {
105         if (mApplicationInfo != null) {
106             return mApplicationInfo.loadIcon(packageManager);
107         }
108         return packageManager.getInstantAppIcon(mPackageName);
109     }
110 
111     /**
112      * @return The requested permissions.
113      */
getRequestedPermissions()114     public @Nullable String[] getRequestedPermissions() {
115         return mRequestedPermissions;
116     }
117 
118     /**
119      * @return The granted permissions.
120      */
getGrantedPermissions()121     public @Nullable String[] getGrantedPermissions() {
122         return mGrantedPermissions;
123     }
124 
125     @Override
describeContents()126     public int describeContents() {
127         return 0;
128     }
129 
130     @Override
writeToParcel(Parcel parcel, int flags)131     public void writeToParcel(Parcel parcel, int flags) {
132         parcel.writeString(mPackageName);
133         parcel.writeCharSequence(mLabelText);
134         parcel.writeStringArray(mRequestedPermissions);
135         parcel.writeStringArray(mGrantedPermissions);
136         parcel.writeParcelable(mApplicationInfo, flags);
137     }
138 
139     public static final @android.annotation.NonNull Creator<InstantAppInfo> CREATOR =
140             new Creator<InstantAppInfo>() {
141         @Override
142         public InstantAppInfo createFromParcel(Parcel parcel) {
143             return new InstantAppInfo(parcel);
144         }
145 
146         @Override
147         public InstantAppInfo[] newArray(int size) {
148             return new InstantAppInfo[0];
149         }
150     };
151 }
152