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

Commit 8fe347b0 authored by Xiang Wang's avatar Xiang Wang Committed by Android (Google) Code Review
Browse files

Merge changes I0317cf08,Ie1db1889,I0909bc7c

* changes:
  Add updateCustomGameModeConfiguration system API
  Add GameModeConfiguration
  Add opt-in game modes and intervention flags to GameModeInfo
parents 4fbea803 4ab05d06
Loading
Loading
Loading
Loading
+34 −1
Original line number Diff line number Diff line
@@ -811,17 +811,50 @@ package android.app {
  public final class GameManager {
    method @Nullable @RequiresPermission(android.Manifest.permission.MANAGE_GAME_MODE) public android.app.GameModeInfo getGameModeInfo(@NonNull String);
    method @RequiresPermission(android.Manifest.permission.MANAGE_GAME_MODE) public void setGameMode(@NonNull String, int);
    method @RequiresPermission(android.Manifest.permission.MANAGE_GAME_MODE) public void updateCustomGameModeConfiguration(@NonNull String, @NonNull android.app.GameModeConfiguration);
  }
  public final class GameModeConfiguration implements android.os.Parcelable {
    method public int describeContents();
    method public int getFpsOverride();
    method public float getScalingFactor();
    method @NonNull public android.app.GameModeConfiguration.Builder toBuilder();
    method public void writeToParcel(@NonNull android.os.Parcel, int);
    field @NonNull public static final android.os.Parcelable.Creator<android.app.GameModeConfiguration> CREATOR;
    field public static final int FPS_OVERRIDE_NONE = 0; // 0x0
  }
  public static final class GameModeConfiguration.Builder {
    ctor public GameModeConfiguration.Builder();
    method @NonNull public android.app.GameModeConfiguration build();
    method @NonNull public android.app.GameModeConfiguration.Builder setFpsOverride(int);
    method @NonNull public android.app.GameModeConfiguration.Builder setScalingFactor(float);
  }
  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 @Nullable public android.app.GameModeConfiguration getGameModeConfiguration(int);
    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 setGameModeConfiguration(int, @NonNull android.app.GameModeConfiguration);
    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);
+21 −0
Original line number Diff line number Diff line
@@ -279,4 +279,25 @@ public final class GameManager {
            throw e.rethrowFromSystemServer();
        }
    }

    /**
     * Updates the config for the game's {@link #GAME_MODE_CUSTOM} mode.
     * <p>
     * The caller must have {@link android.Manifest.permission#MANAGE_GAME_MODE}.
     *
     * @param gameModeConfig The configuration to use for game mode interventions
     * @hide
     */
    @SystemApi
    @UserHandleAware
    @RequiresPermission(Manifest.permission.MANAGE_GAME_MODE)
    public void updateCustomGameModeConfiguration(@NonNull String packageName,
            @NonNull GameModeConfiguration gameModeConfig) {
        try {
            mService.updateCustomGameModeConfiguration(packageName, gameModeConfig,
                    mContext.getUserId());
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }
}
+20 −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;

/** @hide*/
parcelable GameModeConfiguration;
 No newline at end of file
+192 −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 android.annotation.NonNull;
import android.annotation.SystemApi;
import android.os.Parcel;
import android.os.Parcelable;
import android.view.Display;

import androidx.annotation.FloatRange;
import androidx.annotation.IntRange;

import com.android.internal.annotations.Immutable;
import com.android.internal.util.Preconditions;

/**
 * GameModeConfiguration is the game's platform configuration for a game mode.
 * <p>
 * Only the game modes that are enabled by OEMs will have an active configuration, whereas game
 * modes opted in by the game will not.
 *
 * @hide
 */
@Immutable
@SystemApi
public final class GameModeConfiguration implements Parcelable {
    // Default value indicating that no FPS override will be applied as game intervention, or
    // default to the current display mode's frame rate.
    public static final int FPS_OVERRIDE_NONE = 0;

    public static final @NonNull Creator<GameModeConfiguration> CREATOR = new Creator<>() {
        @Override
        public GameModeConfiguration createFromParcel(Parcel in) {
            return new GameModeConfiguration(in);
        }

        @Override
        public GameModeConfiguration[] newArray(int size) {
            return new GameModeConfiguration[size];
        }
    };

    /**
     * Builder for {@link GameModeConfiguration}.
     *
     * @hide
     */
    @SystemApi
    public static final class Builder {
        /** Constructs a new Builder for a game mode’s configuration */
        public Builder() {
        }

        /**
         * Sets the scaling factor used for game resolution downscaling.
         * <br>
         *
         * @param scalingFactor the desired scaling factor ranged from 0.1 to 1.0 inclusively
         * @throws IllegalArgumentException if the scaling factor is not in range of [0.1, 1.0]
         */
        @NonNull
        public GameModeConfiguration.Builder setScalingFactor(
                @FloatRange(from = 0.1, to = 1.0) float scalingFactor) {
            Preconditions.checkArgument(scalingFactor >= 0.1 && scalingFactor <= 1.0,
                    "Scaling factor should fall between 0.1 and 1.0 (inclusive)");
            mScalingFactor = scalingFactor;
            return this;
        }

