diff --git a/Android.bp b/Android.bp index c2024084d95d0..2808c3517ab0a 100644 --- a/Android.bp +++ b/Android.bp @@ -476,6 +476,7 @@ java_defaults { "media/java/android/media/IMediaRouterClient.aidl", "media/java/android/media/IMediaRouterService.aidl", "media/java/android/media/IMediaSession2.aidl", + "media/java/android/media/IMediaSession2Service.aidl", "media/java/android/media/IMediaScannerListener.aidl", "media/java/android/media/IMediaScannerService.aidl", "media/java/android/media/IPlaybackConfigDispatcher.aidl", diff --git a/media/java/android/media/IMediaSession2Service.aidl b/media/java/android/media/IMediaSession2Service.aidl new file mode 100644 index 0000000000000..10ac1be0a36ee --- /dev/null +++ b/media/java/android/media/IMediaSession2Service.aidl @@ -0,0 +1,32 @@ +/* + * Copyright 2019 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.media; + +import android.os.Bundle; +import android.media.Controller2Link; + +/** + * Interface from MediaController2 to MediaSession2Service. + *
+ * Keep this interface oneway. Otherwise a malicious app may implement fake version of this,
+ * and holds calls from controller to make controller owner(s) frozen.
+ * @hide
+ */
+oneway interface IMediaSession2Service {
+ void connect(in Controller2Link caller, int seq, in Bundle connectionRequest) = 0;
+ // Next Id : 1
+}
diff --git a/media/java/android/media/MediaController2.java b/media/java/android/media/MediaController2.java
index 774ea185f57cf..165ea412333e7 100644
--- a/media/java/android/media/MediaController2.java
+++ b/media/java/android/media/MediaController2.java
@@ -26,12 +26,16 @@ import static android.media.Session2Token.TYPE_SESSION;
import android.annotation.NonNull;
import android.annotation.Nullable;
+import android.content.ComponentName;
import android.content.Context;
+import android.content.Intent;
+import android.content.ServiceConnection;
import android.os.Binder;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Process;
+import android.os.RemoteException;
import android.os.ResultReceiver;
import android.util.ArrayMap;
import android.util.ArraySet;
@@ -63,6 +67,7 @@ public class MediaController2 implements AutoCloseable {
private final Executor mCallbackExecutor;
private final Controller2Link mControllerStub;
private final Handler mResultHandler;
+ private final SessionServiceConnection mServiceConnection;
private final Object mLock = new Object();
//@GuardedBy("mLock")
@@ -118,16 +123,25 @@ public class MediaController2 implements AutoCloseable {
mPendingCommands = new ArrayMap<>();
mRequestedCommandSeqNumbers = new ArraySet<>();
+ boolean connectRequested;
if (token.getType() == TYPE_SESSION) {
- connectToSession();
+ mServiceConnection = null;
+ connectRequested = requestConnectToSession();
} else {
- // TODO: Handle connect to session service.
+ mServiceConnection = new SessionServiceConnection();
+ connectRequested = requestConnectToService();
+ }
+ if (!connectRequested) {
+ close();
}
}
@Override
public void close() {
synchronized (mLock) {
+ if (mServiceConnection != null) {
+ mContext.unbindService(mServiceConnection);
+ }
if (mSessionBinder != null) {
try {
mSessionBinder.disconnect(mControllerStub, getNextSeqNumber());
@@ -299,18 +313,55 @@ public class MediaController2 implements AutoCloseable {
}
}
- private void connectToSession() {
- Session2Link sessionBinder = mSessionToken.getSessionLink();
+ private Bundle createConnectionRequest() {
Bundle connectionRequest = new Bundle();
connectionRequest.putString(KEY_PACKAGE_NAME, mContext.getPackageName());
connectionRequest.putInt(KEY_PID, Process.myPid());
+ return connectionRequest;
+ }
+ private boolean requestConnectToSession() {
+ Session2Link sessionBinder = mSessionToken.getSessionLink();
+ Bundle connectionRequest = createConnectionRequest();
try {
sessionBinder.connect(mControllerStub, getNextSeqNumber(), connectionRequest);
} catch (RuntimeException e) {
- Log.w(TAG, "Failed to call connection request. Framework will retry"
- + " automatically");
+ Log.w(TAG, "Failed to call connection request", e);
+ return false;
}
+ return true;
+ }
+
+ private boolean requestConnectToService() {
+ // Service. Needs to get fresh binder whenever connection is needed.
+ final Intent intent = new Intent(MediaSession2Service.SERVICE_INTERFACE);
+ intent.setClassName(mSessionToken.getPackageName(), mSessionToken.getServiceName());
+
+ // Use bindService() instead of startForegroundService() to start session service for three
+ // reasons.
+ // 1. Prevent session service owner's stopSelf() from destroying service.
+ // With the startForegroundService(), service's call of stopSelf() will trigger immediate
+ // onDestroy() calls on the main thread even when onConnect() is running in another
+ // thread.
+ // 2. Minimize APIs for developers to take care about.
+ // With bindService(), developers only need to take care about Service.onBind()
+ // but Service.onStartCommand() should be also taken care about with the
+ // startForegroundService().
+ // 3. Future support for UI-less playback
+ // If a service wants to keep running, it should be either foreground service or
+ // bound service. But there had been request for the feature for system apps
+ // and using bindService() will be better fit with it.
+ synchronized (mLock) {
+ boolean result = mContext.bindService(
+ intent, mServiceConnection, Context.BIND_AUTO_CREATE);
+ if (!result) {
+ Log.w(TAG, "bind to " + mSessionToken + " failed");
+ return false;
+ } else if (DEBUG) {
+ Log.d(TAG, "bind to " + mSessionToken + " succeeded");
+ }
+ }
+ return true;
}
/**
@@ -367,4 +418,59 @@ public class MediaController2 implements AutoCloseable {
public void onCommandResult(@NonNull MediaController2 controller, @NonNull Object token,
@NonNull Session2Command command, @NonNull Session2Command.Result result) {}
}
+
+ // This will be called on the main thread.
+ private class SessionServiceConnection implements ServiceConnection {
+ SessionServiceConnection() {
+ }
+
+ @Override
+ public void onServiceConnected(ComponentName name, IBinder service) {
+ // Note that it's always main-thread.
+ boolean connectRequested = false;
+ try {
+ if (DEBUG) {
+ Log.d(TAG, "onServiceConnected " + name + " " + this);
+ }
+ // Sanity check
+ if (!mSessionToken.getPackageName().equals(name.getPackageName())) {
+ Log.wtf(TAG, "Expected connection to " + mSessionToken.getPackageName()
+ + " but is connected to " + name);
+ return;
+ }
+ IMediaSession2Service iService = IMediaSession2Service.Stub.asInterface(service);
+ if (iService == null) {
+ Log.wtf(TAG, "Service interface is missing.");
+ return;
+ }
+ Bundle connectionRequest = createConnectionRequest();
+ iService.connect(mControllerStub, getNextSeqNumber(), connectionRequest);
+ connectRequested = true;
+ } catch (RemoteException e) {
+ Log.w(TAG, "Service " + name + " has died prematurely", e);
+ } finally {
+ if (!connectRequested) {
+ close();
+ }
+ }
+ }
+
+ @Override
+ public void onServiceDisconnected(ComponentName name) {
+ // Temporal lose of the binding because of the service crash. System will automatically
+ // rebind, so just no-op.
+ if (DEBUG) {
+ Log.w(TAG, "Session service " + name + " is disconnected.");
+ }
+ close();
+ }
+
+ @Override
+ public void onBindingDied(ComponentName name) {
+ // Permanent lose of the binding because of the service package update or removed.
+ // This SessionServiceRecord will be removed accordingly, but forget session binder here
+ // for sure.
+ close();
+ }
+ }
}
diff --git a/media/java/android/media/MediaSession2.java b/media/java/android/media/MediaSession2.java
index e008adf120abe..dceef34596b74 100644
--- a/media/java/android/media/MediaSession2.java
+++ b/media/java/android/media/MediaSession2.java
@@ -31,7 +31,6 @@ import android.content.Context;
import android.content.Intent;
import android.media.session.MediaSessionManager;
import android.media.session.MediaSessionManager.RemoteUserInfo;
-import android.os.Binder;
import android.os.Bundle;
import android.os.Handler;
import android.os.Process;
@@ -41,7 +40,6 @@ import android.util.ArraySet;
import android.util.Log;
import java.util.ArrayList;
-import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -117,15 +115,19 @@ public class MediaSession2 implements AutoCloseable {
@Override
public void close() {
try {
+ List
+ * This API is not generally intended for third party application developers.
+ * Use the AndroidX
+ * Media2 Library
+ * for consistent behavior across all devices.
+ * @hide
+ */
+// TODO: Unhide
+// TODO: Add onUpdateNotification(), and calls it to get Notification for startForegroundService()
+// when a session's player state becomes playing.
+public abstract class MediaSession2Service extends Service {
+ /**
+ * The {@link Intent} that must be declared as handled by the service.
+ */
+ public static final String SERVICE_INTERFACE = "android.media.MediaSession2Service";
+
+ private static final String TAG = "MediaSession2Service";
+ private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
+
+ private final Object mLock = new Object();
+ @GuardedBy("mLock")
+ private Map
+ * Override this method if you need your own initialization. Derived classes MUST call through
+ * to the super class's implementation of this method.
+ */
+ @CallSuper
+ @Override
+ public void onCreate() {
+ super.onCreate();
+ mStub = new MediaSession2ServiceStub(this);
+ }
+
+ @CallSuper
+ @Override
+ @Nullable
+ public IBinder onBind(@NonNull Intent intent) {
+ if (SERVICE_INTERFACE.equals(intent.getAction())) {
+ return mStub;
+ }
+ return null;
+ }
+
+ @CallSuper
+ @Override
+ public int onStartCommand(Intent intent, int flags, int startId) {
+ // TODO: Dispatch media key events to the primary session.
+ return START_STICKY;
+ }
+
+ /**
+ * Called by the system to notify that it is no longer used and is being removed. Do not call
+ * this method directly.
+ *
+ * Override this method if you need your own clean up. Derived classes MUST call through
+ * to the super class's implementation of this method.
+ */
+ @CallSuper
+ @Override
+ public void onDestroy() {
+ super.onDestroy();
+ synchronized (mLock) {
+ for (MediaSession2 session : mSessions.values()) {
+ session.getCallback().setForegroundServiceEventCallback(null);
+ }
+ mSessions.clear();
+ }
+ mStub.close();
+ }
+
+ /**
+ * Called when a {@link MediaController2} is created with the this service's
+ * {@link Session2Token}. Return the primary session for telling the controller which session to
+ * connect.
+ *
+ * Primary session is the highest priority session that this service manages. Here are some
+ * recommendations of the primary session.
+ *
+ * Session returned here will be added to this service automatically. You don't need to call
+ * {@link #addSession(MediaSession2)} for that.
+ *
+ * Session service will accept or reject the connection with the
+ * {@link MediaSession2.SessionCallback} in the session returned here.
+ *
+ * This method is always called on the main thread.
+ *
+ * @return a new session
+ * @see MediaSession2.Builder
+ * @see #getSessions()
+ */
+ @NonNull
+ public abstract MediaSession2 onGetPrimarySession();
+
+ /**
+ * Adds a session to this service.
+ *
+ * Added session will be removed automatically when it's closed, or removed when
+ * {@link #removeSession} is called.
+ *
+ * @param session a session to be added.
+ * @see #removeSession(MediaSession2)
+ */
+ public final void addSession(@NonNull MediaSession2 session) {
+ if (session == null) {
+ throw new IllegalArgumentException("session shouldn't be null");
+ }
+ if (session.isClosed()) {
+ throw new IllegalArgumentException("session is already closed");
+ }
+ synchronized (mLock) {
+ MediaSession2 previousSession = mSessions.get(session.getSessionId());
+ if (previousSession != session) {
+ if (previousSession != null) {
+ Log.w(TAG, "Session ID should be unique, ID=" + session.getSessionId()
+ + ", previous=" + previousSession + ", session=" + session);
+ }
+ return;
+ }
+ mSessions.put(session.getSessionId(), session);
+ session.getCallback().setForegroundServiceEventCallback(
+ new MediaSession2.SessionCallback.ForegroundServiceEventCallback() {
+ @Override
+ public void onSessionClosed(MediaSession2 session) {
+ removeSession(session);
+ }
+ });
+ }
+ }
+
+ /**
+ * Removes a session from this service.
+ *
+ * @param session a session to be removed.
+ * @see #addSession(MediaSession2)
+ */
+ public final void removeSession(@NonNull MediaSession2 session) {
+ if (session == null) {
+ throw new IllegalArgumentException("session shouldn't be null");
+ }
+ synchronized (mLock) {
+ mSessions.remove(session.getSessionId());
+ }
+ }
+
+ /**
+ * Gets the list of {@link MediaSession2}s that you've added to this service.
+ *
+ * @return sessions
+ */
+ public final @NonNull List
+ *
+ *