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

Commit f16710a3 authored by Sudheer Shanka's avatar Sudheer Shanka Committed by Automerger Merge Worker
Browse files

Merge "Add "clear-last-used-timestamps" command to usagestats service." into tm-dev am: be65c9c0

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/17211948

Change-Id: I9b14f17bdeb7ece5c6075274bed03722f17fe955
parents e277dd66 be65c9c0
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -245,4 +245,7 @@ public interface AppStandbyInternal {
     */
    @Nullable
    String getAppStandbyConstant(@NonNull String key);

    /** Clears the last used timestamps data for the given {@code packageName}. */
    void clearLastUsedTimestampsForTest(@NonNull String packageName, @UserIdInt int userId);
}
+11 −0
Original line number Diff line number Diff line
@@ -686,6 +686,17 @@ public class AppIdleHistory {
                Integer.toString(userId)), APP_IDLE_FILENAME);
    }

    void clearLastUsedTimestamps(String packageName, int userId) {
        ArrayMap<String, AppUsageHistory> userHistory = getUserHistory(userId);
        AppUsageHistory appUsageHistory = getPackageHistory(userHistory, packageName,
                SystemClock.elapsedRealtime(), false /* create */);
        if (appUsageHistory != null) {
            appUsageHistory.lastUsedByUserElapsedTime = Integer.MIN_VALUE;
            appUsageHistory.lastUsedElapsedTime = Integer.MIN_VALUE;
            appUsageHistory.lastUsedScreenTime = Integer.MIN_VALUE;
        }
    }

    /**
     * Check if App Idle File exists on disk
     * @param userId
+7 −0
Original line number Diff line number Diff line
@@ -1856,6 +1856,13 @@ public class AppStandbyController
        return mAppStandbyProperties.get(key);
    }

    @Override
    public void clearLastUsedTimestampsForTest(@NonNull String packageName, @UserIdInt int userId) {
        synchronized (mAppIdleLock) {
            mAppIdleHistory.clearLastUsedTimestamps(packageName, userId);
        }
    }

    @Override
    public void flushToDisk() {
        synchronized (mAppIdleLock) {
+13 −0
Original line number Diff line number Diff line
@@ -79,6 +79,7 @@ import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.ParcelFileDescriptor;
import android.os.Process;
import android.os.RemoteException;
import android.os.SystemClock;
@@ -1982,6 +1983,10 @@ public class UsageStatsService extends SystemService implements
        }
    }

    void clearLastUsedTimestamps(@NonNull String packageName, @UserIdInt int userId) {
        mAppStandby.clearLastUsedTimestampsForTest(packageName, userId);
    }

    private final class BinderService extends IUsageStatsManager.Stub {

        private boolean hasPermission(String callingPackage) {
@@ -2816,6 +2821,14 @@ public class UsageStatsService extends SystemService implements
            }
            return mAppStandby.getAppStandbyConstant(key);
        }

        @Override
        public int handleShellCommand(@NonNull ParcelFileDescriptor in,
                @NonNull ParcelFileDescriptor out, @NonNull ParcelFileDescriptor err,
                @NonNull String[] args) {
            return new UsageStatsShellCommand(UsageStatsService.this).exec(this,
                    in.getFileDescriptor(), out.getFileDescriptor(), err.getFileDescriptor(), args);
        }
    }

    void registerAppUsageObserver(int callingUid, int observerId, String[] packages,
+79 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.usage;

import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.os.ShellCommand;
import android.os.UserHandle;

import java.io.PrintWriter;

class UsageStatsShellCommand extends ShellCommand {
    private final UsageStatsService mService;

    UsageStatsShellCommand(UsageStatsService usageStatsService) {
        mService = usageStatsService;
    }

    @Override
    public int onCommand(String cmd) {
        if (cmd == null) {
            return handleDefaultCommands(null);
        }
        switch (cmd) {
            case "clear-last-used-timestamps":
                return runClearLastUsedTimestamps();
            default:
                return handleDefaultCommands(cmd);
        }
    }

    @Override
    public void onHelp() {
        final PrintWriter pw = getOutPrintWriter();
        pw.println("UsageStats service (usagestats) commands:");
        pw.println("help");
        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();
    }

    @SuppressLint("AndroidFrameworkRequiresPermission")
    private int runClearLastUsedTimestamps() {
        final String packageName = getNextArgRequired();

        int userId = UserHandle.USER_CURRENT;
        String opt;
        while ((opt = getNextOption()) != null) {
            if ("-u".equals(opt) || "--user".equals(opt)) {
                userId = UserHandle.parseUserArg(getNextArgRequired());
            } else {
                getErrPrintWriter().println("Error: unknown option: " + opt);
                return -1;
            }
        }
        if (userId == UserHandle.USER_CURRENT) {
            userId = ActivityManager.getCurrentUser();
        }

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