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

Commit 0253bd2c authored by Varun Shah's avatar Varun Shah Committed by Android (Google) Code Review
Browse files

Merge "Add a new adb command to prune usagestats for a package." into main

parents 36e42d77 4527577c
Loading
Loading
Loading
Loading
+8 −0
Original line number Original line Diff line number Diff line
@@ -22,6 +22,7 @@ import android.app.usage.UsageEvents;
import android.app.usage.UsageStats;
import android.app.usage.UsageStats;
import android.app.usage.UsageStatsManager;
import android.app.usage.UsageStatsManager;
import android.os.Build;
import android.os.Build;
import android.os.SystemClock;
import android.os.SystemProperties;
import android.os.SystemProperties;
import android.util.ArrayMap;
import android.util.ArrayMap;
import android.util.ArraySet;
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) {
    IntervalStats readIntervalStatsForFile(int interval, long fileName) {
        synchronized (mLock) {
        synchronized (mLock) {
            final IntervalStats stats = new IntervalStats();
            final IntervalStats stats = new IntervalStats();
+6 −0
Original line number Original line Diff line number Diff line
@@ -2043,6 +2043,12 @@ public class UsageStatsService extends SystemService implements
        mAppStandby.clearLastUsedTimestampsForTest(packageName, userId);
        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 final class BinderService extends IUsageStatsManager.Stub {


        private boolean hasPermission(String callingPackage) {
        private boolean hasPermission(String callingPackage) {
+28 −4
Original line number Original line Diff line number Diff line
@@ -38,6 +38,8 @@ class UsageStatsShellCommand extends ShellCommand {
        switch (cmd) {
        switch (cmd) {
            case "clear-last-used-timestamps":
            case "clear-last-used-timestamps":
                return runClearLastUsedTimestamps();
                return runClearLastUsedTimestamps();
            case "delete-package-data":
                return deletePackageData();
            default:
            default:
                return handleDefaultCommands(cmd);
                return handleDefaultCommands(cmd);
        }
        }
@@ -51,14 +53,38 @@ class UsageStatsShellCommand extends ShellCommand {
        pw.println("    Print this help text.");
        pw.println("    Print this help text.");
        pw.println();
        pw.println();
        pw.println("clear-last-used-timestamps PACKAGE_NAME [-u | --user USER_ID]");
        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();
        pw.println();
    }
    }


    @SuppressLint("AndroidFrameworkRequiresPermission")
    @SuppressLint("AndroidFrameworkRequiresPermission")
    private int runClearLastUsedTimestamps() {
    private int runClearLastUsedTimestamps() {
        final String packageName = getNextArgRequired();
        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;
        int userId = UserHandle.USER_CURRENT;
        String opt;
        String opt;
        while ((opt = getNextOption()) != null) {
        while ((opt = getNextOption()) != null) {
@@ -72,8 +98,6 @@ class UsageStatsShellCommand extends ShellCommand {
        if (userId == UserHandle.USER_CURRENT) {
        if (userId == UserHandle.USER_CURRENT) {
            userId = ActivityManager.getCurrentUser();
            userId = ActivityManager.getCurrentUser();
        }
        }

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


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

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