blob: cb04fc3ce8e117554353e2f96e751a5158372232 [file] [log] [blame]
Svet Ganov2acf0632015-11-24 19:10:59 -08001/*
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
17package android.content.pm;
18
19import android.annotation.NonNull;
20import android.annotation.Nullable;
Jeff Sharkeyc204c222017-11-27 15:02:10 -070021import android.annotation.SystemApi;
Svet Ganov2acf0632015-11-24 19:10:59 -080022import android.graphics.drawable.Drawable;
23import android.os.Parcel;
24import android.os.Parcelable;
25
26/**
Svetoslav Ganov096d3042017-01-30 16:34:13 -080027 * 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.
Svet Ganov2acf0632015-11-24 19:10:59 -080032 *
33 * @hide
34 */
Jeff Sharkeyc204c222017-11-27 15:02:10 -070035@SystemApi
Svetoslav Ganov096d3042017-01-30 16:34:13 -080036public final class InstantAppInfo implements Parcelable {
Svet Ganov2acf0632015-11-24 19:10:59 -080037 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
Svetoslav Ganov096d3042017-01-30 16:34:13 -080045 public InstantAppInfo(ApplicationInfo appInfo,
Svet Ganov2acf0632015-11-24 19:10:59 -080046 String[] requestedPermissions, String[] grantedPermissions) {
47 mApplicationInfo = appInfo;
48 mPackageName = null;
49 mLabelText = null;
50 mRequestedPermissions = requestedPermissions;
51 mGrantedPermissions = grantedPermissions;
52 }
53
Svetoslav Ganov096d3042017-01-30 16:34:13 -080054 public InstantAppInfo(String packageName, CharSequence label,
Svet Ganov2acf0632015-11-24 19:10:59 -080055 String[] requestedPermissions, String[] grantedPermissions) {
56 mApplicationInfo = null;
57 mPackageName = packageName;
58 mLabelText = label;
59 mRequestedPermissions = requestedPermissions;
60 mGrantedPermissions = grantedPermissions;
61 }
62
Svetoslav Ganov096d3042017-01-30 16:34:13 -080063 private InstantAppInfo(Parcel parcel) {
Svet Ganov2acf0632015-11-24 19:10:59 -080064 mPackageName = parcel.readString();
65 mLabelText = parcel.readCharSequence();
66 mRequestedPermissions = parcel.readStringArray();
67 mGrantedPermissions = parcel.createStringArray();
68 mApplicationInfo = parcel.readParcelable(null);
69 }
70
Svetoslav Ganov096d3042017-01-30 16:34:13 -080071 /**
72 * @return The application info if the app is installed,
73 * <code>null</code> otherwise,
74 */
75 public @Nullable ApplicationInfo getApplicationInfo() {
76 return mApplicationInfo;
77 }
78
79 /**
Todd Kennedybcdaf922017-02-28 16:20:09 -080080 * @return The package name.
Svetoslav Ganov096d3042017-01-30 16:34:13 -080081 */
Svet Ganov2acf0632015-11-24 19:10:59 -080082 public @NonNull String getPackageName() {
83 if (mApplicationInfo != null) {
84 return mApplicationInfo.packageName;
85 }
86 return mPackageName;
87 }
88
Svetoslav Ganov096d3042017-01-30 16:34:13 -080089 /**
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 */
Svet Ganov2acf0632015-11-24 19:10:59 -080093 public @NonNull CharSequence loadLabel(@NonNull PackageManager packageManager) {
94 if (mApplicationInfo != null) {
95 return mApplicationInfo.loadLabel(packageManager);
96 }
97 return mLabelText;
98 }
99
Svetoslav Ganov096d3042017-01-30 16:34:13 -0800100 /**
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 */
Svet Ganov2acf0632015-11-24 19:10:59 -0800104 public @NonNull Drawable loadIcon(@NonNull PackageManager packageManager) {
105 if (mApplicationInfo != null) {
106 return mApplicationInfo.loadIcon(packageManager);
107 }
Svetoslav Ganov096d3042017-01-30 16:34:13 -0800108 return packageManager.getInstantAppIcon(mPackageName);
Svet Ganov2acf0632015-11-24 19:10:59 -0800109 }
110
Svetoslav Ganov096d3042017-01-30 16:34:13 -0800111 /**
112 * @return The requested permissions.
113 */
Svet Ganov2acf0632015-11-24 19:10:59 -0800114 public @Nullable String[] getRequestedPermissions() {
115 return mRequestedPermissions;
116 }
117
Svetoslav Ganov096d3042017-01-30 16:34:13 -0800118 /**
119 * @return The granted permissions.
120 */
Svet Ganov2acf0632015-11-24 19:10:59 -0800121 public @Nullable String[] getGrantedPermissions() {
122 return mGrantedPermissions;
123 }
124
125 @Override
126 public int describeContents() {
127 return 0;
128 }
129
130 @Override
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
Svetoslav Ganov096d3042017-01-30 16:34:13 -0800139 public static final Creator<InstantAppInfo> CREATOR =
140 new Creator<InstantAppInfo>() {
Svet Ganov2acf0632015-11-24 19:10:59 -0800141 @Override
Svetoslav Ganov096d3042017-01-30 16:34:13 -0800142 public InstantAppInfo createFromParcel(Parcel parcel) {
143 return new InstantAppInfo(parcel);
Svet Ganov2acf0632015-11-24 19:10:59 -0800144 }
145
146 @Override
Svetoslav Ganov096d3042017-01-30 16:34:13 -0800147 public InstantAppInfo[] newArray(int size) {
148 return new InstantAppInfo[0];
Svet Ganov2acf0632015-11-24 19:10:59 -0800149 }
150 };
151}