Svet Ganov | 2acf063 | 2015-11-24 19:10:59 -0800 | [diff] [blame] | 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; |
Jeff Sharkey | c204c22 | 2017-11-27 15:02:10 -0700 | [diff] [blame] | 21 | import android.annotation.SystemApi; |
Svet Ganov | 2acf063 | 2015-11-24 19:10:59 -0800 | [diff] [blame] | 22 | import android.graphics.drawable.Drawable; |
| 23 | import android.os.Parcel; |
| 24 | import android.os.Parcelable; |
| 25 | |
| 26 | /** |
Svetoslav Ganov | 096d304 | 2017-01-30 16:34:13 -0800 | [diff] [blame] | 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. |
Svet Ganov | 2acf063 | 2015-11-24 19:10:59 -0800 | [diff] [blame] | 32 | * |
| 33 | * @hide |
| 34 | */ |
Jeff Sharkey | c204c22 | 2017-11-27 15:02:10 -0700 | [diff] [blame] | 35 | @SystemApi |
Svetoslav Ganov | 096d304 | 2017-01-30 16:34:13 -0800 | [diff] [blame] | 36 | public final class InstantAppInfo implements Parcelable { |
Svet Ganov | 2acf063 | 2015-11-24 19:10:59 -0800 | [diff] [blame] | 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 | |
Svetoslav Ganov | 096d304 | 2017-01-30 16:34:13 -0800 | [diff] [blame] | 45 | public InstantAppInfo(ApplicationInfo appInfo, |
Svet Ganov | 2acf063 | 2015-11-24 19:10:59 -0800 | [diff] [blame] | 46 | String[] requestedPermissions, String[] grantedPermissions) { |
| 47 | mApplicationInfo = appInfo; |
| 48 | mPackageName = null; |
| 49 | mLabelText = null; |
| 50 | mRequestedPermissions = requestedPermissions; |
| 51 | mGrantedPermissions = grantedPermissions; |
| 52 | } |
| 53 | |
Svetoslav Ganov | 096d304 | 2017-01-30 16:34:13 -0800 | [diff] [blame] | 54 | public InstantAppInfo(String packageName, CharSequence label, |
Svet Ganov | 2acf063 | 2015-11-24 19:10:59 -0800 | [diff] [blame] | 55 | String[] requestedPermissions, String[] grantedPermissions) { |
| 56 | mApplicationInfo = null; |
| 57 | mPackageName = packageName; |
| 58 | mLabelText = label; |
| 59 | mRequestedPermissions = requestedPermissions; |
| 60 | mGrantedPermissions = grantedPermissions; |
| 61 | } |
| 62 | |
Svetoslav Ganov | 096d304 | 2017-01-30 16:34:13 -0800 | [diff] [blame] | 63 | private InstantAppInfo(Parcel parcel) { |
Svet Ganov | 2acf063 | 2015-11-24 19:10:59 -0800 | [diff] [blame] | 64 | mPackageName = parcel.readString(); |
| 65 | mLabelText = parcel.readCharSequence(); |
| 66 | mRequestedPermissions = parcel.readStringArray(); |
| 67 | mGrantedPermissions = parcel.createStringArray(); |
| 68 | mApplicationInfo = parcel.readParcelable(null); |
| 69 | } |
| 70 | |
Svetoslav Ganov | 096d304 | 2017-01-30 16:34:13 -0800 | [diff] [blame] | 71 | /** |
| 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 Kennedy | bcdaf92 | 2017-02-28 16:20:09 -0800 | [diff] [blame] | 80 | * @return The package name. |
Svetoslav Ganov | 096d304 | 2017-01-30 16:34:13 -0800 | [diff] [blame] | 81 | */ |
Svet Ganov | 2acf063 | 2015-11-24 19:10:59 -0800 | [diff] [blame] | 82 | public @NonNull String getPackageName() { |
| 83 | if (mApplicationInfo != null) { |
| 84 | return mApplicationInfo.packageName; |
| 85 | } |
| 86 | return mPackageName; |
| 87 | } |
| 88 | |
Svetoslav Ganov | 096d304 | 2017-01-30 16:34:13 -0800 | [diff] [blame] | 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 | */ |
Svet Ganov | 2acf063 | 2015-11-24 19:10:59 -0800 | [diff] [blame] | 93 | public @NonNull CharSequence loadLabel(@NonNull PackageManager packageManager) { |
| 94 | if (mApplicationInfo != null) { |
| 95 | return mApplicationInfo.loadLabel(packageManager); |
| 96 | } |
| 97 | return mLabelText; |
| 98 | } |
| 99 | |
Svetoslav Ganov | 096d304 | 2017-01-30 16:34:13 -0800 | [diff] [blame] | 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 | */ |
Svet Ganov | 2acf063 | 2015-11-24 19:10:59 -0800 | [diff] [blame] | 104 | public @NonNull Drawable loadIcon(@NonNull PackageManager packageManager) { |
| 105 | if (mApplicationInfo != null) { |
| 106 | return mApplicationInfo.loadIcon(packageManager); |
| 107 | } |
Svetoslav Ganov | 096d304 | 2017-01-30 16:34:13 -0800 | [diff] [blame] | 108 | return packageManager.getInstantAppIcon(mPackageName); |
Svet Ganov | 2acf063 | 2015-11-24 19:10:59 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Svetoslav Ganov | 096d304 | 2017-01-30 16:34:13 -0800 | [diff] [blame] | 111 | /** |
| 112 | * @return The requested permissions. |
| 113 | */ |
Svet Ganov | 2acf063 | 2015-11-24 19:10:59 -0800 | [diff] [blame] | 114 | public @Nullable String[] getRequestedPermissions() { |
| 115 | return mRequestedPermissions; |
| 116 | } |
| 117 | |
Svetoslav Ganov | 096d304 | 2017-01-30 16:34:13 -0800 | [diff] [blame] | 118 | /** |
| 119 | * @return The granted permissions. |
| 120 | */ |
Svet Ganov | 2acf063 | 2015-11-24 19:10:59 -0800 | [diff] [blame] | 121 | 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 Ganov | 096d304 | 2017-01-30 16:34:13 -0800 | [diff] [blame] | 139 | public static final Creator<InstantAppInfo> CREATOR = |
| 140 | new Creator<InstantAppInfo>() { |
Svet Ganov | 2acf063 | 2015-11-24 19:10:59 -0800 | [diff] [blame] | 141 | @Override |
Svetoslav Ganov | 096d304 | 2017-01-30 16:34:13 -0800 | [diff] [blame] | 142 | public InstantAppInfo createFromParcel(Parcel parcel) { |
| 143 | return new InstantAppInfo(parcel); |
Svet Ganov | 2acf063 | 2015-11-24 19:10:59 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | @Override |
Svetoslav Ganov | 096d304 | 2017-01-30 16:34:13 -0800 | [diff] [blame] | 147 | public InstantAppInfo[] newArray(int size) { |
| 148 | return new InstantAppInfo[0]; |
Svet Ganov | 2acf063 | 2015-11-24 19:10:59 -0800 | [diff] [blame] | 149 | } |
| 150 | }; |
| 151 | } |