Pm.java revision 37d83a3caf138e35fa1a355787d20c351f6b37a6
19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Copyright (C) 2007 The Android Open Source Project
39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * you may not use this file except in compliance with the License.
69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * You may obtain a copy of the License at
79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * See the License for the specific language governing permissions and
149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * limitations under the License.
159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpackage com.android.commands.pm;
189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
19ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Cowardimport com.android.internal.content.PackageHelper;
20ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
21690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackbornimport android.app.ActivityManagerNative;
229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.ComponentName;
239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.pm.ApplicationInfo;
24ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Cowardimport android.content.pm.ContainerEncryptionParams;
25039c68e75606e837cf021815a0210836724574adDianne Hackbornimport android.content.pm.FeatureInfo;
26690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackbornimport android.content.pm.IPackageDataObserver;
279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.pm.IPackageDeleteObserver;
289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.pm.IPackageInstallObserver;
299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.pm.IPackageManager;
309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.pm.InstrumentationInfo;
319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.pm.PackageInfo;
329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.pm.PackageItemInfo;
339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.pm.PackageManager;
340e2c0f37d98bb5539b0fe41865aaf1add0ff1bb3Kenny Rootimport android.content.pm.ParceledListSlice;
359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.pm.PermissionGroupInfo;
369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.pm.PermissionInfo;
37742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasaniimport android.content.pm.UserInfo;
38706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich canningsimport android.content.pm.VerificationParams;
399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.res.AssetManager;
409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.res.Resources;
419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.net.Uri;
42258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasaniimport android.os.IUserManager;
439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.os.RemoteException;
449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.os.ServiceManager;
45151ec4c9f8ca34625733730a4551a23eb97cda23Amith Yamasaniimport android.os.UserHandle;
469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.io.File;
4823b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohetimport java.lang.reflect.Field;
4923b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohetimport java.lang.reflect.Modifier;
50ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Cowardimport java.security.InvalidAlgorithmParameterException;
519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.util.ArrayList;
529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.util.Collections;
539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.util.Comparator;
549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.util.List;
559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.util.WeakHashMap;
569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
57ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Cowardimport javax.crypto.SecretKey;
58ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Cowardimport javax.crypto.spec.IvParameterSpec;
59ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Cowardimport javax.crypto.spec.SecretKeySpec;
60ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpublic final class Pm {
629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    IPackageManager mPm;
63258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani    IUserManager mUm;
6423b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private WeakHashMap<String, Resources> mResourceCache
669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            = new WeakHashMap<String, Resources>();
6723b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private String[] mArgs;
699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private int mNextArg;
709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private String mCurArgData;
7123b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
7223b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet    private static final String PM_NOT_RUNNING_ERR =
739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        "Error: Could not access the Package Manager.  Is the system running?";
7423b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static void main(String[] args) {
769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        new Pm().run(args);
779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
7823b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void run(String[] args) {
809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        boolean validCommand = false;
819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (args.length < 1) {
829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            showUsage();
839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return;
849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
86258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani        mUm = IUserManager.Stub.asInterface(ServiceManager.getService("user"));
879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mPm = IPackageManager.Stub.asInterface(ServiceManager.getService("package"));
889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (mPm == null) {
899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.err.println(PM_NOT_RUNNING_ERR);
909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return;
919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mArgs = args;
949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        String op = args[0];
959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mNextArg = 1;
9623b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if ("list".equals(op)) {
989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            runList();
999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return;
1009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
10123b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
1029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if ("path".equals(op)) {
1039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            runPath();
1049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return;
1059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
10623b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
1079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if ("install".equals(op)) {
1089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            runInstall();
1099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return;
1109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
11123b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
1129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if ("uninstall".equals(op)) {
1139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            runUninstall();
1149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return;
1159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
11623b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
117690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn        if ("clear".equals(op)) {
118690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn            runClear();
119690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn            return;
120690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn        }
121690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn
1229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if ("enable".equals(op)) {
1239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            runSetEnabledSetting(PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
1249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return;
1259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
12623b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
1279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if ("disable".equals(op)) {
1289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            runSetEnabledSetting(PackageManager.COMPONENT_ENABLED_STATE_DISABLED);
1299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return;
1309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
13123b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
1320ac3031c159a0904e73eb4439cdc724d8df4a6e6Dianne Hackborn        if ("disable-user".equals(op)) {
1330ac3031c159a0904e73eb4439cdc724d8df4a6e6Dianne Hackborn            runSetEnabledSetting(PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER);
1340ac3031c159a0904e73eb4439cdc724d8df4a6e6Dianne Hackborn            return;
1350ac3031c159a0904e73eb4439cdc724d8df4a6e6Dianne Hackborn        }
1360ac3031c159a0904e73eb4439cdc724d8df4a6e6Dianne Hackborn
137e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn        if ("grant".equals(op)) {
138e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn            runGrantRevokePermission(true);
139e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn            return;
140e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn        }
141e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn
142e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn        if ("revoke".equals(op)) {
143e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn            runGrantRevokePermission(false);
144e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn            return;
145e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn        }
146e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn
147719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey        if ("set-permission-enforced".equals(op)) {
148719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey            runSetPermissionEnforced();
149719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey            return;
150719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey        }
151719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey
15246d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        if ("set-install-location".equals(op)) {
15340e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu            runSetInstallLocation();
15440e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu            return;
15540e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu        }
15640e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu
15746d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        if ("get-install-location".equals(op)) {
15840e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu            runGetInstallLocation();
15940e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu            return;
16040e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu        }
16140e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu
162197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        if ("trim-caches".equals(op)) {
163197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            runTrimCaches();
164197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            return;
165197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        }
166197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn
167483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani        if ("create-user".equals(op)) {
168483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani            runCreateUser();
169742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani            return;
170742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani        }
171742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani
172483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani        if ("remove-user".equals(op)) {
173483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani            runRemoveUser();
1740b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani            return;
1750b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani        }
1760b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani
1779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
1789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (args.length == 1) {
1799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (args[0].equalsIgnoreCase("-l")) {
1809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    validCommand = true;
1819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    runListPackages(false);
1829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                } else if (args[0].equalsIgnoreCase("-lf")){
1839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    validCommand = true;
1849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    runListPackages(true);
1859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
1869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            } else if (args.length == 2) {
1879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (args[0].equalsIgnoreCase("-p")) {
1889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    validCommand = true;
1899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    displayPackageFilePath(args[1]);
1909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
1919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
1929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } finally {
1939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (validCommand == false) {
1949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (op != null) {
1959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    System.err.println("Error: unknown command '" + op + "'");
1969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
1979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                showUsage();
1989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
1999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
2009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
20123b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
2029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
2039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Execute the list sub-command.
20423b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet     *
2059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * pm list [package | packages]
2069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * pm list permission-groups
2079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * pm list permissions
208039c68e75606e837cf021815a0210836724574adDianne Hackborn     * pm list features
209e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn     * pm list libraries
2109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * pm list instrumentation
2119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
2129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private void runList() {
2139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        String type = nextArg();
2149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (type == null) {
2159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.err.println("Error: didn't specify type of data to list");
2169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return;
2179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
2189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if ("package".equals(type) || "packages".equals(type)) {
2199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            runListPackages(false);
2209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else if ("permission-groups".equals(type)) {
2219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            runListPermissionGroups();
2229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else if ("permissions".equals(type)) {
2239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            runListPermissions();
224039c68e75606e837cf021815a0210836724574adDianne Hackborn        } else if ("features".equals(type)) {
225039c68e75606e837cf021815a0210836724574adDianne Hackborn            runListFeatures();
226e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn        } else if ("libraries".equals(type)) {
227e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn            runListLibraries();
2289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else if ("instrumentation".equals(type)) {
2299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            runListInstrumentation();
230483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani        } else if ("users".equals(type)) {
231483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani            runListUsers();
2329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else {
2339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.err.println("Error: unknown list type '" + type + "'");
2349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
2359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
23623b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
2379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
2389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Lists all the installed packages.
2399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
2409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private void runListPackages(boolean showApplicationPackage) {
241e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn        int getFlags = 0;
242e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn        boolean listDisabled = false, listEnabled = false;
2430f1de9adde0b52d2a385a76232bd7ac30c3eeea2Dianne Hackborn        boolean listSystem = false, listThirdParty = false;
24492a208c6d3e0fdae54bbc942e7e041e48812bcaaAshish Sharma        boolean listInstaller = false;
245151ec4c9f8ca34625733730a4551a23eb97cda23Amith Yamasani        int userId = UserHandle.USER_OWNER;
2469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
2479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            String opt;
2489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            while ((opt=nextOption()) != null) {
2499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (opt.equals("-l")) {
2509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    // old compat
2519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                } else if (opt.equals("-lf")) {
2529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    showApplicationPackage = true;
2539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                } else if (opt.equals("-f")) {
2549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    showApplicationPackage = true;
255e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn                } else if (opt.equals("-d")) {
256e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn                    listDisabled = true;
257e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn                } else if (opt.equals("-e")) {
258e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn                    listEnabled = true;
2590f1de9adde0b52d2a385a76232bd7ac30c3eeea2Dianne Hackborn                } else if (opt.equals("-s")) {
2600f1de9adde0b52d2a385a76232bd7ac30c3eeea2Dianne Hackborn                    listSystem = true;
2610f1de9adde0b52d2a385a76232bd7ac30c3eeea2Dianne Hackborn                } else if (opt.equals("-3")) {
2620f1de9adde0b52d2a385a76232bd7ac30c3eeea2Dianne Hackborn                    listThirdParty = true;
26392a208c6d3e0fdae54bbc942e7e041e48812bcaaAshish Sharma                } else if (opt.equals("-i")) {
26492a208c6d3e0fdae54bbc942e7e041e48812bcaaAshish Sharma                    listInstaller = true;
265151ec4c9f8ca34625733730a4551a23eb97cda23Amith Yamasani                } else if (opt.equals("--user")) {
266151ec4c9f8ca34625733730a4551a23eb97cda23Amith Yamasani                    userId = Integer.parseInt(nextArg());
267e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn                } else if (opt.equals("-u")) {
268e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn                    getFlags |= PackageManager.GET_UNINSTALLED_PACKAGES;
2699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                } else {
2709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    System.err.println("Error: Unknown option: " + opt);
2719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    return;
2729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
2739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
2749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } catch (RuntimeException ex) {
2759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.err.println("Error: " + ex.toString());
2769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return;
2779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
27823b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
279e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn        String filter = nextArg();
280e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn
2819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
282151ec4c9f8ca34625733730a4551a23eb97cda23Amith Yamasani            final List<PackageInfo> packages = getInstalledPackages(mPm, getFlags, userId);
28323b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
2849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            int count = packages.size();
2859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            for (int p = 0 ; p < count ; p++) {
2869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                PackageInfo info = packages.get(p);
287e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn                if (filter != null && !info.packageName.contains(filter)) {
288e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn                    continue;
289e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn                }
2900f1de9adde0b52d2a385a76232bd7ac30c3eeea2Dianne Hackborn                final boolean isSystem =
2910f1de9adde0b52d2a385a76232bd7ac30c3eeea2Dianne Hackborn                        (info.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM) != 0;
292e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn                if ((!listDisabled || !info.applicationInfo.enabled) &&
2930f1de9adde0b52d2a385a76232bd7ac30c3eeea2Dianne Hackborn                        (!listEnabled || info.applicationInfo.enabled) &&
2940f1de9adde0b52d2a385a76232bd7ac30c3eeea2Dianne Hackborn                        (!listSystem || isSystem) &&
2950f1de9adde0b52d2a385a76232bd7ac30c3eeea2Dianne Hackborn                        (!listThirdParty || !isSystem)) {
296e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn                    System.out.print("package:");
297e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn                    if (showApplicationPackage) {
298e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn                        System.out.print(info.applicationInfo.sourceDir);
299e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn                        System.out.print("=");
300e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn                    }
30192a208c6d3e0fdae54bbc942e7e041e48812bcaaAshish Sharma                    System.out.print(info.packageName);
30292a208c6d3e0fdae54bbc942e7e041e48812bcaaAshish Sharma                    if (listInstaller) {
30392a208c6d3e0fdae54bbc942e7e041e48812bcaaAshish Sharma                        System.out.print("  installer=");
30492a208c6d3e0fdae54bbc942e7e041e48812bcaaAshish Sharma                        System.out.print(mPm.getInstallerPackageName(info.packageName));
30592a208c6d3e0fdae54bbc942e7e041e48812bcaaAshish Sharma                    }
30692a208c6d3e0fdae54bbc942e7e041e48812bcaaAshish Sharma                    System.out.println();
3079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
3089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
3099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } catch (RemoteException e) {
3109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.err.println(e.toString());
3119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.err.println(PM_NOT_RUNNING_ERR);
3129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
3139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
31423b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
3150e2c0f37d98bb5539b0fe41865aaf1add0ff1bb3Kenny Root    @SuppressWarnings("unchecked")
316151ec4c9f8ca34625733730a4551a23eb97cda23Amith Yamasani    private List<PackageInfo> getInstalledPackages(IPackageManager pm, int flags, int userId)
3170e2c0f37d98bb5539b0fe41865aaf1add0ff1bb3Kenny Root            throws RemoteException {
3180e2c0f37d98bb5539b0fe41865aaf1add0ff1bb3Kenny Root        final List<PackageInfo> packageInfos = new ArrayList<PackageInfo>();
3190e2c0f37d98bb5539b0fe41865aaf1add0ff1bb3Kenny Root        PackageInfo lastItem = null;
3200e2c0f37d98bb5539b0fe41865aaf1add0ff1bb3Kenny Root        ParceledListSlice<PackageInfo> slice;
3210e2c0f37d98bb5539b0fe41865aaf1add0ff1bb3Kenny Root
3220e2c0f37d98bb5539b0fe41865aaf1add0ff1bb3Kenny Root        do {
3230e2c0f37d98bb5539b0fe41865aaf1add0ff1bb3Kenny Root            final String lastKey = lastItem != null ? lastItem.packageName : null;
324151ec4c9f8ca34625733730a4551a23eb97cda23Amith Yamasani            slice = pm.getInstalledPackages(flags, lastKey, userId);
3250e2c0f37d98bb5539b0fe41865aaf1add0ff1bb3Kenny Root            lastItem = slice.populateList(packageInfos, PackageInfo.CREATOR);
3260e2c0f37d98bb5539b0fe41865aaf1add0ff1bb3Kenny Root        } while (!slice.isLastSlice());
3270e2c0f37d98bb5539b0fe41865aaf1add0ff1bb3Kenny Root
3280e2c0f37d98bb5539b0fe41865aaf1add0ff1bb3Kenny Root        return packageInfos;
3290e2c0f37d98bb5539b0fe41865aaf1add0ff1bb3Kenny Root    }
3300e2c0f37d98bb5539b0fe41865aaf1add0ff1bb3Kenny Root
3319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
332039c68e75606e837cf021815a0210836724574adDianne Hackborn     * Lists all of the features supported by the current device.
333039c68e75606e837cf021815a0210836724574adDianne Hackborn     *
334039c68e75606e837cf021815a0210836724574adDianne Hackborn     * pm list features
335039c68e75606e837cf021815a0210836724574adDianne Hackborn     */
336039c68e75606e837cf021815a0210836724574adDianne Hackborn    private void runListFeatures() {
337039c68e75606e837cf021815a0210836724574adDianne Hackborn        try {
338039c68e75606e837cf021815a0210836724574adDianne Hackborn            List<FeatureInfo> list = new ArrayList<FeatureInfo>();
339039c68e75606e837cf021815a0210836724574adDianne Hackborn            FeatureInfo[] rawList = mPm.getSystemAvailableFeatures();
340039c68e75606e837cf021815a0210836724574adDianne Hackborn            for (int i=0; i<rawList.length; i++) {
341039c68e75606e837cf021815a0210836724574adDianne Hackborn                list.add(rawList[i]);
342039c68e75606e837cf021815a0210836724574adDianne Hackborn            }
343babe28e01c6e845e41f8a7d23ab0b2a2c93a2e6bJoe Malin
344039c68e75606e837cf021815a0210836724574adDianne Hackborn
345039c68e75606e837cf021815a0210836724574adDianne Hackborn            // Sort by name
346039c68e75606e837cf021815a0210836724574adDianne Hackborn            Collections.sort(list, new Comparator<FeatureInfo>() {
347039c68e75606e837cf021815a0210836724574adDianne Hackborn                public int compare(FeatureInfo o1, FeatureInfo o2) {
348039c68e75606e837cf021815a0210836724574adDianne Hackborn                    if (o1.name == o2.name) return 0;
349039c68e75606e837cf021815a0210836724574adDianne Hackborn                    if (o1.name == null) return -1;
350039c68e75606e837cf021815a0210836724574adDianne Hackborn                    if (o2.name == null) return 1;
351039c68e75606e837cf021815a0210836724574adDianne Hackborn                    return o1.name.compareTo(o2.name);
352039c68e75606e837cf021815a0210836724574adDianne Hackborn                }
353039c68e75606e837cf021815a0210836724574adDianne Hackborn            });
354039c68e75606e837cf021815a0210836724574adDianne Hackborn
355039c68e75606e837cf021815a0210836724574adDianne Hackborn            int count = (list != null) ? list.size() : 0;
356039c68e75606e837cf021815a0210836724574adDianne Hackborn            for (int p = 0; p < count; p++) {
357039c68e75606e837cf021815a0210836724574adDianne Hackborn                FeatureInfo fi = list.get(p);
358039c68e75606e837cf021815a0210836724574adDianne Hackborn                System.out.print("feature:");
359039c68e75606e837cf021815a0210836724574adDianne Hackborn                if (fi.name != null) System.out.println(fi.name);
360039c68e75606e837cf021815a0210836724574adDianne Hackborn                else System.out.println("reqGlEsVersion=0x"
361039c68e75606e837cf021815a0210836724574adDianne Hackborn                        + Integer.toHexString(fi.reqGlEsVersion));
362039c68e75606e837cf021815a0210836724574adDianne Hackborn            }
363039c68e75606e837cf021815a0210836724574adDianne Hackborn        } catch (RemoteException e) {
364039c68e75606e837cf021815a0210836724574adDianne Hackborn            System.err.println(e.toString());
365039c68e75606e837cf021815a0210836724574adDianne Hackborn            System.err.println(PM_NOT_RUNNING_ERR);
366039c68e75606e837cf021815a0210836724574adDianne Hackborn        }
367039c68e75606e837cf021815a0210836724574adDianne Hackborn    }
368039c68e75606e837cf021815a0210836724574adDianne Hackborn
369039c68e75606e837cf021815a0210836724574adDianne Hackborn    /**
370e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn     * Lists all of the libraries supported by the current device.
371e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn     *
372e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn     * pm list libraries
373e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn     */
374e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn    private void runListLibraries() {
375e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn        try {
376e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn            List<String> list = new ArrayList<String>();
377e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn            String[] rawList = mPm.getSystemSharedLibraryNames();
378e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn            for (int i=0; i<rawList.length; i++) {
379e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn                list.add(rawList[i]);
380e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn            }
381e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn
382e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn
383e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn            // Sort by name
384e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn            Collections.sort(list, new Comparator<String>() {
385e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn                public int compare(String o1, String o2) {
386e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn                    if (o1 == o2) return 0;
387e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn                    if (o1 == null) return -1;
388e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn                    if (o2 == null) return 1;
389e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn                    return o1.compareTo(o2);
390e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn                }
391e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn            });
392e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn
393e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn            int count = (list != null) ? list.size() : 0;
394e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn            for (int p = 0; p < count; p++) {
395e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn                String lib = list.get(p);
396e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn                System.out.print("library:");
397e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn                System.out.println(lib);
398e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn            }
399e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn        } catch (RemoteException e) {
400e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn            System.err.println(e.toString());
401e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn            System.err.println(PM_NOT_RUNNING_ERR);
402e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn        }
403e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn    }
404e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn
405e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn    /**
4069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Lists all of the installed instrumentation, or all for a given package
40723b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet     *
4089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * pm list instrumentation [package] [-f]
4099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
4109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private void runListInstrumentation() {
4119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int flags = 0;      // flags != 0 is only used to request meta-data
4129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        boolean showPackage = false;
4139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        String targetPackage = null;
4149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
4169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            String opt;
4179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            while ((opt=nextArg()) != null) {
4189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (opt.equals("-f")) {
4199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    showPackage = true;
4209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                } else if (opt.charAt(0) != '-') {
4219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    targetPackage = opt;
4229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                } else {
4239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    System.err.println("Error: Unknown option: " + opt);
4249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    return;
4259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
4269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
4279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } catch (RuntimeException ex) {
4289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.err.println("Error: " + ex.toString());
4299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return;
4309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
4339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            List<InstrumentationInfo> list = mPm.queryInstrumentation(targetPackage, flags);
4349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            // Sort by target package
4369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            Collections.sort(list, new Comparator<InstrumentationInfo>() {
4379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                public int compare(InstrumentationInfo o1, InstrumentationInfo o2) {
4389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    return o1.targetPackage.compareTo(o2.targetPackage);
4399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
4409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            });
4419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            int count = (list != null) ? list.size() : 0;
4439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            for (int p = 0; p < count; p++) {
4449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                InstrumentationInfo ii = list.get(p);
4459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                System.out.print("instrumentation:");
4469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (showPackage) {
4479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    System.out.print(ii.sourceDir);
4489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    System.out.print("=");
4499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
4509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                ComponentName cn = new ComponentName(ii.packageName, ii.name);
4519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                System.out.print(cn.flattenToShortString());
4529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                System.out.print(" (target=");
4539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                System.out.print(ii.targetPackage);
4549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                System.out.println(")");
4559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
4569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } catch (RemoteException e) {
4579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.err.println(e.toString());
4589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.err.println(PM_NOT_RUNNING_ERR);
4599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
46123b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
4629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
4639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Lists all the known permission groups.
4649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
4659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private void runListPermissionGroups() {
4669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
4679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            List<PermissionGroupInfo> pgs = mPm.getAllPermissionGroups(0);
46823b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
4699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            int count = pgs.size();
4709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            for (int p = 0 ; p < count ; p++) {
4719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                PermissionGroupInfo pgi = pgs.get(p);
4729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                System.out.print("permission group:");
4739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                System.out.println(pgi.name);
4749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
4759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } catch (RemoteException e) {
4769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.err.println(e.toString());
4779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.err.println(PM_NOT_RUNNING_ERR);
4789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
48023b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
4819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private String loadText(PackageItemInfo pii, int res, CharSequence nonLocalized) {
4829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (nonLocalized != null) {
4839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return nonLocalized.toString();
4849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4851c3274ca12a6931568693b897c269107f0346eabAli Utku Selen        if (res != 0) {
4861c3274ca12a6931568693b897c269107f0346eabAli Utku Selen            Resources r = getResources(pii);
4871c3274ca12a6931568693b897c269107f0346eabAli Utku Selen            if (r != null) {
4881c3274ca12a6931568693b897c269107f0346eabAli Utku Selen                return r.getString(res);
4891c3274ca12a6931568693b897c269107f0346eabAli Utku Selen            }
4909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return null;
4929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
49323b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
4949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
4959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Lists all the permissions in a group.
4969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
4979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private void runListPermissions() {
4989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
4999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            boolean labels = false;
5009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            boolean groups = false;
5019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            boolean userOnly = false;
5029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            boolean summary = false;
5039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            boolean dangerousOnly = false;
5049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            String opt;
5059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            while ((opt=nextOption()) != null) {
5069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (opt.equals("-f")) {
5079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    labels = true;
5089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                } else if (opt.equals("-g")) {
5099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    groups = true;
5109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                } else if (opt.equals("-s")) {
5119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    groups = true;
5129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    labels = true;
5139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    summary = true;
5149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                } else if (opt.equals("-u")) {
5159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    userOnly = true;
5169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                } else if (opt.equals("-d")) {
5179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    dangerousOnly = true;
5189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                } else {
5199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    System.err.println("Error: Unknown option: " + opt);
5209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    return;
5219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
5229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
52323b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
5249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            String grp = nextOption();
5259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            ArrayList<String> groupList = new ArrayList<String>();
5269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (groups) {
5279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                List<PermissionGroupInfo> infos =
5289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        mPm.getAllPermissionGroups(0);
5299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                for (int i=0; i<infos.size(); i++) {
5309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    groupList.add(infos.get(i).name);
5319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
5329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                groupList.add(null);
5339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            } else {
5349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                groupList.add(grp);
5359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
53623b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
5379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (dangerousOnly) {
5389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                System.out.println("Dangerous Permissions:");
5399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                System.out.println("");
5409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                doListPermissions(groupList, groups, labels, summary,
5419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        PermissionInfo.PROTECTION_DANGEROUS,
5429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        PermissionInfo.PROTECTION_DANGEROUS);
5439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (userOnly) {
5449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    System.out.println("Normal Permissions:");
5459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    System.out.println("");
5469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    doListPermissions(groupList, groups, labels, summary,
5479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            PermissionInfo.PROTECTION_NORMAL,
5489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            PermissionInfo.PROTECTION_NORMAL);
5499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
5509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            } else if (userOnly) {
5519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                System.out.println("Dangerous and Normal Permissions:");
5529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                System.out.println("");
5539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                doListPermissions(groupList, groups, labels, summary,
5549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        PermissionInfo.PROTECTION_NORMAL,
5559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        PermissionInfo.PROTECTION_DANGEROUS);
5569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            } else {
5579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                System.out.println("All Permissions:");
5589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                System.out.println("");
5599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                doListPermissions(groupList, groups, labels, summary,
5609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        -10000, 10000);
5619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
5629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } catch (RemoteException e) {
5639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.err.println(e.toString());
5649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.err.println(PM_NOT_RUNNING_ERR);
5659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
5669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
56723b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
5689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private void doListPermissions(ArrayList<String> groupList,
5699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            boolean groups, boolean labels, boolean summary,
5709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            int startProtectionLevel, int endProtectionLevel)
5719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throws RemoteException {
5729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        for (int i=0; i<groupList.size(); i++) {
5739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            String groupName = groupList.get(i);
5749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            String prefix = "";
5759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (groups) {
5769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (i > 0) System.out.println("");
5779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (groupName != null) {
5789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    PermissionGroupInfo pgi = mPm.getPermissionGroupInfo(
5799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            groupName, 0);
5809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    if (summary) {
5819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        Resources res = getResources(pgi);
5829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        if (res != null) {
5839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            System.out.print(loadText(pgi, pgi.labelRes,
5849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                    pgi.nonLocalizedLabel) + ": ");
5859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        } else {
5869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            System.out.print(pgi.name + ": ");
58723b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
5889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        }
5899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    } else {
5909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        System.out.println((labels ? "+ " : "")
5919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                + "group:" + pgi.name);
5929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        if (labels) {
5939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            System.out.println("  package:" + pgi.packageName);
5949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            Resources res = getResources(pgi);
5959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            if (res != null) {
5969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                System.out.println("  label:"
5979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                        + loadText(pgi, pgi.labelRes,
5989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                                pgi.nonLocalizedLabel));
5999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                System.out.println("  description:"
6009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                        + loadText(pgi, pgi.descriptionRes,
6019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                                pgi.nonLocalizedDescription));
6029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            }
6039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        }
6049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    }
6059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                } else {
6069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    System.out.println(((labels && !summary)
6079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            ? "+ " : "") + "ungrouped:");
6089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
6099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                prefix = "  ";
6109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
6119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            List<PermissionInfo> ps = mPm.queryPermissionsByGroup(
6129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    groupList.get(i), 0);
6139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            int count = ps.size();
6149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            boolean first = true;
6159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            for (int p = 0 ; p < count ; p++) {
6169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                PermissionInfo pi = ps.get(p);
6179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (groups && groupName == null && pi.group != null) {
6189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    continue;
6199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
620e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn                final int base = pi.protectionLevel & PermissionInfo.PROTECTION_MASK_BASE;
621e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn                if (base < startProtectionLevel
622e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn                        || base > endProtectionLevel) {
6239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    continue;
6249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
6259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (summary) {
6269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    if (first) {
6279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        first = false;
6289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    } else {
6299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        System.out.print(", ");
6309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    }
6319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    Resources res = getResources(pi);
6329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    if (res != null) {
6339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        System.out.print(loadText(pi, pi.labelRes,
6349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                pi.nonLocalizedLabel));
6359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    } else {
6369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        System.out.print(pi.name);
6379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    }
6389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                } else {
6399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    System.out.println(prefix + (labels ? "+ " : "")
6409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            + "permission:" + pi.name);
6419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    if (labels) {
6429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        System.out.println(prefix + "  package:" + pi.packageName);
6439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        Resources res = getResources(pi);
6449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        if (res != null) {
6459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            System.out.println(prefix + "  label:"
6469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                    + loadText(pi, pi.labelRes,
6479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                            pi.nonLocalizedLabel));
6489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            System.out.println(prefix + "  description:"
6499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                    + loadText(pi, pi.descriptionRes,
6509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                            pi.nonLocalizedDescription));
6519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        }
652e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn                        System.out.println(prefix + "  protectionLevel:"
653e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn                                + PermissionInfo.protectionToString(pi.protectionLevel));
6549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    }
6559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
6569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
65723b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
6589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (summary) {
6599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                System.out.println("");
6609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
6619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
6629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
66323b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
6649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private void runPath() {
6659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        String pkg = nextArg();
6669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (pkg == null) {
6679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.err.println("Error: no package specified");
6689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return;
6699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
6709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        displayPackageFilePath(pkg);
6719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
67223b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
6739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    class PackageInstallObserver extends IPackageInstallObserver.Stub {
6749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        boolean finished;
6759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int result;
6769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        public void packageInstalled(String name, int status) {
6789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            synchronized( this) {
6799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                finished = true;
6809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                result = status;
6819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                notifyAll();
6829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
6839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
6849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
68523b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
68623b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet    /**
68723b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet     * Converts a failure code into a string by using reflection to find a matching constant
68823b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet     * in PackageManager.
68923b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet     */
6909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private String installFailureToString(int result) {
69123b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet        Field[] fields = PackageManager.class.getFields();
69223b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet        for (Field f: fields) {
69323b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet            if (f.getType() == int.class) {
69423b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet                int modifiers = f.getModifiers();
69523b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet                // only look at public final static fields.
69623b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet                if (((modifiers & Modifier.FINAL) != 0) &&
69723b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet                        ((modifiers & Modifier.PUBLIC) != 0) &&
69823b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet                        ((modifiers & Modifier.STATIC) != 0)) {
69923b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet                    String fieldName = f.getName();
70023b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet                    if (fieldName.startsWith("INSTALL_FAILED_") ||
70123b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet                            fieldName.startsWith("INSTALL_PARSE_FAILED_")) {
70223b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet                        // get the int value and compare it to result.
70323b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet                        try {
70423b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet                            if (result == f.getInt(null)) {
70523b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet                                return fieldName;
70623b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet                            }
70723b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet                        } catch (IllegalAccessException e) {
70823b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet                            // this shouldn't happen since we only look for public static fields.
70923b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet                        }
71023b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet                    }
71123b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet                }
71223b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet            }
71323b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet        }
71423b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
71523b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet        // couldn't find a matching constant? return the value
71623b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet        return Integer.toString(result);
7179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
71823b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
71940e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu    private void runSetInstallLocation() {
72040e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu        int loc;
72140e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu
72240e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu        String arg = nextArg();
72340e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu        if (arg == null) {
72440e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu            System.err.println("Error: no install location specified.");
72540e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu            return;
72640e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu        }
72740e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu        try {
72840e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu            loc = Integer.parseInt(arg);
72940e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu        } catch (NumberFormatException e) {
73040e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu            System.err.println("Error: install location has to be a number.");
73140e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu            return;
73240e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu        }
73340e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu        try {
73440e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu            if (!mPm.setInstallLocation(loc)) {
73540e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu                System.err.println("Error: install location has to be a number.");
73640e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu            }
73740e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu        } catch (RemoteException e) {
73840e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu            System.err.println(e.toString());
73940e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu            System.err.println(PM_NOT_RUNNING_ERR);
74040e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu        }
74140e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu    }
74240e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu
74340e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu    private void runGetInstallLocation() {
74440e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu        try {
74540e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu            int loc = mPm.getInstallLocation();
74640e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu            String locStr = "invalid";
74740e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu            if (loc == PackageHelper.APP_INSTALL_AUTO) {
74840e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu                locStr = "auto";
74940e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu            } else if (loc == PackageHelper.APP_INSTALL_INTERNAL) {
75040e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu                locStr = "internal";
75140e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu            } else if (loc == PackageHelper.APP_INSTALL_EXTERNAL) {
75240e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu                locStr = "external";
75340e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu            }
75440e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu            System.out.println(loc + "[" + locStr + "]");
75540e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu        } catch (RemoteException e) {
75640e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu            System.err.println(e.toString());
75740e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu            System.err.println(PM_NOT_RUNNING_ERR);
75840e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu        }
75940e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu    }
76040e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu
7619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private void runInstall() {
7627767eac3232ba2fb9828766813cdb481d6a97584Dianne Hackborn        int installFlags = PackageManager.INSTALL_ALL_USERS;
763c64322c35212e919906ffd66118c7d5d3ad36636Jacek Surazski        String installerPackageName = null;
7649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        String opt;
766ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
767ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        String algo = null;
768ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        byte[] iv = null;
769ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        byte[] key = null;
770ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
771ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        String macAlgo = null;
772ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        byte[] macKey = null;
773ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        byte[] tag = null;
774706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings        String originatingUriString = null;
775706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings        String referrer = null;
776ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
7779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        while ((opt=nextOption()) != null) {
7789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (opt.equals("-l")) {
779ade3ecad94d1f4431576f53bae26c35efbf7a2c9Dianne Hackborn                installFlags |= PackageManager.INSTALL_FORWARD_LOCK;
7809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            } else if (opt.equals("-r")) {
781ade3ecad94d1f4431576f53bae26c35efbf7a2c9Dianne Hackborn                installFlags |= PackageManager.INSTALL_REPLACE_EXISTING;
782c64322c35212e919906ffd66118c7d5d3ad36636Jacek Surazski            } else if (opt.equals("-i")) {
783c64322c35212e919906ffd66118c7d5d3ad36636Jacek Surazski                installerPackageName = nextOptionData();
784c64322c35212e919906ffd66118c7d5d3ad36636Jacek Surazski                if (installerPackageName == null) {
785c64322c35212e919906ffd66118c7d5d3ad36636Jacek Surazski                    System.err.println("Error: no value specified for -i");
786c64322c35212e919906ffd66118c7d5d3ad36636Jacek Surazski                    return;
787c64322c35212e919906ffd66118c7d5d3ad36636Jacek Surazski                }
788ade3ecad94d1f4431576f53bae26c35efbf7a2c9Dianne Hackborn            } else if (opt.equals("-t")) {
789ade3ecad94d1f4431576f53bae26c35efbf7a2c9Dianne Hackborn                installFlags |= PackageManager.INSTALL_ALLOW_TEST;
790af8e9f4805643f90a9dc0ecfa119e0a860c12f8aSuchi Amalapurapu            } else if (opt.equals("-s")) {
7918946dd3355fc1dcbad872c0546e356474d4cc5deSuchi Amalapurapu                // Override if -s option is specified.
7925b993ce7bc29e43a3215a50ce6ce5d6550d4e5e2Suchi Amalapurapu                installFlags |= PackageManager.INSTALL_EXTERNAL;
79314b6abda1309631d49d4bebbb0317a7e1dfc0a50Suchi Amalapurapu            } else if (opt.equals("-f")) {
79414b6abda1309631d49d4bebbb0317a7e1dfc0a50Suchi Amalapurapu                // Override if -s option is specified.
79514b6abda1309631d49d4bebbb0317a7e1dfc0a50Suchi Amalapurapu                installFlags |= PackageManager.INSTALL_INTERNAL;
7967767eac3232ba2fb9828766813cdb481d6a97584Dianne Hackborn            } else if (opt.equals("-d")) {
7977767eac3232ba2fb9828766813cdb481d6a97584Dianne Hackborn                installFlags |= PackageManager.INSTALL_ALLOW_DOWNGRADE;
798ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            } else if (opt.equals("--algo")) {
799ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                algo = nextOptionData();
800ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                if (algo == null) {
801ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                    System.err.println("Error: must supply argument for --algo");
802ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                    return;
803ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                }
804ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            } else if (opt.equals("--iv")) {
805ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                iv = hexToBytes(nextOptionData());
806ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                if (iv == null) {
807ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                    System.err.println("Error: must supply argument for --iv");
808ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                    return;
809ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                }
810ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            } else if (opt.equals("--key")) {
811ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                key = hexToBytes(nextOptionData());
812ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                if (key == null) {
813ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                    System.err.println("Error: must supply argument for --key");
814ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                    return;
815ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                }
816ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            } else if (opt.equals("--macalgo")) {
817ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                macAlgo = nextOptionData();
818ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                if (macAlgo == null) {
819ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                    System.err.println("Error: must supply argument for --macalgo");
820ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                    return;
821ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                }
822ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            } else if (opt.equals("--mackey")) {
823ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                macKey = hexToBytes(nextOptionData());
824ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                if (macKey == null) {
825ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                    System.err.println("Error: must supply argument for --mackey");
826ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                    return;
827ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                }
828ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            } else if (opt.equals("--tag")) {
829ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                tag = hexToBytes(nextOptionData());
830ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                if (tag == null) {
831ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                    System.err.println("Error: must supply argument for --tag");
832ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                    return;
833ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                }
834706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings            } else if (opt.equals("--originating-uri")) {
835706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings                originatingUriString = nextOptionData();
836706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings                if (originatingUriString == null) {
837706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings                    System.err.println("Error: must supply argument for --originating-uri");
838706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings                    return;
839706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings                }
840706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings            } else if (opt.equals("--referrer")) {
841706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings                referrer = nextOptionData();
842706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings                if (referrer == null) {
843706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings                    System.err.println("Error: must supply argument for --referrer");
844706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings                    return;
845706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings                }
8469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            } else {
8479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                System.err.println("Error: Unknown option: " + opt);
8489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                return;
8499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
8509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
8519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
852ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        final ContainerEncryptionParams encryptionParams;
853ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        if (algo != null || iv != null || key != null || macAlgo != null || macKey != null
854ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                || tag != null) {
855ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            if (algo == null || iv == null || key == null) {
856ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                System.err.println("Error: all of --algo, --iv, and --key must be specified");
857ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                return;
858ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            }
859ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
860ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            if (macAlgo != null || macKey != null || tag != null) {
861ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                if (macAlgo == null || macKey == null || tag == null) {
862ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                    System.err.println("Error: all of --macalgo, --mackey, and --tag must "
863ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                            + "be specified");
864ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                    return;
865ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                }
866ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            }
867ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
868ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            try {
869ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                final SecretKey encKey = new SecretKeySpec(key, "RAW");
870ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
871ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                final SecretKey macSecretKey;
872ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                if (macKey == null || macKey.length == 0) {
873ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                    macSecretKey = null;
874ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                } else {
875ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                    macSecretKey = new SecretKeySpec(macKey, "RAW");
876ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                }
877ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
878ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                encryptionParams = new ContainerEncryptionParams(algo, new IvParameterSpec(iv),
879ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                        encKey, macAlgo, null, macSecretKey, tag, -1, -1, -1);
880ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            } catch (InvalidAlgorithmParameterException e) {
881ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                e.printStackTrace();
882ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                return;
883ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            }
884ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        } else {
885ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            encryptionParams = null;
886ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        }
887ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
8885ab2157bf1f105b02d3e2913cd3a33f9765b74caKenny Root        final Uri apkURI;
8895ab2157bf1f105b02d3e2913cd3a33f9765b74caKenny Root        final Uri verificationURI;
890706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings        final Uri originatingURI;
891706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings        final Uri referrerURI;
892706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings
893706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings        if (originatingUriString != null) {
894706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings            originatingURI = Uri.parse(originatingUriString);
895706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings        } else {
896706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings            originatingURI = null;
897706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings        }
898706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings
899706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings        if (referrer != null) {
900706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings            referrerURI = Uri.parse(referrer);
901706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings        } else {
902706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings            referrerURI = null;
903706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings        }
9045ab2157bf1f105b02d3e2913cd3a33f9765b74caKenny Root
9055ab2157bf1f105b02d3e2913cd3a33f9765b74caKenny Root        // Populate apkURI, must be present
9065ab2157bf1f105b02d3e2913cd3a33f9765b74caKenny Root        final String apkFilePath = nextArg();
9079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        System.err.println("\tpkg: " + apkFilePath);
9085ab2157bf1f105b02d3e2913cd3a33f9765b74caKenny Root        if (apkFilePath != null) {
9095ab2157bf1f105b02d3e2913cd3a33f9765b74caKenny Root            apkURI = Uri.fromFile(new File(apkFilePath));
9105ab2157bf1f105b02d3e2913cd3a33f9765b74caKenny Root        } else {
9119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.err.println("Error: no package specified");
9129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return;
9139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
9149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9155ab2157bf1f105b02d3e2913cd3a33f9765b74caKenny Root        // Populate verificationURI, optionally present
9165ab2157bf1f105b02d3e2913cd3a33f9765b74caKenny Root        final String verificationFilePath = nextArg();
9175ab2157bf1f105b02d3e2913cd3a33f9765b74caKenny Root        if (verificationFilePath != null) {
9185ab2157bf1f105b02d3e2913cd3a33f9765b74caKenny Root            System.err.println("\tver: " + verificationFilePath);
9195ab2157bf1f105b02d3e2913cd3a33f9765b74caKenny Root            verificationURI = Uri.fromFile(new File(verificationFilePath));
9205ab2157bf1f105b02d3e2913cd3a33f9765b74caKenny Root        } else {
9215ab2157bf1f105b02d3e2913cd3a33f9765b74caKenny Root            verificationURI = null;
9225ab2157bf1f105b02d3e2913cd3a33f9765b74caKenny Root        }
9235ab2157bf1f105b02d3e2913cd3a33f9765b74caKenny Root
9249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        PackageInstallObserver obs = new PackageInstallObserver();
9259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
926706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings            VerificationParams verificationParams = new VerificationParams(verificationURI,
92737d83a3caf138e35fa1a355787d20c351f6b37a6Ben Gruver                    originatingURI, referrerURI, VerificationParams.NO_UID, null);
928706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings
929706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings            mPm.installPackageWithVerificationAndEncryption(apkURI, obs, installFlags,
930706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings                    installerPackageName, verificationParams, encryptionParams);
93123b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
9329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            synchronized (obs) {
9339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                while (!obs.finished) {
9349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    try {
9359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        obs.wait();
9369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    } catch (InterruptedException e) {
9379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    }
9389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
9399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (obs.result == PackageManager.INSTALL_SUCCEEDED) {
9409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    System.out.println("Success");
9419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                } else {
9429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    System.err.println("Failure ["
9439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            + installFailureToString(obs.result)
9449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            + "]");
9459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
9469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
9479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } catch (RemoteException e) {
9489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.err.println(e.toString());
9499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.err.println(PM_NOT_RUNNING_ERR);
9509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
9519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
95223b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
953ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    /**
954ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward     * Convert a string containing hex-encoded bytes to a byte array.
955ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward     *
956ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward     * @param input String containing hex-encoded bytes
957ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward     * @return input as an array of bytes
958ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward     */
959ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    private byte[] hexToBytes(String input) {
960ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        if (input == null) {
961ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            return null;
962ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        }
963ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
964ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        final int inputLength = input.length();
965ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        if ((inputLength % 2) != 0) {
966ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            System.err.print("Invalid length; must be multiple of 2");
967ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            return null;
968ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        }
969ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
970ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        final int byteLength = inputLength / 2;
971ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        final byte[] output = new byte[byteLength];
972ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
973ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        int inputIndex = 0;
974ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        int byteIndex = 0;
975ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        while (inputIndex < inputLength) {
976ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            output[byteIndex++] = (byte) Integer.parseInt(
977ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                    input.substring(inputIndex, inputIndex + 2), 16);
978ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            inputIndex += 2;
979ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        }
980ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
981ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        return output;
982ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    }
983ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
984483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani    public void runCreateUser() {
9850b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani        String name;
9860b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani        String arg = nextArg();
9870b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani        if (arg == null) {
9880b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani            System.err.println("Error: no user name specified.");
9890b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani            return;
9900b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani        }
9910b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani        name = arg;
9920b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani        try {
993258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani            if (mUm.createUser(name, 0) == null) {
994483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani                System.err.println("Error: couldn't create User.");
9950b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani            }
9960b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani        } catch (RemoteException e) {
9970b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani            System.err.println(e.toString());
9980b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani            System.err.println(PM_NOT_RUNNING_ERR);
9990b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani        }
10000b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani
10010b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani    }
10020b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani
1003483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani    public void runRemoveUser() {
10040b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani        int userId;
10050b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani        String arg = nextArg();
10060b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani        if (arg == null) {
10070b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani            System.err.println("Error: no user id specified.");
10080b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani            return;
10090b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani        }
10100b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani        try {
10110b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani            userId = Integer.parseInt(arg);
10120b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani        } catch (NumberFormatException e) {
1013786b44046a79d6c4c9cd07f5989d491c7196ad80Dianne Hackborn            System.err.println("Error: user id '" + arg + "' is not a number.");
10140b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani            return;
10150b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani        }
10160b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani        try {
1017258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani            if (!mUm.removeUser(userId)) {
1018786b44046a79d6c4c9cd07f5989d491c7196ad80Dianne Hackborn                System.err.println("Error: couldn't remove user #" + userId + ".");
10190b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani            }
10200b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani        } catch (RemoteException e) {
10210b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani            System.err.println(e.toString());
10220b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani            System.err.println(PM_NOT_RUNNING_ERR);
10230b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani        }
10240b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani    }
10250b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani
1026483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani    public void runListUsers() {
1027742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani        try {
1028920ace0bbc2d4133dbec991d2636c99a57d6245eAmith Yamasani            List<UserInfo> users = mUm.getUsers(false);
1029742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani            if (users == null) {
1030742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani                System.err.println("Error: couldn't get users");
1031742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani            } else {
1032742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani                System.out.println("Users:");
1033742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani                for (int i = 0; i < users.size(); i++) {
1034742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani                    System.out.println("\t" + users.get(i).toString());
1035742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani                }
1036742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani            }
1037742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani        } catch (RemoteException e) {
1038742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani            System.err.println(e.toString());
1039742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani            System.err.println(PM_NOT_RUNNING_ERR);
1040742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani        }
1041742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani    }
10429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    class PackageDeleteObserver extends IPackageDeleteObserver.Stub {
10439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        boolean finished;
10449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        boolean result;
104523b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
1046c39bb4aaa7be1b9b0cc9fb0b1f03d54f7609ffebKenny Root        public void packageDeleted(String packageName, int returnCode) {
10479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            synchronized (this) {
10489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                finished = true;
1049c39bb4aaa7be1b9b0cc9fb0b1f03d54f7609ffebKenny Root                result = returnCode == PackageManager.DELETE_SUCCEEDED;
10509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                notifyAll();
10519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
10529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
10539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
105423b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
10559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private void runUninstall() {
10565e03e2ca7d25b899b129baad2dd5eca6bf99d88aDianne Hackborn        int unInstallFlags = PackageManager.DELETE_ALL_USERS;
10579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
10585e03e2ca7d25b899b129baad2dd5eca6bf99d88aDianne Hackborn        String opt;
10595e03e2ca7d25b899b129baad2dd5eca6bf99d88aDianne Hackborn        while ((opt=nextOption()) != null) {
10605e03e2ca7d25b899b129baad2dd5eca6bf99d88aDianne Hackborn            if (opt.equals("-k")) {
10615e03e2ca7d25b899b129baad2dd5eca6bf99d88aDianne Hackborn                unInstallFlags |= PackageManager.DELETE_KEEP_DATA;
10625e03e2ca7d25b899b129baad2dd5eca6bf99d88aDianne Hackborn            } else {
10635e03e2ca7d25b899b129baad2dd5eca6bf99d88aDianne Hackborn                System.err.println("Error: Unknown option: " + opt);
10645e03e2ca7d25b899b129baad2dd5eca6bf99d88aDianne Hackborn                return;
10655e03e2ca7d25b899b129baad2dd5eca6bf99d88aDianne Hackborn            }
10669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
10679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
10689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        String pkg = nextArg();
10699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (pkg == null) {
10709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.err.println("Error: no package specified");
10719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            showUsage();
10729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return;
10739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
10749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        boolean result = deletePackage(pkg, unInstallFlags);
10759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (result) {
10769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.out.println("Success");
10779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else {
10789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.out.println("Failure");
10799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
10809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
10819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
10829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private boolean deletePackage(String pkg, int unInstallFlags) {
10839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        PackageDeleteObserver obs = new PackageDeleteObserver();
10849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
10859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mPm.deletePackage(pkg, obs, unInstallFlags);
10869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
10879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            synchronized (obs) {
10889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                while (!obs.finished) {
10899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    try {
10909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        obs.wait();
10919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    } catch (InterruptedException e) {
10929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    }
10939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
10949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
10959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } catch (RemoteException e) {
10969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.err.println(e.toString());
10979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.err.println(PM_NOT_RUNNING_ERR);
10989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
10999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return obs.result;
11009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
11019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1102197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn    static class ClearDataObserver extends IPackageDataObserver.Stub {
1103690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn        boolean finished;
1104690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn        boolean result;
1105690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn
1106690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn        @Override
1107690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn        public void onRemoveCompleted(String packageName, boolean succeeded) throws RemoteException {
1108690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn            synchronized (this) {
1109690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn                finished = true;
1110690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn                result = succeeded;
1111690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn                notifyAll();
1112690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn            }
1113690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn        }
1114690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn
1115690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn    }
1116690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn
1117690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn    private void runClear() {
1118690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn        String pkg = nextArg();
1119690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn        if (pkg == null) {
1120690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn            System.err.println("Error: no package specified");
1121690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn            showUsage();
1122690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn            return;
1123690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn        }
1124690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn
1125690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn        ClearDataObserver obs = new ClearDataObserver();
1126690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn        try {
11274120375d46091df8527bb701882e056fbb0e6b06Dianne Hackborn            // XXX TO DO: add user arg
11284120375d46091df8527bb701882e056fbb0e6b06Dianne Hackborn            if (!ActivityManagerNative.getDefault().clearApplicationUserData(pkg, obs, 0)) {
1129690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn                System.err.println("Failed");
1130690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn            }
1131690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn
1132690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn            synchronized (obs) {
1133690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn                while (!obs.finished) {
1134690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn                    try {
1135690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn                        obs.wait();
1136690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn                    } catch (InterruptedException e) {
1137690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn                    }
1138690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn                }
1139690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn            }
1140690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn
1141690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn            if (obs.result) {
1142690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn                System.err.println("Success");
1143690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn            } else {
1144690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn                System.err.println("Failed");
1145690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn            }
1146690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn        } catch (RemoteException e) {
1147690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn            System.err.println(e.toString());
1148690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn            System.err.println(PM_NOT_RUNNING_ERR);
1149690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn        }
1150690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn    }
1151690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn
11529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static String enabledSettingToString(int state) {
11539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        switch (state) {
11549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            case PackageManager.COMPONENT_ENABLED_STATE_DEFAULT:
11559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                return "default";
11569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            case PackageManager.COMPONENT_ENABLED_STATE_ENABLED:
11579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                return "enabled";
11589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            case PackageManager.COMPONENT_ENABLED_STATE_DISABLED:
11599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                return "disabled";
11600ac3031c159a0904e73eb4439cdc724d8df4a6e6Dianne Hackborn            case PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER:
11610ac3031c159a0904e73eb4439cdc724d8df4a6e6Dianne Hackborn                return "disabled-user";
11629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
11639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return "unknown";
11649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
116523b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
1166483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani    private boolean isNumber(String s) {
1167483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani        try {
1168483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani            Integer.parseInt(s);
1169483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani        } catch (NumberFormatException nfe) {
1170483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani            return false;
1171483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani        }
1172483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani        return true;
1173483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani    }
1174483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani
11759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private void runSetEnabledSetting(int state) {
1176483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani        int userId = 0;
1177483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani        String option = nextOption();
1178483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani        if (option != null && option.equals("--user")) {
1179483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani            String optionData = nextOptionData();
1180483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani            if (optionData == null || !isNumber(optionData)) {
1181483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani                System.err.println("Error: no USER_ID specified");
1182483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani                showUsage();
1183483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani                return;
1184483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani            } else {
1185483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani                userId = Integer.parseInt(optionData);
1186483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani            }
1187483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani        }
1188483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani
11899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        String pkg = nextArg();
11909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (pkg == null) {
11919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.err.println("Error: no package or component specified");
11929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            showUsage();
11939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return;
11949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
11959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        ComponentName cn = ComponentName.unflattenFromString(pkg);
11969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (cn == null) {
11979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            try {
1198483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani                mPm.setApplicationEnabledSetting(pkg, state, 0, userId);
11999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                System.err.println("Package " + pkg + " new state: "
12009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        + enabledSettingToString(
1201483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani                        mPm.getApplicationEnabledSetting(pkg, userId)));
12029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            } catch (RemoteException e) {
12039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                System.err.println(e.toString());
12049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                System.err.println(PM_NOT_RUNNING_ERR);
12059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
12069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else {
12079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            try {
1208483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani                mPm.setComponentEnabledSetting(cn, state, 0, userId);
12099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                System.err.println("Component " + cn.toShortString() + " new state: "
12109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        + enabledSettingToString(
1211483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani                        mPm.getComponentEnabledSetting(cn, userId)));
12129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            } catch (RemoteException e) {
12139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                System.err.println(e.toString());
12149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                System.err.println(PM_NOT_RUNNING_ERR);
12159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
12169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
12179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
12189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1219e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn    private void runGrantRevokePermission(boolean grant) {
1220e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn        String pkg = nextArg();
1221e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn        if (pkg == null) {
1222e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn            System.err.println("Error: no package specified");
1223e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn            showUsage();
1224e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn            return;
1225e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn        }
1226e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn        String perm = nextArg();
1227e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn        if (perm == null) {
1228e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn            System.err.println("Error: no permission specified");
1229e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn            showUsage();
1230e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn            return;
1231e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn        }
1232e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn        try {
1233e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn            if (grant) {
1234e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn                mPm.grantPermission(pkg, perm);
1235e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn            } else {
1236e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn                mPm.revokePermission(pkg, perm);
1237e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn            }
1238e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn        } catch (RemoteException e) {
1239e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn            System.err.println(e.toString());
1240e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn            System.err.println(PM_NOT_RUNNING_ERR);
1241e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn        } catch (IllegalArgumentException e) {
1242e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn            System.err.println("Bad argument: " + e.toString());
1243e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn            showUsage();
1244e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn        } catch (SecurityException e) {
1245e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn            System.err.println("Operation not allowed: " + e.toString());
1246e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn        }
1247e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn    }
1248e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn
1249719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey    private void runSetPermissionEnforced() {
1250719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey        final String permission = nextArg();
1251719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey        if (permission == null) {
1252719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey            System.err.println("Error: no permission specified");
1253719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey            showUsage();
1254719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey            return;
1255719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey        }
1256719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey        final String enforcedRaw = nextArg();
1257719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey        if (enforcedRaw == null) {
1258719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey            System.err.println("Error: no enforcement specified");
1259719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey            showUsage();
1260719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey            return;
1261719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey        }
1262719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey        final boolean enforced = Boolean.parseBoolean(enforcedRaw);
1263719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey        try {
1264719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey            mPm.setPermissionEnforced(permission, enforced);
1265719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey        } catch (RemoteException e) {
1266719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey            System.err.println(e.toString());
1267719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey            System.err.println(PM_NOT_RUNNING_ERR);
1268719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey        } catch (IllegalArgumentException e) {
1269719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey            System.err.println("Bad argument: " + e.toString());
1270719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey            showUsage();
1271719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey        } catch (SecurityException e) {
1272719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey            System.err.println("Operation not allowed: " + e.toString());
1273719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey        }
1274719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey    }
1275719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey
1276197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn    static class ClearCacheObserver extends IPackageDataObserver.Stub {
1277197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        boolean finished;
1278197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        boolean result;
1279197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn
1280197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        @Override
1281197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        public void onRemoveCompleted(String packageName, boolean succeeded) throws RemoteException {
1282197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            synchronized (this) {
1283197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn                finished = true;
1284197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn                result = succeeded;
1285197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn                notifyAll();
1286197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            }
1287197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        }
1288197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn
1289197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn    }
1290197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn
1291197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn    private void runTrimCaches() {
1292197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        String size = nextArg();
1293197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        if (size == null) {
1294197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            System.err.println("Error: no size specified");
1295197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            showUsage();
1296197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            return;
1297197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        }
1298197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        int len = size.length();
1299197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        long multiplier = 1;
1300197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        if (len > 1) {
1301197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            char c = size.charAt(len-1);
1302197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            if (c == 'K' || c == 'k') {
1303197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn                multiplier = 1024L;
1304197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            } else if (c == 'M' || c == 'm') {
1305197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn                multiplier = 1024L*1024L;
1306197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            } else if (c == 'G' || c == 'g') {
1307197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn                multiplier = 1024L*1024L*1024L;
1308197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            } else {
1309197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn                System.err.println("Invalid suffix: " + c);
1310197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn                showUsage();
1311197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn                return;
1312197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            }
1313197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            size = size.substring(0, len-1);
1314197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        }
1315197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        long sizeVal;
1316197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        try {
1317197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            sizeVal = Long.parseLong(size) * multiplier;
1318197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        } catch (NumberFormatException e) {
1319197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            System.err.println("Error: expected number at: " + size);
1320197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            showUsage();
1321197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            return;
1322197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        }
1323197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        ClearDataObserver obs = new ClearDataObserver();
1324197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        try {
1325197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            mPm.freeStorageAndNotify(sizeVal, obs);
1326197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            synchronized (obs) {
1327197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn                while (!obs.finished) {
1328197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn                    try {
1329197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn                        obs.wait();
1330197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn                    } catch (InterruptedException e) {
1331197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn                    }
1332197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn                }
1333197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            }
1334197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        } catch (RemoteException e) {
1335197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            System.err.println(e.toString());
1336197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            System.err.println(PM_NOT_RUNNING_ERR);
1337197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        } catch (IllegalArgumentException e) {
1338197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            System.err.println("Bad argument: " + e.toString());
1339197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            showUsage();
1340197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        } catch (SecurityException e) {
1341197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            System.err.println("Operation not allowed: " + e.toString());
1342197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        }
1343197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn    }
1344197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn
13459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
13469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Displays the package file for a package.
13479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param pckg
13489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
13499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private void displayPackageFilePath(String pckg) {
13509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
1351483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani            PackageInfo info = mPm.getPackageInfo(pckg, 0, 0);
13529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (info != null && info.applicationInfo != null) {
13539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                System.out.print("package:");
13549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                System.out.println(info.applicationInfo.sourceDir);
13559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
13569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } catch (RemoteException e) {
13579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.err.println(e.toString());
13589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.err.println(PM_NOT_RUNNING_ERR);
13599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
13609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
136123b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
13629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private Resources getResources(PackageItemInfo pii) {
13639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        Resources res = mResourceCache.get(pii.packageName);
13649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (res != null) return res;
136523b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
13669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
1367483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani            ApplicationInfo ai = mPm.getApplicationInfo(pii.packageName, 0, 0);
13689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            AssetManager am = new AssetManager();
13699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            am.addAssetPath(ai.publicSourceDir);
13709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            res = new Resources(am, null, null);
13719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mResourceCache.put(pii.packageName, res);
13729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return res;
13739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } catch (RemoteException e) {
13749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.err.println(e.toString());
13759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.err.println(PM_NOT_RUNNING_ERR);
13769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return null;
13779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
13789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
137923b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
13809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private String nextOption() {
13819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (mNextArg >= mArgs.length) {
13829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return null;
13839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
13849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        String arg = mArgs[mNextArg];
13859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (!arg.startsWith("-")) {
13869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return null;
13879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
13889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mNextArg++;
13899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (arg.equals("--")) {
13909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return null;
13919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
13929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (arg.length() > 1 && arg.charAt(1) != '-') {
13939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (arg.length() > 2) {
13949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                mCurArgData = arg.substring(2);
13959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                return arg.substring(0, 2);
13969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            } else {
13979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                mCurArgData = null;
13989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                return arg;
13999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
14009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
14019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mCurArgData = null;
14029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return arg;
14039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
14049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
14059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private String nextOptionData() {
14069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (mCurArgData != null) {
14079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return mCurArgData;
14089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
14099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (mNextArg >= mArgs.length) {
14109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return null;
14119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
14129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        String data = mArgs[mNextArg];
14139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mNextArg++;
14149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return data;
14159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
14169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
14179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private String nextArg() {
14189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (mNextArg >= mArgs.length) {
14199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return null;
14209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
14219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        String arg = mArgs[mNextArg];
14229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mNextArg++;
14239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return arg;
14249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
14259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
14269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static void showUsage() {
1427151ec4c9f8ca34625733730a4551a23eb97cda23Amith Yamasani        System.err.println("usage: pm list packages [-f] [-d] [-e] [-s] [-3] [-i] [-u] [--user USER_ID] [FILTER]");
14289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        System.err.println("       pm list permission-groups");
14299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        System.err.println("       pm list permissions [-g] [-f] [-d] [-u] [GROUP]");
143023b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet        System.err.println("       pm list instrumentation [-f] [TARGET-PACKAGE]");
1431039c68e75606e837cf021815a0210836724574adDianne Hackborn        System.err.println("       pm list features");
1432e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn        System.err.println("       pm list libraries");
14337e0e19d4201374d72b7082492bd269dbdb17d972Dianne Hackborn        System.err.println("       pm list users");
14349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        System.err.println("       pm path PACKAGE");
1435ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        System.err.println("       pm install [-l] [-r] [-t] [-i INSTALLER_PACKAGE_NAME] [-s] [-f]");
1436706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings        System.err.println("                  [--algo <algorithm name> --key <key-in-hex> --iv <IV-in-hex>]");
1437706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings        System.err.println("                  [--originating-uri <URI>] [--referrer <URI>] PATH");
14389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        System.err.println("       pm uninstall [-k] PACKAGE");
1439690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn        System.err.println("       pm clear PACKAGE");
14407e0e19d4201374d72b7082492bd269dbdb17d972Dianne Hackborn        System.err.println("       pm enable [--user USER_ID] PACKAGE_OR_COMPONENT");
14417e0e19d4201374d72b7082492bd269dbdb17d972Dianne Hackborn        System.err.println("       pm disable [--user USER_ID] PACKAGE_OR_COMPONENT");
14427e0e19d4201374d72b7082492bd269dbdb17d972Dianne Hackborn        System.err.println("       pm disable-user [--user USER_ID] PACKAGE_OR_COMPONENT");
1443e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn        System.err.println("       pm grant PACKAGE PERMISSION");
1444e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn        System.err.println("       pm revoke PACKAGE PERMISSION");
144546d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("       pm set-install-location [0/auto] [1/internal] [2/external]");
144646d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("       pm get-install-location");
1447719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey        System.err.println("       pm set-permission-enforced PERMISSION [true|false]");
1448197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        System.err.println("       pm trim-caches DESIRED_FREE_SPACE");
14497e0e19d4201374d72b7082492bd269dbdb17d972Dianne Hackborn        System.err.println("       pm create-user USER_NAME");
14507e0e19d4201374d72b7082492bd269dbdb17d972Dianne Hackborn        System.err.println("       pm remove-user USER_ID");
14519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        System.err.println("");
145246d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("pm list packages: prints all packages, optionally only");
145346d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("  those whose package name contains the text in FILTER.  Options:");
145446d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("    -f: see their associated file.");
145546d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("    -d: filter to only show disbled packages.");
145646d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("    -e: filter to only show enabled packages.");
145746d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("    -s: filter to only show system packages.");
145846d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("    -3: filter to only show third party packages.");
145992a208c6d3e0fdae54bbc942e7e041e48812bcaaAshish Sharma        System.err.println("    -i: see the installer for the packages.");
146046d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("    -u: also include uninstalled packages.");
14619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        System.err.println("");
146246d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("pm list permission-groups: prints all known permission groups.");
14639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        System.err.println("");
146446d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("pm list permissions: prints all known permissions, optionally only");
146546d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("  those in GROUP.  Options:");
146646d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("    -g: organize by group.");
146746d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("    -f: print all information.");
146846d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("    -s: short summary.");
146946d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("    -d: only list dangerous permissions.");
147046d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("    -u: list only the permissions users will see.");
14719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        System.err.println("");
147246d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("pm list instrumentation: use to list all test packages; optionally");
147346d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("  supply <TARGET-PACKAGE> to list the test packages for a particular");
147446d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("  application.  Options:");
147546d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("    -f: list the .apk file for the test package.");
14769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        System.err.println("");
147746d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("pm list features: prints all features of the system.");
1478039c68e75606e837cf021815a0210836724574adDianne Hackborn        System.err.println("");
1479786b44046a79d6c4c9cd07f5989d491c7196ad80Dianne Hackborn        System.err.println("pm list users: prints all users on the system.");
1480786b44046a79d6c4c9cd07f5989d491c7196ad80Dianne Hackborn        System.err.println("");
148146d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("pm path: print the path to the .apk of the given PACKAGE.");
14829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        System.err.println("");
148346d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("pm install: installs a package to the system.  Options:");
148446d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("    -l: install the package with FORWARD_LOCK.");
148546d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("    -r: reinstall an exisiting app, keeping its data.");
148646d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("    -t: allow test .apks to be installed.");
148746d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("    -i: specify the installer package name.");
148846d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("    -s: install package on sdcard.");
148946d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("    -f: install package on internal flash.");
14907767eac3232ba2fb9828766813cdb481d6a97584Dianne Hackborn        System.err.println("    -d: allow version code downgrade.");
14919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        System.err.println("");
149246d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("pm uninstall: removes a package from the system. Options:");
149346d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("    -k: keep the data and cache directories around after package removal.");
14949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        System.err.println("");
149546d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("pm clear: deletes all data associated with a package.");
1496690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn        System.err.println("");
149746d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("pm enable, disable, disable-user: these commands change the enabled state");
149846d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("  of a given package or component (written as \"package/class\").");
149940e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu        System.err.println("");
1500e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn        System.err.println("pm grant, revoke: these commands either grant or revoke permissions");
1501e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn        System.err.println("  to applications.  Only optional permissions the application has");
1502e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn        System.err.println("  declared can be granted or revoked.");
1503e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn        System.err.println("");
150446d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("pm get-install-location: returns the current install location.");
150546d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("    0 [auto]: Let system decide the best location");
150646d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("    1 [internal]: Install on internal device storage");
150746d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("    2 [external]: Install on external media");
150840e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu        System.err.println("");
150946d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("pm set-install-location: changes the default install location.");
151046d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("  NOTE: this is only intended for debugging; using this can cause");
151146d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("  applications to break and other undersireable behavior.");
151246d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("    0 [auto]: Let system decide the best location");
151346d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("    1 [internal]: Install on internal device storage");
151446d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("    2 [external]: Install on external media");
1515197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        System.err.println("");
1516197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        System.err.println("pm trim-caches: trim cache files to reach the given free space.");
1517786b44046a79d6c4c9cd07f5989d491c7196ad80Dianne Hackborn        System.err.println("");
1518786b44046a79d6c4c9cd07f5989d491c7196ad80Dianne Hackborn        System.err.println("pm create-user: create a new user with the given USER_NAME,");
1519786b44046a79d6c4c9cd07f5989d491c7196ad80Dianne Hackborn        System.err.println("  printing the new user identifier of the user.");
1520786b44046a79d6c4c9cd07f5989d491c7196ad80Dianne Hackborn        System.err.println("");
1521786b44046a79d6c4c9cd07f5989d491c7196ad80Dianne Hackborn        System.err.println("pm remove-user: remove the user with the given USER_IDENTIFIER,");
1522786b44046a79d6c4c9cd07f5989d491c7196ad80Dianne Hackborn        System.err.println("  deleting all data associated with that user");
15239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
15249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1525