| 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.annotation.SdkConstant; |
| 22 |
import android.annotation.SystemApi; |
| 23 |
import android.app.Service; |
| 24 |
import android.content.Context; |
| 25 |
import android.content.Intent; |
| 26 |
import android.hardware.display.DisplayManager; |
| 27 |
import android.os.Binder; |
| 28 |
import android.os.Handler; |
| 29 |
import android.os.IBinder; |
| 30 |
import android.view.Display; |
| 31 |
import android.view.SurfaceControlViewHost; |
| 32 |
import android.view.WindowManager; |
| 33 |
|
| 34 |
import com.android.internal.infra.AndroidFuture; |
| 35 |
import com.android.internal.util.function.pooled.PooledLambda; |
| 36 |
|
| 37 |
import java.util.Objects; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
@SystemApi |
| 56 |
public abstract class GameSessionService extends Service { |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
@SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION) |
| 64 |
public static final String ACTION_GAME_SESSION_SERVICE = |
| 65 |
"android.service.games.action.GAME_SESSION_SERVICE"; |
| 66 |
|
| 67 |
private final IGameSessionService mInterface = new IGameSessionService.Stub() { |
| 68 |
@Override |
| 69 |
public void create( |
| 70 |
IGameSessionController gameSessionController, |
| 71 |
CreateGameSessionRequest createGameSessionRequest, |
| 72 |
GameSessionViewHostConfiguration gameSessionViewHostConfiguration, |
| 73 |
AndroidFuture gameSessionFuture) { |
| 74 |
Handler.getMain().post(PooledLambda.obtainRunnable( |
| 75 |
GameSessionService::doCreate, GameSessionService.this, |
| 76 |
gameSessionController, |
| 77 |
createGameSessionRequest, |
| 78 |
gameSessionViewHostConfiguration, |
| 79 |
gameSessionFuture)); |
| 80 |
} |
| 81 |
}; |
| 82 |
|
| 83 |
private DisplayManager mDisplayManager; |
| 84 |
|
| 85 |
@Override |
| 86 |
public void onCreate() { |
| 87 |
super.onCreate(); |
| 88 |
mDisplayManager = this.getSystemService(DisplayManager.class); |
| 89 |
} |
| 90 |
|
| 91 |
@Override |
| 92 |
@Nullable |
| 93 |
public final IBinder onBind(@Nullable Intent intent) { |
| 94 |
if (intent == null) { |
| 95 |
return null; |
| 96 |
} |
| 97 |
|
| 98 |
if (!ACTION_GAME_SESSION_SERVICE.equals(intent.getAction())) { |
| 99 |
return null; |
| 100 |
} |
| 101 |
|
| 102 |
return mInterface.asBinder(); |
| 103 |
} |
| 104 |
|
| 105 |
private void doCreate( |
| 106 |
IGameSessionController gameSessionController, |
| 107 |
CreateGameSessionRequest createGameSessionRequest, |
| 108 |
GameSessionViewHostConfiguration gameSessionViewHostConfiguration, |
| 109 |
AndroidFuture<CreateGameSessionResult> createGameSessionResultFuture) { |
| 110 |
GameSession gameSession = onNewSession(createGameSessionRequest); |
| 111 |
Objects.requireNonNull(gameSession); |
| 112 |
|
| 113 |
Display display = mDisplayManager.getDisplay(gameSessionViewHostConfiguration.mDisplayId); |
| 114 |
if (display == null) { |
| 115 |
createGameSessionResultFuture.completeExceptionally( |
| 116 |
new IllegalStateException("No display found for id: " |
| 117 |
+ gameSessionViewHostConfiguration.mDisplayId)); |
| 118 |
return; |
| 119 |
} |
| 120 |
|
| 121 |
IBinder hostToken = new Binder(); |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
final Context windowContext = createWindowContext(display, |
| 126 |
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, null); |
| 127 |
SurfaceControlViewHost surfaceControlViewHost = |
| 128 |
new SurfaceControlViewHost(windowContext, display, hostToken); |
| 129 |
|
| 130 |
gameSession.attach( |
| 131 |
gameSessionController, |
| 132 |
createGameSessionRequest.getTaskId(), |
| 133 |
windowContext, |
| 134 |
surfaceControlViewHost, |
| 135 |
gameSessionViewHostConfiguration.mWidthPx, |
| 136 |
gameSessionViewHostConfiguration.mHeightPx); |
| 137 |
|
| 138 |
CreateGameSessionResult createGameSessionResult = |
| 139 |
new CreateGameSessionResult(gameSession.mInterface, |
| 140 |
surfaceControlViewHost.getSurfacePackage()); |
| 141 |
|
| 142 |
createGameSessionResultFuture.complete(createGameSessionResult); |
| 143 |
|
| 144 |
gameSession.doCreate(); |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
@NonNull |
| 151 |
public abstract GameSession onNewSession( |
| 152 |
@NonNull CreateGameSessionRequest createGameSessionRequest); |
| 153 |
} |