Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4527577c authored by Varun Shah's avatar Varun Shah
Browse files

Add a new adb command to prune usagestats for a package.

This will help address some of the flaky behavior in CTS tests.

Bug: 295216985
Test: atest UsageStatsTest
Change-Id: Iab86e890c0aacfed3556e14b7b93949edd96f935
parent 48e4cb6a
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import android.app.usage.UsageEvents;
import android.app.usage.UsageStats;
import android.app.usage.UsageStatsManager;
import android.os.Build;
import android.os.SystemClock;
import android.os.SystemProperties;
import android.util.ArrayMap;
import android.util.ArraySet;
@@ -1567,6 +1568,13 @@ public class UsageStatsDatabase {
        }
    }

    void deleteDataFor(String pkg) {
        // reuse the existing prune method to delete data for the specified package.
        // we'll use the current timestamp so that all events before now get pruned.
        prunePackagesDataOnUpgrade(
                new HashMap<>(Collections.singletonMap(pkg, SystemClock.elapsedRealtime())));
    }

    IntervalStats readIntervalStatsForFile(int interval, long fileName) {
        synchronized (mLock) {
            final IntervalStats stats = new IntervalStats();
+6 −0
Original line number Diff line number Diff line
@@ -2043,6 +2043,12 @@ public class UsageStatsService extends SystemService implements
        mAppStandby.clearLastUsedTimestampsForTest(packageName, userId);
    }

    void deletePackageData(@NonNull String packageName, @UserIdInt int userId) {
        synchronized (mLock) {
            mUserState.get(userId).deleteDataFor(packageName);
        }
    }

    private final class BinderService extends IUsageStatsManager.Stub {

        private boolean hasPermission(String callingPackage) {
+28 −4
Original line number Diff line number Diff line
@@ -38,6 +38,8 @@ class UsageStatsShellCommand extends ShellCommand {
        switch (cmd) {
            case "clear-last-used-timestamps":
                return runClearLastUsedTimestamps();
            case "delete-package-data":
                return deletePackageData();
            default:
                return handleDefaultCommands(cmd);
        }
@@ -51,14 +53,38 @@ class UsageStatsShellCommand extends ShellCommand {
        pw.println("    Print this help text.");
        pw.println();
        pw.println("clear-last-used-timestamps PACKAGE_NAME [-u | --user USER_ID]");
        pw.println("    Clears any existing usage data for the given package.");
        pw.println("    Clears the last used timestamps for the given package.");
        pw.println();
        pw.println("delete-package-data PACKAGE_NAME [-u | --user USER_ID]");
        pw.println("    Deletes all the usage stats for the given package.");
        pw.println();
    }

    @SuppressLint("AndroidFrameworkRequiresPermission")
    private int runClearLastUsedTimestamps() {
        final String packageName = getNextArgRequired();
        final int userId = getUserId();
        if (userId == -1) {
            return -1;
        }

        mService.clearLastUsedTimestamps(packageName, userId);
        return 0;
    }

    @SuppressLint("AndroidFrameworkRequiresPermission")
    private int deletePackageData() {
        final String packageName = getNextArgRequired();
        final int userId = getUserId();
        if (userId == -1) {
            return -1;
        }

        mService.deletePackageData(packageName, userId);
        return 0;
    }

    private int getUserId() {
        int userId = UserHandle.USER_CURRENT;
        String opt;
        while ((opt = getNextOption()) != null) {
@@ -72,8 +98,6 @@ class UsageStatsShellCommand extends ShellCommand {
        if (userId == UserHandle.USER_CURRENT) {
            userId = ActivityManager.getCurrentUser();
        }

        mService.clearLastUsedTimestamps(packageName, userId);
        return 0;
        return userId;
    }
}
+4 −0
Original line number Diff line number Diff line
@@ -974,6 +974,10 @@ class UserUsageStatsService {
        mDatabase.dumpMappings(ipw);
    }

    void deleteDataFor(String pkg) {
        mDatabase.deleteDataFor(pkg);
    }

    void dumpFile(IndentingPrintWriter ipw, String[] args) {
        if (args == null || args.length == 0) {
            // dump all files for every interval for specified user