From 51ccdb0f399c82858c979d7de27e59345fc5692f Mon Sep 17 00:00:00 2001 From: Sungsoo Lim Date: Thu, 14 Apr 2016 14:05:02 -0700 Subject: [PATCH] MediaBrowserService: Provide a way to retrieve root hints Bug: 28075963 Change-Id: Id8993e67732a2dda5bf75ecab6037dbbba8c8c99 --- api/current.txt | 1 + api/system-current.txt | 1 + api/test-current.txt | 1 + .../android/media/browse/MediaBrowser.java | 4 +-- .../service/media/IMediaBrowserService.aidl | 2 +- .../service/media/MediaBrowserService.java | 35 ++++++++++++++++--- 6 files changed, 37 insertions(+), 7 deletions(-) diff --git a/api/current.txt b/api/current.txt index f924b5b8c54a8..1f5c85e1f4383 100644 --- a/api/current.txt +++ b/api/current.txt @@ -34650,6 +34650,7 @@ package android.service.media { public abstract class MediaBrowserService extends android.app.Service { ctor public MediaBrowserService(); method public void dump(java.io.FileDescriptor, java.io.PrintWriter, java.lang.String[]); + method public final android.os.Bundle getBrowserRootHints(); method public android.media.session.MediaSession.Token getSessionToken(); method public void notifyChildrenChanged(java.lang.String); method public void notifyChildrenChanged(java.lang.String, android.os.Bundle); diff --git a/api/system-current.txt b/api/system-current.txt index 915ceb20a6a53..a70b58ad0d50d 100644 --- a/api/system-current.txt +++ b/api/system-current.txt @@ -37116,6 +37116,7 @@ package android.service.media { public abstract class MediaBrowserService extends android.app.Service { ctor public MediaBrowserService(); method public void dump(java.io.FileDescriptor, java.io.PrintWriter, java.lang.String[]); + method public final android.os.Bundle getBrowserRootHints(); method public android.media.session.MediaSession.Token getSessionToken(); method public void notifyChildrenChanged(java.lang.String); method public void notifyChildrenChanged(java.lang.String, android.os.Bundle); diff --git a/api/test-current.txt b/api/test-current.txt index 0c86fd1a32e53..54ff7cc78d02f 100644 --- a/api/test-current.txt +++ b/api/test-current.txt @@ -34725,6 +34725,7 @@ package android.service.media { public abstract class MediaBrowserService extends android.app.Service { ctor public MediaBrowserService(); method public void dump(java.io.FileDescriptor, java.io.PrintWriter, java.lang.String[]); + method public final android.os.Bundle getBrowserRootHints(); method public android.media.session.MediaSession.Token getSessionToken(); method public void notifyChildrenChanged(java.lang.String); method public void notifyChildrenChanged(java.lang.String, android.os.Bundle); diff --git a/media/java/android/media/browse/MediaBrowser.java b/media/java/android/media/browse/MediaBrowser.java index 2cd98721bd41f..baa6b0fc27ef4 100644 --- a/media/java/android/media/browse/MediaBrowser.java +++ b/media/java/android/media/browse/MediaBrowser.java @@ -135,7 +135,7 @@ public final class MediaBrowser { mContext = context; mServiceComponent = serviceComponent; mCallback = callback; - mRootHints = rootHints; + mRootHints = rootHints == null ? null : new Bundle(rootHints); } /** @@ -444,7 +444,7 @@ public final class MediaBrowser { } }; try { - mServiceBinder.getMediaItem(mediaId, receiver); + mServiceBinder.getMediaItem(mediaId, receiver, mServiceCallbacks); } catch (RemoteException e) { Log.i(TAG, "Remote error getting media item."); mHandler.post(new Runnable() { diff --git a/media/java/android/service/media/IMediaBrowserService.aidl b/media/java/android/service/media/IMediaBrowserService.aidl index eef5a7ccecad2..6b6a561168180 100644 --- a/media/java/android/service/media/IMediaBrowserService.aidl +++ b/media/java/android/service/media/IMediaBrowserService.aidl @@ -20,5 +20,5 @@ oneway interface IMediaBrowserService { void addSubscription(String uri, in IBinder token, in Bundle options, IMediaBrowserServiceCallbacks callbacks); void removeSubscription(String uri, in IBinder token, IMediaBrowserServiceCallbacks callbacks); - void getMediaItem(String uri, in ResultReceiver cb); + void getMediaItem(String uri, in ResultReceiver cb, IMediaBrowserServiceCallbacks callbacks); } diff --git a/media/java/android/service/media/MediaBrowserService.java b/media/java/android/service/media/MediaBrowserService.java index ddc0e882f9f5a..4b88926ce4d43 100644 --- a/media/java/android/service/media/MediaBrowserService.java +++ b/media/java/android/service/media/MediaBrowserService.java @@ -97,6 +97,7 @@ public abstract class MediaBrowserService extends Service { private @interface ResultFlags { } private final ArrayMap mConnections = new ArrayMap<>(); + private ConnectionRecord mCurConnection; private final Handler mHandler = new Handler(); private ServiceBinder mBinder; MediaSession.Token mSession; @@ -291,7 +292,8 @@ public abstract class MediaBrowserService extends Service { } @Override - public void getMediaItem(final String mediaId, final ResultReceiver receiver) { + public void getMediaItem(final String mediaId, final ResultReceiver receiver, + final IMediaBrowserServiceCallbacks callbacks) { if (TextUtils.isEmpty(mediaId) || receiver == null) { return; } @@ -299,7 +301,13 @@ public abstract class MediaBrowserService extends Service { mHandler.post(new Runnable() { @Override public void run() { - performLoadItem(mediaId, receiver); + final IBinder b = callbacks.asBinder(); + ConnectionRecord connection = mConnections.get(b); + if (connection == null) { + Log.w(TAG, "getMediaItem for callback that isn't registered id=" + mediaId); + return; + } + performLoadItem(mediaId, connection, receiver); } }); } @@ -469,6 +477,20 @@ public abstract class MediaBrowserService extends Service { return mSession; } + /** + * Gets the root hints sent from the currently connected {@link MediaBrowser}. + * + * @throws IllegalStateException If this method is called outside of {@link #onLoadChildren} + * or {@link #onLoadItem} + */ + public final Bundle getBrowserRootHints() { + if (mCurConnection == null) { + throw new IllegalStateException("This should be called inside of onLoadChildren or" + + " onLoadItem methods"); + } + return mCurConnection.rootHints == null ? null : new Bundle(mCurConnection.rootHints); + } + /** * Notifies all connected media browsers that the children of * the specified parent id have changed in some way. @@ -619,11 +641,13 @@ public abstract class MediaBrowserService extends Service { } }; + mCurConnection = connection; if (options == null) { onLoadChildren(parentId, result); } else { onLoadChildren(parentId, result, options); } + mCurConnection = null; if (!result.isDone()) { throw new IllegalStateException("onLoadChildren must call detach() or sendResult()" @@ -652,7 +676,8 @@ public abstract class MediaBrowserService extends Service { return list.subList(fromIndex, toIndex); } - private void performLoadItem(String itemId, final ResultReceiver receiver) { + private void performLoadItem(String itemId, final ConnectionRecord connection, + final ResultReceiver receiver) { final Result result = new Result(itemId) { @Override @@ -663,7 +688,9 @@ public abstract class MediaBrowserService extends Service { } }; - MediaBrowserService.this.onLoadItem(itemId, result); + mCurConnection = connection; + onLoadItem(itemId, result); + mCurConnection = null; if (!result.isDone()) { throw new IllegalStateException("onLoadItem must call detach() or sendResult()"