blob: 0f10c4f10cd4f60d5aa29cc8579541a6015fdd7a [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
19import android.content.ComponentName;
20import android.content.ContentResolver;
21import android.content.Intent;
22import android.content.IntentFilter;
23import android.content.IntentSender;
24import android.content.pm.ActivityInfo;
25import android.content.pm.ApplicationInfo;
26import android.content.pm.ComponentInfo;
Anonymous Cowardceb1b0b2012-04-24 10:35:16 -070027import android.content.pm.ContainerEncryptionParams;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080028import android.content.pm.FeatureInfo;
29import android.content.pm.IPackageDataObserver;
30import android.content.pm.IPackageDeleteObserver;
31import android.content.pm.IPackageInstallObserver;
32import android.content.pm.IPackageManager;
33import android.content.pm.IPackageMoveObserver;
34import android.content.pm.IPackageStatsObserver;
35import android.content.pm.InstrumentationInfo;
36import android.content.pm.PackageInfo;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080037import android.content.pm.PackageManager;
Kenny Roote6cd0c72011-05-19 12:48:14 -070038import android.content.pm.ParceledListSlice;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080039import android.content.pm.PermissionGroupInfo;
40import android.content.pm.PermissionInfo;
41import android.content.pm.ProviderInfo;
42import android.content.pm.ResolveInfo;
43import android.content.pm.ServiceInfo;
Kenny Root5ab21572011-07-27 11:11:19 -070044import android.content.pm.ManifestDigest;
Amith Yamasani258848d2012-08-10 17:06:33 -070045import android.content.pm.UserInfo;
rich cannings706e8ba2012-08-20 13:20:14 -070046import android.content.pm.VerificationParams;
Kenny Root0aaa0d92011-09-12 16:42:55 -070047import android.content.pm.VerifierDeviceIdentity;
Dianne Hackborn7767eac2012-08-23 18:25:40 -070048import android.content.pm.PackageManager.NameNotFoundException;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080049import android.content.res.Resources;
50import android.content.res.XmlResourceParser;
51import android.graphics.drawable.Drawable;
52import android.net.Uri;
53import android.os.Process;
54import android.os.RemoteException;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070055import android.os.UserHandle;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080056import android.util.Log;
57
58import java.lang.ref.WeakReference;
59import java.util.ArrayList;
60import java.util.HashMap;
61import java.util.Iterator;
62import java.util.List;
63
64/*package*/
65final class ApplicationPackageManager extends PackageManager {
66 private static final String TAG = "ApplicationPackageManager";
67 private final static boolean DEBUG = false;
68 private final static boolean DEBUG_ICONS = false;
69
70 @Override
71 public PackageInfo getPackageInfo(String packageName, int flags)
72 throws NameNotFoundException {
73 try {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070074 PackageInfo pi = mPM.getPackageInfo(packageName, flags, UserHandle.myUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -080075 if (pi != null) {
76 return pi;
77 }
78 } catch (RemoteException e) {
79 throw new RuntimeException("Package manager has died", e);
80 }
81
82 throw new NameNotFoundException(packageName);
83 }
84
85 @Override
86 public String[] currentToCanonicalPackageNames(String[] names) {
87 try {
88 return mPM.currentToCanonicalPackageNames(names);
89 } catch (RemoteException e) {
90 throw new RuntimeException("Package manager has died", e);
91 }
92 }
93
94 @Override
95 public String[] canonicalToCurrentPackageNames(String[] names) {
96 try {
97 return mPM.canonicalToCurrentPackageNames(names);
98 } catch (RemoteException e) {
99 throw new RuntimeException("Package manager has died", e);
100 }
101 }
102
103 @Override
104 public Intent getLaunchIntentForPackage(String packageName) {
105 // First see if the package has an INFO activity; the existence of
106 // such an activity is implied to be the desired front-door for the
107 // overall package (such as if it has multiple launcher entries).
108 Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
109 intentToResolve.addCategory(Intent.CATEGORY_INFO);
110 intentToResolve.setPackage(packageName);
Dianne Hackborn19415762010-12-15 00:20:27 -0800111 List<ResolveInfo> ris = queryIntentActivities(intentToResolve, 0);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800112
113 // Otherwise, try to find a main launcher activity.
Dianne Hackborn19415762010-12-15 00:20:27 -0800114 if (ris == null || ris.size() <= 0) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800115 // reuse the intent instance
116 intentToResolve.removeCategory(Intent.CATEGORY_INFO);
117 intentToResolve.addCategory(Intent.CATEGORY_LAUNCHER);
118 intentToResolve.setPackage(packageName);
Dianne Hackborn19415762010-12-15 00:20:27 -0800119 ris = queryIntentActivities(intentToResolve, 0);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800120 }
Dianne Hackborn19415762010-12-15 00:20:27 -0800121 if (ris == null || ris.size() <= 0) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800122 return null;
123 }
124 Intent intent = new Intent(intentToResolve);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800125 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Dianne Hackborn19415762010-12-15 00:20:27 -0800126 intent.setClassName(ris.get(0).activityInfo.packageName,
127 ris.get(0).activityInfo.name);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800128 return intent;
129 }
130
131 @Override
132 public int[] getPackageGids(String packageName)
133 throws NameNotFoundException {
134 try {
135 int[] gids = mPM.getPackageGids(packageName);
136 if (gids == null || gids.length > 0) {
137 return gids;
138 }
139 } catch (RemoteException e) {
140 throw new RuntimeException("Package manager has died", e);
141 }
142
143 throw new NameNotFoundException(packageName);
144 }
145
146 @Override
147 public PermissionInfo getPermissionInfo(String name, int flags)
148 throws NameNotFoundException {
149 try {
150 PermissionInfo pi = mPM.getPermissionInfo(name, flags);
151 if (pi != null) {
152 return pi;
153 }
154 } catch (RemoteException e) {
155 throw new RuntimeException("Package manager has died", e);
156 }
157
158 throw new NameNotFoundException(name);
159 }
160
161 @Override
162 public List<PermissionInfo> queryPermissionsByGroup(String group, int flags)
163 throws NameNotFoundException {
164 try {
165 List<PermissionInfo> pi = mPM.queryPermissionsByGroup(group, flags);
166 if (pi != null) {
167 return pi;
168 }
169 } catch (RemoteException e) {
170 throw new RuntimeException("Package manager has died", e);
171 }
172
173 throw new NameNotFoundException(group);
174 }
175
176 @Override
177 public PermissionGroupInfo getPermissionGroupInfo(String name,
178 int flags) throws NameNotFoundException {
179 try {
180 PermissionGroupInfo pgi = mPM.getPermissionGroupInfo(name, flags);
181 if (pgi != null) {
182 return pgi;
183 }
184 } catch (RemoteException e) {
185 throw new RuntimeException("Package manager has died", e);
186 }
187
188 throw new NameNotFoundException(name);
189 }
190
191 @Override
192 public List<PermissionGroupInfo> getAllPermissionGroups(int flags) {
193 try {
194 return mPM.getAllPermissionGroups(flags);
195 } catch (RemoteException e) {
196 throw new RuntimeException("Package manager has died", e);
197 }
198 }
199
200 @Override
201 public ApplicationInfo getApplicationInfo(String packageName, int flags)
202 throws NameNotFoundException {
203 try {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700204 ApplicationInfo ai = mPM.getApplicationInfo(packageName, flags, UserHandle.myUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800205 if (ai != null) {
206 return ai;
207 }
208 } catch (RemoteException e) {
209 throw new RuntimeException("Package manager has died", e);
210 }
211
212 throw new NameNotFoundException(packageName);
213 }
214
215 @Override
216 public ActivityInfo getActivityInfo(ComponentName className, int flags)
217 throws NameNotFoundException {
218 try {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700219 ActivityInfo ai = mPM.getActivityInfo(className, flags, UserHandle.myUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800220 if (ai != null) {
221 return ai;
222 }
223 } catch (RemoteException e) {
224 throw new RuntimeException("Package manager has died", e);
225 }
226
227 throw new NameNotFoundException(className.toString());
228 }
229
230 @Override
231 public ActivityInfo getReceiverInfo(ComponentName className, int flags)
232 throws NameNotFoundException {
233 try {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700234 ActivityInfo ai = mPM.getReceiverInfo(className, flags, UserHandle.myUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800235 if (ai != null) {
236 return ai;
237 }
238 } catch (RemoteException e) {
239 throw new RuntimeException("Package manager has died", e);
240 }
241
242 throw new NameNotFoundException(className.toString());
243 }
244
245 @Override
246 public ServiceInfo getServiceInfo(ComponentName className, int flags)
247 throws NameNotFoundException {
248 try {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700249 ServiceInfo si = mPM.getServiceInfo(className, flags, UserHandle.myUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800250 if (si != null) {
251 return si;
252 }
253 } catch (RemoteException e) {
254 throw new RuntimeException("Package manager has died", e);
255 }
256
257 throw new NameNotFoundException(className.toString());
258 }
259
260 @Override
261 public ProviderInfo getProviderInfo(ComponentName className, int flags)
262 throws NameNotFoundException {
263 try {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700264 ProviderInfo pi = mPM.getProviderInfo(className, flags, UserHandle.myUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800265 if (pi != null) {
266 return pi;
267 }
268 } catch (RemoteException e) {
269 throw new RuntimeException("Package manager has died", e);
270 }
271
272 throw new NameNotFoundException(className.toString());
273 }
274
275 @Override
276 public String[] getSystemSharedLibraryNames() {
277 try {
278 return mPM.getSystemSharedLibraryNames();
279 } catch (RemoteException e) {
280 throw new RuntimeException("Package manager has died", e);
281 }
282 }
283
284 @Override
285 public FeatureInfo[] getSystemAvailableFeatures() {
286 try {
287 return mPM.getSystemAvailableFeatures();
288 } catch (RemoteException e) {
289 throw new RuntimeException("Package manager has died", e);
290 }
291 }
292
293 @Override
294 public boolean hasSystemFeature(String name) {
295 try {
296 return mPM.hasSystemFeature(name);
297 } catch (RemoteException e) {
298 throw new RuntimeException("Package manager has died", e);
299 }
300 }
301
302 @Override
303 public int checkPermission(String permName, String pkgName) {
304 try {
305 return mPM.checkPermission(permName, pkgName);
306 } catch (RemoteException e) {
307 throw new RuntimeException("Package manager has died", e);
308 }
309 }
310
311 @Override
312 public boolean addPermission(PermissionInfo info) {
313 try {
314 return mPM.addPermission(info);
315 } catch (RemoteException e) {
316 throw new RuntimeException("Package manager has died", e);
317 }
318 }
319
320 @Override
321 public boolean addPermissionAsync(PermissionInfo info) {
322 try {
323 return mPM.addPermissionAsync(info);
324 } catch (RemoteException e) {
325 throw new RuntimeException("Package manager has died", e);
326 }
327 }
328
329 @Override
330 public void removePermission(String name) {
331 try {
332 mPM.removePermission(name);
333 } catch (RemoteException e) {
334 throw new RuntimeException("Package manager has died", e);
335 }
336 }
337
338 @Override
Dianne Hackborne639da72012-02-21 15:11:13 -0800339 public void grantPermission(String packageName, String permissionName) {
340 try {
341 mPM.grantPermission(packageName, permissionName);
342 } catch (RemoteException e) {
343 throw new RuntimeException("Package manager has died", e);
344 }
345 }
346
347 @Override
348 public void revokePermission(String packageName, String permissionName) {
349 try {
350 mPM.revokePermission(packageName, permissionName);
351 } catch (RemoteException e) {
352 throw new RuntimeException("Package manager has died", e);
353 }
354 }
355
356 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800357 public int checkSignatures(String pkg1, String pkg2) {
358 try {
359 return mPM.checkSignatures(pkg1, pkg2);
360 } catch (RemoteException e) {
361 throw new RuntimeException("Package manager has died", e);
362 }
363 }
364
365 @Override
366 public int checkSignatures(int uid1, int uid2) {
367 try {
368 return mPM.checkUidSignatures(uid1, uid2);
369 } catch (RemoteException e) {
370 throw new RuntimeException("Package manager has died", e);
371 }
372 }
373
374 @Override
375 public String[] getPackagesForUid(int uid) {
376 try {
377 return mPM.getPackagesForUid(uid);
378 } catch (RemoteException e) {
379 throw new RuntimeException("Package manager has died", e);
380 }
381 }
382
383 @Override
384 public String getNameForUid(int uid) {
385 try {
386 return mPM.getNameForUid(uid);
387 } catch (RemoteException e) {
388 throw new RuntimeException("Package manager has died", e);
389 }
390 }
391
392 @Override
393 public int getUidForSharedUser(String sharedUserName)
394 throws NameNotFoundException {
395 try {
396 int uid = mPM.getUidForSharedUser(sharedUserName);
397 if(uid != -1) {
398 return uid;
399 }
400 } catch (RemoteException e) {
401 throw new RuntimeException("Package manager has died", e);
402 }
403 throw new NameNotFoundException("No shared userid for user:"+sharedUserName);
404 }
405
Kenny Roote6cd0c72011-05-19 12:48:14 -0700406 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800407 @Override
408 public List<PackageInfo> getInstalledPackages(int flags) {
409 try {
Kenny Roote6cd0c72011-05-19 12:48:14 -0700410 final List<PackageInfo> packageInfos = new ArrayList<PackageInfo>();
411 PackageInfo lastItem = null;
412 ParceledListSlice<PackageInfo> slice;
413
414 do {
415 final String lastKey = lastItem != null ? lastItem.packageName : null;
416 slice = mPM.getInstalledPackages(flags, lastKey);
417 lastItem = slice.populateList(packageInfos, PackageInfo.CREATOR);
418 } while (!slice.isLastSlice());
419
420 return packageInfos;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800421 } catch (RemoteException e) {
422 throw new RuntimeException("Package manager has died", e);
423 }
424 }
425
Kenny Roote6cd0c72011-05-19 12:48:14 -0700426 @SuppressWarnings("unchecked")
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800427 @Override
428 public List<ApplicationInfo> getInstalledApplications(int flags) {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700429 int userId = UserHandle.getUserId(Process.myUid());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800430 try {
Kenny Roote6cd0c72011-05-19 12:48:14 -0700431 final List<ApplicationInfo> applicationInfos = new ArrayList<ApplicationInfo>();
432 ApplicationInfo lastItem = null;
433 ParceledListSlice<ApplicationInfo> slice;
434
435 do {
436 final String lastKey = lastItem != null ? lastItem.packageName : null;
Amith Yamasani483f3b02012-03-13 16:08:00 -0700437 slice = mPM.getInstalledApplications(flags, lastKey, userId);
Kenny Roote6cd0c72011-05-19 12:48:14 -0700438 lastItem = slice.populateList(applicationInfos, ApplicationInfo.CREATOR);
439 } while (!slice.isLastSlice());
440
441 return applicationInfos;
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800442 } catch (RemoteException e) {
443 throw new RuntimeException("Package manager has died", e);
444 }
445 }
446
447 @Override
448 public ResolveInfo resolveActivity(Intent intent, int flags) {
449 try {
450 return mPM.resolveIntent(
451 intent,
452 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700453 flags, UserHandle.myUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800454 } catch (RemoteException e) {
455 throw new RuntimeException("Package manager has died", e);
456 }
457 }
458
459 @Override
460 public List<ResolveInfo> queryIntentActivities(Intent intent,
461 int flags) {
462 try {
463 return mPM.queryIntentActivities(
464 intent,
465 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700466 flags,
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700467 UserHandle.myUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800468 } catch (RemoteException e) {
469 throw new RuntimeException("Package manager has died", e);
470 }
471 }
472
473 @Override
474 public List<ResolveInfo> queryIntentActivityOptions(
475 ComponentName caller, Intent[] specifics, Intent intent,
476 int flags) {
477 final ContentResolver resolver = mContext.getContentResolver();
478
479 String[] specificTypes = null;
480 if (specifics != null) {
481 final int N = specifics.length;
482 for (int i=0; i<N; i++) {
483 Intent sp = specifics[i];
484 if (sp != null) {
485 String t = sp.resolveTypeIfNeeded(resolver);
486 if (t != null) {
487 if (specificTypes == null) {
488 specificTypes = new String[N];
489 }
490 specificTypes[i] = t;
491 }
492 }
493 }
494 }
495
496 try {
497 return mPM.queryIntentActivityOptions(caller, specifics,
498 specificTypes, intent, intent.resolveTypeIfNeeded(resolver),
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700499 flags, UserHandle.myUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800500 } catch (RemoteException e) {
501 throw new RuntimeException("Package manager has died", e);
502 }
503 }
504
505 @Override
506 public List<ResolveInfo> queryBroadcastReceivers(Intent intent, int flags) {
507 try {
508 return mPM.queryIntentReceivers(
509 intent,
510 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700511 flags,
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700512 UserHandle.myUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800513 } catch (RemoteException e) {
514 throw new RuntimeException("Package manager has died", e);
515 }
516 }
517
518 @Override
519 public ResolveInfo resolveService(Intent intent, int flags) {
520 try {
521 return mPM.resolveService(
522 intent,
523 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700524 flags,
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700525 UserHandle.myUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800526 } catch (RemoteException e) {
527 throw new RuntimeException("Package manager has died", e);
528 }
529 }
530
531 @Override
532 public List<ResolveInfo> queryIntentServices(Intent intent, int flags) {
533 try {
534 return mPM.queryIntentServices(
535 intent,
536 intent.resolveTypeIfNeeded(mContext.getContentResolver()),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700537 flags,
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700538 UserHandle.myUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800539 } catch (RemoteException e) {
540 throw new RuntimeException("Package manager has died", e);
541 }
542 }
543
544 @Override
545 public ProviderInfo resolveContentProvider(String name,
546 int flags) {
547 try {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700548 return mPM.resolveContentProvider(name, flags, UserHandle.myUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800549 } catch (RemoteException e) {
550 throw new RuntimeException("Package manager has died", e);
551 }
552 }
553
554 @Override
555 public List<ProviderInfo> queryContentProviders(String processName,
556 int uid, int flags) {
557 try {
558 return mPM.queryContentProviders(processName, uid, flags);
559 } catch (RemoteException e) {
560 throw new RuntimeException("Package manager has died", e);
561 }
562 }
563
564 @Override
565 public InstrumentationInfo getInstrumentationInfo(
566 ComponentName className, int flags)
567 throws NameNotFoundException {
568 try {
569 InstrumentationInfo ii = mPM.getInstrumentationInfo(
570 className, flags);
571 if (ii != null) {
572 return ii;
573 }
574 } catch (RemoteException e) {
575 throw new RuntimeException("Package manager has died", e);
576 }
577
578 throw new NameNotFoundException(className.toString());
579 }
580
581 @Override
582 public List<InstrumentationInfo> queryInstrumentation(
583 String targetPackage, int flags) {
584 try {
585 return mPM.queryInstrumentation(targetPackage, flags);
586 } catch (RemoteException e) {
587 throw new RuntimeException("Package manager has died", e);
588 }
589 }
590
591 @Override public Drawable getDrawable(String packageName, int resid,
592 ApplicationInfo appInfo) {
593 ResourceName name = new ResourceName(packageName, resid);
594 Drawable dr = getCachedIcon(name);
595 if (dr != null) {
596 return dr;
597 }
598 if (appInfo == null) {
599 try {
600 appInfo = getApplicationInfo(packageName, 0);
601 } catch (NameNotFoundException e) {
602 return null;
603 }
604 }
605 try {
606 Resources r = getResourcesForApplication(appInfo);
607 dr = r.getDrawable(resid);
608 if (false) {
609 RuntimeException e = new RuntimeException("here");
610 e.fillInStackTrace();
611 Log.w(TAG, "Getting drawable 0x" + Integer.toHexString(resid)
612 + " from package " + packageName
613 + ": app scale=" + r.getCompatibilityInfo().applicationScale
614 + ", caller scale=" + mContext.getResources().getCompatibilityInfo().applicationScale,
615 e);
616 }
617 if (DEBUG_ICONS) Log.v(TAG, "Getting drawable 0x"
618 + Integer.toHexString(resid) + " from " + r
619 + ": " + dr);
620 putCachedIcon(name, dr);
621 return dr;
622 } catch (NameNotFoundException e) {
623 Log.w("PackageManager", "Failure retrieving resources for"
624 + appInfo.packageName);
Joe Onorato08f16542011-01-20 11:49:56 -0800625 } catch (Resources.NotFoundException e) {
626 Log.w("PackageManager", "Failure retrieving resources for"
627 + appInfo.packageName + ": " + e.getMessage());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800628 } catch (RuntimeException e) {
629 // If an exception was thrown, fall through to return
630 // default icon.
631 Log.w("PackageManager", "Failure retrieving icon 0x"
632 + Integer.toHexString(resid) + " in package "
633 + packageName, e);
634 }
635 return null;
636 }
637
638 @Override public Drawable getActivityIcon(ComponentName activityName)
639 throws NameNotFoundException {
640 return getActivityInfo(activityName, 0).loadIcon(this);
641 }
642
643 @Override public Drawable getActivityIcon(Intent intent)
644 throws NameNotFoundException {
645 if (intent.getComponent() != null) {
646 return getActivityIcon(intent.getComponent());
647 }
648
649 ResolveInfo info = resolveActivity(
650 intent, PackageManager.MATCH_DEFAULT_ONLY);
651 if (info != null) {
652 return info.activityInfo.loadIcon(this);
653 }
654
Romain Guy39fe17c2011-11-30 10:34:07 -0800655 throw new NameNotFoundException(intent.toUri(0));
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800656 }
657
658 @Override public Drawable getDefaultActivityIcon() {
659 return Resources.getSystem().getDrawable(
660 com.android.internal.R.drawable.sym_def_app_icon);
661 }
662
663 @Override public Drawable getApplicationIcon(ApplicationInfo info) {
664 return info.loadIcon(this);
665 }
666
667 @Override public Drawable getApplicationIcon(String packageName)
668 throws NameNotFoundException {
669 return getApplicationIcon(getApplicationInfo(packageName, 0));
670 }
671
672 @Override
673 public Drawable getActivityLogo(ComponentName activityName)
674 throws NameNotFoundException {
675 return getActivityInfo(activityName, 0).loadLogo(this);
676 }
677
678 @Override
679 public Drawable getActivityLogo(Intent intent)
680 throws NameNotFoundException {
681 if (intent.getComponent() != null) {
682 return getActivityLogo(intent.getComponent());
683 }
684
685 ResolveInfo info = resolveActivity(
686 intent, PackageManager.MATCH_DEFAULT_ONLY);
687 if (info != null) {
688 return info.activityInfo.loadLogo(this);
689 }
690
691 throw new NameNotFoundException(intent.toUri(0));
692 }
693
694 @Override
695 public Drawable getApplicationLogo(ApplicationInfo info) {
696 return info.loadLogo(this);
697 }
698
699 @Override
700 public Drawable getApplicationLogo(String packageName)
701 throws NameNotFoundException {
702 return getApplicationLogo(getApplicationInfo(packageName, 0));
703 }
704
705 @Override public Resources getResourcesForActivity(
706 ComponentName activityName) throws NameNotFoundException {
707 return getResourcesForApplication(
708 getActivityInfo(activityName, 0).applicationInfo);
709 }
710
711 @Override public Resources getResourcesForApplication(
712 ApplicationInfo app) throws NameNotFoundException {
713 if (app.packageName.equals("system")) {
714 return mContext.mMainThread.getSystemContext().getResources();
715 }
716 Resources r = mContext.mMainThread.getTopLevelResources(
717 app.uid == Process.myUid() ? app.sourceDir
Dianne Hackborn756220b2012-08-14 16:45:30 -0700718 : app.publicSourceDir, null, mContext.mPackageInfo);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800719 if (r != null) {
720 return r;
721 }
722 throw new NameNotFoundException("Unable to open " + app.publicSourceDir);
723 }
724
725 @Override public Resources getResourcesForApplication(
726 String appPackageName) throws NameNotFoundException {
727 return getResourcesForApplication(
728 getApplicationInfo(appPackageName, 0));
729 }
730
731 int mCachedSafeMode = -1;
732 @Override public boolean isSafeMode() {
733 try {
734 if (mCachedSafeMode < 0) {
735 mCachedSafeMode = mPM.isSafeMode() ? 1 : 0;
736 }
737 return mCachedSafeMode != 0;
738 } catch (RemoteException e) {
739 throw new RuntimeException("Package manager has died", e);
740 }
741 }
742
743 static void configurationChanged() {
744 synchronized (sSync) {
745 sIconCache.clear();
746 sStringCache.clear();
747 }
748 }
749
750 ApplicationPackageManager(ContextImpl context,
751 IPackageManager pm) {
752 mContext = context;
753 mPM = pm;
754 }
755
756 private Drawable getCachedIcon(ResourceName name) {
757 synchronized (sSync) {
Romain Guy39fe17c2011-11-30 10:34:07 -0800758 WeakReference<Drawable.ConstantState> wr = sIconCache.get(name);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800759 if (DEBUG_ICONS) Log.v(TAG, "Get cached weak drawable ref for "
760 + name + ": " + wr);
761 if (wr != null) { // we have the activity
Romain Guy39fe17c2011-11-30 10:34:07 -0800762 Drawable.ConstantState state = wr.get();
763 if (state != null) {
764 if (DEBUG_ICONS) {
765 Log.v(TAG, "Get cached drawable state for " + name + ": " + state);
766 }
767 // Note: It's okay here to not use the newDrawable(Resources) variant
768 // of the API. The ConstantState comes from a drawable that was
769 // originally created by passing the proper app Resources instance
770 // which means the state should already contain the proper
771 // resources specific information (like density.) See
772 // BitmapDrawable.BitmapState for instance.
773 return state.newDrawable();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800774 }
775 // our entry has been purged
776 sIconCache.remove(name);
777 }
778 }
779 return null;
780 }
781
782 private void putCachedIcon(ResourceName name, Drawable dr) {
783 synchronized (sSync) {
Romain Guy39fe17c2011-11-30 10:34:07 -0800784 sIconCache.put(name, new WeakReference<Drawable.ConstantState>(dr.getConstantState()));
785 if (DEBUG_ICONS) Log.v(TAG, "Added cached drawable state for " + name + ": " + dr);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800786 }
787 }
788
Romain Guy39fe17c2011-11-30 10:34:07 -0800789 static void handlePackageBroadcast(int cmd, String[] pkgList, boolean hasPkgInfo) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800790 boolean immediateGc = false;
791 if (cmd == IApplicationThread.EXTERNAL_STORAGE_UNAVAILABLE) {
792 immediateGc = true;
793 }
794 if (pkgList != null && (pkgList.length > 0)) {
795 boolean needCleanup = false;
796 for (String ssp : pkgList) {
797 synchronized (sSync) {
798 if (sIconCache.size() > 0) {
799 Iterator<ResourceName> it = sIconCache.keySet().iterator();
800 while (it.hasNext()) {
801 ResourceName nm = it.next();
802 if (nm.packageName.equals(ssp)) {
803 //Log.i(TAG, "Removing cached drawable for " + nm);
804 it.remove();
805 needCleanup = true;
806 }
807 }
808 }
809 if (sStringCache.size() > 0) {
810 Iterator<ResourceName> it = sStringCache.keySet().iterator();
811 while (it.hasNext()) {
812 ResourceName nm = it.next();
813 if (nm.packageName.equals(ssp)) {
814 //Log.i(TAG, "Removing cached string for " + nm);
815 it.remove();
816 needCleanup = true;
817 }
818 }
819 }
820 }
821 }
822 if (needCleanup || hasPkgInfo) {
823 if (immediateGc) {
824 // Schedule an immediate gc.
825 Runtime.getRuntime().gc();
826 } else {
827 ActivityThread.currentActivityThread().scheduleGcIdler();
828 }
829 }
830 }
831 }
832
833 private static final class ResourceName {
834 final String packageName;
835 final int iconId;
836
837 ResourceName(String _packageName, int _iconId) {
838 packageName = _packageName;
839 iconId = _iconId;
840 }
841
842 ResourceName(ApplicationInfo aInfo, int _iconId) {
843 this(aInfo.packageName, _iconId);
844 }
845
846 ResourceName(ComponentInfo cInfo, int _iconId) {
847 this(cInfo.applicationInfo.packageName, _iconId);
848 }
849
850 ResourceName(ResolveInfo rInfo, int _iconId) {
851 this(rInfo.activityInfo.applicationInfo.packageName, _iconId);
852 }
853
854 @Override
855 public boolean equals(Object o) {
856 if (this == o) return true;
857 if (o == null || getClass() != o.getClass()) return false;
858
859 ResourceName that = (ResourceName) o;
860
861 if (iconId != that.iconId) return false;
862 return !(packageName != null ?
863 !packageName.equals(that.packageName) : that.packageName != null);
864
865 }
866
867 @Override
868 public int hashCode() {
869 int result;
870 result = packageName.hashCode();
871 result = 31 * result + iconId;
872 return result;
873 }
874
875 @Override
876 public String toString() {
877 return "{ResourceName " + packageName + " / " + iconId + "}";
878 }
879 }
880
881 private CharSequence getCachedString(ResourceName name) {
882 synchronized (sSync) {
883 WeakReference<CharSequence> wr = sStringCache.get(name);
884 if (wr != null) { // we have the activity
885 CharSequence cs = wr.get();
886 if (cs != null) {
887 return cs;
888 }
889 // our entry has been purged
890 sStringCache.remove(name);
891 }
892 }
893 return null;
894 }
895
896 private void putCachedString(ResourceName name, CharSequence cs) {
897 synchronized (sSync) {
898 sStringCache.put(name, new WeakReference<CharSequence>(cs));
899 }
900 }
901
902 @Override
903 public CharSequence getText(String packageName, int resid,
904 ApplicationInfo appInfo) {
905 ResourceName name = new ResourceName(packageName, resid);
906 CharSequence text = getCachedString(name);
907 if (text != null) {
908 return text;
909 }
910 if (appInfo == null) {
911 try {
912 appInfo = getApplicationInfo(packageName, 0);
913 } catch (NameNotFoundException e) {
914 return null;
915 }
916 }
917 try {
918 Resources r = getResourcesForApplication(appInfo);
919 text = r.getText(resid);
920 putCachedString(name, text);
921 return text;
922 } catch (NameNotFoundException e) {
923 Log.w("PackageManager", "Failure retrieving resources for"
924 + appInfo.packageName);
925 } catch (RuntimeException e) {
926 // If an exception was thrown, fall through to return
927 // default icon.
928 Log.w("PackageManager", "Failure retrieving text 0x"
929 + Integer.toHexString(resid) + " in package "
930 + packageName, e);
931 }
932 return null;
933 }
934
935 @Override
936 public XmlResourceParser getXml(String packageName, int resid,
937 ApplicationInfo appInfo) {
938 if (appInfo == null) {
939 try {
940 appInfo = getApplicationInfo(packageName, 0);
941 } catch (NameNotFoundException e) {
942 return null;
943 }
944 }
945 try {
946 Resources r = getResourcesForApplication(appInfo);
947 return r.getXml(resid);
948 } catch (RuntimeException e) {
949 // If an exception was thrown, fall through to return
950 // default icon.
951 Log.w("PackageManager", "Failure retrieving xml 0x"
952 + Integer.toHexString(resid) + " in package "
953 + packageName, e);
954 } catch (NameNotFoundException e) {
Alon Albert3fa51e32010-11-11 09:24:04 -0800955 Log.w("PackageManager", "Failure retrieving resources for "
Brad Fitzpatrick390dae12010-11-10 08:27:28 -0800956 + appInfo.packageName);
957 }
958 return null;
959 }
960
961 @Override
962 public CharSequence getApplicationLabel(ApplicationInfo info) {
963 return info.loadLabel(this);
964 }
965
966 @Override
967 public void installPackage(Uri packageURI, IPackageInstallObserver observer, int flags,
968 String installerPackageName) {
969 try {
970 mPM.installPackage(packageURI, observer, flags, installerPackageName);
971 } catch (RemoteException e) {
972 // Should never happen!
973 }
974 }
975
976 @Override
Kenny Root5ab21572011-07-27 11:11:19 -0700977 public void installPackageWithVerification(Uri packageURI, IPackageInstallObserver observer,
978 int flags, String installerPackageName, Uri verificationURI,
Rich Canningse1d7c712012-08-08 12:46:06 -0700979 ManifestDigest manifestDigest, ContainerEncryptionParams encryptionParams) {
Kenny Root5ab21572011-07-27 11:11:19 -0700980 try {
981 mPM.installPackageWithVerification(packageURI, observer, flags, installerPackageName,
Rich Canningse1d7c712012-08-08 12:46:06 -0700982 verificationURI, manifestDigest, encryptionParams);
Kenny Root5ab21572011-07-27 11:11:19 -0700983 } catch (RemoteException e) {
984 // Should never happen!
985 }
986 }
987
988 @Override
rich cannings706e8ba2012-08-20 13:20:14 -0700989 public void installPackageWithVerificationAndEncryption(Uri packageURI,
990 IPackageInstallObserver observer, int flags, String installerPackageName,
991 VerificationParams verificationParams, ContainerEncryptionParams encryptionParams) {
992 try {
993 mPM.installPackageWithVerificationAndEncryption(packageURI, observer, flags,
994 installerPackageName, verificationParams, encryptionParams);
995 } catch (RemoteException e) {
996 // Should never happen!
997 }
998 }
999
1000 @Override
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001001 public int installExistingPackage(String packageName)
1002 throws NameNotFoundException {
1003 try {
1004 int res = mPM.installExistingPackage(packageName);
1005 if (res == INSTALL_FAILED_INVALID_URI) {
1006 throw new NameNotFoundException("Package " + packageName + " doesn't exist");
1007 }
1008 return res;
1009 } catch (RemoteException e) {
1010 // Should never happen!
1011 throw new NameNotFoundException("Package " + packageName + " doesn't exist");
1012 }
1013 }
1014
1015 @Override
Kenny Root3a9b5fb2011-09-20 14:15:38 -07001016 public void verifyPendingInstall(int id, int response) {
Kenny Root5ab21572011-07-27 11:11:19 -07001017 try {
Kenny Root3a9b5fb2011-09-20 14:15:38 -07001018 mPM.verifyPendingInstall(id, response);
Kenny Root5ab21572011-07-27 11:11:19 -07001019 } catch (RemoteException e) {
1020 // Should never happen!
1021 }
1022 }
1023
1024 @Override
rich canningsd9ef3e52012-08-22 14:28:05 -07001025 public void extendVerificationTimeout(int id, int verificationCodeAtTimeout,
1026 long millisecondsToDelay) {
1027 try {
1028 mPM.extendVerificationTimeout(id, verificationCodeAtTimeout, millisecondsToDelay);
1029 } catch (RemoteException e) {
1030 // Should never happen!
1031 }
1032 }
1033
1034 @Override
Dianne Hackborn880119b2010-11-18 22:26:40 -08001035 public void setInstallerPackageName(String targetPackage,
1036 String installerPackageName) {
1037 try {
1038 mPM.setInstallerPackageName(targetPackage, installerPackageName);
1039 } catch (RemoteException e) {
1040 // Should never happen!
1041 }
1042 }
1043
1044 @Override
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001045 public void movePackage(String packageName, IPackageMoveObserver observer, int flags) {
1046 try {
1047 mPM.movePackage(packageName, observer, flags);
1048 } catch (RemoteException e) {
1049 // Should never happen!
1050 }
1051 }
1052
1053 @Override
1054 public String getInstallerPackageName(String packageName) {
1055 try {
1056 return mPM.getInstallerPackageName(packageName);
1057 } catch (RemoteException e) {
1058 // Should never happen!
1059 }
1060 return null;
1061 }
1062
1063 @Override
1064 public void deletePackage(String packageName, IPackageDeleteObserver observer, int flags) {
1065 try {
1066 mPM.deletePackage(packageName, observer, flags);
1067 } catch (RemoteException e) {
1068 // Should never happen!
1069 }
1070 }
1071 @Override
1072 public void clearApplicationUserData(String packageName,
1073 IPackageDataObserver observer) {
1074 try {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001075 mPM.clearApplicationUserData(packageName, observer, UserHandle.myUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001076 } catch (RemoteException e) {
1077 // Should never happen!
1078 }
1079 }
1080 @Override
1081 public void deleteApplicationCacheFiles(String packageName,
1082 IPackageDataObserver observer) {
1083 try {
1084 mPM.deleteApplicationCacheFiles(packageName, observer);
1085 } catch (RemoteException e) {
1086 // Should never happen!
1087 }
1088 }
1089 @Override
1090 public void freeStorageAndNotify(long idealStorageSize, IPackageDataObserver observer) {
1091 try {
1092 mPM.freeStorageAndNotify(idealStorageSize, observer);
1093 } catch (RemoteException e) {
1094 // Should never happen!
1095 }
1096 }
1097
1098 @Override
1099 public void freeStorage(long freeStorageSize, IntentSender pi) {
1100 try {
1101 mPM.freeStorage(freeStorageSize, pi);
1102 } catch (RemoteException e) {
1103 // Should never happen!
1104 }
1105 }
1106
1107 @Override
Dianne Hackborn0c380492012-08-20 17:23:30 -07001108 public void getPackageSizeInfo(String packageName, int userHandle,
1109 IPackageStatsObserver observer) {
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001110 try {
Dianne Hackborn0c380492012-08-20 17:23:30 -07001111 mPM.getPackageSizeInfo(packageName, userHandle, observer);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001112 } catch (RemoteException e) {
1113 // Should never happen!
1114 }
1115 }
1116 @Override
1117 public void addPackageToPreferred(String packageName) {
1118 try {
1119 mPM.addPackageToPreferred(packageName);
1120 } catch (RemoteException e) {
1121 // Should never happen!
1122 }
1123 }
1124
1125 @Override
1126 public void removePackageFromPreferred(String packageName) {
1127 try {
1128 mPM.removePackageFromPreferred(packageName);
1129 } catch (RemoteException e) {
1130 // Should never happen!
1131 }
1132 }
1133
1134 @Override
1135 public List<PackageInfo> getPreferredPackages(int flags) {
1136 try {
1137 return mPM.getPreferredPackages(flags);
1138 } catch (RemoteException e) {
1139 // Should never happen!
1140 }
1141 return new ArrayList<PackageInfo>();
1142 }
1143
1144 @Override
1145 public void addPreferredActivity(IntentFilter filter,
1146 int match, ComponentName[] set, ComponentName activity) {
1147 try {
Amith Yamasania3f133a2012-08-09 17:11:28 -07001148 mPM.addPreferredActivity(filter, match, set, activity, UserHandle.myUserId());
1149 } catch (RemoteException e) {
1150 // Should never happen!
1151 }
1152 }
1153
1154 @Override
1155 public void addPreferredActivity(IntentFilter filter, int match,
1156 ComponentName[] set, ComponentName activity, int userId) {
1157 try {
1158 mPM.addPreferredActivity(filter, match, set, activity, userId);
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001159 } catch (RemoteException e) {
1160 // Should never happen!
1161 }
1162 }
1163
1164 @Override
1165 public void replacePreferredActivity(IntentFilter filter,
1166 int match, ComponentName[] set, ComponentName activity) {
1167 try {
1168 mPM.replacePreferredActivity(filter, match, set, activity);
1169 } catch (RemoteException e) {
1170 // Should never happen!
1171 }
1172 }
1173
1174 @Override
1175 public void clearPackagePreferredActivities(String packageName) {
1176 try {
1177 mPM.clearPackagePreferredActivities(packageName);
1178 } catch (RemoteException e) {
1179 // Should never happen!
1180 }
1181 }
1182
1183 @Override
1184 public int getPreferredActivities(List<IntentFilter> outFilters,
1185 List<ComponentName> outActivities, String packageName) {
1186 try {
1187 return mPM.getPreferredActivities(outFilters, outActivities, packageName);
1188 } catch (RemoteException e) {
1189 // Should never happen!
1190 }
1191 return 0;
1192 }
1193
1194 @Override
1195 public void setComponentEnabledSetting(ComponentName componentName,
1196 int newState, int flags) {
1197 try {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001198 mPM.setComponentEnabledSetting(componentName, newState, flags, UserHandle.myUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001199 } catch (RemoteException e) {
1200 // Should never happen!
1201 }
1202 }
1203
1204 @Override
1205 public int getComponentEnabledSetting(ComponentName componentName) {
1206 try {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001207 return mPM.getComponentEnabledSetting(componentName, UserHandle.myUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001208 } catch (RemoteException e) {
1209 // Should never happen!
1210 }
1211 return PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
1212 }
1213
1214 @Override
1215 public void setApplicationEnabledSetting(String packageName,
1216 int newState, int flags) {
1217 try {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001218 mPM.setApplicationEnabledSetting(packageName, newState, flags, UserHandle.myUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001219 } catch (RemoteException e) {
1220 // Should never happen!
1221 }
1222 }
1223
1224 @Override
1225 public int getApplicationEnabledSetting(String packageName) {
1226 try {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001227 return mPM.getApplicationEnabledSetting(packageName, UserHandle.myUserId());
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001228 } catch (RemoteException e) {
1229 // Should never happen!
1230 }
1231 return PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
1232 }
1233
Kenny Root0aaa0d92011-09-12 16:42:55 -07001234 /**
1235 * @hide
1236 */
1237 @Override
1238 public VerifierDeviceIdentity getVerifierDeviceIdentity() {
1239 try {
1240 return mPM.getVerifierDeviceIdentity();
1241 } catch (RemoteException e) {
1242 // Should never happen!
1243 }
1244 return null;
1245 }
1246
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001247 private final ContextImpl mContext;
1248 private final IPackageManager mPM;
1249
1250 private static final Object sSync = new Object();
Romain Guy39fe17c2011-11-30 10:34:07 -08001251 private static HashMap<ResourceName, WeakReference<Drawable.ConstantState>> sIconCache
1252 = new HashMap<ResourceName, WeakReference<Drawable.ConstantState>>();
1253 private static HashMap<ResourceName, WeakReference<CharSequence>> sStringCache
1254 = new HashMap<ResourceName, WeakReference<CharSequence>>();
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08001255}