Merge "Add filter to getInstalledModules for showing installed modules only" into qt-dev

This commit is contained in:
Mohammad Samiul Islam
2019-05-10 14:25:42 +00:00
committed by Android (Google) Code Review
2 changed files with 75 additions and 1 deletions

View File

@@ -28,6 +28,7 @@ import android.os.RemoteException;
import android.os.UserHandle;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.ArraySet;
import android.util.Slog;
import com.android.internal.R;
@@ -39,6 +40,7 @@ import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@@ -165,7 +167,27 @@ public class ModuleInfoProvider {
throw new IllegalStateException("Call to getInstalledModules before metadata loaded");
}
return new ArrayList<>(mModuleInfo.values());
ArrayList<ModuleInfo> allModules = new ArrayList<>(mModuleInfo.values());
if ((flags & PackageManager.MATCH_ALL) != 0) {
return allModules;
}
ArraySet<String> allPackages;
try {
allPackages = new ArraySet<>(mPackageManager.getAllPackages());
} catch (RemoteException e) {
Slog.w(TAG, "Unable to retrieve all package names", e);
return Collections.emptyList();
}
ArrayList<ModuleInfo> installedModules = new ArrayList<>(allPackages.size());
for (int i = allModules.size() - 1; i >= 0; --i) {
ModuleInfo mi = allModules.get(i);
if (allPackages.contains(mi.getPackageName())) {
installedModules.add(mi);
}
}
return installedModules;
}
ModuleInfo getModuleInfo(String packageName, int flags) {

View File

@@ -39,6 +39,7 @@ import android.content.pm.IPackageDataObserver;
import android.content.pm.IPackageInstaller;
import android.content.pm.IPackageManager;
import android.content.pm.InstrumentationInfo;
import android.content.pm.ModuleInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageInstaller;
import android.content.pm.PackageInstaller.SessionInfo;
@@ -273,6 +274,8 @@ class PackageManagerShellCommand extends ShellCommand {
return uninstallSystemUpdates();
case "rollback-app":
return runRollbackApp();
case "get-moduleinfo":
return runGetModuleInfo();
default: {
String nextArg = getNextArg();
if (nextArg == null) {
@@ -295,6 +298,49 @@ class PackageManagerShellCommand extends ShellCommand {
return -1;
}
/**
* Shows module info
*
* Usage: get-moduleinfo [--all | --installed] [module-name]
* Example: get-moduleinfo, get-moduleinfo --all, get-moduleinfo xyz
*/
private int runGetModuleInfo() {
final PrintWriter pw = getOutPrintWriter();
int flags = 0;
String opt;
while ((opt = getNextOption()) != null) {
switch (opt) {
case "--all":
flags |= PackageManager.MATCH_ALL;
break;
case "--installed":
break;
default:
pw.println("Error: Unknown option: " + opt);
return -1;
}
}
String moduleName = getNextArg();
try {
if (moduleName != null) {
ModuleInfo m = mInterface.getModuleInfo(moduleName, flags);
pw.println(m.toString() + " packageName: " + m.getPackageName());
} else {
List<ModuleInfo> modules = mInterface.getInstalledModules(flags);
for (ModuleInfo m: modules) {
pw.println(m.toString() + " packageName: " + m.getPackageName());
}
}
} catch (RemoteException e) {
pw.println("Failure [" + e.getClass().getName() + " - " + e.getMessage() + "]");
return -1;
}
return 1;
}
private int getStagedSessions() {
final PrintWriter pw = getOutPrintWriter();
try {
@@ -3205,6 +3251,12 @@ class PackageManagerShellCommand extends ShellCommand {
pw.println(" Remove updates to all system applications and fall back to their /system " +
"version.");
pw.println();
pw.println(" get-moduleinfo [--all | --installed] [module-name]");
pw.println(" Displays module info. If module-name is specified only that info is shown");
pw.println(" By default, without any argument only installed modules are shown.");
pw.println(" --all: show all module info");
pw.println(" --installed: show only installed modules");
pw.println("");
Intent.printIntentArgsHelp(pw , "");
}