package androidx.media3.session;

import android.app.PendingIntent;
import android.os.Binder;
import android.os.Bundle;
import android.os.RemoteException;
import android.text.TextUtils;
import androidx.media3.common.Player;
import androidx.media3.common.util.BundleCollectionUtil;
import androidx.media3.common.util.Log;
import androidx.media3.common.util.Util;
import androidx.media3.session.IMediaController;
import androidx.media3.session.MediaLibraryService;
import androidx.media3.session.PlayerInfo;
import defpackage.rq6;
import defpackage.tb5;
import defpackage.vw2;
import java.lang.ref.WeakReference;
import java.util.List;
import java.util.Objects;

/* JADX INFO: loaded from: classes.dex */
class MediaControllerStub extends IMediaController.Stub {
    private static final String TAG = "MediaControllerStub";
    public static final int VERSION_INT = 8;
    private final WeakReference<MediaControllerImplBase> controller;

    public interface ControllerTask<T extends MediaControllerImplBase> {
        void run(T t);
    }

    public MediaControllerStub(MediaControllerImplBase mediaControllerImplBase) {
        this.controller = new WeakReference<>(mediaControllerImplBase);
    }

    private <T extends MediaControllerImplBase> void dispatchControllerTaskOnHandler(ControllerTask<T> controllerTask) {
        long jClearCallingIdentity = Binder.clearCallingIdentity();
        try {
            MediaControllerImplBase mediaControllerImplBase = this.controller.get();
            if (mediaControllerImplBase == null) {
                return;
            }
            Util.postOrRun(mediaControllerImplBase.getInstance().applicationHandler, new g(mediaControllerImplBase, controllerTask, 3));
        } finally {
            Binder.restoreCallingIdentity(jClearCallingIdentity);
        }
    }

