• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 com.android.settingslib.applications;
18 
19 import android.app.AppGlobals;
20 import android.annotation.Nullable;
21 import android.content.ComponentName;
22 import android.content.Context;
23 import android.content.pm.ApplicationInfo;
24 import android.content.pm.ComponentInfo;
25 import android.content.pm.PackageItemInfo;
26 import android.content.pm.PackageManager;
27 import android.graphics.drawable.Drawable;
28 import android.os.RemoteException;
29 import android.util.IconDrawableFactory;
30 
31 import com.android.settingslib.widget.CandidateInfo;
32 
33 /**
34  * Data model representing an app in DefaultAppPicker UI.
35  */
36 public class DefaultAppInfo extends CandidateInfo {
37 
38     public final int userId;
39     public final ComponentName componentName;
40     public final PackageItemInfo packageItemInfo;
41     public final String summary;
42     protected final PackageManager mPm;
43     private final Context mContext;
44 
DefaultAppInfo(Context context, PackageManager pm, int uid, ComponentName cn)45     public DefaultAppInfo(Context context, PackageManager pm, int uid, ComponentName cn) {
46         this(context, pm, uid, cn, null /* summary */, true /* enabled */);
47     }
48 
DefaultAppInfo(Context context, PackageManager pm, int uid, PackageItemInfo info)49     public DefaultAppInfo(Context context, PackageManager pm, int uid, PackageItemInfo info) {
50         this(context, pm, uid, info, null /* summary */, true /* enabled */);
51     }
52 
DefaultAppInfo(Context context, PackageManager pm, int uid, ComponentName cn, String summary, boolean enabled)53     public DefaultAppInfo(Context context, PackageManager pm, int uid, ComponentName cn,
54                           String summary, boolean enabled) {
55         super(enabled);
56         mContext = context;
57         mPm = pm;
58         packageItemInfo = null;
59         userId = uid;
60         componentName = cn;
61         this.summary = summary;
62     }
63 
DefaultAppInfo(Context context, PackageManager pm, int uid, PackageItemInfo info, String summary, boolean enabled)64     public DefaultAppInfo(Context context, PackageManager pm, int uid, PackageItemInfo info,
65                           String summary, boolean enabled) {
66         super(enabled);
67         mContext = context;
68         mPm = pm;
69         userId = uid;
70         packageItemInfo = info;
71         componentName = null;
72         this.summary = summary;
73     }
74 
75     @Override
loadLabel()76     public CharSequence loadLabel() {
77         if (componentName != null) {
78             try {
79                 final ComponentInfo componentInfo = getComponentInfo();
80                 if (componentInfo != null) {
81                     return componentInfo.loadLabel(mPm);
82                 } else {
83                     final ApplicationInfo appInfo = mPm.getApplicationInfoAsUser(
84                             componentName.getPackageName(), 0, userId);
85                     return appInfo.loadLabel(mPm);
86                 }
87             } catch (PackageManager.NameNotFoundException e) {
88                 return null;
89             }
90         } else if (packageItemInfo != null) {
91             return packageItemInfo.loadLabel(mPm);
92         } else {
93             return null;
94         }
95 
96     }
97 
getSummary()98     public @Nullable String getSummary() {
99         return this.summary;
100     }
101 
102     @Override
loadIcon()103     public Drawable loadIcon() {
104         final IconDrawableFactory factory = IconDrawableFactory.newInstance(mContext);
105         if (componentName != null) {
106             try {
107                 final ComponentInfo componentInfo = getComponentInfo();
108                 final ApplicationInfo appInfo = mPm.getApplicationInfoAsUser(
109                         componentName.getPackageName(), 0, userId);
110                 if (componentInfo != null) {
111                     return factory.getBadgedIcon(componentInfo, appInfo, userId);
112                 } else {
113                     return factory.getBadgedIcon(appInfo);
114                 }
115             } catch (PackageManager.NameNotFoundException e) {
116                 return null;
117             }
118         }
119         if (packageItemInfo != null) {
120             try {
121                 final ApplicationInfo appInfo = mPm.getApplicationInfoAsUser(
122                         packageItemInfo.packageName, 0, userId);
123                 return factory.getBadgedIcon(packageItemInfo, appInfo, userId);
124             } catch (PackageManager.NameNotFoundException e) {
125                 return null;
126             }
127         } else {
128             return null;
129         }
130     }
131 
132     @Override
getKey()133     public String getKey() {
134         if (componentName != null) {
135             return componentName.flattenToString();
136         } else if (packageItemInfo != null) {
137             return packageItemInfo.packageName;
138         } else {
139             return null;
140         }
141     }
142 
getComponentInfo()143     private ComponentInfo getComponentInfo() {
144         try {
145             ComponentInfo componentInfo = AppGlobals.getPackageManager().getActivityInfo(
146                     componentName, 0, userId);
147             if (componentInfo == null) {
148                 componentInfo = AppGlobals.getPackageManager().getServiceInfo(
149                         componentName, 0, userId);
150             }
151             return componentInfo;
152         } catch (RemoteException e) {
153             return null;
154         }
155     }
156 }
157