blob: ca050918d83acd50c04c0742d4b2db84cd6b5848 [file] [log] [blame]
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001/*
2 * Copyright (C) 2010 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
17package android.app;
18
Tor Norbye7b9c9122013-05-30 16:48:33 -070019import android.annotation.DrawableRes;
Alan Viveretteecd585a2015-04-13 10:32:51 -070020import android.annotation.NonNull;
21import android.annotation.Nullable;
Tor Norbye7b9c9122013-05-30 16:48:33 -070022import android.annotation.StringRes;
23import android.annotation.XmlRes;
Makoto Onukif34db0a2016-02-17 11:17:15 -080024import android.app.admin.DevicePolicyManager;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080025import android.content.ComponentName;
26import android.content.ContentResolver;
27import android.content.Intent;
28import android.content.IntentFilter;
29import android.content.IntentSender;
30import android.content.pm.ActivityInfo;
31import android.content.pm.ApplicationInfo;
32import android.content.pm.ComponentInfo;
Svet Ganov2acf0632015-11-24 19:10:59 -080033import android.content.pm.EphemeralApplicationInfo;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080034import android.content.pm.FeatureInfo;
Svetoslavf7c06eb2015-06-10 18:43:22 -070035import android.content.pm.IOnPermissionsChangeListener;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080036import android.content.pm.IPackageDataObserver;
37import android.content.pm.IPackageDeleteObserver;
38import android.content.pm.IPackageInstallObserver;
39import android.content.pm.IPackageManager;
40import android.content.pm.IPackageMoveObserver;
41import android.content.pm.IPackageStatsObserver;
42import android.content.pm.InstrumentationInfo;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -080043import android.content.pm.IntentFilterVerificationInfo;
dcashman9d2f4412014-06-09 09:27:54 -070044import android.content.pm.KeySet;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080045import android.content.pm.PackageInfo;
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -070046import android.content.pm.PackageInstaller;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +010047import android.content.pm.PackageItemInfo;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080048import android.content.pm.PackageManager;
Kenny Roote6cd0c72011-05-19 12:48:14 -070049import android.content.pm.ParceledListSlice;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080050import android.content.pm.PermissionGroupInfo;
51import android.content.pm.PermissionInfo;
52import android.content.pm.ProviderInfo;
53import android.content.pm.ResolveInfo;
54import android.content.pm.ServiceInfo;
Svetoslavc7d62f02014-09-04 15:39:54 -070055import android.content.pm.UserInfo;
Kenny Root0aaa0d92011-09-12 16:42:55 -070056import android.content.pm.VerifierDeviceIdentity;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080057import android.content.res.Resources;
58import android.content.res.XmlResourceParser;
Svetoslavc7d62f02014-09-04 15:39:54 -070059import android.graphics.Bitmap;
60import android.graphics.Canvas;
61import android.graphics.Rect;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +010062import android.graphics.drawable.BitmapDrawable;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080063import android.graphics.drawable.Drawable;
64import android.net.Uri;
Jeff Sharkey50a05452015-04-29 11:24:52 -070065import android.os.Bundle;
Jeff Sharkey620b32b2015-04-23 19:36:02 -070066import android.os.Handler;
67import android.os.Looper;
68import android.os.Message;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080069import android.os.Process;
70import android.os.RemoteException;
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -070071import android.os.SystemProperties;
Amith Yamasani67df64b2012-12-14 12:09:36 -080072import android.os.UserHandle;
Nicolas Prevot88cc3462014-05-14 14:51:48 +010073import android.os.UserManager;
Jeff Sharkeye2d45be2015-04-15 17:14:12 -070074import android.os.storage.StorageManager;
75import android.os.storage.VolumeInfo;
Todd Kennedyf39ca8f2015-08-07 14:15:07 -070076import android.provider.Settings;
Dianne Hackbornadd005c2013-07-17 18:43:12 -070077import android.util.ArrayMap;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080078import android.util.Log;
Jeff Browna492c3a2012-08-23 19:48:44 -070079import android.view.Display;
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -070080
81import dalvik.system.VMRuntime;
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -070082
83import com.android.internal.annotations.GuardedBy;
Jeff Sharkeyb36586a2015-04-27 08:42:28 -070084import com.android.internal.os.SomeArgs;
dcashman9d2f4412014-06-09 09:27:54 -070085import com.android.internal.util.Preconditions;
Alexandra Gherghina64d4dca2014-08-28 18:26:56 +010086import com.android.internal.util.UserIcons;
Svet Ganov2acf0632015-11-24 19:10:59 -080087import libcore.util.EmptyArray;
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -070088
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080089import java.lang.ref.WeakReference;
90import java.util.ArrayList;
Svet Ganov2acf0632015-11-24 19:10:59 -080091import java.util.Collections;
Jeff Sharkey620b32b2015-04-23 19:36:02 -070092import java.util.Iterator;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080093import java.util.List;
Svetoslavf7c06eb2015-06-10 18:43:22 -070094import java.util.Map;
Jeff Sharkey620b32b2015-04-23 19:36:02 -070095import java.util.Objects;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080096
Makoto Onukicc4bbeb2015-09-17 10:28:24 -070097/** @hide */
98public class ApplicationPackageManager extends PackageManager {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080099 private static final String TAG = "ApplicationPackageManager";
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800100 private final static boolean DEBUG_ICONS = false;
101
Svet Ganov2acf0632015-11-24 19:10:59 -0800102 private static final int DEFAULT_EPHEMERAL_COOKIE_MAX_SIZE_BYTES = 16384; // 16KB
103
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700104 // Default flags to use with PackageManager when no flags are given.
105 private final static int sDefaultFlags = PackageManager.GET_SHARED_LIBRARY_FILES;
106
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -0700107 private final Object mLock = new Object();
108
109 @GuardedBy("mLock")
110 private UserManager mUserManager;
111 @GuardedBy("mLock")
112 private PackageInstaller mInstaller;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100113
Jeff Sharkey620b32b2015-04-23 19:36:02 -0700114 @GuardedBy("mDelegates")
115 private final ArrayList<MoveCallbackDelegate> mDelegates = new ArrayList<>();
116
Svet Ganovf1b7f202015-07-29 08:33:42 -0700117 @GuardedBy("mLock")
118 private String mPermissionsControllerPackageName;
119
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100120 UserManager getUserManager() {
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -0700121 synchronized (mLock) {
122 if (mUserManager == null) {
123 mUserManager = UserManager.get(mContext);
124 }
125 return mUserManager;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100126 }
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100127 }
128
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800129 @Override
130 public PackageInfo getPackageInfo(String packageName, int flags)
131 throws NameNotFoundException {
Nicolas Prevot9a80e532015-09-23 15:49:28 +0100132 return getPackageInfoAsUser(packageName, flags, mContext.getUserId());
133 }
134
135 @Override
136 public PackageInfo getPackageInfoAsUser(String packageName, int flags, int userId)
137 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800138 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +0100139 PackageInfo pi = mPM.getPackageInfo(packageName, flags, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800140 if (pi != null) {
141 return pi;
142 }
143 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700144 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800145 }
146
147 throw new NameNotFoundException(packageName);
148 }
149
150 @Override
151 public String[] currentToCanonicalPackageNames(String[] names) {
152 try {
153 return mPM.currentToCanonicalPackageNames(names);
154 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700155 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800156 }
157 }
158
159 @Override
160 public String[] canonicalToCurrentPackageNames(String[] names) {
161 try {
162 return mPM.canonicalToCurrentPackageNames(names);
163 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700164 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800165 }
166 }
167
168 @Override
169 public Intent getLaunchIntentForPackage(String packageName) {
170 // First see if the package has an INFO activity; the existence of
171 // such an activity is implied to be the desired front-door for the
172 // overall package (such as if it has multiple launcher entries).
173 Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
174 intentToResolve.addCategory(Intent.CATEGORY_INFO);
175 intentToResolve.setPackage(packageName);
Dianne Hackborn19415762010-12-15 00:20:27 -0800176 List<ResolveInfo> ris = queryIntentActivities(intentToResolve, 0);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800177
178 // Otherwise, try to find a main launcher activity.
Dianne Hackborn19415762010-12-15 00:20:27 -0800179 if (ris == null || ris.size() <= 0) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800180 // reuse the intent instance
181 intentToResolve.removeCategory(Intent.CATEGORY_INFO);
182 intentToResolve.addCategory(Intent.CATEGORY_LAUNCHER);
183 intentToResolve.setPackage(packageName);
Dianne Hackborn19415762010-12-15 00:20:27 -0800184 ris = queryIntentActivities(intentToResolve, 0);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800185 }
Dianne Hackborn19415762010-12-15 00:20:27 -0800186 if (ris == null || ris.size() <= 0) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800187 return null;
188 }
189 Intent intent = new Intent(intentToResolve);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800190 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Dianne Hackborn19415762010-12-15 00:20:27 -0800191 intent.setClassName(ris.get(0).activityInfo.packageName,
192 ris.get(0).activityInfo.name);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800193 return intent;
194 }
195
196 @Override
Jose Lima970417c2014-04-10 10:42:19 -0700197 public Intent getLeanbackLaunchIntentForPackage(String packageName) {
198 // Try to find a main leanback_launcher activity.
199 Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
200 intentToResolve.addCategory(Intent.CATEGORY_LEANBACK_LAUNCHER);
201 intentToResolve.setPackage(packageName);
202 List<ResolveInfo> ris = queryIntentActivities(intentToResolve, 0);
203
204 if (ris == null || ris.size() <= 0) {
205 return null;
206 }
207 Intent intent = new Intent(intentToResolve);
208 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
209 intent.setClassName(ris.get(0).activityInfo.packageName,
210 ris.get(0).activityInfo.name);
211 return intent;
212 }
213
214 @Override
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700215 public int[] getPackageGids(String packageName) throws NameNotFoundException {
216 return getPackageGids(packageName, 0);
217 }
218
219 @Override
220 public int[] getPackageGids(String packageName, int flags)
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800221 throws NameNotFoundException {
222 try {
Jeff Sharkeycd654482016-01-08 17:42:11 -0700223 int[] gids = mPM.getPackageGids(packageName, flags, mContext.getUserId());
Svetoslavc6d1c342015-02-26 14:44:43 -0800224 if (gids != null) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800225 return gids;
226 }
227 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700228 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800229 }
230
231 throw new NameNotFoundException(packageName);
232 }
233
234 @Override
Jeff Sharkey8588bc12016-01-06 16:47:42 -0700235 public int getPackageUid(String packageName, int flags) throws NameNotFoundException {
236 return getPackageUidAsUser(packageName, flags, mContext.getUserId());
237 }
238
239 @Override
240 public int getPackageUidAsUser(String packageName, int userId) throws NameNotFoundException {
241 return getPackageUidAsUser(packageName, 0, userId);
242 }
243
244 @Override
245 public int getPackageUidAsUser(String packageName, int flags, int userId)
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800246 throws NameNotFoundException {
247 try {
Jeff Sharkeycd654482016-01-08 17:42:11 -0700248 int uid = mPM.getPackageUid(packageName, flags, userId);
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800249 if (uid >= 0) {
250 return uid;
251 }
252 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700253 throw e.rethrowFromSystemServer();
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800254 }
255
256 throw new NameNotFoundException(packageName);
257 }
258
259 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800260 public PermissionInfo getPermissionInfo(String name, int flags)
261 throws NameNotFoundException {
262 try {
263 PermissionInfo pi = mPM.getPermissionInfo(name, flags);
264 if (pi != null) {
265 return pi;
266 }
267 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700268 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800269 }
270
271 throw new NameNotFoundException(name);
272 }
273
274 @Override
275 public List<PermissionInfo> queryPermissionsByGroup(String group, int flags)
276 throws NameNotFoundException {
277 try {
Jeff Sharkeyd5896632016-03-04 16:16:00 -0700278 List<PermissionInfo> pi = mPM.queryPermissionsByGroup(group, flags).getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800279 if (pi != null) {
280 return pi;
281 }
282 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700283 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800284 }
285
286 throw new NameNotFoundException(group);
287 }
288
289 @Override
290 public PermissionGroupInfo getPermissionGroupInfo(String name,
291 int flags) throws NameNotFoundException {
292 try {
293 PermissionGroupInfo pgi = mPM.getPermissionGroupInfo(name, flags);
294 if (pgi != null) {
295 return pgi;
296 }
297 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700298 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800299 }
300
301 throw new NameNotFoundException(name);
302 }
303
304 @Override
305 public List<PermissionGroupInfo> getAllPermissionGroups(int flags) {
306 try {
Jeff Sharkeyd5896632016-03-04 16:16:00 -0700307 return mPM.getAllPermissionGroups(flags).getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800308 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700309 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800310 }
311 }
312
313 @Override
314 public ApplicationInfo getApplicationInfo(String packageName, int flags)
315 throws NameNotFoundException {
Jeff Sharkeycd654482016-01-08 17:42:11 -0700316 return getApplicationInfoAsUser(packageName, flags, mContext.getUserId());
317 }
318
319 @Override
320 public ApplicationInfo getApplicationInfoAsUser(String packageName, int flags, int userId)
321 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800322 try {
Jeff Sharkeycd654482016-01-08 17:42:11 -0700323 ApplicationInfo ai = mPM.getApplicationInfo(packageName, flags, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800324 if (ai != null) {
Narayan Kamathcaa71192014-07-16 11:06:43 +0100325 // This is a temporary hack. Callers must use
326 // createPackageContext(packageName).getApplicationInfo() to
327 // get the right paths.
Tao Baic9a02372016-01-12 15:02:24 -0800328 return maybeAdjustApplicationInfo(ai);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800329 }
330 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700331 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800332 }
333
334 throw new NameNotFoundException(packageName);
335 }
336
Tao Baic9a02372016-01-12 15:02:24 -0800337 private static ApplicationInfo maybeAdjustApplicationInfo(ApplicationInfo info) {
Narayan Kamathcaa71192014-07-16 11:06:43 +0100338 // If we're dealing with a multi-arch application that has both
339 // 32 and 64 bit shared libraries, we might need to choose the secondary
340 // depending on what the current runtime's instruction set is.
341 if (info.primaryCpuAbi != null && info.secondaryCpuAbi != null) {
342 final String runtimeIsa = VMRuntime.getRuntime().vmInstructionSet();
jgu214741cd92014-12-17 17:23:29 -0500343
344 // Get the instruction set that the libraries of secondary Abi is supported.
345 // In presence of a native bridge this might be different than the one secondary Abi used.
346 String secondaryIsa = VMRuntime.getInstructionSet(info.secondaryCpuAbi);
347 final String secondaryDexCodeIsa = SystemProperties.get("ro.dalvik.vm.isa." + secondaryIsa);
348 secondaryIsa = secondaryDexCodeIsa.isEmpty() ? secondaryIsa : secondaryDexCodeIsa;
Narayan Kamathcaa71192014-07-16 11:06:43 +0100349
350 // If the runtimeIsa is the same as the primary isa, then we do nothing.
351 // Everything will be set up correctly because info.nativeLibraryDir will
352 // correspond to the right ISA.
353 if (runtimeIsa.equals(secondaryIsa)) {
Tao Baic9a02372016-01-12 15:02:24 -0800354 ApplicationInfo modified = new ApplicationInfo(info);
355 modified.nativeLibraryDir = info.secondaryNativeLibraryDir;
356 return modified;
Narayan Kamathcaa71192014-07-16 11:06:43 +0100357 }
358 }
Tao Baic9a02372016-01-12 15:02:24 -0800359 return info;
Narayan Kamathcaa71192014-07-16 11:06:43 +0100360 }
361
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800362 @Override
363 public ActivityInfo getActivityInfo(ComponentName className, int flags)
364 throws NameNotFoundException {
365 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700366 ActivityInfo ai = mPM.getActivityInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800367 if (ai != null) {
368 return ai;
369 }
370 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700371 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800372 }
373
374 throw new NameNotFoundException(className.toString());
375 }
376
377 @Override
378 public ActivityInfo getReceiverInfo(ComponentName className, int flags)
379 throws NameNotFoundException {
380 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700381 ActivityInfo ai = mPM.getReceiverInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800382 if (ai != null) {
383 return ai;
384 }
385 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700386 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800387 }
388
389 throw new NameNotFoundException(className.toString());
390 }
391
392 @Override
393 public ServiceInfo getServiceInfo(ComponentName className, int flags)
394 throws NameNotFoundException {
395 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700396 ServiceInfo si = mPM.getServiceInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800397 if (si != null) {
398 return si;
399 }
400 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700401 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800402 }
403
404 throw new NameNotFoundException(className.toString());
405 }
406
407 @Override
408 public ProviderInfo getProviderInfo(ComponentName className, int flags)
409 throws NameNotFoundException {
410 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700411 ProviderInfo pi = mPM.getProviderInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800412 if (pi != null) {
413 return pi;
414 }
415 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700416 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800417 }
418
419 throw new NameNotFoundException(className.toString());
420 }
421
422 @Override
423 public String[] getSystemSharedLibraryNames() {
424 try {
425 return mPM.getSystemSharedLibraryNames();
426 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700427 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800428 }
429 }
430
Svet Ganovd7b1f4112016-02-09 18:49:23 -0800431 /** @hide */
432 @Override
433 public @Nullable String getServicesSystemSharedLibraryPackageName() {
434 try {
435 return mPM.getServicesSystemSharedLibraryPackageName();
436 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700437 throw e.rethrowFromSystemServer();
Svet Ganovd7b1f4112016-02-09 18:49:23 -0800438 }
439 }
440
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800441 @Override
442 public FeatureInfo[] getSystemAvailableFeatures() {
443 try {
Jeff Sharkeyd5896632016-03-04 16:16:00 -0700444 final List<FeatureInfo> list = mPM.getSystemAvailableFeatures().getList();
445 final FeatureInfo[] res = new FeatureInfo[list.size()];
446 for (int i = 0; i < res.length; i++) {
447 res[i] = list.get(i);
448 }
449 return res;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800450 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700451 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800452 }
453 }
454
455 @Override
456 public boolean hasSystemFeature(String name) {
Jeff Sharkey115d2c12016-02-15 17:25:57 -0700457 return hasSystemFeature(name, 0);
458 }
459
460 @Override
461 public boolean hasSystemFeature(String name, int version) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800462 try {
Jeff Sharkey115d2c12016-02-15 17:25:57 -0700463 return mPM.hasSystemFeature(name, version);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800464 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700465 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800466 }
467 }
468
469 @Override
470 public int checkPermission(String permName, String pkgName) {
471 try {
Svetoslavc6d1c342015-02-26 14:44:43 -0800472 return mPM.checkPermission(permName, pkgName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800473 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700474 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800475 }
476 }
477
478 @Override
Svet Ganovad3b2972015-07-07 22:49:17 -0700479 public boolean isPermissionRevokedByPolicy(String permName, String pkgName) {
480 try {
481 return mPM.isPermissionRevokedByPolicy(permName, pkgName, mContext.getUserId());
482 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700483 throw e.rethrowFromSystemServer();
Svet Ganovad3b2972015-07-07 22:49:17 -0700484 }
485 }
486
Svet Ganovf1b7f202015-07-29 08:33:42 -0700487 /**
488 * @hide
489 */
490 @Override
491 public String getPermissionControllerPackageName() {
492 synchronized (mLock) {
493 if (mPermissionsControllerPackageName == null) {
494 try {
495 mPermissionsControllerPackageName = mPM.getPermissionControllerPackageName();
496 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700497 throw e.rethrowFromSystemServer();
Svet Ganovf1b7f202015-07-29 08:33:42 -0700498 }
499 }
500 return mPermissionsControllerPackageName;
501 }
502 }
503
Svet Ganovad3b2972015-07-07 22:49:17 -0700504 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800505 public boolean addPermission(PermissionInfo info) {
506 try {
507 return mPM.addPermission(info);
508 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700509 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800510 }
511 }
512
513 @Override
514 public boolean addPermissionAsync(PermissionInfo info) {
515 try {
516 return mPM.addPermissionAsync(info);
517 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700518 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800519 }
520 }
521
522 @Override
523 public void removePermission(String name) {
524 try {
525 mPM.removePermission(name);
526 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700527 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800528 }
529 }
530
531 @Override
Svet Ganov8c7f7002015-05-07 10:48:44 -0700532 public void grantRuntimePermission(String packageName, String permissionName,
533 UserHandle user) {
Dianne Hackborne639da72012-02-21 15:11:13 -0800534 try {
Svet Ganov8c7f7002015-05-07 10:48:44 -0700535 mPM.grantRuntimePermission(packageName, permissionName, user.getIdentifier());
Dianne Hackborne639da72012-02-21 15:11:13 -0800536 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700537 throw e.rethrowFromSystemServer();
Dianne Hackborne639da72012-02-21 15:11:13 -0800538 }
539 }
540
541 @Override
Svet Ganov8c7f7002015-05-07 10:48:44 -0700542 public void revokeRuntimePermission(String packageName, String permissionName,
543 UserHandle user) {
Dianne Hackborne639da72012-02-21 15:11:13 -0800544 try {
Svet Ganov8c7f7002015-05-07 10:48:44 -0700545 mPM.revokeRuntimePermission(packageName, permissionName, user.getIdentifier());
546 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700547 throw e.rethrowFromSystemServer();
Svet Ganov8c7f7002015-05-07 10:48:44 -0700548 }
549 }
550
551 @Override
552 public int getPermissionFlags(String permissionName, String packageName, UserHandle user) {
553 try {
554 return mPM.getPermissionFlags(permissionName, packageName, user.getIdentifier());
555 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700556 throw e.rethrowFromSystemServer();
Svet Ganov8c7f7002015-05-07 10:48:44 -0700557 }
558 }
559
560 @Override
561 public void updatePermissionFlags(String permissionName, String packageName,
562 int flagMask, int flagValues, UserHandle user) {
563 try {
564 mPM.updatePermissionFlags(permissionName, packageName, flagMask,
565 flagValues, user.getIdentifier());
Dianne Hackborne639da72012-02-21 15:11:13 -0800566 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700567 throw e.rethrowFromSystemServer();
Dianne Hackborne639da72012-02-21 15:11:13 -0800568 }
569 }
570
571 @Override
Svetoslav20770dd2015-05-29 15:43:04 -0700572 public boolean shouldShowRequestPermissionRationale(String permission) {
573 try {
574 return mPM.shouldShowRequestPermissionRationale(permission,
575 mContext.getPackageName(), mContext.getUserId());
576 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700577 throw e.rethrowFromSystemServer();
Svetoslav20770dd2015-05-29 15:43:04 -0700578 }
579 }
580
581 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800582 public int checkSignatures(String pkg1, String pkg2) {
583 try {
584 return mPM.checkSignatures(pkg1, pkg2);
585 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700586 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800587 }
588 }
589
590 @Override
591 public int checkSignatures(int uid1, int uid2) {
592 try {
593 return mPM.checkUidSignatures(uid1, uid2);
594 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700595 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800596 }
597 }
598
599 @Override
600 public String[] getPackagesForUid(int uid) {
601 try {
602 return mPM.getPackagesForUid(uid);
603 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700604 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800605 }
606 }
607
608 @Override
609 public String getNameForUid(int uid) {
610 try {
611 return mPM.getNameForUid(uid);
612 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700613 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800614 }
615 }
616
617 @Override
618 public int getUidForSharedUser(String sharedUserName)
619 throws NameNotFoundException {
620 try {
621 int uid = mPM.getUidForSharedUser(sharedUserName);
622 if(uid != -1) {
623 return uid;
624 }
625 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700626 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800627 }
628 throw new NameNotFoundException("No shared userid for user:"+sharedUserName);
629 }
630
Kenny Roote6cd0c72011-05-19 12:48:14 -0700631 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800632 @Override
633 public List<PackageInfo> getInstalledPackages(int flags) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700634 return getInstalledPackagesAsUser(flags, mContext.getUserId());
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700635 }
636
637 /** @hide */
638 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700639 public List<PackageInfo> getInstalledPackagesAsUser(int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800640 try {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800641 ParceledListSlice<PackageInfo> slice = mPM.getInstalledPackages(flags, userId);
642 return slice.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800643 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700644 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800645 }
646 }
647
Kenny Roote6cd0c72011-05-19 12:48:14 -0700648 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800649 @Override
Dianne Hackborne7991752013-01-16 17:56:46 -0800650 public List<PackageInfo> getPackagesHoldingPermissions(
651 String[] permissions, int flags) {
652 final int userId = mContext.getUserId();
653 try {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800654 ParceledListSlice<PackageInfo> slice = mPM.getPackagesHoldingPermissions(
655 permissions, flags, userId);
656 return slice.getList();
Dianne Hackborne7991752013-01-16 17:56:46 -0800657 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700658 throw e.rethrowFromSystemServer();
Dianne Hackborne7991752013-01-16 17:56:46 -0800659 }
660 }
661
662 @SuppressWarnings("unchecked")
663 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800664 public List<ApplicationInfo> getInstalledApplications(int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700665 final int userId = mContext.getUserId();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800666 try {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800667 ParceledListSlice<ApplicationInfo> slice = mPM.getInstalledApplications(flags, userId);
668 return slice.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800669 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700670 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800671 }
672 }
673
Svet Ganov2acf0632015-11-24 19:10:59 -0800674 /** @hide */
675 @SuppressWarnings("unchecked")
676 @Override
677 public List<EphemeralApplicationInfo> getEphemeralApplications() {
678 try {
679 ParceledListSlice<EphemeralApplicationInfo> slice =
680 mPM.getEphemeralApplications(mContext.getUserId());
681 if (slice != null) {
682 return slice.getList();
683 }
684 return Collections.emptyList();
685 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700686 throw e.rethrowFromSystemServer();
Svet Ganov2acf0632015-11-24 19:10:59 -0800687 }
688 }
689
690 /** @hide */
691 @Override
692 public Drawable getEphemeralApplicationIcon(String packageName) {
693 try {
694 Bitmap bitmap = mPM.getEphemeralApplicationIcon(
695 packageName, mContext.getUserId());
696 if (bitmap != null) {
697 return new BitmapDrawable(null, bitmap);
698 }
699 return null;
700 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700701 throw e.rethrowFromSystemServer();
Svet Ganov2acf0632015-11-24 19:10:59 -0800702 }
703 }
704
705 @Override
706 public boolean isEphemeralApplication() {
707 try {
708 return mPM.isEphemeralApplication(
709 mContext.getPackageName(), mContext.getUserId());
710 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700711 throw e.rethrowFromSystemServer();
Svet Ganov2acf0632015-11-24 19:10:59 -0800712 }
Svet Ganov2acf0632015-11-24 19:10:59 -0800713 }
714
715 @Override
716 public int getEphemeralCookieMaxSizeBytes() {
717 return Settings.Global.getInt(mContext.getContentResolver(),
718 Settings.Global.EPHEMERAL_COOKIE_MAX_SIZE_BYTES,
719 DEFAULT_EPHEMERAL_COOKIE_MAX_SIZE_BYTES);
720 }
721
722 @Override
723 public @NonNull byte[] getEphemeralCookie() {
724 try {
725 final byte[] cookie = mPM.getEphemeralApplicationCookie(
726 mContext.getPackageName(), mContext.getUserId());
727 if (cookie != null) {
728 return cookie;
Jeff Sharkey27b2e692016-02-25 17:40:12 -0700729 } else {
730 return EmptyArray.BYTE;
Svet Ganov2acf0632015-11-24 19:10:59 -0800731 }
732 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700733 throw e.rethrowFromSystemServer();
Svet Ganov2acf0632015-11-24 19:10:59 -0800734 }
Svet Ganov2acf0632015-11-24 19:10:59 -0800735 }
736
737 @Override
738 public boolean setEphemeralCookie(@NonNull byte[] cookie) {
739 try {
740 return mPM.setEphemeralApplicationCookie(
741 mContext.getPackageName(), cookie, mContext.getUserId());
742 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700743 throw e.rethrowFromSystemServer();
Svet Ganov2acf0632015-11-24 19:10:59 -0800744 }
Svet Ganov2acf0632015-11-24 19:10:59 -0800745 }
746
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800747 @Override
748 public ResolveInfo resolveActivity(Intent intent, int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700749 return resolveActivityAsUser(intent, flags, mContext.getUserId());
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700750 }
751
752 @Override
753 public ResolveInfo resolveActivityAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800754 try {
755 return mPM.resolveIntent(
756 intent,
757 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700758 flags,
759 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800760 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700761 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800762 }
763 }
764
765 @Override
766 public List<ResolveInfo> queryIntentActivities(Intent intent,
767 int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700768 return queryIntentActivitiesAsUser(intent, flags, mContext.getUserId());
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700769 }
770
771 /** @hide Same as above but for a specific user */
772 @Override
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700773 public List<ResolveInfo> queryIntentActivitiesAsUser(Intent intent,
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700774 int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800775 try {
776 return mPM.queryIntentActivities(
777 intent,
778 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700779 flags,
Jeff Sharkeyd5896632016-03-04 16:16:00 -0700780 userId).getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800781 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700782 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800783 }
784 }
785
786 @Override
787 public List<ResolveInfo> queryIntentActivityOptions(
788 ComponentName caller, Intent[] specifics, Intent intent,
789 int flags) {
790 final ContentResolver resolver = mContext.getContentResolver();
791
792 String[] specificTypes = null;
793 if (specifics != null) {
794 final int N = specifics.length;
795 for (int i=0; i<N; i++) {
796 Intent sp = specifics[i];
797 if (sp != null) {
798 String t = sp.resolveTypeIfNeeded(resolver);
799 if (t != null) {
800 if (specificTypes == null) {
801 specificTypes = new String[N];
802 }
803 specificTypes[i] = t;
804 }
805 }
806 }
807 }
808
809 try {
Jeff Sharkeyd5896632016-03-04 16:16:00 -0700810 return mPM
811 .queryIntentActivityOptions(caller, specifics, specificTypes, intent,
812 intent.resolveTypeIfNeeded(resolver), flags, mContext.getUserId())
813 .getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800814 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700815 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800816 }
817 }
818
Amith Yamasanif203aee2012-08-29 18:41:53 -0700819 /**
820 * @hide
821 */
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800822 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700823 public List<ResolveInfo> queryBroadcastReceiversAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800824 try {
825 return mPM.queryIntentReceivers(
826 intent,
827 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700828 flags,
Jeff Sharkeyd5896632016-03-04 16:16:00 -0700829 userId).getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800830 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700831 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800832 }
833 }
834
835 @Override
Amith Yamasanif203aee2012-08-29 18:41:53 -0700836 public List<ResolveInfo> queryBroadcastReceivers(Intent intent, int flags) {
Jeff Sharkeye06b4d12016-01-06 14:51:50 -0700837 return queryBroadcastReceiversAsUser(intent, flags, mContext.getUserId());
Amith Yamasanif203aee2012-08-29 18:41:53 -0700838 }
839
840 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800841 public ResolveInfo resolveService(Intent intent, int flags) {
842 try {
843 return mPM.resolveService(
844 intent,
845 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700846 flags,
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700847 mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800848 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700849 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800850 }
851 }
852
853 @Override
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700854 public List<ResolveInfo> queryIntentServicesAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800855 try {
856 return mPM.queryIntentServices(
857 intent,
858 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700859 flags,
Jeff Sharkeyd5896632016-03-04 16:16:00 -0700860 userId).getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800861 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700862 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800863 }
864 }
865
866 @Override
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700867 public List<ResolveInfo> queryIntentServices(Intent intent, int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700868 return queryIntentServicesAsUser(intent, flags, mContext.getUserId());
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700869 }
870
871 @Override
Jeff Sharkey85f5f812013-10-07 10:16:12 -0700872 public List<ResolveInfo> queryIntentContentProvidersAsUser(
873 Intent intent, int flags, int userId) {
874 try {
875 return mPM.queryIntentContentProviders(intent,
Jeff Sharkeyd5896632016-03-04 16:16:00 -0700876 intent.resolveTypeIfNeeded(mContext.getContentResolver()), flags, userId)
877 .getList();
Jeff Sharkey85f5f812013-10-07 10:16:12 -0700878 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700879 throw e.rethrowFromSystemServer();
Jeff Sharkey85f5f812013-10-07 10:16:12 -0700880 }
881 }
882
883 @Override
884 public List<ResolveInfo> queryIntentContentProviders(Intent intent, int flags) {
885 return queryIntentContentProvidersAsUser(intent, flags, mContext.getUserId());
886 }
887
888 @Override
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +0100889 public ProviderInfo resolveContentProvider(String name, int flags) {
890 return resolveContentProviderAsUser(name, flags, mContext.getUserId());
891 }
892
893 /** @hide **/
894 @Override
895 public ProviderInfo resolveContentProviderAsUser(String name, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800896 try {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +0100897 return mPM.resolveContentProvider(name, flags, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800898 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700899 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800900 }
901 }
902
903 @Override
904 public List<ProviderInfo> queryContentProviders(String processName,
905 int uid, int flags) {
906 try {
Dianne Hackborn28ec27c2015-08-03 15:28:28 -0700907 ParceledListSlice<ProviderInfo> slice
908 = mPM.queryContentProviders(processName, uid, flags);
909 return slice != null ? slice.getList() : null;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800910 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700911 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800912 }
913 }
914
915 @Override
916 public InstrumentationInfo getInstrumentationInfo(
917 ComponentName className, int flags)
918 throws NameNotFoundException {
919 try {
920 InstrumentationInfo ii = mPM.getInstrumentationInfo(
921 className, flags);
922 if (ii != null) {
923 return ii;
924 }
925 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700926 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800927 }
928
929 throw new NameNotFoundException(className.toString());
930 }
931
932 @Override
933 public List<InstrumentationInfo> queryInstrumentation(
934 String targetPackage, int flags) {
935 try {
Jeff Sharkeyd5896632016-03-04 16:16:00 -0700936 return mPM.queryInstrumentation(targetPackage, flags).getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800937 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700938 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800939 }
940 }
941
Alan Viveretteecd585a2015-04-13 10:32:51 -0700942 @Nullable
943 @Override
944 public Drawable getDrawable(String packageName, @DrawableRes int resId,
945 @Nullable ApplicationInfo appInfo) {
946 final ResourceName name = new ResourceName(packageName, resId);
947 final Drawable cachedIcon = getCachedIcon(name);
948 if (cachedIcon != null) {
949 return cachedIcon;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800950 }
Alan Viveretteecd585a2015-04-13 10:32:51 -0700951
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800952 if (appInfo == null) {
953 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700954 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800955 } catch (NameNotFoundException e) {
956 return null;
957 }
958 }
Alan Viveretteecd585a2015-04-13 10:32:51 -0700959
960 if (resId != 0) {
961 try {
962 final Resources r = getResourcesForApplication(appInfo);
963 final Drawable dr = r.getDrawable(resId, null);
964 if (dr != null) {
965 putCachedIcon(name, dr);
966 }
967
968 if (false) {
969 RuntimeException e = new RuntimeException("here");
970 e.fillInStackTrace();
971 Log.w(TAG, "Getting drawable 0x" + Integer.toHexString(resId)
972 + " from package " + packageName
973 + ": app scale=" + r.getCompatibilityInfo().applicationScale
974 + ", caller scale=" + mContext.getResources()
975 .getCompatibilityInfo().applicationScale,
976 e);
977 }
Ricky Wai3ce46252015-04-15 16:12:22 +0100978 if (DEBUG_ICONS) {
Alan Viveretteecd585a2015-04-13 10:32:51 -0700979 Log.v(TAG, "Getting drawable 0x"
980 + Integer.toHexString(resId) + " from " + r
981 + ": " + dr);
Ricky Wai3ce46252015-04-15 16:12:22 +0100982 }
983 return dr;
Alan Viveretteecd585a2015-04-13 10:32:51 -0700984 } catch (NameNotFoundException e) {
985 Log.w("PackageManager", "Failure retrieving resources for "
986 + appInfo.packageName);
987 } catch (Resources.NotFoundException e) {
988 Log.w("PackageManager", "Failure retrieving resources for "
989 + appInfo.packageName + ": " + e.getMessage());
990 } catch (Exception e) {
991 // If an exception was thrown, fall through to return
992 // default icon.
993 Log.w("PackageManager", "Failure retrieving icon 0x"
994 + Integer.toHexString(resId) + " in package "
995 + packageName, e);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800996 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800997 }
Alan Viveretteecd585a2015-04-13 10:32:51 -0700998
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800999 return null;
1000 }
1001
1002 @Override public Drawable getActivityIcon(ComponentName activityName)
1003 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001004 return getActivityInfo(activityName, sDefaultFlags).loadIcon(this);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001005 }
1006
1007 @Override public Drawable getActivityIcon(Intent intent)
1008 throws NameNotFoundException {
1009 if (intent.getComponent() != null) {
1010 return getActivityIcon(intent.getComponent());
1011 }
1012
1013 ResolveInfo info = resolveActivity(
1014 intent, PackageManager.MATCH_DEFAULT_ONLY);
1015 if (info != null) {
1016 return info.activityInfo.loadIcon(this);
1017 }
1018
Romain Guy39fe17c2011-11-30 10:34:07 -08001019 throw new NameNotFoundException(intent.toUri(0));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001020 }
1021
1022 @Override public Drawable getDefaultActivityIcon() {
1023 return Resources.getSystem().getDrawable(
1024 com.android.internal.R.drawable.sym_def_app_icon);
1025 }
1026
1027 @Override public Drawable getApplicationIcon(ApplicationInfo info) {
1028 return info.loadIcon(this);
1029 }
1030
1031 @Override public Drawable getApplicationIcon(String packageName)
1032 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001033 return getApplicationIcon(getApplicationInfo(packageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001034 }
1035
1036 @Override
Jose Limaf78e3122014-03-06 12:13:15 -08001037 public Drawable getActivityBanner(ComponentName activityName)
1038 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001039 return getActivityInfo(activityName, sDefaultFlags).loadBanner(this);
Jose Limaf78e3122014-03-06 12:13:15 -08001040 }
1041
1042 @Override
1043 public Drawable getActivityBanner(Intent intent)
1044 throws NameNotFoundException {
1045 if (intent.getComponent() != null) {
1046 return getActivityBanner(intent.getComponent());
1047 }
1048
1049 ResolveInfo info = resolveActivity(
1050 intent, PackageManager.MATCH_DEFAULT_ONLY);
1051 if (info != null) {
1052 return info.activityInfo.loadBanner(this);
1053 }
1054
1055 throw new NameNotFoundException(intent.toUri(0));
1056 }
1057
1058 @Override
1059 public Drawable getApplicationBanner(ApplicationInfo info) {
1060 return info.loadBanner(this);
1061 }
1062
1063 @Override
1064 public Drawable getApplicationBanner(String packageName)
1065 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001066 return getApplicationBanner(getApplicationInfo(packageName, sDefaultFlags));
Jose Limaf78e3122014-03-06 12:13:15 -08001067 }
1068
1069 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001070 public Drawable getActivityLogo(ComponentName activityName)
1071 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001072 return getActivityInfo(activityName, sDefaultFlags).loadLogo(this);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001073 }
1074
1075 @Override
1076 public Drawable getActivityLogo(Intent intent)
1077 throws NameNotFoundException {
1078 if (intent.getComponent() != null) {
1079 return getActivityLogo(intent.getComponent());
1080 }
1081
1082 ResolveInfo info = resolveActivity(
1083 intent, PackageManager.MATCH_DEFAULT_ONLY);
1084 if (info != null) {
1085 return info.activityInfo.loadLogo(this);
1086 }
1087
1088 throw new NameNotFoundException(intent.toUri(0));
1089 }
1090
1091 @Override
1092 public Drawable getApplicationLogo(ApplicationInfo info) {
1093 return info.loadLogo(this);
1094 }
1095
1096 @Override
1097 public Drawable getApplicationLogo(String packageName)
1098 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001099 return getApplicationLogo(getApplicationInfo(packageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001100 }
1101
Svetoslavc7d62f02014-09-04 15:39:54 -07001102 @Override
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08001103 public Drawable getManagedUserBadgedDrawable(Drawable drawable, Rect badgeLocation,
1104 int badgeDensity) {
1105 Drawable badgeDrawable = getDrawableForDensity(
1106 com.android.internal.R.drawable.ic_corp_badge, badgeDensity);
1107 return getBadgedDrawable(drawable, badgeDrawable, badgeLocation, true);
1108 }
1109
1110 @Override
Svetoslavc7d62f02014-09-04 15:39:54 -07001111 public Drawable getUserBadgedIcon(Drawable icon, UserHandle user) {
1112 final int badgeResId = getBadgeResIdForUser(user.getIdentifier());
1113 if (badgeResId == 0) {
1114 return icon;
1115 }
1116 Drawable badgeIcon = getDrawable("system", badgeResId, null);
1117 return getBadgedDrawable(icon, badgeIcon, null, true);
1118 }
1119
1120 @Override
1121 public Drawable getUserBadgedDrawableForDensity(Drawable drawable, UserHandle user,
1122 Rect badgeLocation, int badgeDensity) {
1123 Drawable badgeDrawable = getUserBadgeForDensity(user, badgeDensity);
1124 if (badgeDrawable == null) {
1125 return drawable;
1126 }
1127 return getBadgedDrawable(drawable, badgeDrawable, badgeLocation, true);
1128 }
1129
1130 @Override
1131 public Drawable getUserBadgeForDensity(UserHandle user, int density) {
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08001132 return getManagedProfileIconForDensity(user, com.android.internal.R.drawable.ic_corp_badge,
1133 density);
Selim Cineke6ff9462016-01-15 15:07:06 -08001134 }
1135
1136 @Override
1137 public Drawable getUserBadgeForDensityNoBackground(UserHandle user, int density) {
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08001138 return getManagedProfileIconForDensity(user,
1139 com.android.internal.R.drawable.ic_corp_badge_no_background, density);
Selim Cineke6ff9462016-01-15 15:07:06 -08001140 }
1141
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08001142 private Drawable getDrawableForDensity(int drawableId, int density) {
1143 if (density <= 0) {
1144 density = mContext.getResources().getDisplayMetrics().densityDpi;
1145 }
1146 return Resources.getSystem().getDrawableForDensity(drawableId, density);
1147 }
1148
1149 private Drawable getManagedProfileIconForDensity(UserHandle user, int drawableId, int density) {
Svetoslavc7d62f02014-09-04 15:39:54 -07001150 UserInfo userInfo = getUserIfProfile(user.getIdentifier());
1151 if (userInfo != null && userInfo.isManagedProfile()) {
Vadim Tryshev66ae66a2016-02-18 15:41:21 -08001152 return getDrawableForDensity(drawableId, density);
Svetoslavc7d62f02014-09-04 15:39:54 -07001153 }
1154 return null;
1155 }
1156
1157 @Override
1158 public CharSequence getUserBadgedLabel(CharSequence label, UserHandle user) {
1159 UserInfo userInfo = getUserIfProfile(user.getIdentifier());
1160 if (userInfo != null && userInfo.isManagedProfile()) {
1161 return Resources.getSystem().getString(
1162 com.android.internal.R.string.managed_profile_label_badge, label);
1163 }
1164 return label;
1165 }
1166
Alan Viveretteecd585a2015-04-13 10:32:51 -07001167 @Override
1168 public Resources getResourcesForActivity(ComponentName activityName)
1169 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001170 return getResourcesForApplication(
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001171 getActivityInfo(activityName, sDefaultFlags).applicationInfo);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001172 }
1173
Alan Viveretteecd585a2015-04-13 10:32:51 -07001174 @Override
1175 public Resources getResourcesForApplication(@NonNull ApplicationInfo app)
1176 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001177 if (app.packageName.equals("system")) {
1178 return mContext.mMainThread.getSystemContext().getResources();
1179 }
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07001180 final boolean sameUid = (app.uid == Process.myUid());
Alan Viveretteecd585a2015-04-13 10:32:51 -07001181 final Resources r = mContext.mMainThread.getTopLevelResources(
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07001182 sameUid ? app.sourceDir : app.publicSourceDir,
1183 sameUid ? app.splitSourceDirs : app.splitPublicSourceDirs,
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001184 app.resourceDirs, app.sharedLibraryFiles, Display.DEFAULT_DISPLAY,
Adam Lesinski082614c2016-03-04 14:33:47 -08001185 mContext.mPackageInfo);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001186 if (r != null) {
1187 return r;
1188 }
1189 throw new NameNotFoundException("Unable to open " + app.publicSourceDir);
1190 }
1191
Alan Viveretteecd585a2015-04-13 10:32:51 -07001192 @Override
1193 public Resources getResourcesForApplication(String appPackageName)
1194 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001195 return getResourcesForApplication(
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001196 getApplicationInfo(appPackageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001197 }
1198
Amith Yamasani98edc952012-09-25 14:09:27 -07001199 /** @hide */
1200 @Override
1201 public Resources getResourcesForApplicationAsUser(String appPackageName, int userId)
1202 throws NameNotFoundException {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001203 if (userId < 0) {
1204 throw new IllegalArgumentException(
1205 "Call does not support special user #" + userId);
1206 }
1207 if ("system".equals(appPackageName)) {
1208 return mContext.mMainThread.getSystemContext().getResources();
1209 }
Amith Yamasani98edc952012-09-25 14:09:27 -07001210 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001211 ApplicationInfo ai = mPM.getApplicationInfo(appPackageName, sDefaultFlags, userId);
Amith Yamasani98edc952012-09-25 14:09:27 -07001212 if (ai != null) {
1213 return getResourcesForApplication(ai);
1214 }
1215 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001216 throw e.rethrowFromSystemServer();
Amith Yamasani98edc952012-09-25 14:09:27 -07001217 }
1218 throw new NameNotFoundException("Package " + appPackageName + " doesn't exist");
1219 }
1220
Jeff Sharkeycd654482016-01-08 17:42:11 -07001221 volatile int mCachedSafeMode = -1;
1222
1223 @Override
1224 public boolean isSafeMode() {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001225 try {
1226 if (mCachedSafeMode < 0) {
1227 mCachedSafeMode = mPM.isSafeMode() ? 1 : 0;
1228 }
1229 return mCachedSafeMode != 0;
1230 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001231 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001232 }
1233 }
1234
Svetoslavf7c06eb2015-06-10 18:43:22 -07001235 @Override
1236 public void addOnPermissionsChangeListener(OnPermissionsChangedListener listener) {
1237 synchronized (mPermissionListeners) {
1238 if (mPermissionListeners.get(listener) != null) {
1239 return;
1240 }
1241 OnPermissionsChangeListenerDelegate delegate =
1242 new OnPermissionsChangeListenerDelegate(listener, Looper.getMainLooper());
1243 try {
1244 mPM.addOnPermissionsChangeListener(delegate);
1245 mPermissionListeners.put(listener, delegate);
1246 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001247 throw e.rethrowFromSystemServer();
Svetoslavf7c06eb2015-06-10 18:43:22 -07001248 }
1249 }
1250 }
1251
1252 @Override
1253 public void removeOnPermissionsChangeListener(OnPermissionsChangedListener listener) {
1254 synchronized (mPermissionListeners) {
1255 IOnPermissionsChangeListener delegate = mPermissionListeners.get(listener);
1256 if (delegate != null) {
1257 try {
1258 mPM.removeOnPermissionsChangeListener(delegate);
1259 mPermissionListeners.remove(listener);
1260 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001261 throw e.rethrowFromSystemServer();
Svetoslavf7c06eb2015-06-10 18:43:22 -07001262 }
1263 }
1264 }
1265 }
1266
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001267 static void configurationChanged() {
1268 synchronized (sSync) {
1269 sIconCache.clear();
1270 sStringCache.clear();
1271 }
1272 }
1273
1274 ApplicationPackageManager(ContextImpl context,
1275 IPackageManager pm) {
1276 mContext = context;
1277 mPM = pm;
1278 }
1279
Alan Viveretteecd585a2015-04-13 10:32:51 -07001280 @Nullable
1281 private Drawable getCachedIcon(@NonNull ResourceName name) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001282 synchronized (sSync) {
Alan Viveretteecd585a2015-04-13 10:32:51 -07001283 final WeakReference<Drawable.ConstantState> wr = sIconCache.get(name);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001284 if (DEBUG_ICONS) Log.v(TAG, "Get cached weak drawable ref for "
1285 + name + ": " + wr);
1286 if (wr != null) { // we have the activity
Alan Viveretteecd585a2015-04-13 10:32:51 -07001287 final Drawable.ConstantState state = wr.get();
Romain Guy39fe17c2011-11-30 10:34:07 -08001288 if (state != null) {
1289 if (DEBUG_ICONS) {
1290 Log.v(TAG, "Get cached drawable state for " + name + ": " + state);
1291 }
1292 // Note: It's okay here to not use the newDrawable(Resources) variant
1293 // of the API. The ConstantState comes from a drawable that was
1294 // originally created by passing the proper app Resources instance
1295 // which means the state should already contain the proper
1296 // resources specific information (like density.) See
1297 // BitmapDrawable.BitmapState for instance.
1298 return state.newDrawable();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001299 }
1300 // our entry has been purged
1301 sIconCache.remove(name);
1302 }
1303 }
1304 return null;
1305 }
1306
Alan Viveretteecd585a2015-04-13 10:32:51 -07001307 private void putCachedIcon(@NonNull ResourceName name, @NonNull Drawable dr) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001308 synchronized (sSync) {
Alan Viveretteecd585a2015-04-13 10:32:51 -07001309 sIconCache.put(name, new WeakReference<>(dr.getConstantState()));
Romain Guy39fe17c2011-11-30 10:34:07 -08001310 if (DEBUG_ICONS) Log.v(TAG, "Added cached drawable state for " + name + ": " + dr);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001311 }
1312 }
1313
Romain Guy39fe17c2011-11-30 10:34:07 -08001314 static void handlePackageBroadcast(int cmd, String[] pkgList, boolean hasPkgInfo) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001315 boolean immediateGc = false;
1316 if (cmd == IApplicationThread.EXTERNAL_STORAGE_UNAVAILABLE) {
1317 immediateGc = true;
1318 }
1319 if (pkgList != null && (pkgList.length > 0)) {
1320 boolean needCleanup = false;
1321 for (String ssp : pkgList) {
1322 synchronized (sSync) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07001323 for (int i=sIconCache.size()-1; i>=0; i--) {
1324 ResourceName nm = sIconCache.keyAt(i);
1325 if (nm.packageName.equals(ssp)) {
1326 //Log.i(TAG, "Removing cached drawable for " + nm);
1327 sIconCache.removeAt(i);
1328 needCleanup = true;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001329 }
1330 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07001331 for (int i=sStringCache.size()-1; i>=0; i--) {
1332 ResourceName nm = sStringCache.keyAt(i);
1333 if (nm.packageName.equals(ssp)) {
1334 //Log.i(TAG, "Removing cached string for " + nm);
1335 sStringCache.removeAt(i);
1336 needCleanup = true;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001337 }
1338 }
1339 }
1340 }
1341 if (needCleanup || hasPkgInfo) {
1342 if (immediateGc) {
1343 // Schedule an immediate gc.
1344 Runtime.getRuntime().gc();
1345 } else {
1346 ActivityThread.currentActivityThread().scheduleGcIdler();
1347 }
1348 }
1349 }
1350 }
1351
1352 private static final class ResourceName {
1353 final String packageName;
1354 final int iconId;
1355
1356 ResourceName(String _packageName, int _iconId) {
1357 packageName = _packageName;
1358 iconId = _iconId;
1359 }
1360
1361 ResourceName(ApplicationInfo aInfo, int _iconId) {
1362 this(aInfo.packageName, _iconId);
1363 }
1364
1365 ResourceName(ComponentInfo cInfo, int _iconId) {
1366 this(cInfo.applicationInfo.packageName, _iconId);
1367 }
1368
1369 ResourceName(ResolveInfo rInfo, int _iconId) {
1370 this(rInfo.activityInfo.applicationInfo.packageName, _iconId);
1371 }
1372
1373 @Override
1374 public boolean equals(Object o) {
1375 if (this == o) return true;
1376 if (o == null || getClass() != o.getClass()) return false;
1377
1378 ResourceName that = (ResourceName) o;
1379
1380 if (iconId != that.iconId) return false;
1381 return !(packageName != null ?
1382 !packageName.equals(that.packageName) : that.packageName != null);
1383
1384 }
1385
1386 @Override
1387 public int hashCode() {
1388 int result;
1389 result = packageName.hashCode();
1390 result = 31 * result + iconId;
1391 return result;
1392 }
1393
1394 @Override
1395 public String toString() {
1396 return "{ResourceName " + packageName + " / " + iconId + "}";
1397 }
1398 }
1399
1400 private CharSequence getCachedString(ResourceName name) {
1401 synchronized (sSync) {
1402 WeakReference<CharSequence> wr = sStringCache.get(name);
1403 if (wr != null) { // we have the activity
1404 CharSequence cs = wr.get();
1405 if (cs != null) {
1406 return cs;
1407 }
1408 // our entry has been purged
1409 sStringCache.remove(name);
1410 }
1411 }
1412 return null;
1413 }
1414
1415 private void putCachedString(ResourceName name, CharSequence cs) {
1416 synchronized (sSync) {
1417 sStringCache.put(name, new WeakReference<CharSequence>(cs));
1418 }
1419 }
1420
1421 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07001422 public CharSequence getText(String packageName, @StringRes int resid,
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001423 ApplicationInfo appInfo) {
1424 ResourceName name = new ResourceName(packageName, resid);
1425 CharSequence text = getCachedString(name);
1426 if (text != null) {
1427 return text;
1428 }
1429 if (appInfo == null) {
1430 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001431 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001432 } catch (NameNotFoundException e) {
1433 return null;
1434 }
1435 }
1436 try {
1437 Resources r = getResourcesForApplication(appInfo);
1438 text = r.getText(resid);
1439 putCachedString(name, text);
1440 return text;
1441 } catch (NameNotFoundException e) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001442 Log.w("PackageManager", "Failure retrieving resources for "
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001443 + appInfo.packageName);
1444 } catch (RuntimeException e) {
1445 // If an exception was thrown, fall through to return
1446 // default icon.
1447 Log.w("PackageManager", "Failure retrieving text 0x"
1448 + Integer.toHexString(resid) + " in package "
1449 + packageName, e);
1450 }
1451 return null;
1452 }
1453
1454 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07001455 public XmlResourceParser getXml(String packageName, @XmlRes int resid,
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001456 ApplicationInfo appInfo) {
1457 if (appInfo == null) {
1458 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001459 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001460 } catch (NameNotFoundException e) {
1461 return null;
1462 }
1463 }
1464 try {
1465 Resources r = getResourcesForApplication(appInfo);
1466 return r.getXml(resid);
1467 } catch (RuntimeException e) {
1468 // If an exception was thrown, fall through to return
1469 // default icon.
1470 Log.w("PackageManager", "Failure retrieving xml 0x"
1471 + Integer.toHexString(resid) + " in package "
1472 + packageName, e);
1473 } catch (NameNotFoundException e) {
Alon Albert3fa51e32010-11-11 09:24:04 -08001474 Log.w("PackageManager", "Failure retrieving resources for "
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001475 + appInfo.packageName);
1476 }
1477 return null;
1478 }
1479
1480 @Override
1481 public CharSequence getApplicationLabel(ApplicationInfo info) {
1482 return info.loadLabel(this);
1483 }
1484
1485 @Override
1486 public void installPackage(Uri packageURI, IPackageInstallObserver observer, int flags,
1487 String installerPackageName) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001488 installCommon(packageURI, new LegacyPackageInstallObserver(observer), flags,
Todd Kennedya6793232016-02-24 22:46:00 +00001489 installerPackageName, mContext.getUserId());
Christopher Tatef1977b42014-03-24 16:25:51 -07001490 }
1491
Christopher Tatef1977b42014-03-24 16:25:51 -07001492 @Override
1493 public void installPackage(Uri packageURI, PackageInstallObserver observer,
1494 int flags, String installerPackageName) {
Todd Kennedya6793232016-02-24 22:46:00 +00001495 installCommon(packageURI, observer, flags, installerPackageName, mContext.getUserId());
Jeff Sharkey513a0742014-07-08 17:10:32 -07001496 }
1497
1498 private void installCommon(Uri packageURI,
1499 PackageInstallObserver observer, int flags, String installerPackageName,
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001500 int userId) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001501 if (!"file".equals(packageURI.getScheme())) {
1502 throw new UnsupportedOperationException("Only file:// URIs are supported");
1503 }
Jeff Sharkey513a0742014-07-08 17:10:32 -07001504
1505 final String originPath = packageURI.getPath();
Christopher Tatef1977b42014-03-24 16:25:51 -07001506 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001507 mPM.installPackageAsUser(originPath, observer.getBinder(), flags, installerPackageName,
Todd Kennedya6793232016-02-24 22:46:00 +00001508 userId);
Jeff Sharkey27b2e692016-02-25 17:40:12 -07001509 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001510 throw e.rethrowFromSystemServer();
rich cannings706e8ba2012-08-20 13:20:14 -07001511 }
1512 }
1513
1514 @Override
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001515 public int installExistingPackage(String packageName) throws NameNotFoundException {
Robin Lee0e27c872015-09-28 14:37:40 +01001516 return installExistingPackageAsUser(packageName, mContext.getUserId());
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001517 }
1518
1519 @Override
1520 public int installExistingPackageAsUser(String packageName, int userId)
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001521 throws NameNotFoundException {
1522 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001523 int res = mPM.installExistingPackageAsUser(packageName, userId);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001524 if (res == INSTALL_FAILED_INVALID_URI) {
1525 throw new NameNotFoundException("Package " + packageName + " doesn't exist");
1526 }
1527 return res;
1528 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001529 throw e.rethrowFromSystemServer();
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001530 }
1531 }
1532
1533 @Override
Kenny Root3a9b5fb2011-09-20 14:15:38 -07001534 public void verifyPendingInstall(int id, int response) {
Kenny Root5ab21572011-07-27 11:11:19 -07001535 try {
Kenny Root3a9b5fb2011-09-20 14:15:38 -07001536 mPM.verifyPendingInstall(id, response);
Kenny Root5ab21572011-07-27 11:11:19 -07001537 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001538 throw e.rethrowFromSystemServer();
Kenny Root5ab21572011-07-27 11:11:19 -07001539 }
1540 }
1541
1542 @Override
rich canningsd9ef3e52012-08-22 14:28:05 -07001543 public void extendVerificationTimeout(int id, int verificationCodeAtTimeout,
1544 long millisecondsToDelay) {
1545 try {
1546 mPM.extendVerificationTimeout(id, verificationCodeAtTimeout, millisecondsToDelay);
1547 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001548 throw e.rethrowFromSystemServer();
rich canningsd9ef3e52012-08-22 14:28:05 -07001549 }
1550 }
1551
1552 @Override
Todd Kennedydfa93ab2016-03-03 15:24:33 -08001553 public void verifyIntentFilter(int id, int verificationCode, List<String> failedDomains) {
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001554 try {
Todd Kennedydfa93ab2016-03-03 15:24:33 -08001555 mPM.verifyIntentFilter(id, verificationCode, failedDomains);
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001556 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001557 throw e.rethrowFromSystemServer();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001558 }
1559 }
1560
1561 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001562 public int getIntentVerificationStatusAsUser(String packageName, int userId) {
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001563 try {
1564 return mPM.getIntentVerificationStatus(packageName, userId);
1565 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001566 throw e.rethrowFromSystemServer();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001567 }
1568 }
1569
1570 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001571 public boolean updateIntentVerificationStatusAsUser(String packageName, int status, int userId) {
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001572 try {
1573 return mPM.updateIntentVerificationStatus(packageName, status, userId);
1574 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001575 throw e.rethrowFromSystemServer();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001576 }
1577 }
1578
1579 @Override
1580 public List<IntentFilterVerificationInfo> getIntentFilterVerifications(String packageName) {
1581 try {
Jeff Sharkeyd5896632016-03-04 16:16:00 -07001582 return mPM.getIntentFilterVerifications(packageName).getList();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001583 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001584 throw e.rethrowFromSystemServer();
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001585 }
1586 }
1587
1588 @Override
Fabrice Di Meglio07885952015-04-06 19:41:28 -07001589 public List<IntentFilter> getAllIntentFilters(String packageName) {
1590 try {
Jeff Sharkeyd5896632016-03-04 16:16:00 -07001591 return mPM.getAllIntentFilters(packageName).getList();
Fabrice Di Meglio07885952015-04-06 19:41:28 -07001592 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001593 throw e.rethrowFromSystemServer();
Fabrice Di Meglio07885952015-04-06 19:41:28 -07001594 }
1595 }
1596
1597 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001598 public String getDefaultBrowserPackageNameAsUser(int userId) {
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001599 try {
1600 return mPM.getDefaultBrowserPackageName(userId);
1601 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001602 throw e.rethrowFromSystemServer();
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001603 }
1604 }
1605
1606 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001607 public boolean setDefaultBrowserPackageNameAsUser(String packageName, int userId) {
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001608 try {
1609 return mPM.setDefaultBrowserPackageName(packageName, userId);
1610 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001611 throw e.rethrowFromSystemServer();
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001612 }
1613 }
1614
1615 @Override
Dianne Hackborn880119b2010-11-18 22:26:40 -08001616 public void setInstallerPackageName(String targetPackage,
1617 String installerPackageName) {
1618 try {
1619 mPM.setInstallerPackageName(targetPackage, installerPackageName);
1620 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001621 throw e.rethrowFromSystemServer();
Dianne Hackborn880119b2010-11-18 22:26:40 -08001622 }
1623 }
1624
1625 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001626 public String getInstallerPackageName(String packageName) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001627 try {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001628 return mPM.getInstallerPackageName(packageName);
1629 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001630 throw e.rethrowFromSystemServer();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001631 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001632 }
1633
1634 @Override
1635 public int getMoveStatus(int moveId) {
1636 try {
1637 return mPM.getMoveStatus(moveId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001638 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001639 throw e.rethrowFromSystemServer();
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -07001640 }
1641 }
1642
1643 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001644 public void registerMoveCallback(MoveCallback callback, Handler handler) {
1645 synchronized (mDelegates) {
1646 final MoveCallbackDelegate delegate = new MoveCallbackDelegate(callback,
1647 handler.getLooper());
1648 try {
1649 mPM.registerMoveCallback(delegate);
1650 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001651 throw e.rethrowFromSystemServer();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001652 }
1653 mDelegates.add(delegate);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001654 }
1655 }
1656
1657 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001658 public void unregisterMoveCallback(MoveCallback callback) {
1659 synchronized (mDelegates) {
1660 for (Iterator<MoveCallbackDelegate> i = mDelegates.iterator(); i.hasNext();) {
1661 final MoveCallbackDelegate delegate = i.next();
1662 if (delegate.mCallback == callback) {
1663 try {
1664 mPM.unregisterMoveCallback(delegate);
1665 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001666 throw e.rethrowFromSystemServer();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001667 }
1668 i.remove();
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001669 }
1670 }
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001671 }
1672 }
1673
1674 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001675 public int movePackage(String packageName, VolumeInfo vol) {
1676 try {
1677 final String volumeUuid;
1678 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.id)) {
1679 volumeUuid = StorageManager.UUID_PRIVATE_INTERNAL;
1680 } else if (vol.isPrimaryPhysical()) {
1681 volumeUuid = StorageManager.UUID_PRIMARY_PHYSICAL;
1682 } else {
1683 volumeUuid = Preconditions.checkNotNull(vol.fsUuid);
1684 }
1685
1686 return mPM.movePackage(packageName, volumeUuid);
1687 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001688 throw e.rethrowFromSystemServer();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001689 }
1690 }
1691
1692 @Override
1693 public @Nullable VolumeInfo getPackageCurrentVolume(ApplicationInfo app) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001694 final StorageManager storage = mContext.getSystemService(StorageManager.class);
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001695 if (app.isInternal()) {
1696 return storage.findVolumeById(VolumeInfo.ID_PRIVATE_INTERNAL);
1697 } else if (app.isExternalAsec()) {
1698 return storage.getPrimaryPhysicalVolume();
1699 } else {
1700 return storage.findVolumeByUuid(app.volumeUuid);
1701 }
1702 }
1703
1704 @Override
1705 public @NonNull List<VolumeInfo> getPackageCandidateVolumes(ApplicationInfo app) {
1706 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1707 final VolumeInfo currentVol = getPackageCurrentVolume(app);
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001708 final List<VolumeInfo> vols = storage.getVolumes();
1709 final List<VolumeInfo> candidates = new ArrayList<>();
1710 for (VolumeInfo vol : vols) {
Todd Kennedyf39ca8f2015-08-07 14:15:07 -07001711 if (Objects.equals(vol, currentVol) || isPackageCandidateVolume(mContext, app, vol)) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001712 candidates.add(vol);
1713 }
1714 }
1715 return candidates;
1716 }
1717
Makoto Onukif34db0a2016-02-17 11:17:15 -08001718 private boolean isPackageCandidateVolume(
Todd Kennedyf39ca8f2015-08-07 14:15:07 -07001719 ContextImpl context, ApplicationInfo app, VolumeInfo vol) {
1720 final boolean forceAllowOnExternal = Settings.Global.getInt(
1721 context.getContentResolver(), Settings.Global.FORCE_ALLOW_ON_EXTERNAL, 0) != 0;
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001722 // Private internal is always an option
1723 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.getId())) {
1724 return true;
1725 }
1726
1727 // System apps and apps demanding internal storage can't be moved
1728 // anywhere else
Todd Kennedyf39ca8f2015-08-07 14:15:07 -07001729 if (app.isSystemApp()) {
1730 return false;
1731 }
1732 if (!forceAllowOnExternal
Dianne Hackborn30a4e6d2015-10-12 17:14:56 -07001733 && (app.installLocation == PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY
1734 || app.installLocation == PackageInfo.INSTALL_LOCATION_UNSPECIFIED)) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001735 return false;
1736 }
1737
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001738 // Gotta be able to write there
1739 if (!vol.isMountedWritable()) {
1740 return false;
1741 }
1742
1743 // Moving into an ASEC on public primary is only option internal
1744 if (vol.isPrimaryPhysical()) {
1745 return app.isInternal();
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001746 }
1747
Makoto Onukif34db0a2016-02-17 11:17:15 -08001748 // Some apps can't be moved. (e.g. device admins)
1749 try {
1750 if (mPM.isPackageDeviceAdminOnAnyUser(app.packageName)) {
1751 return false;
1752 }
1753 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001754 throw e.rethrowFromSystemServer();
Makoto Onukif34db0a2016-02-17 11:17:15 -08001755 }
1756
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001757 // Otherwise we can move to any private volume
1758 return (vol.getType() == VolumeInfo.TYPE_PRIVATE);
1759 }
1760
1761 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001762 public int movePrimaryStorage(VolumeInfo vol) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001763 try {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001764 final String volumeUuid;
1765 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.id)) {
1766 volumeUuid = StorageManager.UUID_PRIVATE_INTERNAL;
1767 } else if (vol.isPrimaryPhysical()) {
1768 volumeUuid = StorageManager.UUID_PRIMARY_PHYSICAL;
1769 } else {
1770 volumeUuid = Preconditions.checkNotNull(vol.fsUuid);
1771 }
1772
1773 return mPM.movePrimaryStorage(volumeUuid);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001774 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001775 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001776 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001777 }
1778
Jeff Sharkey275e3e42015-04-24 16:10:32 -07001779 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001780 public @Nullable VolumeInfo getPrimaryStorageCurrentVolume() {
1781 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1782 final String volumeUuid = storage.getPrimaryStorageUuid();
Jeff Sharkey50a05452015-04-29 11:24:52 -07001783 return storage.findVolumeByQualifiedUuid(volumeUuid);
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001784 }
1785
Jeff Sharkey275e3e42015-04-24 16:10:32 -07001786 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001787 public @NonNull List<VolumeInfo> getPrimaryStorageCandidateVolumes() {
1788 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1789 final VolumeInfo currentVol = getPrimaryStorageCurrentVolume();
1790 final List<VolumeInfo> vols = storage.getVolumes();
1791 final List<VolumeInfo> candidates = new ArrayList<>();
Jeff Sharkeyfced5342015-05-10 14:53:34 -07001792 if (Objects.equals(StorageManager.UUID_PRIMARY_PHYSICAL,
1793 storage.getPrimaryStorageUuid()) && currentVol != null) {
1794 // TODO: support moving primary physical to emulated volume
1795 candidates.add(currentVol);
1796 } else {
1797 for (VolumeInfo vol : vols) {
1798 if (Objects.equals(vol, currentVol) || isPrimaryStorageCandidateVolume(vol)) {
1799 candidates.add(vol);
1800 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001801 }
1802 }
1803 return candidates;
1804 }
1805
1806 private static boolean isPrimaryStorageCandidateVolume(VolumeInfo vol) {
1807 // Private internal is always an option
1808 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.getId())) {
1809 return true;
1810 }
1811
1812 // Gotta be able to write there
1813 if (!vol.isMountedWritable()) {
1814 return false;
1815 }
1816
Jeff Sharkeyfced5342015-05-10 14:53:34 -07001817 // We can move to any private volume
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001818 return (vol.getType() == VolumeInfo.TYPE_PRIVATE);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001819 }
1820
1821 @Override
1822 public void deletePackage(String packageName, IPackageDeleteObserver observer, int flags) {
Robin Lee0e27c872015-09-28 14:37:40 +01001823 deletePackageAsUser(packageName, observer, flags, mContext.getUserId());
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001824 }
1825
1826 @Override
1827 public void deletePackageAsUser(String packageName, IPackageDeleteObserver observer, int flags,
1828 int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001829 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001830 mPM.deletePackageAsUser(packageName, observer, userId, flags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001831 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001832 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001833 }
1834 }
Jeff Sharkeyfbd0e9f2014-08-06 16:34:34 -07001835
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001836 @Override
1837 public void clearApplicationUserData(String packageName,
1838 IPackageDataObserver observer) {
1839 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001840 mPM.clearApplicationUserData(packageName, observer, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001841 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001842 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001843 }
1844 }
1845 @Override
1846 public void deleteApplicationCacheFiles(String packageName,
1847 IPackageDataObserver observer) {
1848 try {
1849 mPM.deleteApplicationCacheFiles(packageName, observer);
1850 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001851 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001852 }
1853 }
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001854
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001855 @Override
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001856 public void freeStorageAndNotify(String volumeUuid, long idealStorageSize,
1857 IPackageDataObserver observer) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001858 try {
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001859 mPM.freeStorageAndNotify(volumeUuid, idealStorageSize, observer);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001860 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001861 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001862 }
1863 }
1864
1865 @Override
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001866 public void freeStorage(String volumeUuid, long freeStorageSize, IntentSender pi) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001867 try {
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001868 mPM.freeStorage(volumeUuid, freeStorageSize, pi);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001869 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001870 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001871 }
1872 }
1873
1874 @Override
Andrei Stingaceanueb84b182016-01-26 18:39:55 +00001875 public String[] setPackagesSuspendedAsUser(String[] packageNames, boolean suspended,
1876 int userId) {
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00001877 try {
Andrei Stingaceanueb84b182016-01-26 18:39:55 +00001878 return mPM.setPackagesSuspendedAsUser(packageNames, suspended, userId);
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00001879 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001880 throw e.rethrowFromSystemServer();
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00001881 }
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00001882 }
1883
1884 @Override
Andrei Stingaceanu355b2322016-02-12 16:43:51 +00001885 public boolean isPackageSuspendedForUser(String packageName, int userId) {
1886 try {
1887 return mPM.isPackageSuspendedForUser(packageName, userId);
1888 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001889 throw e.rethrowFromSystemServer();
Andrei Stingaceanu355b2322016-02-12 16:43:51 +00001890 }
Andrei Stingaceanu355b2322016-02-12 16:43:51 +00001891 }
1892
1893 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001894 public void getPackageSizeInfoAsUser(String packageName, int userHandle,
Dianne Hackborn0c380492012-08-20 17:23:30 -07001895 IPackageStatsObserver observer) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001896 try {
Dianne Hackborn0c380492012-08-20 17:23:30 -07001897 mPM.getPackageSizeInfo(packageName, userHandle, observer);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001898 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001899 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001900 }
1901 }
Jeff Sharkeyd5896632016-03-04 16:16:00 -07001902
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001903 @Override
1904 public void addPackageToPreferred(String packageName) {
Jeff Sharkeyd5896632016-03-04 16:16:00 -07001905 Log.w(TAG, "addPackageToPreferred() is a no-op");
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001906 }
1907
1908 @Override
1909 public void removePackageFromPreferred(String packageName) {
Jeff Sharkeyd5896632016-03-04 16:16:00 -07001910 Log.w(TAG, "removePackageFromPreferred() is a no-op");
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001911 }
1912
1913 @Override
1914 public List<PackageInfo> getPreferredPackages(int flags) {
Jeff Sharkeyd5896632016-03-04 16:16:00 -07001915 Log.w(TAG, "getPreferredPackages() is a no-op");
1916 return Collections.emptyList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001917 }
1918
1919 @Override
1920 public void addPreferredActivity(IntentFilter filter,
1921 int match, ComponentName[] set, ComponentName activity) {
1922 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001923 mPM.addPreferredActivity(filter, match, set, activity, mContext.getUserId());
Amith Yamasania3f133a2012-08-09 17:11:28 -07001924 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001925 throw e.rethrowFromSystemServer();
Amith Yamasania3f133a2012-08-09 17:11:28 -07001926 }
1927 }
1928
1929 @Override
Jeff Sharkeye06b4d12016-01-06 14:51:50 -07001930 public void addPreferredActivityAsUser(IntentFilter filter, int match,
Amith Yamasania3f133a2012-08-09 17:11:28 -07001931 ComponentName[] set, ComponentName activity, int userId) {
1932 try {
1933 mPM.addPreferredActivity(filter, match, set, activity, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001934 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001935 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001936 }
1937 }
1938
1939 @Override
1940 public void replacePreferredActivity(IntentFilter filter,
1941 int match, ComponentName[] set, ComponentName activity) {
1942 try {
Robin Lee0e27c872015-09-28 14:37:40 +01001943 mPM.replacePreferredActivity(filter, match, set, activity, mContext.getUserId());
Amith Yamasani41c1ded2014-08-05 11:15:05 -07001944 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001945 throw e.rethrowFromSystemServer();
Amith Yamasani41c1ded2014-08-05 11:15:05 -07001946 }
1947 }
1948
1949 @Override
1950 public void replacePreferredActivityAsUser(IntentFilter filter,
1951 int match, ComponentName[] set, ComponentName activity,
1952 int userId) {
1953 try {
1954 mPM.replacePreferredActivity(filter, match, set, activity, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001955 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001956 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001957 }
1958 }
1959
1960 @Override
1961 public void clearPackagePreferredActivities(String packageName) {
1962 try {
1963 mPM.clearPackagePreferredActivities(packageName);
1964 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001965 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001966 }
1967 }
1968
1969 @Override
1970 public int getPreferredActivities(List<IntentFilter> outFilters,
1971 List<ComponentName> outActivities, String packageName) {
1972 try {
1973 return mPM.getPreferredActivities(outFilters, outActivities, packageName);
1974 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001975 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001976 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001977 }
1978
1979 @Override
Christopher Tatea2a0850d2013-09-05 16:38:58 -07001980 public ComponentName getHomeActivities(List<ResolveInfo> outActivities) {
1981 try {
1982 return mPM.getHomeActivities(outActivities);
1983 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001984 throw e.rethrowFromSystemServer();
Christopher Tatea2a0850d2013-09-05 16:38:58 -07001985 }
Christopher Tatea2a0850d2013-09-05 16:38:58 -07001986 }
1987
1988 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001989 public void setComponentEnabledSetting(ComponentName componentName,
1990 int newState, int flags) {
1991 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001992 mPM.setComponentEnabledSetting(componentName, newState, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001993 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001994 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001995 }
1996 }
1997
1998 @Override
1999 public int getComponentEnabledSetting(ComponentName componentName) {
2000 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002001 return mPM.getComponentEnabledSetting(componentName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002002 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002003 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002004 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002005 }
2006
2007 @Override
2008 public void setApplicationEnabledSetting(String packageName,
2009 int newState, int flags) {
2010 try {
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07002011 mPM.setApplicationEnabledSetting(packageName, newState, flags,
Dianne Hackborn95d78532013-09-11 09:51:14 -07002012 mContext.getUserId(), mContext.getOpPackageName());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002013 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002014 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002015 }
2016 }
2017
2018 @Override
2019 public int getApplicationEnabledSetting(String packageName) {
2020 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07002021 return mPM.getApplicationEnabledSetting(packageName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002022 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002023 throw e.rethrowFromSystemServer();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002024 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002025 }
2026
Amith Yamasani655d0e22013-06-12 14:19:10 -07002027 @Override
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002028 public boolean setApplicationHiddenSettingAsUser(String packageName, boolean hidden,
Amith Yamasani655d0e22013-06-12 14:19:10 -07002029 UserHandle user) {
2030 try {
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002031 return mPM.setApplicationHiddenSettingAsUser(packageName, hidden,
Amith Yamasani655d0e22013-06-12 14:19:10 -07002032 user.getIdentifier());
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002033 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002034 throw e.rethrowFromSystemServer();
Amith Yamasani655d0e22013-06-12 14:19:10 -07002035 }
Amith Yamasani655d0e22013-06-12 14:19:10 -07002036 }
2037
2038 @Override
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002039 public boolean getApplicationHiddenSettingAsUser(String packageName, UserHandle user) {
Amith Yamasani655d0e22013-06-12 14:19:10 -07002040 try {
Amith Yamasanie5bcff62014-07-19 15:44:09 -07002041 return mPM.getApplicationHiddenSettingAsUser(packageName, user.getIdentifier());
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002042 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002043 throw e.rethrowFromSystemServer();
Amith Yamasani655d0e22013-06-12 14:19:10 -07002044 }
Amith Yamasani655d0e22013-06-12 14:19:10 -07002045 }
2046
dcashmanc6f22492014-08-14 09:54:51 -07002047 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002048 @Override
2049 public KeySet getKeySetByAlias(String packageName, String alias) {
2050 Preconditions.checkNotNull(packageName);
2051 Preconditions.checkNotNull(alias);
dcashman9d2f4412014-06-09 09:27:54 -07002052 try {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002053 return mPM.getKeySetByAlias(packageName, alias);
dcashman9d2f4412014-06-09 09:27:54 -07002054 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002055 throw e.rethrowFromSystemServer();
dcashman9d2f4412014-06-09 09:27:54 -07002056 }
dcashman9d2f4412014-06-09 09:27:54 -07002057 }
2058
dcashmanc6f22492014-08-14 09:54:51 -07002059 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002060 @Override
2061 public KeySet getSigningKeySet(String packageName) {
2062 Preconditions.checkNotNull(packageName);
dcashman9d2f4412014-06-09 09:27:54 -07002063 try {
Jeff Sharkey27b2e692016-02-25 17:40:12 -07002064 return mPM.getSigningKeySet(packageName);
dcashman9d2f4412014-06-09 09:27:54 -07002065 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002066 throw e.rethrowFromSystemServer();
dcashman9d2f4412014-06-09 09:27:54 -07002067 }
dcashman9d2f4412014-06-09 09:27:54 -07002068 }
2069
dcashmanc6f22492014-08-14 09:54:51 -07002070 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002071 @Override
2072 public boolean isSignedBy(String packageName, KeySet ks) {
2073 Preconditions.checkNotNull(packageName);
2074 Preconditions.checkNotNull(ks);
dcashman9d2f4412014-06-09 09:27:54 -07002075 try {
dcashmanc6f22492014-08-14 09:54:51 -07002076 return mPM.isPackageSignedByKeySet(packageName, ks);
dcashman9d2f4412014-06-09 09:27:54 -07002077 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002078 throw e.rethrowFromSystemServer();
dcashman9d2f4412014-06-09 09:27:54 -07002079 }
2080 }
2081
dcashmanc6f22492014-08-14 09:54:51 -07002082 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07002083 @Override
2084 public boolean isSignedByExactly(String packageName, KeySet ks) {
2085 Preconditions.checkNotNull(packageName);
2086 Preconditions.checkNotNull(ks);
dcashman9d2f4412014-06-09 09:27:54 -07002087 try {
dcashmanc6f22492014-08-14 09:54:51 -07002088 return mPM.isPackageSignedByKeySetExactly(packageName, ks);
dcashman9d2f4412014-06-09 09:27:54 -07002089 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002090 throw e.rethrowFromSystemServer();
dcashman9d2f4412014-06-09 09:27:54 -07002091 }
2092 }
2093
Kenny Root0aaa0d92011-09-12 16:42:55 -07002094 /**
2095 * @hide
2096 */
2097 @Override
2098 public VerifierDeviceIdentity getVerifierDeviceIdentity() {
2099 try {
2100 return mPM.getVerifierDeviceIdentity();
2101 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002102 throw e.rethrowFromSystemServer();
Kenny Root0aaa0d92011-09-12 16:42:55 -07002103 }
Kenny Root0aaa0d92011-09-12 16:42:55 -07002104 }
2105
Jeff Hao9f60c082014-10-28 18:51:07 -07002106 /**
2107 * @hide
2108 */
2109 @Override
2110 public boolean isUpgrade() {
2111 try {
2112 return mPM.isUpgrade();
2113 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002114 throw e.rethrowFromSystemServer();
Jeff Hao9f60c082014-10-28 18:51:07 -07002115 }
2116 }
2117
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07002118 @Override
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07002119 public PackageInstaller getPackageInstaller() {
2120 synchronized (mLock) {
2121 if (mInstaller == null) {
2122 try {
Jeff Sharkeya0907432014-08-15 10:23:11 -07002123 mInstaller = new PackageInstaller(mContext, this, mPM.getPackageInstaller(),
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07002124 mContext.getPackageName(), mContext.getUserId());
2125 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002126 throw e.rethrowFromSystemServer();
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07002127 }
2128 }
2129 return mInstaller;
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07002130 }
2131 }
2132
Jeff Sharkey6c833e02014-07-14 22:44:30 -07002133 @Override
2134 public boolean isPackageAvailable(String packageName) {
2135 try {
2136 return mPM.isPackageAvailable(packageName, mContext.getUserId());
2137 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002138 throw e.rethrowFromSystemServer();
Jeff Sharkey6c833e02014-07-14 22:44:30 -07002139 }
2140 }
2141
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002142 /**
2143 * @hide
2144 */
2145 @Override
Nicolas Prevot63798c52014-05-27 13:22:38 +01002146 public void addCrossProfileIntentFilter(IntentFilter filter, int sourceUserId, int targetUserId,
2147 int flags) {
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002148 try {
Nicolas Prevot3f7777f2014-07-24 15:58:39 +01002149 mPM.addCrossProfileIntentFilter(filter, mContext.getOpPackageName(),
Nicolas Prevot4b8d5822015-03-05 15:20:49 +00002150 sourceUserId, targetUserId, flags);
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002151 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002152 throw e.rethrowFromSystemServer();
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002153 }
2154 }
2155
2156 /**
2157 * @hide
2158 */
2159 @Override
Nicolas Prevot81948992014-05-16 18:25:26 +01002160 public void clearCrossProfileIntentFilters(int sourceUserId) {
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002161 try {
Nicolas Prevot4b8d5822015-03-05 15:20:49 +00002162 mPM.clearCrossProfileIntentFilters(sourceUserId, mContext.getOpPackageName());
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002163 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002164 throw e.rethrowFromSystemServer();
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002165 }
2166 }
2167
Nicolas Prevot88cc3462014-05-14 14:51:48 +01002168 /**
2169 * @hide
2170 */
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002171 public Drawable loadItemIcon(PackageItemInfo itemInfo, ApplicationInfo appInfo) {
Benjamin Franzec2d48b2014-10-01 15:38:43 +01002172 Drawable dr = loadUnbadgedItemIcon(itemInfo, appInfo);
2173 if (itemInfo.showUserIcon != UserHandle.USER_NULL) {
2174 return dr;
2175 }
2176 return getUserBadgedIcon(dr, new UserHandle(mContext.getUserId()));
2177 }
2178
2179 /**
2180 * @hide
2181 */
2182 public Drawable loadUnbadgedItemIcon(PackageItemInfo itemInfo, ApplicationInfo appInfo) {
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002183 if (itemInfo.showUserIcon != UserHandle.USER_NULL) {
Alexandra Gherghina64d4dca2014-08-28 18:26:56 +01002184 Bitmap bitmap = getUserManager().getUserIcon(itemInfo.showUserIcon);
2185 if (bitmap == null) {
2186 return UserIcons.getDefaultUserIcon(itemInfo.showUserIcon, /* light= */ false);
2187 }
2188 return new BitmapDrawable(bitmap);
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002189 }
Alexandra Gherghinadb811db2014-08-29 13:43:59 +01002190 Drawable dr = null;
2191 if (itemInfo.packageName != null) {
2192 dr = getDrawable(itemInfo.packageName, itemInfo.icon, appInfo);
2193 }
Alexandra Gherghinaa71e3902014-07-25 20:03:47 +01002194 if (dr == null) {
Alexandra Gherghinaa7093142014-07-30 13:43:39 +01002195 dr = itemInfo.loadDefaultIcon(this);
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002196 }
Benjamin Franzec2d48b2014-10-01 15:38:43 +01002197 return dr;
Svetoslavc7d62f02014-09-04 15:39:54 -07002198 }
2199
2200 private Drawable getBadgedDrawable(Drawable drawable, Drawable badgeDrawable,
2201 Rect badgeLocation, boolean tryBadgeInPlace) {
2202 final int badgedWidth = drawable.getIntrinsicWidth();
2203 final int badgedHeight = drawable.getIntrinsicHeight();
2204 final boolean canBadgeInPlace = tryBadgeInPlace
2205 && (drawable instanceof BitmapDrawable)
2206 && ((BitmapDrawable) drawable).getBitmap().isMutable();
2207
2208 final Bitmap bitmap;
2209 if (canBadgeInPlace) {
2210 bitmap = ((BitmapDrawable) drawable).getBitmap();
2211 } else {
2212 bitmap = Bitmap.createBitmap(badgedWidth, badgedHeight, Bitmap.Config.ARGB_8888);
2213 }
2214 Canvas canvas = new Canvas(bitmap);
2215
2216 if (!canBadgeInPlace) {
2217 drawable.setBounds(0, 0, badgedWidth, badgedHeight);
2218 drawable.draw(canvas);
2219 }
2220
2221 if (badgeLocation != null) {
2222 if (badgeLocation.left < 0 || badgeLocation.top < 0
2223 || badgeLocation.width() > badgedWidth || badgeLocation.height() > badgedHeight) {
2224 throw new IllegalArgumentException("Badge location " + badgeLocation
2225 + " not in badged drawable bounds "
2226 + new Rect(0, 0, badgedWidth, badgedHeight));
2227 }
2228 badgeDrawable.setBounds(0, 0, badgeLocation.width(), badgeLocation.height());
2229
2230 canvas.save();
2231 canvas.translate(badgeLocation.left, badgeLocation.top);
2232 badgeDrawable.draw(canvas);
2233 canvas.restore();
2234 } else {
2235 badgeDrawable.setBounds(0, 0, badgedWidth, badgedHeight);
2236 badgeDrawable.draw(canvas);
2237 }
2238
2239 if (!canBadgeInPlace) {
2240 BitmapDrawable mergedDrawable = new BitmapDrawable(mContext.getResources(), bitmap);
2241
2242 if (drawable instanceof BitmapDrawable) {
2243 BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
2244 mergedDrawable.setTargetDensity(bitmapDrawable.getBitmap().getDensity());
2245 }
2246
2247 return mergedDrawable;
2248 }
2249
2250 return drawable;
2251 }
2252
2253 private int getBadgeResIdForUser(int userHandle) {
2254 // Return the framework-provided badge.
2255 UserInfo userInfo = getUserIfProfile(userHandle);
2256 if (userInfo != null && userInfo.isManagedProfile()) {
2257 return com.android.internal.R.drawable.ic_corp_icon_badge;
2258 }
2259 return 0;
2260 }
2261
2262 private UserInfo getUserIfProfile(int userHandle) {
Robin Lee0e27c872015-09-28 14:37:40 +01002263 List<UserInfo> userProfiles = getUserManager().getProfiles(mContext.getUserId());
Svetoslavc7d62f02014-09-04 15:39:54 -07002264 for (UserInfo user : userProfiles) {
2265 if (user.id == userHandle) {
2266 return user;
2267 }
2268 }
2269 return null;
Nicolas Prevot88cc3462014-05-14 14:51:48 +01002270 }
2271
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002272 /** {@hide} */
2273 private static class MoveCallbackDelegate extends IPackageMoveObserver.Stub implements
2274 Handler.Callback {
Jeff Sharkey50a05452015-04-29 11:24:52 -07002275 private static final int MSG_CREATED = 1;
2276 private static final int MSG_STATUS_CHANGED = 2;
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002277
2278 final MoveCallback mCallback;
2279 final Handler mHandler;
2280
2281 public MoveCallbackDelegate(MoveCallback callback, Looper looper) {
2282 mCallback = callback;
2283 mHandler = new Handler(looper, this);
2284 }
2285
2286 @Override
2287 public boolean handleMessage(Message msg) {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002288 switch (msg.what) {
Jeff Sharkey50a05452015-04-29 11:24:52 -07002289 case MSG_CREATED: {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002290 final SomeArgs args = (SomeArgs) msg.obj;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002291 mCallback.onCreated(args.argi1, (Bundle) args.arg2);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002292 args.recycle();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002293 return true;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002294 }
2295 case MSG_STATUS_CHANGED: {
2296 final SomeArgs args = (SomeArgs) msg.obj;
2297 mCallback.onStatusChanged(args.argi1, args.argi2, (long) args.arg3);
2298 args.recycle();
2299 return true;
2300 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002301 }
2302 return false;
2303 }
2304
2305 @Override
Jeff Sharkey50a05452015-04-29 11:24:52 -07002306 public void onCreated(int moveId, Bundle extras) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002307 final SomeArgs args = SomeArgs.obtain();
2308 args.argi1 = moveId;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002309 args.arg2 = extras;
2310 mHandler.obtainMessage(MSG_CREATED, args).sendToTarget();
2311 }
2312
2313 @Override
2314 public void onStatusChanged(int moveId, int status, long estMillis) {
2315 final SomeArgs args = SomeArgs.obtain();
2316 args.argi1 = moveId;
2317 args.argi2 = status;
2318 args.arg3 = estMillis;
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002319 mHandler.obtainMessage(MSG_STATUS_CHANGED, args).sendToTarget();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002320 }
2321 }
2322
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002323 private final ContextImpl mContext;
2324 private final IPackageManager mPM;
2325
2326 private static final Object sSync = new Object();
Dianne Hackbornadd005c2013-07-17 18:43:12 -07002327 private static ArrayMap<ResourceName, WeakReference<Drawable.ConstantState>> sIconCache
2328 = new ArrayMap<ResourceName, WeakReference<Drawable.ConstantState>>();
2329 private static ArrayMap<ResourceName, WeakReference<CharSequence>> sStringCache
2330 = new ArrayMap<ResourceName, WeakReference<CharSequence>>();
Svetoslavf7c06eb2015-06-10 18:43:22 -07002331
2332 private final Map<OnPermissionsChangedListener, IOnPermissionsChangeListener>
2333 mPermissionListeners = new ArrayMap<>();
2334
2335 public class OnPermissionsChangeListenerDelegate extends IOnPermissionsChangeListener.Stub
2336 implements Handler.Callback{
2337 private static final int MSG_PERMISSIONS_CHANGED = 1;
2338
2339 private final OnPermissionsChangedListener mListener;
2340 private final Handler mHandler;
2341
2342
2343 public OnPermissionsChangeListenerDelegate(OnPermissionsChangedListener listener,
2344 Looper looper) {
2345 mListener = listener;
2346 mHandler = new Handler(looper, this);
2347 }
2348
2349 @Override
2350 public void onPermissionsChanged(int uid) {
2351 mHandler.obtainMessage(MSG_PERMISSIONS_CHANGED, uid, 0).sendToTarget();
2352 }
2353
2354 @Override
2355 public boolean handleMessage(Message msg) {
2356 switch (msg.what) {
2357 case MSG_PERMISSIONS_CHANGED: {
2358 final int uid = msg.arg1;
2359 mListener.onPermissionsChanged(uid);
2360 return true;
2361 }
2362 }
2363 return false;
2364 }
2365 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002366}