@SuppressWarnings("unchecked") private List<PackageInfo> getInstalledPackages(IPackageManager pm, int flags, int userId) throws RemoteException { final List<PackageInfo> packageInfos = new ArrayList<PackageInfo>(); PackageInfo lastItem = null; ParceledListSlice<PackageInfo> slice; do { final String lastKey = lastItem != null ? lastItem.packageName : null; slice = pm.getInstalledPackages(flags, lastKey, userId); lastItem = slice.populateList(packageInfos, PackageInfo.CREATOR); } while (!slice.isLastSlice()); return packageInfos; }
/** Lists all the installed packages. */ private void runListPackages(boolean showApplicationPackage) { try { String opt; while ((opt = nextOption()) != null) { if (opt.equals("-l")) { // old compat } else if (opt.equals("-lf")) { showApplicationPackage = true; } else if (opt.equals("-f")) { showApplicationPackage = true; } else { System.err.println("Error: Unknown option: " + opt); showUsage(); return; } } } catch (RuntimeException ex) { System.err.println("Error: " + ex.toString()); showUsage(); return; } try { List<PackageInfo> packages = mPm.getInstalledPackages(0 /* all */); int count = packages.size(); for (int p = 0; p < count; p++) { PackageInfo info = packages.get(p); System.out.print("package:"); if (showApplicationPackage) { System.out.print(info.applicationInfo.sourceDir); System.out.print("="); } System.out.println(info.packageName); } } catch (RemoteException e) { System.err.println(e.toString()); System.err.println(PM_NOT_RUNNING_ERR); } }