package androidx.media3.session;

import android.app.PendingIntent;
import android.media.session.MediaSession;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import androidx.media3.common.Player;
import androidx.media3.common.util.BundleCollectionUtil;
import androidx.media3.common.util.Util;
import androidx.media3.session.IMediaSession;
import defpackage.bj4;
import defpackage.rq6;
import java.util.ArrayList;

/* JADX INFO: loaded from: classes.dex */
class ConnectionState {
    public final bj4<CommandButton> commandButtonsForMediaItems;
    public final bj4<CommandButton> customLayout;
    public final int libraryVersion;
    public final bj4<CommandButton> mediaButtonPreferences;

    @rq6
    public final MediaSession.Token platformToken;
    public final Player.Commands playerCommandsFromPlayer;
    public final Player.Commands playerCommandsFromSession;
    public final PlayerInfo playerInfo;

    @rq6
    public final PendingIntent sessionActivity;
    public final IMediaSession sessionBinder;
    public final SessionCommands sessionCommands;
    public final Bundle sessionExtras;
    public final int sessionInterfaceVersion;
    public final Bundle tokenExtras;
    private static final String FIELD_LIBRARY_VERSION = Util.intToStringMaxRadix(0);
    private static final String FIELD_SESSION_BINDER = Util.intToStringMaxRadix(1);
    private static final String FIELD_SESSION_ACTIVITY = Util.intToStringMaxRadix(2);
    private static final String FIELD_CUSTOM_LAYOUT = Util.intToStringMaxRadix(9);
    private static final String FIELD_MEDIA_BUTTON_PREFERENCES = Util.intToStringMaxRadix(14);
    private static final String FIELD_COMMAND_BUTTONS_FOR_MEDIA_ITEMS = Util.intToStringMaxRadix(13);
    private static final String FIELD_SESSION_COMMANDS = Util.intToStringMaxRadix(3);
    private static final String FIELD_PLAYER_COMMANDS_FROM_SESSION = Util.intToStringMaxRadix(4);
    private static final String FIELD_PLAYER_COMMANDS_FROM_PLAYER = Util.intToStringMaxRadix(5);
    private static final String FIELD_TOKEN_EXTRAS = Util.intToStringMaxRadix(6);
    private static final String FIELD_SESSION_EXTRAS = Util.intToStringMaxRadix(11);
    private static final String FIELD_PLAYER_INFO = Util.intToStringMaxRadix(7);
    private static final String FIELD_SESSION_INTERFACE_VERSION = Util.intToStringMaxRadix(8);
    private static final String FIELD_IN_PROCESS_BINDER = Util.intToStringMaxRadix(10);
    private static final String FIELD_PLATFORM_TOKEN = Util.intToStringMaxRadix(12);

    public final class InProcessBinder extends Binder {
        private InProcessBinder() {
        }

        public ConnectionState getConnectionState() {
            return ConnectionState.this;
        }
    }

    public ConnectionState(int i, int i2, IMediaSession iMediaSession, @rq6 PendingIntent pendingIntent, bj4<CommandButton> bj4Var, bj4<CommandButton> bj4Var2, bj4<CommandButton> bj4Var3, SessionCommands sessionCommands, Player.Commands commands, Player.Commands commands2, Bundle bundle, Bundle bundle2, PlayerInfo playerInfo, @rq6 MediaSession.Token token) {
        this.libraryVersion = i;
        this.sessionInterfaceVersion = i2;
        this.sessionBinder = iMediaSession;
        this.sessionActivity = pendingIntent;
        this.customLayout = bj4Var;
        this.mediaButtonPreferences = bj4Var2;
        this.commandButtonsForMediaItems = bj4Var3;
        this.sessionCommands = sessionCommands;
        this.playerCommandsFromSession = commands;
        this.playerCommandsFromPlayer = commands2;
        this.tokenExtras = bundle;
        this.sessionExtras = bundle2;
        this.playerInfo = playerInfo;
        this.platformToken = token;
    }

