Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 92815089 authored by jimblackler's avatar jimblackler Committed by Jim Blackler
Browse files

Add statsd logging and address API review feedback

Bug: 207662418
Bug: 216294586
Test: atest CtsStatsdAtomHostTestCases:GameManagerStatsTests
Change-Id: Icaa9d49f020f25d982a515b03aa9c91c34d2a778
parent 73141db5
Loading
Loading
Loading
Loading
+3 −3
Original line number Original line Diff line number Diff line
@@ -5560,11 +5560,11 @@ package android.app {
  public final class GameState implements android.os.Parcelable {
  public final class GameState implements android.os.Parcelable {
    ctor public GameState(boolean, int);
    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 public int describeContents();
    method @Nullable public String getDescription();
    method public int getLabel();
    method @NonNull public android.os.Bundle getMetadata();
    method public int getMode();
    method public int getMode();
    method public int getQuality();
    method public boolean isLoading();
    method public boolean isLoading();
    method public void writeToParcel(@NonNull android.os.Parcel, int);
    method public void writeToParcel(@NonNull android.os.Parcel, int);
    field @NonNull public static final android.os.Parcelable.Creator<android.app.GameState> CREATOR;
    field @NonNull public static final android.os.Parcelable.Creator<android.app.GameState> CREATOR;
+23 −25
Original line number Original line Diff line number Diff line
@@ -18,8 +18,6 @@ package android.app;


import android.annotation.IntDef;
import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.Parcelable;


@@ -88,12 +86,11 @@ public final class GameState implements Parcelable {
    // One of the states listed above.
    // One of the states listed above.
    private final @GameStateMode int mMode;
    private final @GameStateMode int mMode;


    // This is a game specific description. For example can be level or scene name.
    // A developer-supplied enum, e.g. to indicate level or scene.
    private final @Nullable String mDescription;
    private final int mLabel;


    // This contains any other game specific parameters not covered by the fields above. It can be
    // The developer-supplied enum, e.g. to indicate the current quality level.
    // quality parameter data, settings, or game modes.
    private final int mQuality;
    private final @NonNull Bundle mMetaData;


    /**
    /**
     * Create a GameState with the specified loading status.
     * 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.
     * @param mode The game state mode of type @GameStateMode.
     */
     */
    public GameState(boolean isLoading, @GameStateMode int mode) {
    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.
     * Create a GameState with the given state variables.
     * @param isLoading Whether the game is in the loading state.
     * @param isLoading Whether the game is in the loading state.
     * @param mode The game state mode of type @GameStateMode.
     * @param mode The game state mode.
     * @param description An optional description of the state.
     * @param label An optional developer-supplied enum e.g. for the current level.
     * @param metaData Optional metadata.
     * @param quality An optional developer-supplied enum, e.g. for the current quality level.
     */
     */
    public GameState(boolean isLoading, @GameStateMode int mode, @Nullable String description,
    public GameState(boolean isLoading, @GameStateMode int mode, int label, int quality) {
            @NonNull Bundle metaData) {
        mIsLoading = isLoading;
        mIsLoading = isLoading;
        mMode = mode;
        mMode = mode;
        mDescription = description;
        mLabel = label;
        mMetaData = metaData;
        mQuality = quality;
    }
    }


    private GameState(Parcel in) {
    private GameState(Parcel in) {
        mIsLoading = in.readBoolean();
        mIsLoading = in.readBoolean();
        mMode = in.readInt();
        mMode = in.readInt();
        mDescription = in.readString();
        mLabel = in.readInt();
        mMetaData = in.readBundle();
        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() {
    public int getLabel() {
        return mDescription;
        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() {
    public int getQuality() {
        return mMetaData;
        return mQuality;
    }
    }


    @Override
    @Override
@@ -163,8 +161,8 @@ public final class GameState implements Parcelable {
    public void writeToParcel(@NonNull Parcel parcel, int flags) {
    public void writeToParcel(@NonNull Parcel parcel, int flags) {
        parcel.writeBoolean(mIsLoading);
        parcel.writeBoolean(mIsLoading);
        parcel.writeInt(mMode);
        parcel.writeInt(mMode);
        parcel.writeString(mDescription);
        parcel.writeInt(mLabel);
        parcel.writeBundle(mMetaData);
        parcel.writeInt(mQuality);
    }
    }


    /**
    /**
+43 −14
Original line number Original line Diff line number Diff line
@@ -90,6 +90,7 @@ import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.compat.CompatibilityOverrideConfig;
import com.android.internal.compat.CompatibilityOverrideConfig;
import com.android.internal.compat.IPlatformCompat;
import com.android.internal.compat.IPlatformCompat;
import com.android.internal.os.BackgroundThread;
import com.android.internal.os.BackgroundThread;
import com.android.internal.util.FrameworkStatsLog;
import com.android.server.LocalServices;
import com.android.server.LocalServices;
import com.android.server.ServiceThread;
import com.android.server.ServiceThread;
import com.android.server.SystemService;
import com.android.server.SystemService;
@@ -268,16 +269,34 @@ public final class GameManagerService extends IGameManagerService.Stub {
                    break;
                    break;
                }
                }
                case SET_GAME_STATE: {
                case SET_GAME_STATE: {
                    if (mPowerManagerInternal == null) {
                    final GameState gameState = (GameState) msg.obj;
                    final boolean isLoading = gameState.isLoading();
                    final Bundle data = msg.getData();
                    final Bundle data = msg.getData();
                        Slog.d(TAG, "Error setting loading mode for package "
                    final String packageName = data.getString(PACKAGE_NAME_MSG_KEY);
                                + data.getString(PACKAGE_NAME_MSG_KEY)
                    final int userId = data.getInt(USER_ID_MSG_KEY);
                                + " and 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;
                            break;
                        }
                        }
                    final GameState gameState = (GameState) msg.obj;
                    final boolean isLoading = gameState.isLoading();
                        mPowerManagerInternal.setPowerMode(Mode.GAME_LOADING, isLoading);
                        mPowerManagerInternal.setPowerMode(Mode.GAME_LOADING, isLoading);
                    }
                    break;
                    break;
                }
                }
            }
            }
@@ -387,12 +406,6 @@ public final class GameManagerService extends IGameManagerService.Stub {
            // Restrict to games only.
            // Restrict to games only.
            return;
            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 Message msg = mHandler.obtainMessage(SET_GAME_STATE);
        final Bundle data = new Bundle();
        final Bundle data = new Bundle();
        data.putString(PACKAGE_NAME_MSG_KEY, packageName);
        data.putString(PACKAGE_NAME_MSG_KEY, packageName);
@@ -1543,6 +1556,22 @@ public final class GameManagerService extends IGameManagerService.Stub {
        return out.toString();
        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() {
    private static ServiceThread createServiceThread() {
        ServiceThread handlerThread = new ServiceThread(TAG,
        ServiceThread handlerThread = new ServiceThread(TAG,
                Process.THREAD_PRIORITY_BACKGROUND, true /*allowIo*/);
                Process.THREAD_PRIORITY_BACKGROUND, true /*allowIo*/);