package androidx.media3.common.util;

import android.os.Bundle;
import android.util.SparseArray;
import defpackage.bj4;
import defpackage.fj4;
import defpackage.jr3;
import defpackage.rq6;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/* JADX INFO: loaded from: classes.dex */
@UnstableApi
public final class BundleCollectionUtil {
    private BundleCollectionUtil() {
    }

    public static HashMap<String, String> bundleToStringHashMap(Bundle bundle) {
        HashMap<String, String> map = new HashMap<>();
        if (bundle != Bundle.EMPTY) {
            for (String str : bundle.keySet()) {
                String string = bundle.getString(str);
                if (string != null) {
                    map.put(str, string);
                }
            }
        }
        return map;
    }

    public static fj4<String, String> bundleToStringImmutableMap(Bundle bundle) {
        return bundle == Bundle.EMPTY ? fj4.l() : fj4.c(bundleToStringHashMap(bundle));
    }

    public static void ensureClassLoader(@rq6 Bundle bundle) {
        if (bundle != null) {
            bundle.setClassLoader((ClassLoader) Util.castNonNull(BundleCollectionUtil.class.getClassLoader()));
        }
    }

    public static <T> bj4<T> fromBundleList(jr3<Bundle, T> jr3Var, List<Bundle> list) {
        bj4.a aVarI = bj4.i();
        for (int i = 0; i < list.size(); i++) {
            Bundle bundle = list.get(i);
            bundle.getClass();
            aVarI.h(jr3Var.apply(bundle));
        }
        return aVarI.j();
    }

    public static <T> SparseArray<T> fromBundleSparseArray(jr3<Bundle, T> jr3Var, SparseArray<Bundle> sparseArray) {
        SparseArray<T> sparseArray2 = new SparseArray<>(sparseArray.size());
        for (int i = 0; i < sparseArray.size(); i++) {
            sparseArray2.put(sparseArray.keyAt(i), jr3Var.apply(sparseArray.valueAt(i)));
        }
        return sparseArray2;
    }

    public static Bundle getBundleWithDefault(Bundle bundle, String str, Bundle bundle2) {
        Bundle bundle3 = bundle.getBundle(str);
        return bundle3 != null ? bundle3 : bundle2;
    }

    public static ArrayList<Integer> getIntegerArrayListWithDefault(Bundle bundle, String str, ArrayList<Integer> arrayList) {
        ArrayList<Integer> integerArrayList = bundle.getIntegerArrayList(str);
        return integerArrayList != null ? integerArrayList : arrayList;
    }

    public static Bundle stringMapToBundle(Map<String, String> map) {
        Bundle bundle = new Bundle();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            bundle.putString(entry.getKey(), entry.getValue());
        }
        return bundle;
    }

    public static <T> ArrayList<Bundle> toBundleArrayList(Collection<T> collection, jr3<T, Bundle> jr3Var) {
        ArrayList<Bundle> arrayList = new ArrayList<>(collection.size());
        Iterator<T> it = collection.iterator();
        while (it.hasNext()) {
            arrayList.add(jr3Var.apply(it.next()));
        }
        return arrayList;
    }

    public static <T> bj4<Bundle> toBundleList(List<T> list, jr3<T, Bundle> jr3Var) {
        bj4.a aVarI = bj4.i();
        for (int i = 0; i < list.size(); i++) {
            aVarI.h(jr3Var.apply(list.get(i)));
        }
        return aVarI.j();
    }

    public static <T> SparseArray<Bundle> toBundleSparseArray(SparseArray<T> sparseArray, jr3<T, Bundle> jr3Var) {
        SparseArray<Bundle> sparseArray2 = new SparseArray<>(sparseArray.size());
        for (int i = 0; i < sparseArray.size(); i++) {
            sparseArray2.put(sparseArray.keyAt(i), jr3Var.apply(sparseArray.valueAt(i)));
        }
        return sparseArray2;
    }
}
