package androidx.activity.result;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import androidx.lifecycle.AbstractC0424i;
import androidx.lifecycle.InterfaceC0432k;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import p004b.AbstractC0463a;

/* renamed from: androidx.activity.result.d */
/* loaded from: classes.dex */
public abstract class AbstractC0072d {

    /* renamed from: a */
    public Random f79a = new Random();

    /* renamed from: b */
    public final Map<Integer, String> f80b = new HashMap();

    /* renamed from: c */
    public final Map<String, Integer> f81c = new HashMap();

    /* renamed from: d */
    public final Map<String, C0075c> f82d = new HashMap();

    /* renamed from: e */
    public ArrayList<String> f83e = new ArrayList<>();

    /* renamed from: f */
    public final transient Map<String, C0074b<?>> f84f = new HashMap();

    /* renamed from: g */
    public final Map<String, Object> f85g = new HashMap();

    /* renamed from: h */
    public final Bundle f86h = new Bundle();

    /* JADX INFO: Add missing generic type declarations: [I] */
    /* renamed from: androidx.activity.result.d$a */
    /* loaded from: classes.dex */
    public class C0073a<I> extends AbstractC0071c<I> {

        /* renamed from: a */
        public final /* synthetic */ String f87a;

        /* renamed from: b */
        public final /* synthetic */ AbstractC0463a f88b;

        public C0073a(String str, AbstractC0463a abstractC0463a) {
            this.f87a = str;
            this.f88b = abstractC0463a;
        }

        @Override // androidx.activity.result.AbstractC0071c
        /* renamed from: a */
        public void mo4982a() {
            AbstractC0072d.this.m4983i(this.f87a);
        }
    }

    /* renamed from: androidx.activity.result.d$b */
    /* loaded from: classes.dex */
    public static class C0074b<O> {

        /* renamed from: a */
        public final InterfaceC0070b<O> f90a;

        /* renamed from: b */
        public final AbstractC0463a<?, O> f91b;

        public C0074b(InterfaceC0070b<O> interfaceC0070b, AbstractC0463a<?, O> abstractC0463a) {
            this.f90a = interfaceC0070b;
            this.f91b = abstractC0463a;
        }
    }

    /* renamed from: androidx.activity.result.d$c */
    /* loaded from: classes.dex */
    public static class C0075c {

        /* renamed from: a */
        public final AbstractC0424i f92a;

        /* renamed from: b */
        public final ArrayList<InterfaceC0432k> f93b;

        /* renamed from: a */
        public void m4981a() {
            Iterator<InterfaceC0432k> it = this.f93b.iterator();
            while (it.hasNext()) {
                this.f92a.mo3691c(it.next());
            }
            this.f93b.clear();
        }
    }

    /* renamed from: a */
    public final void m4991a(int i, String str) {
        this.f80b.put(Integer.valueOf(i), str);
        this.f81c.put(str, Integer.valueOf(i));
    }

    /* renamed from: b */
    public final boolean m4990b(int i, int i2, Intent intent) {
        String str = this.f80b.get(Integer.valueOf(i));
        if (str == null) {
            return false;
        }
        m4989c(str, i2, intent, this.f84f.get(str));
        return true;
    }

    /* renamed from: c */
    public final <O> void m4989c(String str, int i, Intent intent, C0074b<O> c0074b) {
        if (c0074b == null || c0074b.f90a == null || !this.f83e.contains(str)) {
            this.f85g.remove(str);
            this.f86h.putParcelable(str, new C0068a(i, intent));
            return;
        }
        c0074b.f90a.mo3936a(c0074b.f91b.mo3634a(i, intent));
        this.f83e.remove(str);
    }

    /* renamed from: d */
    public final int m4988d() {
        int nextInt = this.f79a.nextInt(2147418112);
        while (true) {
            int i = nextInt + 65536;
            if (!this.f80b.containsKey(Integer.valueOf(i))) {
                return i;
            }
            nextInt = this.f79a.nextInt(2147418112);
        }
    }

    /* renamed from: e */
    public final void m4987e(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.f83e = bundle.getStringArrayList("KEY_COMPONENT_ACTIVITY_LAUNCHED_KEYS");
        this.f79a = (Random) bundle.getSerializable("KEY_COMPONENT_ACTIVITY_RANDOM_OBJECT");
        this.f86h.putAll(bundle.getBundle("KEY_COMPONENT_ACTIVITY_PENDING_RESULT"));
        for (int i = 0; i < stringArrayList.size(); i++) {
            String str = stringArrayList.get(i);
            if (this.f81c.containsKey(str)) {
                Integer remove = this.f81c.remove(str);
                if (!this.f86h.containsKey(str)) {
                    this.f80b.remove(remove);
                }
            }
            m4991a(integerArrayList.get(i).intValue(), stringArrayList.get(i));
        }
    }

    /* renamed from: f */
    public final void m4986f(Bundle bundle) {
        bundle.putIntegerArrayList("KEY_COMPONENT_ACTIVITY_REGISTERED_RCS", new ArrayList<>(this.f81c.values()));
        bundle.putStringArrayList("KEY_COMPONENT_ACTIVITY_REGISTERED_KEYS", new ArrayList<>(this.f81c.keySet()));
        bundle.putStringArrayList("KEY_COMPONENT_ACTIVITY_LAUNCHED_KEYS", new ArrayList<>(this.f83e));
        bundle.putBundle("KEY_COMPONENT_ACTIVITY_PENDING_RESULT", (Bundle) this.f86h.clone());
        bundle.putSerializable("KEY_COMPONENT_ACTIVITY_RANDOM_OBJECT", this.f79a);
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* renamed from: g */
    public final <I, O> AbstractC0071c<I> m4985g(String str, AbstractC0463a<I, O> abstractC0463a, InterfaceC0070b<O> interfaceC0070b) {
        m4984h(str);
        this.f84f.put(str, new C0074b<>(interfaceC0070b, abstractC0463a));
        if (this.f85g.containsKey(str)) {
            Object obj = this.f85g.get(str);
            this.f85g.remove(str);
            interfaceC0070b.mo3936a(obj);
        }
        C0068a c0068a = (C0068a) this.f86h.getParcelable(str);
        if (c0068a != null) {
            this.f86h.remove(str);
            interfaceC0070b.mo3936a(abstractC0463a.mo3634a(c0068a.m4995b(), c0068a.m4996a()));
        }
        return new C0073a(str, abstractC0463a);
    }

    /* renamed from: h */
    public final void m4984h(String str) {
        if (this.f81c.get(str) != null) {
            return;
        }
        m4991a(m4988d(), str);
    }

    /* renamed from: i */
    public final void m4983i(String str) {
        Integer remove;
        if (!this.f83e.contains(str) && (remove = this.f81c.remove(str)) != null) {
            this.f80b.remove(remove);
        }
        this.f84f.remove(str);
        if (this.f85g.containsKey(str)) {
            Log.w("ActivityResultRegistry", "Dropping pending result for request " + str + ": " + this.f85g.get(str));
            this.f85g.remove(str);
        }
        if (this.f86h.containsKey(str)) {
            Log.w("ActivityResultRegistry", "Dropping pending result for request " + str + ": " + this.f86h.getParcelable(str));
            this.f86h.remove(str);
        }
        C0075c c0075c = this.f82d.get(str);
        if (c0075c != null) {
            c0075c.m4981a();
            this.f82d.remove(str);
        }
    }
}