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