package androidx.activity.result;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import c.l.j;
import c.l.l;
import c.l.n;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;

/* JADX INFO: loaded from: classes.dex */
public abstract class ActivityResultRegistry {
    public Random a = new Random();

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

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

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

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

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

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

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

    /* JADX INFO: Add missing generic type declarations: [I] */
    public class a<I> extends c.a.m.b<I> {
        public final /* synthetic */ String a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        public final /* synthetic */ c.a.m.d.a f35b;

        public a(String str, c.a.m.d.a aVar) {
            this.a = str;
            this.f35b = aVar;
        }

        @Override // c.a.m.b
        public void a(I i2, c.f.b.c cVar) throws Exception {
            Integer num = ActivityResultRegistry.this.f26c.get(this.a);
            if (num != null) {
                ActivityResultRegistry.this.f28e.add(this.a);
                try {
                    ActivityResultRegistry.this.b(num.intValue(), this.f35b, i2, cVar);
                    return;
                } catch (Exception e2) {
                    ActivityResultRegistry.this.f28e.remove(this.a);
                    throw e2;
                }
            }
            StringBuilder sbB = d.b.a.a.a.B("Attempting to launch an unregistered ActivityResultLauncher with contract ");
            sbB.append(this.f35b);
            sbB.append(" and input ");
            sbB.append(i2);
            sbB.append(". You must ensure the ActivityResultLauncher is registered before calling launch().");
            throw new IllegalStateException(sbB.toString());
        }

        @Override // c.a.m.b
        public void b() {
            ActivityResultRegistry.this.f(this.a);
        }
    }

    /* JADX INFO: Add missing generic type declarations: [I] */
    public class b<I> extends c.a.m.b<I> {
        public final /* synthetic */ String a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        public final /* synthetic */ c.a.m.d.a f37b;

        public b(String str, c.a.m.d.a aVar) {
            this.a = str;
            this.f37b = aVar;
        }

        @Override // c.a.m.b
        public void a(I i2, c.f.b.c cVar) throws Exception {
            Integer num = ActivityResultRegistry.this.f26c.get(this.a);
            if (num != null) {
                ActivityResultRegistry.this.f28e.add(this.a);
                try {
                    ActivityResultRegistry.this.b(num.intValue(), this.f37b, i2, cVar);
                    return;
                } catch (Exception e2) {
                    ActivityResultRegistry.this.f28e.remove(this.a);
                    throw e2;
                }
            }
            StringBuilder sbB = d.b.a.a.a.B("Attempting to launch an unregistered ActivityResultLauncher with contract ");
            sbB.append(this.f37b);
            sbB.append(" and input ");
            sbB.append(i2);
            sbB.append(". You must ensure the ActivityResultLauncher is registered before calling launch().");
            throw new IllegalStateException(sbB.toString());
        }

        @Override // c.a.m.b
        public void b() {
            ActivityResultRegistry.this.f(this.a);
        }
    }

    public static class c<O> {
        public final c.a.m.a<O> a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        public final c.a.m.d.a<?, O> f39b;

        public c(c.a.m.a<O> aVar, c.a.m.d.a<?, O> aVar2) {
            this.a = aVar;
            this.f39b = aVar2;
        }
    }

    public static class d {
        public final j a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        public final ArrayList<l> f40b = new ArrayList<>();

        public d(j jVar) {
            this.a = jVar;
        }
    }

    public final boolean a(int i2, int i3, Intent intent) {
        String str = this.f25b.get(Integer.valueOf(i2));
        if (str == null) {
            return false;
        }
        c<?> cVar = this.f29f.get(str);
        if (cVar == null || cVar.a == null || !this.f28e.contains(str)) {
            this.f30g.remove(str);
            this.f31h.putParcelable(str, new ActivityResult(i3, intent));
            return true;
        }
        cVar.a.a(cVar.f39b.c(i3, intent));
        this.f28e.remove(str);
        return true;
    }

    public abstract <I, O> void b(int i2, c.a.m.d.a<I, O> aVar, @SuppressLint({"UnknownNullness"}) I i3, c.f.b.c cVar);

