package androidx.savedstate;

import android.annotation.SuppressLint;
import android.os.Bundle;
import b.j.d;
import b.j.e;
import b.j.g;
import b.j.h;
import b.l.a;
import b.l.c;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

/* JADX INFO: loaded from: classes.dex */
@SuppressLint({"RestrictedApi"})
public final class Recreator implements e {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final c f190a;

    public static final class a implements a.b {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        public final Set<String> f191a = new HashSet();

        public a(b.l.a aVar) {
            aVar.b("androidx.savedstate.Restarter", this);
        }

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

    public Recreator(c cVar) {
        this.f190a = cVar;
    }

    @Override // b.j.e
    public void g(g gVar, d.a aVar) {
        if (aVar != d.a.ON_CREATE) {
            throw new AssertionError("Next event must be ON_CREATE");
        }
        h hVar = (h) gVar.a();
        hVar.c("removeObserver");
        hVar.f1290a.e(this);
        Bundle bundleA = this.f190a.d().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(a.InterfaceC0024a.class);
                try {
                    Constructor declaredConstructor = clsAsSubclass.getDeclaredConstructor(new Class[0]);
                    declaredConstructor.setAccessible(true);
                    try {
                        ((a.InterfaceC0024a) declaredConstructor.newInstance(new Object[0])).a(this.f190a);
                    } catch (Exception e2) {
                        throw new RuntimeException(c.a.a.a.a.b("Failed to instantiate ", str), e2);
                    }
                } catch (NoSuchMethodException e3) {
                    StringBuilder sbD = c.a.a.a.a.d("Class");
                    sbD.append(clsAsSubclass.getSimpleName());
                    sbD.append(" must have default constructor in order to be automatically recreated");
                    throw new IllegalStateException(sbD.toString(), e3);
                }
            } catch (ClassNotFoundException e4) {
                throw new RuntimeException(c.a.a.a.a.c("Class ", str, " wasn't found"), e4);
            }
        }
    }
}
