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

Commit 8954f782 authored by Xiang Wang's avatar Xiang Wang
Browse files

Add opt-in game modes and intervention flags to GameModeInfo

Bug: b/243448953
Test: atest GameManagerServiceTests
Change-Id: I0909bc7cb9193418effd9231f747fe0b838ba366
parent b6f89235
Loading
Loading
Loading
Loading
+14 −1
Original line number Diff line number Diff line
@@ -814,14 +814,27 @@ package android.app {
  }
  public final class GameModeInfo implements android.os.Parcelable {
    ctor public GameModeInfo(int, @NonNull int[]);
    ctor @Deprecated public GameModeInfo(int, @NonNull int[]);
    method public int describeContents();
    method public int getActiveGameMode();
    method @NonNull public int[] getAvailableGameModes();
    method @NonNull public int[] getOptedInGameModes();
    method public boolean isDownscalingAllowed();
    method public boolean isFpsOverrideAllowed();
    method public void writeToParcel(@NonNull android.os.Parcel, int);
    field @NonNull public static final android.os.Parcelable.Creator<android.app.GameModeInfo> CREATOR;
  }
  public static final class GameModeInfo.Builder {
    ctor public GameModeInfo.Builder();
    method @NonNull public android.app.GameModeInfo build();
    method @NonNull public android.app.GameModeInfo.Builder setActiveGameMode(@NonNull int);
    method @NonNull public android.app.GameModeInfo.Builder setAvailableGameModes(@NonNull int[]);
    method @NonNull public android.app.GameModeInfo.Builder setDownscalingAllowed(boolean);
    method @NonNull public android.app.GameModeInfo.Builder setFpsOverrideAllowed(boolean);
    method @NonNull public android.app.GameModeInfo.Builder setOptedInGameModes(@NonNull int[]);
  }
  public abstract class InstantAppResolverService extends android.app.Service {
    ctor public InstantAppResolverService();
    method public final void attachBaseContext(android.content.Context);
+163 −24
Original line number Diff line number Diff line
@@ -21,8 +21,29 @@ import android.annotation.SystemApi;
import android.os.Parcel;
import android.os.Parcelable;

import com.android.internal.annotations.VisibleForTesting;

import java.util.Arrays;

/**
 * GameModeInfo returned from {@link GameManager#getGameModeInfo(String)}.
 *
 * Developers can enable game modes or interventions by adding
 * <pre>{@code
 * <meta-data android:name="android.game_mode_intervention"
 *   android:resource="@xml/GAME_MODE_CONFIG_FILE" />
 * }</pre>
 * to the <pre>{@code <application>}</pre>, where the GAME_MODE_CONFIG_FILE is an XML file that
 * specifies the game mode enablement and intervention configuration:
 * <pre>{@code
 * <game-mode-config xmlns:android="http://schemas.android.com/apk/res/android"
 *   android:gameModePerformance="true"
 *   android:gameModeBattery="false"
 *   android:allowGameDownscaling="true"
 *   android:allowGameFpsOverride="false"
 * />
 * }</pre>
 *
 * @hide
 */
@SystemApi
@@ -40,52 +61,168 @@ public final class GameModeInfo implements Parcelable {
        }
    };

    /**
     * Builder for {@link GameModeInfo}.
     *
     * @hide
     */
    @SystemApi
    public static final class Builder {
        /** Constructs a new Builder for a game mode info. */
        public Builder() {
        }

        /**
         * Sets the available game modes.
         */
        @NonNull
        public GameModeInfo.Builder setAvailableGameModes(
                @NonNull @GameManager.GameMode int[] availableGameModes) {
            mAvailableGameModes = availableGameModes;
            return this;
        }

        /**
         * Sets the opted-in game modes.
         */
        @NonNull
        public GameModeInfo.Builder setOptedInGameModes(
                @NonNull @GameManager.GameMode int[] optedInGameModes) {
            mOptedInGameModes = optedInGameModes;
            return this;
        }

        /**
         * Sets the active game mode.
         */
        @NonNull
        public GameModeInfo.Builder setActiveGameMode(
                @NonNull @GameManager.GameMode int activeGameMode) {
            mActiveGameMode = activeGameMode;
            return this;
        }

        /**
         * Sets the downscaling intervention flag.
         */
        @NonNull
        public GameModeInfo.Builder setDownscalingAllowed(boolean allowed) {
            mIsDownscalingAllowed = allowed;
            return this;
        }

        /**
         * Sets the FPS override flag.
         */
        @NonNull
        public GameModeInfo.Builder setFpsOverrideAllowed(boolean allowed) {
            mIsFpsOverrideAllowed = allowed;
            return this;
        }

        /**
         * Builds a GameModeInfo.
         */
        @NonNull
        public GameModeInfo build() {
            return new GameModeInfo(mActiveGameMode, mAvailableGameModes, mOptedInGameModes,
                    mIsDownscalingAllowed, mIsFpsOverrideAllowed);
        }

        private @GameManager.GameMode int[] mAvailableGameModes = new int[]{};
        private @GameManager.GameMode int[] mOptedInGameModes = new int[]{};
        private @GameManager.GameMode int mActiveGameMode;
        private boolean mIsDownscalingAllowed;
        private boolean mIsFpsOverrideAllowed;
    }

    /**
     * Creates a game mode info.
     *
     * @deprecated Use the {@link Builder} instead.
     */
    public GameModeInfo(@GameManager.GameMode int activeGameMode,
            @NonNull @GameManager.GameMode int[] availableGameModes) {
        this(activeGameMode, availableGameModes, new int[]{}, true, true);
    }

    GameModeInfo(@GameManager.GameMode int activeGameMode,
            @NonNull @GameManager.GameMode int[] availableGameModes,
            @NonNull @GameManager.GameMode int[] optedInGameModes, boolean isDownscalingAllowed,
            boolean isFpsOverrideAllowed) {
        mActiveGameMode = activeGameMode;
        mAvailableGameModes = availableGameModes;
        mAvailableGameModes = Arrays.copyOf(availableGameModes, availableGameModes.length);
        mOptedInGameModes = Arrays.copyOf(optedInGameModes, optedInGameModes.length);
        mIsDownscalingAllowed = isDownscalingAllowed;
        mIsFpsOverrideAllowed = isFpsOverrideAllowed;
    }

    GameModeInfo(Parcel in) {
    /** @hide */
    @VisibleForTesting
    public GameModeInfo(Parcel in) {
        mActiveGameMode = in.readInt();
        final int availableGameModesCount = in.readInt();
        mAvailableGameModes = new int[availableGameModesCount];
        in.readIntArray(mAvailableGameModes);
        mAvailableGameModes = in.createIntArray();
        mOptedInGameModes = in.createIntArray();
        mIsDownscalingAllowed = in.readBoolean();
        mIsFpsOverrideAllowed = in.readBoolean();
    }

    /**
     * Returns the {@link GameManager.GameMode} the application is currently using.
     * Developers can enable game modes by adding
     * <code>
     *     <meta-data android:name="android.game_mode_intervention"
     *             android:resource="@xml/GAME_MODE_CONFIG_FILE" />
     * </code>
     * to the {@link <application> tag}, where the GAME_MODE_CONFIG_FILE is an XML file that
     * specifies the game mode enablement and configuration:
     * <code>
     *     <game-mode-config xmlns:android="http://schemas.android.com/apk/res/android"
     *         android:gameModePerformance="true"
     *         android:gameModeBattery="false"
     *     />
     * </code>
     */
    public @GameManager.GameMode int getActiveGameMode() {
        return mActiveGameMode;
    }

    /**
     * The collection of {@link GameManager.GameMode GameModes} that can be applied to the game.
     * Gets the collection of {@link GameManager.GameMode} that can be applied to the game.
     * <p>
     * Available games include all game modes that are either supported by the OEM in device
     * config, or opted in by the game developers in game mode config XML, plus the default enabled
     * modes for any game including {@link GameManager#GAME_MODE_STANDARD} and
     * {@link GameManager#GAME_MODE_CUSTOM}.
     * <p>
     * Also see {@link GameModeInfo}.
     */
    @NonNull
    public @GameManager.GameMode int[] getAvailableGameModes() {
        return mAvailableGameModes;
        return Arrays.copyOf(mAvailableGameModes, mAvailableGameModes.length);
    }

    /**
     * Gets the collection of {@link GameManager.GameMode} that are opted in by the game.
     * <p>
     * Also see {@link GameModeInfo}.
     */
    @NonNull
    public @GameManager.GameMode int[] getOptedInGameModes() {
        return Arrays.copyOf(mOptedInGameModes, mOptedInGameModes.length);
    }

    // Ideally there should be callback that the caller can register to know when the available
    // GameMode and/or the active GameMode is changed, however, there's no concrete use case
    // at the moment so there's no callback mechanism introduced    .
    /**
     * Returns if downscaling is allowed (not opted out) by the game in their Game Mode config.
     * <p>
     * Also see {@link GameModeInfo}.
     */
    public boolean isDownscalingAllowed() {
        return mIsDownscalingAllowed;
    }

    /**
     * Returns if FPS override is allowed (not opted out) by the game in their Game Mode config.
     * <p>
     * Also see {@link GameModeInfo}.
     */
    public boolean isFpsOverrideAllowed() {
        return mIsFpsOverrideAllowed;
    }


    private final @GameManager.GameMode int[] mAvailableGameModes;
    private final @GameManager.GameMode int[] mOptedInGameModes;
    private final @GameManager.GameMode int mActiveGameMode;
    private final boolean mIsDownscalingAllowed;
    private final boolean mIsFpsOverrideAllowed;

    @Override
    public int describeContents() {
@@ -95,7 +232,9 @@ public final class GameModeInfo implements Parcelable {
    @Override
    public void writeToParcel(@NonNull Parcel dest, int flags) {
        dest.writeInt(mActiveGameMode);
        dest.writeInt(mAvailableGameModes.length);
        dest.writeIntArray(mAvailableGameModes);
        dest.writeIntArray(mOptedInGameModes);
        dest.writeBoolean(mIsDownscalingAllowed);
        dest.writeBoolean(mIsFpsOverrideAllowed);
    }
}
+72 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.app;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import android.os.Parcel;
import android.platform.test.annotations.Presubmit;

import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

/**
 * Unit tests for {@link android.app.GameModeInfo}.
 */
@SmallTest
@Presubmit
@RunWith(AndroidJUnit4.class)
public class GameModeInfoTest {
    @Test
    public void testParcelable() {
        int activeGameMode = GameManager.GAME_MODE_PERFORMANCE;
        int[] availableGameModes =
                new int[]{GameManager.GAME_MODE_STANDARD, GameManager.GAME_MODE_PERFORMANCE,
                        GameManager.GAME_MODE_BATTERY, GameManager.GAME_MODE_CUSTOM};
        int[] optedInGameModes = new int[]{GameManager.GAME_MODE_PERFORMANCE};
        GameModeInfo gameModeInfo = new GameModeInfo.Builder()
                .setActiveGameMode(activeGameMode)
                .setAvailableGameModes(availableGameModes)
                .setOptedInGameModes(optedInGameModes)
                .setDownscalingAllowed(true)
                .setFpsOverrideAllowed(false).build();

        assertArrayEquals(availableGameModes, gameModeInfo.getAvailableGameModes());
        assertArrayEquals(optedInGameModes, gameModeInfo.getOptedInGameModes());
        assertEquals(activeGameMode, gameModeInfo.getActiveGameMode());
        assertTrue(gameModeInfo.isDownscalingAllowed());
        assertFalse(gameModeInfo.isFpsOverrideAllowed());

        Parcel parcel = Parcel.obtain();
        gameModeInfo.writeToParcel(parcel, 0);
        parcel.setDataPosition(0);
        GameModeInfo newGameModeInfo = new GameModeInfo(parcel);
        assertEquals(gameModeInfo.getActiveGameMode(), newGameModeInfo.getActiveGameMode());
        assertArrayEquals(gameModeInfo.getAvailableGameModes(),
                newGameModeInfo.getAvailableGameModes());
        assertArrayEquals(gameModeInfo.getOptedInGameModes(),
                newGameModeInfo.getOptedInGameModes());
        assertTrue(newGameModeInfo.isDownscalingAllowed());
        assertFalse(newGameModeInfo.isFpsOverrideAllowed());
    }
}
+29 −3
Original line number Diff line number Diff line
@@ -762,6 +762,21 @@ public final class GameManagerService extends IGameManagerService.Stub {
            return modes;
        }

        /**
         * Get an array of a package's opted-in game modes.
         */
        public @GameMode int[] getOptedInGameModes() {
            if (mBatteryModeOptedIn && mPerfModeOptedIn) {
                return new int[]{GameManager.GAME_MODE_BATTERY, GameManager.GAME_MODE_PERFORMANCE};
            } else if (mBatteryModeOptedIn) {
                return new int[]{GameManager.GAME_MODE_BATTERY};
            } else if (mPerfModeOptedIn) {
                return new int[]{GameManager.GAME_MODE_PERFORMANCE};
            } else {
                return new int[]{};
            }
        }

        /**
         * Get a GameModeConfiguration for a given game mode.
         *
@@ -1022,9 +1037,20 @@ public final class GameManagerService extends IGameManagerService.Stub {
        }

        final @GameMode int activeGameMode = getGameModeFromSettings(packageName, userId);
        final @GameMode int[] availableGameModes = getAvailableGameModesUnchecked(packageName);

        return new GameModeInfo(activeGameMode, availableGameModes);
        final GamePackageConfiguration config = getConfig(packageName, userId);
        if (config != null) {
            final @GameMode int[] optedInGameModes = config.getOptedInGameModes();
            final @GameMode int[] availableGameModes = config.getAvailableGameModes();
            return new GameModeInfo.Builder()
                    .setActiveGameMode(activeGameMode)
                    .setAvailableGameModes(availableGameModes)
                    .setOptedInGameModes(optedInGameModes)
                    .setDownscalingAllowed(config.mAllowDownscale)
                    .setFpsOverrideAllowed(config.mAllowFpsOverride)
                    .build();
        } else {
            return new GameModeInfo.Builder().setActiveGameMode(activeGameMode).build();
        }
    }

    /**
+9 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="UTF-8"?>
<game-mode-config
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:supportsPerformanceGameMode="false"
    android:supportsBatteryGameMode="true"
    android:allowGameAngleDriver="true"
    android:allowGameDownscaling="true"
    android:allowGameFpsOverride="true"
/>
 No newline at end of file
Loading