    /* JADX WARN: Multi-variable type inference failed */
    public final <I, O> c.a.m.b<I> c(String str, c.a.m.d.a<I, O> aVar, c.a.m.a<O> aVar2) {
        e(str);
        this.f29f.put(str, new c<>(aVar2, aVar));
        if (this.f30g.containsKey(str)) {
            Object obj = this.f30g.get(str);
            this.f30g.remove(str);
            aVar2.a(obj);
        }
        ActivityResult activityResult = (ActivityResult) this.f31h.getParcelable(str);
        if (activityResult != null) {
            this.f31h.remove(str);
            aVar2.a(aVar.c(activityResult.f23b, activityResult.f24e));
        }
        return new b(str, aVar);
    }

    public final <I, O> c.a.m.b<I> d(final String str, n nVar, final c.a.m.d.a<I, O> aVar, final c.a.m.a<O> aVar2) {
        j lifecycle = nVar.getLifecycle();
        if (lifecycle.b().isAtLeast(j.b.STARTED)) {
            throw new IllegalStateException("LifecycleOwner " + nVar + " is attempting to register while current state is " + lifecycle.b() + ". LifecycleOwners must call register before they are STARTED.");
        }
        e(str);
        d dVar = this.f27d.get(str);
        if (dVar == null) {
            dVar = new d(lifecycle);
        }
        l lVar = new l() { // from class: androidx.activity.result.ActivityResultRegistry.1
            @Override // c.l.l
            public void g(n nVar2, j.a aVar3) {
                if (!j.a.ON_START.equals(aVar3)) {
                    if (j.a.ON_STOP.equals(aVar3)) {
                        ActivityResultRegistry.this.f29f.remove(str);
                        return;
                    } else {
                        if (j.a.ON_DESTROY.equals(aVar3)) {
                            ActivityResultRegistry.this.f(str);
                            return;
                        }
                        return;
                    }
                }
                ActivityResultRegistry.this.f29f.put(str, new c<>(aVar2, aVar));
                if (ActivityResultRegistry.this.f30g.containsKey(str)) {
                    Object obj = ActivityResultRegistry.this.f30g.get(str);
                    ActivityResultRegistry.this.f30g.remove(str);
                    aVar2.a(obj);
                }
                ActivityResult activityResult = (ActivityResult) ActivityResultRegistry.this.f31h.getParcelable(str);
                if (activityResult != null) {
                    ActivityResultRegistry.this.f31h.remove(str);
                    aVar2.a(aVar.c(activityResult.f23b, activityResult.f24e));
                }
            }
        };
        dVar.a.a(lVar);
        dVar.f40b.add(lVar);
        this.f27d.put(str, dVar);
        return new a(str, aVar);
    }

    public final void e(String str) {
        if (this.f26c.get(str) != null) {
            return;
        }
        int iNextInt = this.a.nextInt(2147418112);
        while (true) {
            int i2 = iNextInt + 65536;
            if (!this.f25b.containsKey(Integer.valueOf(i2))) {
                this.f25b.put(Integer.valueOf(i2), str);
                this.f26c.put(str, Integer.valueOf(i2));
                return;
            }
            iNextInt = this.a.nextInt(2147418112);
        }
    }

    public final void f(String str) {
        Integer numRemove;
        if (!this.f28e.contains(str) && (numRemove = this.f26c.remove(str)) != null) {
            this.f25b.remove(numRemove);
        }
        this.f29f.remove(str);
        if (this.f30g.containsKey(str)) {
            StringBuilder sbE = d.b.a.a.a.E("Dropping pending result for request ", str, ": ");
            sbE.append(this.f30g.get(str));
            Log.w("ActivityResultRegistry", sbE.toString());
            this.f30g.remove(str);
        }
        if (this.f31h.containsKey(str)) {
            StringBuilder sbE2 = d.b.a.a.a.E("Dropping pending result for request ", str, ": ");
            sbE2.append(this.f31h.getParcelable(str));
            Log.w("ActivityResultRegistry", sbE2.toString());
            this.f31h.remove(str);
        }
        d dVar = this.f27d.get(str);
        if (dVar != null) {
            Iterator<l> it = dVar.f40b.iterator();
            while (it.hasNext()) {
                dVar.a.c(it.next());
            }
            dVar.f40b.clear();
            this.f27d.remove(str);
        }
    }
}
