| 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.Manifest; |
| 20 |
import android.annotation.IntRange; |
| 21 |
import android.annotation.NonNull; |
| 22 |
import android.annotation.Nullable; |
| 23 |
import android.annotation.RequiresPermission; |
| 24 |
import android.annotation.SdkConstant; |
| 25 |
import android.annotation.SystemApi; |
| 26 |
import android.app.IGameManagerService; |
| 27 |
import android.app.Service; |
| 28 |
import android.content.Context; |
| 29 |
import android.content.Intent; |
| 30 |
import android.os.Handler; |
| 31 |
import android.os.IBinder; |
| 32 |
import android.os.RemoteException; |
| 33 |
import android.os.ServiceManager; |
| 34 |
import android.util.Log; |
| 35 |
|
| 36 |
import com.android.internal.util.function.pooled.PooledLambda; |
| 37 |
|
| 38 |
import java.util.Objects; |
| 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 |
@SystemApi |
| 71 |
public class GameService extends Service { |
| 72 |
private static final String TAG = "GameService"; |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
@SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION) |
| 81 |
public static final String ACTION_GAME_SERVICE = |
| 82 |
"android.service.games.action.GAME_SERVICE"; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public static final String SERVICE_META_DATA = "android.game_service"; |
| 91 |
|
| 92 |
private IGameServiceController mGameServiceController; |
| 93 |
private IGameManagerService mGameManagerService; |
| 94 |
private final IGameService mInterface = new IGameService.Stub() { |
| 95 |
@Override |
| 96 |
public void connected(IGameServiceController gameServiceController) { |
| 97 |
Handler.getMain().executeOrSendMessage(PooledLambda.obtainMessage( |
| 98 |
GameService::doOnConnected, GameService.this, gameServiceController)); |
| 99 |
} |
| 100 |
|
| 101 |
@Override |
| 102 |
public void disconnected() { |
| 103 |
Handler.getMain().executeOrSendMessage(PooledLambda.obtainMessage( |
| 104 |
GameService::onDisconnected, GameService.this)); |
| 105 |
} |
| 106 |
|
| 107 |
@Override |
| 108 |
public void gameStarted(GameStartedEvent gameStartedEvent) { |
| 109 |
Handler.getMain().executeOrSendMessage(PooledLambda.obtainMessage( |
| 110 |
GameService::onGameStarted, GameService.this, gameStartedEvent)); |
| 111 |
} |
| 112 |
}; |
| 113 |
|
| 114 |
private final IBinder.DeathRecipient mGameManagerServiceDeathRecipient = () -> { |
| 115 |
Log.w(TAG, "System service binder died. Shutting down"); |
| 116 |
|
| 117 |
Handler.getMain().executeOrSendMessage(PooledLambda.obtainMessage( |
| 118 |
GameService::onDisconnected, GameService.this)); |
| 119 |
}; |
| 120 |
|
| 121 |
@Override |
| 122 |
@Nullable |
| 123 |
public final IBinder onBind(@Nullable Intent intent) { |
| 124 |
if (ACTION_GAME_SERVICE.equals(intent.getAction())) { |
| 125 |
return mInterface.asBinder(); |
| 126 |
} |
| 127 |
|
| 128 |
return null; |
| 129 |
} |
| 130 |
|
| 131 |
private void doOnConnected(@NonNull IGameServiceController gameServiceController) { |
| 132 |
mGameManagerService = |
| 133 |
IGameManagerService.Stub.asInterface( |
| 134 |
ServiceManager.getService(Context.GAME_SERVICE)); |
| 135 |
Objects.requireNonNull(mGameManagerService); |
| 136 |
try { |
| 137 |
mGameManagerService.asBinder().linkToDeath(mGameManagerServiceDeathRecipient, 0); |
| 138 |
} catch (RemoteException e) { |
| 139 |
Log.w(TAG, "Unable to link to death with system service"); |
| 140 |
} |
| 141 |
|
| 142 |
mGameServiceController = gameServiceController; |
| 143 |
onConnected(); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
public void onConnected() {} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public void onDisconnected() {} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
public void onGameStarted(@NonNull GameStartedEvent gameStartedEvent) {} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
@RequiresPermission(Manifest.permission.MANAGE_GAME_ACTIVITY) |
| 179 |
public final void createGameSession(@IntRange(from = 0) int taskId) { |
| 180 |
if (mGameServiceController == null) { |
| 181 |
throw new IllegalStateException("Can not call before connected()"); |
| 182 |
} |
| 183 |
|
| 184 |
try { |
| 185 |
mGameServiceController.createGameSession(taskId); |
| 186 |
} catch (RemoteException e) { |
| 187 |
Log.e(TAG, "Request for game session failed", e); |
| 188 |
} |
| 189 |
} |
| 190 |
} |