MediaBrowser: Make ItemCallback can take a null media item

When the implementation of MediaBrowserService returns null,
ItemCallback.onError was called, but the JavaDoc is saying that
ItemCallback.onItemLoaded can be called with a null result.

This CL makes ItemCallback.onItemLoaded be able to take a null result,
and ItemCallback.onError be called when the implementation doesn't override
the onLoadItem method or when the returned object is not MediaItem.

Bug: 27808084
Change-Id: I97cfd83786f8f857dc9551e5b0a358962f98e4a2
This commit is contained in:
Sungsoo
2016-08-18 13:17:58 +09:00
parent 808699f2bb
commit d570d5ca3c
2 changed files with 11 additions and 6 deletions

View File

@@ -441,7 +441,7 @@ public final class MediaBrowser {
return;
}
Parcelable item = resultData.getParcelable(MediaBrowserService.KEY_MEDIA_ITEM);
if (!(item instanceof MediaItem)) {
if (item != null && !(item instanceof MediaItem)) {
cb.onError(mediaId);
return;
}

View File

@@ -91,10 +91,12 @@ public abstract class MediaBrowserService extends Service {
public static final String KEY_MEDIA_ITEM = "media_item";
private static final int RESULT_FLAG_OPTION_NOT_HANDLED = 0x00000001;
private static final int RESULT_FLAG_ON_LOAD_ITEM_NOT_IMPLEMENTED = 0x00000002;
/** @hide */
@Retention(RetentionPolicy.SOURCE)
@IntDef(flag=true, value = { RESULT_FLAG_OPTION_NOT_HANDLED })
@IntDef(flag=true, value = { RESULT_FLAG_OPTION_NOT_HANDLED,
RESULT_FLAG_ON_LOAD_ITEM_NOT_IMPLEMENTED })
private @interface ResultFlags { }
private final ArrayMap<IBinder, ConnectionRecord> mConnections = new ArrayMap<>();
@@ -433,11 +435,9 @@ public abstract class MediaBrowserService extends Service {
* been loaded.
* </p><p>
* When the given {@code itemId} is invalid, implementations must call
* {@link Result#sendResult result.sendResult} with {@code null}, which will
* invoke {@link MediaBrowser.ItemCallback#onError}.
* {@link Result#sendResult result.sendResult} with {@code null}.
* </p><p>
* The default implementation calls {@link Result#sendResult result.sendResult}
* with {@code null}.
* The default implementation will invoke {@link MediaBrowser.ItemCallback#onError}.
* </p>
*
* @param itemId The id for the specific
@@ -445,6 +445,7 @@ public abstract class MediaBrowserService extends Service {
* @param result The Result to send the item to.
*/
public void onLoadItem(String itemId, Result<MediaBrowser.MediaItem> result) {
result.setFlags(RESULT_FLAG_ON_LOAD_ITEM_NOT_IMPLEMENTED);
result.sendResult(null);
}
@@ -703,6 +704,10 @@ public abstract class MediaBrowserService extends Service {
new Result<MediaBrowser.MediaItem>(itemId) {
@Override
void onResultSent(MediaBrowser.MediaItem item, @ResultFlags int flag) {
if ((flag & RESULT_FLAG_ON_LOAD_ITEM_NOT_IMPLEMENTED) != 0) {
receiver.send(-1, null);
return;
}
Bundle bundle = new Bundle();
bundle.putParcelable(KEY_MEDIA_ITEM, item);
receiver.send(0, bundle);