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.gamemanager.cts; 18 19 import android.app.Activity; 20 import android.app.GameManager; 21 import android.content.BroadcastReceiver; 22 import android.content.Context; 23 import android.content.Intent; 24 import android.content.IntentFilter; 25 import android.os.Bundle; 26 import android.util.ArrayMap; 27 import android.util.Log; 28 29 import com.android.internal.annotations.GuardedBy; 30 31 import java.util.Map; 32 import java.util.concurrent.CompletableFuture; 33 import java.util.concurrent.TimeUnit; 34 35 public class GameManagerCtsActivity extends Activity { 36 37 private static final String TAG = "GameManagerCtsActivity"; 38 39 Context mContext; 40 GameManager mGameManager; 41 GameModeReceiver mGameModeReceiver; 42 private final Object mGameModeLock = new Object(); 43 @GuardedBy("mGameModeLock") 44 private Map<String, CompletableFuture<Integer>> mReceivedGameModes = new ArrayMap<>(); 45 46 public class GameModeReceiver extends BroadcastReceiver { 47 @Override onReceive(Context context, Intent intent)48 public void onReceive(Context context, Intent intent) { 49 final int gameMode = intent.getIntExtra("game-mode", -1); 50 Log.d(TAG, "Received game mode intent " + intent); 51 final String senderPackage = intent.getStringExtra("sender-package"); 52 if (senderPackage == null) { 53 Log.w(TAG, "Received game mode broadcast without sender package"); 54 return; 55 } 56 synchronized (mGameModeLock) { 57 mReceivedGameModes.putIfAbsent(senderPackage, new CompletableFuture<>()); 58 mReceivedGameModes.get(senderPackage).complete(gameMode); 59 } 60 } 61 } 62 63 @Override onCreate(Bundle savedInstanceState)64 public void onCreate(Bundle savedInstanceState) { 65 super.onCreate(savedInstanceState); 66 mContext = getApplicationContext(); 67 mGameManager = mContext.getSystemService(GameManager.class); 68 69 IntentFilter intentFilter = new IntentFilter("android.gamemanager.cts.GAME_MODE"); 70 mGameModeReceiver = new GameModeReceiver(); 71 registerReceiver(mGameModeReceiver, intentFilter, Context.RECEIVER_EXPORTED); 72 } 73 74 @Override onDestroy()75 protected void onDestroy() { 76 super.onDestroy(); 77 if (mGameModeReceiver != null) { 78 unregisterReceiver(mGameModeReceiver); 79 } 80 } 81 getPackageName()82 public String getPackageName() { 83 return mContext.getPackageName(); 84 } 85 getLastReceivedGameMode(String packageName, long timeoutMillis)86 public int getLastReceivedGameMode(String packageName, long timeoutMillis) throws Exception { 87 final CompletableFuture<Integer> gameModeFuture; 88 synchronized (mGameModeLock) { 89 mReceivedGameModes.putIfAbsent(packageName, new CompletableFuture<>()); 90 gameModeFuture = mReceivedGameModes.get(packageName); 91 } 92 return gameModeFuture.get(timeoutMillis, TimeUnit.SECONDS); 93 } 94 getGameMode()95 public int getGameMode() { 96 return mGameManager.getGameMode(); 97 } 98 99 } 100