        /**
         * Sets the FPS override used for game frame rate throttling.
         * <br>
         * The list of valid throttled frame rates can be queried by
         * <ol>
         * <li>Obtain display modes by calling {@link Display#getSupportedModes}
         * <li>For each mode, get valid FPS by getting the divisor of the
         * {@link Display.Mode#getRefreshRate()} that is >= 30,
         * e.g. when Display.Mode#getRefreshRate() is 120 Hz, the valid FPS
         * of this mode is 120, 60, 40, 30
         * <li>Aggregate the valid FPS of each mode to get the full list
         * </ol>
         * <br>
         *
         * @param fpsOverride the desired non-negative FPS override value, default to
         *                    {@link #FPS_OVERRIDE_NONE}.
         * @throws IllegalArgumentException if the provided value is negative
         */
        @NonNull
        public GameModeConfiguration.Builder setFpsOverride(@IntRange(from = 0) int fpsOverride) {
            Preconditions.checkArgument(fpsOverride >= 0,
                    "FPS override should be non-negative");
            mFpsOverride = fpsOverride;
            return this;
        }

        /**
         * Builds a GameModeConfiguration.
         */
        @NonNull
        public GameModeConfiguration build() {
            return new GameModeConfiguration(mScalingFactor, mFpsOverride);
        }

        ;
        private float mScalingFactor;
        private int mFpsOverride;
    }

    GameModeConfiguration(float scalingFactor, int fpsOverride) {
        this.mScalingFactor = scalingFactor;
        this.mFpsOverride = fpsOverride;
    }

    GameModeConfiguration(Parcel in) {
        this.mScalingFactor = in.readFloat();
        this.mFpsOverride = in.readInt();
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(@NonNull Parcel dest, int flags) {
        dest.writeFloat(mScalingFactor);
        dest.writeInt(mFpsOverride);
    }

    /**
     * Gets the scaling factor used for game resolution downscaling.
     */
    public float getScalingFactor() {
        return mScalingFactor;
    }

    /**
     * Gets the FPS override used for frame rate throttling.
     */
    public int getFpsOverride() {
        return mFpsOverride;
    }

    /**
     * Converts and returns the game mode config as a new builder.
     */
    @NonNull
    public GameModeConfiguration.Builder toBuilder() {
        return new GameModeConfiguration.Builder()
                .setFpsOverride(mFpsOverride)
                .setScalingFactor(mScalingFactor);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        }
        if (!(obj instanceof GameModeConfiguration)) {
            return false;
        }
        GameModeConfiguration config = (GameModeConfiguration) obj;
        return config.mFpsOverride == this.mFpsOverride
                && config.mScalingFactor == this.mScalingFactor;
    }

    @Override
    public int hashCode() {
        int result = 7;
        result = 31 * result + mFpsOverride;
        result = 31 * result + Float.floatToIntBits(mScalingFactor);
        return result;
    }

    private final float mScalingFactor;
    private final int mFpsOverride;
}
+196 −24
Original line number Diff line number Diff line
@@ -20,9 +20,34 @@ import android.annotation.NonNull;
import android.annotation.SystemApi;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.ArrayMap;

import androidx.annotation.Nullable;

import com.android.internal.annotations.VisibleForTesting;

import java.util.Arrays;
import java.util.Map;

/**
 * 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 +65,196 @@ 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;
        }

        /**
         * Sets the GameModeConfiguration for a game mode.
         */
        @NonNull
        public GameModeInfo.Builder setGameModeConfiguration(
                @GameManager.GameMode int gameMode,
                @NonNull GameModeConfiguration gameModeConfiguration) {
            mConfigMap.put(gameMode, gameModeConfiguration);
            return this;
        }

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

        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;
        private Map<Integer, GameModeConfiguration> mConfigMap = new ArrayMap<>();
    }

    /**
     * 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, new ArrayMap<>());
    }

    private GameModeInfo(@GameManager.GameMode int activeGameMode,
            @NonNull @GameManager.GameMode int[] availableGameModes,
            @NonNull @GameManager.GameMode int[] optedInGameModes, boolean isDownscalingAllowed,
            boolean isFpsOverrideAllowed, @NonNull Map<Integer, GameModeConfiguration> configMap) {
        mActiveGameMode = activeGameMode;
        mAvailableGameModes = availableGameModes;
        mAvailableGameModes = Arrays.copyOf(availableGameModes, availableGameModes.length);
        mOptedInGameModes = Arrays.copyOf(optedInGameModes, optedInGameModes.length);
        mIsDownscalingAllowed = isDownscalingAllowed;
        mIsFpsOverrideAllowed = isFpsOverrideAllowed;
        mConfigMap = configMap;
    }

    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();
        mConfigMap = new ArrayMap<>();
        in.readMap(mConfigMap,
                getClass().getClassLoader(), Integer.class, GameModeConfiguration.class);
    }

    /**
     * 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);
    }

    /**
     * Gets the current game mode configuration of a game mode.
     * <p>
     * The game mode can be null if it's opted in by the game itself, or not configured in device
     * config nor set by the user as custom game mode configuration.
     */
    public @Nullable GameModeConfiguration getGameModeConfiguration(
            @GameManager.GameMode int gameMode) {
        return mConfigMap.get(gameMode);
    }

    /**
     * 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;
    }

    // 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 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;
    private final Map<Integer, GameModeConfiguration> mConfigMap;

    @Override
    public int describeContents() {
@@ -95,7 +264,10 @@ 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);
        dest.writeMap(mConfigMap);
    }
}
Loading