blob: fe6e4f36d99d3ce7ef8a1403d50b4420bab7bba7 [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;
34import android.content.pm.IPackageDataObserver;
35import android.content.pm.IPackageDeleteObserver;
36import android.content.pm.IPackageInstallObserver;
37import android.content.pm.IPackageManager;
38import android.content.pm.IPackageMoveObserver;
39import android.content.pm.IPackageStatsObserver;
40import android.content.pm.InstrumentationInfo;
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -080041import android.content.pm.IntentFilterVerificationInfo;
dcashman9d2f4412014-06-09 09:27:54 -070042import android.content.pm.KeySet;
Jeff Sharkey513a0742014-07-08 17:10:32 -070043import android.content.pm.ManifestDigest;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080044import android.content.pm.PackageInfo;
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -070045import android.content.pm.PackageInstaller;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +010046import android.content.pm.PackageItemInfo;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080047import android.content.pm.PackageManager;
Kenny Roote6cd0c72011-05-19 12:48:14 -070048import android.content.pm.ParceledListSlice;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080049import android.content.pm.PermissionGroupInfo;
50import android.content.pm.PermissionInfo;
51import android.content.pm.ProviderInfo;
52import android.content.pm.ResolveInfo;
53import android.content.pm.ServiceInfo;
Svetoslavc7d62f02014-09-04 15:39:54 -070054import android.content.pm.UserInfo;
rich cannings706e8ba2012-08-20 13:20:14 -070055import android.content.pm.VerificationParams;
Kenny Root0aaa0d92011-09-12 16:42:55 -070056import android.content.pm.VerifierDeviceIdentity;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080057import android.content.res.Resources;
58import android.content.res.XmlResourceParser;
Svetoslavc7d62f02014-09-04 15:39:54 -070059import android.graphics.Bitmap;
60import android.graphics.Canvas;
61import android.graphics.Rect;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +010062import android.graphics.drawable.BitmapDrawable;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080063import android.graphics.drawable.Drawable;
64import android.net.Uri;
65import android.os.Process;
66import android.os.RemoteException;
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -070067import android.os.SystemProperties;
Amith Yamasani67df64b2012-12-14 12:09:36 -080068import android.os.UserHandle;
Nicolas Prevot88cc3462014-05-14 14:51:48 +010069import android.os.UserManager;
Dianne Hackbornadd005c2013-07-17 18:43:12 -070070import android.util.ArrayMap;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080071import android.util.Log;
Jeff Browna492c3a2012-08-23 19:48:44 -070072import android.view.Display;
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -070073
74import dalvik.system.VMRuntime;
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -070075
76import com.android.internal.annotations.GuardedBy;
dcashman9d2f4412014-06-09 09:27:54 -070077import com.android.internal.util.Preconditions;
Alexandra Gherghina64d4dca2014-08-28 18:26:56 +010078import com.android.internal.util.UserIcons;
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -070079
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080080import java.lang.ref.WeakReference;
81import java.util.ArrayList;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080082import java.util.List;
83
84/*package*/
85final class ApplicationPackageManager extends PackageManager {
86 private static final String TAG = "ApplicationPackageManager";
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080087 private final static boolean DEBUG_ICONS = false;
88
Adam Lesinskid00bb5e2014-10-07 12:14:45 -070089 // Default flags to use with PackageManager when no flags are given.
90 private final static int sDefaultFlags = PackageManager.GET_SHARED_LIBRARY_FILES;
91
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -070092 private final Object mLock = new Object();
93
94 @GuardedBy("mLock")
95 private UserManager mUserManager;
96 @GuardedBy("mLock")
97 private PackageInstaller mInstaller;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +010098
99 UserManager getUserManager() {
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -0700100 synchronized (mLock) {
101 if (mUserManager == null) {
102 mUserManager = UserManager.get(mContext);
103 }
104 return mUserManager;
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100105 }
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +0100106 }
107
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800108 @Override
109 public PackageInfo getPackageInfo(String packageName, int flags)
110 throws NameNotFoundException {
111 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700112 PackageInfo pi = mPM.getPackageInfo(packageName, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800113 if (pi != null) {
114 return pi;
115 }
116 } catch (RemoteException e) {
117 throw new RuntimeException("Package manager has died", e);
118 }
119
120 throw new NameNotFoundException(packageName);
121 }
122
123 @Override
124 public String[] currentToCanonicalPackageNames(String[] names) {
125 try {
126 return mPM.currentToCanonicalPackageNames(names);
127 } catch (RemoteException e) {
128 throw new RuntimeException("Package manager has died", e);
129 }
130 }
131
132 @Override
133 public String[] canonicalToCurrentPackageNames(String[] names) {
134 try {
135 return mPM.canonicalToCurrentPackageNames(names);
136 } catch (RemoteException e) {
137 throw new RuntimeException("Package manager has died", e);
138 }
139 }
140
141 @Override
142 public Intent getLaunchIntentForPackage(String packageName) {
143 // First see if the package has an INFO activity; the existence of
144 // such an activity is implied to be the desired front-door for the
145 // overall package (such as if it has multiple launcher entries).
146 Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
147 intentToResolve.addCategory(Intent.CATEGORY_INFO);
148 intentToResolve.setPackage(packageName);
Dianne Hackborn19415762010-12-15 00:20:27 -0800149 List<ResolveInfo> ris = queryIntentActivities(intentToResolve, 0);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800150
151 // Otherwise, try to find a main launcher activity.
Dianne Hackborn19415762010-12-15 00:20:27 -0800152 if (ris == null || ris.size() <= 0) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800153 // reuse the intent instance
154 intentToResolve.removeCategory(Intent.CATEGORY_INFO);
155 intentToResolve.addCategory(Intent.CATEGORY_LAUNCHER);
156 intentToResolve.setPackage(packageName);
Dianne Hackborn19415762010-12-15 00:20:27 -0800157 ris = queryIntentActivities(intentToResolve, 0);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800158 }
Dianne Hackborn19415762010-12-15 00:20:27 -0800159 if (ris == null || ris.size() <= 0) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800160 return null;
161 }
162 Intent intent = new Intent(intentToResolve);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800163 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Dianne Hackborn19415762010-12-15 00:20:27 -0800164 intent.setClassName(ris.get(0).activityInfo.packageName,
165 ris.get(0).activityInfo.name);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800166 return intent;
167 }
168
169 @Override
Jose Lima970417c2014-04-10 10:42:19 -0700170 public Intent getLeanbackLaunchIntentForPackage(String packageName) {
171 // Try to find a main leanback_launcher activity.
172 Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
173 intentToResolve.addCategory(Intent.CATEGORY_LEANBACK_LAUNCHER);
174 intentToResolve.setPackage(packageName);
175 List<ResolveInfo> ris = queryIntentActivities(intentToResolve, 0);
176
177 if (ris == null || ris.size() <= 0) {
178 return null;
179 }
180 Intent intent = new Intent(intentToResolve);
181 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
182 intent.setClassName(ris.get(0).activityInfo.packageName,
183 ris.get(0).activityInfo.name);
184 return intent;
185 }
186
187 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800188 public int[] getPackageGids(String packageName)
189 throws NameNotFoundException {
190 try {
Svetoslavc6d1c342015-02-26 14:44:43 -0800191 int[] gids = mPM.getPackageGids(packageName, mContext.getUserId());
192 if (gids != null) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800193 return gids;
194 }
195 } catch (RemoteException e) {
196 throw new RuntimeException("Package manager has died", e);
197 }
198
199 throw new NameNotFoundException(packageName);
200 }
201
202 @Override
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800203 public int getPackageUid(String packageName, int userHandle)
204 throws NameNotFoundException {
205 try {
206 int uid = mPM.getPackageUid(packageName, userHandle);
207 if (uid >= 0) {
208 return uid;
209 }
210 } catch (RemoteException e) {
211 throw new RuntimeException("Package manager has died", e);
212 }
213
214 throw new NameNotFoundException(packageName);
215 }
216
217 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800218 public PermissionInfo getPermissionInfo(String name, int flags)
219 throws NameNotFoundException {
220 try {
221 PermissionInfo pi = mPM.getPermissionInfo(name, flags);
222 if (pi != null) {
223 return pi;
224 }
225 } catch (RemoteException e) {
226 throw new RuntimeException("Package manager has died", e);
227 }
228
229 throw new NameNotFoundException(name);
230 }
231
232 @Override
233 public List<PermissionInfo> queryPermissionsByGroup(String group, int flags)
234 throws NameNotFoundException {
235 try {
236 List<PermissionInfo> pi = mPM.queryPermissionsByGroup(group, flags);
237 if (pi != null) {
238 return pi;
239 }
240 } catch (RemoteException e) {
241 throw new RuntimeException("Package manager has died", e);
242 }
243
244 throw new NameNotFoundException(group);
245 }
246
247 @Override
248 public PermissionGroupInfo getPermissionGroupInfo(String name,
249 int flags) throws NameNotFoundException {
250 try {
251 PermissionGroupInfo pgi = mPM.getPermissionGroupInfo(name, flags);
252 if (pgi != null) {
253 return pgi;
254 }
255 } catch (RemoteException e) {
256 throw new RuntimeException("Package manager has died", e);
257 }
258
259 throw new NameNotFoundException(name);
260 }
261
262 @Override
263 public List<PermissionGroupInfo> getAllPermissionGroups(int flags) {
264 try {
265 return mPM.getAllPermissionGroups(flags);
266 } catch (RemoteException e) {
267 throw new RuntimeException("Package manager has died", e);
268 }
269 }
270
271 @Override
272 public ApplicationInfo getApplicationInfo(String packageName, int flags)
273 throws NameNotFoundException {
274 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700275 ApplicationInfo ai = mPM.getApplicationInfo(packageName, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800276 if (ai != null) {
Narayan Kamathcaa71192014-07-16 11:06:43 +0100277 // This is a temporary hack. Callers must use
278 // createPackageContext(packageName).getApplicationInfo() to
279 // get the right paths.
280 maybeAdjustApplicationInfo(ai);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800281 return ai;
282 }
283 } catch (RemoteException e) {
284 throw new RuntimeException("Package manager has died", e);
285 }
286
287 throw new NameNotFoundException(packageName);
288 }
289
Narayan Kamathcaa71192014-07-16 11:06:43 +0100290 private static void maybeAdjustApplicationInfo(ApplicationInfo info) {
291 // If we're dealing with a multi-arch application that has both
292 // 32 and 64 bit shared libraries, we might need to choose the secondary
293 // depending on what the current runtime's instruction set is.
294 if (info.primaryCpuAbi != null && info.secondaryCpuAbi != null) {
295 final String runtimeIsa = VMRuntime.getRuntime().vmInstructionSet();
jgu214741cd92014-12-17 17:23:29 -0500296
297 // Get the instruction set that the libraries of secondary Abi is supported.
298 // In presence of a native bridge this might be different than the one secondary Abi used.
299 String secondaryIsa = VMRuntime.getInstructionSet(info.secondaryCpuAbi);
300 final String secondaryDexCodeIsa = SystemProperties.get("ro.dalvik.vm.isa." + secondaryIsa);
301 secondaryIsa = secondaryDexCodeIsa.isEmpty() ? secondaryIsa : secondaryDexCodeIsa;
Narayan Kamathcaa71192014-07-16 11:06:43 +0100302
303 // If the runtimeIsa is the same as the primary isa, then we do nothing.
304 // Everything will be set up correctly because info.nativeLibraryDir will
305 // correspond to the right ISA.
306 if (runtimeIsa.equals(secondaryIsa)) {
307 info.nativeLibraryDir = info.secondaryNativeLibraryDir;
308 }
309 }
310 }
311
312
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800313 @Override
314 public ActivityInfo getActivityInfo(ComponentName className, int flags)
315 throws NameNotFoundException {
316 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700317 ActivityInfo ai = mPM.getActivityInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800318 if (ai != null) {
319 return ai;
320 }
321 } catch (RemoteException e) {
322 throw new RuntimeException("Package manager has died", e);
323 }
324
325 throw new NameNotFoundException(className.toString());
326 }
327
328 @Override
329 public ActivityInfo getReceiverInfo(ComponentName className, int flags)
330 throws NameNotFoundException {
331 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700332 ActivityInfo ai = mPM.getReceiverInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800333 if (ai != null) {
334 return ai;
335 }
336 } catch (RemoteException e) {
337 throw new RuntimeException("Package manager has died", e);
338 }
339
340 throw new NameNotFoundException(className.toString());
341 }
342
343 @Override
344 public ServiceInfo getServiceInfo(ComponentName className, int flags)
345 throws NameNotFoundException {
346 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700347 ServiceInfo si = mPM.getServiceInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800348 if (si != null) {
349 return si;
350 }
351 } catch (RemoteException e) {
352 throw new RuntimeException("Package manager has died", e);
353 }
354
355 throw new NameNotFoundException(className.toString());
356 }
357
358 @Override
359 public ProviderInfo getProviderInfo(ComponentName className, int flags)
360 throws NameNotFoundException {
361 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700362 ProviderInfo pi = mPM.getProviderInfo(className, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800363 if (pi != null) {
364 return pi;
365 }
366 } catch (RemoteException e) {
367 throw new RuntimeException("Package manager has died", e);
368 }
369
370 throw new NameNotFoundException(className.toString());
371 }
372
373 @Override
374 public String[] getSystemSharedLibraryNames() {
375 try {
376 return mPM.getSystemSharedLibraryNames();
377 } catch (RemoteException e) {
378 throw new RuntimeException("Package manager has died", e);
379 }
380 }
381
382 @Override
383 public FeatureInfo[] getSystemAvailableFeatures() {
384 try {
385 return mPM.getSystemAvailableFeatures();
386 } catch (RemoteException e) {
387 throw new RuntimeException("Package manager has died", e);
388 }
389 }
390
391 @Override
392 public boolean hasSystemFeature(String name) {
393 try {
394 return mPM.hasSystemFeature(name);
395 } catch (RemoteException e) {
396 throw new RuntimeException("Package manager has died", e);
397 }
398 }
399
400 @Override
401 public int checkPermission(String permName, String pkgName) {
402 try {
Svetoslavc6d1c342015-02-26 14:44:43 -0800403 return mPM.checkPermission(permName, pkgName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800404 } catch (RemoteException e) {
405 throw new RuntimeException("Package manager has died", e);
406 }
407 }
408
409 @Override
410 public boolean addPermission(PermissionInfo info) {
411 try {
412 return mPM.addPermission(info);
413 } catch (RemoteException e) {
414 throw new RuntimeException("Package manager has died", e);
415 }
416 }
417
418 @Override
419 public boolean addPermissionAsync(PermissionInfo info) {
420 try {
421 return mPM.addPermissionAsync(info);
422 } catch (RemoteException e) {
423 throw new RuntimeException("Package manager has died", e);
424 }
425 }
426
427 @Override
428 public void removePermission(String name) {
429 try {
430 mPM.removePermission(name);
431 } catch (RemoteException e) {
432 throw new RuntimeException("Package manager has died", e);
433 }
434 }
435
436 @Override
Svetoslavc6d1c342015-02-26 14:44:43 -0800437 public void grantPermission(String packageName, String permissionName, UserHandle user) {
Dianne Hackborne639da72012-02-21 15:11:13 -0800438 try {
Svetoslavc6d1c342015-02-26 14:44:43 -0800439 mPM.grantPermission(packageName, permissionName, user.getIdentifier());
Dianne Hackborne639da72012-02-21 15:11:13 -0800440 } catch (RemoteException e) {
441 throw new RuntimeException("Package manager has died", e);
442 }
443 }
444
445 @Override
Svetoslavc6d1c342015-02-26 14:44:43 -0800446 public void revokePermission(String packageName, String permissionName, UserHandle user) {
Dianne Hackborne639da72012-02-21 15:11:13 -0800447 try {
Svetoslavc6d1c342015-02-26 14:44:43 -0800448 mPM.revokePermission(packageName, permissionName, user.getIdentifier());
Dianne Hackborne639da72012-02-21 15:11:13 -0800449 } catch (RemoteException e) {
450 throw new RuntimeException("Package manager has died", e);
451 }
452 }
453
454 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800455 public int checkSignatures(String pkg1, String pkg2) {
456 try {
457 return mPM.checkSignatures(pkg1, pkg2);
458 } catch (RemoteException e) {
459 throw new RuntimeException("Package manager has died", e);
460 }
461 }
462
463 @Override
464 public int checkSignatures(int uid1, int uid2) {
465 try {
466 return mPM.checkUidSignatures(uid1, uid2);
467 } catch (RemoteException e) {
468 throw new RuntimeException("Package manager has died", e);
469 }
470 }
471
472 @Override
473 public String[] getPackagesForUid(int uid) {
474 try {
475 return mPM.getPackagesForUid(uid);
476 } catch (RemoteException e) {
477 throw new RuntimeException("Package manager has died", e);
478 }
479 }
480
481 @Override
482 public String getNameForUid(int uid) {
483 try {
484 return mPM.getNameForUid(uid);
485 } catch (RemoteException e) {
486 throw new RuntimeException("Package manager has died", e);
487 }
488 }
489
490 @Override
491 public int getUidForSharedUser(String sharedUserName)
492 throws NameNotFoundException {
493 try {
494 int uid = mPM.getUidForSharedUser(sharedUserName);
495 if(uid != -1) {
496 return uid;
497 }
498 } catch (RemoteException e) {
499 throw new RuntimeException("Package manager has died", e);
500 }
501 throw new NameNotFoundException("No shared userid for user:"+sharedUserName);
502 }
503
Kenny Roote6cd0c72011-05-19 12:48:14 -0700504 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800505 @Override
506 public List<PackageInfo> getInstalledPackages(int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700507 return getInstalledPackages(flags, mContext.getUserId());
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700508 }
509
510 /** @hide */
511 @Override
512 public List<PackageInfo> getInstalledPackages(int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800513 try {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800514 ParceledListSlice<PackageInfo> slice = mPM.getInstalledPackages(flags, userId);
515 return slice.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800516 } catch (RemoteException e) {
517 throw new RuntimeException("Package manager has died", e);
518 }
519 }
520
Kenny Roote6cd0c72011-05-19 12:48:14 -0700521 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800522 @Override
Dianne Hackborne7991752013-01-16 17:56:46 -0800523 public List<PackageInfo> getPackagesHoldingPermissions(
524 String[] permissions, int flags) {
525 final int userId = mContext.getUserId();
526 try {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800527 ParceledListSlice<PackageInfo> slice = mPM.getPackagesHoldingPermissions(
528 permissions, flags, userId);
529 return slice.getList();
Dianne Hackborne7991752013-01-16 17:56:46 -0800530 } catch (RemoteException e) {
531 throw new RuntimeException("Package manager has died", e);
532 }
533 }
534
535 @SuppressWarnings("unchecked")
536 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800537 public List<ApplicationInfo> getInstalledApplications(int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700538 final int userId = mContext.getUserId();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800539 try {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800540 ParceledListSlice<ApplicationInfo> slice = mPM.getInstalledApplications(flags, userId);
541 return slice.getList();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800542 } catch (RemoteException e) {
543 throw new RuntimeException("Package manager has died", e);
544 }
545 }
546
547 @Override
548 public ResolveInfo resolveActivity(Intent intent, int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700549 return resolveActivityAsUser(intent, flags, mContext.getUserId());
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700550 }
551
552 @Override
553 public ResolveInfo resolveActivityAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800554 try {
555 return mPM.resolveIntent(
556 intent,
557 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700558 flags,
559 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800560 } catch (RemoteException e) {
561 throw new RuntimeException("Package manager has died", e);
562 }
563 }
564
565 @Override
566 public List<ResolveInfo> queryIntentActivities(Intent intent,
567 int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700568 return queryIntentActivitiesAsUser(intent, flags, mContext.getUserId());
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700569 }
570
571 /** @hide Same as above but for a specific user */
572 @Override
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700573 public List<ResolveInfo> queryIntentActivitiesAsUser(Intent intent,
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700574 int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800575 try {
576 return mPM.queryIntentActivities(
577 intent,
578 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700579 flags,
Amith Yamasani151ec4c2012-09-07 19:25:16 -0700580 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800581 } catch (RemoteException e) {
582 throw new RuntimeException("Package manager has died", e);
583 }
584 }
585
586 @Override
587 public List<ResolveInfo> queryIntentActivityOptions(
588 ComponentName caller, Intent[] specifics, Intent intent,
589 int flags) {
590 final ContentResolver resolver = mContext.getContentResolver();
591
592 String[] specificTypes = null;
593 if (specifics != null) {
594 final int N = specifics.length;
595 for (int i=0; i<N; i++) {
596 Intent sp = specifics[i];
597 if (sp != null) {
598 String t = sp.resolveTypeIfNeeded(resolver);
599 if (t != null) {
600 if (specificTypes == null) {
601 specificTypes = new String[N];
602 }
603 specificTypes[i] = t;
604 }
605 }
606 }
607 }
608
609 try {
610 return mPM.queryIntentActivityOptions(caller, specifics,
611 specificTypes, intent, intent.resolveTypeIfNeeded(resolver),
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700612 flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800613 } catch (RemoteException e) {
614 throw new RuntimeException("Package manager has died", e);
615 }
616 }
617
Amith Yamasanif203aee2012-08-29 18:41:53 -0700618 /**
619 * @hide
620 */
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800621 @Override
Amith Yamasanif203aee2012-08-29 18:41:53 -0700622 public List<ResolveInfo> queryBroadcastReceivers(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800623 try {
624 return mPM.queryIntentReceivers(
625 intent,
626 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700627 flags,
Amith Yamasanif203aee2012-08-29 18:41:53 -0700628 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800629 } catch (RemoteException e) {
630 throw new RuntimeException("Package manager has died", e);
631 }
632 }
633
634 @Override
Amith Yamasanif203aee2012-08-29 18:41:53 -0700635 public List<ResolveInfo> queryBroadcastReceivers(Intent intent, int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700636 return queryBroadcastReceivers(intent, flags, mContext.getUserId());
Amith Yamasanif203aee2012-08-29 18:41:53 -0700637 }
638
639 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800640 public ResolveInfo resolveService(Intent intent, int flags) {
641 try {
642 return mPM.resolveService(
643 intent,
644 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700645 flags,
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700646 mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800647 } catch (RemoteException e) {
648 throw new RuntimeException("Package manager has died", e);
649 }
650 }
651
652 @Override
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700653 public List<ResolveInfo> queryIntentServicesAsUser(Intent intent, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800654 try {
655 return mPM.queryIntentServices(
656 intent,
657 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700658 flags,
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700659 userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800660 } catch (RemoteException e) {
661 throw new RuntimeException("Package manager has died", e);
662 }
663 }
664
665 @Override
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700666 public List<ResolveInfo> queryIntentServices(Intent intent, int flags) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700667 return queryIntentServicesAsUser(intent, flags, mContext.getUserId());
Svetoslav Ganov58d37b52012-09-18 12:04:19 -0700668 }
669
670 @Override
Jeff Sharkey85f5f812013-10-07 10:16:12 -0700671 public List<ResolveInfo> queryIntentContentProvidersAsUser(
672 Intent intent, int flags, int userId) {
673 try {
674 return mPM.queryIntentContentProviders(intent,
675 intent.resolveTypeIfNeeded(mContext.getContentResolver()), flags, userId);
676 } catch (RemoteException e) {
677 throw new RuntimeException("Package manager has died", e);
678 }
679 }
680
681 @Override
682 public List<ResolveInfo> queryIntentContentProviders(Intent intent, int flags) {
683 return queryIntentContentProvidersAsUser(intent, flags, mContext.getUserId());
684 }
685
686 @Override
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +0100687 public ProviderInfo resolveContentProvider(String name, int flags) {
688 return resolveContentProviderAsUser(name, flags, mContext.getUserId());
689 }
690
691 /** @hide **/
692 @Override
693 public ProviderInfo resolveContentProviderAsUser(String name, int flags, int userId) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800694 try {
Alexandra Gherghina0363c3e2014-06-23 13:34:59 +0100695 return mPM.resolveContentProvider(name, flags, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800696 } catch (RemoteException e) {
697 throw new RuntimeException("Package manager has died", e);
698 }
699 }
700
701 @Override
702 public List<ProviderInfo> queryContentProviders(String processName,
703 int uid, int flags) {
704 try {
705 return mPM.queryContentProviders(processName, uid, flags);
706 } catch (RemoteException e) {
707 throw new RuntimeException("Package manager has died", e);
708 }
709 }
710
711 @Override
712 public InstrumentationInfo getInstrumentationInfo(
713 ComponentName className, int flags)
714 throws NameNotFoundException {
715 try {
716 InstrumentationInfo ii = mPM.getInstrumentationInfo(
717 className, flags);
718 if (ii != null) {
719 return ii;
720 }
721 } catch (RemoteException e) {
722 throw new RuntimeException("Package manager has died", e);
723 }
724
725 throw new NameNotFoundException(className.toString());
726 }
727
728 @Override
729 public List<InstrumentationInfo> queryInstrumentation(
730 String targetPackage, int flags) {
731 try {
732 return mPM.queryInstrumentation(targetPackage, flags);
733 } catch (RemoteException e) {
734 throw new RuntimeException("Package manager has died", e);
735 }
736 }
737
Alan Viveretteecd585a2015-04-13 10:32:51 -0700738 @Nullable
739 @Override
740 public Drawable getDrawable(String packageName, @DrawableRes int resId,
741 @Nullable ApplicationInfo appInfo) {
742 final ResourceName name = new ResourceName(packageName, resId);
743 final Drawable cachedIcon = getCachedIcon(name);
744 if (cachedIcon != null) {
745 return cachedIcon;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800746 }
Alan Viveretteecd585a2015-04-13 10:32:51 -0700747
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800748 if (appInfo == null) {
749 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700750 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800751 } catch (NameNotFoundException e) {
752 return null;
753 }
754 }
Alan Viveretteecd585a2015-04-13 10:32:51 -0700755
756 if (resId != 0) {
757 try {
758 final Resources r = getResourcesForApplication(appInfo);
759 final Drawable dr = r.getDrawable(resId, null);
760 if (dr != null) {
761 putCachedIcon(name, dr);
762 }
763
764 if (false) {
765 RuntimeException e = new RuntimeException("here");
766 e.fillInStackTrace();
767 Log.w(TAG, "Getting drawable 0x" + Integer.toHexString(resId)
768 + " from package " + packageName
769 + ": app scale=" + r.getCompatibilityInfo().applicationScale
770 + ", caller scale=" + mContext.getResources()
771 .getCompatibilityInfo().applicationScale,
772 e);
773 }
Ricky Wai3ce46252015-04-15 16:12:22 +0100774 if (DEBUG_ICONS) {
Alan Viveretteecd585a2015-04-13 10:32:51 -0700775 Log.v(TAG, "Getting drawable 0x"
776 + Integer.toHexString(resId) + " from " + r
777 + ": " + dr);
Ricky Wai3ce46252015-04-15 16:12:22 +0100778 }
779 return dr;
Alan Viveretteecd585a2015-04-13 10:32:51 -0700780 } catch (NameNotFoundException e) {
781 Log.w("PackageManager", "Failure retrieving resources for "
782 + appInfo.packageName);
783 } catch (Resources.NotFoundException e) {
784 Log.w("PackageManager", "Failure retrieving resources for "
785 + appInfo.packageName + ": " + e.getMessage());
786 } catch (Exception e) {
787 // If an exception was thrown, fall through to return
788 // default icon.
789 Log.w("PackageManager", "Failure retrieving icon 0x"
790 + Integer.toHexString(resId) + " in package "
791 + packageName, e);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800792 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800793 }
Alan Viveretteecd585a2015-04-13 10:32:51 -0700794
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800795 return null;
796 }
797
798 @Override public Drawable getActivityIcon(ComponentName activityName)
799 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700800 return getActivityInfo(activityName, sDefaultFlags).loadIcon(this);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800801 }
802
803 @Override public Drawable getActivityIcon(Intent intent)
804 throws NameNotFoundException {
805 if (intent.getComponent() != null) {
806 return getActivityIcon(intent.getComponent());
807 }
808
809 ResolveInfo info = resolveActivity(
810 intent, PackageManager.MATCH_DEFAULT_ONLY);
811 if (info != null) {
812 return info.activityInfo.loadIcon(this);
813 }
814
Romain Guy39fe17c2011-11-30 10:34:07 -0800815 throw new NameNotFoundException(intent.toUri(0));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800816 }
817
818 @Override public Drawable getDefaultActivityIcon() {
819 return Resources.getSystem().getDrawable(
820 com.android.internal.R.drawable.sym_def_app_icon);
821 }
822
823 @Override public Drawable getApplicationIcon(ApplicationInfo info) {
824 return info.loadIcon(this);
825 }
826
827 @Override public Drawable getApplicationIcon(String packageName)
828 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700829 return getApplicationIcon(getApplicationInfo(packageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800830 }
831
832 @Override
Jose Limaf78e3122014-03-06 12:13:15 -0800833 public Drawable getActivityBanner(ComponentName activityName)
834 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700835 return getActivityInfo(activityName, sDefaultFlags).loadBanner(this);
Jose Limaf78e3122014-03-06 12:13:15 -0800836 }
837
838 @Override
839 public Drawable getActivityBanner(Intent intent)
840 throws NameNotFoundException {
841 if (intent.getComponent() != null) {
842 return getActivityBanner(intent.getComponent());
843 }
844
845 ResolveInfo info = resolveActivity(
846 intent, PackageManager.MATCH_DEFAULT_ONLY);
847 if (info != null) {
848 return info.activityInfo.loadBanner(this);
849 }
850
851 throw new NameNotFoundException(intent.toUri(0));
852 }
853
854 @Override
855 public Drawable getApplicationBanner(ApplicationInfo info) {
856 return info.loadBanner(this);
857 }
858
859 @Override
860 public Drawable getApplicationBanner(String packageName)
861 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700862 return getApplicationBanner(getApplicationInfo(packageName, sDefaultFlags));
Jose Limaf78e3122014-03-06 12:13:15 -0800863 }
864
865 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800866 public Drawable getActivityLogo(ComponentName activityName)
867 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700868 return getActivityInfo(activityName, sDefaultFlags).loadLogo(this);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800869 }
870
871 @Override
872 public Drawable getActivityLogo(Intent intent)
873 throws NameNotFoundException {
874 if (intent.getComponent() != null) {
875 return getActivityLogo(intent.getComponent());
876 }
877
878 ResolveInfo info = resolveActivity(
879 intent, PackageManager.MATCH_DEFAULT_ONLY);
880 if (info != null) {
881 return info.activityInfo.loadLogo(this);
882 }
883
884 throw new NameNotFoundException(intent.toUri(0));
885 }
886
887 @Override
888 public Drawable getApplicationLogo(ApplicationInfo info) {
889 return info.loadLogo(this);
890 }
891
892 @Override
893 public Drawable getApplicationLogo(String packageName)
894 throws NameNotFoundException {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700895 return getApplicationLogo(getApplicationInfo(packageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800896 }
897
Svetoslavc7d62f02014-09-04 15:39:54 -0700898 @Override
899 public Drawable getUserBadgedIcon(Drawable icon, UserHandle user) {
900 final int badgeResId = getBadgeResIdForUser(user.getIdentifier());
901 if (badgeResId == 0) {
902 return icon;
903 }
904 Drawable badgeIcon = getDrawable("system", badgeResId, null);
905 return getBadgedDrawable(icon, badgeIcon, null, true);
906 }
907
908 @Override
909 public Drawable getUserBadgedDrawableForDensity(Drawable drawable, UserHandle user,
910 Rect badgeLocation, int badgeDensity) {
911 Drawable badgeDrawable = getUserBadgeForDensity(user, badgeDensity);
912 if (badgeDrawable == null) {
913 return drawable;
914 }
915 return getBadgedDrawable(drawable, badgeDrawable, badgeLocation, true);
916 }
917
918 @Override
919 public Drawable getUserBadgeForDensity(UserHandle user, int density) {
920 UserInfo userInfo = getUserIfProfile(user.getIdentifier());
921 if (userInfo != null && userInfo.isManagedProfile()) {
922 if (density <= 0) {
923 density = mContext.getResources().getDisplayMetrics().densityDpi;
924 }
925 return Resources.getSystem().getDrawableForDensity(
926 com.android.internal.R.drawable.ic_corp_badge, density);
927 }
928 return null;
929 }
930
931 @Override
932 public CharSequence getUserBadgedLabel(CharSequence label, UserHandle user) {
933 UserInfo userInfo = getUserIfProfile(user.getIdentifier());
934 if (userInfo != null && userInfo.isManagedProfile()) {
935 return Resources.getSystem().getString(
936 com.android.internal.R.string.managed_profile_label_badge, label);
937 }
938 return label;
939 }
940
Alan Viveretteecd585a2015-04-13 10:32:51 -0700941 @Override
942 public Resources getResourcesForActivity(ComponentName activityName)
943 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800944 return getResourcesForApplication(
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700945 getActivityInfo(activityName, sDefaultFlags).applicationInfo);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800946 }
947
Alan Viveretteecd585a2015-04-13 10:32:51 -0700948 @Override
949 public Resources getResourcesForApplication(@NonNull ApplicationInfo app)
950 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800951 if (app.packageName.equals("system")) {
952 return mContext.mMainThread.getSystemContext().getResources();
953 }
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700954 final boolean sameUid = (app.uid == Process.myUid());
Alan Viveretteecd585a2015-04-13 10:32:51 -0700955 final Resources r = mContext.mMainThread.getTopLevelResources(
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700956 sameUid ? app.sourceDir : app.publicSourceDir,
957 sameUid ? app.splitSourceDirs : app.splitPublicSourceDirs,
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700958 app.resourceDirs, app.sharedLibraryFiles, Display.DEFAULT_DISPLAY,
959 null, mContext.mPackageInfo);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800960 if (r != null) {
961 return r;
962 }
963 throw new NameNotFoundException("Unable to open " + app.publicSourceDir);
964 }
965
Alan Viveretteecd585a2015-04-13 10:32:51 -0700966 @Override
967 public Resources getResourcesForApplication(String appPackageName)
968 throws NameNotFoundException {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800969 return getResourcesForApplication(
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700970 getApplicationInfo(appPackageName, sDefaultFlags));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800971 }
972
Amith Yamasani98edc952012-09-25 14:09:27 -0700973 /** @hide */
974 @Override
975 public Resources getResourcesForApplicationAsUser(String appPackageName, int userId)
976 throws NameNotFoundException {
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700977 if (userId < 0) {
978 throw new IllegalArgumentException(
979 "Call does not support special user #" + userId);
980 }
981 if ("system".equals(appPackageName)) {
982 return mContext.mMainThread.getSystemContext().getResources();
983 }
Amith Yamasani98edc952012-09-25 14:09:27 -0700984 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -0700985 ApplicationInfo ai = mPM.getApplicationInfo(appPackageName, sDefaultFlags, userId);
Amith Yamasani98edc952012-09-25 14:09:27 -0700986 if (ai != null) {
987 return getResourcesForApplication(ai);
988 }
989 } catch (RemoteException e) {
990 throw new RuntimeException("Package manager has died", e);
991 }
992 throw new NameNotFoundException("Package " + appPackageName + " doesn't exist");
993 }
994
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800995 int mCachedSafeMode = -1;
996 @Override public boolean isSafeMode() {
997 try {
998 if (mCachedSafeMode < 0) {
999 mCachedSafeMode = mPM.isSafeMode() ? 1 : 0;
1000 }
1001 return mCachedSafeMode != 0;
1002 } catch (RemoteException e) {
1003 throw new RuntimeException("Package manager has died", e);
1004 }
1005 }
1006
1007 static void configurationChanged() {
1008 synchronized (sSync) {
1009 sIconCache.clear();
1010 sStringCache.clear();
1011 }
1012 }
1013
1014 ApplicationPackageManager(ContextImpl context,
1015 IPackageManager pm) {
1016 mContext = context;
1017 mPM = pm;
1018 }
1019
Alan Viveretteecd585a2015-04-13 10:32:51 -07001020 @Nullable
1021 private Drawable getCachedIcon(@NonNull ResourceName name) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001022 synchronized (sSync) {
Alan Viveretteecd585a2015-04-13 10:32:51 -07001023 final WeakReference<Drawable.ConstantState> wr = sIconCache.get(name);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001024 if (DEBUG_ICONS) Log.v(TAG, "Get cached weak drawable ref for "
1025 + name + ": " + wr);
1026 if (wr != null) { // we have the activity
Alan Viveretteecd585a2015-04-13 10:32:51 -07001027 final Drawable.ConstantState state = wr.get();
Romain Guy39fe17c2011-11-30 10:34:07 -08001028 if (state != null) {
1029 if (DEBUG_ICONS) {
1030 Log.v(TAG, "Get cached drawable state for " + name + ": " + state);
1031 }
1032 // Note: It's okay here to not use the newDrawable(Resources) variant
1033 // of the API. The ConstantState comes from a drawable that was
1034 // originally created by passing the proper app Resources instance
1035 // which means the state should already contain the proper
1036 // resources specific information (like density.) See
1037 // BitmapDrawable.BitmapState for instance.
1038 return state.newDrawable();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001039 }
1040 // our entry has been purged
1041 sIconCache.remove(name);
1042 }
1043 }
1044 return null;
1045 }
1046
Alan Viveretteecd585a2015-04-13 10:32:51 -07001047 private void putCachedIcon(@NonNull ResourceName name, @NonNull Drawable dr) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001048 synchronized (sSync) {
Alan Viveretteecd585a2015-04-13 10:32:51 -07001049 sIconCache.put(name, new WeakReference<>(dr.getConstantState()));
Romain Guy39fe17c2011-11-30 10:34:07 -08001050 if (DEBUG_ICONS) Log.v(TAG, "Added cached drawable state for " + name + ": " + dr);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001051 }
1052 }
1053
Romain Guy39fe17c2011-11-30 10:34:07 -08001054 static void handlePackageBroadcast(int cmd, String[] pkgList, boolean hasPkgInfo) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001055 boolean immediateGc = false;
1056 if (cmd == IApplicationThread.EXTERNAL_STORAGE_UNAVAILABLE) {
1057 immediateGc = true;
1058 }
1059 if (pkgList != null && (pkgList.length > 0)) {
1060 boolean needCleanup = false;
1061 for (String ssp : pkgList) {
1062 synchronized (sSync) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07001063 for (int i=sIconCache.size()-1; i>=0; i--) {
1064 ResourceName nm = sIconCache.keyAt(i);
1065 if (nm.packageName.equals(ssp)) {
1066 //Log.i(TAG, "Removing cached drawable for " + nm);
1067 sIconCache.removeAt(i);
1068 needCleanup = true;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001069 }
1070 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07001071 for (int i=sStringCache.size()-1; i>=0; i--) {
1072 ResourceName nm = sStringCache.keyAt(i);
1073 if (nm.packageName.equals(ssp)) {
1074 //Log.i(TAG, "Removing cached string for " + nm);
1075 sStringCache.removeAt(i);
1076 needCleanup = true;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001077 }
1078 }
1079 }
1080 }
1081 if (needCleanup || hasPkgInfo) {
1082 if (immediateGc) {
1083 // Schedule an immediate gc.
1084 Runtime.getRuntime().gc();
1085 } else {
1086 ActivityThread.currentActivityThread().scheduleGcIdler();
1087 }
1088 }
1089 }
1090 }
1091
1092 private static final class ResourceName {
1093 final String packageName;
1094 final int iconId;
1095
1096 ResourceName(String _packageName, int _iconId) {
1097 packageName = _packageName;
1098 iconId = _iconId;
1099 }
1100
1101 ResourceName(ApplicationInfo aInfo, int _iconId) {
1102 this(aInfo.packageName, _iconId);
1103 }
1104
1105 ResourceName(ComponentInfo cInfo, int _iconId) {
1106 this(cInfo.applicationInfo.packageName, _iconId);
1107 }
1108
1109 ResourceName(ResolveInfo rInfo, int _iconId) {
1110 this(rInfo.activityInfo.applicationInfo.packageName, _iconId);
1111 }
1112
1113 @Override
1114 public boolean equals(Object o) {
1115 if (this == o) return true;
1116 if (o == null || getClass() != o.getClass()) return false;
1117
1118 ResourceName that = (ResourceName) o;
1119
1120 if (iconId != that.iconId) return false;
1121 return !(packageName != null ?
1122 !packageName.equals(that.packageName) : that.packageName != null);
1123
1124 }
1125
1126 @Override
1127 public int hashCode() {
1128 int result;
1129 result = packageName.hashCode();
1130 result = 31 * result + iconId;
1131 return result;
1132 }
1133
1134 @Override
1135 public String toString() {
1136 return "{ResourceName " + packageName + " / " + iconId + "}";
1137 }
1138 }
1139
1140 private CharSequence getCachedString(ResourceName name) {
1141 synchronized (sSync) {
1142 WeakReference<CharSequence> wr = sStringCache.get(name);
1143 if (wr != null) { // we have the activity
1144 CharSequence cs = wr.get();
1145 if (cs != null) {
1146 return cs;
1147 }
1148 // our entry has been purged
1149 sStringCache.remove(name);
1150 }
1151 }
1152 return null;
1153 }
1154
1155 private void putCachedString(ResourceName name, CharSequence cs) {
1156 synchronized (sSync) {
1157 sStringCache.put(name, new WeakReference<CharSequence>(cs));
1158 }
1159 }
1160
1161 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07001162 public CharSequence getText(String packageName, @StringRes int resid,
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001163 ApplicationInfo appInfo) {
1164 ResourceName name = new ResourceName(packageName, resid);
1165 CharSequence text = getCachedString(name);
1166 if (text != null) {
1167 return text;
1168 }
1169 if (appInfo == null) {
1170 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001171 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001172 } catch (NameNotFoundException e) {
1173 return null;
1174 }
1175 }
1176 try {
1177 Resources r = getResourcesForApplication(appInfo);
1178 text = r.getText(resid);
1179 putCachedString(name, text);
1180 return text;
1181 } catch (NameNotFoundException e) {
Dianne Hackbornaec68bb2014-08-20 15:25:13 -07001182 Log.w("PackageManager", "Failure retrieving resources for "
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001183 + appInfo.packageName);
1184 } catch (RuntimeException e) {
1185 // If an exception was thrown, fall through to return
1186 // default icon.
1187 Log.w("PackageManager", "Failure retrieving text 0x"
1188 + Integer.toHexString(resid) + " in package "
1189 + packageName, e);
1190 }
1191 return null;
1192 }
1193
1194 @Override
Tor Norbye7b9c9122013-05-30 16:48:33 -07001195 public XmlResourceParser getXml(String packageName, @XmlRes int resid,
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001196 ApplicationInfo appInfo) {
1197 if (appInfo == null) {
1198 try {
Adam Lesinskid00bb5e2014-10-07 12:14:45 -07001199 appInfo = getApplicationInfo(packageName, sDefaultFlags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001200 } catch (NameNotFoundException e) {
1201 return null;
1202 }
1203 }
1204 try {
1205 Resources r = getResourcesForApplication(appInfo);
1206 return r.getXml(resid);
1207 } catch (RuntimeException e) {
1208 // If an exception was thrown, fall through to return
1209 // default icon.
1210 Log.w("PackageManager", "Failure retrieving xml 0x"
1211 + Integer.toHexString(resid) + " in package "
1212 + packageName, e);
1213 } catch (NameNotFoundException e) {
Alon Albert3fa51e32010-11-11 09:24:04 -08001214 Log.w("PackageManager", "Failure retrieving resources for "
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001215 + appInfo.packageName);
1216 }
1217 return null;
1218 }
1219
1220 @Override
1221 public CharSequence getApplicationLabel(ApplicationInfo info) {
1222 return info.loadLabel(this);
1223 }
1224
1225 @Override
1226 public void installPackage(Uri packageURI, IPackageInstallObserver observer, int flags,
1227 String installerPackageName) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001228 final VerificationParams verificationParams = new VerificationParams(null, null,
1229 null, VerificationParams.NO_UID, null);
1230 installCommon(packageURI, new LegacyPackageInstallObserver(observer), flags,
1231 installerPackageName, verificationParams, null);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001232 }
1233
1234 @Override
Kenny Root5ab21572011-07-27 11:11:19 -07001235 public void installPackageWithVerification(Uri packageURI, IPackageInstallObserver observer,
1236 int flags, String installerPackageName, Uri verificationURI,
Rich Canningse1d7c712012-08-08 12:46:06 -07001237 ManifestDigest manifestDigest, ContainerEncryptionParams encryptionParams) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001238 final VerificationParams verificationParams = new VerificationParams(verificationURI, null,
1239 null, VerificationParams.NO_UID, manifestDigest);
1240 installCommon(packageURI, new LegacyPackageInstallObserver(observer), flags,
1241 installerPackageName, verificationParams, encryptionParams);
Kenny Root5ab21572011-07-27 11:11:19 -07001242 }
1243
1244 @Override
John Spurlock8a985d22014-02-25 09:40:05 -05001245 public void installPackageWithVerificationAndEncryption(Uri packageURI,
rich cannings706e8ba2012-08-20 13:20:14 -07001246 IPackageInstallObserver observer, int flags, String installerPackageName,
1247 VerificationParams verificationParams, ContainerEncryptionParams encryptionParams) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001248 installCommon(packageURI, new LegacyPackageInstallObserver(observer), flags,
1249 installerPackageName, verificationParams, encryptionParams);
Christopher Tatef1977b42014-03-24 16:25:51 -07001250 }
1251
Christopher Tatef1977b42014-03-24 16:25:51 -07001252 @Override
1253 public void installPackage(Uri packageURI, PackageInstallObserver observer,
1254 int flags, String installerPackageName) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001255 final VerificationParams verificationParams = new VerificationParams(null, null,
1256 null, VerificationParams.NO_UID, null);
1257 installCommon(packageURI, observer, flags, installerPackageName, verificationParams, null);
Christopher Tatef1977b42014-03-24 16:25:51 -07001258 }
1259
1260 @Override
1261 public void installPackageWithVerification(Uri packageURI,
1262 PackageInstallObserver observer, int flags, String installerPackageName,
1263 Uri verificationURI, ManifestDigest manifestDigest,
1264 ContainerEncryptionParams encryptionParams) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001265 final VerificationParams verificationParams = new VerificationParams(verificationURI, null,
1266 null, VerificationParams.NO_UID, manifestDigest);
1267 installCommon(packageURI, observer, flags, installerPackageName, verificationParams,
1268 encryptionParams);
Christopher Tatef1977b42014-03-24 16:25:51 -07001269 }
1270
1271 @Override
1272 public void installPackageWithVerificationAndEncryption(Uri packageURI,
1273 PackageInstallObserver observer, int flags, String installerPackageName,
1274 VerificationParams verificationParams, ContainerEncryptionParams encryptionParams) {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001275 installCommon(packageURI, observer, flags, installerPackageName, verificationParams,
1276 encryptionParams);
1277 }
1278
1279 private void installCommon(Uri packageURI,
1280 PackageInstallObserver observer, int flags, String installerPackageName,
1281 VerificationParams verificationParams, ContainerEncryptionParams encryptionParams) {
1282 if (!"file".equals(packageURI.getScheme())) {
1283 throw new UnsupportedOperationException("Only file:// URIs are supported");
1284 }
1285 if (encryptionParams != null) {
1286 throw new UnsupportedOperationException("ContainerEncryptionParams not supported");
1287 }
1288
1289 final String originPath = packageURI.getPath();
Christopher Tatef1977b42014-03-24 16:25:51 -07001290 try {
Jeff Sharkey513a0742014-07-08 17:10:32 -07001291 mPM.installPackage(originPath, observer.getBinder(), flags, installerPackageName,
1292 verificationParams, null);
1293 } catch (RemoteException ignored) {
rich cannings706e8ba2012-08-20 13:20:14 -07001294 }
1295 }
1296
1297 @Override
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001298 public int installExistingPackage(String packageName)
1299 throws NameNotFoundException {
1300 try {
Amith Yamasani67df64b2012-12-14 12:09:36 -08001301 int res = mPM.installExistingPackageAsUser(packageName, UserHandle.myUserId());
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001302 if (res == INSTALL_FAILED_INVALID_URI) {
1303 throw new NameNotFoundException("Package " + packageName + " doesn't exist");
1304 }
1305 return res;
1306 } catch (RemoteException e) {
1307 // Should never happen!
1308 throw new NameNotFoundException("Package " + packageName + " doesn't exist");
1309 }
1310 }
1311
1312 @Override
Kenny Root3a9b5fb2011-09-20 14:15:38 -07001313 public void verifyPendingInstall(int id, int response) {
Kenny Root5ab21572011-07-27 11:11:19 -07001314 try {
Kenny Root3a9b5fb2011-09-20 14:15:38 -07001315 mPM.verifyPendingInstall(id, response);
Kenny Root5ab21572011-07-27 11:11:19 -07001316 } catch (RemoteException e) {
1317 // Should never happen!
1318 }
1319 }
1320
1321 @Override
rich canningsd9ef3e52012-08-22 14:28:05 -07001322 public void extendVerificationTimeout(int id, int verificationCodeAtTimeout,
1323 long millisecondsToDelay) {
1324 try {
1325 mPM.extendVerificationTimeout(id, verificationCodeAtTimeout, millisecondsToDelay);
1326 } catch (RemoteException e) {
1327 // Should never happen!
1328 }
1329 }
1330
1331 @Override
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08001332 public void verifyIntentFilter(int id, int verificationCode, List<String> outFailedDomains) {
1333 try {
1334 mPM.verifyIntentFilter(id, verificationCode, outFailedDomains);
1335 } catch (RemoteException e) {
1336 // Should never happen!
1337 }
1338 }
1339
1340 @Override
1341 public int getIntentVerificationStatus(String packageName, int userId) {
1342 try {
1343 return mPM.getIntentVerificationStatus(packageName, userId);
1344 } catch (RemoteException e) {
1345 // Should never happen!
1346 return PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED;
1347 }
1348 }
1349
1350 @Override
1351 public boolean updateIntentVerificationStatus(String packageName, int status, int userId) {
1352 try {
1353 return mPM.updateIntentVerificationStatus(packageName, status, userId);
1354 } catch (RemoteException e) {
1355 // Should never happen!
1356 return false;
1357 }
1358 }
1359
1360 @Override
1361 public List<IntentFilterVerificationInfo> getIntentFilterVerifications(String packageName) {
1362 try {
1363 return mPM.getIntentFilterVerifications(packageName);
1364 } catch (RemoteException e) {
1365 // Should never happen!
1366 return null;
1367 }
1368 }
1369
1370 @Override
Fabrice Di Meglio07885952015-04-06 19:41:28 -07001371 public List<IntentFilter> getAllIntentFilters(String packageName) {
1372 try {
1373 return mPM.getAllIntentFilters(packageName);
1374 } catch (RemoteException e) {
1375 // Should never happen!
1376 return null;
1377 }
1378 }
1379
1380 @Override
Fabrice Di Meglio62271722015-04-10 17:24:02 -07001381 public String getDefaultBrowserPackageName(int userId) {
1382 try {
1383 return mPM.getDefaultBrowserPackageName(userId);
1384 } catch (RemoteException e) {
1385 // Should never happen!
1386 return null;
1387 }
1388 }
1389
1390 @Override
1391 public boolean setDefaultBrowserPackageName(String packageName, int userId) {
1392 try {
1393 return mPM.setDefaultBrowserPackageName(packageName, userId);
1394 } catch (RemoteException e) {
1395 // Should never happen!
1396 return false;
1397 }
1398 }
1399
1400 @Override
Dianne Hackborn880119b2010-11-18 22:26:40 -08001401 public void setInstallerPackageName(String targetPackage,
1402 String installerPackageName) {
1403 try {
1404 mPM.setInstallerPackageName(targetPackage, installerPackageName);
1405 } catch (RemoteException e) {
1406 // Should never happen!
1407 }
1408 }
1409
1410 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001411 public void movePackage(String packageName, IPackageMoveObserver observer, int flags) {
1412 try {
1413 mPM.movePackage(packageName, observer, flags);
1414 } catch (RemoteException e) {
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -07001415 throw e.rethrowAsRuntimeException();
1416 }
1417 }
1418
1419 @Override
1420 public void movePackageAndData(String packageName, String volumeUuid,
1421 IPackageMoveObserver observer) {
1422 try {
1423 mPM.movePackageAndData(packageName, volumeUuid, observer);
1424 } catch (RemoteException e) {
1425 throw e.rethrowAsRuntimeException();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001426 }
1427 }
1428
1429 @Override
1430 public String getInstallerPackageName(String packageName) {
1431 try {
1432 return mPM.getInstallerPackageName(packageName);
1433 } catch (RemoteException e) {
1434 // Should never happen!
1435 }
1436 return null;
1437 }
1438
1439 @Override
1440 public void deletePackage(String packageName, IPackageDeleteObserver observer, int flags) {
1441 try {
Amith Yamasani67df64b2012-12-14 12:09:36 -08001442 mPM.deletePackageAsUser(packageName, observer, UserHandle.myUserId(), flags);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001443 } catch (RemoteException e) {
1444 // Should never happen!
1445 }
1446 }
Jeff Sharkeyfbd0e9f2014-08-06 16:34:34 -07001447
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001448 @Override
1449 public void clearApplicationUserData(String packageName,
1450 IPackageDataObserver observer) {
1451 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001452 mPM.clearApplicationUserData(packageName, observer, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001453 } catch (RemoteException e) {
1454 // Should never happen!
1455 }
1456 }
1457 @Override
1458 public void deleteApplicationCacheFiles(String packageName,
1459 IPackageDataObserver observer) {
1460 try {
1461 mPM.deleteApplicationCacheFiles(packageName, observer);
1462 } catch (RemoteException e) {
1463 // Should never happen!
1464 }
1465 }
1466 @Override
1467 public void freeStorageAndNotify(long idealStorageSize, IPackageDataObserver observer) {
1468 try {
1469 mPM.freeStorageAndNotify(idealStorageSize, observer);
1470 } catch (RemoteException e) {
1471 // Should never happen!
1472 }
1473 }
1474
1475 @Override
1476 public void freeStorage(long freeStorageSize, IntentSender pi) {
1477 try {
1478 mPM.freeStorage(freeStorageSize, pi);
1479 } catch (RemoteException e) {
1480 // Should never happen!
1481 }
1482 }
1483
1484 @Override
Dianne Hackborn0c380492012-08-20 17:23:30 -07001485 public void getPackageSizeInfo(String packageName, int userHandle,
1486 IPackageStatsObserver observer) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001487 try {
Dianne Hackborn0c380492012-08-20 17:23:30 -07001488 mPM.getPackageSizeInfo(packageName, userHandle, observer);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001489 } catch (RemoteException e) {
1490 // Should never happen!
1491 }
1492 }
1493 @Override
1494 public void addPackageToPreferred(String packageName) {
1495 try {
1496 mPM.addPackageToPreferred(packageName);
1497 } catch (RemoteException e) {
1498 // Should never happen!
1499 }
1500 }
1501
1502 @Override
1503 public void removePackageFromPreferred(String packageName) {
1504 try {
1505 mPM.removePackageFromPreferred(packageName);
1506 } catch (RemoteException e) {
1507 // Should never happen!
1508 }
1509 }
1510
1511 @Override
1512 public List<PackageInfo> getPreferredPackages(int flags) {
1513 try {
1514 return mPM.getPreferredPackages(flags);
1515 } catch (RemoteException e) {
1516 // Should never happen!
1517 }
1518 return new ArrayList<PackageInfo>();
1519 }
1520
1521 @Override
1522 public void addPreferredActivity(IntentFilter filter,
1523 int match, ComponentName[] set, ComponentName activity) {
1524 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001525 mPM.addPreferredActivity(filter, match, set, activity, mContext.getUserId());
Amith Yamasania3f133a2012-08-09 17:11:28 -07001526 } catch (RemoteException e) {
1527 // Should never happen!
1528 }
1529 }
1530
1531 @Override
1532 public void addPreferredActivity(IntentFilter filter, int match,
1533 ComponentName[] set, ComponentName activity, int userId) {
1534 try {
1535 mPM.addPreferredActivity(filter, match, set, activity, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001536 } catch (RemoteException e) {
1537 // Should never happen!
1538 }
1539 }
1540
1541 @Override
1542 public void replacePreferredActivity(IntentFilter filter,
1543 int match, ComponentName[] set, ComponentName activity) {
1544 try {
Amith Yamasani41c1ded2014-08-05 11:15:05 -07001545 mPM.replacePreferredActivity(filter, match, set, activity, UserHandle.myUserId());
1546 } catch (RemoteException e) {
1547 // Should never happen!
1548 }
1549 }
1550
1551 @Override
1552 public void replacePreferredActivityAsUser(IntentFilter filter,
1553 int match, ComponentName[] set, ComponentName activity,
1554 int userId) {
1555 try {
1556 mPM.replacePreferredActivity(filter, match, set, activity, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001557 } catch (RemoteException e) {
1558 // Should never happen!
1559 }
1560 }
1561
1562 @Override
1563 public void clearPackagePreferredActivities(String packageName) {
1564 try {
1565 mPM.clearPackagePreferredActivities(packageName);
1566 } catch (RemoteException e) {
1567 // Should never happen!
1568 }
1569 }
1570
1571 @Override
1572 public int getPreferredActivities(List<IntentFilter> outFilters,
1573 List<ComponentName> outActivities, String packageName) {
1574 try {
1575 return mPM.getPreferredActivities(outFilters, outActivities, packageName);
1576 } catch (RemoteException e) {
1577 // Should never happen!
1578 }
1579 return 0;
1580 }
1581
1582 @Override
Christopher Tatea2a0850d2013-09-05 16:38:58 -07001583 public ComponentName getHomeActivities(List<ResolveInfo> outActivities) {
1584 try {
1585 return mPM.getHomeActivities(outActivities);
1586 } catch (RemoteException e) {
1587 // Should never happen!
1588 }
1589 return null;
1590 }
1591
1592 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001593 public void setComponentEnabledSetting(ComponentName componentName,
1594 int newState, int flags) {
1595 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001596 mPM.setComponentEnabledSetting(componentName, newState, flags, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001597 } catch (RemoteException e) {
1598 // Should never happen!
1599 }
1600 }
1601
1602 @Override
1603 public int getComponentEnabledSetting(ComponentName componentName) {
1604 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001605 return mPM.getComponentEnabledSetting(componentName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001606 } catch (RemoteException e) {
1607 // Should never happen!
1608 }
1609 return PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
1610 }
1611
1612 @Override
1613 public void setApplicationEnabledSetting(String packageName,
1614 int newState, int flags) {
1615 try {
Dianne Hackborn3fa3c28a2013-03-26 16:15:41 -07001616 mPM.setApplicationEnabledSetting(packageName, newState, flags,
Dianne Hackborn95d78532013-09-11 09:51:14 -07001617 mContext.getUserId(), mContext.getOpPackageName());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001618 } catch (RemoteException e) {
1619 // Should never happen!
1620 }
1621 }
1622
1623 @Override
1624 public int getApplicationEnabledSetting(String packageName) {
1625 try {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001626 return mPM.getApplicationEnabledSetting(packageName, mContext.getUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001627 } catch (RemoteException e) {
1628 // Should never happen!
1629 }
1630 return PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
1631 }
1632
Amith Yamasani655d0e22013-06-12 14:19:10 -07001633 @Override
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001634 public boolean setApplicationHiddenSettingAsUser(String packageName, boolean hidden,
Amith Yamasani655d0e22013-06-12 14:19:10 -07001635 UserHandle user) {
1636 try {
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001637 return mPM.setApplicationHiddenSettingAsUser(packageName, hidden,
Amith Yamasani655d0e22013-06-12 14:19:10 -07001638 user.getIdentifier());
1639 } catch (RemoteException re) {
1640 // Should never happen!
1641 }
1642 return false;
1643 }
1644
1645 @Override
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001646 public boolean getApplicationHiddenSettingAsUser(String packageName, UserHandle user) {
Amith Yamasani655d0e22013-06-12 14:19:10 -07001647 try {
Amith Yamasanie5bcff62014-07-19 15:44:09 -07001648 return mPM.getApplicationHiddenSettingAsUser(packageName, user.getIdentifier());
Amith Yamasani655d0e22013-06-12 14:19:10 -07001649 } catch (RemoteException re) {
1650 // Should never happen!
1651 }
1652 return false;
1653 }
1654
dcashmanc6f22492014-08-14 09:54:51 -07001655 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07001656 @Override
1657 public KeySet getKeySetByAlias(String packageName, String alias) {
1658 Preconditions.checkNotNull(packageName);
1659 Preconditions.checkNotNull(alias);
dcashmanc6f22492014-08-14 09:54:51 -07001660 KeySet ks;
dcashman9d2f4412014-06-09 09:27:54 -07001661 try {
dcashmanc6f22492014-08-14 09:54:51 -07001662 ks = mPM.getKeySetByAlias(packageName, alias);
dcashman9d2f4412014-06-09 09:27:54 -07001663 } catch (RemoteException e) {
1664 return null;
1665 }
dcashmanc6f22492014-08-14 09:54:51 -07001666 return ks;
dcashman9d2f4412014-06-09 09:27:54 -07001667 }
1668
dcashmanc6f22492014-08-14 09:54:51 -07001669 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07001670 @Override
1671 public KeySet getSigningKeySet(String packageName) {
1672 Preconditions.checkNotNull(packageName);
dcashmanc6f22492014-08-14 09:54:51 -07001673 KeySet ks;
dcashman9d2f4412014-06-09 09:27:54 -07001674 try {
dcashmanc6f22492014-08-14 09:54:51 -07001675 ks = mPM.getSigningKeySet(packageName);
dcashman9d2f4412014-06-09 09:27:54 -07001676 } catch (RemoteException e) {
1677 return null;
1678 }
dcashmanc6f22492014-08-14 09:54:51 -07001679 return ks;
dcashman9d2f4412014-06-09 09:27:54 -07001680 }
1681
dcashmanc6f22492014-08-14 09:54:51 -07001682 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07001683 @Override
1684 public boolean isSignedBy(String packageName, KeySet ks) {
1685 Preconditions.checkNotNull(packageName);
1686 Preconditions.checkNotNull(ks);
dcashman9d2f4412014-06-09 09:27:54 -07001687 try {
dcashmanc6f22492014-08-14 09:54:51 -07001688 return mPM.isPackageSignedByKeySet(packageName, ks);
dcashman9d2f4412014-06-09 09:27:54 -07001689 } catch (RemoteException e) {
1690 return false;
1691 }
1692 }
1693
dcashmanc6f22492014-08-14 09:54:51 -07001694 /** @hide */
dcashman9d2f4412014-06-09 09:27:54 -07001695 @Override
1696 public boolean isSignedByExactly(String packageName, KeySet ks) {
1697 Preconditions.checkNotNull(packageName);
1698 Preconditions.checkNotNull(ks);
dcashman9d2f4412014-06-09 09:27:54 -07001699 try {
dcashmanc6f22492014-08-14 09:54:51 -07001700 return mPM.isPackageSignedByKeySetExactly(packageName, ks);
dcashman9d2f4412014-06-09 09:27:54 -07001701 } catch (RemoteException e) {
1702 return false;
1703 }
1704 }
1705
Kenny Root0aaa0d92011-09-12 16:42:55 -07001706 /**
1707 * @hide
1708 */
1709 @Override
1710 public VerifierDeviceIdentity getVerifierDeviceIdentity() {
1711 try {
1712 return mPM.getVerifierDeviceIdentity();
1713 } catch (RemoteException e) {
1714 // Should never happen!
1715 }
1716 return null;
1717 }
1718
Jeff Hao9f60c082014-10-28 18:51:07 -07001719 /**
1720 * @hide
1721 */
1722 @Override
1723 public boolean isUpgrade() {
1724 try {
1725 return mPM.isUpgrade();
1726 } catch (RemoteException e) {
1727 return false;
1728 }
1729 }
1730
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07001731 @Override
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07001732 public PackageInstaller getPackageInstaller() {
1733 synchronized (mLock) {
1734 if (mInstaller == null) {
1735 try {
Jeff Sharkeya0907432014-08-15 10:23:11 -07001736 mInstaller = new PackageInstaller(mContext, this, mPM.getPackageInstaller(),
Jeff Sharkey16c8e3f2014-07-24 17:08:17 -07001737 mContext.getPackageName(), mContext.getUserId());
1738 } catch (RemoteException e) {
1739 throw e.rethrowAsRuntimeException();
1740 }
1741 }
1742 return mInstaller;
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07001743 }
1744 }
1745
Jeff Sharkey6c833e02014-07-14 22:44:30 -07001746 @Override
1747 public boolean isPackageAvailable(String packageName) {
1748 try {
1749 return mPM.isPackageAvailable(packageName, mContext.getUserId());
1750 } catch (RemoteException e) {
1751 throw e.rethrowAsRuntimeException();
1752 }
1753 }
1754
Nicolas Prevotc79586e2014-05-06 12:47:57 +01001755 /**
1756 * @hide
1757 */
1758 @Override
Nicolas Prevot63798c52014-05-27 13:22:38 +01001759 public void addCrossProfileIntentFilter(IntentFilter filter, int sourceUserId, int targetUserId,
1760 int flags) {
Nicolas Prevotc79586e2014-05-06 12:47:57 +01001761 try {
Nicolas Prevot3f7777f2014-07-24 15:58:39 +01001762 mPM.addCrossProfileIntentFilter(filter, mContext.getOpPackageName(),
Nicolas Prevot4b8d5822015-03-05 15:20:49 +00001763 sourceUserId, targetUserId, flags);
Nicolas Prevotc79586e2014-05-06 12:47:57 +01001764 } catch (RemoteException e) {
1765 // Should never happen!
1766 }
1767 }
1768
1769 /**
1770 * @hide
1771 */
1772 @Override
Nicolas Prevot81948992014-05-16 18:25:26 +01001773 public void clearCrossProfileIntentFilters(int sourceUserId) {
Nicolas Prevotc79586e2014-05-06 12:47:57 +01001774 try {
Nicolas Prevot4b8d5822015-03-05 15:20:49 +00001775 mPM.clearCrossProfileIntentFilters(sourceUserId, mContext.getOpPackageName());
Nicolas Prevotc79586e2014-05-06 12:47:57 +01001776 } catch (RemoteException e) {
1777 // Should never happen!
1778 }
1779 }
1780
Nicolas Prevot88cc3462014-05-14 14:51:48 +01001781 /**
1782 * @hide
1783 */
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01001784 public Drawable loadItemIcon(PackageItemInfo itemInfo, ApplicationInfo appInfo) {
Benjamin Franzec2d48b2014-10-01 15:38:43 +01001785 Drawable dr = loadUnbadgedItemIcon(itemInfo, appInfo);
1786 if (itemInfo.showUserIcon != UserHandle.USER_NULL) {
1787 return dr;
1788 }
1789 return getUserBadgedIcon(dr, new UserHandle(mContext.getUserId()));
1790 }
1791
1792 /**
1793 * @hide
1794 */
1795 public Drawable loadUnbadgedItemIcon(PackageItemInfo itemInfo, ApplicationInfo appInfo) {
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01001796 if (itemInfo.showUserIcon != UserHandle.USER_NULL) {
Alexandra Gherghina64d4dca2014-08-28 18:26:56 +01001797 Bitmap bitmap = getUserManager().getUserIcon(itemInfo.showUserIcon);
1798 if (bitmap == null) {
1799 return UserIcons.getDefaultUserIcon(itemInfo.showUserIcon, /* light= */ false);
1800 }
1801 return new BitmapDrawable(bitmap);
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01001802 }
Alexandra Gherghinadb811db2014-08-29 13:43:59 +01001803 Drawable dr = null;
1804 if (itemInfo.packageName != null) {
1805 dr = getDrawable(itemInfo.packageName, itemInfo.icon, appInfo);
1806 }
Alexandra Gherghinaa71e3902014-07-25 20:03:47 +01001807 if (dr == null) {
Alexandra Gherghinaa7093142014-07-30 13:43:39 +01001808 dr = itemInfo.loadDefaultIcon(this);
Nicolas Prevot7f7b0c72014-06-23 15:59:38 +01001809 }
Benjamin Franzec2d48b2014-10-01 15:38:43 +01001810 return dr;
Svetoslavc7d62f02014-09-04 15:39:54 -07001811 }
1812
1813 private Drawable getBadgedDrawable(Drawable drawable, Drawable badgeDrawable,
1814 Rect badgeLocation, boolean tryBadgeInPlace) {
1815 final int badgedWidth = drawable.getIntrinsicWidth();
1816 final int badgedHeight = drawable.getIntrinsicHeight();
1817 final boolean canBadgeInPlace = tryBadgeInPlace
1818 && (drawable instanceof BitmapDrawable)
1819 && ((BitmapDrawable) drawable).getBitmap().isMutable();
1820
1821 final Bitmap bitmap;
1822 if (canBadgeInPlace) {
1823 bitmap = ((BitmapDrawable) drawable).getBitmap();
1824 } else {
1825 bitmap = Bitmap.createBitmap(badgedWidth, badgedHeight, Bitmap.Config.ARGB_8888);
1826 }
1827 Canvas canvas = new Canvas(bitmap);
1828
1829 if (!canBadgeInPlace) {
1830 drawable.setBounds(0, 0, badgedWidth, badgedHeight);
1831 drawable.draw(canvas);
1832 }
1833
1834 if (badgeLocation != null) {
1835 if (badgeLocation.left < 0 || badgeLocation.top < 0
1836 || badgeLocation.width() > badgedWidth || badgeLocation.height() > badgedHeight) {
1837 throw new IllegalArgumentException("Badge location " + badgeLocation
1838 + " not in badged drawable bounds "
1839 + new Rect(0, 0, badgedWidth, badgedHeight));
1840 }
1841 badgeDrawable.setBounds(0, 0, badgeLocation.width(), badgeLocation.height());
1842
1843 canvas.save();
1844 canvas.translate(badgeLocation.left, badgeLocation.top);
1845 badgeDrawable.draw(canvas);
1846 canvas.restore();
1847 } else {
1848 badgeDrawable.setBounds(0, 0, badgedWidth, badgedHeight);
1849 badgeDrawable.draw(canvas);
1850 }
1851
1852 if (!canBadgeInPlace) {
1853 BitmapDrawable mergedDrawable = new BitmapDrawable(mContext.getResources(), bitmap);
1854
1855 if (drawable instanceof BitmapDrawable) {
1856 BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
1857 mergedDrawable.setTargetDensity(bitmapDrawable.getBitmap().getDensity());
1858 }
1859
1860 return mergedDrawable;
1861 }
1862
1863 return drawable;
1864 }
1865
1866 private int getBadgeResIdForUser(int userHandle) {
1867 // Return the framework-provided badge.
1868 UserInfo userInfo = getUserIfProfile(userHandle);
1869 if (userInfo != null && userInfo.isManagedProfile()) {
1870 return com.android.internal.R.drawable.ic_corp_icon_badge;
1871 }
1872 return 0;
1873 }
1874
1875 private UserInfo getUserIfProfile(int userHandle) {
Svetoslav7de2abb2014-09-05 11:28:00 -07001876 List<UserInfo> userProfiles = getUserManager().getProfiles(UserHandle.myUserId());
Svetoslavc7d62f02014-09-04 15:39:54 -07001877 for (UserInfo user : userProfiles) {
1878 if (user.id == userHandle) {
1879 return user;
1880 }
1881 }
1882 return null;
Nicolas Prevot88cc3462014-05-14 14:51:48 +01001883 }
1884
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001885 private final ContextImpl mContext;
1886 private final IPackageManager mPM;
1887
1888 private static final Object sSync = new Object();
Dianne Hackbornadd005c2013-07-17 18:43:12 -07001889 private static ArrayMap<ResourceName, WeakReference<Drawable.ConstantState>> sIconCache
1890 = new ArrayMap<ResourceName, WeakReference<Drawable.ConstantState>>();
1891 private static ArrayMap<ResourceName, WeakReference<CharSequence>> sStringCache
1892 = new ArrayMap<ResourceName, WeakReference<CharSequence>>();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001893}