| 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.NonNull; |
| 20 |
import android.annotation.Nullable; |
| 21 |
import android.app.Activity; |
| 22 |
import android.content.ComponentName; |
| 23 |
import android.content.Intent; |
| 24 |
import android.os.Bundle; |
| 25 |
import android.util.Slog; |
| 26 |
|
| 27 |
import com.android.internal.annotations.VisibleForTesting; |
| 28 |
import com.android.internal.infra.AndroidFuture; |
| 29 |
|
| 30 |
import java.util.concurrent.Executor; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
@VisibleForTesting |
| 42 |
public final class GameSessionTrampolineActivity extends Activity { |
| 43 |
private static final String TAG = "GameSessionTrampoline"; |
| 44 |
private static final int REQUEST_CODE = 1; |
| 45 |
|
| 46 |
static final String FUTURE_KEY = "GameSessionTrampolineActivity.future"; |
| 47 |
static final String INTENT_KEY = "GameSessionTrampolineActivity.intent"; |
| 48 |
static final String OPTIONS_KEY = "GameSessionTrampolineActivity.options"; |
| 49 |
private static final String HAS_LAUNCHED_INTENT_KEY = |
| 50 |
"GameSessionTrampolineActivity.hasLaunchedIntent"; |
| 51 |
private boolean mHasLaunchedIntent = false; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
@VisibleForTesting |
| 67 |
public static Intent createIntent( |
| 68 |
@NonNull Intent targetIntent, |
| 69 |
@Nullable Bundle options, |
| 70 |
@NonNull AndroidFuture<GameSessionActivityResult> resultFuture) { |
| 71 |
final Intent trampolineIntent = new Intent(); |
| 72 |
trampolineIntent.setComponent( |
| 73 |
new ComponentName( |
| 74 |
"android", "android.service.games.GameSessionTrampolineActivity")); |
| 75 |
trampolineIntent.putExtra(INTENT_KEY, targetIntent); |
| 76 |
trampolineIntent.putExtra(OPTIONS_KEY, options); |
| 77 |
trampolineIntent.putExtra(FUTURE_KEY, resultFuture); |
| 78 |
|
| 79 |
return trampolineIntent; |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
protected void onCreate(@Nullable Bundle savedInstanceState) { |
| 84 |
super.onCreate(savedInstanceState); |
| 85 |
|
| 86 |
if (savedInstanceState != null) { |
| 87 |
mHasLaunchedIntent = savedInstanceState.getBoolean(HAS_LAUNCHED_INTENT_KEY); |
| 88 |
} |
| 89 |
|
| 90 |
if (mHasLaunchedIntent) { |
| 91 |
return; |
| 92 |
} |
| 93 |
mHasLaunchedIntent = true; |
| 94 |
|
| 95 |
try { |
| 96 |
startActivityAsCaller( |
| 97 |
getIntent().getParcelableExtra(INTENT_KEY), |
| 98 |
getIntent().getBundleExtra(OPTIONS_KEY), |
| 99 |
false, |
| 100 |
getUserId(), |
| 101 |
REQUEST_CODE); |
| 102 |
} catch (Exception e) { |
| 103 |
Slog.w(TAG, "Unable to launch activity from game session"); |
| 104 |
AndroidFuture<GameSessionActivityResult> future = getIntent().getParcelableExtra( |
| 105 |
FUTURE_KEY); |
| 106 |
future.completeExceptionally(e); |
| 107 |
finish(); |
| 108 |
overridePendingTransition(0, 0); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
@Override |
| 113 |
protected void onSaveInstanceState(Bundle outState) { |
| 114 |
super.onSaveInstanceState(outState); |
| 115 |
outState.putBoolean(HAS_LAUNCHED_INTENT_KEY, mHasLaunchedIntent); |
| 116 |
} |
| 117 |
|
| 118 |
@Override |
| 119 |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 120 |
if (requestCode != REQUEST_CODE) { |
| 121 |
|
| 122 |
throw new IllegalStateException("Unexpected request code: " + requestCode); |
| 123 |
} |
| 124 |
|
| 125 |
AndroidFuture<GameSessionActivityResult> future = getIntent().getParcelableExtra( |
| 126 |
FUTURE_KEY); |
| 127 |
future.complete(new GameSessionActivityResult(resultCode, data)); |
| 128 |
finish(); |
| 129 |
overridePendingTransition(0, 0); |
| 130 |
} |
| 131 |
} |