Merge "MediaBrowserService: Provide a way to retrieve root hints" into nyc-dev

This commit is contained in:
Sungsoo Lim
2016-04-18 17:21:57 +00:00
committed by Android (Google) Code Review
6 changed files with 37 additions and 7 deletions

View File

@@ -34649,6 +34649,7 @@ package android.service.media {
public abstract class MediaBrowserService extends android.app.Service { public abstract class MediaBrowserService extends android.app.Service {
ctor public MediaBrowserService(); ctor public MediaBrowserService();
method public void dump(java.io.FileDescriptor, java.io.PrintWriter, java.lang.String[]); 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 android.media.session.MediaSession.Token getSessionToken();
method public void notifyChildrenChanged(java.lang.String); method public void notifyChildrenChanged(java.lang.String);
method public void notifyChildrenChanged(java.lang.String, android.os.Bundle); method public void notifyChildrenChanged(java.lang.String, android.os.Bundle);

View File

@@ -37304,6 +37304,7 @@ package android.service.media {
public abstract class MediaBrowserService extends android.app.Service { public abstract class MediaBrowserService extends android.app.Service {
ctor public MediaBrowserService(); ctor public MediaBrowserService();
method public void dump(java.io.FileDescriptor, java.io.PrintWriter, java.lang.String[]); 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 android.media.session.MediaSession.Token getSessionToken();
method public void notifyChildrenChanged(java.lang.String); method public void notifyChildrenChanged(java.lang.String);
method public void notifyChildrenChanged(java.lang.String, android.os.Bundle); method public void notifyChildrenChanged(java.lang.String, android.os.Bundle);

View File

@@ -34724,6 +34724,7 @@ package android.service.media {
public abstract class MediaBrowserService extends android.app.Service { public abstract class MediaBrowserService extends android.app.Service {
ctor public MediaBrowserService(); ctor public MediaBrowserService();
method public void dump(java.io.FileDescriptor, java.io.PrintWriter, java.lang.String[]); 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 android.media.session.MediaSession.Token getSessionToken();
method public void notifyChildrenChanged(java.lang.String); method public void notifyChildrenChanged(java.lang.String);
method public void notifyChildrenChanged(java.lang.String, android.os.Bundle); method public void notifyChildrenChanged(java.lang.String, android.os.Bundle);

View File

@@ -135,7 +135,7 @@ public final class MediaBrowser {
mContext = context; mContext = context;
mServiceComponent = serviceComponent; mServiceComponent = serviceComponent;
mCallback = callback; mCallback = callback;
mRootHints = rootHints; mRootHints = rootHints == null ? null : new Bundle(rootHints);
} }
/** /**
@@ -444,7 +444,7 @@ public final class MediaBrowser {
} }
}; };
try { try {
mServiceBinder.getMediaItem(mediaId, receiver); mServiceBinder.getMediaItem(mediaId, receiver, mServiceCallbacks);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.i(TAG, "Remote error getting media item."); Log.i(TAG, "Remote error getting media item.");
mHandler.post(new Runnable() { mHandler.post(new Runnable() {

View File

@@ -20,5 +20,5 @@ oneway interface IMediaBrowserService {
void addSubscription(String uri, in IBinder token, in Bundle options, void addSubscription(String uri, in IBinder token, in Bundle options,
IMediaBrowserServiceCallbacks callbacks); IMediaBrowserServiceCallbacks callbacks);
void removeSubscription(String uri, in IBinder token, 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);
} }

View File

@@ -97,6 +97,7 @@ public abstract class MediaBrowserService extends Service {
private @interface ResultFlags { } private @interface ResultFlags { }
private final ArrayMap<IBinder, ConnectionRecord> mConnections = new ArrayMap<>(); private final ArrayMap<IBinder, ConnectionRecord> mConnections = new ArrayMap<>();
private ConnectionRecord mCurConnection;
private final Handler mHandler = new Handler(); private final Handler mHandler = new Handler();
private ServiceBinder mBinder; private ServiceBinder mBinder;
MediaSession.Token mSession; MediaSession.Token mSession;
@@ -291,7 +292,8 @@ public abstract class MediaBrowserService extends Service {
} }
@Override @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) { if (TextUtils.isEmpty(mediaId) || receiver == null) {
return; return;
} }
@@ -299,7 +301,13 @@ public abstract class MediaBrowserService extends Service {
mHandler.post(new Runnable() { mHandler.post(new Runnable() {
@Override @Override
public void run() { 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; 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 * Notifies all connected media browsers that the children of
* the specified parent id have changed in some way. * the specified parent id have changed in some way.
@@ -619,11 +641,13 @@ public abstract class MediaBrowserService extends Service {
} }
}; };
mCurConnection = connection;
if (options == null) { if (options == null) {
onLoadChildren(parentId, result); onLoadChildren(parentId, result);
} else { } else {
onLoadChildren(parentId, result, options); onLoadChildren(parentId, result, options);
} }
mCurConnection = null;
if (!result.isDone()) { if (!result.isDone()) {
throw new IllegalStateException("onLoadChildren must call detach() or sendResult()" throw new IllegalStateException("onLoadChildren must call detach() or sendResult()"
@@ -652,7 +676,8 @@ public abstract class MediaBrowserService extends Service {
return list.subList(fromIndex, toIndex); 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<MediaBrowser.MediaItem> result = final Result<MediaBrowser.MediaItem> result =
new Result<MediaBrowser.MediaItem>(itemId) { new Result<MediaBrowser.MediaItem>(itemId) {
@Override @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()) { if (!result.isDone()) {
throw new IllegalStateException("onLoadItem must call detach() or sendResult()" throw new IllegalStateException("onLoadItem must call detach() or sendResult()"