    private int getSessionInterfaceVersion() {
        SessionToken connectedToken;
        MediaControllerImplBase mediaControllerImplBase = this.controller.get();
        if (mediaControllerImplBase == null || (connectedToken = mediaControllerImplBase.getConnectedToken()) == null) {
            return -1;
        }
        return connectedToken.getInterfaceVersion();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static /* synthetic */ void lambda$dispatchControllerTaskOnHandler$18(MediaControllerImplBase mediaControllerImplBase, ControllerTask controllerTask) {
        if (mediaControllerImplBase.isReleased()) {
            return;
        }
        controllerTask.run(mediaControllerImplBase);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static /* synthetic */ void lambda$onDisconnected$1(MediaControllerImplBase mediaControllerImplBase) {
        MediaController mediaControllerImplBase2 = mediaControllerImplBase.getInstance();
        MediaController mediaControllerImplBase3 = mediaControllerImplBase.getInstance();
        Objects.requireNonNull(mediaControllerImplBase3);
        mediaControllerImplBase2.runOnApplicationLooper(new tb5(mediaControllerImplBase3, 5));
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static /* synthetic */ CommandButton lambda$onSetCustomLayout$2(int i, Bundle bundle) {
        return CommandButton.fromBundle(bundle, i);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static /* synthetic */ CommandButton lambda$onSetMediaButtonPreferences$4(int i, Bundle bundle) {
        return CommandButton.fromBundle(bundle, i);
    }

    private <T> void setControllerFutureResult(int i, T t) {
        long jClearCallingIdentity = Binder.clearCallingIdentity();
        try {
            MediaControllerImplBase mediaControllerImplBase = this.controller.get();
            if (mediaControllerImplBase == null) {
                return;
            }
            mediaControllerImplBase.setFutureResult(i, t);
        } finally {
            Binder.restoreCallingIdentity(jClearCallingIdentity);
        }
    }

    public void destroy() {
        this.controller.clear();
    }

    @Override // androidx.media3.session.IMediaController
    public void onAvailableCommandsChangedFromPlayer(int i, @rq6 Bundle bundle) {
        if (bundle == null) {
            return;
        }
        try {
            dispatchControllerTaskOnHandler(new bl(Player.Commands.fromBundle(bundle)));
        } catch (RuntimeException e) {
            Log.w(TAG, "Ignoring malformed Bundle for Commands", e);
        }
    }

    @Override // androidx.media3.session.IMediaController
    public void onAvailableCommandsChangedFromSession(int i, @rq6 Bundle bundle, @rq6 Bundle bundle2) {
        if (bundle == null || bundle2 == null) {
            return;
        }
        try {
            try {
                dispatchControllerTaskOnHandler(new bm(SessionCommands.fromBundle(bundle), Player.Commands.fromBundle(bundle2)));
            } catch (RuntimeException e) {
                Log.w(TAG, "Ignoring malformed Bundle for Commands", e);
            }
        } catch (RuntimeException e2) {
            Log.w(TAG, "Ignoring malformed Bundle for SessionCommands", e2);
        }
    }

    @Override // androidx.media3.session.IMediaController
    public void onChildrenChanged(int i, @rq6 String str, int i2, @rq6 Bundle bundle) {
        MediaLibraryService.LibraryParams libraryParamsFromBundle;
        if (TextUtils.isEmpty(str)) {
            Log.w(TAG, "onChildrenChanged(): Ignoring empty parentId");
            return;
        }
        if (i2 < 0) {
            vw2.u(i2, "onChildrenChanged(): Ignoring negative itemCount: ", TAG);
            return;
        }
        if (bundle == null) {
            libraryParamsFromBundle = null;
        } else {
            try {
                libraryParamsFromBundle = MediaLibraryService.LibraryParams.fromBundle(bundle);
            } catch (RuntimeException e) {
                Log.w(TAG, "Ignoring malformed Bundle for LibraryParams", e);
                return;
            }
        }
        dispatchControllerTaskOnHandler(new bk(i2, str, 1, libraryParamsFromBundle));
    }

    @Override // androidx.media3.session.IMediaController
    public void onConnected(int i, @rq6 Bundle bundle) {
        if (bundle == null) {
            return;
        }
        try {
            dispatchControllerTaskOnHandler(new bi(ConnectionState.fromBundle(bundle), 0));
        } catch (RuntimeException e) {
            Log.w(TAG, "Malformed Bundle for ConnectionResult. Disconnected from the session.", e);
            onDisconnected(i);
        }
    }

    @Override // androidx.media3.session.IMediaController
    public void onCustomCommand(int i, @rq6 Bundle bundle, @rq6 Bundle bundle2) {
        if (bundle == null || bundle2 == null) {
            Log.w(TAG, "Ignoring custom command with null args.");
            return;
        }
        try {
            dispatchControllerTaskOnHandler(new av(i, SessionCommand.fromBundle(bundle), bundle2));
        } catch (RuntimeException e) {
            Log.w(TAG, "Ignoring malformed Bundle for SessionCommand", e);
        }
    }

    @Override // androidx.media3.session.IMediaController
    public void onCustomCommandProgressUpdate(int i, Bundle bundle, Bundle bundle2, Bundle bundle3) throws RemoteException {
        if (bundle == null || bundle2 == null) {
            Log.w(TAG, "Ignoring custom command progress update with null args.");
            return;
        }
        try {
            dispatchControllerTaskOnHandler(new bh(i, SessionCommand.fromBundle(bundle), bundle2, bundle3));
        } catch (RuntimeException e) {
            Log.w(TAG, "Ignoring malformed Bundle for SessionCommand", e);
        }
    }

    @Override // androidx.media3.session.IMediaController
    public void onDisconnected(int i) {
        dispatchControllerTaskOnHandler(new t(3));
    }

    @Override // androidx.media3.session.IMediaController
    public void onError(int i, Bundle bundle) throws RemoteException {
        try {
            dispatchControllerTaskOnHandler(new bj(i, SessionError.fromBundle(bundle), 2));
        } catch (RuntimeException e) {
            Log.w(TAG, "Ignoring malformed Bundle for SessionError", e);
        }
    }

    @Override // androidx.media3.session.IMediaController
    public void onExtrasChanged(int i, @rq6 Bundle bundle) {
        Bundle bundleConvertToNullIfInvalid = Util.convertToNullIfInvalid(bundle);
        if (bundleConvertToNullIfInvalid == null) {
            Log.w(TAG, "Ignoring null Bundle for extras");
        } else {
            dispatchControllerTaskOnHandler(new bg(0, bundleConvertToNullIfInvalid));
        }
    }

    @Override // androidx.media3.session.IMediaController
    public void onLibraryResult(int i, @rq6 Bundle bundle) {
        if (bundle == null) {
            return;
        }
        try {
            setControllerFutureResult(i, LibraryResult.fromUnknownBundle(bundle));
        } catch (RuntimeException e) {
            Log.w(TAG, "Ignoring malformed Bundle for LibraryResult", e);
        }
    }

    @Override // androidx.media3.session.IMediaController
    public void onPeriodicSessionPositionInfoChanged(int i, @rq6 Bundle bundle) {
        if (bundle == null) {
            return;
        }
        try {
            dispatchControllerTaskOnHandler(new bi(SessionPositionInfo.fromBundle(bundle), 1));
        } catch (RuntimeException e) {
            Log.w(TAG, "Ignoring malformed Bundle for SessionPositionInfo", e);
        }
    }

    @Override // androidx.media3.session.IMediaController
    @Deprecated
    public void onPlayerInfoChanged(int i, @rq6 Bundle bundle, boolean z) {
        onPlayerInfoChangedWithExclusions(i, bundle, new PlayerInfo.BundlingExclusions(z, true).toBundle());
    }

    @Override // androidx.media3.session.IMediaController
    public void onPlayerInfoChangedWithExclusions(int i, @rq6 Bundle bundle, @rq6 Bundle bundle2) {
        if (bundle == null || bundle2 == null) {
            return;
        }
        try {
            int sessionInterfaceVersion = getSessionInterfaceVersion();
            if (sessionInterfaceVersion == -1) {
                return;
            }
            try {
                dispatchControllerTaskOnHandler(new j(PlayerInfo.fromBundle(bundle, sessionInterfaceVersion), PlayerInfo.BundlingExclusions.fromBundle(bundle2), 12));
            } catch (RuntimeException e) {
                Log.w(TAG, "Ignoring malformed Bundle for BundlingExclusions", e);
            }
        } catch (RuntimeException e2) {
            Log.w(TAG, "Ignoring malformed Bundle for PlayerInfo", e2);
        }
    }

    @Override // androidx.media3.session.IMediaController
    public void onRenderedFirstFrame(int i) {
        dispatchControllerTaskOnHandler(new t(2));
    }

    @Override // androidx.media3.session.IMediaController
    public void onSearchResultChanged(int i, @rq6 String str, int i2, @rq6 Bundle bundle) throws RuntimeException {
        MediaLibraryService.LibraryParams libraryParamsFromBundle;
        if (TextUtils.isEmpty(str)) {
            Log.w(TAG, "onSearchResultChanged(): Ignoring empty query");
            return;
        }
        if (i2 < 0) {
            vw2.u(i2, "onSearchResultChanged(): Ignoring negative itemCount: ", TAG);
            return;
        }
        if (bundle == null) {
            libraryParamsFromBundle = null;
        } else {
            try {
                libraryParamsFromBundle = MediaLibraryService.LibraryParams.fromBundle(bundle);
            } catch (RuntimeException e) {
                Log.w(TAG, "Ignoring malformed Bundle for LibraryParams", e);
                return;
            }
        }
        dispatchControllerTaskOnHandler(new bk(i2, str, 0, libraryParamsFromBundle));
    }

    @Override // androidx.media3.session.IMediaController
    public void onSessionActivityChanged(int i, @rq6 PendingIntent pendingIntent) throws RemoteException {
        dispatchControllerTaskOnHandler(new bj(i, pendingIntent, 3));
    }

    @Override // androidx.media3.session.IMediaController
    public void onSessionResult(int i, @rq6 Bundle bundle) {
        if (bundle == null) {
            return;
        }
        try {
            setControllerFutureResult(i, SessionResult.fromBundle(bundle));
        } catch (RuntimeException e) {
            Log.w(TAG, "Ignoring malformed Bundle for SessionResult", e);
        }
    }

    @Override // androidx.media3.session.IMediaController
    public void onSetCustomLayout(int i, @rq6 List<Bundle> list) {
        if (list == null) {
            return;
        }
        try {
            int sessionInterfaceVersion = getSessionInterfaceVersion();
            if (sessionInterfaceVersion == -1) {
                return;
            }
            dispatchControllerTaskOnHandler(new bj(i, BundleCollectionUtil.fromBundleList(new f(sessionInterfaceVersion, 4), list), 0));
        } catch (RuntimeException e) {
            Log.w(TAG, "Ignoring malformed Bundle for CommandButton", e);
        }
    }

    @Override // androidx.media3.session.IMediaController
    public void onSetMediaButtonPreferences(int i, @rq6 List<Bundle> list) {
        if (list == null) {
            return;
        }
        try {
            int sessionInterfaceVersion = getSessionInterfaceVersion();
            if (sessionInterfaceVersion == -1) {
                return;
            }
            dispatchControllerTaskOnHandler(new bj(i, BundleCollectionUtil.fromBundleList(new f(sessionInterfaceVersion, 3), list), 1));
        } catch (RuntimeException e) {
            Log.w(TAG, "Ignoring malformed Bundle for CommandButton", e);
        }
    }

    @Override // androidx.media3.session.IMediaController
    public void onSurfaceSizeChanged(int i, int i2, int i3) {
        dispatchControllerTaskOnHandler(new s(i2, i3, 1));
    }
}
