package androidx.savedstate;

import android.os.Bundle;
import androidx.lifecycle.g;
import androidx.lifecycle.i;
import androidx.lifecycle.k;
import g0.c;
import g0.e;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
import w1.g;

/* JADX INFO: compiled from: Recreator.kt */
/* JADX INFO: loaded from: classes.dex */
public final class Recreator implements i {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private final e f3014a;

    /* JADX INFO: compiled from: Recreator.kt */
    public static final class a {
        private a() {
        }

        public /* synthetic */ a(w1.e eVar) {
            this();
        }
    }

    /* JADX INFO: compiled from: Recreator.kt */
    public static final class b implements c.InterfaceC0065c {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        private final Set<String> f3015a;

        public b(c cVar) {
            g.e(cVar, "registry");
            this.f3015a = new LinkedHashSet();
            cVar.h("androidx.savedstate.Restarter", this);
        }

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

        public final void b(String str) {
            g.e(str, "className");
            this.f3015a.add(str);
        }
    }

    static {
        new a(null);
    }

    public Recreator(e eVar) {
        g.e(eVar, "owner");
        this.f3014a = eVar;
    }

    private final void h(String str) {
        try {
            Class<? extends U> clsAsSubclass = Class.forName(str, false, Recreator.class.getClassLoader()).asSubclass(c.a.class);
            g.d(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.d(objNewInstance, "{\n                constr…wInstance()\n            }");
                    ((c.a) objNewInstance).a(this.f3014a);
                } catch (Exception e3) {
                    throw new RuntimeException("Failed to instantiate " + str, e3);
                }
            } catch (NoSuchMethodException e4) {
                throw new IllegalStateException("Class " + clsAsSubclass.getSimpleName() + " must have default constructor in order to be automatically recreated", e4);
            }
        } catch (ClassNotFoundException e5) {
            throw new RuntimeException("Class " + str + " wasn't found", e5);
        }
    }

    @Override // androidx.lifecycle.i
    public void g(k kVar, g.b bVar) {
        w1.g.e(kVar, "source");
        w1.g.e(bVar, "event");
        if (bVar != g.b.ON_CREATE) {
            throw new AssertionError("Next event must be ON_CREATE");
        }
        kVar.a().c(this);
        Bundle bundleB = this.f3014a.d().b("androidx.savedstate.Restarter");
        if (bundleB == null) {
            return;
        }
        ArrayList<String> stringArrayList = bundleB.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\"");
        }
        Iterator<String> it = stringArrayList.iterator();
        while (it.hasNext()) {
            h(it.next());
        }
    }
}
