package androidx.media3.session;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.BadParcelableException;
import android.os.Bundle;
import android.os.Parcelable;
import android.os.RemoteException;
import android.text.TextUtils;
import androidx.media3.common.MediaItem;
import androidx.media3.common.MediaMetadata;
import androidx.media3.common.util.ConditionVariable;
import androidx.media3.common.util.Log;
import androidx.media3.common.util.Util;
import androidx.media3.session.MediaLibraryService;
import androidx.media3.session.MediaSession;
import androidx.media3.session.legacy.MediaBrowserCompat;
import androidx.media3.session.legacy.MediaBrowserServiceCompat;
import androidx.media3.session.legacy.MediaSessionManager;
import defpackage.bj4;
import defpackage.if7;
import defpackage.ks3;
import defpackage.o96;
import defpackage.om8;
import defpackage.qv;
import defpackage.qy3;
import defpackage.rq6;
import defpackage.zb5;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

/* JADX INFO: loaded from: classes.dex */
class MediaLibraryServiceLegacyStub extends MediaSessionServiceLegacyStub {
    private static final String TAG = "MLSLegacyStub";
    private final MediaSession.ControllerCb browserLegacyCbForBroadcast;
    private final MediaLibrarySessionImpl librarySessionImpl;

    public final class BrowserLegacyCb implements MediaSession.ControllerCb {
        private final MediaSessionManager.RemoteUserInfo remoteUserInfo;
        private final Object lock = new Object();

        @qy3
        private final List<SearchRequest> searchRequests = new ArrayList();

        public BrowserLegacyCb(MediaSessionManager.RemoteUserInfo remoteUserInfo) {
            this.remoteUserInfo = remoteUserInfo;
        }

        /* JADX INFO: Access modifiers changed from: private */
        public /* synthetic */ void lambda$onSearchResultChanged$0(List list) {
            int i;
            int i2;
            int i3;
            int i4;
            for (int i5 = 0; i5 < list.size(); i5++) {
                SearchRequest searchRequest = (SearchRequest) list.get(i5);
                Bundle bundle = searchRequest.extras;
                if (bundle != null) {
                    try {
                        bundle.setClassLoader(MediaLibraryServiceLegacyStub.this.librarySessionImpl.getContext().getClassLoader());
                        i = searchRequest.extras.getInt(MediaBrowserCompat.EXTRA_PAGE, -1);
                        i2 = searchRequest.extras.getInt(MediaBrowserCompat.EXTRA_PAGE_SIZE, -1);
                    } catch (BadParcelableException unused) {
                        searchRequest.result.sendResult(null);
                        return;
                    }
                } else {
                    i = 0;
                    i2 = Integer.MAX_VALUE;
                }
                if (i < 0 || i2 < 1) {
                    i3 = 0;
                    i4 = Integer.MAX_VALUE;
                } else {
                    i3 = i;
                    i4 = i2;
                }
                MediaLibraryServiceLegacyStub.sendLibraryResultWithMediaItemsWhenReady(searchRequest.result, Util.transformFutureAsync(MediaLibraryServiceLegacyStub.this.librarySessionImpl.onGetSearchResultOnHandler(searchRequest.controller, searchRequest.query, i3, i4, LegacyConversions.convertToLibraryParams(MediaLibraryServiceLegacyStub.this.librarySessionImpl.getContext(), searchRequest.extras)), MediaLibraryServiceLegacyStub.this.createMediaItemsToBrowserItemsAsyncFunction()));
            }
        }

        /* JADX INFO: Access modifiers changed from: private */
        public void registerSearchRequest(MediaSession.ControllerInfo controllerInfo, String str, @rq6 Bundle bundle, MediaBrowserServiceCompat.Result<List<MediaBrowserCompat.MediaItem>> result) {
            synchronized (this.lock) {
                this.searchRequests.add(new SearchRequest(controllerInfo, controllerInfo.getRemoteUserInfo(), str, bundle, result));
            }
        }

