From f44ddcc068d1d387a19c197080a1ae64576c8b18 Mon Sep 17 00:00:00 2001 From: jimblackler Date: Thu, 13 Jan 2022 16:52:29 +0000 Subject: [PATCH 1/2] Add statsd logging and address API review feedback Bug: 207662418 Bug: 216294586 Test: atest CtsStatsdAtomHostTestCases:GameManagerStatsTests Change-Id: Icaa9d49f020f25d982a515b03aa9c91c34d2a778 Merged-In: Icaa9d49f020f25d982a515b03aa9c91c34d2a778 --- core/api/current.txt | 6 +- core/java/android/app/GameState.java | 48 ++++++++-------- .../server/app/GameManagerService.java | 57 ++++++++++++++----- 3 files changed, 69 insertions(+), 42 deletions(-) diff --git a/core/api/current.txt b/core/api/current.txt index ddfb14b83f6c1..3d5e8d85856d7 100644 --- a/core/api/current.txt +++ b/core/api/current.txt @@ -5559,11 +5559,11 @@ package android.app { public final class GameState implements android.os.Parcelable { ctor public GameState(boolean, int); - ctor public GameState(boolean, int, @Nullable String, @NonNull android.os.Bundle); + ctor public GameState(boolean, int, int, int); method public int describeContents(); - method @Nullable public String getDescription(); - method @NonNull public android.os.Bundle getMetadata(); + method public int getLabel(); method public int getMode(); + method public int getQuality(); method public boolean isLoading(); method public void writeToParcel(@NonNull android.os.Parcel, int); field @NonNull public static final android.os.Parcelable.Creator CREATOR; diff --git a/core/java/android/app/GameState.java b/core/java/android/app/GameState.java index 979dd34e8276c..fe6e5543a6625 100644 --- a/core/java/android/app/GameState.java +++ b/core/java/android/app/GameState.java @@ -18,8 +18,6 @@ package android.app; import android.annotation.IntDef; import android.annotation.NonNull; -import android.annotation.Nullable; -import android.os.Bundle; import android.os.Parcel; import android.os.Parcelable; @@ -88,12 +86,11 @@ public final class GameState implements Parcelable { // One of the states listed above. private final @GameStateMode int mMode; - // This is a game specific description. For example can be level or scene name. - private final @Nullable String mDescription; + // A developer-supplied enum, e.g. to indicate level or scene. + private final int mLabel; - // This contains any other game specific parameters not covered by the fields above. It can be - // quality parameter data, settings, or game modes. - private final @NonNull Bundle mMetaData; + // The developer-supplied enum, e.g. to indicate the current quality level. + private final int mQuality; /** * Create a GameState with the specified loading status. @@ -101,29 +98,28 @@ public final class GameState implements Parcelable { * @param mode The game state mode of type @GameStateMode. */ public GameState(boolean isLoading, @GameStateMode int mode) { - this(isLoading, mode, null, new Bundle()); + this(isLoading, mode, -1, -1); } /** * Create a GameState with the given state variables. * @param isLoading Whether the game is in the loading state. - * @param mode The game state mode of type @GameStateMode. - * @param description An optional description of the state. - * @param metaData Optional metadata. + * @param mode The game state mode. + * @param label An optional developer-supplied enum e.g. for the current level. + * @param quality An optional developer-supplied enum, e.g. for the current quality level. */ - public GameState(boolean isLoading, @GameStateMode int mode, @Nullable String description, - @NonNull Bundle metaData) { + public GameState(boolean isLoading, @GameStateMode int mode, int label, int quality) { mIsLoading = isLoading; mMode = mode; - mDescription = description; - mMetaData = metaData; + mLabel = label; + mQuality = quality; } private GameState(Parcel in) { mIsLoading = in.readBoolean(); mMode = in.readInt(); - mDescription = in.readString(); - mMetaData = in.readBundle(); + mLabel = in.readInt(); + mQuality = in.readInt(); } /** @@ -141,17 +137,19 @@ public final class GameState implements Parcelable { } /** - * @return The state description, or null if one is not set. + * @return The developer-supplied enum, e.g. to indicate level or scene. The default value (if + * not supplied) is -1. */ - public @Nullable String getDescription() { - return mDescription; + public int getLabel() { + return mLabel; } /** - * @return metadata associated with the state. + * @return The developer-supplied enum, e.g. to indicate the current quality level. The default + * value (if not suplied) is -1. */ - public @NonNull Bundle getMetadata() { - return mMetaData; + public int getQuality() { + return mQuality; } @Override @@ -163,8 +161,8 @@ public final class GameState implements Parcelable { public void writeToParcel(@NonNull Parcel parcel, int flags) { parcel.writeBoolean(mIsLoading); parcel.writeInt(mMode); - parcel.writeString(mDescription); - parcel.writeBundle(mMetaData); + parcel.writeInt(mLabel); + parcel.writeInt(mQuality); } /** diff --git a/services/core/java/com/android/server/app/GameManagerService.java b/services/core/java/com/android/server/app/GameManagerService.java index b813bc48118a5..0edbea0dbd28e 100644 --- a/services/core/java/com/android/server/app/GameManagerService.java +++ b/services/core/java/com/android/server/app/GameManagerService.java @@ -90,6 +90,7 @@ import com.android.internal.annotations.VisibleForTesting; import com.android.internal.compat.CompatibilityOverrideConfig; import com.android.internal.compat.IPlatformCompat; import com.android.internal.os.BackgroundThread; +import com.android.internal.util.FrameworkStatsLog; import com.android.server.LocalServices; import com.android.server.ServiceThread; import com.android.server.SystemService; @@ -268,16 +269,34 @@ public final class GameManagerService extends IGameManagerService.Stub { break; } case SET_GAME_STATE: { - if (mPowerManagerInternal == null) { - final Bundle data = msg.getData(); - Slog.d(TAG, "Error setting loading mode for package " - + data.getString(PACKAGE_NAME_MSG_KEY) - + " and userId " + data.getInt(USER_ID_MSG_KEY)); - break; - } final GameState gameState = (GameState) msg.obj; final boolean isLoading = gameState.isLoading(); - mPowerManagerInternal.setPowerMode(Mode.GAME_LOADING, isLoading); + final Bundle data = msg.getData(); + final String packageName = data.getString(PACKAGE_NAME_MSG_KEY); + final int userId = data.getInt(USER_ID_MSG_KEY); + + // Restrict to games only. Requires performance mode to be enabled. + final boolean boostEnabled = + getGameMode(packageName, userId) == GameManager.GAME_MODE_PERFORMANCE; + int uid; + try { + uid = mPackageManager.getPackageUidAsUser(packageName, userId); + } catch (NameNotFoundException e) { + Slog.v(TAG, "Failed to get package metadata"); + uid = -1; + } + FrameworkStatsLog.write(FrameworkStatsLog.GAME_STATE_CHANGED, packageName, uid, + boostEnabled, gameStateModeToStatsdGameState(gameState.getMode()), + isLoading, gameState.getLabel(), gameState.getQuality()); + + if (boostEnabled) { + if (mPowerManagerInternal == null) { + Slog.d(TAG, "Error setting loading mode for package " + packageName + + " and userId " + userId); + break; + } + mPowerManagerInternal.setPowerMode(Mode.GAME_LOADING, isLoading); + } break; } } @@ -387,12 +406,6 @@ public final class GameManagerService extends IGameManagerService.Stub { // Restrict to games only. return; } - - if (getGameMode(packageName, userId) != GameManager.GAME_MODE_PERFORMANCE) { - // Requires performance mode to be enabled. - return; - } - final Message msg = mHandler.obtainMessage(SET_GAME_STATE); final Bundle data = new Bundle(); data.putString(PACKAGE_NAME_MSG_KEY, packageName); @@ -1543,6 +1556,22 @@ public final class GameManagerService extends IGameManagerService.Stub { return out.toString(); } + private static int gameStateModeToStatsdGameState(int mode) { + switch (mode) { + case GameState.MODE_NONE: + return FrameworkStatsLog.GAME_STATE_CHANGED__STATE__MODE_NONE; + case GameState.MODE_GAMEPLAY_INTERRUPTIBLE: + return FrameworkStatsLog.GAME_STATE_CHANGED__STATE__MODE_GAMEPLAY_INTERRUPTIBLE; + case GameState.MODE_GAMEPLAY_UNINTERRUPTIBLE: + return FrameworkStatsLog.GAME_STATE_CHANGED__STATE__MODE_GAMEPLAY_UNINTERRUPTIBLE; + case GameState.MODE_CONTENT: + return FrameworkStatsLog.GAME_STATE_CHANGED__STATE__MODE_CONTENT; + case GameState.MODE_UNKNOWN: + default: + return FrameworkStatsLog.GAME_STATE_CHANGED__STATE__MODE_UNKNOWN; + } + } + private static ServiceThread createServiceThread() { ServiceThread handlerThread = new ServiceThread(TAG, Process.THREAD_PRIORITY_BACKGROUND, true /*allowIo*/); From f8cbc0b6f437c24e35f3e26e68d9c67f23c5b874 Mon Sep 17 00:00:00 2001 From: Jim Blackler Date: Mon, 7 Feb 2022 22:28:58 +0000 Subject: [PATCH 2/2] Increase API coverage for android.app.GameState Test: atest com.android.server.app.GameManagerServiceTests Bug: 215435581 Change-Id: I30e9157133bb6e35f9c8d00b4150c233919c4137 Merged-In: I30e9157133bb6e35f9c8d00b4150c233919c4137 --- .../com/android/server/app/GameManagerServiceTests.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/services/tests/mockingservicestests/src/com/android/server/app/GameManagerServiceTests.java b/services/tests/mockingservicestests/src/com/android/server/app/GameManagerServiceTests.java index d2358a08624d1..023608c68ee33 100644 --- a/services/tests/mockingservicestests/src/com/android/server/app/GameManagerServiceTests.java +++ b/services/tests/mockingservicestests/src/com/android/server/app/GameManagerServiceTests.java @@ -1167,7 +1167,14 @@ public class GameManagerServiceTests { startUser(gameManagerService, USER_ID_1); gameManagerService.setGameMode( mPackageName, GameManager.GAME_MODE_PERFORMANCE, USER_ID_1); - GameState gameState = new GameState(isLoading, GameState.MODE_NONE); + int testMode = GameState.MODE_NONE; + int testLabel = 99; + int testQuality = 123; + GameState gameState = new GameState(isLoading, testMode, testLabel, testQuality); + assertEquals(isLoading, gameState.isLoading()); + assertEquals(testMode, gameState.getMode()); + assertEquals(testLabel, gameState.getLabel()); + assertEquals(testQuality, gameState.getQuality()); gameManagerService.setGameState(mPackageName, gameState, USER_ID_1); mTestLooper.dispatchAll(); verify(mMockPowerManager, times(1)).setPowerMode(Mode.GAME_LOADING, isLoading);