Pm.java revision 690d20bb53ba2485f59d128b365eff991d5cc3e6
1/* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.commands.pm; 18 19import com.android.internal.content.PackageHelper; 20 21import android.app.ActivityManagerNative; 22import android.content.ComponentName; 23import android.content.pm.ApplicationInfo; 24import android.content.pm.FeatureInfo; 25import android.content.pm.IPackageDataObserver; 26import android.content.pm.IPackageDeleteObserver; 27import android.content.pm.IPackageInstallObserver; 28import android.content.pm.IPackageManager; 29import android.content.pm.InstrumentationInfo; 30import android.content.pm.PackageInfo; 31import android.content.pm.PackageItemInfo; 32import android.content.pm.PackageManager; 33import android.content.pm.PermissionGroupInfo; 34import android.content.pm.PermissionInfo; 35import android.content.res.AssetManager; 36import android.content.res.Resources; 37import android.net.Uri; 38import android.os.RemoteException; 39import android.os.ServiceManager; 40import android.provider.Settings; 41 42import java.io.File; 43import java.lang.reflect.Field; 44import java.lang.reflect.Modifier; 45import java.util.ArrayList; 46import java.util.Collections; 47import java.util.Comparator; 48import java.util.List; 49import java.util.WeakHashMap; 50 51public final class Pm { 52 IPackageManager mPm; 53 54 private WeakHashMap<String, Resources> mResourceCache 55 = new WeakHashMap<String, Resources>(); 56 57 private String[] mArgs; 58 private int mNextArg; 59 private String mCurArgData; 60 61 private static final String PM_NOT_RUNNING_ERR = 62 "Error: Could not access the Package Manager. Is the system running?"; 63 64 public static void main(String[] args) { 65 new Pm().run(args); 66 } 67 68 public void run(String[] args) { 69 boolean validCommand = false; 70 if (args.length < 1) { 71 showUsage(); 72 return; 73 } 74 75 mPm = IPackageManager.Stub.asInterface(ServiceManager.getService("package")); 76 if (mPm == null) { 77 System.err.println(PM_NOT_RUNNING_ERR); 78 return; 79 } 80 81 mArgs = args; 82 String op = args[0]; 83 mNextArg = 1; 84 85 if ("list".equals(op)) { 86 runList(); 87 return; 88 } 89 90 if ("path".equals(op)) { 91 runPath(); 92 return; 93 } 94 95 if ("install".equals(op)) { 96 runInstall(); 97 return; 98 } 99 100 if ("uninstall".equals(op)) { 101 runUninstall(); 102 return; 103 } 104 105 if ("clear".equals(op)) { 106 runClear(); 107 return; 108 } 109 110 if ("enable".equals(op)) { 111 runSetEnabledSetting(PackageManager.COMPONENT_ENABLED_STATE_ENABLED); 112 return; 113 } 114 115 if ("disable".equals(op)) { 116 runSetEnabledSetting(PackageManager.COMPONENT_ENABLED_STATE_DISABLED); 117 return; 118 } 119 120 if ("setInstallLocation".equals(op)) { 121 runSetInstallLocation(); 122 return; 123 } 124 125 if ("getInstallLocation".equals(op)) { 126 runGetInstallLocation(); 127 return; 128 } 129 130 try { 131 if (args.length == 1) { 132 if (args[0].equalsIgnoreCase("-l")) { 133 validCommand = true; 134 runListPackages(false); 135 } else if (args[0].equalsIgnoreCase("-lf")){ 136 validCommand = true; 137 runListPackages(true); 138 } 139 } else if (args.length == 2) { 140 if (args[0].equalsIgnoreCase("-p")) { 141 validCommand = true; 142 displayPackageFilePath(args[1]); 143 } 144 } 145 } finally { 146 if (validCommand == false) { 147 if (op != null) { 148 System.err.println("Error: unknown command '" + op + "'"); 149 } 150 showUsage(); 151 } 152 } 153 } 154 155 /** 156 * Execute the list sub-command. 157 * 158 * pm list [package | packages] 159 * pm list permission-groups 160 * pm list permissions 161 * pm list features 162 * pm list libraries 163 * pm list instrumentation 164 */ 165 private void runList() { 166 String type = nextArg(); 167 if (type == null) { 168 System.err.println("Error: didn't specify type of data to list"); 169 showUsage(); 170 return; 171 } 172 if ("package".equals(type) || "packages".equals(type)) { 173 runListPackages(false); 174 } else if ("permission-groups".equals(type)) { 175 runListPermissionGroups(); 176 } else if ("permissions".equals(type)) { 177 runListPermissions(); 178 } else if ("features".equals(type)) { 179 runListFeatures(); 180 } else if ("libraries".equals(type)) { 181 runListLibraries(); 182 } else if ("instrumentation".equals(type)) { 183 runListInstrumentation(); 184 } else { 185 System.err.println("Error: unknown list type '" + type + "'"); 186 showUsage(); 187 } 188 } 189 190 /** 191 * Lists all the installed packages. 192 */ 193 private void runListPackages(boolean showApplicationPackage) { 194 int getFlags = 0; 195 boolean listDisabled = false, listEnabled = false; 196 try { 197 String opt; 198 while ((opt=nextOption()) != null) { 199 if (opt.equals("-l")) { 200 // old compat 201 } else if (opt.equals("-lf")) { 202 showApplicationPackage = true; 203 } else if (opt.equals("-f")) { 204 showApplicationPackage = true; 205 } else if (opt.equals("-d")) { 206 listDisabled = true; 207 } else if (opt.equals("-e")) { 208 listEnabled = true; 209 } else if (opt.equals("-u")) { 210 getFlags |= PackageManager.GET_UNINSTALLED_PACKAGES; 211 } else { 212 System.err.println("Error: Unknown option: " + opt); 213 showUsage(); 214 return; 215 } 216 } 217 } catch (RuntimeException ex) { 218 System.err.println("Error: " + ex.toString()); 219 showUsage(); 220 return; 221 } 222 223 String filter = nextArg(); 224 225 try { 226 List<PackageInfo> packages = mPm.getInstalledPackages(getFlags); 227 228 int count = packages.size(); 229 for (int p = 0 ; p < count ; p++) { 230 PackageInfo info = packages.get(p); 231 if (filter != null && !info.packageName.contains(filter)) { 232 continue; 233 } 234 if ((!listDisabled || !info.applicationInfo.enabled) && 235 (!listEnabled || info.applicationInfo.enabled)) { 236 System.out.print("package:"); 237 if (showApplicationPackage) { 238 System.out.print(info.applicationInfo.sourceDir); 239 System.out.print("="); 240 } 241 System.out.println(info.packageName); 242 } 243 } 244 } catch (RemoteException e) { 245 System.err.println(e.toString()); 246 System.err.println(PM_NOT_RUNNING_ERR); 247 } 248 } 249 250 /** 251 * Lists all of the features supported by the current device. 252 * 253 * pm list features 254 */ 255 private void runListFeatures() { 256 try { 257 List<FeatureInfo> list = new ArrayList<FeatureInfo>(); 258 FeatureInfo[] rawList = mPm.getSystemAvailableFeatures(); 259 for (int i=0; i<rawList.length; i++) { 260 list.add(rawList[i]); 261 } 262 263 264 // Sort by name 265 Collections.sort(list, new Comparator<FeatureInfo>() { 266 public int compare(FeatureInfo o1, FeatureInfo o2) { 267 if (o1.name == o2.name) return 0; 268 if (o1.name == null) return -1; 269 if (o2.name == null) return 1; 270 return o1.name.compareTo(o2.name); 271 } 272 }); 273 274 int count = (list != null) ? list.size() : 0; 275 for (int p = 0; p < count; p++) { 276 FeatureInfo fi = list.get(p); 277 System.out.print("feature:"); 278 if (fi.name != null) System.out.println(fi.name); 279 else System.out.println("reqGlEsVersion=0x" 280 + Integer.toHexString(fi.reqGlEsVersion)); 281 } 282 } catch (RemoteException e) { 283 System.err.println(e.toString()); 284 System.err.println(PM_NOT_RUNNING_ERR); 285 } 286 } 287 288 /** 289 * Lists all of the libraries supported by the current device. 290 * 291 * pm list libraries 292 */ 293 private void runListLibraries() { 294 try { 295 List<String> list = new ArrayList<String>(); 296 String[] rawList = mPm.getSystemSharedLibraryNames(); 297 for (int i=0; i<rawList.length; i++) { 298 list.add(rawList[i]); 299 } 300 301 302 // Sort by name 303 Collections.sort(list, new Comparator<String>() { 304 public int compare(String o1, String o2) { 305 if (o1 == o2) return 0; 306 if (o1 == null) return -1; 307 if (o2 == null) return 1; 308 return o1.compareTo(o2); 309 } 310 }); 311 312 int count = (list != null) ? list.size() : 0; 313 for (int p = 0; p < count; p++) { 314 String lib = list.get(p); 315 System.out.print("library:"); 316 System.out.println(lib); 317 } 318 } catch (RemoteException e) { 319 System.err.println(e.toString()); 320 System.err.println(PM_NOT_RUNNING_ERR); 321 } 322 } 323 324 /** 325 * Lists all of the installed instrumentation, or all for a given package 326 * 327 * pm list instrumentation [package] [-f] 328 */ 329 private void runListInstrumentation() { 330 int flags = 0; // flags != 0 is only used to request meta-data 331 boolean showPackage = false; 332 String targetPackage = null; 333 334 try { 335 String opt; 336 while ((opt=nextArg()) != null) { 337 if (opt.equals("-f")) { 338 showPackage = true; 339 } else if (opt.charAt(0) != '-') { 340 targetPackage = opt; 341 } else { 342 System.err.println("Error: Unknown option: " + opt); 343 showUsage(); 344 return; 345 } 346 } 347 } catch (RuntimeException ex) { 348 System.err.println("Error: " + ex.toString()); 349 showUsage(); 350 return; 351 } 352 353 try { 354 List<InstrumentationInfo> list = mPm.queryInstrumentation(targetPackage, flags); 355 356 // Sort by target package 357 Collections.sort(list, new Comparator<InstrumentationInfo>() { 358 public int compare(InstrumentationInfo o1, InstrumentationInfo o2) { 359 return o1.targetPackage.compareTo(o2.targetPackage); 360 } 361 }); 362 363 int count = (list != null) ? list.size() : 0; 364 for (int p = 0; p < count; p++) { 365 InstrumentationInfo ii = list.get(p); 366 System.out.print("instrumentation:"); 367 if (showPackage) { 368 System.out.print(ii.sourceDir); 369 System.out.print("="); 370 } 371 ComponentName cn = new ComponentName(ii.packageName, ii.name); 372 System.out.print(cn.flattenToShortString()); 373 System.out.print(" (target="); 374 System.out.print(ii.targetPackage); 375 System.out.println(")"); 376 } 377 } catch (RemoteException e) { 378 System.err.println(e.toString()); 379 System.err.println(PM_NOT_RUNNING_ERR); 380 } 381 } 382 383 /** 384 * Lists all the known permission groups. 385 */ 386 private void runListPermissionGroups() { 387 try { 388 List<PermissionGroupInfo> pgs = mPm.getAllPermissionGroups(0); 389 390 int count = pgs.size(); 391 for (int p = 0 ; p < count ; p++) { 392 PermissionGroupInfo pgi = pgs.get(p); 393 System.out.print("permission group:"); 394 System.out.println(pgi.name); 395 } 396 } catch (RemoteException e) { 397 System.err.println(e.toString()); 398 System.err.println(PM_NOT_RUNNING_ERR); 399 } 400 } 401 402 private String loadText(PackageItemInfo pii, int res, CharSequence nonLocalized) { 403 if (nonLocalized != null) { 404 return nonLocalized.toString(); 405 } 406 Resources r = getResources(pii); 407 if (r != null) { 408 return r.getString(res); 409 } 410 return null; 411 } 412 413 /** 414 * Lists all the permissions in a group. 415 */ 416 private void runListPermissions() { 417 try { 418 boolean labels = false; 419 boolean groups = false; 420 boolean userOnly = false; 421 boolean summary = false; 422 boolean dangerousOnly = false; 423 String opt; 424 while ((opt=nextOption()) != null) { 425 if (opt.equals("-f")) { 426 labels = true; 427 } else if (opt.equals("-g")) { 428 groups = true; 429 } else if (opt.equals("-s")) { 430 groups = true; 431 labels = true; 432 summary = true; 433 } else if (opt.equals("-u")) { 434 userOnly = true; 435 } else if (opt.equals("-d")) { 436 dangerousOnly = true; 437 } else { 438 System.err.println("Error: Unknown option: " + opt); 439 showUsage(); 440 return; 441 } 442 } 443 444 String grp = nextOption(); 445 ArrayList<String> groupList = new ArrayList<String>(); 446 if (groups) { 447 List<PermissionGroupInfo> infos = 448 mPm.getAllPermissionGroups(0); 449 for (int i=0; i<infos.size(); i++) { 450 groupList.add(infos.get(i).name); 451 } 452 groupList.add(null); 453 } else { 454 groupList.add(grp); 455 } 456 457 if (dangerousOnly) { 458 System.out.println("Dangerous Permissions:"); 459 System.out.println(""); 460 doListPermissions(groupList, groups, labels, summary, 461 PermissionInfo.PROTECTION_DANGEROUS, 462 PermissionInfo.PROTECTION_DANGEROUS); 463 if (userOnly) { 464 System.out.println("Normal Permissions:"); 465 System.out.println(""); 466 doListPermissions(groupList, groups, labels, summary, 467 PermissionInfo.PROTECTION_NORMAL, 468 PermissionInfo.PROTECTION_NORMAL); 469 } 470 } else if (userOnly) { 471 System.out.println("Dangerous and Normal Permissions:"); 472 System.out.println(""); 473 doListPermissions(groupList, groups, labels, summary, 474 PermissionInfo.PROTECTION_NORMAL, 475 PermissionInfo.PROTECTION_DANGEROUS); 476 } else { 477 System.out.println("All Permissions:"); 478 System.out.println(""); 479 doListPermissions(groupList, groups, labels, summary, 480 -10000, 10000); 481 } 482 } catch (RemoteException e) { 483 System.err.println(e.toString()); 484 System.err.println(PM_NOT_RUNNING_ERR); 485 } 486 } 487 488 private void doListPermissions(ArrayList<String> groupList, 489 boolean groups, boolean labels, boolean summary, 490 int startProtectionLevel, int endProtectionLevel) 491 throws RemoteException { 492 for (int i=0; i<groupList.size(); i++) { 493 String groupName = groupList.get(i); 494 String prefix = ""; 495 if (groups) { 496 if (i > 0) System.out.println(""); 497 if (groupName != null) { 498 PermissionGroupInfo pgi = mPm.getPermissionGroupInfo( 499 groupName, 0); 500 if (summary) { 501 Resources res = getResources(pgi); 502 if (res != null) { 503 System.out.print(loadText(pgi, pgi.labelRes, 504 pgi.nonLocalizedLabel) + ": "); 505 } else { 506 System.out.print(pgi.name + ": "); 507 508 } 509 } else { 510 System.out.println((labels ? "+ " : "") 511 + "group:" + pgi.name); 512 if (labels) { 513 System.out.println(" package:" + pgi.packageName); 514 Resources res = getResources(pgi); 515 if (res != null) { 516 System.out.println(" label:" 517 + loadText(pgi, pgi.labelRes, 518 pgi.nonLocalizedLabel)); 519 System.out.println(" description:" 520 + loadText(pgi, pgi.descriptionRes, 521 pgi.nonLocalizedDescription)); 522 } 523 } 524 } 525 } else { 526 System.out.println(((labels && !summary) 527 ? "+ " : "") + "ungrouped:"); 528 } 529 prefix = " "; 530 } 531 List<PermissionInfo> ps = mPm.queryPermissionsByGroup( 532 groupList.get(i), 0); 533 int count = ps.size(); 534 boolean first = true; 535 for (int p = 0 ; p < count ; p++) { 536 PermissionInfo pi = ps.get(p); 537 if (groups && groupName == null && pi.group != null) { 538 continue; 539 } 540 if (pi.protectionLevel < startProtectionLevel 541 || pi.protectionLevel > endProtectionLevel) { 542 continue; 543 } 544 if (summary) { 545 if (first) { 546 first = false; 547 } else { 548 System.out.print(", "); 549 } 550 Resources res = getResources(pi); 551 if (res != null) { 552 System.out.print(loadText(pi, pi.labelRes, 553 pi.nonLocalizedLabel)); 554 } else { 555 System.out.print(pi.name); 556 } 557 } else { 558 System.out.println(prefix + (labels ? "+ " : "") 559 + "permission:" + pi.name); 560 if (labels) { 561 System.out.println(prefix + " package:" + pi.packageName); 562 Resources res = getResources(pi); 563 if (res != null) { 564 System.out.println(prefix + " label:" 565 + loadText(pi, pi.labelRes, 566 pi.nonLocalizedLabel)); 567 System.out.println(prefix + " description:" 568 + loadText(pi, pi.descriptionRes, 569 pi.nonLocalizedDescription)); 570 } 571 String protLevel = "unknown"; 572 switch(pi.protectionLevel) { 573 case PermissionInfo.PROTECTION_DANGEROUS: 574 protLevel = "dangerous"; 575 break; 576 case PermissionInfo.PROTECTION_NORMAL: 577 protLevel = "normal"; 578 break; 579 case PermissionInfo.PROTECTION_SIGNATURE: 580 protLevel = "signature"; 581 break; 582 case PermissionInfo.PROTECTION_SIGNATURE_OR_SYSTEM: 583 protLevel = "signatureOrSystem"; 584 break; 585 } 586 System.out.println(prefix + " protectionLevel:" + protLevel); 587 } 588 } 589 } 590 591 if (summary) { 592 System.out.println(""); 593 } 594 } 595 } 596 597 private void runPath() { 598 String pkg = nextArg(); 599 if (pkg == null) { 600 System.err.println("Error: no package specified"); 601 showUsage(); 602 return; 603 } 604 displayPackageFilePath(pkg); 605 } 606 607 class PackageInstallObserver extends IPackageInstallObserver.Stub { 608 boolean finished; 609 int result; 610 611 public void packageInstalled(String name, int status) { 612 synchronized( this) { 613 finished = true; 614 result = status; 615 notifyAll(); 616 } 617 } 618 } 619 620 /** 621 * Converts a failure code into a string by using reflection to find a matching constant 622 * in PackageManager. 623 */ 624 private String installFailureToString(int result) { 625 Field[] fields = PackageManager.class.getFields(); 626 for (Field f: fields) { 627 if (f.getType() == int.class) { 628 int modifiers = f.getModifiers(); 629 // only look at public final static fields. 630 if (((modifiers & Modifier.FINAL) != 0) && 631 ((modifiers & Modifier.PUBLIC) != 0) && 632 ((modifiers & Modifier.STATIC) != 0)) { 633 String fieldName = f.getName(); 634 if (fieldName.startsWith("INSTALL_FAILED_") || 635 fieldName.startsWith("INSTALL_PARSE_FAILED_")) { 636 // get the int value and compare it to result. 637 try { 638 if (result == f.getInt(null)) { 639 return fieldName; 640 } 641 } catch (IllegalAccessException e) { 642 // this shouldn't happen since we only look for public static fields. 643 } 644 } 645 } 646 } 647 } 648 649 // couldn't find a matching constant? return the value 650 return Integer.toString(result); 651 } 652 653 private void runSetInstallLocation() { 654 int loc; 655 656 String arg = nextArg(); 657 if (arg == null) { 658 System.err.println("Error: no install location specified."); 659 showUsage(); 660 return; 661 } 662 try { 663 loc = Integer.parseInt(arg); 664 } catch (NumberFormatException e) { 665 System.err.println("Error: install location has to be a number."); 666 showUsage(); 667 return; 668 } 669 try { 670 if (!mPm.setInstallLocation(loc)) { 671 System.err.println("Error: install location has to be a number."); 672 showUsage(); 673 } 674 } catch (RemoteException e) { 675 System.err.println(e.toString()); 676 System.err.println(PM_NOT_RUNNING_ERR); 677 } 678 } 679 680 private void runGetInstallLocation() { 681 try { 682 int loc = mPm.getInstallLocation(); 683 String locStr = "invalid"; 684 if (loc == PackageHelper.APP_INSTALL_AUTO) { 685 locStr = "auto"; 686 } else if (loc == PackageHelper.APP_INSTALL_INTERNAL) { 687 locStr = "internal"; 688 } else if (loc == PackageHelper.APP_INSTALL_EXTERNAL) { 689 locStr = "external"; 690 } 691 System.out.println(loc + "[" + locStr + "]"); 692 } catch (RemoteException e) { 693 System.err.println(e.toString()); 694 System.err.println(PM_NOT_RUNNING_ERR); 695 } 696 } 697 698 private void runInstall() { 699 int installFlags = 0; 700 String installerPackageName = null; 701 702 String opt; 703 while ((opt=nextOption()) != null) { 704 if (opt.equals("-l")) { 705 installFlags |= PackageManager.INSTALL_FORWARD_LOCK; 706 } else if (opt.equals("-r")) { 707 installFlags |= PackageManager.INSTALL_REPLACE_EXISTING; 708 } else if (opt.equals("-i")) { 709 installerPackageName = nextOptionData(); 710 if (installerPackageName == null) { 711 System.err.println("Error: no value specified for -i"); 712 showUsage(); 713 return; 714 } 715 } else if (opt.equals("-t")) { 716 installFlags |= PackageManager.INSTALL_ALLOW_TEST; 717 } else if (opt.equals("-s")) { 718 // Override if -s option is specified. 719 installFlags |= PackageManager.INSTALL_EXTERNAL; 720 } else if (opt.equals("-f")) { 721 // Override if -s option is specified. 722 installFlags |= PackageManager.INSTALL_INTERNAL; 723 } else { 724 System.err.println("Error: Unknown option: " + opt); 725 showUsage(); 726 return; 727 } 728 } 729 730 String apkFilePath = nextArg(); 731 System.err.println("\tpkg: " + apkFilePath); 732 if (apkFilePath == null) { 733 System.err.println("Error: no package specified"); 734 showUsage(); 735 return; 736 } 737 738 PackageInstallObserver obs = new PackageInstallObserver(); 739 try { 740 mPm.installPackage(Uri.fromFile(new File(apkFilePath)), obs, installFlags, 741 installerPackageName); 742 743 synchronized (obs) { 744 while (!obs.finished) { 745 try { 746 obs.wait(); 747 } catch (InterruptedException e) { 748 } 749 } 750 if (obs.result == PackageManager.INSTALL_SUCCEEDED) { 751 System.out.println("Success"); 752 } else { 753 System.err.println("Failure [" 754 + installFailureToString(obs.result) 755 + "]"); 756 } 757 } 758 } catch (RemoteException e) { 759 System.err.println(e.toString()); 760 System.err.println(PM_NOT_RUNNING_ERR); 761 } 762 } 763 764 class PackageDeleteObserver extends IPackageDeleteObserver.Stub { 765 boolean finished; 766 boolean result; 767 768 public void packageDeleted(boolean succeeded) { 769 synchronized (this) { 770 finished = true; 771 result = succeeded; 772 notifyAll(); 773 } 774 } 775 } 776 777 private void runUninstall() { 778 int unInstallFlags = 0; 779 780 String opt = nextOption(); 781 if (opt != null && opt.equals("-k")) { 782 unInstallFlags = PackageManager.DONT_DELETE_DATA; 783 } 784 785 String pkg = nextArg(); 786 if (pkg == null) { 787 System.err.println("Error: no package specified"); 788 showUsage(); 789 return; 790 } 791 boolean result = deletePackage(pkg, unInstallFlags); 792 if (result) { 793 System.out.println("Success"); 794 } else { 795 System.out.println("Failure"); 796 } 797 } 798 799 private boolean deletePackage(String pkg, int unInstallFlags) { 800 PackageDeleteObserver obs = new PackageDeleteObserver(); 801 try { 802 mPm.deletePackage(pkg, obs, unInstallFlags); 803 804 synchronized (obs) { 805 while (!obs.finished) { 806 try { 807 obs.wait(); 808 } catch (InterruptedException e) { 809 } 810 } 811 } 812 } catch (RemoteException e) { 813 System.err.println(e.toString()); 814 System.err.println(PM_NOT_RUNNING_ERR); 815 } 816 return obs.result; 817 } 818 819 class ClearDataObserver extends IPackageDataObserver.Stub { 820 boolean finished; 821 boolean result; 822 823 @Override 824 public void onRemoveCompleted(String packageName, boolean succeeded) throws RemoteException { 825 synchronized (this) { 826 finished = true; 827 result = succeeded; 828 notifyAll(); 829 } 830 } 831 832 } 833 834 private void runClear() { 835 String pkg = nextArg(); 836 if (pkg == null) { 837 System.err.println("Error: no package specified"); 838 showUsage(); 839 return; 840 } 841 842 ClearDataObserver obs = new ClearDataObserver(); 843 try { 844 if (!ActivityManagerNative.getDefault().clearApplicationUserData(pkg, obs)) { 845 System.err.println("Failed"); 846 } 847 848 synchronized (obs) { 849 while (!obs.finished) { 850 try { 851 obs.wait(); 852 } catch (InterruptedException e) { 853 } 854 } 855 } 856 857 if (obs.result) { 858 System.err.println("Success"); 859 } else { 860 System.err.println("Failed"); 861 } 862 } catch (RemoteException e) { 863 System.err.println(e.toString()); 864 System.err.println(PM_NOT_RUNNING_ERR); 865 } 866 } 867 868 private static String enabledSettingToString(int state) { 869 switch (state) { 870 case PackageManager.COMPONENT_ENABLED_STATE_DEFAULT: 871 return "default"; 872 case PackageManager.COMPONENT_ENABLED_STATE_ENABLED: 873 return "enabled"; 874 case PackageManager.COMPONENT_ENABLED_STATE_DISABLED: 875 return "disabled"; 876 } 877 return "unknown"; 878 } 879 880 private void runSetEnabledSetting(int state) { 881 String pkg = nextArg(); 882 if (pkg == null) { 883 System.err.println("Error: no package or component specified"); 884 showUsage(); 885 return; 886 } 887 ComponentName cn = ComponentName.unflattenFromString(pkg); 888 if (cn == null) { 889 try { 890 mPm.setApplicationEnabledSetting(pkg, state, 0); 891 System.err.println("Package " + pkg + " new state: " 892 + enabledSettingToString( 893 mPm.getApplicationEnabledSetting(pkg))); 894 } catch (RemoteException e) { 895 System.err.println(e.toString()); 896 System.err.println(PM_NOT_RUNNING_ERR); 897 } 898 } else { 899 try { 900 mPm.setComponentEnabledSetting(cn, state, 0); 901 System.err.println("Component " + cn.toShortString() + " new state: " 902 + enabledSettingToString( 903 mPm.getComponentEnabledSetting(cn))); 904 } catch (RemoteException e) { 905 System.err.println(e.toString()); 906 System.err.println(PM_NOT_RUNNING_ERR); 907 } 908 } 909 } 910 911 /** 912 * Displays the package file for a package. 913 * @param pckg 914 */ 915 private void displayPackageFilePath(String pckg) { 916 try { 917 PackageInfo info = mPm.getPackageInfo(pckg, 0); 918 if (info != null && info.applicationInfo != null) { 919 System.out.print("package:"); 920 System.out.println(info.applicationInfo.sourceDir); 921 } 922 } catch (RemoteException e) { 923 System.err.println(e.toString()); 924 System.err.println(PM_NOT_RUNNING_ERR); 925 } 926 } 927 928 private Resources getResources(PackageItemInfo pii) { 929 Resources res = mResourceCache.get(pii.packageName); 930 if (res != null) return res; 931 932 try { 933 ApplicationInfo ai = mPm.getApplicationInfo(pii.packageName, 0); 934 AssetManager am = new AssetManager(); 935 am.addAssetPath(ai.publicSourceDir); 936 res = new Resources(am, null, null); 937 mResourceCache.put(pii.packageName, res); 938 return res; 939 } catch (RemoteException e) { 940 System.err.println(e.toString()); 941 System.err.println(PM_NOT_RUNNING_ERR); 942 return null; 943 } 944 } 945 946 private String nextOption() { 947 if (mNextArg >= mArgs.length) { 948 return null; 949 } 950 String arg = mArgs[mNextArg]; 951 if (!arg.startsWith("-")) { 952 return null; 953 } 954 mNextArg++; 955 if (arg.equals("--")) { 956 return null; 957 } 958 if (arg.length() > 1 && arg.charAt(1) != '-') { 959 if (arg.length() > 2) { 960 mCurArgData = arg.substring(2); 961 return arg.substring(0, 2); 962 } else { 963 mCurArgData = null; 964 return arg; 965 } 966 } 967 mCurArgData = null; 968 return arg; 969 } 970 971 private String nextOptionData() { 972 if (mCurArgData != null) { 973 return mCurArgData; 974 } 975 if (mNextArg >= mArgs.length) { 976 return null; 977 } 978 String data = mArgs[mNextArg]; 979 mNextArg++; 980 return data; 981 } 982 983 private String nextArg() { 984 if (mNextArg >= mArgs.length) { 985 return null; 986 } 987 String arg = mArgs[mNextArg]; 988 mNextArg++; 989 return arg; 990 } 991 992 private static void showUsage() { 993 System.err.println("usage: pm [list|path|install|uninstall]"); 994 System.err.println(" pm list packages [-f] [-d] [-e] [-u] [FILTER]"); 995 System.err.println(" pm list permission-groups"); 996 System.err.println(" pm list permissions [-g] [-f] [-d] [-u] [GROUP]"); 997 System.err.println(" pm list instrumentation [-f] [TARGET-PACKAGE]"); 998 System.err.println(" pm list features"); 999 System.err.println(" pm list libraries"); 1000 System.err.println(" pm path PACKAGE"); 1001 System.err.println(" pm install [-l] [-r] [-t] [-i INSTALLER_PACKAGE_NAME] [-s] [-f] PATH"); 1002 System.err.println(" pm uninstall [-k] PACKAGE"); 1003 System.err.println(" pm clear PACKAGE"); 1004 System.err.println(" pm enable PACKAGE_OR_COMPONENT"); 1005 System.err.println(" pm disable PACKAGE_OR_COMPONENT"); 1006 System.err.println(" pm setInstallLocation [0/auto] [1/internal] [2/external]"); 1007 System.err.println(""); 1008 System.err.println("The list packages command prints all packages, optionally only"); 1009 System.err.println("those whose package name contains the text in FILTER. Options:"); 1010 System.err.println(" -f: see their associated file."); 1011 System.err.println(" -d: filter to include disbled packages."); 1012 System.err.println(" -e: filter to include enabled packages."); 1013 System.err.println(" -u: also include uninstalled packages."); 1014 System.err.println(""); 1015 System.err.println("The list permission-groups command prints all known"); 1016 System.err.println("permission groups."); 1017 System.err.println(""); 1018 System.err.println("The list permissions command prints all known"); 1019 System.err.println("permissions, optionally only those in GROUP. Options:"); 1020 System.err.println(" -g: organize by group."); 1021 System.err.println(" -f: print all information."); 1022 System.err.println(" -s: short summary."); 1023 System.err.println(" -d: only list dangerous permissions."); 1024 System.err.println(" -u: list only the permissions users will see."); 1025 System.err.println(""); 1026 System.err.println("The list instrumentation command prints all instrumentations,"); 1027 System.err.println("or only those that target a specified package. Options:"); 1028 System.err.println(" -f: see their associated file."); 1029 System.err.println(""); 1030 System.err.println("The list features command prints all features of the system."); 1031 System.err.println(""); 1032 System.err.println("The path command prints the path to the .apk of a package."); 1033 System.err.println(""); 1034 System.err.println("The install command installs a package to the system. Options:"); 1035 System.err.println(" -l: install the package with FORWARD_LOCK."); 1036 System.err.println(" -r: reinstall an exisiting app, keeping its data."); 1037 System.err.println(" -t: allow test .apks to be installed."); 1038 System.err.println(" -i: specify the installer package name."); 1039 System.err.println(" -s: install package on sdcard."); 1040 System.err.println(" -f: install package on internal flash."); 1041 System.err.println(""); 1042 System.err.println("The uninstall command removes a package from the system. Options:"); 1043 System.err.println(" -k: keep the data and cache directories around."); 1044 System.err.println("after the package removal."); 1045 System.err.println(""); 1046 System.err.println("The clear command deletes all data associated with a package."); 1047 System.err.println(""); 1048 System.err.println("The enable and disable commands change the enabled state of"); 1049 System.err.println("a given package or component (written as \"package/class\")."); 1050 System.err.println(""); 1051 System.err.println("The getInstallLocation command gets the current install location"); 1052 System.err.println(" 0 [auto]: Let system decide the best location"); 1053 System.err.println(" 1 [internal]: Install on internal device storage"); 1054 System.err.println(" 2 [external]: Install on external media"); 1055 System.err.println(""); 1056 System.err.println("The setInstallLocation command changes the default install location"); 1057 System.err.println(" 0 [auto]: Let system decide the best location"); 1058 System.err.println(" 1 [internal]: Install on internal device storage"); 1059 System.err.println(" 2 [external]: Install on external media"); 1060 } 1061} 1062