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
This commit is contained in:
Peiyong Lin
2021-06-09 06:33:14 +00:00
parent 40069e8943
commit 10de6326c7
2 changed files with 15 additions and 6 deletions

View File

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

View File

@@ -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 {
* <PACKAGE_NAME> <CONFIG_STRING>`
* 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] <PACKAGE_NAME>");
pw.println(" Force app to run at the specified scaling ratio.");
pw.println(" mode [1|2|3|standard|performance|battery] <PACKAGE_NAME> <USER_ID>");
pw.println(" mode [--user <USER_ID>] [1|2|3|standard|performance|battery] <PACKAGE_NAME>");
pw.println(" Force app to run in the specified game mode, if supported.");
pw.println(" --user <USER_ID>: apply for the given user, the current user is used when unspecified.");
}
}