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

Commit d4de9791 authored by Amith Yamasani's avatar Amith Yamasani
Browse files

Make UsageStats query API UserHandle-aware

Allow queryUsageStats to take in the context's userId. This
allows apps like Settings to query usage stats across
profiles.

Bug: 146921442
Test: atest CtsUsageStatsTestCases:UsageStatsTests
Change-Id: Ia6c90c1a31887efa8d5446c1b1b11e75cb9ab83e
parent e7ce67f4
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -30,7 +30,7 @@ import java.util.Map;
interface IUsageStatsManager {
    @UnsupportedAppUsage(maxTargetSdk = 30, trackingBug = 170729553)
    ParceledListSlice queryUsageStats(int bucketType, long beginTime, long endTime,
            String callingPackage);
            String callingPackage, int userId);
    @UnsupportedAppUsage(maxTargetSdk = 30, trackingBug = 170729553)
    ParceledListSlice queryConfigurationStats(int bucketType, long beginTime, long endTime,
            String callingPackage);
+3 −1
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import android.annotation.RequiresPermission;
import android.annotation.SystemApi;
import android.annotation.SystemService;
import android.annotation.TestApi;
import android.annotation.UserHandleAware;
import android.app.Activity;
import android.app.PendingIntent;
import android.compat.annotation.UnsupportedAppUsage;
@@ -437,11 +438,12 @@ public final class UsageStatsManager {
     * @see #INTERVAL_YEARLY
     * @see #INTERVAL_BEST
     */
    @UserHandleAware
    public List<UsageStats> queryUsageStats(int intervalType, long beginTime, long endTime) {
        try {
            @SuppressWarnings("unchecked")
            ParceledListSlice<UsageStats> slice = mService.queryUsageStats(intervalType, beginTime,
                    endTime, mContext.getOpPackageName());
                    endTime, mContext.getOpPackageName(), mContext.getUserId());
            if (slice != null) {
                return slice.getList();
            }
+1 −1
Original line number Diff line number Diff line
@@ -100,7 +100,7 @@ public class ResolverListControllerTest {
        final List<UsageStats> slices = new ArrayList<>();
        slices.add(packageStats);
        ParceledListSlice<UsageStats> stats = new ParceledListSlice<>(slices);
        when(mMockService.queryUsageStats(anyInt(), anyLong(), anyLong(), anyString()))
        when(mMockService.queryUsageStats(anyInt(), anyLong(), anyLong(), anyString(), anyInt()))
                .thenReturn(stats);
        Answer<Void> answer = new Answer<Void>() {
            @Override
+7 −3
Original line number Diff line number Diff line
@@ -1523,15 +1523,19 @@ public class UsageStatsService extends SystemService implements

        @Override
        public ParceledListSlice<UsageStats> queryUsageStats(int bucketType, long beginTime,
                long endTime, String callingPackage) {
                long endTime, String callingPackage, int userId) {
            if (!hasPermission(callingPackage)) {
                return null;
            }

            final int callingUid = Binder.getCallingUid();
            userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), callingUid,
                    userId, false, true, "queryUsageStats", callingPackage);

            // Check the caller's userId for obfuscation decision, not the user being queried
            final boolean obfuscateInstantApps = shouldObfuscateInstantAppsForCaller(
                    Binder.getCallingUid(), UserHandle.getCallingUserId());
                    callingUid, UserHandle.getCallingUserId());

            final int userId = UserHandle.getCallingUserId();
            final long token = Binder.clearCallingIdentity();
            try {
                final List<UsageStats> results = UsageStatsService.this.queryUsageStats(