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