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