        public boolean equals(@rq6 Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj instanceof BrowserLegacyCb) {
                return Objects.equals(this.remoteUserInfo, ((BrowserLegacyCb) obj).remoteUserInfo);
            }
            return false;
        }

        public int hashCode() {
            return Objects.hash(this.remoteUserInfo);
        }

        @Override // androidx.media3.session.MediaSession.ControllerCb
        public void onChildrenChanged(int i, String str, int i2, @rq6 MediaLibraryService.LibraryParams libraryParams) throws RemoteException {
            Bundle bundle = libraryParams != null ? libraryParams.extras : null;
            MediaLibraryServiceLegacyStub mediaLibraryServiceLegacyStub = MediaLibraryServiceLegacyStub.this;
            MediaSessionManager.RemoteUserInfo remoteUserInfo = this.remoteUserInfo;
            if (bundle == null) {
                bundle = Bundle.EMPTY;
            }
            mediaLibraryServiceLegacyStub.notifyChildrenChanged(remoteUserInfo, str, bundle);
        }

        @Override // androidx.media3.session.MediaSession.ControllerCb
        public void onSearchResultChanged(int i, String str, int i2, @rq6 MediaLibraryService.LibraryParams libraryParams) throws RemoteException {
            ArrayList arrayList = new ArrayList();
            synchronized (this.lock) {
                try {
                    for (int size = this.searchRequests.size() - 1; size >= 0; size--) {
                        SearchRequest searchRequest = this.searchRequests.get(size);
                        if (Objects.equals(this.remoteUserInfo, searchRequest.remoteUserInfo) && searchRequest.query.equals(str)) {
                            arrayList.add(searchRequest);
                            this.searchRequests.remove(size);
                        }
                    }
                    if (arrayList.isEmpty()) {
                        return;
                    }
                    Util.postOrRun(MediaLibraryServiceLegacyStub.this.librarySessionImpl.getApplicationHandler(), new g(this, arrayList, 6));
                } catch (Throwable th) {
                    throw th;
                }
            }
        }
    }

    public final class BrowserLegacyCbForBroadcast implements MediaSession.ControllerCb {
        private BrowserLegacyCbForBroadcast() {
        }

        @Override // androidx.media3.session.MediaSession.ControllerCb
        public void onChildrenChanged(int i, String str, int i2, @rq6 MediaLibraryService.LibraryParams libraryParams) throws RemoteException {
            Bundle bundle;
            if (libraryParams == null || (bundle = libraryParams.extras) == null) {
                MediaLibraryServiceLegacyStub.this.notifyChildrenChanged(str);
            } else {
                MediaLibraryServiceLegacyStub.this.notifyChildrenChanged(str, (Bundle) Util.castNonNull(bundle));
            }
        }

        @Override // androidx.media3.session.MediaSession.ControllerCb
        public void onSearchResultChanged(int i, String str, int i2, @rq6 MediaLibraryService.LibraryParams libraryParams) throws RemoteException {
        }
    }

    public static class ProgressReporter implements MediaSession.ProgressReporter {

        @rq6
        private zb5<SessionResult> future;
        private final MediaBrowserServiceCompat.Result<Bundle> result;
        private final MediaLibrarySessionImpl session;

        public ProgressReporter(MediaLibrarySessionImpl mediaLibrarySessionImpl, MediaBrowserServiceCompat.Result<Bundle> result) {
            this.session = mediaLibrarySessionImpl;
            this.result = result;
        }

        @Override // androidx.media3.session.MediaSession.ProgressReporter
        public void sendProgressUpdate(Bundle bundle) {
            zb5<SessionResult> zb5Var = this.future;
            if ((zb5Var == null || !zb5Var.isDone()) && !this.session.isReleased()) {
                this.result.sendProgressUpdate(bundle);
            }
        }

        public void setFuture(zb5<SessionResult> zb5Var) {
            this.future = zb5Var;
        }
    }

    public static class SearchRequest {
        public final MediaSession.ControllerInfo controller;

        @rq6
        public final Bundle extras;
        public final String query;
        public final MediaSessionManager.RemoteUserInfo remoteUserInfo;
        public final MediaBrowserServiceCompat.Result<List<MediaBrowserCompat.MediaItem>> result;

        public SearchRequest(MediaSession.ControllerInfo controllerInfo, MediaSessionManager.RemoteUserInfo remoteUserInfo, String str, @rq6 Bundle bundle, MediaBrowserServiceCompat.Result<List<MediaBrowserCompat.MediaItem>> result) {
            this.controller = controllerInfo;
            this.remoteUserInfo = remoteUserInfo;
            this.query = str;
            this.extras = bundle;
            this.result = result;
        }
    }

    public MediaLibraryServiceLegacyStub(MediaLibrarySessionImpl mediaLibrarySessionImpl) {
        super(mediaLibrarySessionImpl);
        this.librarySessionImpl = mediaLibrarySessionImpl;
        this.browserLegacyCbForBroadcast = new BrowserLegacyCbForBroadcast();
    }

    private static <T> void cancelAllFutures(List<zb5<T>> list) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i) != null) {
                list.get(i).cancel(false);
            }
        }
    }

    private qv<LibraryResult<MediaItem>, MediaBrowserCompat.MediaItem> createMediaItemToBrowserItemAsyncFunction() {
        return new bq(this, 0);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public qv<LibraryResult<bj4<MediaItem>>, List<MediaBrowserCompat.MediaItem>> createMediaItemsToBrowserItemsAsyncFunction() {
        return new bq(this, 1);
    }

    @rq6
    private MediaSession.ControllerInfo getCurrentController() {
        return getConnectedControllersManager().getController(getCurrentBrowserInfo());
    }

    private void handleBitmapFuturesAllCompletedAndSetOutputFuture(List<zb5<Bitmap>> list, List<MediaItem> list2, om8<List<MediaBrowserCompat.MediaItem>> om8Var) {
        Bitmap bitmap;
        ArrayList arrayList = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            zb5<Bitmap> zb5Var = list.get(i);
            if (zb5Var != null) {
                try {
                    bitmap = (Bitmap) ks3.b(zb5Var);
                } catch (CancellationException | ExecutionException e) {
                    Log.d(TAG, "Failed to get bitmap", e);
                    bitmap = null;
                }
            } else {
                bitmap = null;
            }
            arrayList.add(LegacyConversions.convertToBrowserItem(list2.get(i), bitmap));
        }
        om8Var.set(arrayList);
    }

    private static <T> void ignoreFuture(Future<T> future) {
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static /* synthetic */ void lambda$createMediaItemToBrowserItemAsyncFunction$13(om8 om8Var, zb5 zb5Var) {
        if (om8Var.isCancelled()) {
            zb5Var.cancel(false);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static /* synthetic */ void lambda$createMediaItemToBrowserItemAsyncFunction$14(zb5 zb5Var, om8 om8Var, MediaItem mediaItem) {
        Bitmap bitmap;
        try {
            bitmap = (Bitmap) ks3.b(zb5Var);
        } catch (CancellationException | ExecutionException e) {
            Log.d(TAG, "failed to get bitmap", e);
            bitmap = null;
        }
        om8Var.set(LegacyConversions.convertToBrowserItem(mediaItem, bitmap));
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* JADX WARN: Multi-variable type inference failed */
    public zb5 lambda$createMediaItemToBrowserItemAsyncFunction$15(LibraryResult libraryResult) throws Exception {
        V v;
        if7.l(libraryResult, "LibraryResult must not be null");
        om8 om8Var = new om8();
        if (libraryResult.resultCode != 0 || (v = libraryResult.value) == 0) {
            om8Var.set(null);
            return om8Var;
        }
        MediaItem mediaItem = (MediaItem) v;
        MediaMetadata mediaMetadata = mediaItem.mediaMetadata;
        if (mediaMetadata.artworkData == null) {
            om8Var.set(LegacyConversions.convertToBrowserItem(mediaItem, null));
            return om8Var;
        }
        zb5<Bitmap> zb5VarDecodeBitmap = this.librarySessionImpl.getBitmapLoader().decodeBitmap(mediaMetadata.artworkData);
        om8Var.addListener(new g(om8Var, zb5VarDecodeBitmap, 5), o96.a());
        zb5VarDecodeBitmap.addListener(new bp(zb5VarDecodeBitmap, om8Var, mediaItem, 1), o96.a());
        return om8Var;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static /* synthetic */ void lambda$createMediaItemsToBrowserItemsAsyncFunction$10(om8 om8Var, List list) {
        if (om8Var.isCancelled()) {
            cancelAllFutures(list);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ void lambda$createMediaItemsToBrowserItemsAsyncFunction$11(AtomicInteger atomicInteger, bj4 bj4Var, List list, om8 om8Var) {
        if (atomicInteger.incrementAndGet() == bj4Var.size()) {
            handleBitmapFuturesAllCompletedAndSetOutputFuture(list, bj4Var, om8Var);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* JADX WARN: Multi-variable type inference failed */
    public zb5 lambda$createMediaItemsToBrowserItemsAsyncFunction$12(LibraryResult libraryResult) throws Exception {
        V v;
        if7.l(libraryResult, "LibraryResult must not be null");
        om8 om8Var = new om8();
        if (libraryResult.resultCode != 0 || (v = libraryResult.value) == 0) {
            om8Var.set(null);
            return om8Var;
        }
        bj4 bj4Var = (bj4) v;
        if (bj4Var.isEmpty()) {
            om8Var.set(new ArrayList());
            return om8Var;
        }
        ArrayList arrayList = new ArrayList();
        om8Var.addListener(new g(om8Var, arrayList, 4), o96.a());
        b bVar = new b(this, new AtomicInteger(0), bj4Var, arrayList, om8Var, 1);
        for (int i = 0; i < bj4Var.size(); i++) {
            MediaMetadata mediaMetadata = ((MediaItem) bj4Var.get(i)).mediaMetadata;
            if (mediaMetadata.artworkData == null) {
                arrayList.add(null);
                bVar.run();
            } else {
                zb5<Bitmap> zb5VarDecodeBitmap = this.librarySessionImpl.getBitmapLoader().decodeBitmap(mediaMetadata.artworkData);
                arrayList.add(zb5VarDecodeBitmap);
                zb5VarDecodeBitmap.addListener(bVar, o96.a());
            }
        }
        return om8Var;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ void lambda$onCustomAction$6(String str, MediaSession.ControllerInfo controllerInfo, MediaBrowserServiceCompat.Result result, Bundle bundle) {
        SessionCommand sessionCommand = new SessionCommand(str, Bundle.EMPTY);
        if (!getConnectedControllersManager().isSessionCommandAvailable(controllerInfo, sessionCommand)) {
            result.sendError(null);
            return;
        }
        ProgressReporter progressReporter = new ProgressReporter(this.librarySessionImpl, result);
        zb5<SessionResult> zb5VarOnCustomCommandOnHandler = this.librarySessionImpl.onCustomCommandOnHandler(controllerInfo, progressReporter, sessionCommand, bundle);
        progressReporter.setFuture(zb5VarOnCustomCommandOnHandler);
        sendCustomActionResultWhenReady(result, zb5VarOnCustomCommandOnHandler);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ void lambda$onGetRoot$0(AtomicReference atomicReference, MediaSession.ControllerInfo controllerInfo, MediaLibraryService.LibraryParams libraryParams, ConditionVariable conditionVariable) {
        atomicReference.set(this.librarySessionImpl.onGetLibraryRootOnHandler(controllerInfo, libraryParams));
        conditionVariable.open();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ void lambda$onLoadChildren$3(MediaSession.ControllerInfo controllerInfo, MediaBrowserServiceCompat.Result result, Bundle bundle, String str) {
        MediaSession.ControllerInfo controllerInfo2;
        String str2;
        MediaLibraryService.LibraryParams libraryParams;
        MediaLibraryService.LibraryParams libraryParams2 = null;
        if (!getConnectedControllersManager().isSessionCommandAvailable(controllerInfo, SessionCommand.COMMAND_CODE_LIBRARY_GET_CHILDREN)) {
            result.sendResult(null);
            return;
        }
        if (bundle != null) {
            bundle.setClassLoader(this.librarySessionImpl.getContext().getClassLoader());
            try {
                int i = bundle.getInt(MediaBrowserCompat.EXTRA_PAGE);
                int i2 = bundle.getInt(MediaBrowserCompat.EXTRA_PAGE_SIZE);
                MediaLibraryService.LibraryParams libraryParamsConvertToLibraryParams = LegacyConversions.convertToLibraryParams(this.librarySessionImpl.getContext(), bundle);
                if (i < 0 || i2 <= 0) {
                    controllerInfo2 = controllerInfo;
                    str2 = str;
                    libraryParams = libraryParamsConvertToLibraryParams;
                } else {
                    try {
                        controllerInfo2 = controllerInfo;
                        try {
                            str2 = str;
                            try {
                                sendLibraryResultWithMediaItemsWhenReady(result, Util.transformFutureAsync(this.librarySessionImpl.onGetChildrenOnHandler(controllerInfo2, str, i, i2, libraryParamsConvertToLibraryParams), createMediaItemsToBrowserItemsAsyncFunction()));
                                return;
                            } catch (BadParcelableException unused) {
                                libraryParams2 = libraryParamsConvertToLibraryParams;
                                libraryParams = libraryParams2;
                                sendLibraryResultWithMediaItemsWhenReady(result, Util.transformFutureAsync(this.librarySessionImpl.onGetChildrenOnHandler(controllerInfo2, str2, 0, Integer.MAX_VALUE, libraryParams), createMediaItemsToBrowserItemsAsyncFunction()));
                            }
                        } catch (BadParcelableException unused2) {
                            str2 = str;
                        }
                    } catch (BadParcelableException unused3) {
                        controllerInfo2 = controllerInfo;
                        str2 = str;
                    }
                }
            } catch (BadParcelableException unused4) {
                controllerInfo2 = controllerInfo;
                str2 = str;
            }
        } else {
            controllerInfo2 = controllerInfo;
            str2 = str;
            libraryParams = libraryParams2;
        }
        sendLibraryResultWithMediaItemsWhenReady(result, Util.transformFutureAsync(this.librarySessionImpl.onGetChildrenOnHandler(controllerInfo2, str2, 0, Integer.MAX_VALUE, libraryParams), createMediaItemsToBrowserItemsAsyncFunction()));
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ void lambda$onLoadItem$4(MediaSession.ControllerInfo controllerInfo, MediaBrowserServiceCompat.Result result, String str) {
        if (getConnectedControllersManager().isSessionCommandAvailable(controllerInfo, SessionCommand.COMMAND_CODE_LIBRARY_GET_ITEM)) {
            sendLibraryResultWithMediaItemWhenReady(result, Util.transformFutureAsync(this.librarySessionImpl.onGetItemOnHandler(controllerInfo, str), createMediaItemToBrowserItemAsyncFunction()));
        } else {
            result.sendResult(null);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void lambda$onSearch$5(MediaSession.ControllerInfo controllerInfo, MediaBrowserServiceCompat.Result result, String str, Bundle bundle) {
        if (!getConnectedControllersManager().isSessionCommandAvailable(controllerInfo, SessionCommand.COMMAND_CODE_LIBRARY_SEARCH)) {
            result.sendResult(null);
            return;
        }
        MediaSession.ControllerCb controllerCb = controllerInfo.getControllerCb();
        controllerCb.getClass();
        ((BrowserLegacyCb) controllerCb).registerSearchRequest(controllerInfo, str, bundle, result);
        ignoreFuture(this.librarySessionImpl.onSearchOnHandler(controllerInfo, str, LegacyConversions.convertToLibraryParams(this.librarySessionImpl.getContext(), bundle)));
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ void lambda$onSubscribe$1(MediaSession.ControllerInfo controllerInfo, Bundle bundle, String str) {
        if (getConnectedControllersManager().isSessionCommandAvailable(controllerInfo, SessionCommand.COMMAND_CODE_LIBRARY_SUBSCRIBE)) {
            ignoreFuture(this.librarySessionImpl.onSubscribeOnHandler(controllerInfo, str, LegacyConversions.convertToLibraryParams(this.librarySessionImpl.getContext(), bundle)));
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ void lambda$onUnsubscribe$2(MediaSession.ControllerInfo controllerInfo, String str) {
        if (getConnectedControllersManager().isSessionCommandAvailable(controllerInfo, SessionCommand.COMMAND_CODE_LIBRARY_UNSUBSCRIBE)) {
            ignoreFuture(this.librarySessionImpl.onUnsubscribeOnHandler(controllerInfo, str));
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* JADX WARN: Multi-variable type inference failed */
    public static /* synthetic */ void lambda$sendCustomActionResultWhenReady$7(zb5 zb5Var, MediaBrowserServiceCompat.Result result) {
        try {
            SessionResult sessionResult = (SessionResult) zb5Var.get();
            if7.l(sessionResult, "SessionResult must not be null");
            result.sendResult(sessionResult.extras);
        } catch (InterruptedException | CancellationException | ExecutionException e) {
            Log.w(TAG, "Custom action failed", e);
            result.sendError(null);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* JADX WARN: Multi-variable type inference failed */
    public static /* synthetic */ void lambda$sendLibraryResultWithMediaItemWhenReady$8(zb5 zb5Var, MediaBrowserServiceCompat.Result result) {
        try {
            result.sendResult((MediaBrowserCompat.MediaItem) zb5Var.get());
        } catch (InterruptedException | CancellationException | ExecutionException e) {
            Log.w(TAG, "Library operation failed", e);
            result.sendResult(null);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static /* synthetic */ void lambda$sendLibraryResultWithMediaItemsWhenReady$9(zb5 zb5Var, MediaBrowserServiceCompat.Result result) {
        try {
            List list = (List) zb5Var.get();
            result.sendResult(list == null ? null : MediaUtils.truncateListBySize(list, MediaUtils.TRANSACTION_SIZE_LIMIT_IN_BYTES));
        } catch (InterruptedException | CancellationException | ExecutionException e) {
            Log.w(TAG, "Library operation failed", e);
            result.sendResult(null);
        }
    }

    private static void sendCustomActionResultWhenReady(MediaBrowserServiceCompat.Result<Bundle> result, zb5<SessionResult> zb5Var) {
        zb5Var.addListener(new bn(zb5Var, result, 1), o96.a());
    }

    private static void sendLibraryResultWithMediaItemWhenReady(MediaBrowserServiceCompat.Result<MediaBrowserCompat.MediaItem> result, zb5<MediaBrowserCompat.MediaItem> zb5Var) {
        zb5Var.addListener(new bn(zb5Var, result, 0), o96.a());
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static void sendLibraryResultWithMediaItemsWhenReady(MediaBrowserServiceCompat.Result<List<MediaBrowserCompat.MediaItem>> result, zb5<List<MediaBrowserCompat.MediaItem>> zb5Var) {
        zb5Var.addListener(new bn(zb5Var, result, 2), o96.a());
    }

    @Override // androidx.media3.session.MediaSessionServiceLegacyStub
    public MediaSession.ControllerInfo createControllerInfo(MediaSessionManager.RemoteUserInfo remoteUserInfo, Bundle bundle) {
        return new MediaSession.ControllerInfo(remoteUserInfo, 0, 0, getMediaSessionManager().isTrustedForMediaControl(remoteUserInfo), new BrowserLegacyCb(remoteUserInfo), bundle, LegacyConversions.extractMaxCommandsForMediaItemFromRootHints(bundle), true);
    }

    public MediaSession.ControllerCb getBrowserLegacyCbForBroadcast() {
        return this.browserLegacyCbForBroadcast;
    }

    @Override // androidx.media3.session.legacy.MediaBrowserServiceCompat
    public void onCustomAction(String str, Bundle bundle, MediaBrowserServiceCompat.Result<Bundle> result) {
        MediaSession.ControllerInfo currentController = getCurrentController();
        if (currentController == null) {
            result.sendError(null);
        } else {
            result.detach();
            Util.postOrRun(this.librarySessionImpl.getApplicationHandler(), new bo(this, str, currentController, result, bundle));
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    @Override // androidx.media3.session.MediaSessionServiceLegacyStub, androidx.media3.session.legacy.MediaBrowserServiceCompat
    @rq6
    public MediaBrowserServiceCompat.BrowserRoot onGetRoot(@rq6 String str, int i, @rq6 Bundle bundle) {
        MediaSession.ControllerInfo currentController;
        LibraryResult libraryResult;
        if (super.onGetRoot(str, i, bundle) == null || (currentController = getCurrentController()) == null || !getConnectedControllersManager().isSessionCommandAvailable(currentController, 50000)) {
            return null;
        }
        MediaLibraryService.LibraryParams libraryParamsConvertToLibraryParams = LegacyConversions.convertToLibraryParams(this.librarySessionImpl.getContext(), bundle);
        AtomicReference atomicReference = new AtomicReference();
        ConditionVariable conditionVariable = new ConditionVariable();
        Util.postOrRun(this.librarySessionImpl.getApplicationHandler(), new b(this, atomicReference, currentController, libraryParamsConvertToLibraryParams, conditionVariable, 2));
        try {
            conditionVariable.block();
            libraryResult = (LibraryResult) ((zb5) atomicReference.get()).get();
            if7.l(libraryResult, "LibraryResult must not be null");
        } catch (InterruptedException | CancellationException | ExecutionException e) {
            Log.e(TAG, "Couldn't get a result from onGetLibraryRoot", e);
            libraryResult = null;
        }
        if (libraryResult == null || libraryResult.resultCode != 0 || libraryResult.value == 0) {
            if (libraryResult == null || libraryResult.resultCode == 0) {
                return MediaUtils.defaultBrowserRoot;
            }
            return null;
        }
        MediaLibraryService.LibraryParams libraryParams = libraryResult.params;
        Bundle bundleConvertToRootHints = libraryParams != null ? LegacyConversions.convertToRootHints(libraryParams) : new Bundle();
        boolean zIsSessionCommandAvailable = getConnectedControllersManager().isSessionCommandAvailable(currentController, SessionCommand.COMMAND_CODE_LIBRARY_SEARCH);
        bundleConvertToRootHints.getClass();
        bundleConvertToRootHints.putBoolean(androidx.media3.session.legacy.MediaConstants.BROWSER_SERVICE_EXTRAS_KEY_SEARCH_SUPPORTED, zIsSessionCommandAvailable);
        bj4<CommandButton> commandButtonsForMediaItems = this.librarySessionImpl.getCommandButtonsForMediaItems();
        if (!commandButtonsForMediaItems.isEmpty()) {
            ArrayList<? extends Parcelable> arrayList = new ArrayList<>();
            for (int i2 = 0; i2 < commandButtonsForMediaItems.size(); i2++) {
                CommandButton commandButton = commandButtonsForMediaItems.get(i2);
                SessionCommand sessionCommand = commandButton.sessionCommand;
                if (sessionCommand != null && sessionCommand.commandCode == 0) {
                    arrayList.add(LegacyConversions.convertToBundle(commandButton));
                }
            }
            if (!arrayList.isEmpty()) {
                bundleConvertToRootHints.putParcelableArrayList(androidx.media3.session.legacy.MediaConstants.BROWSER_SERVICE_EXTRAS_KEY_CUSTOM_BROWSER_ACTION_ROOT_LIST, arrayList);
            }
        }
        return new MediaBrowserServiceCompat.BrowserRoot(((MediaItem) libraryResult.value).mediaId, bundleConvertToRootHints);
    }

    @Override // androidx.media3.session.MediaSessionServiceLegacyStub, androidx.media3.session.legacy.MediaBrowserServiceCompat
    public void onLoadChildren(@rq6 String str, MediaBrowserServiceCompat.Result<List<MediaBrowserCompat.MediaItem>> result) {
        onLoadChildren(str, result, null);
    }

    @Override // androidx.media3.session.legacy.MediaBrowserServiceCompat
    public void onLoadItem(String str, MediaBrowserServiceCompat.Result<MediaBrowserCompat.MediaItem> result) {
        MediaSession.ControllerInfo currentController = getCurrentController();
        if (currentController == null) {
            result.sendResult(null);
            return;
        }
        if (!TextUtils.isEmpty(str)) {
            result.detach();
            Util.postOrRun(this.librarySessionImpl.getApplicationHandler(), new d(this, currentController, result, str, 2));
        } else {
            Log.w(TAG, "Ignoring empty itemId from " + currentController);
            result.sendResult(null);
        }
    }

    @Override // androidx.media3.session.legacy.MediaBrowserServiceCompat
    public void onSearch(String str, @rq6 Bundle bundle, MediaBrowserServiceCompat.Result<List<MediaBrowserCompat.MediaItem>> result) {
        MediaSession.ControllerInfo currentController = getCurrentController();
        if (currentController == null) {
            result.sendResult(null);
            return;
        }
        if (TextUtils.isEmpty(str)) {
            Log.w(TAG, "Ignoring empty query from " + currentController);
            result.sendResult(null);
            return;
        }
        if (currentController.getControllerCb() instanceof BrowserLegacyCb) {
            result.detach();
            Util.postOrRun(this.librarySessionImpl.getApplicationHandler(), new bo(this, currentController, result, str, bundle));
        }
    }

    @Override // androidx.media3.session.legacy.MediaBrowserServiceCompat
    @SuppressLint({"RestrictedApi"})
    public void onSubscribe(@rq6 String str, @rq6 Bundle bundle) {
        MediaSession.ControllerInfo currentController = getCurrentController();
        if (currentController == null) {
            return;
        }
        if (!TextUtils.isEmpty(str)) {
            Util.postOrRun(this.librarySessionImpl.getApplicationHandler(), new d(this, currentController, bundle, str, 1));
            return;
        }
        Log.w(TAG, "onSubscribe(): Ignoring empty id from " + currentController);
    }

    @Override // androidx.media3.session.legacy.MediaBrowserServiceCompat
    @SuppressLint({"RestrictedApi"})
    public void onUnsubscribe(@rq6 String str) {
        MediaSession.ControllerInfo currentController = getCurrentController();
        if (currentController == null) {
            return;
        }
        if (!TextUtils.isEmpty(str)) {
            Util.postOrRun(this.librarySessionImpl.getApplicationHandler(), new bp(this, currentController, str, 0));
            return;
        }
        Log.w(TAG, "onUnsubscribe(): Ignoring empty id from " + currentController);
    }

    @Override // androidx.media3.session.legacy.MediaBrowserServiceCompat
    public void onLoadChildren(@rq6 String str, MediaBrowserServiceCompat.Result<List<MediaBrowserCompat.MediaItem>> result, @rq6 Bundle bundle) {
        MediaSession.ControllerInfo currentController = getCurrentController();
        if (currentController == null) {
            result.sendResult(null);
            return;
        }
        if (!TextUtils.isEmpty(str)) {
            result.detach();
            Util.postOrRun(this.librarySessionImpl.getApplicationHandler(), new bo(this, currentController, result, bundle, str));
        } else {
            Log.w(TAG, "onLoadChildren(): Ignoring empty parentId from " + currentController);
            result.sendResult(null);
        }
    }
}
