From 10de6326c7fdcf9b140921e4d089d03464edc163 Mon Sep 17 00:00:00 2001 From: Peiyong Lin Date: Wed, 9 Jun 2021 06:33:14 +0000 Subject: [PATCH] Update shell command line to take optional user id. Previously a user id is required when `adb shell cmd game mode` is called. This patch modifies it to be optional and use the current active user id as the default user id when not specified. Minor: Fix multi-user issue in GameManagerService when querying package uid. Bug: b/190561852 Test: atest GameManagerService Change-Id: I1e21ee8e08930fc5ac5d12a13b2aeab09335a5b6 --- .../com/android/server/app/GameManagerService.java | 7 ++++--- .../server/app/GameManagerShellCommand.java | 14 +++++++++++--- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/services/core/java/com/android/server/app/GameManagerService.java b/services/core/java/com/android/server/app/GameManagerService.java index 75367d27a8a14..697f6fa8eea86 100644 --- a/services/core/java/com/android/server/app/GameManagerService.java +++ b/services/core/java/com/android/server/app/GameManagerService.java @@ -469,9 +469,10 @@ public final class GameManagerService extends IGameManagerService.Stub { } } - private boolean isValidPackageName(String packageName) { + private boolean isValidPackageName(String packageName, int userId) { try { - return mPackageManager.getPackageUid(packageName, 0) == Binder.getCallingUid(); + return mPackageManager.getPackageUidAsUser(packageName, userId) + == Binder.getCallingUid(); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); return false; @@ -547,7 +548,7 @@ public final class GameManagerService extends IGameManagerService.Stub { // The least privileged case is a normal app performing a query, so check that first and // return a value if the package name is valid. Next, check if the caller has the necessary // permission and return a value. Do this check last, since it can throw an exception. - if (isValidPackageName(packageName)) { + if (isValidPackageName(packageName, userId)) { return getGameModeFromSettings(packageName, userId); } diff --git a/services/core/java/com/android/server/app/GameManagerShellCommand.java b/services/core/java/com/android/server/app/GameManagerShellCommand.java index c5f1a0b3f67e6..2074ffae1030a 100644 --- a/services/core/java/com/android/server/app/GameManagerShellCommand.java +++ b/services/core/java/com/android/server/app/GameManagerShellCommand.java @@ -16,6 +16,7 @@ package com.android.server.app; +import android.app.ActivityManager; import android.app.GameManager; import android.app.IGameManagerService; import android.compat.Compatibility; @@ -127,9 +128,14 @@ public class GameManagerShellCommand extends ShellCommand { * ` * see: {@link GameManagerServiceTests#mockDeviceConfigAll()} */ + final String option = getNextOption(); + String userIdStr = null; + if (option != null && option.equals("--user")) { + userIdStr = getNextArgRequired(); + } + final String gameMode = getNextArgRequired(); final String packageName = getNextArgRequired(); - final String userIdStr = getNextArgRequired(); final IGameManagerService service = IGameManagerService.Stub.asInterface( ServiceManager.getServiceOrThrow(Context.GAME_SERVICE)); boolean batteryModeSupported = false; @@ -142,7 +148,8 @@ public class GameManagerShellCommand extends ShellCommand { batteryModeSupported = true; } } - int userId = Integer.parseInt(userIdStr); + int userId = userIdStr != null ? Integer.parseInt(userIdStr) + : ActivityManager.getCurrentUser(); switch (gameMode.toLowerCase()) { case "1": case "standard": @@ -199,7 +206,8 @@ public class GameManagerShellCommand extends ShellCommand { pw.println(" Print this help text."); pw.println(" downscale [0.5|0.6|0.7|0.8|0.9|disable] "); pw.println(" Force app to run at the specified scaling ratio."); - pw.println(" mode [1|2|3|standard|performance|battery] "); + pw.println(" mode [--user ] [1|2|3|standard|performance|battery] "); pw.println(" Force app to run in the specified game mode, if supported."); + pw.println(" --user : apply for the given user, the current user is used when unspecified."); } }