package androidx.savedstate;

import android.os.Bundle;
import c.l.j;
import c.l.l;
import c.l.n;
import c.o.b;
import c.o.d;
import g.i;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.Set;

/* JADX INFO: compiled from: Recreator.kt */
/* JADX INFO: loaded from: classes.dex */
@i
public final class Recreator implements l {
    public final d a;

    /* JADX INFO: compiled from: Recreator.kt */
    @i
    public static final class a implements b.InterfaceC0024b {
        public final Set<String> a;

        public a(b bVar) {
            g.h0.c.l.f(bVar, "registry");
            this.a = new LinkedHashSet();
            bVar.c("androidx.savedstate.Restarter", this);
        }

        @Override // c.o.b.InterfaceC0024b
        public Bundle a() {
            Bundle bundle = new Bundle();
            bundle.putStringArrayList("classes_to_restore", new ArrayList<>(this.a));
            return bundle;
        }
    }

    public Recreator(d dVar) {
        g.h0.c.l.f(dVar, "owner");
        this.a = dVar;
    }

    @Override // c.l.l
    public void g(n nVar, j.a aVar) {
        g.h0.c.l.f(nVar, "source");
        g.h0.c.l.f(aVar, "event");
        if (aVar != j.a.ON_CREATE) {
            throw new AssertionError("Next event must be ON_CREATE");
        }
        nVar.getLifecycle().c(this);
        Bundle bundleA = this.a.getSavedStateRegistry().a("androidx.savedstate.Restarter");
        if (bundleA == null) {
            return;
        }
        ArrayList<String> stringArrayList = bundleA.getStringArrayList("classes_to_restore");
        if (stringArrayList == null) {
            throw new IllegalStateException("Bundle with restored state for the component \"androidx.savedstate.Restarter\" must contain list of strings by the key \"classes_to_restore\"");
        }
        for (String str : stringArrayList) {
            try {
                Class<? extends U> clsAsSubclass = Class.forName(str, false, Recreator.class.getClassLoader()).asSubclass(b.a.class);
                g.h0.c.l.e(clsAsSubclass, "{\n                Class.…class.java)\n            }");
                try {
                    Constructor declaredConstructor = clsAsSubclass.getDeclaredConstructor(new Class[0]);
                    declaredConstructor.setAccessible(true);
                    try {
                        Object objNewInstance = declaredConstructor.newInstance(new Object[0]);
                        g.h0.c.l.e(objNewInstance, "{\n                constr…wInstance()\n            }");
                        ((b.a) objNewInstance).a(this.a);
                    } catch (Exception e2) {
                        throw new RuntimeException(d.b.a.a.a.p("Failed to instantiate ", str), e2);
                    }
                } catch (NoSuchMethodException e3) {
                    StringBuilder sbB = d.b.a.a.a.B("Class ");
                    sbB.append(clsAsSubclass.getSimpleName());
                    sbB.append(" must have default constructor in order to be automatically recreated");
                    throw new IllegalStateException(sbB.toString(), e3);
                }
            } catch (ClassNotFoundException e4) {
                throw new RuntimeException(d.b.a.a.a.q("Class ", str, " wasn't found"), e4);
            }
        }
    }
}
