package androidx.activity.result;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import androidx.lifecycle.g;
import androidx.lifecycle.i;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;

/* JADX INFO: compiled from: ActivityResultRegistry.java */
/* JADX INFO: loaded from: classes.dex */
public abstract class d {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private Random f71a = new Random();

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private final Map<Integer, String> f72b = new HashMap();

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    final Map<String, Integer> f73c = new HashMap();

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private final Map<String, c> f74d = new HashMap();

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    ArrayList<String> f75e = new ArrayList<>();

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    final transient Map<String, b<?>> f76f = new HashMap();

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    final Map<String, Object> f77g = new HashMap();

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    final Bundle f78h = new Bundle();

    /* JADX INFO: Add missing generic type declarations: [I] */
    /* JADX INFO: compiled from: ActivityResultRegistry.java */
    class a<I> extends androidx.activity.result.c<I> {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        final /* synthetic */ String f79a;

        a(String str, b.a aVar) {
            this.f79a = str;
        }

        @Override // androidx.activity.result.c
        public void a() {
            d.this.i(this.f79a);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* JADX INFO: compiled from: ActivityResultRegistry.java */
    static class b<O> {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        final androidx.activity.result.b<O> f81a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        final b.a<?, O> f82b;

        b(androidx.activity.result.b<O> bVar, b.a<?, O> aVar) {
            this.f81a = bVar;
            this.f82b = aVar;
        }
    }

    /* JADX INFO: compiled from: ActivityResultRegistry.java */
    private static class c {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        final g f83a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        private final ArrayList<i> f84b;

        void a() {
            Iterator<i> it = this.f84b.iterator();
            while (it.hasNext()) {
                this.f83a.c(it.next());
            }
            this.f84b.clear();
        }
    }

    private void a(int i3, String str) {
        this.f72b.put(Integer.valueOf(i3), str);
        this.f73c.put(str, Integer.valueOf(i3));
    }

    private <O> void c(String str, int i3, Intent intent, b<O> bVar) {
        if (bVar == null || bVar.f81a == null || !this.f75e.contains(str)) {
            this.f77g.remove(str);
            this.f78h.putParcelable(str, new androidx.activity.result.a(i3, intent));
        } else {
            bVar.f81a.a(bVar.f82b.a(i3, intent));
            this.f75e.remove(str);
        }
    }

    private int d() {
        int iNextInt = this.f71a.nextInt(2147418112);
        while (true) {
            int i3 = iNextInt + 65536;
            if (!this.f72b.containsKey(Integer.valueOf(i3))) {
                return i3;
            }
            iNextInt = this.f71a.nextInt(2147418112);
        }
    }

    private void h(String str) {
        if (this.f73c.get(str) != null) {
            return;
        }
        a(d(), str);
    }

    public final boolean b(int i3, int i4, Intent intent) {
        String str = this.f72b.get(Integer.valueOf(i3));
        if (str == null) {
            return false;
        }
        c(str, i4, intent, this.f76f.get(str));
        return true;
    }

    public final void e(Bundle bundle) {
        if (bundle == null) {
            return;
        }
        ArrayList<Integer> integerArrayList = bundle.getIntegerArrayList("KEY_COMPONENT_ACTIVITY_REGISTERED_RCS");
        ArrayList<String> stringArrayList = bundle.getStringArrayList("KEY_COMPONENT_ACTIVITY_REGISTERED_KEYS");
        if (stringArrayList == null || integerArrayList == null) {
            return;
        }
        this.f75e = bundle.getStringArrayList("KEY_COMPONENT_ACTIVITY_LAUNCHED_KEYS");
        this.f71a = (Random) bundle.getSerializable("KEY_COMPONENT_ACTIVITY_RANDOM_OBJECT");
        this.f78h.putAll(bundle.getBundle("KEY_COMPONENT_ACTIVITY_PENDING_RESULT"));
        for (int i3 = 0; i3 < stringArrayList.size(); i3++) {
            String str = stringArrayList.get(i3);
            if (this.f73c.containsKey(str)) {
                Integer numRemove = this.f73c.remove(str);
                if (!this.f78h.containsKey(str)) {
                    this.f72b.remove(numRemove);
                }
            }
            a(integerArrayList.get(i3).intValue(), stringArrayList.get(i3));
        }
    }

    public final void f(Bundle bundle) {
        bundle.putIntegerArrayList("KEY_COMPONENT_ACTIVITY_REGISTERED_RCS", new ArrayList<>(this.f73c.values()));
        bundle.putStringArrayList("KEY_COMPONENT_ACTIVITY_REGISTERED_KEYS", new ArrayList<>(this.f73c.keySet()));
        bundle.putStringArrayList("KEY_COMPONENT_ACTIVITY_LAUNCHED_KEYS", new ArrayList<>(this.f75e));
        bundle.putBundle("KEY_COMPONENT_ACTIVITY_PENDING_RESULT", (Bundle) this.f78h.clone());
        bundle.putSerializable("KEY_COMPONENT_ACTIVITY_RANDOM_OBJECT", this.f71a);
    }

    /* JADX WARN: Multi-variable type inference failed */
    public final <I, O> androidx.activity.result.c<I> g(String str, b.a<I, O> aVar, androidx.activity.result.b<O> bVar) {
        h(str);
        this.f76f.put(str, new b<>(bVar, aVar));
        if (this.f77g.containsKey(str)) {
            Object obj = this.f77g.get(str);
            this.f77g.remove(str);
            bVar.a(obj);
        }
        androidx.activity.result.a aVar2 = (androidx.activity.result.a) this.f78h.getParcelable(str);
        if (aVar2 != null) {
            this.f78h.remove(str);
            bVar.a(aVar.a(aVar2.k(), aVar2.j()));
        }
        return new a(str, aVar);
    }

    final void i(String str) {
        Integer numRemove;
        if (!this.f75e.contains(str) && (numRemove = this.f73c.remove(str)) != null) {
            this.f72b.remove(numRemove);
        }
        this.f76f.remove(str);
        if (this.f77g.containsKey(str)) {
            Log.w("ActivityResultRegistry", "Dropping pending result for request " + str + ": " + this.f77g.get(str));
            this.f77g.remove(str);
        }
        if (this.f78h.containsKey(str)) {
            Log.w("ActivityResultRegistry", "Dropping pending result for request " + str + ": " + this.f78h.getParcelable(str));
            this.f78h.remove(str);
        }
        c cVar = this.f74d.get(str);
        if (cVar != null) {
            cVar.a();
            this.f74d.remove(str);
        }
    }
}
