Clear cache shell commmand

Added changes in runClear to clear cache data of the app

Bug: 221946754
Test: atest SdkSandboxStorageHostTest#testSdkDataPackageDirectory_IsClearedOnClearCache
Change-Id: Ibd61cdec64f51bbc84d6f0536aab877ce0725c6c
This commit is contained in:
Rishabh Singh
2022-05-09 12:25:04 +00:00
parent e4de16b612
commit 6f7d62082e

View File

@@ -2285,10 +2285,23 @@ class PackageManagerShellCommand extends ShellCommand {
}
private int runClear() throws RemoteException {
final PrintWriter pw = getOutPrintWriter();
int userId = UserHandle.USER_SYSTEM;
String option = getNextOption();
if (option != null && option.equals("--user")) {
userId = UserHandle.parseUserArg(getNextArgRequired());
boolean cacheOnly = false;
String opt;
while ((opt = getNextOption()) != null) {
switch (opt) {
case "--user":
userId = UserHandle.parseUserArg(getNextArgRequired());
break;
case "--cache-only":
cacheOnly = true;
break;
default:
pw.println("Error: Unknown option: " + opt);
return 1;
}
}
String pkg = getNextArg();
@@ -2300,7 +2313,12 @@ class PackageManagerShellCommand extends ShellCommand {
final int translatedUserId =
translateUserId(userId, UserHandle.USER_NULL, "runClear");
final ClearDataObserver obs = new ClearDataObserver();
ActivityManager.getService().clearApplicationUserData(pkg, false, obs, translatedUserId);
if (!cacheOnly) {
ActivityManager.getService()
.clearApplicationUserData(pkg, false, obs, translatedUserId);
} else {
mInterface.deleteApplicationCacheFilesAsUser(pkg, translatedUserId, obs);
}
synchronized (obs) {
while (!obs.finished) {
try {
@@ -4060,8 +4078,10 @@ class PackageManagerShellCommand extends ShellCommand {
pw.println(" --user: remove the app from the given user.");
pw.println(" --versionCode: only uninstall if the app has the given version code.");
pw.println("");
pw.println(" clear [--user USER_ID] PACKAGE");
pw.println(" Deletes all data associated with a package.");
pw.println(" clear [--user USER_ID] [--cache-only] PACKAGE");
pw.println(" Deletes data associated with a package. Options are:");
pw.println(" --user: specifies the user for which we need to clear data");
pw.println(" --cache-only: a flag which tells if we only need to clear cache data");
pw.println("");
pw.println(" enable [--user USER_ID] PACKAGE_OR_COMPONENT");
pw.println(" disable [--user USER_ID] PACKAGE_OR_COMPONENT");