blob: e500e15335247370005926020ec79aca8468f063 [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;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080024import android.content.ComponentName;
25import android.content.ContentResolver;
26import android.content.Intent;
27import android.content.IntentFilter;
28import android.content.IntentSender;
29import android.content.pm.ActivityInfo;
30import android.content.pm.ApplicationInfo;
31import android.content.pm.ComponentInfo;
Anonymous Cowardceb1b0b2012-04-24 10:35:16 -070032import android.content.pm.ContainerEncryptionParams;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080033import android.content.pm.FeatureInfo;
Svetoslavf7c06eb2015-06-10 18:43:22 -070034import android.content.pm.IOnPermissionsChangeListener;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080035import android.content.pm.IPackageDataObserver;
36import android.content.pm.IPackageDeleteObserver;
37import android.content.pm.IPackageInstallObserver;
38import android.content.pm.IPackageManager;
39import android.content.pm.IPackageMoveObserver;
40import android.content.pm.IPackageStatsObserver;
41import android.content.pm.InstrumentationInfo;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -080042import android.content.pm.IntentFilterVerificationInfo;
dcashman9d2f4412014-06-09 09:27:54 -070043import android.content.pm.KeySet;
Jeff Sharkey513a0742014-07-08 17:10:32 -070044import android.content.pm.ManifestDigest;
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;
rich cannings706e8ba2012-08-20 13:20:14 -070056import android.content.pm.VerificationParams;
Kenny Root0aaa0d92011-09-12 16:42:55 -070057import android.content.pm.VerifierDeviceIdentity;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080058import android.content.res.Resources;
59import android.content.res.XmlResourceParser;
Svetoslavc7d62f02014-09-04 15:39:54 -070060import android.graphics.Bitmap;
61import android.graphics.Canvas;
62import android.graphics.Rect;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +010063import android.graphics.drawable.BitmapDrawable;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080064import android.graphics.drawable.Drawable;
65import android.net.Uri;
Jeff Sharkey50a05452015-04-29 11:24:52 -070066import android.os.Bundle;
Jeff Sharkey620b32b2015-04-23 19:36:02 -070067import android.os.Handler;
68import android.os.Looper;
69import android.os.Message;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080070import android.os.Process;
71import android.os.RemoteException;
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -070072import android.os.SystemProperties;
Amith Yamasani67df64b2012-12-14 12:09:36 -080073import android.os.UserHandle;
Nicolas Prevot88cc3462014-05-14 14:51:48 +010074import android.os.UserManager;
Jeff Sharkeye2d45be2015-04-15 17:14:12 -070075import android.os.storage.StorageManager;
76import android.os.storage.VolumeInfo;
Todd Kennedyf39ca8f2015-08-07 14:15:07 -070077import android.provider.Settings;
Dianne Hackbornadd005c2013-07-17 18:43:12 -070078import android.util.ArrayMap;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080079import android.util.Log;
Jeff Browna492c3a2012-08-23 19:48:44 -070080import android.view.Display;
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -070081
82import dalvik.system.VMRuntime;
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -070083
84import com.android.internal.annotations.GuardedBy;
Jeff Sharkeyb36586a2015-04-27 08:42:28 -070085import com.android.internal.os.SomeArgs;
dcashman9d2f4412014-06-09 09:27:54 -070086import com.android.internal.util.Preconditions;
Alexandra Gherghina64d4dca2014-08-28 18:26:56 +010087import com.android.internal.util.UserIcons;
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -070088
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080089import java.lang.ref.WeakReference;
90import java.util.ArrayList;
Jeff Sharkey620b32b2015-04-23 19:36:02 -070091import java.util.Iterator;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080092import java.util.List;
Svetoslavf7c06eb2015-06-10 18:43:22 -070093import java.util.Map;
Jeff Sharkey620b32b2015-04-23 19:36:02 -070094import java.util.Objects;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080095
Makoto Onukicc4bbeb2015-09-17 10:28:24 -070096/** @hide */
97public class ApplicationPackageManager extends PackageManager {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080098 private static final String TAG = "ApplicationPackageManager";
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080099 private final static boolean DEBUG_ICONS = false;
100
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700101 // Default flags to use with PackageManager when no flags are given.
102 private final static int sDefaultFlags = PackageManager.GET_SHARED_LIBRARY_FILES;
103
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -0700104 private final Object mLock = new Object();
105
106 @GuardedBy("mLock")
107 private UserManager mUserManager;
108 @GuardedBy("mLock")
109 private PackageInstaller mInstaller;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100110
Jeff Sharkey620b32b2015-04-23 19:36:02 -0700111 @GuardedBy("mDelegates")
112 private final ArrayList<MoveCallbackDelegate> mDelegates = new ArrayList<>();
113
Svet Ganovf1b7f202015-07-29 08:33:42 -0700114 @GuardedBy("mLock")
115 private String mPermissionsControllerPackageName;
116
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100117 UserManager getUserManager() {
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -0700118 synchronized (mLock) {
119 if (mUserManager == null) {
120 mUserManager = UserManager.get(mContext);
121 }
122 return mUserManager;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100123 }
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100124 }
125
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800126 @Override
127 public PackageInfo getPackageInfo(String packageName, int flags)
128 throws NameNotFoundException {
Nicolas Prevot9a80e532015-09-23 15:49:28 +0100129 return getPackageInfoAsUser(packageName, flags, mContext.getUserId());
130 }
131
132 @Override
133 public PackageInfo getPackageInfoAsUser(String packageName, int flags, int userId)
134 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800135 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +0100136 PackageInfo pi = mPM.getPackageInfo(packageName, flags, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800137 if (pi != null) {
138 return pi;
139 }
140 } catch (RemoteException e) {
141 throw new RuntimeException("Package manager has died", e);
142 }
143
144 throw new NameNotFoundException(packageName);
145 }
146
147 @Override
148 public String[] currentToCanonicalPackageNames(String[] names) {
149 try {
150 return mPM.currentToCanonicalPackageNames(names);
151 } catch (RemoteException e) {
152 throw new RuntimeException("Package manager has died", e);
153 }
154 }
155
156 @Override
157 public String[] canonicalToCurrentPackageNames(String[] names) {
158 try {
159 return mPM.canonicalToCurrentPackageNames(names);
160 } catch (RemoteException e) {
161 throw new RuntimeException("Package manager has died", e);
162 }
163 }
164
165 @Override
166 public Intent getLaunchIntentForPackage(String packageName) {
167 // First see if the package has an INFO activity; the existence of
168 // such an activity is implied to be the desired front-door for the
169 // overall package (such as if it has multiple launcher entries).
170 Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
171 intentToResolve.addCategory(Intent.CATEGORY_INFO);
172 intentToResolve.setPackage(packageName);
Dianne Hackborn19415762010-12-15 00:20:27 -0800173 List<ResolveInfo> ris = queryIntentActivities(intentToResolve, 0);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800174
175 // Otherwise, try to find a main launcher activity.
Dianne Hackborn19415762010-12-15 00:20:27 -0800176 if (ris == null || ris.size() <= 0) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800177 // reuse the intent instance
178 intentToResolve.removeCategory(Intent.CATEGORY_INFO);
179 intentToResolve.addCategory(Intent.CATEGORY_LAUNCHER);
180 intentToResolve.setPackage(packageName);
Dianne Hackborn19415762010-12-15 00:20:27 -0800181 ris = queryIntentActivities(intentToResolve, 0);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800182 }
Dianne Hackborn19415762010-12-15 00:20:27 -0800183 if (ris == null || ris.size() <= 0) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800184 return null;
185 }
186 Intent intent = new Intent(intentToResolve);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800187 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Dianne Hackborn19415762010-12-15 00:20:27 -0800188 intent.setClassName(ris.get(0).activityInfo.packageName,
189 ris.get(0).activityInfo.name);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800190 return intent;
191 }
192
193 @Override
Jose Lima970417c2014-04-10 10:42:19 -0700194 public Intent getLeanbackLaunchIntentForPackage(String packageName) {
195 // Try to find a main leanback_launcher activity.
196 Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
197 intentToResolve.addCategory(Intent.CATEGORY_LEANBACK_LAUNCHER);
198 intentToResolve.setPackage(packageName);
199 List<ResolveInfo> ris = queryIntentActivities(intentToResolve, 0);
200
201 if (ris == null || ris.size() <= 0) {
202 return null;
203 }
204 Intent intent = new Intent(intentToResolve);
205 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
206 intent.setClassName(ris.get(0).activityInfo.packageName,
207 ris.get(0).activityInfo.name);
208 return intent;
209 }
210
211 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800212 public int[] getPackageGids(String packageName)
213 throws NameNotFoundException {
214 try {
Svetoslavc6d1c342015-02-26 14:44:43 -0800215 int[] gids = mPM.getPackageGids(packageName, mContext.getUserId());
216 if (gids != null) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800217 return gids;
218 }
219 } catch (RemoteException e) {
220 throw new RuntimeException("Package manager has died", e);
221 }
222
223 throw new NameNotFoundException(packageName);
224 }
225
226 @Override
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800227 public int getPackageUid(String packageName, int userHandle)
228 throws NameNotFoundException {
229 try {
230 int uid = mPM.getPackageUid(packageName, userHandle);
231 if (uid >= 0) {
232 return uid;
233 }
234 } catch (RemoteException e) {
235 throw new RuntimeException("Package manager has died", e);
236 }
237
238 throw new NameNotFoundException(packageName);
239 }
240
241 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800242 public PermissionInfo getPermissionInfo(String name, int flags)
243 throws NameNotFoundException {
244 try {
245 PermissionInfo pi = mPM.getPermissionInfo(name, flags);
246 if (pi != null) {
247 return pi;
248 }
249 } catch (RemoteException e) {
250 throw new RuntimeException("Package manager has died", e);
251 }
252
253 throw new NameNotFoundException(name);
254 }
255
256 @Override
257 public List<PermissionInfo> queryPermissionsByGroup(String group, int flags)
258 throws NameNotFoundException {
259 try {
260 List<PermissionInfo> pi = mPM.queryPermissionsByGroup(group, flags);
261 if (pi != null) {
262 return pi;
263 }
264 } catch (RemoteException e) {
265 throw new RuntimeException("Package manager has died", e);
266 }
267
268 throw new NameNotFoundException(group);
269 }
270
271 @Override
272 public PermissionGroupInfo getPermissionGroupInfo(String name,
273 int flags) throws NameNotFoundException {
274 try {
275 PermissionGroupInfo pgi = mPM.getPermissionGroupInfo(name, flags);
276 if (pgi != null) {
277 return pgi;
278 }
279 } catch (RemoteException e) {
280 throw new RuntimeException("Package manager has died", e);
281 }
282
283 throw new NameNotFoundException(name);
284 }
285
286 @Override
287 public List<PermissionGroupInfo> getAllPermissionGroups(int flags) {
288 try {
289 return mPM.getAllPermissionGroups(flags);
290 } catch (RemoteException e) {
291 throw new RuntimeException("Package manager has died", e);
292 }
293 }
294
295 @Override
296 public ApplicationInfo getApplicationInfo(String packageName, int flags)
297 throws NameNotFoundException {
298 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700299 ApplicationInfo ai = mPM.getApplicationInfo(packageName, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800300 if (ai != null) {
Narayan Kamathcaa71192014-07-16 11:06:43 +0100301 // This is a temporary hack. Callers must use
302 // createPackageContext(packageName).getApplicationInfo() to
303 // get the right paths.
304 maybeAdjustApplicationInfo(ai);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800305 return ai;
306 }
307 } catch (RemoteException e) {
308 throw new RuntimeException("Package manager has died", e);
309 }
310
311 throw new NameNotFoundException(packageName);
312 }
313
Narayan Kamathcaa71192014-07-16 11:06:43 +0100314 private static void maybeAdjustApplicationInfo(ApplicationInfo info) {
315 // If we're dealing with a multi-arch application that has both
316 // 32 and 64 bit shared libraries, we might need to choose the secondary
317 // depending on what the current runtime's instruction set is.
318 if (info.primaryCpuAbi != null && info.secondaryCpuAbi != null) {
319 final String runtimeIsa = VMRuntime.getRuntime().vmInstructionSet();
jgu214741cd92014-12-17 17:23:29 -0500320
321 // Get the instruction set that the libraries of secondary Abi is supported.
322 // In presence of a native bridge this might be different than the one secondary Abi used.
323 String secondaryIsa = VMRuntime.getInstructionSet(info.secondaryCpuAbi);
324 final String secondaryDexCodeIsa = SystemProperties.get("ro.dalvik.vm.isa." + secondaryIsa);
325 secondaryIsa = secondaryDexCodeIsa.isEmpty() ? secondaryIsa : secondaryDexCodeIsa;
Narayan Kamathcaa71192014-07-16 11:06:43 +0100326
327 // If the runtimeIsa is the same as the primary isa, then we do nothing.
328 // Everything will be set up correctly because info.nativeLibraryDir will
329 // correspond to the right ISA.
330 if (runtimeIsa.equals(secondaryIsa)) {
331 info.nativeLibraryDir = info.secondaryNativeLibraryDir;
332 }
333 }
334 }
335
336
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800337 @Override
338 public ActivityInfo getActivityInfo(ComponentName className, int flags)
339 throws NameNotFoundException {
340 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700341 ActivityInfo ai = mPM.getActivityInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800342 if (ai != null) {
343 return ai;
344 }
345 } catch (RemoteException e) {
346 throw new RuntimeException("Package manager has died", e);
347 }
348
349 throw new NameNotFoundException(className.toString());
350 }
351
352 @Override
353 public ActivityInfo getReceiverInfo(ComponentName className, int flags)
354 throws NameNotFoundException {
355 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700356 ActivityInfo ai = mPM.getReceiverInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800357 if (ai != null) {
358 return ai;
359 }
360 } catch (RemoteException e) {
361 throw new RuntimeException("Package manager has died", e);
362 }
363
364 throw new NameNotFoundException(className.toString());
365 }
366
367 @Override
368 public ServiceInfo getServiceInfo(ComponentName className, int flags)
369 throws NameNotFoundException {
370 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700371 ServiceInfo si = mPM.getServiceInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800372 if (si != null) {
373 return si;
374 }
375 } catch (RemoteException e) {
376 throw new RuntimeException("Package manager has died", e);
377 }
378
379 throw new NameNotFoundException(className.toString());
380 }
381
382 @Override
383 public ProviderInfo getProviderInfo(ComponentName className, int flags)
384 throws NameNotFoundException {
385 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700386 ProviderInfo pi = mPM.getProviderInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800387 if (pi != null) {
388 return pi;
389 }
390 } catch (RemoteException e) {
391 throw new RuntimeException("Package manager has died", e);
392 }
393
394 throw new NameNotFoundException(className.toString());
395 }
396
397 @Override
398 public String[] getSystemSharedLibraryNames() {
399 try {
400 return mPM.getSystemSharedLibraryNames();
401 } catch (RemoteException e) {
402 throw new RuntimeException("Package manager has died", e);
403 }
404 }
405
406 @Override
407 public FeatureInfo[] getSystemAvailableFeatures() {
408 try {
409 return mPM.getSystemAvailableFeatures();
410 } catch (RemoteException e) {
411 throw new RuntimeException("Package manager has died", e);
412 }
413 }
414
415 @Override
416 public boolean hasSystemFeature(String name) {
417 try {
418 return mPM.hasSystemFeature(name);
419 } catch (RemoteException e) {
420 throw new RuntimeException("Package manager has died", e);
421 }
422 }
423
424 @Override
425 public int checkPermission(String permName, String pkgName) {
426 try {
Svetoslavc6d1c342015-02-26 14:44:43 -0800427 return mPM.checkPermission(permName, pkgName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800428 } catch (RemoteException e) {
429 throw new RuntimeException("Package manager has died", e);
430 }
431 }
432
433 @Override
Svet Ganovad3b2972015-07-07 22:49:17 -0700434 public boolean isPermissionRevokedByPolicy(String permName, String pkgName) {
435 try {
436 return mPM.isPermissionRevokedByPolicy(permName, pkgName, mContext.getUserId());
437 } catch (RemoteException e) {
438 throw new RuntimeException("Package manager has died", e);
439 }
440 }
441
Svet Ganovf1b7f202015-07-29 08:33:42 -0700442 /**
443 * @hide
444 */
445 @Override
446 public String getPermissionControllerPackageName() {
447 synchronized (mLock) {
448 if (mPermissionsControllerPackageName == null) {
449 try {
450 mPermissionsControllerPackageName = mPM.getPermissionControllerPackageName();
451 } catch (RemoteException e) {
452 throw new RuntimeException("Package manager has died", e);
453 }
454 }
455 return mPermissionsControllerPackageName;
456 }
457 }
458
Svet Ganovad3b2972015-07-07 22:49:17 -0700459 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800460 public boolean addPermission(PermissionInfo info) {
461 try {
462 return mPM.addPermission(info);
463 } catch (RemoteException e) {
464 throw new RuntimeException("Package manager has died", e);
465 }
466 }
467
468 @Override
469 public boolean addPermissionAsync(PermissionInfo info) {
470 try {
471 return mPM.addPermissionAsync(info);
472 } catch (RemoteException e) {
473 throw new RuntimeException("Package manager has died", e);
474 }
475 }
476
477 @Override
478 public void removePermission(String name) {
479 try {
480 mPM.removePermission(name);
481 } catch (RemoteException e) {
482 throw new RuntimeException("Package manager has died", e);
483 }
484 }
485
486 @Override
Svet Ganov8c7f7002015-05-07 10:48:44 -0700487 public void grantRuntimePermission(String packageName, String permissionName,
488 UserHandle user) {
Dianne Hackborne639da72012-02-21 15:11:13 -0800489 try {
Svet Ganov8c7f7002015-05-07 10:48:44 -0700490 mPM.grantRuntimePermission(packageName, permissionName, user.getIdentifier());
Dianne Hackborne639da72012-02-21 15:11:13 -0800491 } catch (RemoteException e) {
492 throw new RuntimeException("Package manager has died", e);
493 }
494 }
495
496 @Override
Svet Ganov8c7f7002015-05-07 10:48:44 -0700497 public void revokeRuntimePermission(String packageName, String permissionName,
498 UserHandle user) {
Dianne Hackborne639da72012-02-21 15:11:13 -0800499 try {
Svet Ganov8c7f7002015-05-07 10:48:44 -0700500 mPM.revokeRuntimePermission(packageName, permissionName, user.getIdentifier());
501 } catch (RemoteException e) {
502 throw new RuntimeException("Package manager has died", e);
503 }
504 }
505
506 @Override
507 public int getPermissionFlags(String permissionName, String packageName, UserHandle user) {
508 try {
509 return mPM.getPermissionFlags(permissionName, packageName, user.getIdentifier());
510 } catch (RemoteException e) {
511 throw new RuntimeException("Package manager has died", e);
512 }
513 }
514
515 @Override
516 public void updatePermissionFlags(String permissionName, String packageName,
517 int flagMask, int flagValues, UserHandle user) {
518 try {
519 mPM.updatePermissionFlags(permissionName, packageName, flagMask,
520 flagValues, user.getIdentifier());
Dianne Hackborne639da72012-02-21 15:11:13 -0800521 } catch (RemoteException e) {
522 throw new RuntimeException("Package manager has died", e);
523 }
524 }
525
526 @Override
Svetoslav20770dd2015-05-29 15:43:04 -0700527 public boolean shouldShowRequestPermissionRationale(String permission) {
528 try {
529 return mPM.shouldShowRequestPermissionRationale(permission,
530 mContext.getPackageName(), mContext.getUserId());
531 } catch (RemoteException e) {
532 throw new RuntimeException("Package manager has died", e);
533 }
534 }
535
536 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800537 public int checkSignatures(String pkg1, String pkg2) {
538 try {
539 return mPM.checkSignatures(pkg1, pkg2);
540 } catch (RemoteException e) {
541 throw new RuntimeException("Package manager has died", e);
542 }
543 }
544
545 @Override
546 public int checkSignatures(int uid1, int uid2) {
547 try {
548 return mPM.checkUidSignatures(uid1, uid2);
549 } catch (RemoteException e) {
550 throw new RuntimeException("Package manager has died", e);
551 }
552 }
553
554 @Override
555 public String[] getPackagesForUid(int uid) {
556 try {
557 return mPM.getPackagesForUid(uid);
558 } catch (RemoteException e) {
559 throw new RuntimeException("Package manager has died", e);
560 }
561 }
562
563 @Override
564 public String getNameForUid(int uid) {
565 try {
566 return mPM.getNameForUid(uid);
567 } catch (RemoteException e) {
568 throw new RuntimeException("Package manager has died", e);
569 }
570 }
571
572 @Override
573 public int getUidForSharedUser(String sharedUserName)
574 throws NameNotFoundException {
575 try {
576 int uid = mPM.getUidForSharedUser(sharedUserName);
577 if(uid != -1) {
578 return uid;
579 }
580 } catch (RemoteException e) {
581 throw new RuntimeException("Package manager has died", e);
582 }
583 throw new NameNotFoundException("No shared userid for user:"+sharedUserName);
584 }
585
Kenny Roote6cd0c72011-05-19 12:48:14 -0700586 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800587 @Override
588 public List<PackageInfo> getInstalledPackages(int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700589 return getInstalledPackages(flags, mContext.getUserId());
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700590 }
591
592 /** @hide */
593 @Override
594 public List<PackageInfo> getInstalledPackages(int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800595 try {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800596 ParceledListSlice<PackageInfo> slice = mPM.getInstalledPackages(flags, userId);
597 return slice.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800598 } catch (RemoteException e) {
599 throw new RuntimeException("Package manager has died", e);
600 }
601 }
602
Kenny Roote6cd0c72011-05-19 12:48:14 -0700603 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800604 @Override
Dianne Hackborne7991752013-01-16 17:56:46 -0800605 public List<PackageInfo> getPackagesHoldingPermissions(
606 String[] permissions, int flags) {
607 final int userId = mContext.getUserId();
608 try {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800609 ParceledListSlice<PackageInfo> slice = mPM.getPackagesHoldingPermissions(
610 permissions, flags, userId);
611 return slice.getList();
Dianne Hackborne7991752013-01-16 17:56:46 -0800612 } catch (RemoteException e) {
613 throw new RuntimeException("Package manager has died", e);
614 }
615 }
616
617 @SuppressWarnings("unchecked")
618 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800619 public List<ApplicationInfo> getInstalledApplications(int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700620 final int userId = mContext.getUserId();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800621 try {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800622 ParceledListSlice<ApplicationInfo> slice = mPM.getInstalledApplications(flags, userId);
623 return slice.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800624 } catch (RemoteException e) {
625 throw new RuntimeException("Package manager has died", e);
626 }
627 }
628
629 @Override
630 public ResolveInfo resolveActivity(Intent intent, int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700631 return resolveActivityAsUser(intent, flags, mContext.getUserId());
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700632 }
633
634 @Override
635 public ResolveInfo resolveActivityAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800636 try {
637 return mPM.resolveIntent(
638 intent,
639 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700640 flags,
641 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800642 } catch (RemoteException e) {
643 throw new RuntimeException("Package manager has died", e);
644 }
645 }
646
647 @Override
648 public List<ResolveInfo> queryIntentActivities(Intent intent,
649 int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700650 return queryIntentActivitiesAsUser(intent, flags, mContext.getUserId());
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700651 }
652
653 /** @hide Same as above but for a specific user */
654 @Override
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700655 public List<ResolveInfo> queryIntentActivitiesAsUser(Intent intent,
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700656 int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800657 try {
658 return mPM.queryIntentActivities(
659 intent,
660 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700661 flags,
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700662 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800663 } catch (RemoteException e) {
664 throw new RuntimeException("Package manager has died", e);
665 }
666 }
667
668 @Override
669 public List<ResolveInfo> queryIntentActivityOptions(
670 ComponentName caller, Intent[] specifics, Intent intent,
671 int flags) {
672 final ContentResolver resolver = mContext.getContentResolver();
673
674 String[] specificTypes = null;
675 if (specifics != null) {
676 final int N = specifics.length;
677 for (int i=0; i<N; i++) {
678 Intent sp = specifics[i];
679 if (sp != null) {
680 String t = sp.resolveTypeIfNeeded(resolver);
681 if (t != null) {
682 if (specificTypes == null) {
683 specificTypes = new String[N];
684 }
685 specificTypes[i] = t;
686 }
687 }
688 }
689 }
690
691 try {
692 return mPM.queryIntentActivityOptions(caller, specifics,
693 specificTypes, intent, intent.resolveTypeIfNeeded(resolver),
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700694 flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800695 } catch (RemoteException e) {
696 throw new RuntimeException("Package manager has died", e);
697 }
698 }
699
Amith Yamasanif203aee2012-08-29 18:41:53 -0700700 /**
701 * @hide
702 */
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800703 @Override
Amith Yamasanif203aee2012-08-29 18:41:53 -0700704 public List<ResolveInfo> queryBroadcastReceivers(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800705 try {
706 return mPM.queryIntentReceivers(
707 intent,
708 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700709 flags,
Amith Yamasanif203aee2012-08-29 18:41:53 -0700710 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800711 } catch (RemoteException e) {
712 throw new RuntimeException("Package manager has died", e);
713 }
714 }
715
716 @Override
Amith Yamasanif203aee2012-08-29 18:41:53 -0700717 public List<ResolveInfo> queryBroadcastReceivers(Intent intent, int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700718 return queryBroadcastReceivers(intent, flags, mContext.getUserId());
Amith Yamasanif203aee2012-08-29 18:41:53 -0700719 }
720
721 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800722 public ResolveInfo resolveService(Intent intent, int flags) {
723 try {
724 return mPM.resolveService(
725 intent,
726 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700727 flags,
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700728 mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800729 } catch (RemoteException e) {
730 throw new RuntimeException("Package manager has died", e);
731 }
732 }
733
734 @Override
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700735 public List<ResolveInfo> queryIntentServicesAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800736 try {
737 return mPM.queryIntentServices(
738 intent,
739 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700740 flags,
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700741 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800742 } catch (RemoteException e) {
743 throw new RuntimeException("Package manager has died", e);
744 }
745 }
746
747 @Override
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700748 public List<ResolveInfo> queryIntentServices(Intent intent, int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700749 return queryIntentServicesAsUser(intent, flags, mContext.getUserId());
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700750 }
751
752 @Override
Jeff Sharkey85f5f812013-10-07 10:16:12 -0700753 public List<ResolveInfo> queryIntentContentProvidersAsUser(
754 Intent intent, int flags, int userId) {
755 try {
756 return mPM.queryIntentContentProviders(intent,
757 intent.resolveTypeIfNeeded(mContext.getContentResolver()), flags, userId);
758 } catch (RemoteException e) {
759 throw new RuntimeException("Package manager has died", e);
760 }
761 }
762
763 @Override
764 public List<ResolveInfo> queryIntentContentProviders(Intent intent, int flags) {
765 return queryIntentContentProvidersAsUser(intent, flags, mContext.getUserId());
766 }
767
768 @Override
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +0100769 public ProviderInfo resolveContentProvider(String name, int flags) {
770 return resolveContentProviderAsUser(name, flags, mContext.getUserId());
771 }
772
773 /** @hide **/
774 @Override
775 public ProviderInfo resolveContentProviderAsUser(String name, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800776 try {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +0100777 return mPM.resolveContentProvider(name, flags, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800778 } catch (RemoteException e) {
779 throw new RuntimeException("Package manager has died", e);
780 }
781 }
782
783 @Override
784 public List<ProviderInfo> queryContentProviders(String processName,
785 int uid, int flags) {
786 try {
Dianne Hackborn28ec27c2015-08-03 15:28:28 -0700787 ParceledListSlice<ProviderInfo> slice
788 = mPM.queryContentProviders(processName, uid, flags);
789 return slice != null ? slice.getList() : null;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800790 } catch (RemoteException e) {
791 throw new RuntimeException("Package manager has died", e);
792 }
793 }
794
795 @Override
796 public InstrumentationInfo getInstrumentationInfo(
797 ComponentName className, int flags)
798 throws NameNotFoundException {
799 try {
800 InstrumentationInfo ii = mPM.getInstrumentationInfo(
801 className, flags);
802 if (ii != null) {
803 return ii;
804 }
805 } catch (RemoteException e) {
806 throw new RuntimeException("Package manager has died", e);
807 }
808
809 throw new NameNotFoundException(className.toString());
810 }
811
812 @Override
813 public List<InstrumentationInfo> queryInstrumentation(
814 String targetPackage, int flags) {
815 try {
816 return mPM.queryInstrumentation(targetPackage, flags);
817 } catch (RemoteException e) {
818 throw new RuntimeException("Package manager has died", e);
819 }
820 }
821
Alan Viveretteecd585a2015-04-13 10:32:51 -0700822 @Nullable
823 @Override
824 public Drawable getDrawable(String packageName, @DrawableRes int resId,
825 @Nullable ApplicationInfo appInfo) {
826 final ResourceName name = new ResourceName(packageName, resId);
827 final Drawable cachedIcon = getCachedIcon(name);
828 if (cachedIcon != null) {
829 return cachedIcon;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800830 }
Alan Viveretteecd585a2015-04-13 10:32:51 -0700831
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800832 if (appInfo == null) {
833 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700834 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800835 } catch (NameNotFoundException e) {
836 return null;
837 }
838 }
Alan Viveretteecd585a2015-04-13 10:32:51 -0700839
840 if (resId != 0) {
841 try {
842 final Resources r = getResourcesForApplication(appInfo);
843 final Drawable dr = r.getDrawable(resId, null);
844 if (dr != null) {
845 putCachedIcon(name, dr);
846 }
847
848 if (false) {
849 RuntimeException e = new RuntimeException("here");
850 e.fillInStackTrace();
851 Log.w(TAG, "Getting drawable 0x" + Integer.toHexString(resId)
852 + " from package " + packageName
853 + ": app scale=" + r.getCompatibilityInfo().applicationScale
854 + ", caller scale=" + mContext.getResources()
855 .getCompatibilityInfo().applicationScale,
856 e);
857 }
Ricky Wai3ce46252015-04-15 16:12:22 +0100858 if (DEBUG_ICONS) {
Alan Viveretteecd585a2015-04-13 10:32:51 -0700859 Log.v(TAG, "Getting drawable 0x"
860 + Integer.toHexString(resId) + " from " + r
861 + ": " + dr);
Ricky Wai3ce46252015-04-15 16:12:22 +0100862 }
863 return dr;
Alan Viveretteecd585a2015-04-13 10:32:51 -0700864 } catch (NameNotFoundException e) {
865 Log.w("PackageManager", "Failure retrieving resources for "
866 + appInfo.packageName);
867 } catch (Resources.NotFoundException e) {
868 Log.w("PackageManager", "Failure retrieving resources for "
869 + appInfo.packageName + ": " + e.getMessage());
870 } catch (Exception e) {
871 // If an exception was thrown, fall through to return
872 // default icon.
873 Log.w("PackageManager", "Failure retrieving icon 0x"
874 + Integer.toHexString(resId) + " in package "
875 + packageName, e);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800876 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800877 }
Alan Viveretteecd585a2015-04-13 10:32:51 -0700878
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800879 return null;
880 }
881
882 @Override public Drawable getActivityIcon(ComponentName activityName)
883 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700884 return getActivityInfo(activityName, sDefaultFlags).loadIcon(this);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800885 }
886
887 @Override public Drawable getActivityIcon(Intent intent)
888 throws NameNotFoundException {
889 if (intent.getComponent() != null) {
890 return getActivityIcon(intent.getComponent());
891 }
892
893 ResolveInfo info = resolveActivity(
894 intent, PackageManager.MATCH_DEFAULT_ONLY);
895 if (info != null) {
896 return info.activityInfo.loadIcon(this);
897 }
898
Romain Guy39fe17c2011-11-30 10:34:07 -0800899 throw new NameNotFoundException(intent.toUri(0));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800900 }
901
902 @Override public Drawable getDefaultActivityIcon() {
903 return Resources.getSystem().getDrawable(
904 com.android.internal.R.drawable.sym_def_app_icon);
905 }
906
907 @Override public Drawable getApplicationIcon(ApplicationInfo info) {
908 return info.loadIcon(this);
909 }
910
911 @Override public Drawable getApplicationIcon(String packageName)
912 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700913 return getApplicationIcon(getApplicationInfo(packageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800914 }
915
916 @Override
Jose Limaf78e3122014-03-06 12:13:15 -0800917 public Drawable getActivityBanner(ComponentName activityName)
918 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700919 return getActivityInfo(activityName, sDefaultFlags).loadBanner(this);
Jose Limaf78e3122014-03-06 12:13:15 -0800920 }
921
922 @Override
923 public Drawable getActivityBanner(Intent intent)
924 throws NameNotFoundException {
925 if (intent.getComponent() != null) {
926 return getActivityBanner(intent.getComponent());
927 }
928
929 ResolveInfo info = resolveActivity(
930 intent, PackageManager.MATCH_DEFAULT_ONLY);
931 if (info != null) {
932 return info.activityInfo.loadBanner(this);
933 }
934
935 throw new NameNotFoundException(intent.toUri(0));
936 }
937
938 @Override
939 public Drawable getApplicationBanner(ApplicationInfo info) {
940 return info.loadBanner(this);
941 }
942
943 @Override
944 public Drawable getApplicationBanner(String packageName)
945 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700946 return getApplicationBanner(getApplicationInfo(packageName, sDefaultFlags));
Jose Limaf78e3122014-03-06 12:13:15 -0800947 }
948
949 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800950 public Drawable getActivityLogo(ComponentName activityName)
951 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700952 return getActivityInfo(activityName, sDefaultFlags).loadLogo(this);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800953 }
954
955 @Override
956 public Drawable getActivityLogo(Intent intent)
957 throws NameNotFoundException {
958 if (intent.getComponent() != null) {
959 return getActivityLogo(intent.getComponent());
960 }
961
962 ResolveInfo info = resolveActivity(
963 intent, PackageManager.MATCH_DEFAULT_ONLY);
964 if (info != null) {
965 return info.activityInfo.loadLogo(this);
966 }
967
968 throw new NameNotFoundException(intent.toUri(0));
969 }
970
971 @Override
972 public Drawable getApplicationLogo(ApplicationInfo info) {
973 return info.loadLogo(this);
974 }
975
976 @Override
977 public Drawable getApplicationLogo(String packageName)
978 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700979 return getApplicationLogo(getApplicationInfo(packageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800980 }
981
Svetoslavc7d62f02014-09-04 15:39:54 -0700982 @Override
983 public Drawable getUserBadgedIcon(Drawable icon, UserHandle user) {
984 final int badgeResId = getBadgeResIdForUser(user.getIdentifier());
985 if (badgeResId == 0) {
986 return icon;
987 }
988 Drawable badgeIcon = getDrawable("system", badgeResId, null);
989 return getBadgedDrawable(icon, badgeIcon, null, true);
990 }
991
992 @Override
993 public Drawable getUserBadgedDrawableForDensity(Drawable drawable, UserHandle user,
994 Rect badgeLocation, int badgeDensity) {
995 Drawable badgeDrawable = getUserBadgeForDensity(user, badgeDensity);
996 if (badgeDrawable == null) {
997 return drawable;
998 }
999 return getBadgedDrawable(drawable, badgeDrawable, badgeLocation, true);
1000 }
1001
1002 @Override
1003 public Drawable getUserBadgeForDensity(UserHandle user, int density) {
1004 UserInfo userInfo = getUserIfProfile(user.getIdentifier());
1005 if (userInfo != null && userInfo.isManagedProfile()) {
1006 if (density <= 0) {
1007 density = mContext.getResources().getDisplayMetrics().densityDpi;
1008 }
1009 return Resources.getSystem().getDrawableForDensity(
1010 com.android.internal.R.drawable.ic_corp_badge, density);
1011 }
1012 return null;
1013 }
1014
1015 @Override
1016 public CharSequence getUserBadgedLabel(CharSequence label, UserHandle user) {
1017 UserInfo userInfo = getUserIfProfile(user.getIdentifier());
1018 if (userInfo != null && userInfo.isManagedProfile()) {
1019 return Resources.getSystem().getString(
1020 com.android.internal.R.string.managed_profile_label_badge, label);
1021 }
1022 return label;
1023 }
1024
Alan Viveretteecd585a2015-04-13 10:32:51 -07001025 @Override
1026 public Resources getResourcesForActivity(ComponentName activityName)
1027 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001028 return getResourcesForApplication(
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001029 getActivityInfo(activityName, sDefaultFlags).applicationInfo);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001030 }
1031
Alan Viveretteecd585a2015-04-13 10:32:51 -07001032 @Override
1033 public Resources getResourcesForApplication(@NonNull ApplicationInfo app)
1034 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001035 if (app.packageName.equals("system")) {
1036 return mContext.mMainThread.getSystemContext().getResources();
1037 }
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07001038 final boolean sameUid = (app.uid == Process.myUid());
Alan Viveretteecd585a2015-04-13 10:32:51 -07001039 final Resources r = mContext.mMainThread.getTopLevelResources(
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07001040 sameUid ? app.sourceDir : app.publicSourceDir,
1041 sameUid ? app.splitSourceDirs : app.splitPublicSourceDirs,
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001042 app.resourceDirs, app.sharedLibraryFiles, Display.DEFAULT_DISPLAY,
1043 null, mContext.mPackageInfo);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001044 if (r != null) {
1045 return r;
1046 }
1047 throw new NameNotFoundException("Unable to open " + app.publicSourceDir);
1048 }
1049
Alan Viveretteecd585a2015-04-13 10:32:51 -07001050 @Override
1051 public Resources getResourcesForApplication(String appPackageName)
1052 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001053 return getResourcesForApplication(
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001054 getApplicationInfo(appPackageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001055 }
1056
Amith Yamasani98edc952012-09-25 14:09:27 -07001057 /** @hide */
1058 @Override
1059 public Resources getResourcesForApplicationAsUser(String appPackageName, int userId)
1060 throws NameNotFoundException {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001061 if (userId < 0) {
1062 throw new IllegalArgumentException(
1063 "Call does not support special user #" + userId);
1064 }
1065 if ("system".equals(appPackageName)) {
1066 return mContext.mMainThread.getSystemContext().getResources();
1067 }
Amith Yamasani98edc952012-09-25 14:09:27 -07001068 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001069 ApplicationInfo ai = mPM.getApplicationInfo(appPackageName, sDefaultFlags, userId);
Amith Yamasani98edc952012-09-25 14:09:27 -07001070 if (ai != null) {
1071 return getResourcesForApplication(ai);
1072 }
1073 } catch (RemoteException e) {
1074 throw new RuntimeException("Package manager has died", e);
1075 }
1076 throw new NameNotFoundException("Package " + appPackageName + " doesn't exist");
1077 }
1078
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001079 int mCachedSafeMode = -1;
1080 @Override public boolean isSafeMode() {
1081 try {
1082 if (mCachedSafeMode < 0) {
1083 mCachedSafeMode = mPM.isSafeMode() ? 1 : 0;
1084 }
1085 return mCachedSafeMode != 0;
1086 } catch (RemoteException e) {
1087 throw new RuntimeException("Package manager has died", e);
1088 }
1089 }
1090
Svetoslavf7c06eb2015-06-10 18:43:22 -07001091 @Override
1092 public void addOnPermissionsChangeListener(OnPermissionsChangedListener listener) {
1093 synchronized (mPermissionListeners) {
1094 if (mPermissionListeners.get(listener) != null) {
1095 return;
1096 }
1097 OnPermissionsChangeListenerDelegate delegate =
1098 new OnPermissionsChangeListenerDelegate(listener, Looper.getMainLooper());
1099 try {
1100 mPM.addOnPermissionsChangeListener(delegate);
1101 mPermissionListeners.put(listener, delegate);
1102 } catch (RemoteException e) {
1103 throw new RuntimeException("Package manager has died", e);
1104 }
1105 }
1106 }
1107
1108 @Override
1109 public void removeOnPermissionsChangeListener(OnPermissionsChangedListener listener) {
1110 synchronized (mPermissionListeners) {
1111 IOnPermissionsChangeListener delegate = mPermissionListeners.get(listener);
1112 if (delegate != null) {
1113 try {
1114 mPM.removeOnPermissionsChangeListener(delegate);
1115 mPermissionListeners.remove(listener);
1116 } catch (RemoteException e) {
1117 throw new RuntimeException("Package manager has died", e);
1118 }
1119 }
1120 }
1121 }
1122
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001123 static void configurationChanged() {
1124 synchronized (sSync) {
1125 sIconCache.clear();
1126 sStringCache.clear();
1127 }
1128 }
1129
1130 ApplicationPackageManager(ContextImpl context,
1131 IPackageManager pm) {
1132 mContext = context;
1133 mPM = pm;
1134 }
1135
Alan Viveretteecd585a2015-04-13 10:32:51 -07001136 @Nullable
1137 private Drawable getCachedIcon(@NonNull ResourceName name) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001138 synchronized (sSync) {
Alan Viveretteecd585a2015-04-13 10:32:51 -07001139 final WeakReference<Drawable.ConstantState> wr = sIconCache.get(name);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001140 if (DEBUG_ICONS) Log.v(TAG, "Get cached weak drawable ref for "
1141 + name + ": " + wr);
1142 if (wr != null) { // we have the activity
Alan Viveretteecd585a2015-04-13 10:32:51 -07001143 final Drawable.ConstantState state = wr.get();
Romain Guy39fe17c2011-11-30 10:34:07 -08001144 if (state != null) {
1145 if (DEBUG_ICONS) {
1146 Log.v(TAG, "Get cached drawable state for " + name + ": " + state);
1147 }
1148 // Note: It's okay here to not use the newDrawable(Resources) variant
1149 // of the API. The ConstantState comes from a drawable that was
1150 // originally created by passing the proper app Resources instance
1151 // which means the state should already contain the proper
1152 // resources specific information (like density.) See
1153 // BitmapDrawable.BitmapState for instance.
1154 return state.newDrawable();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001155 }
1156 // our entry has been purged
1157 sIconCache.remove(name);
1158 }
1159 }
1160 return null;
1161 }
1162
Alan Viveretteecd585a2015-04-13 10:32:51 -07001163 private void putCachedIcon(@NonNull ResourceName name, @NonNull Drawable dr) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001164 synchronized (sSync) {
Alan Viveretteecd585a2015-04-13 10:32:51 -07001165 sIconCache.put(name, new WeakReference<>(dr.getConstantState()));
Romain Guy39fe17c2011-11-30 10:34:07 -08001166 if (DEBUG_ICONS) Log.v(TAG, "Added cached drawable state for " + name + ": " + dr);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001167 }
1168 }
1169
Romain Guy39fe17c2011-11-30 10:34:07 -08001170 static void handlePackageBroadcast(int cmd, String[] pkgList, boolean hasPkgInfo) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001171 boolean immediateGc = false;
1172 if (cmd == IApplicationThread.EXTERNAL_STORAGE_UNAVAILABLE) {
1173 immediateGc = true;
1174 }
1175 if (pkgList != null && (pkgList.length > 0)) {
1176 boolean needCleanup = false;
1177 for (String ssp : pkgList) {
1178 synchronized (sSync) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07001179 for (int i=sIconCache.size()-1; i>=0; i--) {
1180 ResourceName nm = sIconCache.keyAt(i);
1181 if (nm.packageName.equals(ssp)) {
1182 //Log.i(TAG, "Removing cached drawable for " + nm);
1183 sIconCache.removeAt(i);
1184 needCleanup = true;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001185 }
1186 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07001187 for (int i=sStringCache.size()-1; i>=0; i--) {
1188 ResourceName nm = sStringCache.keyAt(i);
1189 if (nm.packageName.equals(ssp)) {
1190 //Log.i(TAG, "Removing cached string for " + nm);
1191 sStringCache.removeAt(i);
1192 needCleanup = true;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001193 }
1194 }
1195 }
1196 }
1197 if (needCleanup || hasPkgInfo) {
1198 if (immediateGc) {
1199 // Schedule an immediate gc.
1200 Runtime.getRuntime().gc();
1201 } else {
1202 ActivityThread.currentActivityThread().scheduleGcIdler();
1203 }
1204 }
1205 }
1206 }
1207
1208 private static final class ResourceName {
1209 final String packageName;
1210 final int iconId;
1211
1212 ResourceName(String _packageName, int _iconId) {
1213 packageName = _packageName;
1214 iconId = _iconId;
1215 }
1216
1217 ResourceName(ApplicationInfo aInfo, int _iconId) {
1218 this(aInfo.packageName, _iconId);
1219 }
1220
1221 ResourceName(ComponentInfo cInfo, int _iconId) {
1222 this(cInfo.applicationInfo.packageName, _iconId);
1223 }
1224
1225 ResourceName(ResolveInfo rInfo, int _iconId) {
1226 this(rInfo.activityInfo.applicationInfo.packageName, _iconId);
1227 }
1228
1229 @Override
1230 public boolean equals(Object o) {
1231 if (this == o) return true;
1232 if (o == null || getClass() != o.getClass()) return false;
1233
1234 ResourceName that = (ResourceName) o;
1235
1236 if (iconId != that.iconId) return false;
1237 return !(packageName != null ?
1238 !packageName.equals(that.packageName) : that.packageName != null);
1239
1240 }
1241
1242 @Override
1243 public int hashCode() {
1244 int result;
1245 result = packageName.hashCode();
1246 result = 31 * result + iconId;
1247 return result;
1248 }
1249
1250 @Override
1251 public String toString() {
1252 return "{ResourceName " + packageName + " / " + iconId + "}";
1253 }
1254 }
1255
1256 private CharSequence getCachedString(ResourceName name) {
1257 synchronized (sSync) {
1258 WeakReference<CharSequence> wr = sStringCache.get(name);
1259 if (wr != null) { // we have the activity
1260 CharSequence cs = wr.get();
1261 if (cs != null) {
1262 return cs;
1263 }
1264 // our entry has been purged
1265 sStringCache.remove(name);
1266 }
1267 }
1268 return null;
1269 }
1270
1271 private void putCachedString(ResourceName name, CharSequence cs) {
1272 synchronized (sSync) {
1273 sStringCache.put(name, new WeakReference<CharSequence>(cs));
1274 }
1275 }
1276
1277 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07001278 public CharSequence getText(String packageName, @StringRes int resid,
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001279 ApplicationInfo appInfo) {
1280 ResourceName name = new ResourceName(packageName, resid);
1281 CharSequence text = getCachedString(name);
1282 if (text != null) {
1283 return text;
1284 }
1285 if (appInfo == null) {
1286 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001287 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001288 } catch (NameNotFoundException e) {
1289 return null;
1290 }
1291 }
1292 try {
1293 Resources r = getResourcesForApplication(appInfo);
1294 text = r.getText(resid);
1295 putCachedString(name, text);
1296 return text;
1297 } catch (NameNotFoundException e) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001298 Log.w("PackageManager", "Failure retrieving resources for "
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001299 + appInfo.packageName);
1300 } catch (RuntimeException e) {
1301 // If an exception was thrown, fall through to return
1302 // default icon.
1303 Log.w("PackageManager", "Failure retrieving text 0x"
1304 + Integer.toHexString(resid) + " in package "
1305 + packageName, e);
1306 }
1307 return null;
1308 }
1309
1310 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07001311 public XmlResourceParser getXml(String packageName, @XmlRes int resid,
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001312 ApplicationInfo appInfo) {
1313 if (appInfo == null) {
1314 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001315 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001316 } catch (NameNotFoundException e) {
1317 return null;
1318 }
1319 }
1320 try {
1321 Resources r = getResourcesForApplication(appInfo);
1322 return r.getXml(resid);
1323 } catch (RuntimeException e) {
1324 // If an exception was thrown, fall through to return
1325 // default icon.
1326 Log.w("PackageManager", "Failure retrieving xml 0x"
1327 + Integer.toHexString(resid) + " in package "
1328 + packageName, e);
1329 } catch (NameNotFoundException e) {
Alon Albert3fa51e32010-11-11 09:24:04 -08001330 Log.w("PackageManager", "Failure retrieving resources for "
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001331 + appInfo.packageName);
1332 }
1333 return null;
1334 }
1335
1336 @Override
1337 public CharSequence getApplicationLabel(ApplicationInfo info) {
1338 return info.loadLabel(this);
1339 }
1340
1341 @Override
1342 public void installPackage(Uri packageURI, IPackageInstallObserver observer, int flags,
1343 String installerPackageName) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001344 final VerificationParams verificationParams = new VerificationParams(null, null,
1345 null, VerificationParams.NO_UID, null);
1346 installCommon(packageURI, new LegacyPackageInstallObserver(observer), flags,
Robin Lee0e27c872015-09-28 14:37:40 +01001347 installerPackageName, verificationParams, null, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001348 }
1349
1350 @Override
Kenny Root5ab21572011-07-27 11:11:19 -07001351 public void installPackageWithVerification(Uri packageURI, IPackageInstallObserver observer,
1352 int flags, String installerPackageName, Uri verificationURI,
Rich Canningse1d7c712012-08-08 12:46:06 -07001353 ManifestDigest manifestDigest, ContainerEncryptionParams encryptionParams) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001354 final VerificationParams verificationParams = new VerificationParams(verificationURI, null,
1355 null, VerificationParams.NO_UID, manifestDigest);
1356 installCommon(packageURI, new LegacyPackageInstallObserver(observer), flags,
Robin Lee0e27c872015-09-28 14:37:40 +01001357 installerPackageName, verificationParams, encryptionParams, mContext.getUserId());
Kenny Root5ab21572011-07-27 11:11:19 -07001358 }
1359
1360 @Override
John Spurlock8a985d22014-02-25 09:40:05 -05001361 public void installPackageWithVerificationAndEncryption(Uri packageURI,
rich cannings706e8ba2012-08-20 13:20:14 -07001362 IPackageInstallObserver observer, int flags, String installerPackageName,
1363 VerificationParams verificationParams, ContainerEncryptionParams encryptionParams) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001364 installCommon(packageURI, new LegacyPackageInstallObserver(observer), flags,
Robin Lee0e27c872015-09-28 14:37:40 +01001365 installerPackageName, verificationParams, encryptionParams, mContext.getUserId());
Christopher Tatef1977b42014-03-24 16:25:51 -07001366 }
1367
Christopher Tatef1977b42014-03-24 16:25:51 -07001368 @Override
1369 public void installPackage(Uri packageURI, PackageInstallObserver observer,
1370 int flags, String installerPackageName) {
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001371 installPackageAsUser(packageURI, observer, flags, installerPackageName,
Robin Lee0e27c872015-09-28 14:37:40 +01001372 mContext.getUserId());
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001373 }
1374
1375 @Override
1376 public void installPackageAsUser(Uri packageURI, PackageInstallObserver observer, int flags,
1377 String installerPackageName, int userId) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001378 final VerificationParams verificationParams = new VerificationParams(null, null,
1379 null, VerificationParams.NO_UID, null);
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001380 installCommon(packageURI, observer, flags, installerPackageName, verificationParams, null,
1381 userId);
Christopher Tatef1977b42014-03-24 16:25:51 -07001382 }
1383
1384 @Override
1385 public void installPackageWithVerification(Uri packageURI,
1386 PackageInstallObserver observer, int flags, String installerPackageName,
1387 Uri verificationURI, ManifestDigest manifestDigest,
1388 ContainerEncryptionParams encryptionParams) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001389 final VerificationParams verificationParams = new VerificationParams(verificationURI, null,
1390 null, VerificationParams.NO_UID, manifestDigest);
1391 installCommon(packageURI, observer, flags, installerPackageName, verificationParams,
Robin Lee0e27c872015-09-28 14:37:40 +01001392 encryptionParams, mContext.getUserId());
Christopher Tatef1977b42014-03-24 16:25:51 -07001393 }
1394
1395 @Override
1396 public void installPackageWithVerificationAndEncryption(Uri packageURI,
1397 PackageInstallObserver observer, int flags, String installerPackageName,
1398 VerificationParams verificationParams, ContainerEncryptionParams encryptionParams) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001399 installCommon(packageURI, observer, flags, installerPackageName, verificationParams,
Robin Lee0e27c872015-09-28 14:37:40 +01001400 encryptionParams, mContext.getUserId());
Jeff Sharkey513a0742014-07-08 17:10:32 -07001401 }
1402
1403 private void installCommon(Uri packageURI,
1404 PackageInstallObserver observer, int flags, String installerPackageName,
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001405 VerificationParams verificationParams, ContainerEncryptionParams encryptionParams,
1406 int userId) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001407 if (!"file".equals(packageURI.getScheme())) {
1408 throw new UnsupportedOperationException("Only file:// URIs are supported");
1409 }
1410 if (encryptionParams != null) {
1411 throw new UnsupportedOperationException("ContainerEncryptionParams not supported");
1412 }
1413
1414 final String originPath = packageURI.getPath();
Christopher Tatef1977b42014-03-24 16:25:51 -07001415 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001416 mPM.installPackageAsUser(originPath, observer.getBinder(), flags, installerPackageName,
1417 verificationParams, null, userId);
Jeff Sharkey513a0742014-07-08 17:10:32 -07001418 } catch (RemoteException ignored) {
rich cannings706e8ba2012-08-20 13:20:14 -07001419 }
1420 }
1421
1422 @Override
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001423 public int installExistingPackage(String packageName) throws NameNotFoundException {
Robin Lee0e27c872015-09-28 14:37:40 +01001424 return installExistingPackageAsUser(packageName, mContext.getUserId());
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001425 }
1426
1427 @Override
1428 public int installExistingPackageAsUser(String packageName, int userId)
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001429 throws NameNotFoundException {
1430 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001431 int res = mPM.installExistingPackageAsUser(packageName, userId);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001432 if (res == INSTALL_FAILED_INVALID_URI) {
1433 throw new NameNotFoundException("Package " + packageName + " doesn't exist");
1434 }
1435 return res;
1436 } catch (RemoteException e) {
1437 // Should never happen!
1438 throw new NameNotFoundException("Package " + packageName + " doesn't exist");
1439 }
1440 }
1441
1442 @Override
Kenny Root3a9b5fb2011-09-20 14:15:38 -07001443 public void verifyPendingInstall(int id, int response) {
Kenny Root5ab21572011-07-27 11:11:19 -07001444 try {
Kenny Root3a9b5fb2011-09-20 14:15:38 -07001445 mPM.verifyPendingInstall(id, response);
Kenny Root5ab21572011-07-27 11:11:19 -07001446 } catch (RemoteException e) {
1447 // Should never happen!
1448 }
1449 }
1450
1451 @Override
rich canningsd9ef3e52012-08-22 14:28:05 -07001452 public void extendVerificationTimeout(int id, int verificationCodeAtTimeout,
1453 long millisecondsToDelay) {
1454 try {
1455 mPM.extendVerificationTimeout(id, verificationCodeAtTimeout, millisecondsToDelay);
1456 } catch (RemoteException e) {
1457 // Should never happen!
1458 }
1459 }
1460
1461 @Override
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001462 public void verifyIntentFilter(int id, int verificationCode, List<String> outFailedDomains) {
1463 try {
1464 mPM.verifyIntentFilter(id, verificationCode, outFailedDomains);
1465 } catch (RemoteException e) {
1466 // Should never happen!
1467 }
1468 }
1469
1470 @Override
1471 public int getIntentVerificationStatus(String packageName, int userId) {
1472 try {
1473 return mPM.getIntentVerificationStatus(packageName, userId);
1474 } catch (RemoteException e) {
1475 // Should never happen!
1476 return PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED;
1477 }
1478 }
1479
1480 @Override
1481 public boolean updateIntentVerificationStatus(String packageName, int status, int userId) {
1482 try {
1483 return mPM.updateIntentVerificationStatus(packageName, status, userId);
1484 } catch (RemoteException e) {
1485 // Should never happen!
1486 return false;
1487 }
1488 }
1489
1490 @Override
1491 public List<IntentFilterVerificationInfo> getIntentFilterVerifications(String packageName) {
1492 try {
1493 return mPM.getIntentFilterVerifications(packageName);
1494 } catch (RemoteException e) {
1495 // Should never happen!
1496 return null;
1497 }
1498 }
1499
1500 @Override
Fabrice Di Meglio07885952015-04-06 19:41:28 -07001501 public List<IntentFilter> getAllIntentFilters(String packageName) {
1502 try {
1503 return mPM.getAllIntentFilters(packageName);
1504 } catch (RemoteException e) {
1505 // Should never happen!
1506 return null;
1507 }
1508 }
1509
1510 @Override
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001511 public String getDefaultBrowserPackageName(int userId) {
1512 try {
1513 return mPM.getDefaultBrowserPackageName(userId);
1514 } catch (RemoteException e) {
1515 // Should never happen!
1516 return null;
1517 }
1518 }
1519
1520 @Override
1521 public boolean setDefaultBrowserPackageName(String packageName, int userId) {
1522 try {
1523 return mPM.setDefaultBrowserPackageName(packageName, userId);
1524 } catch (RemoteException e) {
1525 // Should never happen!
1526 return false;
1527 }
1528 }
1529
1530 @Override
Dianne Hackborn880119b2010-11-18 22:26:40 -08001531 public void setInstallerPackageName(String targetPackage,
1532 String installerPackageName) {
1533 try {
1534 mPM.setInstallerPackageName(targetPackage, installerPackageName);
1535 } catch (RemoteException e) {
1536 // Should never happen!
1537 }
1538 }
1539
1540 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001541 public String getInstallerPackageName(String packageName) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001542 try {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001543 return mPM.getInstallerPackageName(packageName);
1544 } catch (RemoteException e) {
1545 // Should never happen!
1546 }
1547 return null;
1548 }
1549
1550 @Override
1551 public int getMoveStatus(int moveId) {
1552 try {
1553 return mPM.getMoveStatus(moveId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001554 } catch (RemoteException e) {
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -07001555 throw e.rethrowAsRuntimeException();
1556 }
1557 }
1558
1559 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001560 public void registerMoveCallback(MoveCallback callback, Handler handler) {
1561 synchronized (mDelegates) {
1562 final MoveCallbackDelegate delegate = new MoveCallbackDelegate(callback,
1563 handler.getLooper());
1564 try {
1565 mPM.registerMoveCallback(delegate);
1566 } catch (RemoteException e) {
1567 throw e.rethrowAsRuntimeException();
1568 }
1569 mDelegates.add(delegate);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001570 }
1571 }
1572
1573 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001574 public void unregisterMoveCallback(MoveCallback callback) {
1575 synchronized (mDelegates) {
1576 for (Iterator<MoveCallbackDelegate> i = mDelegates.iterator(); i.hasNext();) {
1577 final MoveCallbackDelegate delegate = i.next();
1578 if (delegate.mCallback == callback) {
1579 try {
1580 mPM.unregisterMoveCallback(delegate);
1581 } catch (RemoteException e) {
1582 throw e.rethrowAsRuntimeException();
1583 }
1584 i.remove();
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001585 }
1586 }
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001587 }
1588 }
1589
1590 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001591 public int movePackage(String packageName, VolumeInfo vol) {
1592 try {
1593 final String volumeUuid;
1594 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.id)) {
1595 volumeUuid = StorageManager.UUID_PRIVATE_INTERNAL;
1596 } else if (vol.isPrimaryPhysical()) {
1597 volumeUuid = StorageManager.UUID_PRIMARY_PHYSICAL;
1598 } else {
1599 volumeUuid = Preconditions.checkNotNull(vol.fsUuid);
1600 }
1601
1602 return mPM.movePackage(packageName, volumeUuid);
1603 } catch (RemoteException e) {
1604 throw e.rethrowAsRuntimeException();
1605 }
1606 }
1607
1608 @Override
1609 public @Nullable VolumeInfo getPackageCurrentVolume(ApplicationInfo app) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001610 final StorageManager storage = mContext.getSystemService(StorageManager.class);
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001611 if (app.isInternal()) {
1612 return storage.findVolumeById(VolumeInfo.ID_PRIVATE_INTERNAL);
1613 } else if (app.isExternalAsec()) {
1614 return storage.getPrimaryPhysicalVolume();
1615 } else {
1616 return storage.findVolumeByUuid(app.volumeUuid);
1617 }
1618 }
1619
1620 @Override
1621 public @NonNull List<VolumeInfo> getPackageCandidateVolumes(ApplicationInfo app) {
1622 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1623 final VolumeInfo currentVol = getPackageCurrentVolume(app);
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001624 final List<VolumeInfo> vols = storage.getVolumes();
1625 final List<VolumeInfo> candidates = new ArrayList<>();
1626 for (VolumeInfo vol : vols) {
Todd Kennedyf39ca8f2015-08-07 14:15:07 -07001627 if (Objects.equals(vol, currentVol) || isPackageCandidateVolume(mContext, app, vol)) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001628 candidates.add(vol);
1629 }
1630 }
1631 return candidates;
1632 }
1633
Todd Kennedyf39ca8f2015-08-07 14:15:07 -07001634 private static boolean isPackageCandidateVolume(
1635 ContextImpl context, ApplicationInfo app, VolumeInfo vol) {
1636 final boolean forceAllowOnExternal = Settings.Global.getInt(
1637 context.getContentResolver(), Settings.Global.FORCE_ALLOW_ON_EXTERNAL, 0) != 0;
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001638 // Private internal is always an option
1639 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.getId())) {
1640 return true;
1641 }
1642
1643 // System apps and apps demanding internal storage can't be moved
1644 // anywhere else
Todd Kennedyf39ca8f2015-08-07 14:15:07 -07001645 if (app.isSystemApp()) {
1646 return false;
1647 }
1648 if (!forceAllowOnExternal
Dianne Hackborn30a4e6d2015-10-12 17:14:56 -07001649 && (app.installLocation == PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY
1650 || app.installLocation == PackageInfo.INSTALL_LOCATION_UNSPECIFIED)) {
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001651 return false;
1652 }
1653
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001654 // Gotta be able to write there
1655 if (!vol.isMountedWritable()) {
1656 return false;
1657 }
1658
1659 // Moving into an ASEC on public primary is only option internal
1660 if (vol.isPrimaryPhysical()) {
1661 return app.isInternal();
Jeff Sharkeye2d45be2015-04-15 17:14:12 -07001662 }
1663
1664 // Otherwise we can move to any private volume
1665 return (vol.getType() == VolumeInfo.TYPE_PRIVATE);
1666 }
1667
1668 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001669 public int movePrimaryStorage(VolumeInfo vol) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001670 try {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001671 final String volumeUuid;
1672 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.id)) {
1673 volumeUuid = StorageManager.UUID_PRIVATE_INTERNAL;
1674 } else if (vol.isPrimaryPhysical()) {
1675 volumeUuid = StorageManager.UUID_PRIMARY_PHYSICAL;
1676 } else {
1677 volumeUuid = Preconditions.checkNotNull(vol.fsUuid);
1678 }
1679
1680 return mPM.movePrimaryStorage(volumeUuid);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001681 } catch (RemoteException e) {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001682 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001683 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001684 }
1685
Jeff Sharkey275e3e42015-04-24 16:10:32 -07001686 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001687 public @Nullable VolumeInfo getPrimaryStorageCurrentVolume() {
1688 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1689 final String volumeUuid = storage.getPrimaryStorageUuid();
Jeff Sharkey50a05452015-04-29 11:24:52 -07001690 return storage.findVolumeByQualifiedUuid(volumeUuid);
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001691 }
1692
Jeff Sharkey275e3e42015-04-24 16:10:32 -07001693 @Override
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001694 public @NonNull List<VolumeInfo> getPrimaryStorageCandidateVolumes() {
1695 final StorageManager storage = mContext.getSystemService(StorageManager.class);
1696 final VolumeInfo currentVol = getPrimaryStorageCurrentVolume();
1697 final List<VolumeInfo> vols = storage.getVolumes();
1698 final List<VolumeInfo> candidates = new ArrayList<>();
Jeff Sharkeyfced5342015-05-10 14:53:34 -07001699 if (Objects.equals(StorageManager.UUID_PRIMARY_PHYSICAL,
1700 storage.getPrimaryStorageUuid()) && currentVol != null) {
1701 // TODO: support moving primary physical to emulated volume
1702 candidates.add(currentVol);
1703 } else {
1704 for (VolumeInfo vol : vols) {
1705 if (Objects.equals(vol, currentVol) || isPrimaryStorageCandidateVolume(vol)) {
1706 candidates.add(vol);
1707 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001708 }
1709 }
1710 return candidates;
1711 }
1712
1713 private static boolean isPrimaryStorageCandidateVolume(VolumeInfo vol) {
1714 // Private internal is always an option
1715 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(vol.getId())) {
1716 return true;
1717 }
1718
1719 // Gotta be able to write there
1720 if (!vol.isMountedWritable()) {
1721 return false;
1722 }
1723
Jeff Sharkeyfced5342015-05-10 14:53:34 -07001724 // We can move to any private volume
Jeff Sharkey620b32b2015-04-23 19:36:02 -07001725 return (vol.getType() == VolumeInfo.TYPE_PRIVATE);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001726 }
1727
1728 @Override
1729 public void deletePackage(String packageName, IPackageDeleteObserver observer, int flags) {
Robin Lee0e27c872015-09-28 14:37:40 +01001730 deletePackageAsUser(packageName, observer, flags, mContext.getUserId());
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001731 }
1732
1733 @Override
1734 public void deletePackageAsUser(String packageName, IPackageDeleteObserver observer, int flags,
1735 int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001736 try {
Nicolas Prevot9a80e532015-09-23 15:49:28 +01001737 mPM.deletePackageAsUser(packageName, observer, userId, flags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001738 } catch (RemoteException e) {
1739 // Should never happen!
1740 }
1741 }
Jeff Sharkeyfbd0e9f2014-08-06 16:34:34 -07001742
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001743 @Override
1744 public void clearApplicationUserData(String packageName,
1745 IPackageDataObserver observer) {
1746 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001747 mPM.clearApplicationUserData(packageName, observer, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001748 } catch (RemoteException e) {
1749 // Should never happen!
1750 }
1751 }
1752 @Override
1753 public void deleteApplicationCacheFiles(String packageName,
1754 IPackageDataObserver observer) {
1755 try {
1756 mPM.deleteApplicationCacheFiles(packageName, observer);
1757 } catch (RemoteException e) {
1758 // Should never happen!
1759 }
1760 }
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001761
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001762 @Override
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001763 public void freeStorageAndNotify(String volumeUuid, long idealStorageSize,
1764 IPackageDataObserver observer) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001765 try {
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001766 mPM.freeStorageAndNotify(volumeUuid, idealStorageSize, observer);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001767 } catch (RemoteException e) {
1768 // Should never happen!
1769 }
1770 }
1771
1772 @Override
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001773 public void freeStorage(String volumeUuid, long freeStorageSize, IntentSender pi) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001774 try {
Jeff Sharkey529f91f2015-04-18 20:23:13 -07001775 mPM.freeStorage(volumeUuid, freeStorageSize, pi);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001776 } catch (RemoteException e) {
1777 // Should never happen!
1778 }
1779 }
1780
1781 @Override
Dianne Hackborn0c380492012-08-20 17:23:30 -07001782 public void getPackageSizeInfo(String packageName, int userHandle,
1783 IPackageStatsObserver observer) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001784 try {
Dianne Hackborn0c380492012-08-20 17:23:30 -07001785 mPM.getPackageSizeInfo(packageName, userHandle, observer);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001786 } catch (RemoteException e) {
1787 // Should never happen!
1788 }
1789 }
1790 @Override
1791 public void addPackageToPreferred(String packageName) {
1792 try {
1793 mPM.addPackageToPreferred(packageName);
1794 } catch (RemoteException e) {
1795 // Should never happen!
1796 }
1797 }
1798
1799 @Override
1800 public void removePackageFromPreferred(String packageName) {
1801 try {
1802 mPM.removePackageFromPreferred(packageName);
1803 } catch (RemoteException e) {
1804 // Should never happen!
1805 }
1806 }
1807
1808 @Override
1809 public List<PackageInfo> getPreferredPackages(int flags) {
1810 try {
1811 return mPM.getPreferredPackages(flags);
1812 } catch (RemoteException e) {
1813 // Should never happen!
1814 }
1815 return new ArrayList<PackageInfo>();
1816 }
1817
1818 @Override
1819 public void addPreferredActivity(IntentFilter filter,
1820 int match, ComponentName[] set, ComponentName activity) {
1821 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001822 mPM.addPreferredActivity(filter, match, set, activity, mContext.getUserId());
Amith Yamasania3f133a2012-08-09 17:11:28 -07001823 } catch (RemoteException e) {
1824 // Should never happen!
1825 }
1826 }
1827
1828 @Override
1829 public void addPreferredActivity(IntentFilter filter, int match,
1830 ComponentName[] set, ComponentName activity, int userId) {
1831 try {
1832 mPM.addPreferredActivity(filter, match, set, activity, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001833 } catch (RemoteException e) {
1834 // Should never happen!
1835 }
1836 }
1837
1838 @Override
1839 public void replacePreferredActivity(IntentFilter filter,
1840 int match, ComponentName[] set, ComponentName activity) {
1841 try {
Robin Lee0e27c872015-09-28 14:37:40 +01001842 mPM.replacePreferredActivity(filter, match, set, activity, mContext.getUserId());
Amith Yamasani41c1ded2014-08-05 11:15:05 -07001843 } catch (RemoteException e) {
1844 // Should never happen!
1845 }
1846 }
1847
1848 @Override
1849 public void replacePreferredActivityAsUser(IntentFilter filter,
1850 int match, ComponentName[] set, ComponentName activity,
1851 int userId) {
1852 try {
1853 mPM.replacePreferredActivity(filter, match, set, activity, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001854 } catch (RemoteException e) {
1855 // Should never happen!
1856 }
1857 }
1858
1859 @Override
1860 public void clearPackagePreferredActivities(String packageName) {
1861 try {
1862 mPM.clearPackagePreferredActivities(packageName);
1863 } catch (RemoteException e) {
1864 // Should never happen!
1865 }
1866 }
1867
1868 @Override
1869 public int getPreferredActivities(List<IntentFilter> outFilters,
1870 List<ComponentName> outActivities, String packageName) {
1871 try {
1872 return mPM.getPreferredActivities(outFilters, outActivities, packageName);
1873 } catch (RemoteException e) {
1874 // Should never happen!
1875 }
1876 return 0;
1877 }
1878
1879 @Override
Christopher Tatea2a0850d2013-09-05 16:38:58 -07001880 public ComponentName getHomeActivities(List<ResolveInfo> outActivities) {
1881 try {
1882 return mPM.getHomeActivities(outActivities);
1883 } catch (RemoteException e) {
1884 // Should never happen!
1885 }
1886 return null;
1887 }
1888
1889 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001890 public void setComponentEnabledSetting(ComponentName componentName,
1891 int newState, int flags) {
1892 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001893 mPM.setComponentEnabledSetting(componentName, newState, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001894 } catch (RemoteException e) {
1895 // Should never happen!
1896 }
1897 }
1898
1899 @Override
1900 public int getComponentEnabledSetting(ComponentName componentName) {
1901 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001902 return mPM.getComponentEnabledSetting(componentName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001903 } catch (RemoteException e) {
1904 // Should never happen!
1905 }
1906 return PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
1907 }
1908
1909 @Override
1910 public void setApplicationEnabledSetting(String packageName,
1911 int newState, int flags) {
1912 try {
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001913 mPM.setApplicationEnabledSetting(packageName, newState, flags,
Dianne Hackborn95d78532013-09-11 09:51:14 -07001914 mContext.getUserId(), mContext.getOpPackageName());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001915 } catch (RemoteException e) {
1916 // Should never happen!
1917 }
1918 }
1919
1920 @Override
1921 public int getApplicationEnabledSetting(String packageName) {
1922 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001923 return mPM.getApplicationEnabledSetting(packageName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001924 } catch (RemoteException e) {
1925 // Should never happen!
1926 }
1927 return PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
1928 }
1929
Amith Yamasani655d0e22013-06-12 14:19:10 -07001930 @Override
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001931 public boolean setApplicationHiddenSettingAsUser(String packageName, boolean hidden,
Amith Yamasani655d0e22013-06-12 14:19:10 -07001932 UserHandle user) {
1933 try {
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001934 return mPM.setApplicationHiddenSettingAsUser(packageName, hidden,
Amith Yamasani655d0e22013-06-12 14:19:10 -07001935 user.getIdentifier());
1936 } catch (RemoteException re) {
1937 // Should never happen!
1938 }
1939 return false;
1940 }
1941
1942 @Override
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001943 public boolean getApplicationHiddenSettingAsUser(String packageName, UserHandle user) {
Amith Yamasani655d0e22013-06-12 14:19:10 -07001944 try {
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001945 return mPM.getApplicationHiddenSettingAsUser(packageName, user.getIdentifier());
Amith Yamasani655d0e22013-06-12 14:19:10 -07001946 } catch (RemoteException re) {
1947 // Should never happen!
1948 }
1949 return false;
1950 }
1951
dcashmanc6f22492014-08-14 09:54:51 -07001952 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07001953 @Override
1954 public KeySet getKeySetByAlias(String packageName, String alias) {
1955 Preconditions.checkNotNull(packageName);
1956 Preconditions.checkNotNull(alias);
dcashmanc6f22492014-08-14 09:54:51 -07001957 KeySet ks;
dcashman9d2f4412014-06-09 09:27:54 -07001958 try {
dcashmanc6f22492014-08-14 09:54:51 -07001959 ks = mPM.getKeySetByAlias(packageName, alias);
dcashman9d2f4412014-06-09 09:27:54 -07001960 } catch (RemoteException e) {
1961 return null;
1962 }
dcashmanc6f22492014-08-14 09:54:51 -07001963 return ks;
dcashman9d2f4412014-06-09 09:27:54 -07001964 }
1965
dcashmanc6f22492014-08-14 09:54:51 -07001966 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07001967 @Override
1968 public KeySet getSigningKeySet(String packageName) {
1969 Preconditions.checkNotNull(packageName);
dcashmanc6f22492014-08-14 09:54:51 -07001970 KeySet ks;
dcashman9d2f4412014-06-09 09:27:54 -07001971 try {
dcashmanc6f22492014-08-14 09:54:51 -07001972 ks = mPM.getSigningKeySet(packageName);
dcashman9d2f4412014-06-09 09:27:54 -07001973 } catch (RemoteException e) {
1974 return null;
1975 }
dcashmanc6f22492014-08-14 09:54:51 -07001976 return ks;
dcashman9d2f4412014-06-09 09:27:54 -07001977 }
1978
dcashmanc6f22492014-08-14 09:54:51 -07001979 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07001980 @Override
1981 public boolean isSignedBy(String packageName, KeySet ks) {
1982 Preconditions.checkNotNull(packageName);
1983 Preconditions.checkNotNull(ks);
dcashman9d2f4412014-06-09 09:27:54 -07001984 try {
dcashmanc6f22492014-08-14 09:54:51 -07001985 return mPM.isPackageSignedByKeySet(packageName, ks);
dcashman9d2f4412014-06-09 09:27:54 -07001986 } catch (RemoteException e) {
1987 return false;
1988 }
1989 }
1990
dcashmanc6f22492014-08-14 09:54:51 -07001991 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07001992 @Override
1993 public boolean isSignedByExactly(String packageName, KeySet ks) {
1994 Preconditions.checkNotNull(packageName);
1995 Preconditions.checkNotNull(ks);
dcashman9d2f4412014-06-09 09:27:54 -07001996 try {
dcashmanc6f22492014-08-14 09:54:51 -07001997 return mPM.isPackageSignedByKeySetExactly(packageName, ks);
dcashman9d2f4412014-06-09 09:27:54 -07001998 } catch (RemoteException e) {
1999 return false;
2000 }
2001 }
2002
Kenny Root0aaa0d92011-09-12 16:42:55 -07002003 /**
2004 * @hide
2005 */
2006 @Override
2007 public VerifierDeviceIdentity getVerifierDeviceIdentity() {
2008 try {
2009 return mPM.getVerifierDeviceIdentity();
2010 } catch (RemoteException e) {
2011 // Should never happen!
2012 }
2013 return null;
2014 }
2015
Jeff Hao9f60c082014-10-28 18:51:07 -07002016 /**
2017 * @hide
2018 */
2019 @Override
2020 public boolean isUpgrade() {
2021 try {
2022 return mPM.isUpgrade();
2023 } catch (RemoteException e) {
2024 return false;
2025 }
2026 }
2027
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07002028 @Override
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07002029 public PackageInstaller getPackageInstaller() {
2030 synchronized (mLock) {
2031 if (mInstaller == null) {
2032 try {
Jeff Sharkeya0907432014-08-15 10:23:11 -07002033 mInstaller = new PackageInstaller(mContext, this, mPM.getPackageInstaller(),
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07002034 mContext.getPackageName(), mContext.getUserId());
2035 } catch (RemoteException e) {
2036 throw e.rethrowAsRuntimeException();
2037 }
2038 }
2039 return mInstaller;
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07002040 }
2041 }
2042
Jeff Sharkey6c833e02014-07-14 22:44:30 -07002043 @Override
2044 public boolean isPackageAvailable(String packageName) {
2045 try {
2046 return mPM.isPackageAvailable(packageName, mContext.getUserId());
2047 } catch (RemoteException e) {
2048 throw e.rethrowAsRuntimeException();
2049 }
2050 }
2051
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002052 /**
2053 * @hide
2054 */
2055 @Override
Nicolas Prevot63798c52014-05-27 13:22:38 +01002056 public void addCrossProfileIntentFilter(IntentFilter filter, int sourceUserId, int targetUserId,
2057 int flags) {
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002058 try {
Nicolas Prevot3f7777f2014-07-24 15:58:39 +01002059 mPM.addCrossProfileIntentFilter(filter, mContext.getOpPackageName(),
Nicolas Prevot4b8d5822015-03-05 15:20:49 +00002060 sourceUserId, targetUserId, flags);
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002061 } catch (RemoteException e) {
2062 // Should never happen!
2063 }
2064 }
2065
2066 /**
2067 * @hide
2068 */
2069 @Override
Nicolas Prevot81948992014-05-16 18:25:26 +01002070 public void clearCrossProfileIntentFilters(int sourceUserId) {
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002071 try {
Nicolas Prevot4b8d5822015-03-05 15:20:49 +00002072 mPM.clearCrossProfileIntentFilters(sourceUserId, mContext.getOpPackageName());
Nicolas Prevotc79586e2014-05-06 12:47:57 +01002073 } catch (RemoteException e) {
2074 // Should never happen!
2075 }
2076 }
2077
Nicolas Prevot88cc3462014-05-14 14:51:48 +01002078 /**
2079 * @hide
2080 */
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002081 public Drawable loadItemIcon(PackageItemInfo itemInfo, ApplicationInfo appInfo) {
Benjamin Franzec2d48b2014-10-01 15:38:43 +01002082 Drawable dr = loadUnbadgedItemIcon(itemInfo, appInfo);
2083 if (itemInfo.showUserIcon != UserHandle.USER_NULL) {
2084 return dr;
2085 }
2086 return getUserBadgedIcon(dr, new UserHandle(mContext.getUserId()));
2087 }
2088
2089 /**
2090 * @hide
2091 */
2092 public Drawable loadUnbadgedItemIcon(PackageItemInfo itemInfo, ApplicationInfo appInfo) {
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002093 if (itemInfo.showUserIcon != UserHandle.USER_NULL) {
Alexandra Gherghina64d4dca2014-08-28 18:26:56 +01002094 Bitmap bitmap = getUserManager().getUserIcon(itemInfo.showUserIcon);
2095 if (bitmap == null) {
2096 return UserIcons.getDefaultUserIcon(itemInfo.showUserIcon, /* light= */ false);
2097 }
2098 return new BitmapDrawable(bitmap);
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002099 }
Alexandra Gherghinadb811db2014-08-29 13:43:59 +01002100 Drawable dr = null;
2101 if (itemInfo.packageName != null) {
2102 dr = getDrawable(itemInfo.packageName, itemInfo.icon, appInfo);
2103 }
Alexandra Gherghinaa71e3902014-07-25 20:03:47 +01002104 if (dr == null) {
Alexandra Gherghinaa7093142014-07-30 13:43:39 +01002105 dr = itemInfo.loadDefaultIcon(this);
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01002106 }
Benjamin Franzec2d48b2014-10-01 15:38:43 +01002107 return dr;
Svetoslavc7d62f02014-09-04 15:39:54 -07002108 }
2109
2110 private Drawable getBadgedDrawable(Drawable drawable, Drawable badgeDrawable,
2111 Rect badgeLocation, boolean tryBadgeInPlace) {
2112 final int badgedWidth = drawable.getIntrinsicWidth();
2113 final int badgedHeight = drawable.getIntrinsicHeight();
2114 final boolean canBadgeInPlace = tryBadgeInPlace
2115 && (drawable instanceof BitmapDrawable)
2116 && ((BitmapDrawable) drawable).getBitmap().isMutable();
2117
2118 final Bitmap bitmap;
2119 if (canBadgeInPlace) {
2120 bitmap = ((BitmapDrawable) drawable).getBitmap();
2121 } else {
2122 bitmap = Bitmap.createBitmap(badgedWidth, badgedHeight, Bitmap.Config.ARGB_8888);
2123 }
2124 Canvas canvas = new Canvas(bitmap);
2125
2126 if (!canBadgeInPlace) {
2127 drawable.setBounds(0, 0, badgedWidth, badgedHeight);
2128 drawable.draw(canvas);
2129 }
2130
2131 if (badgeLocation != null) {
2132 if (badgeLocation.left < 0 || badgeLocation.top < 0
2133 || badgeLocation.width() > badgedWidth || badgeLocation.height() > badgedHeight) {
2134 throw new IllegalArgumentException("Badge location " + badgeLocation
2135 + " not in badged drawable bounds "
2136 + new Rect(0, 0, badgedWidth, badgedHeight));
2137 }
2138 badgeDrawable.setBounds(0, 0, badgeLocation.width(), badgeLocation.height());
2139
2140 canvas.save();
2141 canvas.translate(badgeLocation.left, badgeLocation.top);
2142 badgeDrawable.draw(canvas);
2143 canvas.restore();
2144 } else {
2145 badgeDrawable.setBounds(0, 0, badgedWidth, badgedHeight);
2146 badgeDrawable.draw(canvas);
2147 }
2148
2149 if (!canBadgeInPlace) {
2150 BitmapDrawable mergedDrawable = new BitmapDrawable(mContext.getResources(), bitmap);
2151
2152 if (drawable instanceof BitmapDrawable) {
2153 BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
2154 mergedDrawable.setTargetDensity(bitmapDrawable.getBitmap().getDensity());
2155 }
2156
2157 return mergedDrawable;
2158 }
2159
2160 return drawable;
2161 }
2162
2163 private int getBadgeResIdForUser(int userHandle) {
2164 // Return the framework-provided badge.
2165 UserInfo userInfo = getUserIfProfile(userHandle);
2166 if (userInfo != null && userInfo.isManagedProfile()) {
2167 return com.android.internal.R.drawable.ic_corp_icon_badge;
2168 }
2169 return 0;
2170 }
2171
2172 private UserInfo getUserIfProfile(int userHandle) {
Robin Lee0e27c872015-09-28 14:37:40 +01002173 List<UserInfo> userProfiles = getUserManager().getProfiles(mContext.getUserId());
Svetoslavc7d62f02014-09-04 15:39:54 -07002174 for (UserInfo user : userProfiles) {
2175 if (user.id == userHandle) {
2176 return user;
2177 }
2178 }
2179 return null;
Nicolas Prevot88cc3462014-05-14 14:51:48 +01002180 }
2181
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002182 /** {@hide} */
2183 private static class MoveCallbackDelegate extends IPackageMoveObserver.Stub implements
2184 Handler.Callback {
Jeff Sharkey50a05452015-04-29 11:24:52 -07002185 private static final int MSG_CREATED = 1;
2186 private static final int MSG_STATUS_CHANGED = 2;
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002187
2188 final MoveCallback mCallback;
2189 final Handler mHandler;
2190
2191 public MoveCallbackDelegate(MoveCallback callback, Looper looper) {
2192 mCallback = callback;
2193 mHandler = new Handler(looper, this);
2194 }
2195
2196 @Override
2197 public boolean handleMessage(Message msg) {
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002198 switch (msg.what) {
Jeff Sharkey50a05452015-04-29 11:24:52 -07002199 case MSG_CREATED: {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002200 final SomeArgs args = (SomeArgs) msg.obj;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002201 mCallback.onCreated(args.argi1, (Bundle) args.arg2);
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002202 args.recycle();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002203 return true;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002204 }
2205 case MSG_STATUS_CHANGED: {
2206 final SomeArgs args = (SomeArgs) msg.obj;
2207 mCallback.onStatusChanged(args.argi1, args.argi2, (long) args.arg3);
2208 args.recycle();
2209 return true;
2210 }
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002211 }
2212 return false;
2213 }
2214
2215 @Override
Jeff Sharkey50a05452015-04-29 11:24:52 -07002216 public void onCreated(int moveId, Bundle extras) {
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002217 final SomeArgs args = SomeArgs.obtain();
2218 args.argi1 = moveId;
Jeff Sharkey50a05452015-04-29 11:24:52 -07002219 args.arg2 = extras;
2220 mHandler.obtainMessage(MSG_CREATED, args).sendToTarget();
2221 }
2222
2223 @Override
2224 public void onStatusChanged(int moveId, int status, long estMillis) {
2225 final SomeArgs args = SomeArgs.obtain();
2226 args.argi1 = moveId;
2227 args.argi2 = status;
2228 args.arg3 = estMillis;
Jeff Sharkeyb36586a2015-04-27 08:42:28 -07002229 mHandler.obtainMessage(MSG_STATUS_CHANGED, args).sendToTarget();
Jeff Sharkey620b32b2015-04-23 19:36:02 -07002230 }
2231 }
2232
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002233 private final ContextImpl mContext;
2234 private final IPackageManager mPM;
2235
2236 private static final Object sSync = new Object();
Dianne Hackbornadd005c2013-07-17 18:43:12 -07002237 private static ArrayMap<ResourceName, WeakReference<Drawable.ConstantState>> sIconCache
2238 = new ArrayMap<ResourceName, WeakReference<Drawable.ConstantState>>();
2239 private static ArrayMap<ResourceName, WeakReference<CharSequence>> sStringCache
2240 = new ArrayMap<ResourceName, WeakReference<CharSequence>>();
Svetoslavf7c06eb2015-06-10 18:43:22 -07002241
2242 private final Map<OnPermissionsChangedListener, IOnPermissionsChangeListener>
2243 mPermissionListeners = new ArrayMap<>();
2244
2245 public class OnPermissionsChangeListenerDelegate extends IOnPermissionsChangeListener.Stub
2246 implements Handler.Callback{
2247 private static final int MSG_PERMISSIONS_CHANGED = 1;
2248
2249 private final OnPermissionsChangedListener mListener;
2250 private final Handler mHandler;
2251
2252
2253 public OnPermissionsChangeListenerDelegate(OnPermissionsChangedListener listener,
2254 Looper looper) {
2255 mListener = listener;
2256 mHandler = new Handler(looper, this);
2257 }
2258
2259 @Override
2260 public void onPermissionsChanged(int uid) {
2261 mHandler.obtainMessage(MSG_PERMISSIONS_CHANGED, uid, 0).sendToTarget();
2262 }
2263
2264 @Override
2265 public boolean handleMessage(Message msg) {
2266 switch (msg.what) {
2267 case MSG_PERMISSIONS_CHANGED: {
2268 final int uid = msg.arg1;
2269 mListener.onPermissionsChanged(uid);
2270 return true;
2271 }
2272 }
2273 return false;
2274 }
2275 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08002276}