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