1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.support.v4.media; 18 19 import android.content.ComponentName; 20 import android.content.Context; 21 import android.media.browse.MediaBrowser; 22 import android.os.Bundle; 23 import android.support.annotation.NonNull; 24 import android.support.annotation.RequiresApi; 25 26 import java.util.List; 27 28 @RequiresApi(21) 29 class MediaBrowserCompatApi21 { 30 static final String NULL_MEDIA_ITEM_ID = 31 "android.support.v4.media.MediaBrowserCompat.NULL_MEDIA_ITEM"; 32 createConnectionCallback(ConnectionCallback callback)33 public static Object createConnectionCallback(ConnectionCallback callback) { 34 return new ConnectionCallbackProxy<>(callback); 35 } 36 createBrowser(Context context, ComponentName serviceComponent, Object callback, Bundle rootHints)37 public static Object createBrowser(Context context, ComponentName serviceComponent, 38 Object callback, Bundle rootHints) { 39 return new MediaBrowser(context, serviceComponent, 40 (MediaBrowser.ConnectionCallback) callback, rootHints); 41 } 42 connect(Object browserObj)43 public static void connect(Object browserObj) { 44 ((MediaBrowser)browserObj).connect(); 45 } 46 disconnect(Object browserObj)47 public static void disconnect(Object browserObj) { 48 ((MediaBrowser)browserObj).disconnect(); 49 50 } 51 isConnected(Object browserObj)52 public static boolean isConnected(Object browserObj) { 53 return ((MediaBrowser)browserObj).isConnected(); 54 } 55 getServiceComponent(Object browserObj)56 public static ComponentName getServiceComponent(Object browserObj) { 57 return ((MediaBrowser)browserObj).getServiceComponent(); 58 } 59 getRoot(Object browserObj)60 public static String getRoot(Object browserObj) { 61 return ((MediaBrowser)browserObj).getRoot(); 62 } 63 getExtras(Object browserObj)64 public static Bundle getExtras(Object browserObj) { 65 return ((MediaBrowser)browserObj).getExtras(); 66 } 67 getSessionToken(Object browserObj)68 public static Object getSessionToken(Object browserObj) { 69 return ((MediaBrowser)browserObj).getSessionToken(); 70 } 71 createSubscriptionCallback(SubscriptionCallback callback)72 public static Object createSubscriptionCallback(SubscriptionCallback callback) { 73 return new SubscriptionCallbackProxy<>(callback); 74 } 75 subscribe( Object browserObj, String parentId, Object subscriptionCallbackObj)76 public static void subscribe( 77 Object browserObj, String parentId, Object subscriptionCallbackObj) { 78 ((MediaBrowser)browserObj).subscribe(parentId, 79 (MediaBrowser.SubscriptionCallback) subscriptionCallbackObj); 80 } 81 unsubscribe(Object browserObj, String parentId)82 public static void unsubscribe(Object browserObj, String parentId) { 83 ((MediaBrowser)browserObj).unsubscribe(parentId); 84 } 85 86 interface ConnectionCallback { onConnected()87 void onConnected(); onConnectionSuspended()88 void onConnectionSuspended(); onConnectionFailed()89 void onConnectionFailed(); 90 } 91 92 static class ConnectionCallbackProxy<T extends ConnectionCallback> 93 extends MediaBrowser.ConnectionCallback { 94 protected final T mConnectionCallback; 95 ConnectionCallbackProxy(T connectionCallback)96 public ConnectionCallbackProxy(T connectionCallback) { 97 mConnectionCallback = connectionCallback; 98 } 99 100 @Override onConnected()101 public void onConnected() { 102 mConnectionCallback.onConnected(); 103 } 104 105 @Override onConnectionSuspended()106 public void onConnectionSuspended() { 107 mConnectionCallback.onConnectionSuspended(); 108 } 109 110 @Override onConnectionFailed()111 public void onConnectionFailed() { 112 mConnectionCallback.onConnectionFailed(); 113 } 114 } 115 116 interface SubscriptionCallback { onChildrenLoaded(@onNull String parentId, List<?> children)117 void onChildrenLoaded(@NonNull String parentId, List<?> children); onError(@onNull String parentId)118 void onError(@NonNull String parentId); 119 } 120 121 static class SubscriptionCallbackProxy<T extends SubscriptionCallback> 122 extends MediaBrowser.SubscriptionCallback { 123 protected final T mSubscriptionCallback; 124 SubscriptionCallbackProxy(T callback)125 public SubscriptionCallbackProxy(T callback) { 126 mSubscriptionCallback = callback; 127 } 128 129 @Override onChildrenLoaded(@onNull String parentId, List<MediaBrowser.MediaItem> children)130 public void onChildrenLoaded(@NonNull String parentId, 131 List<MediaBrowser.MediaItem> children) { 132 mSubscriptionCallback.onChildrenLoaded(parentId, children); 133 } 134 135 @Override onError(@onNull String parentId)136 public void onError(@NonNull String parentId) { 137 mSubscriptionCallback.onError(parentId); 138 } 139 } 140 141 static class MediaItem { 142 getFlags(Object itemObj)143 public static int getFlags(Object itemObj) { 144 return ((MediaBrowser.MediaItem) itemObj).getFlags(); 145 } 146 getDescription(Object itemObj)147 public static Object getDescription(Object itemObj) { 148 return ((MediaBrowser.MediaItem) itemObj).getDescription(); 149 } 150 } 151 } 152