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
This commit is contained in:
Amith Yamasani
2021-02-05 14:13:17 -08:00
parent e7ce67f421
commit d4de97915b
4 changed files with 13 additions and 7 deletions

View File

@@ -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);

View File

@@ -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();
}

View File

@@ -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

View File

@@ -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 boolean obfuscateInstantApps = shouldObfuscateInstantAppsForCaller(
Binder.getCallingUid(), UserHandle.getCallingUserId());
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(
callingUid, UserHandle.getCallingUserId());
final int userId = UserHandle.getCallingUserId();
final long token = Binder.clearCallingIdentity();
try {
final List<UsageStats> results = UsageStatsService.this.queryUsageStats(