package androidx.media3.session;

import android.os.Bundle;
import android.os.Parcelable;
import androidx.media3.common.util.Log;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.common.util.Util;
import defpackage.ck4;
import defpackage.hs0;
import defpackage.if7;
import defpackage.p7a;
import defpackage.rq6;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/* JADX INFO: loaded from: classes.dex */
public final class SessionCommands {
    public static final SessionCommands EMPTY = new Builder().build();
    private static final String FIELD_SESSION_COMMANDS = Util.intToStringMaxRadix(0);
    private static final String TAG = "SessionCommands";
    public final ck4<SessionCommand> commands;

    public static final class Builder {
        private final Set<SessionCommand> commands;

        private void addCommandCodes(List<Integer> list) {
            for (int i = 0; i < list.size(); i++) {
                add(new SessionCommand(list.get(i).intValue()));
            }
        }

        @hs0
        public Builder add(SessionCommand sessionCommand) {
            Set<SessionCommand> set = this.commands;
            sessionCommand.getClass();
            set.add(sessionCommand);
            return this;
        }

        @hs0
        public Builder addAllLibraryCommands() {
            addCommandCodes(SessionCommand.LIBRARY_COMMANDS);
            return this;
        }

        @hs0
        public Builder addAllPredefinedCommands() {
            addAllSessionCommands();
            addAllLibraryCommands();
            return this;
        }

        @hs0
        public Builder addAllSessionCommands() {
            addCommandCodes(SessionCommand.SESSION_COMMANDS);
            return this;
        }

        @hs0
        public Builder addSessionCommands(Collection<SessionCommand> collection) {
            this.commands.addAll(collection);
            return this;
        }

        public SessionCommands build() {
            return new SessionCommands(this.commands);
        }

        @hs0
        public Builder remove(SessionCommand sessionCommand) {
            Set<SessionCommand> set = this.commands;
            sessionCommand.getClass();
            set.remove(sessionCommand);
            return this;
        }

        public Builder() {
            this.commands = new HashSet();
        }

        private Builder(SessionCommands sessionCommands) {
            sessionCommands.getClass();
            this.commands = new HashSet(sessionCommands.commands);
        }

        @hs0
        public Builder add(int i) {
            if7.e(i != 0);
            this.commands.add(new SessionCommand(i));
            return this;
        }

        @hs0
        public Builder remove(int i) {
            if7.e(i != 0);
            Iterator<SessionCommand> it = this.commands.iterator();
            while (true) {
                if (!it.hasNext()) {
                    break;
                }
                SessionCommand next = it.next();
                if (next.commandCode == i) {
                    this.commands.remove(next);
                    break;
                }
            }
            return this;
        }
    }

    private static boolean containsCommandCode(Collection<SessionCommand> collection, int i) {
        Iterator<SessionCommand> it = collection.iterator();
        while (it.hasNext()) {
            if (it.next().commandCode == i) {
                return true;
            }
        }
        return false;
    }

    @UnstableApi
    public static SessionCommands fromBundle(Bundle bundle) {
        ArrayList parcelableArrayList = bundle.getParcelableArrayList(FIELD_SESSION_COMMANDS);
        if (parcelableArrayList == null) {
            Log.w(TAG, "Missing commands. Creating an empty SessionCommands");
            return EMPTY;
        }
        Builder builder = new Builder();
        for (int i = 0; i < parcelableArrayList.size(); i++) {
            builder.add(SessionCommand.fromBundle((Bundle) parcelableArrayList.get(i)));
        }
        return builder.build();
    }

    public Builder buildUpon() {
        return new Builder();
    }

    public boolean contains(SessionCommand sessionCommand) {
        ck4<SessionCommand> ck4Var = this.commands;
        sessionCommand.getClass();
        return ck4Var.contains(sessionCommand);
    }

    public boolean equals(@rq6 Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof SessionCommands) {
            return this.commands.equals(((SessionCommands) obj).commands);
        }
        return false;
    }

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

    /* JADX WARN: Multi-variable type inference failed */
    @UnstableApi
    public Bundle toBundle() {
        Bundle bundle = new Bundle();
        ArrayList<? extends Parcelable> arrayList = new ArrayList<>();
        p7a it = this.commands.iterator();
        while (it.hasNext()) {
            arrayList.add(((SessionCommand) it.next()).toBundle());
        }
        bundle.putParcelableArrayList(FIELD_SESSION_COMMANDS, arrayList);
        return bundle;
    }

    private SessionCommands(Collection<SessionCommand> collection) {
        this.commands = ck4.k(collection);
    }

    public boolean contains(int i) {
        if7.f(i != 0, "Use contains(Command) for custom command");
        return containsCommandCode(this.commands, i);
    }
}
