Pm.java revision c64322c35212e919906ffd66118c7d5d3ad36636
1/*
2 * Copyright (C) 2007 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 com.android.commands.pm;
18
19import android.content.ComponentName;
20import android.content.pm.ApplicationInfo;
21import android.content.pm.IPackageDeleteObserver;
22import android.content.pm.IPackageInstallObserver;
23import android.content.pm.IPackageManager;
24import android.content.pm.InstrumentationInfo;
25import android.content.pm.PackageInfo;
26import android.content.pm.PackageItemInfo;
27import android.content.pm.PackageManager;
28import android.content.pm.PermissionGroupInfo;
29import android.content.pm.PermissionInfo;
30import android.content.res.AssetManager;
31import android.content.res.Resources;
32import android.net.Uri;
33import android.os.RemoteException;
34import android.os.ServiceManager;
35
36import java.io.File;
37import java.util.ArrayList;
38import java.util.Collections;
39import java.util.Comparator;
40import java.util.List;
41import java.util.WeakHashMap;
42
43public final class Pm {
44    IPackageManager mPm;
45
46    private WeakHashMap<String, Resources> mResourceCache
47            = new WeakHashMap<String, Resources>();
48
49    private String[] mArgs;
50    private int mNextArg;
51    private String mCurArgData;
52
53    private static final String PM_NOT_RUNNING_ERR =
54        "Error: Could not access the Package Manager.  Is the system running?";
55
56    public static void main(String[] args) {
57        new Pm().run(args);
58    }
59
60    public void run(String[] args) {
61        boolean validCommand = false;
62        if (args.length < 1) {
63            showUsage();
64            return;
65        }
66
67        mPm = IPackageManager.Stub.asInterface(ServiceManager.getService("package"));
68        if (mPm == null) {
69            System.err.println(PM_NOT_RUNNING_ERR);
70            return;
71        }
72
73        mArgs = args;
74        String op = args[0];
75        mNextArg = 1;
76
77        if ("list".equals(op)) {
78            runList();
79            return;
80        }
81
82        if ("path".equals(op)) {
83            runPath();
84            return;
85        }
86
87        if ("install".equals(op)) {
88            runInstall();
89            return;
90        }
91
92        if ("uninstall".equals(op)) {
93            runUninstall();
94            return;
95        }
96
97        if ("enable".equals(op)) {
98            runSetEnabledSetting(PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
99            return;
100        }
101
102        if ("disable".equals(op)) {
103            runSetEnabledSetting(PackageManager.COMPONENT_ENABLED_STATE_DISABLED);
104            return;
105        }
106
107        try {
108            if (args.length == 1) {
109                if (args[0].equalsIgnoreCase("-l")) {
110                    validCommand = true;
111                    runListPackages(false);
112                } else if (args[0].equalsIgnoreCase("-lf")){
113                    validCommand = true;
114                    runListPackages(true);
115                }
116            } else if (args.length == 2) {
117                if (args[0].equalsIgnoreCase("-p")) {
118                    validCommand = true;
119                    displayPackageFilePath(args[1]);
120                }
121            }
122        } finally {
123            if (validCommand == false) {
124                if (op != null) {
125                    System.err.println("Error: unknown command '" + op + "'");
126                }
127                showUsage();
128            }
129        }
130    }
131
132    /**
133     * Execute the list sub-command.
134     *
135     * pm list [package | packages]
136     * pm list permission-groups
137     * pm list permissions
138     * pm list instrumentation
139     */
140    private void runList() {
141        String type = nextArg();
142        if (type == null) {
143            System.err.println("Error: didn't specify type of data to list");
144            showUsage();
145            return;
146        }
147        if ("package".equals(type) || "packages".equals(type)) {
148            runListPackages(false);
149        } else if ("permission-groups".equals(type)) {
150            runListPermissionGroups();
151        } else if ("permissions".equals(type)) {
152            runListPermissions();
153        } else if ("instrumentation".equals(type)) {
154            runListInstrumentation();
155        } else {
156            System.err.println("Error: unknown list type '" + type + "'");
157            showUsage();
158        }
159    }
160
161    /**
162     * Lists all the installed packages.
163     */
164    private void runListPackages(boolean showApplicationPackage) {
165        try {
166            String opt;
167            while ((opt=nextOption()) != null) {
168                if (opt.equals("-l")) {
169                    // old compat
170                } else if (opt.equals("-lf")) {
171                    showApplicationPackage = true;
172                } else if (opt.equals("-f")) {
173                    showApplicationPackage = true;
174                } else {
175                    System.err.println("Error: Unknown option: " + opt);
176                    showUsage();
177                    return;
178                }
179            }
180        } catch (RuntimeException ex) {
181            System.err.println("Error: " + ex.toString());
182            showUsage();
183            return;
184        }
185
186        try {
187            List<PackageInfo> packages = mPm.getInstalledPackages(0 /* all */);
188
189            int count = packages.size();
190            for (int p = 0 ; p < count ; p++) {
191                PackageInfo info = packages.get(p);
192                System.out.print("package:");
193                if (showApplicationPackage) {
194                    System.out.print(info.applicationInfo.sourceDir);
195                    System.out.print("=");
196                }
197                System.out.println(info.packageName);
198            }
199        } catch (RemoteException e) {
200            System.err.println(e.toString());
201            System.err.println(PM_NOT_RUNNING_ERR);
202        }
203    }
204
205    /**
206     * Lists all of the installed instrumentation, or all for a given package
207     *
208     * pm list instrumentation [package] [-f]
209     */
210    private void runListInstrumentation() {
211        int flags = 0;      // flags != 0 is only used to request meta-data
212        boolean showPackage = false;
213        String targetPackage = null;
214
215        try {
216            String opt;
217            while ((opt=nextArg()) != null) {
218                if (opt.equals("-f")) {
219                    showPackage = true;
220                } else if (opt.charAt(0) != '-') {
221                    targetPackage = opt;
222                } else {
223                    System.err.println("Error: Unknown option: " + opt);
224                    showUsage();
225                    return;
226                }
227            }
228        } catch (RuntimeException ex) {
229            System.err.println("Error: " + ex.toString());
230            showUsage();
231            return;
232        }
233
234        try {
235            List<InstrumentationInfo> list = mPm.queryInstrumentation(targetPackage, flags);
236
237            // Sort by target package
238            Collections.sort(list, new Comparator<InstrumentationInfo>() {
239                public int compare(InstrumentationInfo o1, InstrumentationInfo o2) {
240                    return o1.targetPackage.compareTo(o2.targetPackage);
241                }
242            });
243
244            int count = (list != null) ? list.size() : 0;
245            for (int p = 0; p < count; p++) {
246                InstrumentationInfo ii = list.get(p);
247                System.out.print("instrumentation:");
248                if (showPackage) {
249                    System.out.print(ii.sourceDir);
250                    System.out.print("=");
251                }
252                ComponentName cn = new ComponentName(ii.packageName, ii.name);
253                System.out.print(cn.flattenToShortString());
254                System.out.print(" (target=");
255                System.out.print(ii.targetPackage);
256                System.out.println(")");
257            }
258        } catch (RemoteException e) {
259            System.err.println(e.toString());
260            System.err.println(PM_NOT_RUNNING_ERR);
261        }
262    }
263
264    /**
265     * Lists all the known permission groups.
266     */
267    private void runListPermissionGroups() {
268        try {
269            List<PermissionGroupInfo> pgs = mPm.getAllPermissionGroups(0);
270
271            int count = pgs.size();
272            for (int p = 0 ; p < count ; p++) {
273                PermissionGroupInfo pgi = pgs.get(p);
274                System.out.print("permission group:");
275                System.out.println(pgi.name);
276            }
277        } catch (RemoteException e) {
278            System.err.println(e.toString());
279            System.err.println(PM_NOT_RUNNING_ERR);
280        }
281    }
282
283    private String loadText(PackageItemInfo pii, int res, CharSequence nonLocalized) {
284        if (nonLocalized != null) {
285            return nonLocalized.toString();
286        }
287        Resources r = getResources(pii);
288        if (r != null) {
289            return r.getString(res);
290        }
291        return null;
292    }
293
294    /**
295     * Lists all the permissions in a group.
296     */
297    private void runListPermissions() {
298        try {
299            boolean labels = false;
300            boolean groups = false;
301            boolean userOnly = false;
302            boolean summary = false;
303            boolean dangerousOnly = false;
304            String opt;
305            while ((opt=nextOption()) != null) {
306                if (opt.equals("-f")) {
307                    labels = true;
308                } else if (opt.equals("-g")) {
309                    groups = true;
310                } else if (opt.equals("-s")) {
311                    groups = true;
312                    labels = true;
313                    summary = true;
314                } else if (opt.equals("-u")) {
315                    userOnly = true;
316                } else if (opt.equals("-d")) {
317                    dangerousOnly = true;
318                } else {
319                    System.err.println("Error: Unknown option: " + opt);
320                    showUsage();
321                    return;
322                }
323            }
324
325            String grp = nextOption();
326            ArrayList<String> groupList = new ArrayList<String>();
327            if (groups) {
328                List<PermissionGroupInfo> infos =
329                        mPm.getAllPermissionGroups(0);
330                for (int i=0; i<infos.size(); i++) {
331                    groupList.add(infos.get(i).name);
332                }
333                groupList.add(null);
334            } else {
335                groupList.add(grp);
336            }
337
338            if (dangerousOnly) {
339                System.out.println("Dangerous Permissions:");
340                System.out.println("");
341                doListPermissions(groupList, groups, labels, summary,
342                        PermissionInfo.PROTECTION_DANGEROUS,
343                        PermissionInfo.PROTECTION_DANGEROUS);
344                if (userOnly) {
345                    System.out.println("Normal Permissions:");
346                    System.out.println("");
347                    doListPermissions(groupList, groups, labels, summary,
348                            PermissionInfo.PROTECTION_NORMAL,
349                            PermissionInfo.PROTECTION_NORMAL);
350                }
351            } else if (userOnly) {
352                System.out.println("Dangerous and Normal Permissions:");
353                System.out.println("");
354                doListPermissions(groupList, groups, labels, summary,
355                        PermissionInfo.PROTECTION_NORMAL,
356                        PermissionInfo.PROTECTION_DANGEROUS);
357            } else {
358                System.out.println("All Permissions:");
359                System.out.println("");
360                doListPermissions(groupList, groups, labels, summary,
361                        -10000, 10000);
362            }
363        } catch (RemoteException e) {
364            System.err.println(e.toString());
365            System.err.println(PM_NOT_RUNNING_ERR);
366        }
367    }
368
369    private void doListPermissions(ArrayList<String> groupList,
370            boolean groups, boolean labels, boolean summary,
371            int startProtectionLevel, int endProtectionLevel)
372            throws RemoteException {
373        for (int i=0; i<groupList.size(); i++) {
374            String groupName = groupList.get(i);
375            String prefix = "";
376            if (groups) {
377                if (i > 0) System.out.println("");
378                if (groupName != null) {
379                    PermissionGroupInfo pgi = mPm.getPermissionGroupInfo(
380                            groupName, 0);
381                    if (summary) {
382                        Resources res = getResources(pgi);
383                        if (res != null) {
384                            System.out.print(loadText(pgi, pgi.labelRes,
385                                    pgi.nonLocalizedLabel) + ": ");
386                        } else {
387                            System.out.print(pgi.name + ": ");
388
389                        }
390                    } else {
391                        System.out.println((labels ? "+ " : "")
392                                + "group:" + pgi.name);
393                        if (labels) {
394                            System.out.println("  package:" + pgi.packageName);
395                            Resources res = getResources(pgi);
396                            if (res != null) {
397                                System.out.println("  label:"
398                                        + loadText(pgi, pgi.labelRes,
399                                                pgi.nonLocalizedLabel));
400                                System.out.println("  description:"
401                                        + loadText(pgi, pgi.descriptionRes,
402                                                pgi.nonLocalizedDescription));
403                            }
404                        }
405                    }
406                } else {
407                    System.out.println(((labels && !summary)
408                            ? "+ " : "") + "ungrouped:");
409                }
410                prefix = "  ";
411            }
412            List<PermissionInfo> ps = mPm.queryPermissionsByGroup(
413                    groupList.get(i), 0);
414            int count = ps.size();
415            boolean first = true;
416            for (int p = 0 ; p < count ; p++) {
417                PermissionInfo pi = ps.get(p);
418                if (groups && groupName == null && pi.group != null) {
419                    continue;
420                }
421                if (pi.protectionLevel < startProtectionLevel
422                        || pi.protectionLevel > endProtectionLevel) {
423                    continue;
424                }
425                if (summary) {
426                    if (first) {
427                        first = false;
428                    } else {
429                        System.out.print(", ");
430                    }
431                    Resources res = getResources(pi);
432                    if (res != null) {
433                        System.out.print(loadText(pi, pi.labelRes,
434                                pi.nonLocalizedLabel));
435                    } else {
436                        System.out.print(pi.name);
437                    }
438                } else {
439                    System.out.println(prefix + (labels ? "+ " : "")
440                            + "permission:" + pi.name);
441                    if (labels) {
442                        System.out.println(prefix + "  package:" + pi.packageName);
443                        Resources res = getResources(pi);
444                        if (res != null) {
445                            System.out.println(prefix + "  label:"
446                                    + loadText(pi, pi.labelRes,
447                                            pi.nonLocalizedLabel));
448                            System.out.println(prefix + "  description:"
449                                    + loadText(pi, pi.descriptionRes,
450                                            pi.nonLocalizedDescription));
451                        }
452                        String protLevel = "unknown";
453                        switch(pi.protectionLevel) {
454                            case PermissionInfo.PROTECTION_DANGEROUS:
455                                protLevel = "dangerous";
456                                break;
457                            case PermissionInfo.PROTECTION_NORMAL:
458                                protLevel = "normal";
459                                break;
460                            case PermissionInfo.PROTECTION_SIGNATURE:
461                                protLevel = "signature";
462                                break;
463                            case PermissionInfo.PROTECTION_SIGNATURE_OR_SYSTEM:
464                                protLevel = "signatureOrSystem";
465                                break;
466                        }
467                        System.out.println(prefix + "  protectionLevel:" + protLevel);
468                    }
469                }
470            }
471
472            if (summary) {
473                System.out.println("");
474            }
475        }
476    }
477
478    private void runPath() {
479        String pkg = nextArg();
480        if (pkg == null) {
481            System.err.println("Error: no package specified");
482            showUsage();
483            return;
484        }
485        displayPackageFilePath(pkg);
486    }
487
488    class PackageInstallObserver extends IPackageInstallObserver.Stub {
489        boolean finished;
490        int result;
491
492        public void packageInstalled(String name, int status) {
493            synchronized( this) {
494                finished = true;
495                result = status;
496                notifyAll();
497            }
498        }
499    }
500
501    private String installFailureToString(int result) {
502        String s;
503        switch (result) {
504        case PackageManager.INSTALL_FAILED_ALREADY_EXISTS:
505            s = "INSTALL_FAILED_ALREADY_EXISTS";
506            break;
507        case PackageManager.INSTALL_FAILED_INVALID_APK:
508            s = "INSTALL_FAILED_INVALID_APK";
509            break;
510        case PackageManager.INSTALL_FAILED_INVALID_URI:
511            s = "INSTALL_FAILED_INVALID_URI";
512            break;
513        case PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE:
514            s = "INSTALL_FAILED_INSUFFICIENT_STORAGE";
515            break;
516        case PackageManager.INSTALL_FAILED_DUPLICATE_PACKAGE:
517            s = "INSTALL_FAILED_DUPLICATE_PACKAGE";
518            break;
519        case PackageManager.INSTALL_FAILED_NO_SHARED_USER:
520            s = "INSTALL_FAILED_NO_SHARED_USER";
521            break;
522        case PackageManager.INSTALL_FAILED_UPDATE_INCOMPATIBLE:
523            s = "INSTALL_FAILED_UPDATE_INCOMPATIBLE";
524            break;
525        case PackageManager.INSTALL_FAILED_SHARED_USER_INCOMPATIBLE:
526            s = "INSTALL_FAILED_SHARED_USER_INCOMPATIBLE";
527            break;
528        case PackageManager.INSTALL_FAILED_MISSING_SHARED_LIBRARY:
529            s = "INSTALL_FAILED_MISSING_SHARED_LIBRARY";
530            break;
531        case PackageManager.INSTALL_FAILED_DEXOPT:
532            s = "INSTALL_FAILED_DEXOPT";
533            break;
534        case PackageManager.INSTALL_FAILED_OLDER_SDK:
535            s = "INSTALL_FAILED_OLDER_SDK";
536            break;
537        case PackageManager.INSTALL_FAILED_CONFLICTING_PROVIDER:
538            s = "INSTALL_FAILED_CONFLICTING_PROVIDER";
539            break;
540        case PackageManager.INSTALL_FAILED_NEWER_SDK:
541            s = "INSTALL_FAILED_NEWER_SDK";
542            break;
543        case PackageManager.INSTALL_PARSE_FAILED_NOT_APK:
544            s = "INSTALL_PARSE_FAILED_NOT_APK";
545            break;
546        case PackageManager.INSTALL_PARSE_FAILED_BAD_MANIFEST:
547            s = "INSTALL_PARSE_FAILED_BAD_MANIFEST";
548            break;
549        case PackageManager.INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION:
550            s = "INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION";
551            break;
552        case PackageManager.INSTALL_PARSE_FAILED_NO_CERTIFICATES:
553            s = "INSTALL_PARSE_FAILED_NO_CERTIFICATES";
554            break;
555        case PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES:
556            s = "INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES";
557            break;
558        case PackageManager.INSTALL_PARSE_FAILED_CERTIFICATE_ENCODING:
559            s = "INSTALL_PARSE_FAILED_CERTIFICATE_ENCODING";
560            break;
561        case PackageManager.INSTALL_PARSE_FAILED_BAD_PACKAGE_NAME:
562            s = "INSTALL_PARSE_FAILED_BAD_PACKAGE_NAME";
563            break;
564        case PackageManager.INSTALL_PARSE_FAILED_BAD_SHARED_USER_ID:
565            s = "INSTALL_PARSE_FAILED_BAD_SHARED_USER_ID";
566            break;
567        case PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED:
568            s = "INSTALL_PARSE_FAILED_MANIFEST_MALFORMED";
569            break;
570        case PackageManager.INSTALL_PARSE_FAILED_MANIFEST_EMPTY:
571            s = "INSTALL_PARSE_FAILED_MANIFEST_EMPTY";
572            break;
573        default:
574            s = Integer.toString(result);
575        break;
576        }
577        return s;
578    }
579
580    private void runInstall() {
581        int installFlags = 0;
582        String installerPackageName = null;
583
584        String opt;
585        while ((opt=nextOption()) != null) {
586            if (opt.equals("-l")) {
587                installFlags |= PackageManager.FORWARD_LOCK_PACKAGE;
588            } else if (opt.equals("-r")) {
589                installFlags |= PackageManager.REPLACE_EXISTING_PACKAGE;
590            } else if (opt.equals("-i")) {
591                installerPackageName = nextOptionData();
592                if (installerPackageName == null) {
593                    System.err.println("Error: no value specified for -i");
594                    showUsage();
595                    return;
596                }
597            } else {
598                System.err.println("Error: Unknown option: " + opt);
599                showUsage();
600                return;
601            }
602        }
603
604        String apkFilePath = nextArg();
605        System.err.println("\tpkg: " + apkFilePath);
606        if (apkFilePath == null) {
607            System.err.println("Error: no package specified");
608            showUsage();
609            return;
610        }
611
612        PackageInstallObserver obs = new PackageInstallObserver();
613        try {
614            mPm.installPackage(Uri.fromFile(new File(apkFilePath)), obs, installFlags,
615                    installerPackageName);
616
617            synchronized (obs) {
618                while (!obs.finished) {
619                    try {
620                        obs.wait();
621                    } catch (InterruptedException e) {
622                    }
623                }
624                if (obs.result == PackageManager.INSTALL_SUCCEEDED) {
625                    System.out.println("Success");
626                } else {
627                    System.err.println("Failure ["
628                            + installFailureToString(obs.result)
629                            + "]");
630                }
631            }
632        } catch (RemoteException e) {
633            System.err.println(e.toString());
634            System.err.println(PM_NOT_RUNNING_ERR);
635        }
636    }
637
638    class PackageDeleteObserver extends IPackageDeleteObserver.Stub {
639        boolean finished;
640        boolean result;
641
642        public void packageDeleted(boolean succeeded) {
643            synchronized (this) {
644                finished = true;
645                result = succeeded;
646                notifyAll();
647            }
648        }
649    }
650
651    private void runUninstall() {
652        int unInstallFlags = 0;
653
654        String opt = nextOption();
655        if (opt != null && opt.equals("-k")) {
656            unInstallFlags = PackageManager.DONT_DELETE_DATA;
657        }
658
659        String pkg = nextArg();
660        if (pkg == null) {
661            System.err.println("Error: no package specified");
662            showUsage();
663            return;
664        }
665        boolean result = deletePackage(pkg, unInstallFlags);
666        if (result) {
667            System.out.println("Success");
668        } else {
669            System.out.println("Failure");
670        }
671    }
672
673    private boolean deletePackage(String pkg, int unInstallFlags) {
674        PackageDeleteObserver obs = new PackageDeleteObserver();
675        try {
676            mPm.deletePackage(pkg, obs, unInstallFlags);
677
678            synchronized (obs) {
679                while (!obs.finished) {
680                    try {
681                        obs.wait();
682                    } catch (InterruptedException e) {
683                    }
684                }
685            }
686        } catch (RemoteException e) {
687            System.err.println(e.toString());
688            System.err.println(PM_NOT_RUNNING_ERR);
689        }
690        return obs.result;
691    }
692
693    private static String enabledSettingToString(int state) {
694        switch (state) {
695            case PackageManager.COMPONENT_ENABLED_STATE_DEFAULT:
696                return "default";
697            case PackageManager.COMPONENT_ENABLED_STATE_ENABLED:
698                return "enabled";
699            case PackageManager.COMPONENT_ENABLED_STATE_DISABLED:
700                return "disabled";
701        }
702        return "unknown";
703    }
704
705    private void runSetEnabledSetting(int state) {
706        String pkg = nextArg();
707        if (pkg == null) {
708            System.err.println("Error: no package or component specified");
709            showUsage();
710            return;
711        }
712        ComponentName cn = ComponentName.unflattenFromString(pkg);
713        if (cn == null) {
714            try {
715                mPm.setApplicationEnabledSetting(pkg, state, 0);
716                System.err.println("Package " + pkg + " new state: "
717                        + enabledSettingToString(
718                                mPm.getApplicationEnabledSetting(pkg)));
719            } catch (RemoteException e) {
720                System.err.println(e.toString());
721                System.err.println(PM_NOT_RUNNING_ERR);
722            }
723        } else {
724            try {
725                mPm.setComponentEnabledSetting(cn, state, 0);
726                System.err.println("Component " + cn.toShortString() + " new state: "
727                        + enabledSettingToString(
728                                mPm.getComponentEnabledSetting(cn)));
729            } catch (RemoteException e) {
730                System.err.println(e.toString());
731                System.err.println(PM_NOT_RUNNING_ERR);
732            }
733        }
734    }
735
736    /**
737     * Displays the package file for a package.
738     * @param pckg
739     */
740    private void displayPackageFilePath(String pckg) {
741        try {
742            PackageInfo info = mPm.getPackageInfo(pckg, 0);
743            if (info != null && info.applicationInfo != null) {
744                System.out.print("package:");
745                System.out.println(info.applicationInfo.sourceDir);
746            }
747        } catch (RemoteException e) {
748            System.err.println(e.toString());
749            System.err.println(PM_NOT_RUNNING_ERR);
750        }
751    }
752
753    private Resources getResources(PackageItemInfo pii) {
754        Resources res = mResourceCache.get(pii.packageName);
755        if (res != null) return res;
756
757        try {
758            ApplicationInfo ai = mPm.getApplicationInfo(pii.packageName, 0);
759            AssetManager am = new AssetManager();
760            am.addAssetPath(ai.publicSourceDir);
761            res = new Resources(am, null, null);
762            mResourceCache.put(pii.packageName, res);
763            return res;
764        } catch (RemoteException e) {
765            System.err.println(e.toString());
766            System.err.println(PM_NOT_RUNNING_ERR);
767            return null;
768        }
769    }
770
771    private String nextOption() {
772        if (mNextArg >= mArgs.length) {
773            return null;
774        }
775        String arg = mArgs[mNextArg];
776        if (!arg.startsWith("-")) {
777            return null;
778        }
779        mNextArg++;
780        if (arg.equals("--")) {
781            return null;
782        }
783        if (arg.length() > 1 && arg.charAt(1) != '-') {
784            if (arg.length() > 2) {
785                mCurArgData = arg.substring(2);
786                return arg.substring(0, 2);
787            } else {
788                mCurArgData = null;
789                return arg;
790            }
791        }
792        mCurArgData = null;
793        return arg;
794    }
795
796    private String nextOptionData() {
797        if (mCurArgData != null) {
798            return mCurArgData;
799        }
800        if (mNextArg >= mArgs.length) {
801            return null;
802        }
803        String data = mArgs[mNextArg];
804        mNextArg++;
805        return data;
806    }
807
808    private String nextArg() {
809        if (mNextArg >= mArgs.length) {
810            return null;
811        }
812        String arg = mArgs[mNextArg];
813        mNextArg++;
814        return arg;
815    }
816
817    private static void showUsage() {
818        System.err.println("usage: pm [list|path|install|uninstall]");
819        System.err.println("       pm list packages [-f]");
820        System.err.println("       pm list permission-groups");
821        System.err.println("       pm list permissions [-g] [-f] [-d] [-u] [GROUP]");
822        System.err.println("       pm list instrumentation [-f] [TARGET-PACKAGE]");
823        System.err.println("       pm path PACKAGE");
824        System.err.println("       pm install [-l] [-r] [-i INSTALLER_PACKAGE_NAME] PATH");
825        System.err.println("       pm uninstall [-k] PACKAGE");
826        System.err.println("       pm enable PACKAGE_OR_COMPONENT");
827        System.err.println("       pm disable PACKAGE_OR_COMPONENT");
828        System.err.println("");
829        System.err.println("The list packages command prints all packages.  Use");
830        System.err.println("the -f option to see their associated file.");
831        System.err.println("");
832        System.err.println("The list permission-groups command prints all known");
833        System.err.println("permission groups.");
834        System.err.println("");
835        System.err.println("The list permissions command prints all known");
836        System.err.println("permissions, optionally only those in GROUP.  Use");
837        System.err.println("the -g option to organize by group.  Use");
838        System.err.println("the -f option to print all information.  Use");
839        System.err.println("the -s option for a short summary.  Use");
840        System.err.println("the -d option to only list dangerous permissions.  Use");
841        System.err.println("the -u option to list only the permissions users will see.");
842        System.err.println("");
843        System.err.println("The list instrumentation command prints all instrumentations,");
844        System.err.println("or only those that target a specified package.  Use the -f option");
845        System.err.println("to see their associated file.");
846        System.err.println("");
847        System.err.println("The path command prints the path to the .apk of a package.");
848        System.err.println("");
849        System.err.println("The install command installs a package to the system.  Use");
850        System.err.println("the -l option to install the package with FORWARD_LOCK. Use");
851        System.err.println("the -r option to reinstall an exisiting app, keeping its data.");
852        System.err.println("the -i option to specify the installer package name.");
853        System.err.println("");
854        System.err.println("The uninstall command removes a package from the system. Use");
855        System.err.println("the -k option to keep the data and cache directories around");
856        System.err.println("after the package removal.");
857        System.err.println("");
858        System.err.println("The enable and disable commands change the enabled state of");
859        System.err.println("a given package or component (written as \"package/class\").");
860    }
861}
862