• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.app;
18 
19 import android.Manifest;
20 import android.annotation.IntDef;
21 import android.annotation.NonNull;
22 import android.annotation.Nullable;
23 import android.annotation.RequiresPermission;
24 import android.annotation.SystemService;
25 import android.annotation.TestApi;
26 import android.annotation.UserHandleAware;
27 import android.content.Context;
28 import android.os.Handler;
29 import android.os.RemoteException;
30 import android.os.ServiceManager;
31 import android.os.ServiceManager.ServiceNotFoundException;
32 
33 import java.lang.annotation.Retention;
34 import java.lang.annotation.RetentionPolicy;
35 
36 /**
37  * The GameManager allows system apps to modify and query the game mode of apps.
38  */
39 @SystemService(Context.GAME_SERVICE)
40 public final class GameManager {
41 
42     private static final String TAG = "GameManager";
43 
44     private final @Nullable Context mContext;
45     private final IGameManagerService mService;
46 
47     /** @hide */
48     @IntDef(flag = false, prefix = {"GAME_MODE_"}, value = {
49             GAME_MODE_UNSUPPORTED, // 0
50             GAME_MODE_STANDARD, // 1
51             GAME_MODE_PERFORMANCE, // 2
52             GAME_MODE_BATTERY, // 3
53     })
54     @Retention(RetentionPolicy.SOURCE)
55     public @interface GameMode {
56     }
57 
58     /**
59      * Game mode is not supported for this application.
60      */
61     public static final int GAME_MODE_UNSUPPORTED = 0;
62 
63     /**
64      * Standard game mode means the platform will use the game's default
65      * performance characteristics.
66      */
67     public static final int GAME_MODE_STANDARD = 1;
68 
69     /**
70      * Performance game mode maximizes the game's performance.
71      * <p>
72      * This game mode is highly likely to increase battery consumption.
73      */
74     public static final int GAME_MODE_PERFORMANCE = 2;
75 
76     /**
77      * Battery game mode will save battery and give longer game play time.
78      */
79     public static final int GAME_MODE_BATTERY = 3;
80 
GameManager(Context context, Handler handler)81     GameManager(Context context, Handler handler) throws ServiceNotFoundException {
82         mContext = context;
83         mService = IGameManagerService.Stub.asInterface(
84                 ServiceManager.getServiceOrThrow(Context.GAME_SERVICE));
85     }
86 
87     /**
88      * Return the user selected game mode for this application.
89      * <p>
90      * An application can use <code>android:isGame="true"</code> or
91      * <code>android:appCategory="game"</code> to indicate that the application is a game. If an
92      * application is not a game, always return {@link #GAME_MODE_UNSUPPORTED}.
93      * <p>
94      * Developers should call this API every time the application is resumed.
95      */
getGameMode()96     public @GameMode int getGameMode() {
97         try {
98             return mService.getGameMode(mContext.getPackageName(), mContext.getUserId());
99         } catch (RemoteException e) {
100             throw e.rethrowFromSystemServer();
101         }
102     }
103 
104     /**
105      * Gets the game mode for the given package.
106      * <p>
107      * The caller must have {@link android.Manifest.permission#MANAGE_GAME_MODE}.
108      *
109      * @hide
110      */
111     @UserHandleAware
112     @RequiresPermission(Manifest.permission.MANAGE_GAME_MODE)
getGameMode(@onNull String packageName)113     public @GameMode int getGameMode(@NonNull String packageName) {
114         try {
115             return mService.getGameMode(packageName, mContext.getUserId());
116         } catch (RemoteException e) {
117             throw e.rethrowFromSystemServer();
118         }
119     }
120 
121     /**
122      * Sets the game mode for the given package.
123      * <p>
124      * The caller must have {@link android.Manifest.permission#MANAGE_GAME_MODE}.
125      *
126      * @hide
127      */
128     @TestApi
129     @UserHandleAware
130     @RequiresPermission(Manifest.permission.MANAGE_GAME_MODE)
setGameMode(@onNull String packageName, @GameMode int gameMode)131     public void setGameMode(@NonNull String packageName, @GameMode int gameMode) {
132         try {
133             mService.setGameMode(packageName, gameMode, mContext.getUserId());
134         } catch (RemoteException e) {
135             throw e.rethrowFromSystemServer();
136         }
137     }
138     /**
139      * Returns a list of supported game modes for a given package.
140      * <p>
141      * The caller must have {@link android.Manifest.permission#MANAGE_GAME_MODE}.
142      *
143      * @hide
144      */
145     @RequiresPermission(Manifest.permission.MANAGE_GAME_MODE)
getAvailableGameModes(@onNull String packageName)146     public @GameMode int[] getAvailableGameModes(@NonNull String packageName) {
147         try {
148             return mService.getAvailableGameModes(packageName);
149         } catch (RemoteException e) {
150             throw e.rethrowFromSystemServer();
151         }
152     }
153 
154 }
155