package androidx.media3.session;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import androidx.annotation.IntRange;
import androidx.annotation.Nullable;
import androidx.media3.common.util.Assertions;
import androidx.media3.common.util.Consumer;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.common.util.Util;
import androidx.media3.datasource.DataSourceBitmapLoader;
import androidx.media3.session.MediaController;
import androidx.media3.session.MediaLibraryService;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import org.checkerframework.checker.initialization.qual.NotOnlyInitialized;

/* JADX INFO: loaded from: classes.dex */
public final class MediaBrowser extends MediaController {
    private static final String WRONG_THREAD_ERROR_MESSAGE = "MediaBrowser method is called from a wrong thread. See javadoc of MediaController for details.";

    @NotOnlyInitialized
    private MediaBrowserImpl impl;

    public static final class Builder {
        private androidx.media3.common.util.BitmapLoader bitmapLoader;
        private final Context context;
        private int maxCommandsForMediaItems;
        private final SessionToken token;
        private Bundle connectionHints = Bundle.EMPTY;
        private Listener listener = new Listener() { // from class: androidx.media3.session.MediaBrowser.Builder.1
        };
        private Looper applicationLooper = Util.getCurrentOrMainLooper();
        private long platformSessionCallbackAggregationTimeoutMs = 100;

        public Builder(Context context, SessionToken sessionToken) {
            this.context = (Context) Assertions.checkNotNull(context);
            this.token = (SessionToken) Assertions.checkNotNull(sessionToken);
        }

        /* JADX WARN: Type inference failed for: r11v0, types: [androidx.media3.session.MediaController$ConnectionCallback, androidx.media3.session.MediaControllerHolder, com.google.common.util.concurrent.ListenableFuture] */
        public ListenableFuture buildAsync() {
            final ?? mediaControllerHolder = new MediaControllerHolder(this.applicationLooper);
            if (this.token.isLegacySession() && this.bitmapLoader == null) {
                this.bitmapLoader = new CacheBitmapLoader(new DataSourceBitmapLoader(this.context));
            }
            final MediaBrowser mediaBrowser = new MediaBrowser(this.context, this.token, this.connectionHints, this.listener, this.applicationLooper, mediaControllerHolder, this.bitmapLoader, this.maxCommandsForMediaItems, this.platformSessionCallbackAggregationTimeoutMs);
            Util.postOrRun(new Handler(this.applicationLooper), new Runnable() { // from class: androidx.media3.session.l
                @Override // java.lang.Runnable
                public final void run() {
                    mediaControllerHolder.setController(mediaBrowser);
                }
            });
            return mediaControllerHolder;
        }

        @CanIgnoreReturnValue
        @UnstableApi
        public Builder experimentalSetPlatformSessionCallbackAggregationTimeoutMs(long j2) {
            this.platformSessionCallbackAggregationTimeoutMs = j2;
            return this;
        }

        @CanIgnoreReturnValue
        public Builder setApplicationLooper(Looper looper) {
            this.applicationLooper = (Looper) Assertions.checkNotNull(looper);
            return this;
        }

        @CanIgnoreReturnValue
        @UnstableApi
        public Builder setBitmapLoader(androidx.media3.common.util.BitmapLoader bitmapLoader) {
            this.bitmapLoader = (androidx.media3.common.util.BitmapLoader) Assertions.checkNotNull(bitmapLoader);
            return this;
        }

        @CanIgnoreReturnValue
        public Builder setConnectionHints(Bundle bundle) {
            this.connectionHints = new Bundle((Bundle) Assertions.checkNotNull(bundle));
            return this;
        }

        @CanIgnoreReturnValue
        public Builder setListener(Listener listener) {
            this.listener = (Listener) Assertions.checkNotNull(listener);
            return this;
        }

        @CanIgnoreReturnValue
        @UnstableApi
        public Builder setMaxCommandsForMediaItems(int i2) {
            Assertions.checkArgument(i2 >= 0);
            this.maxCommandsForMediaItems = i2;
            return this;
        }
    }

    public interface Listener extends MediaController.Listener {
        default void onChildrenChanged(MediaBrowser mediaBrowser, String str, @IntRange(from = 0) int i2, @Nullable MediaLibraryService.LibraryParams libraryParams) {
        }

        default void onSearchResultChanged(MediaBrowser mediaBrowser, String str, @IntRange(from = 0) int i2, @Nullable MediaLibraryService.LibraryParams libraryParams) {
        }
    }

    interface MediaBrowserImpl extends MediaController.MediaControllerImpl {
        ListenableFuture getChildren(String str, int i2, int i3, @Nullable MediaLibraryService.LibraryParams libraryParams);

        ListenableFuture getItem(String str);

        ListenableFuture getLibraryRoot(@Nullable MediaLibraryService.LibraryParams libraryParams);

        ListenableFuture getSearchResult(String str, int i2, int i3, @Nullable MediaLibraryService.LibraryParams libraryParams);

        ListenableFuture search(String str, @Nullable MediaLibraryService.LibraryParams libraryParams);

