Pm.java revision 786b44046a79d6c4c9cd07f5989d491c7196ad80
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;
23258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasaniimport android.content.Context;
249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.pm.ApplicationInfo;
25ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Cowardimport android.content.pm.ContainerEncryptionParams;
26039c68e75606e837cf021815a0210836724574adDianne Hackbornimport android.content.pm.FeatureInfo;
27690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackbornimport android.content.pm.IPackageDataObserver;
289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.pm.IPackageDeleteObserver;
299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.pm.IPackageInstallObserver;
309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.pm.IPackageManager;
319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.pm.InstrumentationInfo;
329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.pm.PackageInfo;
339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.pm.PackageItemInfo;
349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.pm.PackageManager;
350e2c0f37d98bb5539b0fe41865aaf1add0ff1bb3Kenny Rootimport android.content.pm.ParceledListSlice;
369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.pm.PermissionGroupInfo;
379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.pm.PermissionInfo;
38742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasaniimport android.content.pm.UserInfo;
39706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich canningsimport android.content.pm.VerificationParams;
409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.res.AssetManager;
419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.res.Resources;
429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.net.Uri;
43742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasaniimport android.os.Binder;
44258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasaniimport android.os.IUserManager;
450b285499db739ba50f2f839d633e763c70e67f96Amith Yamasaniimport android.os.Process;
469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.os.RemoteException;
479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.os.ServiceManager;
48258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasaniimport android.os.UserManager;
499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.io.File;
5123b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohetimport java.lang.reflect.Field;
5223b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohetimport java.lang.reflect.Modifier;
53ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Cowardimport java.security.InvalidAlgorithmParameterException;
549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.util.ArrayList;
559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.util.Collections;
569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.util.Comparator;
579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.util.List;
589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport java.util.WeakHashMap;
599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
60ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Cowardimport javax.crypto.SecretKey;
61ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Cowardimport javax.crypto.spec.IvParameterSpec;
62ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Cowardimport javax.crypto.spec.SecretKeySpec;
63ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpublic final class Pm {
659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    IPackageManager mPm;
66258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani    IUserManager mUm;
6723b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private WeakHashMap<String, Resources> mResourceCache
699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            = new WeakHashMap<String, Resources>();
7023b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private String[] mArgs;
729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private int mNextArg;
739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private String mCurArgData;
7423b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
7523b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet    private static final String PM_NOT_RUNNING_ERR =
769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        "Error: Could not access the Package Manager.  Is the system running?";
770b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani    private static final int ROOT_UID = 0;
7823b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static void main(String[] args) {
809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        new Pm().run(args);
819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
8223b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void run(String[] args) {
849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        boolean validCommand = false;
859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (args.length < 1) {
869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            showUsage();
879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return;
889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
90258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani        mUm = IUserManager.Stub.asInterface(ServiceManager.getService("user"));
919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mPm = IPackageManager.Stub.asInterface(ServiceManager.getService("package"));
929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (mPm == null) {
939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.err.println(PM_NOT_RUNNING_ERR);
949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return;
959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mArgs = args;
989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        String op = args[0];
999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mNextArg = 1;
10023b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
1019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if ("list".equals(op)) {
1029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            runList();
1039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return;
1049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
10523b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
1069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if ("path".equals(op)) {
1079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            runPath();
1089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return;
1099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
11023b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
1119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if ("install".equals(op)) {
1129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            runInstall();
1139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return;
1149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
11523b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
1169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if ("uninstall".equals(op)) {
1179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            runUninstall();
1189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return;
1199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
12023b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
121690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn        if ("clear".equals(op)) {
122690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn            runClear();
123690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn            return;
124690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn        }
125690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn
1269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if ("enable".equals(op)) {
1279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            runSetEnabledSetting(PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
1289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return;
1299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
13023b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
1319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if ("disable".equals(op)) {
1329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            runSetEnabledSetting(PackageManager.COMPONENT_ENABLED_STATE_DISABLED);
1339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return;
1349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
13523b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
1360ac3031c159a0904e73eb4439cdc724d8df4a6e6Dianne Hackborn        if ("disable-user".equals(op)) {
1370ac3031c159a0904e73eb4439cdc724d8df4a6e6Dianne Hackborn            runSetEnabledSetting(PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER);
1380ac3031c159a0904e73eb4439cdc724d8df4a6e6Dianne Hackborn            return;
1390ac3031c159a0904e73eb4439cdc724d8df4a6e6Dianne Hackborn        }
1400ac3031c159a0904e73eb4439cdc724d8df4a6e6Dianne Hackborn
141e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn        if ("grant".equals(op)) {
142e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn            runGrantRevokePermission(true);
143e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn            return;
144e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn        }
145e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn
146e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn        if ("revoke".equals(op)) {
147e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn            runGrantRevokePermission(false);
148e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn            return;
149e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn        }
150e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn
151719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey        if ("set-permission-enforced".equals(op)) {
152719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey            runSetPermissionEnforced();
153719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey            return;
154719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey        }
155719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey
15646d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        if ("set-install-location".equals(op)) {
15740e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu            runSetInstallLocation();
15840e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu            return;
15940e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu        }
16040e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu
16146d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        if ("get-install-location".equals(op)) {
16240e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu            runGetInstallLocation();
16340e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu            return;
16440e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu        }
16540e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu
166197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        if ("trim-caches".equals(op)) {
167197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            runTrimCaches();
168197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            return;
169197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        }
170197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn
171483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani        if ("create-user".equals(op)) {
172483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani            runCreateUser();
173742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani            return;
174742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani        }
175742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani
176483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani        if ("remove-user".equals(op)) {
177483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani            runRemoveUser();
1780b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani            return;
1790b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani        }
1800b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani
1819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
1829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (args.length == 1) {
1839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (args[0].equalsIgnoreCase("-l")) {
1849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    validCommand = true;
1859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    runListPackages(false);
1869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                } else if (args[0].equalsIgnoreCase("-lf")){
1879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    validCommand = true;
1889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    runListPackages(true);
1899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
1909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            } else if (args.length == 2) {
1919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (args[0].equalsIgnoreCase("-p")) {
1929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    validCommand = true;
1939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    displayPackageFilePath(args[1]);
1949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
1959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
1969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } finally {
1979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (validCommand == false) {
1989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (op != null) {
1999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    System.err.println("Error: unknown command '" + op + "'");
2009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
2019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                showUsage();
2029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
2039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
2049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
20523b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
2069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
2079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Execute the list sub-command.
20823b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet     *
2099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * pm list [package | packages]
2109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * pm list permission-groups
2119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * pm list permissions
212039c68e75606e837cf021815a0210836724574adDianne Hackborn     * pm list features
213e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn     * pm list libraries
2149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * pm list instrumentation
2159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
2169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private void runList() {
2179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        String type = nextArg();
2189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (type == null) {
2199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.err.println("Error: didn't specify type of data to list");
2209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return;
2219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
2229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if ("package".equals(type) || "packages".equals(type)) {
2239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            runListPackages(false);
2249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else if ("permission-groups".equals(type)) {
2259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            runListPermissionGroups();
2269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else if ("permissions".equals(type)) {
2279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            runListPermissions();
228039c68e75606e837cf021815a0210836724574adDianne Hackborn        } else if ("features".equals(type)) {
229039c68e75606e837cf021815a0210836724574adDianne Hackborn            runListFeatures();
230e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn        } else if ("libraries".equals(type)) {
231e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn            runListLibraries();
2329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else if ("instrumentation".equals(type)) {
2339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            runListInstrumentation();
234483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani        } else if ("users".equals(type)) {
235483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani            runListUsers();
2369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else {
2379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.err.println("Error: unknown list type '" + type + "'");
2389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
2399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
24023b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
2419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
2429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Lists all the installed packages.
2439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
2449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private void runListPackages(boolean showApplicationPackage) {
245e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn        int getFlags = 0;
246e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn        boolean listDisabled = false, listEnabled = false;
2470f1de9adde0b52d2a385a76232bd7ac30c3eeea2Dianne Hackborn        boolean listSystem = false, listThirdParty = false;
24892a208c6d3e0fdae54bbc942e7e041e48812bcaaAshish Sharma        boolean listInstaller = false;
2499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
2509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            String opt;
2519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            while ((opt=nextOption()) != null) {
2529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (opt.equals("-l")) {
2539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    // old compat
2549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                } else if (opt.equals("-lf")) {
2559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    showApplicationPackage = true;
2569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                } else if (opt.equals("-f")) {
2579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    showApplicationPackage = true;
258e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn                } else if (opt.equals("-d")) {
259e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn                    listDisabled = true;
260e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn                } else if (opt.equals("-e")) {
261e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn                    listEnabled = true;
2620f1de9adde0b52d2a385a76232bd7ac30c3eeea2Dianne Hackborn                } else if (opt.equals("-s")) {
2630f1de9adde0b52d2a385a76232bd7ac30c3eeea2Dianne Hackborn                    listSystem = true;
2640f1de9adde0b52d2a385a76232bd7ac30c3eeea2Dianne Hackborn                } else if (opt.equals("-3")) {
2650f1de9adde0b52d2a385a76232bd7ac30c3eeea2Dianne Hackborn                    listThirdParty = true;
26692a208c6d3e0fdae54bbc942e7e041e48812bcaaAshish Sharma                } else if (opt.equals("-i")) {
26792a208c6d3e0fdae54bbc942e7e041e48812bcaaAshish Sharma                    listInstaller = true;
268e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn                } else if (opt.equals("-u")) {
269e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn                    getFlags |= PackageManager.GET_UNINSTALLED_PACKAGES;
2709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                } else {
2719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    System.err.println("Error: Unknown option: " + opt);
2729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    return;
2739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
2749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
2759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } catch (RuntimeException ex) {
2769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.err.println("Error: " + ex.toString());
2779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return;
2789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
27923b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
280e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn        String filter = nextArg();
281e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn
2829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
2830e2c0f37d98bb5539b0fe41865aaf1add0ff1bb3Kenny Root            final List<PackageInfo> packages = getInstalledPackages(mPm, getFlags);
28423b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
2859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            int count = packages.size();
2869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            for (int p = 0 ; p < count ; p++) {
2879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                PackageInfo info = packages.get(p);
288e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn                if (filter != null && !info.packageName.contains(filter)) {
289e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn                    continue;
290e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn                }
2910f1de9adde0b52d2a385a76232bd7ac30c3eeea2Dianne Hackborn                final boolean isSystem =
2920f1de9adde0b52d2a385a76232bd7ac30c3eeea2Dianne Hackborn                        (info.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM) != 0;
293e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn                if ((!listDisabled || !info.applicationInfo.enabled) &&
2940f1de9adde0b52d2a385a76232bd7ac30c3eeea2Dianne Hackborn                        (!listEnabled || info.applicationInfo.enabled) &&
2950f1de9adde0b52d2a385a76232bd7ac30c3eeea2Dianne Hackborn                        (!listSystem || isSystem) &&
2960f1de9adde0b52d2a385a76232bd7ac30c3eeea2Dianne Hackborn                        (!listThirdParty || !isSystem)) {
297e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn                    System.out.print("package:");
298e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn                    if (showApplicationPackage) {
299e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn                        System.out.print(info.applicationInfo.sourceDir);
300e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn                        System.out.print("=");
301e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn                    }
30292a208c6d3e0fdae54bbc942e7e041e48812bcaaAshish Sharma                    System.out.print(info.packageName);
30392a208c6d3e0fdae54bbc942e7e041e48812bcaaAshish Sharma                    if (listInstaller) {
30492a208c6d3e0fdae54bbc942e7e041e48812bcaaAshish Sharma                        System.out.print("  installer=");
30592a208c6d3e0fdae54bbc942e7e041e48812bcaaAshish Sharma                        System.out.print(mPm.getInstallerPackageName(info.packageName));
30692a208c6d3e0fdae54bbc942e7e041e48812bcaaAshish Sharma                    }
30792a208c6d3e0fdae54bbc942e7e041e48812bcaaAshish Sharma                    System.out.println();
3089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
3099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
3109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } catch (RemoteException e) {
3119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.err.println(e.toString());
3129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.err.println(PM_NOT_RUNNING_ERR);
3139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
3149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
31523b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
3160e2c0f37d98bb5539b0fe41865aaf1add0ff1bb3Kenny Root    @SuppressWarnings("unchecked")
3170e2c0f37d98bb5539b0fe41865aaf1add0ff1bb3Kenny Root    private List<PackageInfo> getInstalledPackages(IPackageManager pm, int flags)
3180e2c0f37d98bb5539b0fe41865aaf1add0ff1bb3Kenny Root            throws RemoteException {
3190e2c0f37d98bb5539b0fe41865aaf1add0ff1bb3Kenny Root        final List<PackageInfo> packageInfos = new ArrayList<PackageInfo>();
3200e2c0f37d98bb5539b0fe41865aaf1add0ff1bb3Kenny Root        PackageInfo lastItem = null;
3210e2c0f37d98bb5539b0fe41865aaf1add0ff1bb3Kenny Root        ParceledListSlice<PackageInfo> slice;
3220e2c0f37d98bb5539b0fe41865aaf1add0ff1bb3Kenny Root
3230e2c0f37d98bb5539b0fe41865aaf1add0ff1bb3Kenny Root        do {
3240e2c0f37d98bb5539b0fe41865aaf1add0ff1bb3Kenny Root            final String lastKey = lastItem != null ? lastItem.packageName : null;
3250e2c0f37d98bb5539b0fe41865aaf1add0ff1bb3Kenny Root            slice = pm.getInstalledPackages(flags, lastKey);
3260e2c0f37d98bb5539b0fe41865aaf1add0ff1bb3Kenny Root            lastItem = slice.populateList(packageInfos, PackageInfo.CREATOR);
3270e2c0f37d98bb5539b0fe41865aaf1add0ff1bb3Kenny Root        } while (!slice.isLastSlice());
3280e2c0f37d98bb5539b0fe41865aaf1add0ff1bb3Kenny Root
3290e2c0f37d98bb5539b0fe41865aaf1add0ff1bb3Kenny Root        return packageInfos;
3300e2c0f37d98bb5539b0fe41865aaf1add0ff1bb3Kenny Root    }
3310e2c0f37d98bb5539b0fe41865aaf1add0ff1bb3Kenny Root
3329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
333039c68e75606e837cf021815a0210836724574adDianne Hackborn     * Lists all of the features supported by the current device.
334039c68e75606e837cf021815a0210836724574adDianne Hackborn     *
335039c68e75606e837cf021815a0210836724574adDianne Hackborn     * pm list features
336039c68e75606e837cf021815a0210836724574adDianne Hackborn     */
337039c68e75606e837cf021815a0210836724574adDianne Hackborn    private void runListFeatures() {
338039c68e75606e837cf021815a0210836724574adDianne Hackborn        try {
339039c68e75606e837cf021815a0210836724574adDianne Hackborn            List<FeatureInfo> list = new ArrayList<FeatureInfo>();
340039c68e75606e837cf021815a0210836724574adDianne Hackborn            FeatureInfo[] rawList = mPm.getSystemAvailableFeatures();
341039c68e75606e837cf021815a0210836724574adDianne Hackborn            for (int i=0; i<rawList.length; i++) {
342039c68e75606e837cf021815a0210836724574adDianne Hackborn                list.add(rawList[i]);
343039c68e75606e837cf021815a0210836724574adDianne Hackborn            }
344babe28e01c6e845e41f8a7d23ab0b2a2c93a2e6bJoe Malin
345039c68e75606e837cf021815a0210836724574adDianne Hackborn
346039c68e75606e837cf021815a0210836724574adDianne Hackborn            // Sort by name
347039c68e75606e837cf021815a0210836724574adDianne Hackborn            Collections.sort(list, new Comparator<FeatureInfo>() {
348039c68e75606e837cf021815a0210836724574adDianne Hackborn                public int compare(FeatureInfo o1, FeatureInfo o2) {
349039c68e75606e837cf021815a0210836724574adDianne Hackborn                    if (o1.name == o2.name) return 0;
350039c68e75606e837cf021815a0210836724574adDianne Hackborn                    if (o1.name == null) return -1;
351039c68e75606e837cf021815a0210836724574adDianne Hackborn                    if (o2.name == null) return 1;
352039c68e75606e837cf021815a0210836724574adDianne Hackborn                    return o1.name.compareTo(o2.name);
353039c68e75606e837cf021815a0210836724574adDianne Hackborn                }
354039c68e75606e837cf021815a0210836724574adDianne Hackborn            });
355039c68e75606e837cf021815a0210836724574adDianne Hackborn
356039c68e75606e837cf021815a0210836724574adDianne Hackborn            int count = (list != null) ? list.size() : 0;
357039c68e75606e837cf021815a0210836724574adDianne Hackborn            for (int p = 0; p < count; p++) {
358039c68e75606e837cf021815a0210836724574adDianne Hackborn                FeatureInfo fi = list.get(p);
359039c68e75606e837cf021815a0210836724574adDianne Hackborn                System.out.print("feature:");
360039c68e75606e837cf021815a0210836724574adDianne Hackborn                if (fi.name != null) System.out.println(fi.name);
361039c68e75606e837cf021815a0210836724574adDianne Hackborn                else System.out.println("reqGlEsVersion=0x"
362039c68e75606e837cf021815a0210836724574adDianne Hackborn                        + Integer.toHexString(fi.reqGlEsVersion));
363039c68e75606e837cf021815a0210836724574adDianne Hackborn            }
364039c68e75606e837cf021815a0210836724574adDianne Hackborn        } catch (RemoteException e) {
365039c68e75606e837cf021815a0210836724574adDianne Hackborn            System.err.println(e.toString());
366039c68e75606e837cf021815a0210836724574adDianne Hackborn            System.err.println(PM_NOT_RUNNING_ERR);
367039c68e75606e837cf021815a0210836724574adDianne Hackborn        }
368039c68e75606e837cf021815a0210836724574adDianne Hackborn    }
369039c68e75606e837cf021815a0210836724574adDianne Hackborn
370039c68e75606e837cf021815a0210836724574adDianne Hackborn    /**
371e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn     * Lists all of the libraries supported by the current device.
372e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn     *
373e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn     * pm list libraries
374e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn     */
375e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn    private void runListLibraries() {
376e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn        try {
377e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn            List<String> list = new ArrayList<String>();
378e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn            String[] rawList = mPm.getSystemSharedLibraryNames();
379e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn            for (int i=0; i<rawList.length; i++) {
380e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn                list.add(rawList[i]);
381e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn            }
382e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn
383e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn
384e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn            // Sort by name
385e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn            Collections.sort(list, new Comparator<String>() {
386e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn                public int compare(String o1, String o2) {
387e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn                    if (o1 == o2) return 0;
388e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn                    if (o1 == null) return -1;
389e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn                    if (o2 == null) return 1;
390e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn                    return o1.compareTo(o2);
391e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn                }
392e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn            });
393e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn
394e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn            int count = (list != null) ? list.size() : 0;
395e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn            for (int p = 0; p < count; p++) {
396e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn                String lib = list.get(p);
397e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn                System.out.print("library:");
398e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn                System.out.println(lib);
399e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn            }
400e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn        } catch (RemoteException e) {
401e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn            System.err.println(e.toString());
402e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn            System.err.println(PM_NOT_RUNNING_ERR);
403e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn        }
404e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn    }
405e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn
406e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn    /**
4079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Lists all of the installed instrumentation, or all for a given package
40823b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet     *
4099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * pm list instrumentation [package] [-f]
4109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
4119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private void runListInstrumentation() {
4129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int flags = 0;      // flags != 0 is only used to request meta-data
4139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        boolean showPackage = false;
4149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        String targetPackage = null;
4159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
4179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            String opt;
4189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            while ((opt=nextArg()) != null) {
4199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (opt.equals("-f")) {
4209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    showPackage = true;
4219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                } else if (opt.charAt(0) != '-') {
4229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    targetPackage = opt;
4239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                } else {
4249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    System.err.println("Error: Unknown option: " + opt);
4259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    return;
4269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
4279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
4289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } catch (RuntimeException ex) {
4299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.err.println("Error: " + ex.toString());
4309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return;
4319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
4349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            List<InstrumentationInfo> list = mPm.queryInstrumentation(targetPackage, flags);
4359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            // Sort by target package
4379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            Collections.sort(list, new Comparator<InstrumentationInfo>() {
4389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                public int compare(InstrumentationInfo o1, InstrumentationInfo o2) {
4399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    return o1.targetPackage.compareTo(o2.targetPackage);
4409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
4419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            });
4429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            int count = (list != null) ? list.size() : 0;
4449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            for (int p = 0; p < count; p++) {
4459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                InstrumentationInfo ii = list.get(p);
4469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                System.out.print("instrumentation:");
4479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (showPackage) {
4489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    System.out.print(ii.sourceDir);
4499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    System.out.print("=");
4509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
4519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                ComponentName cn = new ComponentName(ii.packageName, ii.name);
4529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                System.out.print(cn.flattenToShortString());
4539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                System.out.print(" (target=");
4549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                System.out.print(ii.targetPackage);
4559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                System.out.println(")");
4569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
4579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } catch (RemoteException e) {
4589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.err.println(e.toString());
4599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.err.println(PM_NOT_RUNNING_ERR);
4609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
46223b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
4639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
4649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Lists all the known permission groups.
4659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
4669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private void runListPermissionGroups() {
4679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
4689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            List<PermissionGroupInfo> pgs = mPm.getAllPermissionGroups(0);
46923b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
4709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            int count = pgs.size();
4719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            for (int p = 0 ; p < count ; p++) {
4729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                PermissionGroupInfo pgi = pgs.get(p);
4739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                System.out.print("permission group:");
4749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                System.out.println(pgi.name);
4759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
4769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } catch (RemoteException e) {
4779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.err.println(e.toString());
4789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.err.println(PM_NOT_RUNNING_ERR);
4799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
48123b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
4829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private String loadText(PackageItemInfo pii, int res, CharSequence nonLocalized) {
4839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (nonLocalized != null) {
4849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return nonLocalized.toString();
4859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4861c3274ca12a6931568693b897c269107f0346eabAli Utku Selen        if (res != 0) {
4871c3274ca12a6931568693b897c269107f0346eabAli Utku Selen            Resources r = getResources(pii);
4881c3274ca12a6931568693b897c269107f0346eabAli Utku Selen            if (r != null) {
4891c3274ca12a6931568693b897c269107f0346eabAli Utku Selen                return r.getString(res);
4901c3274ca12a6931568693b897c269107f0346eabAli Utku Selen            }
4919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return null;
4939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
49423b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
4959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
4969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Lists all the permissions in a group.
4979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
4989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private void runListPermissions() {
4999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
5009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            boolean labels = false;
5019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            boolean groups = false;
5029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            boolean userOnly = false;
5039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            boolean summary = false;
5049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            boolean dangerousOnly = false;
5059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            String opt;
5069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            while ((opt=nextOption()) != null) {
5079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (opt.equals("-f")) {
5089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    labels = true;
5099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                } else if (opt.equals("-g")) {
5109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    groups = true;
5119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                } else if (opt.equals("-s")) {
5129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    groups = true;
5139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    labels = true;
5149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    summary = true;
5159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                } else if (opt.equals("-u")) {
5169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    userOnly = true;
5179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                } else if (opt.equals("-d")) {
5189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    dangerousOnly = true;
5199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                } else {
5209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    System.err.println("Error: Unknown option: " + opt);
5219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    return;
5229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
5239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
52423b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
5259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            String grp = nextOption();
5269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            ArrayList<String> groupList = new ArrayList<String>();
5279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (groups) {
5289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                List<PermissionGroupInfo> infos =
5299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        mPm.getAllPermissionGroups(0);
5309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                for (int i=0; i<infos.size(); i++) {
5319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    groupList.add(infos.get(i).name);
5329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
5339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                groupList.add(null);
5349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            } else {
5359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                groupList.add(grp);
5369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
53723b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
5389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (dangerousOnly) {
5399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                System.out.println("Dangerous Permissions:");
5409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                System.out.println("");
5419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                doListPermissions(groupList, groups, labels, summary,
5429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        PermissionInfo.PROTECTION_DANGEROUS,
5439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        PermissionInfo.PROTECTION_DANGEROUS);
5449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (userOnly) {
5459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    System.out.println("Normal Permissions:");
5469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    System.out.println("");
5479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    doListPermissions(groupList, groups, labels, summary,
5489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            PermissionInfo.PROTECTION_NORMAL,
5499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            PermissionInfo.PROTECTION_NORMAL);
5509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
5519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            } else if (userOnly) {
5529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                System.out.println("Dangerous and Normal Permissions:");
5539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                System.out.println("");
5549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                doListPermissions(groupList, groups, labels, summary,
5559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        PermissionInfo.PROTECTION_NORMAL,
5569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        PermissionInfo.PROTECTION_DANGEROUS);
5579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            } else {
5589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                System.out.println("All Permissions:");
5599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                System.out.println("");
5609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                doListPermissions(groupList, groups, labels, summary,
5619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        -10000, 10000);
5629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
5639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } catch (RemoteException e) {
5649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.err.println(e.toString());
5659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.err.println(PM_NOT_RUNNING_ERR);
5669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
5679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
56823b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
5699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private void doListPermissions(ArrayList<String> groupList,
5709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            boolean groups, boolean labels, boolean summary,
5719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            int startProtectionLevel, int endProtectionLevel)
5729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            throws RemoteException {
5739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        for (int i=0; i<groupList.size(); i++) {
5749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            String groupName = groupList.get(i);
5759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            String prefix = "";
5769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (groups) {
5779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (i > 0) System.out.println("");
5789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (groupName != null) {
5799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    PermissionGroupInfo pgi = mPm.getPermissionGroupInfo(
5809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            groupName, 0);
5819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    if (summary) {
5829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        Resources res = getResources(pgi);
5839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        if (res != null) {
5849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            System.out.print(loadText(pgi, pgi.labelRes,
5859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                    pgi.nonLocalizedLabel) + ": ");
5869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        } else {
5879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            System.out.print(pgi.name + ": ");
58823b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
5899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        }
5909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    } else {
5919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        System.out.println((labels ? "+ " : "")
5929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                + "group:" + pgi.name);
5939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        if (labels) {
5949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            System.out.println("  package:" + pgi.packageName);
5959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            Resources res = getResources(pgi);
5969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            if (res != null) {
5979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                System.out.println("  label:"
5989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                        + loadText(pgi, pgi.labelRes,
5999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                                pgi.nonLocalizedLabel));
6009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                System.out.println("  description:"
6019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                        + loadText(pgi, pgi.descriptionRes,
6029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                                pgi.nonLocalizedDescription));
6039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            }
6049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        }
6059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    }
6069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                } else {
6079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    System.out.println(((labels && !summary)
6089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            ? "+ " : "") + "ungrouped:");
6099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
6109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                prefix = "  ";
6119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
6129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            List<PermissionInfo> ps = mPm.queryPermissionsByGroup(
6139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    groupList.get(i), 0);
6149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            int count = ps.size();
6159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            boolean first = true;
6169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            for (int p = 0 ; p < count ; p++) {
6179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                PermissionInfo pi = ps.get(p);
6189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (groups && groupName == null && pi.group != null) {
6199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    continue;
6209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
621e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn                final int base = pi.protectionLevel & PermissionInfo.PROTECTION_MASK_BASE;
622e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn                if (base < startProtectionLevel
623e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn                        || base > endProtectionLevel) {
6249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    continue;
6259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
6269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (summary) {
6279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    if (first) {
6289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        first = false;
6299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    } else {
6309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        System.out.print(", ");
6319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    }
6329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    Resources res = getResources(pi);
6339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    if (res != null) {
6349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        System.out.print(loadText(pi, pi.labelRes,
6359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                pi.nonLocalizedLabel));
6369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    } else {
6379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        System.out.print(pi.name);
6389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    }
6399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                } else {
6409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    System.out.println(prefix + (labels ? "+ " : "")
6419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            + "permission:" + pi.name);
6429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    if (labels) {
6439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        System.out.println(prefix + "  package:" + pi.packageName);
6449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        Resources res = getResources(pi);
6459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        if (res != null) {
6469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            System.out.println(prefix + "  label:"
6479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                    + loadText(pi, pi.labelRes,
6489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                            pi.nonLocalizedLabel));
6499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            System.out.println(prefix + "  description:"
6509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                    + loadText(pi, pi.descriptionRes,
6519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                                            pi.nonLocalizedDescription));
6529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        }
653e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn                        System.out.println(prefix + "  protectionLevel:"
654e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn                                + PermissionInfo.protectionToString(pi.protectionLevel));
6559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    }
6569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
6579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
65823b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
6599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (summary) {
6609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                System.out.println("");
6619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
6629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
6639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
66423b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
6659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private void runPath() {
6669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        String pkg = nextArg();
6679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (pkg == null) {
6689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.err.println("Error: no package specified");
6699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return;
6709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
6719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        displayPackageFilePath(pkg);
6729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
67323b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
6749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    class PackageInstallObserver extends IPackageInstallObserver.Stub {
6759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        boolean finished;
6769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int result;
6779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        public void packageInstalled(String name, int status) {
6799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            synchronized( this) {
6809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                finished = true;
6819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                result = status;
6829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                notifyAll();
6839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
6849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
6859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
68623b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
68723b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet    /**
68823b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet     * Converts a failure code into a string by using reflection to find a matching constant
68923b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet     * in PackageManager.
69023b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet     */
6919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private String installFailureToString(int result) {
69223b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet        Field[] fields = PackageManager.class.getFields();
69323b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet        for (Field f: fields) {
69423b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet            if (f.getType() == int.class) {
69523b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet                int modifiers = f.getModifiers();
69623b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet                // only look at public final static fields.
69723b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet                if (((modifiers & Modifier.FINAL) != 0) &&
69823b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet                        ((modifiers & Modifier.PUBLIC) != 0) &&
69923b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet                        ((modifiers & Modifier.STATIC) != 0)) {
70023b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet                    String fieldName = f.getName();
70123b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet                    if (fieldName.startsWith("INSTALL_FAILED_") ||
70223b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet                            fieldName.startsWith("INSTALL_PARSE_FAILED_")) {
70323b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet                        // get the int value and compare it to result.
70423b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet                        try {
70523b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet                            if (result == f.getInt(null)) {
70623b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet                                return fieldName;
70723b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet                            }
70823b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet                        } catch (IllegalAccessException e) {
70923b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet                            // this shouldn't happen since we only look for public static fields.
71023b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet                        }
71123b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet                    }
71223b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet                }
71323b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet            }
71423b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet        }
71523b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
71623b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet        // couldn't find a matching constant? return the value
71723b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet        return Integer.toString(result);
7189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
71923b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
72040e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu    private void runSetInstallLocation() {
72140e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu        int loc;
72240e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu
72340e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu        String arg = nextArg();
72440e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu        if (arg == null) {
72540e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu            System.err.println("Error: no install location specified.");
72640e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu            return;
72740e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu        }
72840e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu        try {
72940e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu            loc = Integer.parseInt(arg);
73040e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu        } catch (NumberFormatException e) {
73140e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu            System.err.println("Error: install location has to be a number.");
73240e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu            return;
73340e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu        }
73440e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu        try {
73540e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu            if (!mPm.setInstallLocation(loc)) {
73640e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu                System.err.println("Error: install location has to be a number.");
73740e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu            }
73840e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu        } catch (RemoteException e) {
73940e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu            System.err.println(e.toString());
74040e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu            System.err.println(PM_NOT_RUNNING_ERR);
74140e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu        }
74240e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu    }
74340e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu
74440e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu    private void runGetInstallLocation() {
74540e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu        try {
74640e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu            int loc = mPm.getInstallLocation();
74740e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu            String locStr = "invalid";
74840e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu            if (loc == PackageHelper.APP_INSTALL_AUTO) {
74940e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu                locStr = "auto";
75040e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu            } else if (loc == PackageHelper.APP_INSTALL_INTERNAL) {
75140e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu                locStr = "internal";
75240e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu            } else if (loc == PackageHelper.APP_INSTALL_EXTERNAL) {
75340e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu                locStr = "external";
75440e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu            }
75540e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu            System.out.println(loc + "[" + locStr + "]");
75640e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu        } catch (RemoteException e) {
75740e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu            System.err.println(e.toString());
75840e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu            System.err.println(PM_NOT_RUNNING_ERR);
75940e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu        }
76040e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu    }
76140e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu
7629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private void runInstall() {
7637767eac3232ba2fb9828766813cdb481d6a97584Dianne Hackborn        int installFlags = PackageManager.INSTALL_ALL_USERS;
764c64322c35212e919906ffd66118c7d5d3ad36636Jacek Surazski        String installerPackageName = null;
7659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
7669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        String opt;
767ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
768ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        String algo = null;
769ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        byte[] iv = null;
770ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        byte[] key = null;
771ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
772ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        String macAlgo = null;
773ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        byte[] macKey = null;
774ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        byte[] tag = null;
775706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings        String originatingUriString = null;
776706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings        String referrer = null;
777ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
7789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        while ((opt=nextOption()) != null) {
7799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (opt.equals("-l")) {
780ade3ecad94d1f4431576f53bae26c35efbf7a2c9Dianne Hackborn                installFlags |= PackageManager.INSTALL_FORWARD_LOCK;
7819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            } else if (opt.equals("-r")) {
782ade3ecad94d1f4431576f53bae26c35efbf7a2c9Dianne Hackborn                installFlags |= PackageManager.INSTALL_REPLACE_EXISTING;
783c64322c35212e919906ffd66118c7d5d3ad36636Jacek Surazski            } else if (opt.equals("-i")) {
784c64322c35212e919906ffd66118c7d5d3ad36636Jacek Surazski                installerPackageName = nextOptionData();
785c64322c35212e919906ffd66118c7d5d3ad36636Jacek Surazski                if (installerPackageName == null) {
786c64322c35212e919906ffd66118c7d5d3ad36636Jacek Surazski                    System.err.println("Error: no value specified for -i");
787c64322c35212e919906ffd66118c7d5d3ad36636Jacek Surazski                    return;
788c64322c35212e919906ffd66118c7d5d3ad36636Jacek Surazski                }
789ade3ecad94d1f4431576f53bae26c35efbf7a2c9Dianne Hackborn            } else if (opt.equals("-t")) {
790ade3ecad94d1f4431576f53bae26c35efbf7a2c9Dianne Hackborn                installFlags |= PackageManager.INSTALL_ALLOW_TEST;
791af8e9f4805643f90a9dc0ecfa119e0a860c12f8aSuchi Amalapurapu            } else if (opt.equals("-s")) {
7928946dd3355fc1dcbad872c0546e356474d4cc5deSuchi Amalapurapu                // Override if -s option is specified.
7935b993ce7bc29e43a3215a50ce6ce5d6550d4e5e2Suchi Amalapurapu                installFlags |= PackageManager.INSTALL_EXTERNAL;
79414b6abda1309631d49d4bebbb0317a7e1dfc0a50Suchi Amalapurapu            } else if (opt.equals("-f")) {
79514b6abda1309631d49d4bebbb0317a7e1dfc0a50Suchi Amalapurapu                // Override if -s option is specified.
79614b6abda1309631d49d4bebbb0317a7e1dfc0a50Suchi Amalapurapu                installFlags |= PackageManager.INSTALL_INTERNAL;
7977767eac3232ba2fb9828766813cdb481d6a97584Dianne Hackborn            } else if (opt.equals("-d")) {
7987767eac3232ba2fb9828766813cdb481d6a97584Dianne Hackborn                installFlags |= PackageManager.INSTALL_ALLOW_DOWNGRADE;
799ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            } else if (opt.equals("--algo")) {
800ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                algo = nextOptionData();
801ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                if (algo == null) {
802ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                    System.err.println("Error: must supply argument for --algo");
803ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                    return;
804ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                }
805ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            } else if (opt.equals("--iv")) {
806ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                iv = hexToBytes(nextOptionData());
807ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                if (iv == null) {
808ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                    System.err.println("Error: must supply argument for --iv");
809ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                    return;
810ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                }
811ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            } else if (opt.equals("--key")) {
812ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                key = hexToBytes(nextOptionData());
813ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                if (key == null) {
814ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                    System.err.println("Error: must supply argument for --key");
815ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                    return;
816ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                }
817ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            } else if (opt.equals("--macalgo")) {
818ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                macAlgo = nextOptionData();
819ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                if (macAlgo == null) {
820ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                    System.err.println("Error: must supply argument for --macalgo");
821ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                    return;
822ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                }
823ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            } else if (opt.equals("--mackey")) {
824ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                macKey = hexToBytes(nextOptionData());
825ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                if (macKey == null) {
826ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                    System.err.println("Error: must supply argument for --mackey");
827ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                    return;
828ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                }
829ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            } else if (opt.equals("--tag")) {
830ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                tag = hexToBytes(nextOptionData());
831ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                if (tag == null) {
832ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                    System.err.println("Error: must supply argument for --tag");
833ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                    return;
834ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                }
835706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings            } else if (opt.equals("--originating-uri")) {
836706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings                originatingUriString = nextOptionData();
837706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings                if (originatingUriString == null) {
838706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings                    System.err.println("Error: must supply argument for --originating-uri");
839706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings                    return;
840706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings                }
841706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings            } else if (opt.equals("--referrer")) {
842706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings                referrer = nextOptionData();
843706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings                if (referrer == null) {
844706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings                    System.err.println("Error: must supply argument for --referrer");
845706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings                    return;
846706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings                }
8479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            } else {
8489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                System.err.println("Error: Unknown option: " + opt);
8499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                return;
8509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
8519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
8529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
853ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        final ContainerEncryptionParams encryptionParams;
854ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        if (algo != null || iv != null || key != null || macAlgo != null || macKey != null
855ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                || tag != null) {
856ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            if (algo == null || iv == null || key == null) {
857ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                System.err.println("Error: all of --algo, --iv, and --key must be specified");
858ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                return;
859ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            }
860ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
861ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            if (macAlgo != null || macKey != null || tag != null) {
862ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                if (macAlgo == null || macKey == null || tag == null) {
863ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                    System.err.println("Error: all of --macalgo, --mackey, and --tag must "
864ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                            + "be specified");
865ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                    return;
866ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                }
867ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            }
868ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
869ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            try {
870ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                final SecretKey encKey = new SecretKeySpec(key, "RAW");
871ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
872ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                final SecretKey macSecretKey;
873ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                if (macKey == null || macKey.length == 0) {
874ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                    macSecretKey = null;
875ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                } else {
876ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                    macSecretKey = new SecretKeySpec(macKey, "RAW");
877ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                }
878ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
879ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                encryptionParams = new ContainerEncryptionParams(algo, new IvParameterSpec(iv),
880ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                        encKey, macAlgo, null, macSecretKey, tag, -1, -1, -1);
881ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            } catch (InvalidAlgorithmParameterException e) {
882ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                e.printStackTrace();
883ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                return;
884ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            }
885ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        } else {
886ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            encryptionParams = null;
887ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        }
888ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
8895ab2157bf1f105b02d3e2913cd3a33f9765b74caKenny Root        final Uri apkURI;
8905ab2157bf1f105b02d3e2913cd3a33f9765b74caKenny Root        final Uri verificationURI;
891706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings        final Uri originatingURI;
892706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings        final Uri referrerURI;
893706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings
894706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings        if (originatingUriString != null) {
895706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings            originatingURI = Uri.parse(originatingUriString);
896706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings        } else {
897706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings            originatingURI = null;
898706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings        }
899706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings
900706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings        if (referrer != null) {
901706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings            referrerURI = Uri.parse(referrer);
902706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings        } else {
903706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings            referrerURI = null;
904706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings        }
9055ab2157bf1f105b02d3e2913cd3a33f9765b74caKenny Root
9065ab2157bf1f105b02d3e2913cd3a33f9765b74caKenny Root        // Populate apkURI, must be present
9075ab2157bf1f105b02d3e2913cd3a33f9765b74caKenny Root        final String apkFilePath = nextArg();
9089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        System.err.println("\tpkg: " + apkFilePath);
9095ab2157bf1f105b02d3e2913cd3a33f9765b74caKenny Root        if (apkFilePath != null) {
9105ab2157bf1f105b02d3e2913cd3a33f9765b74caKenny Root            apkURI = Uri.fromFile(new File(apkFilePath));
9115ab2157bf1f105b02d3e2913cd3a33f9765b74caKenny Root        } else {
9129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.err.println("Error: no package specified");
9139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return;
9149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
9159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9165ab2157bf1f105b02d3e2913cd3a33f9765b74caKenny Root        // Populate verificationURI, optionally present
9175ab2157bf1f105b02d3e2913cd3a33f9765b74caKenny Root        final String verificationFilePath = nextArg();
9185ab2157bf1f105b02d3e2913cd3a33f9765b74caKenny Root        if (verificationFilePath != null) {
9195ab2157bf1f105b02d3e2913cd3a33f9765b74caKenny Root            System.err.println("\tver: " + verificationFilePath);
9205ab2157bf1f105b02d3e2913cd3a33f9765b74caKenny Root            verificationURI = Uri.fromFile(new File(verificationFilePath));
9215ab2157bf1f105b02d3e2913cd3a33f9765b74caKenny Root        } else {
9225ab2157bf1f105b02d3e2913cd3a33f9765b74caKenny Root            verificationURI = null;
9235ab2157bf1f105b02d3e2913cd3a33f9765b74caKenny Root        }
9245ab2157bf1f105b02d3e2913cd3a33f9765b74caKenny Root
9259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        PackageInstallObserver obs = new PackageInstallObserver();
9269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
927706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings            VerificationParams verificationParams = new VerificationParams(verificationURI,
928706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings                    originatingURI, referrerURI, null);
929706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings
930706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings            mPm.installPackageWithVerificationAndEncryption(apkURI, obs, installFlags,
931706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings                    installerPackageName, verificationParams, encryptionParams);
93223b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
9339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            synchronized (obs) {
9349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                while (!obs.finished) {
9359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    try {
9369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        obs.wait();
9379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    } catch (InterruptedException e) {
9389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    }
9399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
9409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (obs.result == PackageManager.INSTALL_SUCCEEDED) {
9419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    System.out.println("Success");
9429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                } else {
9439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    System.err.println("Failure ["
9449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            + installFailureToString(obs.result)
9459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                            + "]");
9469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
9479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
9489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } catch (RemoteException e) {
9499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.err.println(e.toString());
9509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.err.println(PM_NOT_RUNNING_ERR);
9519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
9529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
95323b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
954ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    /**
955ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward     * Convert a string containing hex-encoded bytes to a byte array.
956ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward     *
957ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward     * @param input String containing hex-encoded bytes
958ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward     * @return input as an array of bytes
959ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward     */
960ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    private byte[] hexToBytes(String input) {
961ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        if (input == null) {
962ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            return null;
963ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        }
964ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
965ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        final int inputLength = input.length();
966ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        if ((inputLength % 2) != 0) {
967ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            System.err.print("Invalid length; must be multiple of 2");
968ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            return null;
969ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        }
970ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
971ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        final int byteLength = inputLength / 2;
972ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        final byte[] output = new byte[byteLength];
973ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
974ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        int inputIndex = 0;
975ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        int byteIndex = 0;
976ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        while (inputIndex < inputLength) {
977ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            output[byteIndex++] = (byte) Integer.parseInt(
978ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward                    input.substring(inputIndex, inputIndex + 2), 16);
979ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            inputIndex += 2;
980ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        }
981ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
982ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        return output;
983ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    }
984ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
985483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani    public void runCreateUser() {
9860b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani        String name;
9870b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani        String arg = nextArg();
9880b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani        if (arg == null) {
9890b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani            System.err.println("Error: no user name specified.");
9900b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani            return;
9910b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani        }
9920b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani        name = arg;
9930b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani        try {
994258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani            if (mUm.createUser(name, 0) == null) {
995483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani                System.err.println("Error: couldn't create User.");
9960b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani            }
9970b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani        } catch (RemoteException e) {
9980b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani            System.err.println(e.toString());
9990b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani            System.err.println(PM_NOT_RUNNING_ERR);
10000b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani        }
10010b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani
10020b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani    }
10030b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani
1004483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani    public void runRemoveUser() {
10050b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani        int userId;
10060b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani        String arg = nextArg();
10070b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani        if (arg == null) {
10080b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani            System.err.println("Error: no user id specified.");
10090b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani            return;
10100b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani        }
10110b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani        try {
10120b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani            userId = Integer.parseInt(arg);
10130b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani        } catch (NumberFormatException e) {
1014786b44046a79d6c4c9cd07f5989d491c7196ad80Dianne Hackborn            System.err.println("Error: user id '" + arg + "' is not a number.");
10150b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani            return;
10160b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani        }
10170b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani        try {
1018258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani            if (!mUm.removeUser(userId)) {
1019786b44046a79d6c4c9cd07f5989d491c7196ad80Dianne Hackborn                System.err.println("Error: couldn't remove user #" + userId + ".");
10200b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani            }
10210b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani        } catch (RemoteException e) {
10220b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani            System.err.println(e.toString());
10230b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani            System.err.println(PM_NOT_RUNNING_ERR);
10240b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani        }
10250b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani    }
10260b285499db739ba50f2f839d633e763c70e67f96Amith Yamasani
1027483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani    public void runListUsers() {
1028742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani        try {
1029258848d2ae04f447ff1c18023fa76b139fcc0862Amith Yamasani            List<UserInfo> users = mUm.getUsers();
1030742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani            if (users == null) {
1031742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani                System.err.println("Error: couldn't get users");
1032742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani            } else {
1033742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani                System.out.println("Users:");
1034742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani                for (int i = 0; i < users.size(); i++) {
1035742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani                    System.out.println("\t" + users.get(i).toString());
1036742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani                }
1037742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani            }
1038742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani        } catch (RemoteException e) {
1039742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani            System.err.println(e.toString());
1040742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani            System.err.println(PM_NOT_RUNNING_ERR);
1041742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani        }
1042742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani    }
10439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    class PackageDeleteObserver extends IPackageDeleteObserver.Stub {
10449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        boolean finished;
10459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        boolean result;
104623b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
1047c39bb4aaa7be1b9b0cc9fb0b1f03d54f7609ffebKenny Root        public void packageDeleted(String packageName, int returnCode) {
10489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            synchronized (this) {
10499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                finished = true;
1050c39bb4aaa7be1b9b0cc9fb0b1f03d54f7609ffebKenny Root                result = returnCode == PackageManager.DELETE_SUCCEEDED;
10519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                notifyAll();
10529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
10539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
10549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
105523b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
10569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private void runUninstall() {
10579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        int unInstallFlags = 0;
10589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
10599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        String opt = nextOption();
10609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (opt != null && opt.equals("-k")) {
10617767eac3232ba2fb9828766813cdb481d6a97584Dianne Hackborn            unInstallFlags = PackageManager.DELETE_KEEP_DATA;
10629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
10639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
10649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        String pkg = nextArg();
10659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (pkg == null) {
10669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.err.println("Error: no package specified");
10679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            showUsage();
10689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return;
10699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
10709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        boolean result = deletePackage(pkg, unInstallFlags);
10719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (result) {
10729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.out.println("Success");
10739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else {
10749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.out.println("Failure");
10759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
10769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
10779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
10789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private boolean deletePackage(String pkg, int unInstallFlags) {
10799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        PackageDeleteObserver obs = new PackageDeleteObserver();
10809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
10819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mPm.deletePackage(pkg, obs, unInstallFlags);
10829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
10839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            synchronized (obs) {
10849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                while (!obs.finished) {
10859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    try {
10869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        obs.wait();
10879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    } catch (InterruptedException e) {
10889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    }
10899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                }
10909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
10919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } catch (RemoteException e) {
10929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.err.println(e.toString());
10939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.err.println(PM_NOT_RUNNING_ERR);
10949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
10959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return obs.result;
10969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
10979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1098197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn    static class ClearDataObserver extends IPackageDataObserver.Stub {
1099690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn        boolean finished;
1100690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn        boolean result;
1101690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn
1102690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn        @Override
1103690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn        public void onRemoveCompleted(String packageName, boolean succeeded) throws RemoteException {
1104690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn            synchronized (this) {
1105690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn                finished = true;
1106690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn                result = succeeded;
1107690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn                notifyAll();
1108690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn            }
1109690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn        }
1110690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn
1111690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn    }
1112690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn
1113690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn    private void runClear() {
1114690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn        String pkg = nextArg();
1115690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn        if (pkg == null) {
1116690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn            System.err.println("Error: no package specified");
1117690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn            showUsage();
1118690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn            return;
1119690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn        }
1120690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn
1121690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn        ClearDataObserver obs = new ClearDataObserver();
1122690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn        try {
1123742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani            if (!ActivityManagerNative.getDefault().clearApplicationUserData(pkg, obs,
1124742a67127366c376fdf188ff99ba30b27d3bf90cAmith Yamasani                    Binder.getOrigCallingUser())) {
1125690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn                System.err.println("Failed");
1126690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn            }
1127690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn
1128690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn            synchronized (obs) {
1129690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn                while (!obs.finished) {
1130690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn                    try {
1131690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn                        obs.wait();
1132690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn                    } catch (InterruptedException e) {
1133690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn                    }
1134690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn                }
1135690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn            }
1136690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn
1137690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn            if (obs.result) {
1138690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn                System.err.println("Success");
1139690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn            } else {
1140690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn                System.err.println("Failed");
1141690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn            }
1142690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn        } catch (RemoteException e) {
1143690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn            System.err.println(e.toString());
1144690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn            System.err.println(PM_NOT_RUNNING_ERR);
1145690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn        }
1146690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn    }
1147690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn
11489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static String enabledSettingToString(int state) {
11499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        switch (state) {
11509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            case PackageManager.COMPONENT_ENABLED_STATE_DEFAULT:
11519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                return "default";
11529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            case PackageManager.COMPONENT_ENABLED_STATE_ENABLED:
11539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                return "enabled";
11549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            case PackageManager.COMPONENT_ENABLED_STATE_DISABLED:
11559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                return "disabled";
11560ac3031c159a0904e73eb4439cdc724d8df4a6e6Dianne Hackborn            case PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER:
11570ac3031c159a0904e73eb4439cdc724d8df4a6e6Dianne Hackborn                return "disabled-user";
11589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
11599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return "unknown";
11609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
116123b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
1162483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani    private boolean isNumber(String s) {
1163483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani        try {
1164483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani            Integer.parseInt(s);
1165483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani        } catch (NumberFormatException nfe) {
1166483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani            return false;
1167483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani        }
1168483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani        return true;
1169483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani    }
1170483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani
11719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private void runSetEnabledSetting(int state) {
1172483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani        int userId = 0;
1173483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani        String option = nextOption();
1174483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani        if (option != null && option.equals("--user")) {
1175483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani            String optionData = nextOptionData();
1176483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani            if (optionData == null || !isNumber(optionData)) {
1177483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani                System.err.println("Error: no USER_ID specified");
1178483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani                showUsage();
1179483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani                return;
1180483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani            } else {
1181483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani                userId = Integer.parseInt(optionData);
1182483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani            }
1183483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani        }
1184483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani
11859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        String pkg = nextArg();
11869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (pkg == null) {
11879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.err.println("Error: no package or component specified");
11889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            showUsage();
11899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return;
11909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
11919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        ComponentName cn = ComponentName.unflattenFromString(pkg);
11929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (cn == null) {
11939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            try {
1194483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani                mPm.setApplicationEnabledSetting(pkg, state, 0, userId);
11959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                System.err.println("Package " + pkg + " new state: "
11969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        + enabledSettingToString(
1197483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani                        mPm.getApplicationEnabledSetting(pkg, userId)));
11989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            } catch (RemoteException e) {
11999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                System.err.println(e.toString());
12009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                System.err.println(PM_NOT_RUNNING_ERR);
12019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
12029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else {
12039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            try {
1204483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani                mPm.setComponentEnabledSetting(cn, state, 0, userId);
12059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                System.err.println("Component " + cn.toShortString() + " new state: "
12069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                        + enabledSettingToString(
1207483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani                        mPm.getComponentEnabledSetting(cn, userId)));
12089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            } catch (RemoteException e) {
12099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                System.err.println(e.toString());
12109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                System.err.println(PM_NOT_RUNNING_ERR);
12119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
12129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
12139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
12149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1215e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn    private void runGrantRevokePermission(boolean grant) {
1216e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn        String pkg = nextArg();
1217e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn        if (pkg == null) {
1218e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn            System.err.println("Error: no package specified");
1219e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn            showUsage();
1220e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn            return;
1221e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn        }
1222e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn        String perm = nextArg();
1223e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn        if (perm == null) {
1224e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn            System.err.println("Error: no permission specified");
1225e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn            showUsage();
1226e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn            return;
1227e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn        }
1228e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn        try {
1229e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn            if (grant) {
1230e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn                mPm.grantPermission(pkg, perm);
1231e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn            } else {
1232e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn                mPm.revokePermission(pkg, perm);
1233e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn            }
1234e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn        } catch (RemoteException e) {
1235e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn            System.err.println(e.toString());
1236e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn            System.err.println(PM_NOT_RUNNING_ERR);
1237e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn        } catch (IllegalArgumentException e) {
1238e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn            System.err.println("Bad argument: " + e.toString());
1239e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn            showUsage();
1240e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn        } catch (SecurityException e) {
1241e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn            System.err.println("Operation not allowed: " + e.toString());
1242e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn        }
1243e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn    }
1244e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn
1245719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey    private void runSetPermissionEnforced() {
1246719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey        final String permission = nextArg();
1247719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey        if (permission == null) {
1248719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey            System.err.println("Error: no permission specified");
1249719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey            showUsage();
1250719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey            return;
1251719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey        }
1252719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey        final String enforcedRaw = nextArg();
1253719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey        if (enforcedRaw == null) {
1254719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey            System.err.println("Error: no enforcement specified");
1255719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey            showUsage();
1256719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey            return;
1257719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey        }
1258719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey        final boolean enforced = Boolean.parseBoolean(enforcedRaw);
1259719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey        try {
1260719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey            mPm.setPermissionEnforced(permission, enforced);
1261719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey        } catch (RemoteException e) {
1262719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey            System.err.println(e.toString());
1263719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey            System.err.println(PM_NOT_RUNNING_ERR);
1264719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey        } catch (IllegalArgumentException e) {
1265719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey            System.err.println("Bad argument: " + e.toString());
1266719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey            showUsage();
1267719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey        } catch (SecurityException e) {
1268719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey            System.err.println("Operation not allowed: " + e.toString());
1269719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey        }
1270719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey    }
1271719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey
1272197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn    static class ClearCacheObserver extends IPackageDataObserver.Stub {
1273197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        boolean finished;
1274197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        boolean result;
1275197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn
1276197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        @Override
1277197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        public void onRemoveCompleted(String packageName, boolean succeeded) throws RemoteException {
1278197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            synchronized (this) {
1279197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn                finished = true;
1280197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn                result = succeeded;
1281197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn                notifyAll();
1282197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            }
1283197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        }
1284197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn
1285197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn    }
1286197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn
1287197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn    private void runTrimCaches() {
1288197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        String size = nextArg();
1289197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        if (size == null) {
1290197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            System.err.println("Error: no size specified");
1291197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            showUsage();
1292197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            return;
1293197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        }
1294197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        int len = size.length();
1295197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        long multiplier = 1;
1296197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        if (len > 1) {
1297197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            char c = size.charAt(len-1);
1298197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            if (c == 'K' || c == 'k') {
1299197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn                multiplier = 1024L;
1300197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            } else if (c == 'M' || c == 'm') {
1301197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn                multiplier = 1024L*1024L;
1302197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            } else if (c == 'G' || c == 'g') {
1303197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn                multiplier = 1024L*1024L*1024L;
1304197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            } else {
1305197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn                System.err.println("Invalid suffix: " + c);
1306197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn                showUsage();
1307197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn                return;
1308197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            }
1309197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            size = size.substring(0, len-1);
1310197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        }
1311197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        long sizeVal;
1312197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        try {
1313197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            sizeVal = Long.parseLong(size) * multiplier;
1314197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        } catch (NumberFormatException e) {
1315197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            System.err.println("Error: expected number at: " + size);
1316197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            showUsage();
1317197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            return;
1318197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        }
1319197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        ClearDataObserver obs = new ClearDataObserver();
1320197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        try {
1321197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            mPm.freeStorageAndNotify(sizeVal, obs);
1322197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            synchronized (obs) {
1323197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn                while (!obs.finished) {
1324197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn                    try {
1325197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn                        obs.wait();
1326197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn                    } catch (InterruptedException e) {
1327197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn                    }
1328197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn                }
1329197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            }
1330197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        } catch (RemoteException e) {
1331197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            System.err.println(e.toString());
1332197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            System.err.println(PM_NOT_RUNNING_ERR);
1333197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        } catch (IllegalArgumentException e) {
1334197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            System.err.println("Bad argument: " + e.toString());
1335197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            showUsage();
1336197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        } catch (SecurityException e) {
1337197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn            System.err.println("Operation not allowed: " + e.toString());
1338197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        }
1339197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn    }
1340197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn
13419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
13429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Displays the package file for a package.
13439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param pckg
13449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
13459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private void displayPackageFilePath(String pckg) {
13469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
1347483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani            PackageInfo info = mPm.getPackageInfo(pckg, 0, 0);
13489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (info != null && info.applicationInfo != null) {
13499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                System.out.print("package:");
13509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                System.out.println(info.applicationInfo.sourceDir);
13519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
13529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } catch (RemoteException e) {
13539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.err.println(e.toString());
13549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.err.println(PM_NOT_RUNNING_ERR);
13559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
13569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
135723b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
13589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private Resources getResources(PackageItemInfo pii) {
13599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        Resources res = mResourceCache.get(pii.packageName);
13609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (res != null) return res;
136123b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
13629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
1363483f3b06ea84440a082e21b68ec2c2e54046f5a6Amith Yamasani            ApplicationInfo ai = mPm.getApplicationInfo(pii.packageName, 0, 0);
13649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            AssetManager am = new AssetManager();
13659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            am.addAssetPath(ai.publicSourceDir);
13669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            res = new Resources(am, null, null);
13679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            mResourceCache.put(pii.packageName, res);
13689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return res;
13699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } catch (RemoteException e) {
13709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.err.println(e.toString());
13719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            System.err.println(PM_NOT_RUNNING_ERR);
13729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return null;
13739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
13749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
137523b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet
13769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private String nextOption() {
13779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (mNextArg >= mArgs.length) {
13789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return null;
13799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
13809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        String arg = mArgs[mNextArg];
13819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (!arg.startsWith("-")) {
13829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return null;
13839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
13849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mNextArg++;
13859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (arg.equals("--")) {
13869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return null;
13879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
13889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (arg.length() > 1 && arg.charAt(1) != '-') {
13899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (arg.length() > 2) {
13909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                mCurArgData = arg.substring(2);
13919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                return arg.substring(0, 2);
13929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            } else {
13939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                mCurArgData = null;
13949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                return arg;
13959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
13969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
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    private String nextOptionData() {
14029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (mCurArgData != null) {
14039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return mCurArgData;
14049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
14059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (mNextArg >= mArgs.length) {
14069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return null;
14079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
14089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        String data = mArgs[mNextArg];
14099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mNextArg++;
14109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return data;
14119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
14129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
14139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private String nextArg() {
14149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (mNextArg >= mArgs.length) {
14159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return null;
14169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
14179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        String arg = mArgs[mNextArg];
14189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        mNextArg++;
14199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return arg;
14209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
14219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
14229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static void showUsage() {
142392a208c6d3e0fdae54bbc942e7e041e48812bcaaAshish Sharma        System.err.println("usage: pm list packages [-f] [-d] [-e] [-s] [-3] [-i] [-u] [FILTER]");
14249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        System.err.println("       pm list permission-groups");
14259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        System.err.println("       pm list permissions [-g] [-f] [-d] [-u] [GROUP]");
142623b4faf69437b732d681d9bee5c9a84379d5ccadXavier Ducrohet        System.err.println("       pm list instrumentation [-f] [TARGET-PACKAGE]");
1427039c68e75606e837cf021815a0210836724574adDianne Hackborn        System.err.println("       pm list features");
1428e4a5951925f16f18dae91ed65567e96528f17feeDianne Hackborn        System.err.println("       pm list libraries");
14297e0e19d4201374d72b7082492bd269dbdb17d972Dianne Hackborn        System.err.println("       pm list users");
14309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        System.err.println("       pm path PACKAGE");
1431ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        System.err.println("       pm install [-l] [-r] [-t] [-i INSTALLER_PACKAGE_NAME] [-s] [-f]");
1432706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings        System.err.println("                  [--algo <algorithm name> --key <key-in-hex> --iv <IV-in-hex>]");
1433706e8ba26bf0de19ad5f736516dae40c4c88c2d7rich cannings        System.err.println("                  [--originating-uri <URI>] [--referrer <URI>] PATH");
14349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        System.err.println("       pm uninstall [-k] PACKAGE");
1435690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn        System.err.println("       pm clear PACKAGE");
14367e0e19d4201374d72b7082492bd269dbdb17d972Dianne Hackborn        System.err.println("       pm enable [--user USER_ID] PACKAGE_OR_COMPONENT");
14377e0e19d4201374d72b7082492bd269dbdb17d972Dianne Hackborn        System.err.println("       pm disable [--user USER_ID] PACKAGE_OR_COMPONENT");
14387e0e19d4201374d72b7082492bd269dbdb17d972Dianne Hackborn        System.err.println("       pm disable-user [--user USER_ID] PACKAGE_OR_COMPONENT");
1439e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn        System.err.println("       pm grant PACKAGE PERMISSION");
1440e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn        System.err.println("       pm revoke PACKAGE PERMISSION");
144146d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("       pm set-install-location [0/auto] [1/internal] [2/external]");
144246d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("       pm get-install-location");
1443719a6320a789e76566d6416d5ec35491d21f5c44Jeff Sharkey        System.err.println("       pm set-permission-enforced PERMISSION [true|false]");
1444197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        System.err.println("       pm trim-caches DESIRED_FREE_SPACE");
14457e0e19d4201374d72b7082492bd269dbdb17d972Dianne Hackborn        System.err.println("       pm create-user USER_NAME");
14467e0e19d4201374d72b7082492bd269dbdb17d972Dianne Hackborn        System.err.println("       pm remove-user USER_ID");
14479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        System.err.println("");
144846d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("pm list packages: prints all packages, optionally only");
144946d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("  those whose package name contains the text in FILTER.  Options:");
145046d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("    -f: see their associated file.");
145146d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("    -d: filter to only show disbled packages.");
145246d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("    -e: filter to only show enabled packages.");
145346d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("    -s: filter to only show system packages.");
145446d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("    -3: filter to only show third party packages.");
145592a208c6d3e0fdae54bbc942e7e041e48812bcaaAshish Sharma        System.err.println("    -i: see the installer for the packages.");
145646d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("    -u: also include uninstalled packages.");
14579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        System.err.println("");
145846d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("pm list permission-groups: prints all known permission groups.");
14599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        System.err.println("");
146046d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("pm list permissions: prints all known permissions, optionally only");
146146d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("  those in GROUP.  Options:");
146246d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("    -g: organize by group.");
146346d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("    -f: print all information.");
146446d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("    -s: short summary.");
146546d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("    -d: only list dangerous permissions.");
146646d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("    -u: list only the permissions users will see.");
14679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        System.err.println("");
146846d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("pm list instrumentation: use to list all test packages; optionally");
146946d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("  supply <TARGET-PACKAGE> to list the test packages for a particular");
147046d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("  application.  Options:");
147146d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("    -f: list the .apk file for the test package.");
14729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        System.err.println("");
147346d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("pm list features: prints all features of the system.");
1474039c68e75606e837cf021815a0210836724574adDianne Hackborn        System.err.println("");
1475786b44046a79d6c4c9cd07f5989d491c7196ad80Dianne Hackborn        System.err.println("pm list users: prints all users on the system.");
1476786b44046a79d6c4c9cd07f5989d491c7196ad80Dianne Hackborn        System.err.println("");
147746d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("pm path: print the path to the .apk of the given PACKAGE.");
14789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        System.err.println("");
147946d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("pm install: installs a package to the system.  Options:");
148046d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("    -l: install the package with FORWARD_LOCK.");
148146d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("    -r: reinstall an exisiting app, keeping its data.");
148246d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("    -t: allow test .apks to be installed.");
148346d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("    -i: specify the installer package name.");
148446d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("    -s: install package on sdcard.");
148546d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("    -f: install package on internal flash.");
14867767eac3232ba2fb9828766813cdb481d6a97584Dianne Hackborn        System.err.println("    -d: allow version code downgrade.");
14879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        System.err.println("");
148846d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("pm uninstall: removes a package from the system. Options:");
148946d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("    -k: keep the data and cache directories around after package removal.");
14909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        System.err.println("");
149146d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("pm clear: deletes all data associated with a package.");
1492690d20bb53ba2485f59d128b365eff991d5cc3e6Dianne Hackborn        System.err.println("");
149346d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("pm enable, disable, disable-user: these commands change the enabled state");
149446d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("  of a given package or component (written as \"package/class\").");
149540e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu        System.err.println("");
1496e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn        System.err.println("pm grant, revoke: these commands either grant or revoke permissions");
1497e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn        System.err.println("  to applications.  Only optional permissions the application has");
1498e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn        System.err.println("  declared can be granted or revoked.");
1499e639da7baa23121e35aa06d6e182558e0e755696Dianne Hackborn        System.err.println("");
150046d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("pm get-install-location: returns the current install location.");
150146d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("    0 [auto]: Let system decide the best location");
150246d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("    1 [internal]: Install on internal device storage");
150346d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("    2 [external]: Install on external media");
150440e472521a544f26cb6956995788f7c36fff1404Suchi Amalapurapu        System.err.println("");
150546d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("pm set-install-location: changes the default install location.");
150646d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("  NOTE: this is only intended for debugging; using this can cause");
150746d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("  applications to break and other undersireable behavior.");
150846d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("    0 [auto]: Let system decide the best location");
150946d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("    1 [internal]: Install on internal device storage");
151046d110329e659cc9cb9514e220ce273701eb151dDianne Hackborn        System.err.println("    2 [external]: Install on external media");
1511197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        System.err.println("");
1512197a0c82a1fbf337ec0a85d36b6b89c3d6e8a0acDianne Hackborn        System.err.println("pm trim-caches: trim cache files to reach the given free space.");
1513786b44046a79d6c4c9cd07f5989d491c7196ad80Dianne Hackborn        System.err.println("");
1514786b44046a79d6c4c9cd07f5989d491c7196ad80Dianne Hackborn        System.err.println("pm create-user: create a new user with the given USER_NAME,");
1515786b44046a79d6c4c9cd07f5989d491c7196ad80Dianne Hackborn        System.err.println("  printing the new user identifier of the user.");
1516786b44046a79d6c4c9cd07f5989d491c7196ad80Dianne Hackborn        System.err.println("");
1517786b44046a79d6c4c9cd07f5989d491c7196ad80Dianne Hackborn        System.err.println("pm remove-user: remove the user with the given USER_IDENTIFIER,");
1518786b44046a79d6c4c9cd07f5989d491c7196ad80Dianne Hackborn        System.err.println("  deleting all data associated with that user");
15199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
15209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1521