| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.service.games; |
| 18 |
|
| 19 |
import android.annotation.Hide; |
| 20 |
import android.annotation.IntDef; |
| 21 |
import android.annotation.MainThread; |
| 22 |
import android.annotation.NonNull; |
| 23 |
import android.annotation.Nullable; |
| 24 |
import android.annotation.RequiresPermission; |
| 25 |
import android.annotation.SystemApi; |
| 26 |
import android.app.ActivityTaskManager; |
| 27 |
import android.app.Instrumentation; |
| 28 |
import android.content.Context; |
| 29 |
import android.content.Intent; |
| 30 |
import android.content.res.Configuration; |
| 31 |
import android.graphics.Rect; |
| 32 |
import android.os.Binder; |
| 33 |
import android.os.Bundle; |
| 34 |
import android.os.Handler; |
| 35 |
import android.os.Looper; |
| 36 |
import android.os.RemoteException; |
| 37 |
import android.os.UserHandle; |
| 38 |
import android.util.Slog; |
| 39 |
import android.view.SurfaceControlViewHost; |
| 40 |
import android.view.View; |
| 41 |
import android.view.ViewGroup; |
| 42 |
import android.widget.FrameLayout; |
| 43 |
|
| 44 |
import com.android.internal.annotations.VisibleForTesting; |
| 45 |
import com.android.internal.infra.AndroidFuture; |
| 46 |
import com.android.internal.util.function.pooled.PooledLambda; |
| 47 |
|
| 48 |
import java.lang.annotation.Retention; |
| 49 |
import java.lang.annotation.RetentionPolicy; |
| 50 |
import java.util.Objects; |
| 51 |
import java.util.concurrent.Executor; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
@SystemApi |
| 82 |
public abstract class GameSession { |
| 83 |
private static final String TAG = "GameSession"; |
| 84 |
private static final boolean DEBUG = false; |
| 85 |
|
| 86 |
final IGameSession mInterface = new IGameSession.Stub() { |
| 87 |
@Override |
| 88 |
public void onDestroyed() { |
| 89 |
Handler.getMain().executeOrSendMessage(PooledLambda.obtainMessage( |
| 90 |
GameSession::doDestroy, GameSession.this)); |
| 91 |
} |
| 92 |
|
| 93 |
@Override |
| 94 |
public void onTransientSystemBarVisibilityFromRevealGestureChanged( |
| 95 |
boolean visibleDueToGesture) { |
| 96 |
Handler.getMain().executeOrSendMessage(PooledLambda.obtainMessage( |
| 97 |
GameSession::dispatchTransientSystemBarVisibilityFromRevealGestureChanged, |
| 98 |
GameSession.this, |
| 99 |
visibleDueToGesture)); |
| 100 |
} |
| 101 |
|
| 102 |
@Override |
| 103 |
public void onTaskFocusChanged(boolean focused) { |
| 104 |
Handler.getMain().executeOrSendMessage(PooledLambda.obtainMessage( |
| 105 |
GameSession::moveToState, GameSession.this, |
| 106 |
focused ? LifecycleState.TASK_FOCUSED : LifecycleState.TASK_UNFOCUSED)); |
| 107 |
} |
| 108 |
}; |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
@VisibleForTesting |
| 114 |
public enum LifecycleState { |
| 115 |
|
| 116 |
INITIALIZED, |
| 117 |
|
| 118 |
CREATED, |
| 119 |
|
| 120 |
TASK_FOCUSED, |
| 121 |
|
| 122 |
TASK_UNFOCUSED, |
| 123 |
|
| 124 |
DESTROYED |
| 125 |
} |
| 126 |
|
| 127 |
private LifecycleState mLifecycleState = LifecycleState.INITIALIZED; |
| 128 |
private boolean mAreTransientInsetsVisibleDueToGesture = false; |
| 129 |
private IGameSessionController mGameSessionController; |
| 130 |
private Context mContext; |
| 131 |
private int mTaskId; |
| 132 |
private GameSessionRootView mGameSessionRootView; |
| 133 |
private SurfaceControlViewHost mSurfaceControlViewHost; |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
@VisibleForTesting |
| 139 |
public void attach( |
| 140 |
IGameSessionController gameSessionController, |
| 141 |
int taskId, |
| 142 |
@NonNull Context context, |
| 143 |
@NonNull SurfaceControlViewHost surfaceControlViewHost, |
| 144 |
int widthPx, |
| 145 |
int heightPx) { |
| 146 |
mGameSessionController = gameSessionController; |
| 147 |
mTaskId = taskId; |
| 148 |
mContext = context; |
| 149 |
mSurfaceControlViewHost = surfaceControlViewHost; |
| 150 |
mGameSessionRootView = new GameSessionRootView(context, mSurfaceControlViewHost); |
| 151 |
surfaceControlViewHost.setView(mGameSessionRootView, widthPx, heightPx); |
| 152 |
} |
| 153 |
|
| 154 |
@Hide |
| 155 |
void doCreate() { |
| 156 |
moveToState(LifecycleState.CREATED); |
| 157 |
} |
| 158 |
|
| 159 |
@Hide |
| 160 |
private void doDestroy() { |
| 161 |
mSurfaceControlViewHost.release(); |
| 162 |
moveToState(LifecycleState.DESTROYED); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
@VisibleForTesting |
| 167 |
@MainThread |
| 168 |
public void dispatchTransientSystemBarVisibilityFromRevealGestureChanged( |
| 169 |
boolean visibleDueToGesture) { |
| 170 |
boolean didValueChange = mAreTransientInsetsVisibleDueToGesture != visibleDueToGesture; |
| 171 |
mAreTransientInsetsVisibleDueToGesture = visibleDueToGesture; |
| 172 |
if (didValueChange) { |
| 173 |
onTransientSystemBarVisibilityFromRevealGestureChanged(visibleDueToGesture); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
@VisibleForTesting |
| 181 |
@MainThread |
| 182 |
public void moveToState(LifecycleState newLifecycleState) { |
| 183 |
if (DEBUG) { |
| 184 |
Slog.d(TAG, "moveToState: " + mLifecycleState + " -> " + newLifecycleState); |
| 185 |
} |
| 186 |
|
| 187 |
if (Looper.myLooper() != Looper.getMainLooper()) { |
| 188 |
throw new RuntimeException("moveToState should be used only from the main thread"); |
| 189 |
} |
| 190 |
|
| 191 |
if (mLifecycleState == newLifecycleState) { |
| 192 |
|
| 193 |
return; |
| 194 |
} |
| 195 |
|
| 196 |
switch (mLifecycleState) { |
| 197 |
case INITIALIZED: |
| 198 |
if (newLifecycleState == LifecycleState.CREATED) { |
| 199 |
onCreate(); |
| 200 |
} else if (newLifecycleState == LifecycleState.DESTROYED) { |
| 201 |
onCreate(); |
| 202 |
onDestroy(); |
| 203 |
} else { |
| 204 |
if (DEBUG) { |
| 205 |
Slog.d(TAG, "Ignoring moveToState: INITIALIZED -> " + newLifecycleState); |
| 206 |
} |
| 207 |
return; |
| 208 |
} |
| 209 |
break; |
| 210 |
case CREATED: |
| 211 |
if (newLifecycleState == LifecycleState.TASK_FOCUSED) { |
| 212 |
onGameTaskFocusChanged( true); |
| 213 |
} else if (newLifecycleState == LifecycleState.DESTROYED) { |
| 214 |
onDestroy(); |
| 215 |
} else { |
| 216 |
if (DEBUG) { |
| 217 |
Slog.d(TAG, "Ignoring moveToState: CREATED -> " + newLifecycleState); |
| 218 |
} |
| 219 |
return; |
| 220 |
} |
| 221 |
break; |
| 222 |
case TASK_FOCUSED: |
| 223 |
if (newLifecycleState == LifecycleState.TASK_UNFOCUSED) { |
| 224 |
onGameTaskFocusChanged( false); |
| 225 |
} else if (newLifecycleState == LifecycleState.DESTROYED) { |
| 226 |
onGameTaskFocusChanged( false); |
| 227 |
onDestroy(); |
| 228 |
} else { |
| 229 |
if (DEBUG) { |
| 230 |
Slog.d(TAG, "Ignoring moveToState: TASK_FOCUSED -> " + newLifecycleState); |
| 231 |
} |
| 232 |
return; |
| 233 |
} |
| 234 |
break; |
| 235 |
case TASK_UNFOCUSED: |
| 236 |
if (newLifecycleState == LifecycleState.TASK_FOCUSED) { |
| 237 |
onGameTaskFocusChanged( true); |
| 238 |
} else if (newLifecycleState == LifecycleState.DESTROYED) { |
| 239 |
onDestroy(); |
| 240 |
} else { |
| 241 |
if (DEBUG) { |
| 242 |
Slog.d(TAG, "Ignoring moveToState: TASK_UNFOCUSED -> " + newLifecycleState); |
| 243 |
} |
| 244 |
return; |
| 245 |
} |
| 246 |
break; |
| 247 |
case DESTROYED: |
| 248 |
if (DEBUG) { |
| 249 |
Slog.d(TAG, "Ignoring moveToState: DESTROYED -> " + newLifecycleState); |
| 250 |
} |
| 251 |
return; |
| 252 |
} |
| 253 |
|
| 254 |
mLifecycleState = newLifecycleState; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
public void onCreate() { |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
public void onDestroy() { |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
public void onGameTaskFocusChanged(boolean focused) { |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
public void onTransientSystemBarVisibilityFromRevealGestureChanged( |
| 301 |
boolean visibleDueToGesture) { |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
public void setTaskOverlayView( |
| 317 |
@NonNull View view, |
| 318 |
@NonNull ViewGroup.LayoutParams layoutParams) { |
| 319 |
mGameSessionRootView.removeAllViews(); |
| 320 |
mGameSessionRootView.addView(view, layoutParams); |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
@RequiresPermission(android.Manifest.permission.MANAGE_GAME_ACTIVITY) |
| 331 |
public final boolean restartGame() { |
| 332 |
try { |
| 333 |
mGameSessionController.restartGame(mTaskId); |
| 334 |
} catch (RemoteException e) { |
| 335 |
Slog.w(TAG, "Failed to restart game", e); |
| 336 |
return false; |
| 337 |
} |
| 338 |
|
| 339 |
return true; |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
private static final class GameSessionRootView extends FrameLayout { |
| 348 |
private final SurfaceControlViewHost mSurfaceControlViewHost; |
| 349 |
|
| 350 |
GameSessionRootView(@NonNull Context context, |
| 351 |
SurfaceControlViewHost surfaceControlViewHost) { |
| 352 |
super(context); |
| 353 |
mSurfaceControlViewHost = surfaceControlViewHost; |
| 354 |
} |
| 355 |
|
| 356 |
@Override |
| 357 |
protected void onConfigurationChanged(Configuration newConfig) { |
| 358 |
super.onConfigurationChanged(newConfig); |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
Rect bounds = newConfig.windowConfiguration.getBounds(); |
| 363 |
mSurfaceControlViewHost.relayout(bounds.width(), bounds.height()); |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
public interface ScreenshotCallback { |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
@IntDef(flag = false, prefix = {"ERROR_TAKE_SCREENSHOT_"}, value = { |
| 378 |
ERROR_TAKE_SCREENSHOT_INTERNAL_ERROR, |
| 379 |
}) |
| 380 |
@Retention(RetentionPolicy.SOURCE) |
| 381 |
@interface ScreenshotFailureStatus { |
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
int ERROR_TAKE_SCREENSHOT_INTERNAL_ERROR = 0; |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
void onFailure(@ScreenshotFailureStatus int statusCode); |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
void onSuccess(); |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
@RequiresPermission(android.Manifest.permission.MANAGE_GAME_ACTIVITY) |
| 429 |
public void takeScreenshot(@NonNull Executor executor, @NonNull ScreenshotCallback callback) { |
| 430 |
if (mGameSessionController == null) { |
| 431 |
throw new IllegalStateException("Can not call before onCreate()"); |
| 432 |
} |
| 433 |
|
| 434 |
AndroidFuture<GameScreenshotResult> takeScreenshotResult = |
| 435 |
new AndroidFuture<GameScreenshotResult>().whenCompleteAsync((result, error) -> { |
| 436 |
handleScreenshotResult(callback, result, error); |
| 437 |
}, executor); |
| 438 |
|
| 439 |
try { |
| 440 |
mGameSessionController.takeScreenshot(mTaskId, takeScreenshotResult); |
| 441 |
} catch (RemoteException ex) { |
| 442 |
takeScreenshotResult.completeExceptionally(ex); |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
private void handleScreenshotResult( |
| 447 |
@NonNull ScreenshotCallback callback, |
| 448 |
@NonNull GameScreenshotResult result, |
| 449 |
@NonNull Throwable error) { |
| 450 |
if (error != null) { |
| 451 |
Slog.w(TAG, error.getMessage(), error.getCause()); |
| 452 |
callback.onFailure( |
| 453 |
ScreenshotCallback.ERROR_TAKE_SCREENSHOT_INTERNAL_ERROR); |
| 454 |
return; |
| 455 |
} |
| 456 |
|
| 457 |
@GameScreenshotResult.GameScreenshotStatus int status = result.getStatus(); |
| 458 |
switch (status) { |
| 459 |
case GameScreenshotResult.GAME_SCREENSHOT_SUCCESS: |
| 460 |
callback.onSuccess(); |
| 461 |
break; |
| 462 |
case GameScreenshotResult.GAME_SCREENSHOT_ERROR_INTERNAL_ERROR: |
| 463 |
Slog.w(TAG, "Error taking screenshot"); |
| 464 |
callback.onFailure( |
| 465 |
ScreenshotCallback.ERROR_TAKE_SCREENSHOT_INTERNAL_ERROR); |
| 466 |
break; |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
@RequiresPermission(android.Manifest.permission.MANAGE_GAME_ACTIVITY) |
| 496 |
public final void startActivityFromGameSessionForResult( |
| 497 |
@NonNull Intent intent, @Nullable Bundle options, @NonNull Executor executor, |
| 498 |
@NonNull GameSessionActivityCallback callback) { |
| 499 |
Objects.requireNonNull(intent); |
| 500 |
Objects.requireNonNull(executor); |
| 501 |
Objects.requireNonNull(callback); |
| 502 |
|
| 503 |
AndroidFuture<GameSessionActivityResult> future = |
| 504 |
new AndroidFuture<GameSessionActivityResult>() |
| 505 |
.whenCompleteAsync((result, ex) -> { |
| 506 |
if (ex != null) { |
| 507 |
callback.onActivityStartFailed(ex); |
| 508 |
return; |
| 509 |
} |
| 510 |
callback.onActivityResult(result.getResultCode(), result.getData()); |
| 511 |
}, executor); |
| 512 |
|
| 513 |
final Intent trampolineIntent = |
| 514 |
GameSessionTrampolineActivity.createIntent( |
| 515 |
intent, |
| 516 |
options, |
| 517 |
future); |
| 518 |
|
| 519 |
try { |
| 520 |
int result = ActivityTaskManager.getService().startActivityFromGameSession( |
| 521 |
mContext.getIApplicationThread(), mContext.getPackageName(), "GameSession", |
| 522 |
Binder.getCallingPid(), Binder.getCallingUid(), trampolineIntent, mTaskId, |
| 523 |
UserHandle.myUserId()); |
| 524 |
Instrumentation.checkStartActivityResult(result, trampolineIntent); |
| 525 |
} catch (Throwable t) { |
| 526 |
executor.execute(() -> callback.onActivityStartFailed(t)); |
| 527 |
} |
| 528 |
} |
| 529 |
} |