| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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.SystemApi; |
| 25 |
import android.annotation.SystemService; |
| 26 |
import android.annotation.TestApi; |
| 27 |
import android.annotation.UserHandleAware; |
| 28 |
import android.content.Context; |
| 29 |
import android.os.Handler; |
| 30 |
import android.os.RemoteException; |
| 31 |
import android.os.ServiceManager; |
| 32 |
import android.os.ServiceManager.ServiceNotFoundException; |
| 33 |
|
| 34 |
import java.lang.annotation.Retention; |
| 35 |
import java.lang.annotation.RetentionPolicy; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
@SystemService(Context.GAME_SERVICE) |
| 41 |
public final class GameManager { |
| 42 |
|
| 43 |
private static final String TAG = "GameManager"; |
| 44 |
|
| 45 |
private final @Nullable Context mContext; |
| 46 |
private final IGameManagerService mService; |
| 47 |
|
| 48 |
|
| 49 |
@IntDef(flag = false, prefix = {"GAME_MODE_"}, value = { |
| 50 |
GAME_MODE_UNSUPPORTED, |
| 51 |
GAME_MODE_STANDARD, |
| 52 |
GAME_MODE_PERFORMANCE, |
| 53 |
GAME_MODE_BATTERY, |
| 54 |
}) |
| 55 |
@Retention(RetentionPolicy.SOURCE) |
| 56 |
public @interface GameMode { |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public static final int GAME_MODE_UNSUPPORTED = 0; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public static final int GAME_MODE_STANDARD = 1; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public static final int GAME_MODE_PERFORMANCE = 2; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public static final int GAME_MODE_BATTERY = 3; |
| 81 |
|
| 82 |
GameManager(Context context, Handler handler) throws ServiceNotFoundException { |
| 83 |
mContext = context; |
| 84 |
mService = IGameManagerService.Stub.asInterface( |
| 85 |
ServiceManager.getServiceOrThrow(Context.GAME_SERVICE)); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public @GameMode int getGameMode() { |
| 98 |
try { |
| 99 |
return mService.getGameMode(mContext.getPackageName(), mContext.getUserId()); |
| 100 |
} catch (RemoteException e) { |
| 101 |
throw e.rethrowFromSystemServer(); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
@TestApi |
| 113 |
@UserHandleAware |
| 114 |
@RequiresPermission(Manifest.permission.MANAGE_GAME_MODE) |
| 115 |
public @GameMode int getGameMode(@NonNull String packageName) { |
| 116 |
try { |
| 117 |
return mService.getGameMode(packageName, mContext.getUserId()); |
| 118 |
} catch (RemoteException e) { |
| 119 |
throw e.rethrowFromSystemServer(); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
@SystemApi |
| 138 |
@UserHandleAware |
| 139 |
@RequiresPermission(Manifest.permission.MANAGE_GAME_MODE) |
| 140 |
public @Nullable GameModeInfo getGameModeInfo(@NonNull String packageName) { |
| 141 |
try { |
| 142 |
return mService.getGameModeInfo(packageName, mContext.getUserId()); |
| 143 |
} catch (RemoteException e) { |
| 144 |
throw e.rethrowFromSystemServer(); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
@SystemApi |
| 156 |
@UserHandleAware |
| 157 |
@RequiresPermission(Manifest.permission.MANAGE_GAME_MODE) |
| 158 |
public void setGameMode(@NonNull String packageName, @GameMode int gameMode) { |
| 159 |
try { |
| 160 |
mService.setGameMode(packageName, gameMode, mContext.getUserId()); |
| 161 |
} catch (RemoteException e) { |
| 162 |
throw e.rethrowFromSystemServer(); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
@RequiresPermission(Manifest.permission.MANAGE_GAME_MODE) |
| 174 |
public @GameMode int[] getAvailableGameModes(@NonNull String packageName) { |
| 175 |
try { |
| 176 |
return mService.getAvailableGameModes(packageName); |
| 177 |
} catch (RemoteException e) { |
| 178 |
throw e.rethrowFromSystemServer(); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
@TestApi |
| 193 |
@RequiresPermission(Manifest.permission.MANAGE_GAME_MODE) |
| 194 |
public boolean isAngleEnabled(@NonNull String packageName) { |
| 195 |
try { |
| 196 |
return mService.isAngleEnabled(packageName, mContext.getUserId()); |
| 197 |
} catch (RemoteException e) { |
| 198 |
throw e.rethrowFromSystemServer(); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
@RequiresPermission(Manifest.permission.MANAGE_GAME_MODE) |
| 208 |
public void notifyGraphicsEnvironmentSetup() { |
| 209 |
try { |
| 210 |
mService.notifyGraphicsEnvironmentSetup( |
| 211 |
mContext.getPackageName(), mContext.getUserId()); |
| 212 |
} catch (RemoteException e) { |
| 213 |
throw e.rethrowFromSystemServer(); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
public void setGameState(@NonNull GameState gameState) { |
| 222 |
try { |
| 223 |
mService.setGameState(mContext.getPackageName(), gameState, mContext.getUserId()); |
| 224 |
} catch (RemoteException e) { |
| 225 |
throw e.rethrowFromSystemServer(); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
@TestApi |
| 237 |
public void setGameServiceProvider(@Nullable String packageName) { |
| 238 |
try { |
| 239 |
mService.setGameServiceProvider(packageName); |
| 240 |
} catch (RemoteException e) { |
| 241 |
throw e.rethrowFromSystemServer(); |
| 242 |
} |
| 243 |
} |
| 244 |
} |