Update set and add list-modes adb shell game commands

Hide `--mode` option from `set` command to default to use `custom`
mode. Rename `list` command to `list-configs` and add it to the game
commands' help message.

Bug: 243448953
Test: manual test
Change-Id: I4adae903ce48ba8aeb5e5b00154bec1953df9a9a
This commit is contained in:
Xiang Wang
2022-11-28 15:12:38 -08:00
parent 60e153c3a6
commit 98ca4c8020
2 changed files with 137 additions and 75 deletions

View File

@@ -1632,6 +1632,34 @@ public final class GameManagerService extends IGameManagerService.Stub {
public void setGameModeConfigOverride(String packageName, @UserIdInt int userId,
@GameMode int gameMode, String fpsStr, String scaling) throws SecurityException {
checkPermission(Manifest.permission.MANAGE_GAME_MODE);
int gameUid = -1;
try {
gameUid = mPackageManager.getPackageUidAsUser(packageName, userId);
} catch (NameNotFoundException ex) {
Slog.d(TAG, "Cannot find the UID for package " + packageName + " under user " + userId);
}
GamePackageConfiguration pkgConfig = getConfig(packageName, userId);
if (pkgConfig != null && pkgConfig.getGameModeConfiguration(gameMode) != null) {
final GamePackageConfiguration.GameModeConfiguration currentModeConfig =
pkgConfig.getGameModeConfiguration(gameMode);
FrameworkStatsLog.write(FrameworkStatsLog.GAME_MODE_CONFIGURATION_CHANGED, gameUid,
Binder.getCallingUid(), gameModeToStatsdGameMode(gameMode),
currentModeConfig.getScaling() /* fromScaling */,
scaling == null ? currentModeConfig.getScaling()
: Float.parseFloat(scaling) /* toScaling */,
currentModeConfig.getFps() /* fromFps */,
fpsStr == null ? currentModeConfig.getFps()
: Integer.parseInt(fpsStr)) /* toFps */;
} else {
FrameworkStatsLog.write(FrameworkStatsLog.GAME_MODE_CONFIGURATION_CHANGED, gameUid,
Binder.getCallingUid(), gameModeToStatsdGameMode(gameMode),
GamePackageConfiguration.GameModeConfiguration.DEFAULT_SCALING /* fromScaling*/,
scaling == null ? GamePackageConfiguration.GameModeConfiguration.DEFAULT_SCALING
: Float.parseFloat(scaling) /* toScaling */,
0 /* fromFps */,
fpsStr == null ? 0 : Integer.parseInt(fpsStr) /* toFps */);
}
// Adding game mode config override of the given package name
GamePackageConfiguration configOverride;
synchronized (mLock) {

View File

@@ -27,6 +27,7 @@ import android.os.ShellCommand;
import java.io.PrintWriter;
import java.util.Locale;
import java.util.StringJoiner;
/**
* ShellCommands for GameManagerService.
@@ -34,8 +35,20 @@ import java.util.Locale;
* Use with {@code adb shell cmd game ...}.
*/
public class GameManagerShellCommand extends ShellCommand {
private static final String STANDARD_MODE_STR = "standard";
private static final String STANDARD_MODE_NUM = "1";
private static final String PERFORMANCE_MODE_STR = "performance";
private static final String PERFORMANCE_MODE_NUM = "2";
private static final String BATTERY_MODE_STR = "battery";
private static final String BATTERY_MODE_NUM = "3";
private static final String CUSTOM_MODE_STR = "custom";
private static final String CUSTOM_MODE_NUM = "4";
private static final String UNSUPPORTED_MODE_STR = "unsupported";
private static final String UNSUPPORTED_MODE_NUM = String.valueOf(
GameManager.GAME_MODE_UNSUPPORTED);
public GameManagerShellCommand() {}
public GameManagerShellCommand() {
}
@Override
public int onCommand(String cmd) {
@@ -46,10 +59,10 @@ public class GameManagerShellCommand extends ShellCommand {
try {
switch (cmd) {
case "set": {
return runSetGameMode(pw);
return runSetGameModeConfig(pw);
}
case "reset": {
return runResetGameMode(pw);
return runResetGameModeConfig(pw);
}
case "mode": {
/** The "mode" command allows setting a package's current game mode outside of
@@ -61,10 +74,13 @@ public class GameManagerShellCommand extends ShellCommand {
* <PACKAGE_NAME> <CONFIG_STRING>`
* see: {@link GameManagerServiceTests#mockDeviceConfigAll()}
*/
return runGameMode(pw);
return runSetGameMode(pw);
}
case "list": {
return runGameList(pw);
case "list-modes": {
return runListGameModes(pw);
}
case "list-configs": {
return runListGameModeConfigs(pw);
}
default:
return handleDefaultCommands(cmd);
@@ -75,7 +91,21 @@ public class GameManagerShellCommand extends ShellCommand {
return -1;
}
private int runGameList(PrintWriter pw) throws ServiceNotFoundException, RemoteException {
private int runListGameModes(PrintWriter pw) throws ServiceNotFoundException, RemoteException {
final String packageName = getNextArgRequired();
final GameManagerService gameManagerService = (GameManagerService)
ServiceManager.getService(Context.GAME_SERVICE);
final StringJoiner sj = new StringJoiner(",");
for (int mode : gameManagerService.getAvailableGameModes(packageName,
ActivityManager.getCurrentUser())) {
sj.add(gameModeIntToString(mode));
}
pw.println(packageName + " has available game modes: [" + sj + "]");
return 0;
}
private int runListGameModeConfigs(PrintWriter pw)
throws ServiceNotFoundException, RemoteException {
final String packageName = getNextArgRequired();
final GameManagerService gameManagerService = (GameManagerService)
@@ -92,7 +122,7 @@ public class GameManagerShellCommand extends ShellCommand {
return 0;
}
private int runGameMode(PrintWriter pw) throws ServiceNotFoundException, RemoteException {
private int runSetGameMode(PrintWriter pw) throws ServiceNotFoundException, RemoteException {
final String option = getNextOption();
String userIdStr = null;
if (option != null && option.equals("--user")) {
@@ -116,34 +146,46 @@ public class GameManagerShellCommand extends ShellCommand {
}
}
switch (gameMode.toLowerCase()) {
case "1":
case "standard":
case STANDARD_MODE_NUM:
case STANDARD_MODE_STR:
// Standard mode can be used to specify loading ANGLE as the default OpenGL ES
// driver, so it should always be available.
service.setGameMode(packageName, GameManager.GAME_MODE_STANDARD, userId);
pw.println("Set game mode to `STANDARD` for user `" + userId + "` in game `"
+ packageName + "`");
break;
case "2":
case "performance":
case PERFORMANCE_MODE_NUM:
case PERFORMANCE_MODE_STR:
if (perfModeSupported) {
service.setGameMode(packageName, GameManager.GAME_MODE_PERFORMANCE,
userId);
pw.println("Set game mode to `PERFORMANCE` for user `" + userId + "` in game `"
+ packageName + "`");
} else {
pw.println("Game mode: " + gameMode + " not supported by "
+ packageName);
return -1;
}
break;
case "3":
case "battery":
case BATTERY_MODE_NUM:
case BATTERY_MODE_STR:
if (batteryModeSupported) {
service.setGameMode(packageName, GameManager.GAME_MODE_BATTERY,
userId);
pw.println("Set game mode to `BATTERY` for user `" + userId + "` in game `"
+ packageName + "`");
} else {
pw.println("Game mode: " + gameMode + " not supported by "
+ packageName);
return -1;
}
break;
case CUSTOM_MODE_NUM:
case CUSTOM_MODE_STR:
service.setGameMode(packageName, GameManager.GAME_MODE_CUSTOM, userId);
pw.println("Set game mode to `CUSTOM` for user `" + userId + "` in game `"
+ packageName + "`");
break;
default:
pw.println("Invalid game mode: " + gameMode);
return -1;
@@ -151,15 +193,9 @@ public class GameManagerShellCommand extends ShellCommand {
return 0;
}
private int runSetGameMode(PrintWriter pw) throws ServiceNotFoundException, RemoteException {
String option = getNextArgRequired();
if (!option.equals("--mode")) {
pw.println("Invalid option '" + option + "'");
return -1;
}
final String gameMode = getNextArgRequired();
private int runSetGameModeConfig(PrintWriter pw)
throws ServiceNotFoundException, RemoteException {
String option;
/**
* handling optional input
* "--user", "--downscale" and "--fps" can come in any order
@@ -167,8 +203,12 @@ public class GameManagerShellCommand extends ShellCommand {
String userIdStr = null;
String fpsStr = null;
String downscaleRatio = null;
int gameMode = GameManager.GAME_MODE_CUSTOM;
while ((option = getNextOption()) != null) {
switch (option) {
case "--mode":
gameMode = Integer.parseInt(getNextArgRequired());
break;
case "--user":
if (userIdStr == null) {
userIdStr = getNextArgRequired();
@@ -220,50 +260,21 @@ public class GameManagerShellCommand extends ShellCommand {
final GameManagerService gameManagerService = (GameManagerService)
ServiceManager.getService(Context.GAME_SERVICE);
boolean batteryModeSupported = false;
boolean perfModeSupported = false;
int [] modes = gameManagerService.getAvailableGameModes(packageName, userId);
for (int mode : modes) {
if (mode == GameManager.GAME_MODE_PERFORMANCE) {
perfModeSupported = true;
} else if (mode == GameManager.GAME_MODE_BATTERY) {
batteryModeSupported = true;
}
}
switch (gameMode.toLowerCase(Locale.getDefault())) {
case "2":
case "performance":
if (perfModeSupported) {
gameManagerService.setGameModeConfigOverride(packageName, userId,
GameManager.GAME_MODE_PERFORMANCE, fpsStr, downscaleRatio);
} else {
pw.println("Game mode: " + gameMode + " not supported by "
+ packageName);
return -1;
}
break;
case "3":
case "battery":
if (batteryModeSupported) {
gameManagerService.setGameModeConfigOverride(packageName, userId,
GameManager.GAME_MODE_BATTERY, fpsStr, downscaleRatio);
} else {
pw.println("Game mode: " + gameMode + " not supported by "
+ packageName);
return -1;
}
break;
default:
pw.println("Invalid game mode: " + gameMode);
return -1;
if (gameManagerService == null) {
pw.println("Failed to find GameManagerService on device");
return -1;
}
gameManagerService.setGameModeConfigOverride(packageName, userId, gameMode,
fpsStr, downscaleRatio);
pw.println("Set custom mode intervention config for user `" + userId + "` in game `"
+ packageName + "` as: `"
+ "downscaling-ratio: " + downscaleRatio + ";"
+ "fps-override: " + fpsStr + "`");
return 0;
}
private int runResetGameMode(PrintWriter pw) throws ServiceNotFoundException, RemoteException {
private int runResetGameModeConfig(PrintWriter pw)
throws ServiceNotFoundException, RemoteException {
String option = null;
String gameMode = null;
String userIdStr = null;
@@ -305,13 +316,13 @@ public class GameManagerShellCommand extends ShellCommand {
}
switch (gameMode.toLowerCase(Locale.getDefault())) {
case "2":
case "performance":
case PERFORMANCE_MODE_NUM:
case PERFORMANCE_MODE_STR:
gameManagerService.resetGameModeConfigOverride(packageName, userId,
GameManager.GAME_MODE_PERFORMANCE);
break;
case "3":
case "battery":
case BATTERY_MODE_NUM:
case BATTERY_MODE_STR:
gameManagerService.resetGameModeConfigOverride(packageName, userId,
GameManager.GAME_MODE_BATTERY);
break;
@@ -322,6 +333,22 @@ public class GameManagerShellCommand extends ShellCommand {
return 0;
}
private static String gameModeIntToString(@GameManager.GameMode int gameMode) {
switch (gameMode) {
case GameManager.GAME_MODE_BATTERY:
return BATTERY_MODE_STR;
case GameManager.GAME_MODE_PERFORMANCE:
return PERFORMANCE_MODE_STR;
case GameManager.GAME_MODE_CUSTOM:
return CUSTOM_MODE_STR;
case GameManager.GAME_MODE_STANDARD:
return STANDARD_MODE_STR;
case GameManager.GAME_MODE_UNSUPPORTED:
return UNSUPPORTED_MODE_STR;
}
return "";
}
@Override
public void onHelp() {
PrintWriter pw = getOutPrintWriter();
@@ -329,21 +356,28 @@ public class GameManagerShellCommand extends ShellCommand {
pw.println(" help");
pw.println(" Print this help text.");
pw.println(" downscale");
pw.println(" Deprecated. Please use `set` command.");
pw.println(" mode [--user <USER_ID>] [1|2|3|standard|performance|battery] <PACKAGE_NAME>");
pw.println(" Deprecated. Please use `custom` command.");
pw.println(" list-configs <PACKAGE_NAME>");
pw.println(" Lists the current intervention configs of an app.");
pw.println(" list-modes <PACKAGE_NAME>");
pw.println(" Lists the current available game modes of an app.");
pw.println(" mode [--user <USER_ID>] [1|2|3|4|standard|performance|battery|custom] "
+ "<PACKAGE_NAME>");
pw.println(" Set app to run in the specified game mode, if supported.");
pw.println(" --user <USER_ID>: apply for the given user,");
pw.println(" the current user is used when unspecified.");
pw.println(" set --mode [2|3|performance|battery] [intervention configs] <PACKAGE_NAME>");
pw.println(" Set app to run at given game mode with configs, if supported.");
pw.println(" set [intervention configs] <PACKAGE_NAME>");
pw.println(" Set app to run at custom mode using provided intervention configs");
pw.println(" Intervention configs consists of:");
pw.println(" --downscale [0.3|0.35|0.4|0.45|0.5|0.55|0.6|0.65");
pw.println(" |0.7|0.75|0.8|0.85|0.9|disable]");
pw.println(" Set app to run at the specified scaling ratio.");
pw.println(" --fps [30|45|60|90|120|disable]");
pw.println(" Set app to run at the specified fps, if supported.");
pw.println(" |0.7|0.75|0.8|0.85|0.9|disable]: Set app to run at the");
pw.println(" specified scaling ratio.");
pw.println(" --fps [30|45|60|90|120|disable]: Set app to run at the specified fps,");
pw.println(" if supported.");
pw.println(" reset [--mode [2|3|performance|battery] --user <USER_ID>] <PACKAGE_NAME>");
pw.println(" Resets the game mode of the app to device configuration.");
pw.println(" This should only be used to reset any override to non custom game mode");
pw.println(" applied using the deprecated `set` command");
pw.println(" --mode [2|3|performance|battery]: apply for the given mode,");
pw.println(" resets all modes when unspecified.");
pw.println(" --user <USER_ID>: apply for the given user,");