    public static ConnectionState fromBundle(Bundle bundle) {
        IBinder binder = bundle.getBinder(FIELD_IN_PROCESS_BINDER);
        if (binder instanceof InProcessBinder) {
            return ((InProcessBinder) binder).getConnectionState();
        }
        int i = bundle.getInt(FIELD_LIBRARY_VERSION, 0);
        int i2 = bundle.getInt(FIELD_SESSION_INTERFACE_VERSION, 0);
        IBinder binder2 = bundle.getBinder(FIELD_SESSION_BINDER);
        binder2.getClass();
        IBinder iBinder = binder2;
        PendingIntent pendingIntent = (PendingIntent) bundle.getParcelable(FIELD_SESSION_ACTIVITY);
        ArrayList parcelableArrayList = bundle.getParcelableArrayList(FIELD_CUSTOM_LAYOUT);
        bj4 bj4VarFromBundleList = parcelableArrayList != null ? BundleCollectionUtil.fromBundleList(new f(i2, 0), parcelableArrayList) : bj4.p();
        ArrayList parcelableArrayList2 = bundle.getParcelableArrayList(FIELD_MEDIA_BUTTON_PREFERENCES);
        bj4 bj4VarFromBundleList2 = parcelableArrayList2 != null ? BundleCollectionUtil.fromBundleList(new f(i2, 1), parcelableArrayList2) : bj4.p();
        ArrayList parcelableArrayList3 = bundle.getParcelableArrayList(FIELD_COMMAND_BUTTONS_FOR_MEDIA_ITEMS);
        bj4 bj4VarFromBundleList3 = parcelableArrayList3 != null ? BundleCollectionUtil.fromBundleList(new f(i2, 2), parcelableArrayList3) : bj4.p();
        Bundle bundle2 = bundle.getBundle(FIELD_SESSION_COMMANDS);
        SessionCommands sessionCommandsFromBundle = bundle2 == null ? SessionCommands.EMPTY : SessionCommands.fromBundle(bundle2);
        Bundle bundle3 = bundle.getBundle(FIELD_PLAYER_COMMANDS_FROM_PLAYER);
        Player.Commands commandsFromBundle = bundle3 == null ? Player.Commands.EMPTY : Player.Commands.fromBundle(bundle3);
        Bundle bundle4 = bundle.getBundle(FIELD_PLAYER_COMMANDS_FROM_SESSION);
        Player.Commands commandsFromBundle2 = bundle4 == null ? Player.Commands.EMPTY : Player.Commands.fromBundle(bundle4);
        Bundle bundleConvertToNullIfInvalid = Util.convertToNullIfInvalid(bundle.getBundle(FIELD_TOKEN_EXTRAS));
        Bundle bundleConvertToNullIfInvalid2 = Util.convertToNullIfInvalid(bundle.getBundle(FIELD_SESSION_EXTRAS));
        Bundle bundle5 = bundle.getBundle(FIELD_PLAYER_INFO);
        PlayerInfo playerInfoFromBundle = bundle5 == null ? PlayerInfo.DEFAULT : PlayerInfo.fromBundle(bundle5, i2);
        MediaSession.Token token = (MediaSession.Token) bundle.getParcelable(FIELD_PLATFORM_TOKEN);
        Bundle bundle6 = bundleConvertToNullIfInvalid2;
        IMediaSession iMediaSessionAsInterface = IMediaSession.Stub.asInterface(iBinder);
        if (bundleConvertToNullIfInvalid == null) {
            bundleConvertToNullIfInvalid = Bundle.EMPTY;
        }
        Bundle bundle7 = bundleConvertToNullIfInvalid;
        if (bundle6 == null) {
            bundle6 = Bundle.EMPTY;
        }
        return new ConnectionState(i, i2, iMediaSessionAsInterface, pendingIntent, bj4VarFromBundleList, bj4VarFromBundleList2, bj4VarFromBundleList3, sessionCommandsFromBundle, commandsFromBundle2, commandsFromBundle, bundle7, bundle6, playerInfoFromBundle, token);
    }

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

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

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

    public Bundle toBundleForRemoteProcess(int i) {
        Bundle bundle = new Bundle();
        bundle.putInt(FIELD_LIBRARY_VERSION, this.libraryVersion);
        bundle.putBinder(FIELD_SESSION_BINDER, this.sessionBinder.asBinder());
        bundle.putParcelable(FIELD_SESSION_ACTIVITY, this.sessionActivity);
        if (!this.customLayout.isEmpty()) {
            bundle.putParcelableArrayList(FIELD_CUSTOM_LAYOUT, BundleCollectionUtil.toBundleArrayList(this.customLayout, new defpackage.av(9)));
        }
        if (!this.mediaButtonPreferences.isEmpty()) {
            if (i >= 7) {
                bundle.putParcelableArrayList(FIELD_MEDIA_BUTTON_PREFERENCES, BundleCollectionUtil.toBundleArrayList(this.mediaButtonPreferences, new defpackage.av(9)));
            } else {
                bundle.putParcelableArrayList(FIELD_CUSTOM_LAYOUT, BundleCollectionUtil.toBundleArrayList(CommandButton.getCustomLayoutFromMediaButtonPreferences(this.mediaButtonPreferences, true, true), new defpackage.av(9)));
            }
        }
        if (!this.commandButtonsForMediaItems.isEmpty()) {
            bundle.putParcelableArrayList(FIELD_COMMAND_BUTTONS_FOR_MEDIA_ITEMS, BundleCollectionUtil.toBundleArrayList(this.commandButtonsForMediaItems, new defpackage.av(9)));
        }
        bundle.putBundle(FIELD_SESSION_COMMANDS, this.sessionCommands.toBundle());
        bundle.putBundle(FIELD_PLAYER_COMMANDS_FROM_SESSION, this.playerCommandsFromSession.toBundle());
        bundle.putBundle(FIELD_PLAYER_COMMANDS_FROM_PLAYER, this.playerCommandsFromPlayer.toBundle());
        bundle.putBundle(FIELD_TOKEN_EXTRAS, this.tokenExtras);
        bundle.putBundle(FIELD_SESSION_EXTRAS, this.sessionExtras);
        bundle.putBundle(FIELD_PLAYER_INFO, this.playerInfo.filterByAvailableCommands(MediaUtils.intersect(this.playerCommandsFromSession, this.playerCommandsFromPlayer), false, false).toBundleForRemoteProcess(i));
        bundle.putInt(FIELD_SESSION_INTERFACE_VERSION, this.sessionInterfaceVersion);
        MediaSession.Token token = this.platformToken;
        if (token != null) {
            bundle.putParcelable(FIELD_PLATFORM_TOKEN, token);
        }
        return bundle;
    }

    public Bundle toBundleInProcess() {
        Bundle bundle = new Bundle();
        bundle.putBinder(FIELD_IN_PROCESS_BINDER, new InProcessBinder());
        return bundle;
    }
}
