package androidx.activity.result; import android.content.Intent; import android.os.Bundle; import android.util.Log; import androidx.lifecycle.LifecycleOwner; import androidx.lifecycle.e; import androidx.lifecycle.g; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Random; /* loaded from: classes.dex */ public abstract class ActivityResultRegistry { /* renamed from: a, reason: collision with root package name */ private Random f256a = new Random(); /* renamed from: b, reason: collision with root package name */ private final Map f257b = new HashMap(); /* renamed from: c, reason: collision with root package name */ final Map f258c = new HashMap(); /* renamed from: d, reason: collision with root package name */ private final Map f259d = new HashMap(); /* renamed from: e, reason: collision with root package name */ ArrayList f260e = new ArrayList(); /* renamed from: f, reason: collision with root package name */ final transient Map f261f = new HashMap(); /* renamed from: g, reason: collision with root package name */ final Map f262g = new HashMap(); /* renamed from: h, reason: collision with root package name */ final Bundle f263h = new Bundle(); class a extends androidx.activity.result.c { /* renamed from: a, reason: collision with root package name */ final /* synthetic */ String f268a; /* renamed from: b, reason: collision with root package name */ final /* synthetic */ int f269b; /* renamed from: c, reason: collision with root package name */ final /* synthetic */ c.a f270c; a(String str, int i10, c.a aVar) { this.f268a = str; this.f269b = i10; this.f270c = aVar; } @Override // androidx.activity.result.c public void b(Object obj, androidx.core.app.d dVar) { ActivityResultRegistry.this.f260e.add(this.f268a); Integer num = (Integer) ActivityResultRegistry.this.f258c.get(this.f268a); ActivityResultRegistry.this.f(num != null ? num.intValue() : this.f269b, this.f270c, obj, dVar); } @Override // androidx.activity.result.c public void c() { ActivityResultRegistry.this.l(this.f268a); } } class b extends androidx.activity.result.c { /* renamed from: a, reason: collision with root package name */ final /* synthetic */ String f272a; /* renamed from: b, reason: collision with root package name */ final /* synthetic */ int f273b; /* renamed from: c, reason: collision with root package name */ final /* synthetic */ c.a f274c; b(String str, int i10, c.a aVar) { this.f272a = str; this.f273b = i10; this.f274c = aVar; } @Override // androidx.activity.result.c public void b(Object obj, androidx.core.app.d dVar) { ActivityResultRegistry.this.f260e.add(this.f272a); Integer num = (Integer) ActivityResultRegistry.this.f258c.get(this.f272a); ActivityResultRegistry.this.f(num != null ? num.intValue() : this.f273b, this.f274c, obj, dVar); } @Override // androidx.activity.result.c public void c() { ActivityResultRegistry.this.l(this.f272a); } } private static class c { /* renamed from: a, reason: collision with root package name */ final androidx.activity.result.b f276a; /* renamed from: b, reason: collision with root package name */ final c.a f277b; c(androidx.activity.result.b bVar, c.a aVar) { this.f276a = bVar; this.f277b = aVar; } } private static class d { /* renamed from: a, reason: collision with root package name */ final androidx.lifecycle.e f278a; /* renamed from: b, reason: collision with root package name */ private final ArrayList f279b = new ArrayList(); d(androidx.lifecycle.e eVar) { this.f278a = eVar; } void a(g gVar) { this.f278a.a(gVar); this.f279b.add(gVar); } void b() { Iterator it = this.f279b.iterator(); while (it.hasNext()) { this.f278a.c((g) it.next()); } this.f279b.clear(); } } private void a(int i10, String str) { this.f257b.put(Integer.valueOf(i10), str); this.f258c.put(str, Integer.valueOf(i10)); } private void d(String str, int i10, Intent intent, c cVar) { androidx.activity.result.b bVar; if (cVar != null && (bVar = cVar.f276a) != null) { bVar.a(cVar.f277b.c(i10, intent)); } else { this.f262g.remove(str); this.f263h.putParcelable(str, new androidx.activity.result.a(i10, intent)); } } private int e() { int nextInt = this.f256a.nextInt(2147418112); while (true) { int i10 = nextInt + 65536; if (!this.f257b.containsKey(Integer.valueOf(i10))) { return i10; } nextInt = this.f256a.nextInt(2147418112); } } private int k(String str) { Integer num = (Integer) this.f258c.get(str); if (num != null) { return num.intValue(); } int e10 = e(); a(e10, str); return e10; } public final boolean b(int i10, int i11, Intent intent) { String str = (String) this.f257b.get(Integer.valueOf(i10)); if (str == null) { return false; } this.f260e.remove(str); d(str, i11, intent, (c) this.f261f.get(str)); return true; } public final boolean c(int i10, Object obj) { androidx.activity.result.b bVar; String str = (String) this.f257b.get(Integer.valueOf(i10)); if (str == null) { return false; } this.f260e.remove(str); c cVar = (c) this.f261f.get(str); if (cVar != null && (bVar = cVar.f276a) != null) { bVar.a(obj); return true; } this.f263h.remove(str); this.f262g.put(str, obj); return true; } public abstract void f(int i10, c.a aVar, Object obj, androidx.core.app.d dVar); public final void g(Bundle bundle) { if (bundle == null) { return; } ArrayList integerArrayList = bundle.getIntegerArrayList("KEY_COMPONENT_ACTIVITY_REGISTERED_RCS"); ArrayList stringArrayList = bundle.getStringArrayList("KEY_COMPONENT_ACTIVITY_REGISTERED_KEYS"); if (stringArrayList == null || integerArrayList == null) { return; } this.f260e = bundle.getStringArrayList("KEY_COMPONENT_ACTIVITY_LAUNCHED_KEYS"); this.f256a = (Random) bundle.getSerializable("KEY_COMPONENT_ACTIVITY_RANDOM_OBJECT"); this.f263h.putAll(bundle.getBundle("KEY_COMPONENT_ACTIVITY_PENDING_RESULT")); for (int i10 = 0; i10 < stringArrayList.size(); i10++) { String str = stringArrayList.get(i10); if (this.f258c.containsKey(str)) { Integer num = (Integer) this.f258c.remove(str); if (!this.f263h.containsKey(str)) { this.f257b.remove(num); } } a(integerArrayList.get(i10).intValue(), stringArrayList.get(i10)); } } public final void h(Bundle bundle) { bundle.putIntegerArrayList("KEY_COMPONENT_ACTIVITY_REGISTERED_RCS", new ArrayList<>(this.f258c.values())); bundle.putStringArrayList("KEY_COMPONENT_ACTIVITY_REGISTERED_KEYS", new ArrayList<>(this.f258c.keySet())); bundle.putStringArrayList("KEY_COMPONENT_ACTIVITY_LAUNCHED_KEYS", new ArrayList<>(this.f260e)); bundle.putBundle("KEY_COMPONENT_ACTIVITY_PENDING_RESULT", (Bundle) this.f263h.clone()); bundle.putSerializable("KEY_COMPONENT_ACTIVITY_RANDOM_OBJECT", this.f256a); } public final androidx.activity.result.c i(final String str, LifecycleOwner lifecycleOwner, final c.a aVar, final androidx.activity.result.b bVar) { androidx.lifecycle.e lifecycle = lifecycleOwner.getLifecycle(); if (lifecycle.b().a(e.c.STARTED)) { throw new IllegalStateException("LifecycleOwner " + lifecycleOwner + " is attempting to register while current state is " + lifecycle.b() + ". LifecycleOwners must call register before they are STARTED."); } int k10 = k(str); d dVar = (d) this.f259d.get(str); if (dVar == null) { dVar = new d(lifecycle); } dVar.a(new g() { // from class: androidx.activity.result.ActivityResultRegistry.1 @Override // androidx.lifecycle.g public void onStateChanged(LifecycleOwner lifecycleOwner2, e.b bVar2) { if (!e.b.ON_START.equals(bVar2)) { if (e.b.ON_STOP.equals(bVar2)) { ActivityResultRegistry.this.f261f.remove(str); return; } else { if (e.b.ON_DESTROY.equals(bVar2)) { ActivityResultRegistry.this.l(str); return; } return; } } ActivityResultRegistry.this.f261f.put(str, new c(bVar, aVar)); if (ActivityResultRegistry.this.f262g.containsKey(str)) { Object obj = ActivityResultRegistry.this.f262g.get(str); ActivityResultRegistry.this.f262g.remove(str); bVar.a(obj); } androidx.activity.result.a aVar2 = (androidx.activity.result.a) ActivityResultRegistry.this.f263h.getParcelable(str); if (aVar2 != null) { ActivityResultRegistry.this.f263h.remove(str); bVar.a(aVar.c(aVar2.b(), aVar2.a())); } } }); this.f259d.put(str, dVar); return new a(str, k10, aVar); } public final androidx.activity.result.c j(String str, c.a aVar, androidx.activity.result.b bVar) { int k10 = k(str); this.f261f.put(str, new c(bVar, aVar)); if (this.f262g.containsKey(str)) { Object obj = this.f262g.get(str); this.f262g.remove(str); bVar.a(obj); } androidx.activity.result.a aVar2 = (androidx.activity.result.a) this.f263h.getParcelable(str); if (aVar2 != null) { this.f263h.remove(str); bVar.a(aVar.c(aVar2.b(), aVar2.a())); } return new b(str, k10, aVar); } final void l(String str) { Integer num; if (!this.f260e.contains(str) && (num = (Integer) this.f258c.remove(str)) != null) { this.f257b.remove(num); } this.f261f.remove(str); if (this.f262g.containsKey(str)) { Log.w("ActivityResultRegistry", "Dropping pending result for request " + str + ": " + this.f262g.get(str)); this.f262g.remove(str); } if (this.f263h.containsKey(str)) { Log.w("ActivityResultRegistry", "Dropping pending result for request " + str + ": " + this.f263h.getParcelable(str)); this.f263h.remove(str); } d dVar = (d) this.f259d.get(str); if (dVar != null) { dVar.b(); this.f259d.remove(str); } } }