1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.service.games;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SdkConstant;
import android.annotation.SystemApi;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.hardware.display.DisplayManager;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.view.Display;
import android.view.SurfaceControlViewHost;
import android.view.WindowManager;
import com.android.internal.infra.AndroidFuture;
import com.android.internal.util.function.pooled.PooledLambda;
import java.util.Objects;
/**
* Service that hosts active game sessions.
*
* This service should be in a separate process from the {@link GameService}. This
* allows it to perform the heavyweight operations associated with rendering a game
* session overlay while games are running and release these resources (by allowing
* the process to be killed) when games are not running.
*
* Game Service providers must extend {@link GameSessionService} and declare the service in their
* Manifest. The service must require the {@link android.Manifest.permission#BIND_GAME_SERVICE} so
* that other application can not abuse it. This service is used to create instances of
* {@link GameSession} via {@link #onNewSession(CreateGameSessionRequest)} and will remain bound to
* so long as at least one {@link GameSession} is running.
*
* @hide
*/
@SystemApi
public abstract class GameSessionService extends Service {
/**
* The {@link Intent} action used when binding to the service.
* To be supported, the service must require the
* {@link android.Manifest.permission#BIND_GAME_SERVICE} permission so
* that other applications can not abuse it.
*/
@SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION)
public static final String ACTION_GAME_SESSION_SERVICE =
"android.service.games.action.GAME_SESSION_SERVICE";
private final IGameSessionService mInterface = new IGameSessionService.Stub() {
@Override
public void create(
IGameSessionController gameSessionController,
CreateGameSessionRequest createGameSessionRequest,
GameSessionViewHostConfiguration gameSessionViewHostConfiguration,
AndroidFuture gameSessionFuture) {
Handler.getMain().post(PooledLambda.obtainRunnable(
GameSessionService::doCreate, GameSessionService.this,
gameSessionController,
createGameSessionRequest,
gameSessionViewHostConfiguration,
gameSessionFuture));
}
};
private DisplayManager mDisplayManager;
@Override
public void onCreate() {
super.onCreate();
mDisplayManager = this.getSystemService(DisplayManager.class);
}
@Override
@Nullable
public final IBinder onBind(@Nullable Intent intent) {
if (intent == null) {
return null;
}
if (!ACTION_GAME_SESSION_SERVICE.equals(intent.getAction())) {
return null;
}
return mInterface.asBinder();
}
private void doCreate(
IGameSessionController gameSessionController,
CreateGameSessionRequest createGameSessionRequest,
GameSessionViewHostConfiguration gameSessionViewHostConfiguration,
AndroidFuture<CreateGameSessionResult> createGameSessionResultFuture) {
GameSession gameSession = onNewSession(createGameSessionRequest);
Objects.requireNonNull(gameSession);
Display display = mDisplayManager.getDisplay(gameSessionViewHostConfiguration.mDisplayId);
if (display == null) {
createGameSessionResultFuture.completeExceptionally(
new IllegalStateException("No display found for id: "
+ gameSessionViewHostConfiguration.mDisplayId));
return;
}
IBinder hostToken = new Binder();
// Use a WindowContext so that views attached to the SurfaceControlViewHost will receive
// configuration changes (rather than always perceiving the global configuration).
final Context windowContext = createWindowContext(display,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, /*options=*/ null);
SurfaceControlViewHost surfaceControlViewHost =
new SurfaceControlViewHost(windowContext, display, hostToken);
gameSession.attach(
gameSessionController,
createGameSessionRequest.getTaskId(),
windowContext,
surfaceControlViewHost,
gameSessionViewHostConfiguration.mWidthPx,
gameSessionViewHostConfiguration.mHeightPx);
CreateGameSessionResult createGameSessionResult =
new CreateGameSessionResult(gameSession.mInterface,
surfaceControlViewHost.getSurfacePackage());
createGameSessionResultFuture.complete(createGameSessionResult);
gameSession.doCreate();
}
/**
* Request to create a new {@link GameSession}.
*/
@NonNull
public abstract GameSession onNewSession(
@NonNull CreateGameSessionRequest createGameSessionRequest);
}
|