package androidx.savedstate;

import android.annotation.SuppressLint;
import android.os.Bundle;
import androidx.lifecycle.f;
import androidx.lifecycle.j;
import androidx.lifecycle.l;
import androidx.savedstate.Recreator;
import java.util.Map;

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private Bundle f1179b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private boolean f1180c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private Recreator.a f1181d;
    private c.b.a.b.b<String, b> a = new c.b.a.b.b<>();

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    boolean f1182e = true;

    public interface a {
        void a(c cVar);
    }

    public interface b {
        Bundle a();
    }

    SavedStateRegistry() {
    }

    public Bundle a(String str) {
        if (!this.f1180c) {
            throw new IllegalStateException("You can consumeRestoredStateForKey only after super.onCreate of corresponding component");
        }
        Bundle bundle = this.f1179b;
        if (bundle == null) {
            return null;
        }
        Bundle bundle2 = bundle.getBundle(str);
        this.f1179b.remove(str);
        if (this.f1179b.isEmpty()) {
            this.f1179b = null;
        }
        return bundle2;
    }

    void b(f fVar, Bundle bundle) {
        if (this.f1180c) {
            throw new IllegalStateException("SavedStateRegistry was already restored.");
        }
        if (bundle != null) {
            this.f1179b = bundle.getBundle("androidx.lifecycle.BundlableSavedStateRegistry.key");
        }
        fVar.a(new j() { // from class: androidx.savedstate.SavedStateRegistry.1
            @Override // androidx.lifecycle.j
            public void d(l lVar, f.b bVar) {
                if (bVar == f.b.ON_START) {
                    SavedStateRegistry.this.f1182e = true;
                } else if (bVar == f.b.ON_STOP) {
                    SavedStateRegistry.this.f1182e = false;
                }
            }
        });
        this.f1180c = true;
    }

    void c(Bundle bundle) {
        Bundle bundle2 = new Bundle();
        Bundle bundle3 = this.f1179b;
        if (bundle3 != null) {
            bundle2.putAll(bundle3);
        }
        c.b.a.b.b<String, b>.d dVarC = this.a.c();
        while (dVarC.hasNext()) {
            Map.Entry next = dVarC.next();
            bundle2.putBundle((String) next.getKey(), ((b) next.getValue()).a());
        }
        bundle.putBundle("androidx.lifecycle.BundlableSavedStateRegistry.key", bundle2);
    }

    public void d(String str, b bVar) {
        if (this.a.f(str, bVar) != null) {
            throw new IllegalArgumentException("SavedStateProvider with the given key is already registered");
        }
    }

    public void e(Class<? extends a> cls) {
        if (!this.f1182e) {
            throw new IllegalStateException("Can not perform this action after onSaveInstanceState");
        }
        if (this.f1181d == null) {
            this.f1181d = new Recreator.a(this);
        }
        try {
            cls.getDeclaredConstructor(new Class[0]);
            this.f1181d.b(cls.getName());
        } catch (NoSuchMethodException e2) {
            throw new IllegalArgumentException("Class" + cls.getSimpleName() + " must have default constructor in order to be automatically recreated", e2);
        }
    }
}
