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