        ListenableFuture subscribe(String str, @Nullable MediaLibraryService.LibraryParams libraryParams);

        ListenableFuture unsubscribe(String str);
    }

    MediaBrowser(Context context, SessionToken sessionToken, Bundle bundle, Listener listener, Looper looper, MediaController.ConnectionCallback connectionCallback, @Nullable androidx.media3.common.util.BitmapLoader bitmapLoader, int i2, long j2) {
        super(context, sessionToken, bundle, listener, looper, connectionCallback, bitmapLoader, i2, j2);
    }

    private static <V> ListenableFuture createDisconnectedFuture() {
        return Futures.immediateFuture(LibraryResult.ofError(-100));
    }

    private void verifyApplicationThread() {
        Assertions.checkState(Looper.myLooper() == getApplicationLooper(), WRONG_THREAD_ERROR_MESSAGE);
    }

    public ListenableFuture getChildren(String str, @IntRange(from = 0) int i2, @IntRange(from = 1) int i3, @Nullable MediaLibraryService.LibraryParams libraryParams) {
        verifyApplicationThread();
        Assertions.checkNotEmpty(str, "parentId must not be empty");
        Assertions.checkArgument(i2 >= 0, "page must not be negative");
        Assertions.checkArgument(i3 >= 1, "pageSize must not be less than 1");
        return isConnected() ? ((MediaBrowserImpl) Assertions.checkNotNull(this.impl)).getChildren(str, i2, i3, libraryParams) : createDisconnectedFuture();
    }

    public ListenableFuture getItem(String str) {
        verifyApplicationThread();
        Assertions.checkNotEmpty(str, "mediaId must not be empty");
        return isConnected() ? ((MediaBrowserImpl) Assertions.checkNotNull(this.impl)).getItem(str) : createDisconnectedFuture();
    }

    public ListenableFuture getLibraryRoot(@Nullable MediaLibraryService.LibraryParams libraryParams) {
        verifyApplicationThread();
        return isConnected() ? ((MediaBrowserImpl) Assertions.checkNotNull(this.impl)).getLibraryRoot(libraryParams) : createDisconnectedFuture();
    }

    public ListenableFuture getSearchResult(String str, @IntRange(from = 0) int i2, @IntRange(from = 1) int i3, @Nullable MediaLibraryService.LibraryParams libraryParams) {
        verifyApplicationThread();
        Assertions.checkNotEmpty(str, "query must not be empty");
        Assertions.checkArgument(i2 >= 0, "page must not be negative");
        Assertions.checkArgument(i3 >= 1, "pageSize must not be less than 1");
        return isConnected() ? ((MediaBrowserImpl) Assertions.checkNotNull(this.impl)).getSearchResult(str, i2, i3, libraryParams) : createDisconnectedFuture();
    }

    void notifyBrowserListener(final Consumer<Listener> consumer) {
        final Listener listener = (Listener) this.listener;
        if (listener != null) {
            Util.postOrRun(this.applicationHandler, new Runnable() { // from class: androidx.media3.session.k
                @Override // java.lang.Runnable
                public final void run() {
                    consumer.accept(listener);
                }
            });
        }
    }

    public ListenableFuture search(String str, @Nullable MediaLibraryService.LibraryParams libraryParams) {
        verifyApplicationThread();
        Assertions.checkNotEmpty(str, "query must not be empty");
        return isConnected() ? ((MediaBrowserImpl) Assertions.checkNotNull(this.impl)).search(str, libraryParams) : createDisconnectedFuture();
    }

    public ListenableFuture subscribe(String str, @Nullable MediaLibraryService.LibraryParams libraryParams) {
        verifyApplicationThread();
        Assertions.checkNotEmpty(str, "parentId must not be empty");
        return isConnected() ? ((MediaBrowserImpl) Assertions.checkNotNull(this.impl)).subscribe(str, libraryParams) : createDisconnectedFuture();
    }

    public ListenableFuture unsubscribe(String str) {
        verifyApplicationThread();
        Assertions.checkNotEmpty(str, "parentId must not be empty");
        return isConnected() ? ((MediaBrowserImpl) Assertions.checkNotNull(this.impl)).unsubscribe(str) : createDisconnectedFuture();
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    @Override // androidx.media3.session.MediaController
    public MediaBrowserImpl createImpl(Context context, SessionToken sessionToken, Bundle bundle, Looper looper, @Nullable androidx.media3.common.util.BitmapLoader bitmapLoader, long j2) {
        MediaBrowserImpl mediaBrowserImplLegacy = sessionToken.isLegacySession() ? new MediaBrowserImplLegacy(context, this, sessionToken, bundle, looper, (androidx.media3.common.util.BitmapLoader) Assertions.checkNotNull(bitmapLoader), j2) : new MediaBrowserImplBase(context, this, sessionToken, bundle, looper);
        this.impl = mediaBrowserImplLegacy;
        return mediaBrowserImplLegacy;
    }
}
