package android.support.v4.media; import android.annotation.SuppressLint; import android.content.ComponentName; import android.content.Context; import android.content.ServiceConnection; import android.media.browse.MediaBrowser; import android.os.BadParcelableException; import android.os.Binder; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; import android.os.Message; import android.os.Messenger; import android.os.Parcel; import android.os.Parcelable; import android.os.Process; import android.os.RemoteException; import android.support.v4.media.session.IMediaSession; import android.support.v4.media.session.MediaSessionCompat; import android.support.v4.os.ResultReceiver; import android.text.TextUtils; import android.util.Log; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import androidx.annotation.RestrictTo; import androidx.collection.ArrayMap; import androidx.core.app.BundleCompat; import androidx.media.MediaBrowserCompatUtils; import androidx.media.MediaBrowserProtocol; import androidx.media.MediaBrowserServiceCompat; import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Map; /* loaded from: classes.dex */ public final class MediaBrowserCompat { public static final String CUSTOM_ACTION_DOWNLOAD = "android.support.v4.media.action.DOWNLOAD"; public static final String CUSTOM_ACTION_REMOVE_DOWNLOADED_FILE = "android.support.v4.media.action.REMOVE_DOWNLOADED_FILE"; public static final String EXTRA_DOWNLOAD_PROGRESS = "android.media.browse.extra.DOWNLOAD_PROGRESS"; public static final String EXTRA_MEDIA_ID = "android.media.browse.extra.MEDIA_ID"; public static final String EXTRA_PAGE = "android.media.browse.extra.PAGE"; public static final String EXTRA_PAGE_SIZE = "android.media.browse.extra.PAGE_SIZE"; private final MediaBrowserImpl mImpl; static final String TAG = "MediaBrowserCompat"; static final boolean DEBUG = Log.isLoggable(TAG, 3); public static abstract class CustomActionCallback { public void onError(String str, Bundle bundle, Bundle bundle2) { } public void onProgressUpdate(String str, Bundle bundle, Bundle bundle2) { } public void onResult(String str, Bundle bundle, Bundle bundle2) { } } public interface MediaBrowserImpl { void connect(); void disconnect(); @Nullable Bundle getExtras(); void getItem(@NonNull String str, @NonNull ItemCallback itemCallback); @Nullable Bundle getNotifyChildrenChangedOptions(); @NonNull String getRoot(); ComponentName getServiceComponent(); @NonNull MediaSessionCompat.Token getSessionToken(); boolean isConnected(); void search(@NonNull String str, Bundle bundle, @NonNull SearchCallback searchCallback); void sendCustomAction(@NonNull String str, Bundle bundle, @Nullable CustomActionCallback customActionCallback); void subscribe(@NonNull String str, @Nullable Bundle bundle, @NonNull SubscriptionCallback subscriptionCallback); void unsubscribe(@NonNull String str, SubscriptionCallback subscriptionCallback); } public interface MediaBrowserServiceCallbackImpl { void onConnectionFailed(Messenger messenger); void onLoadChildren(Messenger messenger, String str, List list, Bundle bundle, Bundle bundle2); void onServiceConnected(Messenger messenger, String str, MediaSessionCompat.Token token, Bundle bundle); } public static abstract class SearchCallback { public void onError(@NonNull String str, Bundle bundle) { } public void onSearchResult(@NonNull String str, Bundle bundle, @NonNull List list) { } } public MediaBrowserCompat(Context context, ComponentName componentName, ConnectionCallback connectionCallback, Bundle bundle) { this.mImpl = new MediaBrowserImplApi26(context, componentName, connectionCallback, bundle); } public void connect() { this.mImpl.connect(); } public void disconnect() { this.mImpl.disconnect(); } public boolean isConnected() { return this.mImpl.isConnected(); } @NonNull public ComponentName getServiceComponent() { return this.mImpl.getServiceComponent(); } @NonNull public String getRoot() { return this.mImpl.getRoot(); } @Nullable public Bundle getExtras() { return this.mImpl.getExtras(); } @NonNull public MediaSessionCompat.Token getSessionToken() { return this.mImpl.getSessionToken(); } public void subscribe(@NonNull String str, @NonNull SubscriptionCallback subscriptionCallback) { if (TextUtils.isEmpty(str)) { throw new IllegalArgumentException("parentId is empty"); } if (subscriptionCallback == null) { throw new IllegalArgumentException("callback is null"); } this.mImpl.subscribe(str, null, subscriptionCallback); } public void subscribe(@NonNull String str, @NonNull Bundle bundle, @NonNull SubscriptionCallback subscriptionCallback) { if (TextUtils.isEmpty(str)) { throw new IllegalArgumentException("parentId is empty"); } if (subscriptionCallback == null) { throw new IllegalArgumentException("callback is null"); } if (bundle == null) { throw new IllegalArgumentException("options are null"); } this.mImpl.subscribe(str, bundle, subscriptionCallback); } public void unsubscribe(@NonNull String str) { if (TextUtils.isEmpty(str)) { throw new IllegalArgumentException("parentId is empty"); } this.mImpl.unsubscribe(str, null); } public void unsubscribe(@NonNull String str, @NonNull SubscriptionCallback subscriptionCallback) { if (TextUtils.isEmpty(str)) { throw new IllegalArgumentException("parentId is empty"); } if (subscriptionCallback == null) { throw new IllegalArgumentException("callback is null"); } this.mImpl.unsubscribe(str, subscriptionCallback); } public void getItem(@NonNull String str, @NonNull ItemCallback itemCallback) { this.mImpl.getItem(str, itemCallback); } public void search(@NonNull String str, Bundle bundle, @NonNull SearchCallback searchCallback) { if (TextUtils.isEmpty(str)) { throw new IllegalArgumentException("query cannot be empty"); } if (searchCallback == null) { throw new IllegalArgumentException("callback cannot be null"); } this.mImpl.search(str, bundle, searchCallback); } public void sendCustomAction(@NonNull String str, Bundle bundle, @Nullable CustomActionCallback customActionCallback) { if (TextUtils.isEmpty(str)) { throw new IllegalArgumentException("action cannot be empty"); } this.mImpl.sendCustomAction(str, bundle, customActionCallback); } @Nullable @RestrictTo({RestrictTo.Scope.LIBRARY_GROUP_PREFIX}) public Bundle getNotifyChildrenChangedOptions() { return this.mImpl.getNotifyChildrenChangedOptions(); } @SuppressLint({"BanParcelableUsage"}) public static class MediaItem implements Parcelable { public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { // from class: android.support.v4.media.MediaBrowserCompat.MediaItem.1 /* JADX WARN: Can't rename method to resolve collision */ @Override // android.os.Parcelable.Creator public MediaItem createFromParcel(Parcel parcel) { return new MediaItem(parcel); } /* JADX WARN: Can't rename method to resolve collision */ @Override // android.os.Parcelable.Creator public MediaItem[] newArray(int i) { return new MediaItem[i]; } }; public static final int FLAG_BROWSABLE = 1; public static final int FLAG_PLAYABLE = 2; private final MediaDescriptionCompat mDescription; private final int mFlags; @Override // android.os.Parcelable public int describeContents() { return 0; } @NonNull public MediaDescriptionCompat getDescription() { return this.mDescription; } public int getFlags() { return this.mFlags; } public boolean isBrowsable() { return (this.mFlags & 1) != 0; } public boolean isPlayable() { return (this.mFlags & 2) != 0; } public static MediaItem fromMediaItem(Object obj) { if (obj == null) { return null; } MediaBrowser.MediaItem mediaItem = (MediaBrowser.MediaItem) obj; return new MediaItem(MediaDescriptionCompat.fromMediaDescription(mediaItem.getDescription()), mediaItem.getFlags()); } public static List fromMediaItemList(List list) { if (list == null) { return null; } ArrayList arrayList = new ArrayList(list.size()); Iterator it = list.iterator(); while (it.hasNext()) { arrayList.add(fromMediaItem(it.next())); } return arrayList; } public MediaItem(@NonNull MediaDescriptionCompat mediaDescriptionCompat, int i) { if (mediaDescriptionCompat == null) { throw new IllegalArgumentException("description cannot be null"); } if (TextUtils.isEmpty(mediaDescriptionCompat.getMediaId())) { throw new IllegalArgumentException("description must have a non-empty media id"); } this.mFlags = i; this.mDescription = mediaDescriptionCompat; } public MediaItem(Parcel parcel) { this.mFlags = parcel.readInt(); this.mDescription = MediaDescriptionCompat.CREATOR.createFromParcel(parcel); } @Override // android.os.Parcelable public void writeToParcel(Parcel parcel, int i) { parcel.writeInt(this.mFlags); this.mDescription.writeToParcel(parcel, i); } @NonNull public String toString() { return "MediaItem{mFlags=" + this.mFlags + ", mDescription=" + this.mDescription + '}'; } @Nullable public String getMediaId() { return this.mDescription.getMediaId(); } } public static class ConnectionCallback { final MediaBrowser.ConnectionCallback mConnectionCallbackFwk = new ConnectionCallbackApi21(); ConnectionCallbackInternal mConnectionCallbackInternal; public interface ConnectionCallbackInternal { void onConnected(); void onConnectionFailed(); void onConnectionSuspended(); } public void onConnected() { } public void onConnectionFailed() { } public void onConnectionSuspended() { } public void setInternalConnectionCallback(ConnectionCallbackInternal connectionCallbackInternal) { this.mConnectionCallbackInternal = connectionCallbackInternal; } @RequiresApi(21) public class ConnectionCallbackApi21 extends MediaBrowser.ConnectionCallback { public ConnectionCallbackApi21() { } @Override // android.media.browse.MediaBrowser.ConnectionCallback public void onConnected() { ConnectionCallbackInternal connectionCallbackInternal = ConnectionCallback.this.mConnectionCallbackInternal; if (connectionCallbackInternal != null) { connectionCallbackInternal.onConnected(); } ConnectionCallback.this.onConnected(); } @Override // android.media.browse.MediaBrowser.ConnectionCallback public void onConnectionSuspended() { ConnectionCallbackInternal connectionCallbackInternal = ConnectionCallback.this.mConnectionCallbackInternal; if (connectionCallbackInternal != null) { connectionCallbackInternal.onConnectionSuspended(); } ConnectionCallback.this.onConnectionSuspended(); } @Override // android.media.browse.MediaBrowser.ConnectionCallback public void onConnectionFailed() { ConnectionCallbackInternal connectionCallbackInternal = ConnectionCallback.this.mConnectionCallbackInternal; if (connectionCallbackInternal != null) { connectionCallbackInternal.onConnectionFailed(); } ConnectionCallback.this.onConnectionFailed(); } } } public static abstract class SubscriptionCallback { WeakReference mSubscriptionRef; final IBinder mToken = new Binder(); final MediaBrowser.SubscriptionCallback mSubscriptionCallbackFwk = new SubscriptionCallbackApi26(); public void onChildrenLoaded(@NonNull String str, @NonNull List list) { } public void onChildrenLoaded(@NonNull String str, @NonNull List list, @NonNull Bundle bundle) { } public void onError(@NonNull String str) { } public void onError(@NonNull String str, @NonNull Bundle bundle) { } public void setSubscription(Subscription subscription) { this.mSubscriptionRef = new WeakReference<>(subscription); } @RequiresApi(21) public class SubscriptionCallbackApi21 extends MediaBrowser.SubscriptionCallback { public SubscriptionCallbackApi21() { } @Override // android.media.browse.MediaBrowser.SubscriptionCallback public void onChildrenLoaded(@NonNull String str, List list) { WeakReference weakReference = SubscriptionCallback.this.mSubscriptionRef; Subscription subscription = weakReference == null ? null : weakReference.get(); if (subscription == null) { SubscriptionCallback.this.onChildrenLoaded(str, MediaItem.fromMediaItemList(list)); return; } List fromMediaItemList = MediaItem.fromMediaItemList(list); List callbacks = subscription.getCallbacks(); List optionsList = subscription.getOptionsList(); for (int i = 0; i < callbacks.size(); i++) { Bundle bundle = optionsList.get(i); if (bundle == null) { SubscriptionCallback.this.onChildrenLoaded(str, fromMediaItemList); } else { SubscriptionCallback.this.onChildrenLoaded(str, applyOptions(fromMediaItemList, bundle), bundle); } } } @Override // android.media.browse.MediaBrowser.SubscriptionCallback public void onError(@NonNull String str) { SubscriptionCallback.this.onError(str); } public List applyOptions(List list, Bundle bundle) { if (list == null) { return null; } int i = bundle.getInt(MediaBrowserCompat.EXTRA_PAGE, -1); int i2 = bundle.getInt(MediaBrowserCompat.EXTRA_PAGE_SIZE, -1); if (i == -1 && i2 == -1) { return list; } int i3 = i2 * i; int i4 = i3 + i2; if (i < 0 || i2 < 1 || i3 >= list.size()) { return Collections.emptyList(); } if (i4 > list.size()) { i4 = list.size(); } return list.subList(i3, i4); } } @RequiresApi(26) public class SubscriptionCallbackApi26 extends SubscriptionCallbackApi21 { public SubscriptionCallbackApi26() { super(); } @Override // android.media.browse.MediaBrowser.SubscriptionCallback public void onChildrenLoaded(@NonNull String str, @NonNull List list, @NonNull Bundle bundle) { MediaSessionCompat.ensureClassLoader(bundle); SubscriptionCallback.this.onChildrenLoaded(str, MediaItem.fromMediaItemList(list), bundle); } @Override // android.media.browse.MediaBrowser.SubscriptionCallback public void onError(@NonNull String str, @NonNull Bundle bundle) { MediaSessionCompat.ensureClassLoader(bundle); SubscriptionCallback.this.onError(str, bundle); } } } public static abstract class ItemCallback { final MediaBrowser.ItemCallback mItemCallbackFwk = new ItemCallbackApi23(); public void onError(@NonNull String str) { } public void onItemLoaded(MediaItem mediaItem) { } @RequiresApi(23) public class ItemCallbackApi23 extends MediaBrowser.ItemCallback { public ItemCallbackApi23() { } @Override // android.media.browse.MediaBrowser.ItemCallback public void onItemLoaded(MediaBrowser.MediaItem mediaItem) { ItemCallback.this.onItemLoaded(MediaItem.fromMediaItem(mediaItem)); } @Override // android.media.browse.MediaBrowser.ItemCallback public void onError(@NonNull String str) { ItemCallback.this.onError(str); } } } public static class MediaBrowserImplBase implements MediaBrowserImpl, MediaBrowserServiceCallbackImpl { static final int CONNECT_STATE_CONNECTED = 3; static final int CONNECT_STATE_CONNECTING = 2; static final int CONNECT_STATE_DISCONNECTED = 1; static final int CONNECT_STATE_DISCONNECTING = 0; static final int CONNECT_STATE_SUSPENDED = 4; final ConnectionCallback mCallback; Messenger mCallbacksMessenger; final Context mContext; private Bundle mExtras; private MediaSessionCompat.Token mMediaSessionToken; private Bundle mNotifyChildrenChangedOptions; final Bundle mRootHints; private String mRootId; ServiceBinderWrapper mServiceBinderWrapper; final ComponentName mServiceComponent; MediaServiceConnection mServiceConnection; final CallbackHandler mHandler = new CallbackHandler(this); private final ArrayMap mSubscriptions = new ArrayMap<>(); int mState = 1; @Override // android.support.v4.media.MediaBrowserCompat.MediaBrowserImpl public Bundle getNotifyChildrenChangedOptions() { return this.mNotifyChildrenChangedOptions; } @Override // android.support.v4.media.MediaBrowserCompat.MediaBrowserImpl public boolean isConnected() { return this.mState == 3; } public MediaBrowserImplBase(Context context, ComponentName componentName, ConnectionCallback connectionCallback, Bundle bundle) { if (context == null) { throw new IllegalArgumentException("context must not be null"); } if (componentName == null) { throw new IllegalArgumentException("service component must not be null"); } if (connectionCallback == null) { throw new IllegalArgumentException("connection callback must not be null"); } this.mContext = context; this.mServiceComponent = componentName; this.mCallback = connectionCallback; this.mRootHints = bundle == null ? null : new Bundle(bundle); } @Override // android.support.v4.media.MediaBrowserCompat.MediaBrowserImpl public void connect() { int i = this.mState; if (i != 0 && i != 1) { throw new IllegalStateException("connect() called while neigther disconnecting nor disconnected (state=" + getStateLabel(this.mState) + ")"); } this.mState = 2; this.mHandler.post(new Runnable() { // from class: android.support.v4.media.MediaBrowserCompat.MediaBrowserImplBase.1 /* JADX WARN: Code restructure failed: missing block: B:18:0x0058, code lost: if (r1.mContext.bindService(r0, r1.mServiceConnection, 1) == false) goto L21; */ @Override // java.lang.Runnable /* Code decompiled incorrectly, please refer to instructions dump. To view partially-correct add '--show-bad-code' argument */ public void run() { /* r4 = this; android.support.v4.media.MediaBrowserCompat$MediaBrowserImplBase r0 = android.support.v4.media.MediaBrowserCompat.MediaBrowserImplBase.this int r1 = r0.mState if (r1 != 0) goto L7 return L7: r1 = 2 r0.mState = r1 boolean r1 = android.support.v4.media.MediaBrowserCompat.DEBUG if (r1 == 0) goto L2e android.support.v4.media.MediaBrowserCompat$MediaBrowserImplBase$MediaServiceConnection r1 = r0.mServiceConnection if (r1 != 0) goto L13 goto L2e L13: java.lang.RuntimeException r0 = new java.lang.RuntimeException java.lang.StringBuilder r1 = new java.lang.StringBuilder r1.() java.lang.String r2 = "mServiceConnection should be null. Instead it is " r1.append(r2) android.support.v4.media.MediaBrowserCompat$MediaBrowserImplBase r2 = android.support.v4.media.MediaBrowserCompat.MediaBrowserImplBase.this android.support.v4.media.MediaBrowserCompat$MediaBrowserImplBase$MediaServiceConnection r2 = r2.mServiceConnection r1.append(r2) java.lang.String r1 = r1.toString() r0.(r1) throw r0 L2e: android.support.v4.media.MediaBrowserCompat$ServiceBinderWrapper r1 = r0.mServiceBinderWrapper if (r1 != 0) goto La6 android.os.Messenger r0 = r0.mCallbacksMessenger if (r0 != 0) goto L8b android.content.Intent r0 = new android.content.Intent java.lang.String r1 = "android.media.browse.MediaBrowserService" r0.(r1) android.support.v4.media.MediaBrowserCompat$MediaBrowserImplBase r1 = android.support.v4.media.MediaBrowserCompat.MediaBrowserImplBase.this android.content.ComponentName r1 = r1.mServiceComponent r0.setComponent(r1) android.support.v4.media.MediaBrowserCompat$MediaBrowserImplBase r1 = android.support.v4.media.MediaBrowserCompat.MediaBrowserImplBase.this android.support.v4.media.MediaBrowserCompat$MediaBrowserImplBase$MediaServiceConnection r2 = new android.support.v4.media.MediaBrowserCompat$MediaBrowserImplBase$MediaServiceConnection r2.() r1.mServiceConnection = r2 android.support.v4.media.MediaBrowserCompat$MediaBrowserImplBase r1 = android.support.v4.media.MediaBrowserCompat.MediaBrowserImplBase.this // Catch: java.lang.Exception -> L5b android.content.Context r2 = r1.mContext // Catch: java.lang.Exception -> L5b android.support.v4.media.MediaBrowserCompat$MediaBrowserImplBase$MediaServiceConnection r1 = r1.mServiceConnection // Catch: java.lang.Exception -> L5b r3 = 1 boolean r0 = r2.bindService(r0, r1, r3) // Catch: java.lang.Exception -> L5b if (r0 != 0) goto L81 goto L75 L5b: java.lang.StringBuilder r0 = new java.lang.StringBuilder r0.() java.lang.String r1 = "Failed binding to service " r0.append(r1) android.support.v4.media.MediaBrowserCompat$MediaBrowserImplBase r1 = android.support.v4.media.MediaBrowserCompat.MediaBrowserImplBase.this android.content.ComponentName r1 = r1.mServiceComponent r0.append(r1) java.lang.String r0 = r0.toString() java.lang.String r1 = "MediaBrowserCompat" android.util.Log.e(r1, r0) L75: android.support.v4.media.MediaBrowserCompat$MediaBrowserImplBase r0 = android.support.v4.media.MediaBrowserCompat.MediaBrowserImplBase.this r0.forceCloseConnection() android.support.v4.media.MediaBrowserCompat$MediaBrowserImplBase r0 = android.support.v4.media.MediaBrowserCompat.MediaBrowserImplBase.this android.support.v4.media.MediaBrowserCompat$ConnectionCallback r0 = r0.mCallback r0.onConnectionFailed() L81: boolean r0 = android.support.v4.media.MediaBrowserCompat.DEBUG if (r0 == 0) goto L8a android.support.v4.media.MediaBrowserCompat$MediaBrowserImplBase r0 = android.support.v4.media.MediaBrowserCompat.MediaBrowserImplBase.this r0.dump() L8a: return L8b: java.lang.RuntimeException r0 = new java.lang.RuntimeException java.lang.StringBuilder r1 = new java.lang.StringBuilder r1.() java.lang.String r2 = "mCallbacksMessenger should be null. Instead it is " r1.append(r2) android.support.v4.media.MediaBrowserCompat$MediaBrowserImplBase r2 = android.support.v4.media.MediaBrowserCompat.MediaBrowserImplBase.this android.os.Messenger r2 = r2.mCallbacksMessenger r1.append(r2) java.lang.String r1 = r1.toString() r0.(r1) throw r0 La6: java.lang.RuntimeException r0 = new java.lang.RuntimeException java.lang.StringBuilder r1 = new java.lang.StringBuilder r1.() java.lang.String r2 = "mServiceBinderWrapper should be null. Instead it is " r1.append(r2) android.support.v4.media.MediaBrowserCompat$MediaBrowserImplBase r2 = android.support.v4.media.MediaBrowserCompat.MediaBrowserImplBase.this android.support.v4.media.MediaBrowserCompat$ServiceBinderWrapper r2 = r2.mServiceBinderWrapper r1.append(r2) java.lang.String r1 = r1.toString() r0.(r1) throw r0 */ throw new UnsupportedOperationException("Method not decompiled: android.support.v4.media.MediaBrowserCompat.MediaBrowserImplBase.AnonymousClass1.run():void"); } }); } @Override // android.support.v4.media.MediaBrowserCompat.MediaBrowserImpl public void disconnect() { this.mState = 0; this.mHandler.post(new Runnable() { // from class: android.support.v4.media.MediaBrowserCompat.MediaBrowserImplBase.2 @Override // java.lang.Runnable public void run() { MediaBrowserImplBase mediaBrowserImplBase = MediaBrowserImplBase.this; Messenger messenger = mediaBrowserImplBase.mCallbacksMessenger; if (messenger != null) { try { mediaBrowserImplBase.mServiceBinderWrapper.disconnect(messenger); } catch (RemoteException unused) { Log.w(MediaBrowserCompat.TAG, "RemoteException during connect for " + MediaBrowserImplBase.this.mServiceComponent); } } MediaBrowserImplBase mediaBrowserImplBase2 = MediaBrowserImplBase.this; int i = mediaBrowserImplBase2.mState; mediaBrowserImplBase2.forceCloseConnection(); if (i != 0) { MediaBrowserImplBase.this.mState = i; } if (MediaBrowserCompat.DEBUG) { MediaBrowserImplBase.this.dump(); } } }); } public void forceCloseConnection() { MediaServiceConnection mediaServiceConnection = this.mServiceConnection; if (mediaServiceConnection != null) { this.mContext.unbindService(mediaServiceConnection); } this.mState = 1; this.mServiceConnection = null; this.mServiceBinderWrapper = null; this.mCallbacksMessenger = null; this.mHandler.setCallbacksMessenger(null); this.mRootId = null; this.mMediaSessionToken = null; } @Override // android.support.v4.media.MediaBrowserCompat.MediaBrowserImpl @NonNull public ComponentName getServiceComponent() { if (isConnected()) { return this.mServiceComponent; } throw new IllegalStateException("getServiceComponent() called while not connected (state=" + this.mState + ")"); } @Override // android.support.v4.media.MediaBrowserCompat.MediaBrowserImpl @NonNull public String getRoot() { if (isConnected()) { return this.mRootId; } throw new IllegalStateException("getRoot() called while not connected(state=" + getStateLabel(this.mState) + ")"); } @Override // android.support.v4.media.MediaBrowserCompat.MediaBrowserImpl @Nullable public Bundle getExtras() { if (isConnected()) { return this.mExtras; } throw new IllegalStateException("getExtras() called while not connected (state=" + getStateLabel(this.mState) + ")"); } @Override // android.support.v4.media.MediaBrowserCompat.MediaBrowserImpl @NonNull public MediaSessionCompat.Token getSessionToken() { if (isConnected()) { return this.mMediaSessionToken; } throw new IllegalStateException("getSessionToken() called while not connected(state=" + this.mState + ")"); } @Override // android.support.v4.media.MediaBrowserCompat.MediaBrowserImpl public void subscribe(@NonNull String str, Bundle bundle, @NonNull SubscriptionCallback subscriptionCallback) { Subscription subscription = this.mSubscriptions.get(str); if (subscription == null) { subscription = new Subscription(); this.mSubscriptions.put(str, subscription); } Bundle bundle2 = bundle == null ? null : new Bundle(bundle); subscription.putCallback(bundle2, subscriptionCallback); if (isConnected()) { try { this.mServiceBinderWrapper.addSubscription(str, subscriptionCallback.mToken, bundle2, this.mCallbacksMessenger); } catch (RemoteException unused) { StringBuilder sb = new StringBuilder(); sb.append("addSubscription failed with RemoteException parentId="); sb.append(str); } } } @Override // android.support.v4.media.MediaBrowserCompat.MediaBrowserImpl public void unsubscribe(@NonNull String str, SubscriptionCallback subscriptionCallback) { Subscription subscription = this.mSubscriptions.get(str); if (subscription == null) { return; } try { if (subscriptionCallback == null) { if (isConnected()) { this.mServiceBinderWrapper.removeSubscription(str, null, this.mCallbacksMessenger); } } else { List callbacks = subscription.getCallbacks(); List optionsList = subscription.getOptionsList(); for (int size = callbacks.size() - 1; size >= 0; size--) { if (callbacks.get(size) == subscriptionCallback) { if (isConnected()) { this.mServiceBinderWrapper.removeSubscription(str, subscriptionCallback.mToken, this.mCallbacksMessenger); } callbacks.remove(size); optionsList.remove(size); } } } } catch (RemoteException unused) { StringBuilder sb = new StringBuilder(); sb.append("removeSubscription failed with RemoteException parentId="); sb.append(str); } if (subscription.isEmpty() || subscriptionCallback == null) { this.mSubscriptions.remove(str); } } @Override // android.support.v4.media.MediaBrowserCompat.MediaBrowserImpl public void getItem(@NonNull final String str, @NonNull final ItemCallback itemCallback) { if (TextUtils.isEmpty(str)) { throw new IllegalArgumentException("mediaId is empty"); } if (itemCallback == null) { throw new IllegalArgumentException("cb is null"); } if (!isConnected()) { this.mHandler.post(new Runnable() { // from class: android.support.v4.media.MediaBrowserCompat.MediaBrowserImplBase.3 @Override // java.lang.Runnable public void run() { itemCallback.onError(str); } }); return; } try { this.mServiceBinderWrapper.getMediaItem(str, new ItemReceiver(str, itemCallback, this.mHandler), this.mCallbacksMessenger); } catch (RemoteException unused) { StringBuilder sb = new StringBuilder(); sb.append("Remote error getting media item: "); sb.append(str); this.mHandler.post(new Runnable() { // from class: android.support.v4.media.MediaBrowserCompat.MediaBrowserImplBase.4 @Override // java.lang.Runnable public void run() { itemCallback.onError(str); } }); } } @Override // android.support.v4.media.MediaBrowserCompat.MediaBrowserImpl public void search(@NonNull final String str, final Bundle bundle, @NonNull final SearchCallback searchCallback) { if (!isConnected()) { throw new IllegalStateException("search() called while not connected (state=" + getStateLabel(this.mState) + ")"); } try { this.mServiceBinderWrapper.search(str, bundle, new SearchResultReceiver(str, bundle, searchCallback, this.mHandler), this.mCallbacksMessenger); } catch (RemoteException unused) { StringBuilder sb = new StringBuilder(); sb.append("Remote error searching items with query: "); sb.append(str); this.mHandler.post(new Runnable() { // from class: android.support.v4.media.MediaBrowserCompat.MediaBrowserImplBase.5 @Override // java.lang.Runnable public void run() { searchCallback.onError(str, bundle); } }); } } @Override // android.support.v4.media.MediaBrowserCompat.MediaBrowserImpl public void sendCustomAction(@NonNull final String str, final Bundle bundle, @Nullable final CustomActionCallback customActionCallback) { if (!isConnected()) { throw new IllegalStateException("Cannot send a custom action (" + str + ") with extras " + bundle + " because the browser is not connected to the service."); } try { this.mServiceBinderWrapper.sendCustomAction(str, bundle, new CustomActionResultReceiver(str, bundle, customActionCallback, this.mHandler), this.mCallbacksMessenger); } catch (RemoteException unused) { StringBuilder sb = new StringBuilder(); sb.append("Remote error sending a custom action: action="); sb.append(str); sb.append(", extras="); sb.append(bundle); if (customActionCallback != null) { this.mHandler.post(new Runnable() { // from class: android.support.v4.media.MediaBrowserCompat.MediaBrowserImplBase.6 @Override // java.lang.Runnable public void run() { customActionCallback.onError(str, bundle, null); } }); } } } @Override // android.support.v4.media.MediaBrowserCompat.MediaBrowserServiceCallbackImpl public void onServiceConnected(Messenger messenger, String str, MediaSessionCompat.Token token, Bundle bundle) { if (isCurrent(messenger, "onConnect")) { if (this.mState != 2) { Log.w(MediaBrowserCompat.TAG, "onConnect from service while mState=" + getStateLabel(this.mState) + "... ignoring"); return; } this.mRootId = str; this.mMediaSessionToken = token; this.mExtras = bundle; this.mState = 3; if (MediaBrowserCompat.DEBUG) { dump(); } this.mCallback.onConnected(); try { for (Map.Entry entry : this.mSubscriptions.entrySet()) { String key = entry.getKey(); Subscription value = entry.getValue(); List callbacks = value.getCallbacks(); List optionsList = value.getOptionsList(); for (int i = 0; i < callbacks.size(); i++) { this.mServiceBinderWrapper.addSubscription(key, callbacks.get(i).mToken, optionsList.get(i), this.mCallbacksMessenger); } } } catch (RemoteException unused) { } } } @Override // android.support.v4.media.MediaBrowserCompat.MediaBrowserServiceCallbackImpl public void onConnectionFailed(Messenger messenger) { Log.e(MediaBrowserCompat.TAG, "onConnectFailed for " + this.mServiceComponent); if (isCurrent(messenger, "onConnectFailed")) { if (this.mState != 2) { Log.w(MediaBrowserCompat.TAG, "onConnect from service while mState=" + getStateLabel(this.mState) + "... ignoring"); return; } forceCloseConnection(); this.mCallback.onConnectionFailed(); } } @Override // android.support.v4.media.MediaBrowserCompat.MediaBrowserServiceCallbackImpl public void onLoadChildren(Messenger messenger, String str, List list, Bundle bundle, Bundle bundle2) { if (isCurrent(messenger, "onLoadChildren")) { boolean z = MediaBrowserCompat.DEBUG; if (z) { StringBuilder sb = new StringBuilder(); sb.append("onLoadChildren for "); sb.append(this.mServiceComponent); sb.append(" id="); sb.append(str); } Subscription subscription = this.mSubscriptions.get(str); if (subscription == null) { if (z) { StringBuilder sb2 = new StringBuilder(); sb2.append("onLoadChildren for id that isn't subscribed id="); sb2.append(str); return; } return; } SubscriptionCallback callback = subscription.getCallback(bundle); if (callback != null) { if (bundle == null) { if (list == null) { callback.onError(str); return; } this.mNotifyChildrenChangedOptions = bundle2; callback.onChildrenLoaded(str, list); this.mNotifyChildrenChangedOptions = null; return; } if (list == null) { callback.onError(str, bundle); return; } this.mNotifyChildrenChangedOptions = bundle2; callback.onChildrenLoaded(str, list, bundle); this.mNotifyChildrenChangedOptions = null; } } } private static String getStateLabel(int i) { if (i == 0) { return "CONNECT_STATE_DISCONNECTING"; } if (i == 1) { return "CONNECT_STATE_DISCONNECTED"; } if (i == 2) { return "CONNECT_STATE_CONNECTING"; } if (i == 3) { return "CONNECT_STATE_CONNECTED"; } if (i == 4) { return "CONNECT_STATE_SUSPENDED"; } return "UNKNOWN/" + i; } private boolean isCurrent(Messenger messenger, String str) { int i; if (this.mCallbacksMessenger == messenger && (i = this.mState) != 0 && i != 1) { return true; } int i2 = this.mState; if (i2 == 0 || i2 == 1) { return false; } StringBuilder sb = new StringBuilder(); sb.append(str); sb.append(" for "); sb.append(this.mServiceComponent); sb.append(" with mCallbacksMessenger="); sb.append(this.mCallbacksMessenger); sb.append(" this="); sb.append(this); return false; } public void dump() { StringBuilder sb = new StringBuilder(); sb.append(" mServiceComponent="); sb.append(this.mServiceComponent); StringBuilder sb2 = new StringBuilder(); sb2.append(" mCallback="); sb2.append(this.mCallback); StringBuilder sb3 = new StringBuilder(); sb3.append(" mRootHints="); sb3.append(this.mRootHints); StringBuilder sb4 = new StringBuilder(); sb4.append(" mState="); sb4.append(getStateLabel(this.mState)); StringBuilder sb5 = new StringBuilder(); sb5.append(" mServiceConnection="); sb5.append(this.mServiceConnection); StringBuilder sb6 = new StringBuilder(); sb6.append(" mServiceBinderWrapper="); sb6.append(this.mServiceBinderWrapper); StringBuilder sb7 = new StringBuilder(); sb7.append(" mCallbacksMessenger="); sb7.append(this.mCallbacksMessenger); StringBuilder sb8 = new StringBuilder(); sb8.append(" mRootId="); sb8.append(this.mRootId); StringBuilder sb9 = new StringBuilder(); sb9.append(" mMediaSessionToken="); sb9.append(this.mMediaSessionToken); } public class MediaServiceConnection implements ServiceConnection { public MediaServiceConnection() { } @Override // android.content.ServiceConnection public void onServiceConnected(final ComponentName componentName, final IBinder iBinder) { postOrRun(new Runnable() { // from class: android.support.v4.media.MediaBrowserCompat.MediaBrowserImplBase.MediaServiceConnection.1 @Override // java.lang.Runnable public void run() { boolean z = MediaBrowserCompat.DEBUG; if (z) { StringBuilder sb = new StringBuilder(); sb.append("MediaServiceConnection.onServiceConnected name="); sb.append(componentName); sb.append(" binder="); sb.append(iBinder); MediaBrowserImplBase.this.dump(); } if (MediaServiceConnection.this.isCurrent("onServiceConnected")) { MediaBrowserImplBase mediaBrowserImplBase = MediaBrowserImplBase.this; mediaBrowserImplBase.mServiceBinderWrapper = new ServiceBinderWrapper(iBinder, mediaBrowserImplBase.mRootHints); MediaBrowserImplBase.this.mCallbacksMessenger = new Messenger(MediaBrowserImplBase.this.mHandler); MediaBrowserImplBase mediaBrowserImplBase2 = MediaBrowserImplBase.this; mediaBrowserImplBase2.mHandler.setCallbacksMessenger(mediaBrowserImplBase2.mCallbacksMessenger); MediaBrowserImplBase.this.mState = 2; if (z) { try { MediaBrowserImplBase.this.dump(); } catch (RemoteException unused) { Log.w(MediaBrowserCompat.TAG, "RemoteException during connect for " + MediaBrowserImplBase.this.mServiceComponent); if (MediaBrowserCompat.DEBUG) { MediaBrowserImplBase.this.dump(); return; } return; } } MediaBrowserImplBase mediaBrowserImplBase3 = MediaBrowserImplBase.this; mediaBrowserImplBase3.mServiceBinderWrapper.connect(mediaBrowserImplBase3.mContext, mediaBrowserImplBase3.mCallbacksMessenger); } } }); } @Override // android.content.ServiceConnection public void onServiceDisconnected(final ComponentName componentName) { postOrRun(new Runnable() { // from class: android.support.v4.media.MediaBrowserCompat.MediaBrowserImplBase.MediaServiceConnection.2 @Override // java.lang.Runnable public void run() { if (MediaBrowserCompat.DEBUG) { StringBuilder sb = new StringBuilder(); sb.append("MediaServiceConnection.onServiceDisconnected name="); sb.append(componentName); sb.append(" this="); sb.append(this); sb.append(" mServiceConnection="); sb.append(MediaBrowserImplBase.this.mServiceConnection); MediaBrowserImplBase.this.dump(); } if (MediaServiceConnection.this.isCurrent("onServiceDisconnected")) { MediaBrowserImplBase mediaBrowserImplBase = MediaBrowserImplBase.this; mediaBrowserImplBase.mServiceBinderWrapper = null; mediaBrowserImplBase.mCallbacksMessenger = null; mediaBrowserImplBase.mHandler.setCallbacksMessenger(null); MediaBrowserImplBase mediaBrowserImplBase2 = MediaBrowserImplBase.this; mediaBrowserImplBase2.mState = 4; mediaBrowserImplBase2.mCallback.onConnectionSuspended(); } } }); } private void postOrRun(Runnable runnable) { if (Thread.currentThread() == MediaBrowserImplBase.this.mHandler.getLooper().getThread()) { runnable.run(); } else { MediaBrowserImplBase.this.mHandler.post(runnable); } } public boolean isCurrent(String str) { int i; MediaBrowserImplBase mediaBrowserImplBase = MediaBrowserImplBase.this; if (mediaBrowserImplBase.mServiceConnection == this && (i = mediaBrowserImplBase.mState) != 0 && i != 1) { return true; } int i2 = mediaBrowserImplBase.mState; if (i2 == 0 || i2 == 1) { return false; } StringBuilder sb = new StringBuilder(); sb.append(str); sb.append(" for "); sb.append(MediaBrowserImplBase.this.mServiceComponent); sb.append(" with mServiceConnection="); sb.append(MediaBrowserImplBase.this.mServiceConnection); sb.append(" this="); sb.append(this); return false; } } } @RequiresApi(21) public static class MediaBrowserImplApi21 implements MediaBrowserImpl, MediaBrowserServiceCallbackImpl, ConnectionCallback.ConnectionCallbackInternal { protected final MediaBrowser mBrowserFwk; protected Messenger mCallbacksMessenger; final Context mContext; private MediaSessionCompat.Token mMediaSessionToken; private Bundle mNotifyChildrenChangedOptions; protected final Bundle mRootHints; protected ServiceBinderWrapper mServiceBinderWrapper; protected int mServiceVersion; protected final CallbackHandler mHandler = new CallbackHandler(this); private final ArrayMap mSubscriptions = new ArrayMap<>(); @Override // android.support.v4.media.MediaBrowserCompat.MediaBrowserImpl public Bundle getNotifyChildrenChangedOptions() { return this.mNotifyChildrenChangedOptions; } @Override // android.support.v4.media.MediaBrowserCompat.ConnectionCallback.ConnectionCallbackInternal public void onConnectionFailed() { } @Override // android.support.v4.media.MediaBrowserCompat.MediaBrowserServiceCallbackImpl public void onConnectionFailed(Messenger messenger) { } @Override // android.support.v4.media.MediaBrowserCompat.MediaBrowserServiceCallbackImpl public void onServiceConnected(Messenger messenger, String str, MediaSessionCompat.Token token, Bundle bundle) { } public MediaBrowserImplApi21(Context context, ComponentName componentName, ConnectionCallback connectionCallback, Bundle bundle) { this.mContext = context; Bundle bundle2 = bundle != null ? new Bundle(bundle) : new Bundle(); this.mRootHints = bundle2; bundle2.putInt(MediaBrowserProtocol.EXTRA_CLIENT_VERSION, 1); bundle2.putInt(MediaBrowserProtocol.EXTRA_CALLING_PID, Process.myPid()); connectionCallback.setInternalConnectionCallback(this); this.mBrowserFwk = new MediaBrowser(context, componentName, connectionCallback.mConnectionCallbackFwk, bundle2); } @Override // android.support.v4.media.MediaBrowserCompat.MediaBrowserImpl public void connect() { this.mBrowserFwk.connect(); } @Override // android.support.v4.media.MediaBrowserCompat.MediaBrowserImpl public void disconnect() { Messenger messenger; ServiceBinderWrapper serviceBinderWrapper = this.mServiceBinderWrapper; if (serviceBinderWrapper != null && (messenger = this.mCallbacksMessenger) != null) { try { serviceBinderWrapper.unregisterCallbackMessenger(messenger); } catch (RemoteException unused) { } } this.mBrowserFwk.disconnect(); } @Override // android.support.v4.media.MediaBrowserCompat.MediaBrowserImpl public boolean isConnected() { return this.mBrowserFwk.isConnected(); } @Override // android.support.v4.media.MediaBrowserCompat.MediaBrowserImpl public ComponentName getServiceComponent() { return this.mBrowserFwk.getServiceComponent(); } @Override // android.support.v4.media.MediaBrowserCompat.MediaBrowserImpl @NonNull public String getRoot() { return this.mBrowserFwk.getRoot(); } @Override // android.support.v4.media.MediaBrowserCompat.MediaBrowserImpl @Nullable public Bundle getExtras() { return this.mBrowserFwk.getExtras(); } @Override // android.support.v4.media.MediaBrowserCompat.MediaBrowserImpl @NonNull public MediaSessionCompat.Token getSessionToken() { if (this.mMediaSessionToken == null) { this.mMediaSessionToken = MediaSessionCompat.Token.fromToken(this.mBrowserFwk.getSessionToken()); } return this.mMediaSessionToken; } @Override // android.support.v4.media.MediaBrowserCompat.MediaBrowserImpl public void subscribe(@NonNull String str, Bundle bundle, @NonNull SubscriptionCallback subscriptionCallback) { Subscription subscription = this.mSubscriptions.get(str); if (subscription == null) { subscription = new Subscription(); this.mSubscriptions.put(str, subscription); } subscriptionCallback.setSubscription(subscription); Bundle bundle2 = bundle == null ? null : new Bundle(bundle); subscription.putCallback(bundle2, subscriptionCallback); ServiceBinderWrapper serviceBinderWrapper = this.mServiceBinderWrapper; if (serviceBinderWrapper == null) { this.mBrowserFwk.subscribe(str, subscriptionCallback.mSubscriptionCallbackFwk); return; } try { serviceBinderWrapper.addSubscription(str, subscriptionCallback.mToken, bundle2, this.mCallbacksMessenger); } catch (RemoteException unused) { StringBuilder sb = new StringBuilder(); sb.append("Remote error subscribing media item: "); sb.append(str); } } @Override // android.support.v4.media.MediaBrowserCompat.MediaBrowserImpl public void unsubscribe(@NonNull String str, SubscriptionCallback subscriptionCallback) { Subscription subscription = this.mSubscriptions.get(str); if (subscription == null) { return; } ServiceBinderWrapper serviceBinderWrapper = this.mServiceBinderWrapper; if (serviceBinderWrapper != null) { try { if (subscriptionCallback == null) { serviceBinderWrapper.removeSubscription(str, null, this.mCallbacksMessenger); } else { List callbacks = subscription.getCallbacks(); List optionsList = subscription.getOptionsList(); for (int size = callbacks.size() - 1; size >= 0; size--) { if (callbacks.get(size) == subscriptionCallback) { this.mServiceBinderWrapper.removeSubscription(str, subscriptionCallback.mToken, this.mCallbacksMessenger); callbacks.remove(size); optionsList.remove(size); } } } } catch (RemoteException unused) { StringBuilder sb = new StringBuilder(); sb.append("removeSubscription failed with RemoteException parentId="); sb.append(str); } } else if (subscriptionCallback == null) { this.mBrowserFwk.unsubscribe(str); } else { List callbacks2 = subscription.getCallbacks(); List optionsList2 = subscription.getOptionsList(); for (int size2 = callbacks2.size() - 1; size2 >= 0; size2--) { if (callbacks2.get(size2) == subscriptionCallback) { callbacks2.remove(size2); optionsList2.remove(size2); } } if (callbacks2.size() == 0) { this.mBrowserFwk.unsubscribe(str); } } if (subscription.isEmpty() || subscriptionCallback == null) { this.mSubscriptions.remove(str); } } @Override // android.support.v4.media.MediaBrowserCompat.MediaBrowserImpl public void getItem(@NonNull final String str, @NonNull final ItemCallback itemCallback) { if (TextUtils.isEmpty(str)) { throw new IllegalArgumentException("mediaId is empty"); } if (itemCallback == null) { throw new IllegalArgumentException("cb is null"); } if (!this.mBrowserFwk.isConnected()) { this.mHandler.post(new Runnable() { // from class: android.support.v4.media.MediaBrowserCompat.MediaBrowserImplApi21.1 @Override // java.lang.Runnable public void run() { itemCallback.onError(str); } }); return; } if (this.mServiceBinderWrapper == null) { this.mHandler.post(new Runnable() { // from class: android.support.v4.media.MediaBrowserCompat.MediaBrowserImplApi21.2 @Override // java.lang.Runnable public void run() { itemCallback.onError(str); } }); return; } try { this.mServiceBinderWrapper.getMediaItem(str, new ItemReceiver(str, itemCallback, this.mHandler), this.mCallbacksMessenger); } catch (RemoteException unused) { StringBuilder sb = new StringBuilder(); sb.append("Remote error getting media item: "); sb.append(str); this.mHandler.post(new Runnable() { // from class: android.support.v4.media.MediaBrowserCompat.MediaBrowserImplApi21.3 @Override // java.lang.Runnable public void run() { itemCallback.onError(str); } }); } } @Override // android.support.v4.media.MediaBrowserCompat.MediaBrowserImpl public void search(@NonNull final String str, final Bundle bundle, @NonNull final SearchCallback searchCallback) { if (!isConnected()) { throw new IllegalStateException("search() called while not connected"); } if (this.mServiceBinderWrapper == null) { this.mHandler.post(new Runnable() { // from class: android.support.v4.media.MediaBrowserCompat.MediaBrowserImplApi21.4 @Override // java.lang.Runnable public void run() { searchCallback.onError(str, bundle); } }); return; } try { this.mServiceBinderWrapper.search(str, bundle, new SearchResultReceiver(str, bundle, searchCallback, this.mHandler), this.mCallbacksMessenger); } catch (RemoteException unused) { StringBuilder sb = new StringBuilder(); sb.append("Remote error searching items with query: "); sb.append(str); this.mHandler.post(new Runnable() { // from class: android.support.v4.media.MediaBrowserCompat.MediaBrowserImplApi21.5 @Override // java.lang.Runnable public void run() { searchCallback.onError(str, bundle); } }); } } @Override // android.support.v4.media.MediaBrowserCompat.MediaBrowserImpl public void sendCustomAction(@NonNull final String str, final Bundle bundle, @Nullable final CustomActionCallback customActionCallback) { if (!isConnected()) { throw new IllegalStateException("Cannot send a custom action (" + str + ") with extras " + bundle + " because the browser is not connected to the service."); } if (this.mServiceBinderWrapper == null && customActionCallback != null) { this.mHandler.post(new Runnable() { // from class: android.support.v4.media.MediaBrowserCompat.MediaBrowserImplApi21.6 @Override // java.lang.Runnable public void run() { customActionCallback.onError(str, bundle, null); } }); } try { this.mServiceBinderWrapper.sendCustomAction(str, bundle, new CustomActionResultReceiver(str, bundle, customActionCallback, this.mHandler), this.mCallbacksMessenger); } catch (RemoteException unused) { StringBuilder sb = new StringBuilder(); sb.append("Remote error sending a custom action: action="); sb.append(str); sb.append(", extras="); sb.append(bundle); if (customActionCallback != null) { this.mHandler.post(new Runnable() { // from class: android.support.v4.media.MediaBrowserCompat.MediaBrowserImplApi21.7 @Override // java.lang.Runnable public void run() { customActionCallback.onError(str, bundle, null); } }); } } } @Override // android.support.v4.media.MediaBrowserCompat.ConnectionCallback.ConnectionCallbackInternal public void onConnected() { try { Bundle extras = this.mBrowserFwk.getExtras(); if (extras == null) { return; } this.mServiceVersion = extras.getInt(MediaBrowserProtocol.EXTRA_SERVICE_VERSION, 0); IBinder binder = BundleCompat.getBinder(extras, MediaBrowserProtocol.EXTRA_MESSENGER_BINDER); if (binder != null) { this.mServiceBinderWrapper = new ServiceBinderWrapper(binder, this.mRootHints); Messenger messenger = new Messenger(this.mHandler); this.mCallbacksMessenger = messenger; this.mHandler.setCallbacksMessenger(messenger); try { this.mServiceBinderWrapper.registerCallbackMessenger(this.mContext, this.mCallbacksMessenger); } catch (RemoteException unused) { } } IMediaSession asInterface = IMediaSession.Stub.asInterface(BundleCompat.getBinder(extras, MediaBrowserProtocol.EXTRA_SESSION_BINDER)); if (asInterface != null) { this.mMediaSessionToken = MediaSessionCompat.Token.fromToken(this.mBrowserFwk.getSessionToken(), asInterface); } } catch (IllegalStateException e) { Log.e(MediaBrowserCompat.TAG, "Unexpected IllegalStateException", e); } } @Override // android.support.v4.media.MediaBrowserCompat.ConnectionCallback.ConnectionCallbackInternal public void onConnectionSuspended() { this.mServiceBinderWrapper = null; this.mCallbacksMessenger = null; this.mMediaSessionToken = null; this.mHandler.setCallbacksMessenger(null); } @Override // android.support.v4.media.MediaBrowserCompat.MediaBrowserServiceCallbackImpl public void onLoadChildren(Messenger messenger, String str, List list, Bundle bundle, Bundle bundle2) { if (this.mCallbacksMessenger != messenger) { return; } Subscription subscription = this.mSubscriptions.get(str); if (subscription == null) { if (MediaBrowserCompat.DEBUG) { StringBuilder sb = new StringBuilder(); sb.append("onLoadChildren for id that isn't subscribed id="); sb.append(str); return; } return; } SubscriptionCallback callback = subscription.getCallback(bundle); if (callback != null) { if (bundle == null) { if (list == null) { callback.onError(str); return; } this.mNotifyChildrenChangedOptions = bundle2; callback.onChildrenLoaded(str, list); this.mNotifyChildrenChangedOptions = null; return; } if (list == null) { callback.onError(str, bundle); return; } this.mNotifyChildrenChangedOptions = bundle2; callback.onChildrenLoaded(str, list, bundle); this.mNotifyChildrenChangedOptions = null; } } } @RequiresApi(23) public static class MediaBrowserImplApi23 extends MediaBrowserImplApi21 { public MediaBrowserImplApi23(Context context, ComponentName componentName, ConnectionCallback connectionCallback, Bundle bundle) { super(context, componentName, connectionCallback, bundle); } @Override // android.support.v4.media.MediaBrowserCompat.MediaBrowserImplApi21, android.support.v4.media.MediaBrowserCompat.MediaBrowserImpl public void getItem(@NonNull String str, @NonNull ItemCallback itemCallback) { if (this.mServiceBinderWrapper == null) { this.mBrowserFwk.getItem(str, itemCallback.mItemCallbackFwk); } else { super.getItem(str, itemCallback); } } } @RequiresApi(26) public static class MediaBrowserImplApi26 extends MediaBrowserImplApi23 { public MediaBrowserImplApi26(Context context, ComponentName componentName, ConnectionCallback connectionCallback, Bundle bundle) { super(context, componentName, connectionCallback, bundle); } @Override // android.support.v4.media.MediaBrowserCompat.MediaBrowserImplApi21, android.support.v4.media.MediaBrowserCompat.MediaBrowserImpl public void subscribe(@NonNull String str, @Nullable Bundle bundle, @NonNull SubscriptionCallback subscriptionCallback) { if (this.mServiceBinderWrapper != null && this.mServiceVersion >= 2) { super.subscribe(str, bundle, subscriptionCallback); } else if (bundle == null) { this.mBrowserFwk.subscribe(str, subscriptionCallback.mSubscriptionCallbackFwk); } else { this.mBrowserFwk.subscribe(str, bundle, subscriptionCallback.mSubscriptionCallbackFwk); } } @Override // android.support.v4.media.MediaBrowserCompat.MediaBrowserImplApi21, android.support.v4.media.MediaBrowserCompat.MediaBrowserImpl public void unsubscribe(@NonNull String str, SubscriptionCallback subscriptionCallback) { if (this.mServiceBinderWrapper != null && this.mServiceVersion >= 2) { super.unsubscribe(str, subscriptionCallback); } else if (subscriptionCallback == null) { this.mBrowserFwk.unsubscribe(str); } else { this.mBrowserFwk.unsubscribe(str, subscriptionCallback.mSubscriptionCallbackFwk); } } } public static class Subscription { private final List mCallbacks = new ArrayList(); private final List mOptionsList = new ArrayList(); public List getCallbacks() { return this.mCallbacks; } public List getOptionsList() { return this.mOptionsList; } public boolean isEmpty() { return this.mCallbacks.isEmpty(); } public SubscriptionCallback getCallback(Bundle bundle) { for (int i = 0; i < this.mOptionsList.size(); i++) { if (MediaBrowserCompatUtils.areSameOptions(this.mOptionsList.get(i), bundle)) { return this.mCallbacks.get(i); } } return null; } public void putCallback(Bundle bundle, SubscriptionCallback subscriptionCallback) { for (int i = 0; i < this.mOptionsList.size(); i++) { if (MediaBrowserCompatUtils.areSameOptions(this.mOptionsList.get(i), bundle)) { this.mCallbacks.set(i, subscriptionCallback); return; } } this.mCallbacks.add(subscriptionCallback); this.mOptionsList.add(bundle); } } public static class CallbackHandler extends Handler { private final WeakReference mCallbackImplRef; private WeakReference mCallbacksMessengerRef; public CallbackHandler(MediaBrowserServiceCallbackImpl mediaBrowserServiceCallbackImpl) { this.mCallbackImplRef = new WeakReference<>(mediaBrowserServiceCallbackImpl); } @Override // android.os.Handler public void handleMessage(@NonNull Message message) { WeakReference weakReference = this.mCallbacksMessengerRef; if (weakReference == null || weakReference.get() == null || this.mCallbackImplRef.get() == null) { return; } Bundle data = message.getData(); MediaSessionCompat.ensureClassLoader(data); MediaBrowserServiceCallbackImpl mediaBrowserServiceCallbackImpl = this.mCallbackImplRef.get(); Messenger messenger = this.mCallbacksMessengerRef.get(); try { int i = message.what; if (i == 1) { Bundle bundle = data.getBundle(MediaBrowserProtocol.DATA_ROOT_HINTS); MediaSessionCompat.ensureClassLoader(bundle); mediaBrowserServiceCallbackImpl.onServiceConnected(messenger, data.getString(MediaBrowserProtocol.DATA_MEDIA_ITEM_ID), (MediaSessionCompat.Token) data.getParcelable(MediaBrowserProtocol.DATA_MEDIA_SESSION_TOKEN), bundle); } else if (i == 2) { mediaBrowserServiceCallbackImpl.onConnectionFailed(messenger); } else if (i == 3) { Bundle bundle2 = data.getBundle(MediaBrowserProtocol.DATA_OPTIONS); MediaSessionCompat.ensureClassLoader(bundle2); Bundle bundle3 = data.getBundle(MediaBrowserProtocol.DATA_NOTIFY_CHILDREN_CHANGED_OPTIONS); MediaSessionCompat.ensureClassLoader(bundle3); mediaBrowserServiceCallbackImpl.onLoadChildren(messenger, data.getString(MediaBrowserProtocol.DATA_MEDIA_ITEM_ID), data.getParcelableArrayList(MediaBrowserProtocol.DATA_MEDIA_ITEM_LIST), bundle2, bundle3); } else { Log.w(MediaBrowserCompat.TAG, "Unhandled message: " + message + "\n Client version: 1\n Service version: " + message.arg1); } } catch (BadParcelableException unused) { Log.e(MediaBrowserCompat.TAG, "Could not unparcel the data."); if (message.what == 1) { mediaBrowserServiceCallbackImpl.onConnectionFailed(messenger); } } } public void setCallbacksMessenger(Messenger messenger) { this.mCallbacksMessengerRef = new WeakReference<>(messenger); } } public static class ServiceBinderWrapper { private Messenger mMessenger; private Bundle mRootHints; public ServiceBinderWrapper(IBinder iBinder, Bundle bundle) { this.mMessenger = new Messenger(iBinder); this.mRootHints = bundle; } public void connect(Context context, Messenger messenger) throws RemoteException { Bundle bundle = new Bundle(); bundle.putString(MediaBrowserProtocol.DATA_PACKAGE_NAME, context.getPackageName()); bundle.putInt(MediaBrowserProtocol.DATA_CALLING_PID, Process.myPid()); bundle.putBundle(MediaBrowserProtocol.DATA_ROOT_HINTS, this.mRootHints); sendRequest(1, bundle, messenger); } public void disconnect(Messenger messenger) throws RemoteException { sendRequest(2, null, messenger); } public void addSubscription(String str, IBinder iBinder, Bundle bundle, Messenger messenger) throws RemoteException { Bundle bundle2 = new Bundle(); bundle2.putString(MediaBrowserProtocol.DATA_MEDIA_ITEM_ID, str); BundleCompat.putBinder(bundle2, MediaBrowserProtocol.DATA_CALLBACK_TOKEN, iBinder); bundle2.putBundle(MediaBrowserProtocol.DATA_OPTIONS, bundle); sendRequest(3, bundle2, messenger); } public void removeSubscription(String str, IBinder iBinder, Messenger messenger) throws RemoteException { Bundle bundle = new Bundle(); bundle.putString(MediaBrowserProtocol.DATA_MEDIA_ITEM_ID, str); BundleCompat.putBinder(bundle, MediaBrowserProtocol.DATA_CALLBACK_TOKEN, iBinder); sendRequest(4, bundle, messenger); } public void getMediaItem(String str, ResultReceiver resultReceiver, Messenger messenger) throws RemoteException { Bundle bundle = new Bundle(); bundle.putString(MediaBrowserProtocol.DATA_MEDIA_ITEM_ID, str); bundle.putParcelable(MediaBrowserProtocol.DATA_RESULT_RECEIVER, resultReceiver); sendRequest(5, bundle, messenger); } public void registerCallbackMessenger(Context context, Messenger messenger) throws RemoteException { Bundle bundle = new Bundle(); bundle.putString(MediaBrowserProtocol.DATA_PACKAGE_NAME, context.getPackageName()); bundle.putInt(MediaBrowserProtocol.DATA_CALLING_PID, Process.myPid()); bundle.putBundle(MediaBrowserProtocol.DATA_ROOT_HINTS, this.mRootHints); sendRequest(6, bundle, messenger); } public void unregisterCallbackMessenger(Messenger messenger) throws RemoteException { sendRequest(7, null, messenger); } public void search(String str, Bundle bundle, ResultReceiver resultReceiver, Messenger messenger) throws RemoteException { Bundle bundle2 = new Bundle(); bundle2.putString(MediaBrowserProtocol.DATA_SEARCH_QUERY, str); bundle2.putBundle(MediaBrowserProtocol.DATA_SEARCH_EXTRAS, bundle); bundle2.putParcelable(MediaBrowserProtocol.DATA_RESULT_RECEIVER, resultReceiver); sendRequest(8, bundle2, messenger); } public void sendCustomAction(String str, Bundle bundle, ResultReceiver resultReceiver, Messenger messenger) throws RemoteException { Bundle bundle2 = new Bundle(); bundle2.putString(MediaBrowserProtocol.DATA_CUSTOM_ACTION, str); bundle2.putBundle(MediaBrowserProtocol.DATA_CUSTOM_ACTION_EXTRAS, bundle); bundle2.putParcelable(MediaBrowserProtocol.DATA_RESULT_RECEIVER, resultReceiver); sendRequest(9, bundle2, messenger); } private void sendRequest(int i, Bundle bundle, Messenger messenger) throws RemoteException { Message obtain = Message.obtain(); obtain.what = i; obtain.arg1 = 1; obtain.setData(bundle); obtain.replyTo = messenger; this.mMessenger.send(obtain); } } public static class ItemReceiver extends ResultReceiver { private final ItemCallback mCallback; private final String mMediaId; public ItemReceiver(String str, ItemCallback itemCallback, Handler handler) { super(handler); this.mMediaId = str; this.mCallback = itemCallback; } @Override // android.support.v4.os.ResultReceiver public void onReceiveResult(int i, Bundle bundle) { if (bundle != null) { bundle = MediaSessionCompat.unparcelWithClassLoader(bundle); } if (i != 0 || bundle == null || !bundle.containsKey(MediaBrowserServiceCompat.KEY_MEDIA_ITEM)) { this.mCallback.onError(this.mMediaId); return; } Parcelable parcelable = bundle.getParcelable(MediaBrowserServiceCompat.KEY_MEDIA_ITEM); if (parcelable == null || (parcelable instanceof MediaItem)) { this.mCallback.onItemLoaded((MediaItem) parcelable); } else { this.mCallback.onError(this.mMediaId); } } } public static class SearchResultReceiver extends ResultReceiver { private final SearchCallback mCallback; private final Bundle mExtras; private final String mQuery; public SearchResultReceiver(String str, Bundle bundle, SearchCallback searchCallback, Handler handler) { super(handler); this.mQuery = str; this.mExtras = bundle; this.mCallback = searchCallback; } @Override // android.support.v4.os.ResultReceiver public void onReceiveResult(int i, Bundle bundle) { if (bundle != null) { bundle = MediaSessionCompat.unparcelWithClassLoader(bundle); } if (i != 0 || bundle == null || !bundle.containsKey(MediaBrowserServiceCompat.KEY_SEARCH_RESULTS)) { this.mCallback.onError(this.mQuery, this.mExtras); return; } Parcelable[] parcelableArray = bundle.getParcelableArray(MediaBrowserServiceCompat.KEY_SEARCH_RESULTS); if (parcelableArray != null) { ArrayList arrayList = new ArrayList(); for (Parcelable parcelable : parcelableArray) { arrayList.add((MediaItem) parcelable); } this.mCallback.onSearchResult(this.mQuery, this.mExtras, arrayList); return; } this.mCallback.onError(this.mQuery, this.mExtras); } } public static class CustomActionResultReceiver extends ResultReceiver { private final String mAction; private final CustomActionCallback mCallback; private final Bundle mExtras; public CustomActionResultReceiver(String str, Bundle bundle, CustomActionCallback customActionCallback, Handler handler) { super(handler); this.mAction = str; this.mExtras = bundle; this.mCallback = customActionCallback; } @Override // android.support.v4.os.ResultReceiver public void onReceiveResult(int i, Bundle bundle) { if (this.mCallback == null) { return; } MediaSessionCompat.ensureClassLoader(bundle); if (i == -1) { this.mCallback.onError(this.mAction, this.mExtras, bundle); return; } if (i == 0) { this.mCallback.onResult(this.mAction, this.mExtras, bundle); return; } if (i == 1) { this.mCallback.onProgressUpdate(this.mAction, this.mExtras, bundle); return; } Log.w(MediaBrowserCompat.TAG, "Unknown result code: " + i + " (extras=" + this.mExtras + ", resultData=" + bundle + ")"); } } }