package androidx.fragment.app;

import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import androidx.fragment.app.c0;
import androidx.lifecycle.f;

/* JADX INFO: compiled from: FragmentStateManager.java */
/* JADX INFO: loaded from: classes.dex */
class t {
    private final l a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private final u f819b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private final Fragment f820c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private boolean f821d = false;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private int f822e = -1;

    /* JADX INFO: compiled from: FragmentStateManager.java */
    class a implements View.OnAttachStateChangeListener {

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        final /* synthetic */ View f823b;

        a(t tVar, View view) {
            this.f823b = view;
        }

        @Override // android.view.View.OnAttachStateChangeListener
        public void onViewAttachedToWindow(View view) {
            this.f823b.removeOnAttachStateChangeListener(this);
            c.f.l.t.T(this.f823b);
        }

        @Override // android.view.View.OnAttachStateChangeListener
        public void onViewDetachedFromWindow(View view) {
        }
    }

    /* JADX INFO: compiled from: FragmentStateManager.java */
    static /* synthetic */ class b {
        static final /* synthetic */ int[] a;

        static {
            int[] iArr = new int[f.c.values().length];
            a = iArr;
            try {
                iArr[f.c.RESUMED.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                a[f.c.STARTED.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
            try {
                a[f.c.CREATED.ordinal()] = 3;
            } catch (NoSuchFieldError unused3) {
            }
            try {
                a[f.c.INITIALIZED.ordinal()] = 4;
            } catch (NoSuchFieldError unused4) {
            }
        }
    }

    t(l lVar, u uVar, Fragment fragment) {
        this.a = lVar;
        this.f819b = uVar;
        this.f820c = fragment;
    }

    private boolean l(View view) {
        if (view == this.f820c.L) {
            return true;
        }
        for (ViewParent parent = view.getParent(); parent != null; parent = parent.getParent()) {
            if (parent == this.f820c.L) {
                return true;
            }
        }
        return false;
    }

    private Bundle q() {
        Bundle bundle = new Bundle();
        this.f820c.e1(bundle);
        this.a.j(this.f820c, bundle, false);
        if (bundle.isEmpty()) {
            bundle = null;
        }
        if (this.f820c.L != null) {
            s();
        }
        if (this.f820c.f674g != null) {
            if (bundle == null) {
                bundle = new Bundle();
            }
            bundle.putSparseParcelableArray("android:view_state", this.f820c.f674g);
        }
        if (this.f820c.f675h != null) {
            if (bundle == null) {
                bundle = new Bundle();
            }
            bundle.putBundle("android:view_registry_state", this.f820c.f675h);
        }
        if (!this.f820c.N) {
            if (bundle == null) {
                bundle = new Bundle();
            }
            bundle.putBoolean("android:user_visible_hint", this.f820c.N);
        }
        return bundle;
    }

    void a() {
        if (m.D0(3)) {
            Log.d("FragmentManager", "moveto ACTIVITY_CREATED: " + this.f820c);
        }
        Fragment fragment = this.f820c;
        fragment.K0(fragment.f673f);
        l lVar = this.a;
        Fragment fragment2 = this.f820c;
        lVar.a(fragment2, fragment2.f673f, false);
    }

    void b() {
        int iJ = this.f819b.j(this.f820c);
        Fragment fragment = this.f820c;
        fragment.K.addView(fragment.L, iJ);
    }

    void c() {
        if (m.D0(3)) {
            Log.d("FragmentManager", "moveto ATTACHED: " + this.f820c);
        }
        Fragment fragment = this.f820c;
        Fragment fragment2 = fragment.l;
        t tVarM = null;
        if (fragment2 != null) {
            t tVarM2 = this.f819b.m(fragment2.f677j);
            if (tVarM2 == null) {
                throw new IllegalStateException("Fragment " + this.f820c + " declared target fragment " + this.f820c.l + " that does not belong to this FragmentManager!");
            }
            Fragment fragment3 = this.f820c;
            fragment3.m = fragment3.l.f677j;
            fragment3.l = null;
            tVarM = tVarM2;
        } else {
            String str = fragment.m;
            if (str != null && (tVarM = this.f819b.m(str)) == null) {
                throw new IllegalStateException("Fragment " + this.f820c + " declared target fragment " + this.f820c.m + " that does not belong to this FragmentManager!");
            }
        }
        if (tVarM != null && (m.P || tVarM.k().f672b < 1)) {
            tVarM.m();
        }
        Fragment fragment4 = this.f820c;
        fragment4.x = fragment4.w.s0();
        Fragment fragment5 = this.f820c;
        fragment5.z = fragment5.w.v0();
        this.a.g(this.f820c, false);
        this.f820c.L0();
        this.a.b(this.f820c, false);
    }

    int d() {
        Fragment fragment;
        ViewGroup viewGroup;
        Fragment fragment2 = this.f820c;
        if (fragment2.w == null) {
            return fragment2.f672b;
        }
        int iMin = this.f822e;
        int i2 = b.a[fragment2.U.ordinal()];
        if (i2 != 1) {
            iMin = i2 != 2 ? i2 != 3 ? i2 != 4 ? Math.min(iMin, -1) : Math.min(iMin, 0) : Math.min(iMin, 1) : Math.min(iMin, 5);
        }
        Fragment fragment3 = this.f820c;
        if (fragment3.r) {
            if (fragment3.s) {
                iMin = Math.max(this.f822e, 2);
                View view = this.f820c.L;
                if (view != null && view.getParent() == null) {
                    iMin = Math.min(iMin, 2);
                }
            } else {
                iMin = this.f822e < 4 ? Math.min(iMin, fragment3.f672b) : Math.min(iMin, 1);
            }
        }
        if (!this.f820c.p) {
            iMin = Math.min(iMin, 1);
        }
        c0.e.b bVarL = null;
        if (m.P && (viewGroup = (fragment = this.f820c).K) != null) {
            bVarL = c0.n(viewGroup, fragment.D()).l(this);
        }
        if (bVarL == c0.e.b.ADDING) {
            iMin = Math.min(iMin, 6);
        } else if (bVarL == c0.e.b.REMOVING) {
            iMin = Math.max(iMin, 3);
        } else {
            Fragment fragment4 = this.f820c;
            if (fragment4.q) {
                iMin = fragment4.V() ? Math.min(iMin, 1) : Math.min(iMin, -1);
            }
        }
        Fragment fragment5 = this.f820c;
        if (fragment5.M && fragment5.f672b < 5) {
            iMin = Math.min(iMin, 4);
        }
        if (m.D0(2)) {
            Log.v("FragmentManager", "computeExpectedState() of " + iMin + " for " + this.f820c);
        }
        return iMin;
    }

    void e() {
        if (m.D0(3)) {
            Log.d("FragmentManager", "moveto CREATED: " + this.f820c);
        }
        Fragment fragment = this.f820c;
        if (fragment.T) {
            fragment.l1(fragment.f673f);
            this.f820c.f672b = 1;
            return;
        }
        this.a.h(fragment, fragment.f673f, false);
        Fragment fragment2 = this.f820c;
        fragment2.O0(fragment2.f673f);
        l lVar = this.a;
        Fragment fragment3 = this.f820c;
        lVar.c(fragment3, fragment3.f673f, false);
    }

    void f() {
        String resourceName;
        if (this.f820c.r) {
            return;
        }
        if (m.D0(3)) {
            Log.d("FragmentManager", "moveto CREATE_VIEW: " + this.f820c);
        }
        Fragment fragment = this.f820c;
        LayoutInflater layoutInflaterU0 = fragment.U0(fragment.f673f);
        ViewGroup viewGroup = null;
        Fragment fragment2 = this.f820c;
        ViewGroup viewGroup2 = fragment2.K;
        if (viewGroup2 != null) {
            viewGroup = viewGroup2;
        } else {
            int i2 = fragment2.B;
            if (i2 != 0) {
                if (i2 == -1) {
                    throw new IllegalArgumentException("Cannot create fragment " + this.f820c + " for a container view with no id");
                }
                viewGroup = (ViewGroup) fragment2.w.n0().f(this.f820c.B);
                if (viewGroup == null) {
                    Fragment fragment3 = this.f820c;
                    if (!fragment3.t) {
                        try {
                            resourceName = fragment3.G().getResourceName(this.f820c.B);
                        } catch (Resources.NotFoundException unused) {
                            resourceName = "unknown";
                        }
                        throw new IllegalArgumentException("No view found for id 0x" + Integer.toHexString(this.f820c.B) + " (" + resourceName + ") for fragment " + this.f820c);
                    }
                }
            }
        }
        Fragment fragment4 = this.f820c;
        fragment4.K = viewGroup;
        fragment4.Q0(layoutInflaterU0, viewGroup, fragment4.f673f);
        View view = this.f820c.L;
        if (view != null) {
            boolean z = false;
            view.setSaveFromParentEnabled(false);
            Fragment fragment5 = this.f820c;
            fragment5.L.setTag(c.i.b.fragment_container_view_tag, fragment5);
            if (viewGroup != null) {
                b();
            }
            Fragment fragment6 = this.f820c;
            if (fragment6.D) {
                fragment6.L.setVisibility(8);
            }
            if (c.f.l.t.G(this.f820c.L)) {
                c.f.l.t.T(this.f820c.L);
            } else {
                View view2 = this.f820c.L;
                view2.addOnAttachStateChangeListener(new a(this, view2));
            }
            this.f820c.h1();
            l lVar = this.a;
            Fragment fragment7 = this.f820c;
            lVar.m(fragment7, fragment7.L, fragment7.f673f, false);
            int visibility = this.f820c.L.getVisibility();
            float alpha = this.f820c.L.getAlpha();
            if (m.P) {
                this.f820c.w1(alpha);
                Fragment fragment8 = this.f820c;
                if (fragment8.K != null && visibility == 0) {
                    View viewFindFocus = fragment8.L.findFocus();
                    if (viewFindFocus != null) {
                        this.f820c.r1(viewFindFocus);
                        if (m.D0(2)) {
                            Log.v("FragmentManager", "requestFocus: Saved focused view " + viewFindFocus + " for Fragment " + this.f820c);
                        }
                    }
                    this.f820c.L.setAlpha(0.0f);
                }
            } else {
                Fragment fragment9 = this.f820c;
                if (visibility == 0 && fragment9.K != null) {
                    z = true;
                }
                fragment9.P = z;
            }
        }
        this.f820c.f672b = 2;
    }

    void g() {
        Fragment fragmentF;
        if (m.D0(3)) {
            Log.d("FragmentManager", "movefrom CREATED: " + this.f820c);
        }
        Fragment fragment = this.f820c;
        boolean zIsChangingConfigurations = true;
        boolean z = fragment.q && !fragment.V();
        if (!(z || this.f819b.o().p(this.f820c))) {
            String str = this.f820c.m;
            if (str != null && (fragmentF = this.f819b.f(str)) != null && fragmentF.F) {
                this.f820c.l = fragmentF;
            }
            this.f820c.f672b = 0;
            return;
        }
        j<?> jVar = this.f820c.x;
        if (jVar instanceof androidx.lifecycle.z) {
            zIsChangingConfigurations = this.f819b.o().m();
        } else if (jVar.k() instanceof Activity) {
            zIsChangingConfigurations = true ^ ((Activity) jVar.k()).isChangingConfigurations();
        }
        if (z || zIsChangingConfigurations) {
            this.f819b.o().g(this.f820c);
        }
        this.f820c.R0();
        this.a.d(this.f820c, false);
        for (t tVar : this.f819b.k()) {
            if (tVar != null) {
                Fragment fragmentK = tVar.k();
                if (this.f820c.f677j.equals(fragmentK.m)) {
                    fragmentK.l = this.f820c;
                    fragmentK.m = null;
                }
            }
        }
        Fragment fragment2 = this.f820c;
        String str2 = fragment2.m;
        if (str2 != null) {
            fragment2.l = this.f819b.f(str2);
        }
        this.f819b.q(this);
    }

    void h() {
        View view;
        if (m.D0(3)) {
            Log.d("FragmentManager", "movefrom CREATE_VIEW: " + this.f820c);
        }
        Fragment fragment = this.f820c;
        ViewGroup viewGroup = fragment.K;
        if (viewGroup != null && (view = fragment.L) != null) {
            viewGroup.removeView(view);
        }
        this.f820c.S0();
        this.a.n(this.f820c, false);
        Fragment fragment2 = this.f820c;
        fragment2.K = null;
        fragment2.L = null;
        fragment2.W = null;
        fragment2.X.m(null);
        this.f820c.s = false;
    }

    void i() {
        if (m.D0(3)) {
            Log.d("FragmentManager", "movefrom ATTACHED: " + this.f820c);
        }
        this.f820c.T0();
        boolean z = false;
        this.a.e(this.f820c, false);
        Fragment fragment = this.f820c;
        fragment.f672b = -1;
        fragment.x = null;
        fragment.z = null;
        fragment.w = null;
        if (fragment.q && !fragment.V()) {
            z = true;
        }
        if (z || this.f819b.o().p(this.f820c)) {
            if (m.D0(3)) {
                Log.d("FragmentManager", "initState called for fragment: " + this.f820c);
            }
            this.f820c.S();
        }
    }

    void j() {
        Fragment fragment = this.f820c;
        if (fragment.r && fragment.s && !fragment.u) {
            if (m.D0(3)) {
                Log.d("FragmentManager", "moveto CREATE_VIEW: " + this.f820c);
            }
            Fragment fragment2 = this.f820c;
            fragment2.Q0(fragment2.U0(fragment2.f673f), null, this.f820c.f673f);
            View view = this.f820c.L;
            if (view != null) {
                view.setSaveFromParentEnabled(false);
                Fragment fragment3 = this.f820c;
                fragment3.L.setTag(c.i.b.fragment_container_view_tag, fragment3);
                Fragment fragment4 = this.f820c;
                if (fragment4.D) {
                    fragment4.L.setVisibility(8);
                }
                this.f820c.h1();
                l lVar = this.a;
                Fragment fragment5 = this.f820c;
                lVar.m(fragment5, fragment5.L, fragment5.f673f, false);
                this.f820c.f672b = 2;
            }
        }
    }

    Fragment k() {
        return this.f820c;
    }

    void m() {
        if (this.f821d) {
            if (m.D0(2)) {
                Log.v("FragmentManager", "Ignoring re-entrant call to moveToExpectedState() for " + k());
                return;
            }
            return;
        }
        try {
            this.f821d = true;
            while (true) {
                int iD = d();
                if (iD == this.f820c.f672b) {
                    if (m.P && this.f820c.Q) {
                        if (this.f820c.L != null && this.f820c.K != null) {
                            c0 c0VarN = c0.n(this.f820c.K, this.f820c.D());
                            if (this.f820c.D) {
                                c0VarN.c(this);
                            } else {
                                c0VarN.e(this);
                            }
                        }
                        this.f820c.Q = false;
                        this.f820c.t0(this.f820c.D);
                    }
                    return;
                }
                if (iD <= this.f820c.f672b) {
                    switch (this.f820c.f672b - 1) {
                        case -1:
                            i();
                            break;
                        case 0:
                            g();
                            break;
                        case 1:
                            h();
                            this.f820c.f672b = 1;
                            break;
                        case 2:
                            this.f820c.s = false;
                            this.f820c.f672b = 2;
                            break;
                        case 3:
                            if (m.D0(3)) {
                                Log.d("FragmentManager", "movefrom ACTIVITY_CREATED: " + this.f820c);
                            }
                            if (this.f820c.L != null && this.f820c.f674g == null) {
                                s();
                            }
                            if (this.f820c.L != null && this.f820c.K != null) {
                                c0.n(this.f820c.K, this.f820c.D()).d(this);
                            }
                            this.f820c.f672b = 3;
                            break;
                        case 4:
                            v();
                            break;
                        case 5:
                            this.f820c.f672b = 5;
                            break;
                        case 6:
                            n();
                            break;
                    }
                } else {
                    switch (this.f820c.f672b + 1) {
                        case 0:
                            c();
                            break;
                        case 1:
                            e();
                            break;
                        case 2:
                            j();
                            f();
                            break;
                        case 3:
                            a();
                            break;
                        case 4:
                            if (this.f820c.L != null && this.f820c.K != null) {
                                c0.n(this.f820c.K, this.f820c.D()).b(c0.e.c.from(this.f820c.L.getVisibility()), this);
                            }
                            this.f820c.f672b = 4;
                            break;
                        case 5:
                            u();
                            break;
                        case 6:
                            this.f820c.f672b = 6;
                            break;
                        case 7:
                            p();
                            break;
                    }
                }
            }
        } finally {
            this.f821d = false;
        }
    }

    void n() {
        if (m.D0(3)) {
            Log.d("FragmentManager", "movefrom RESUMED: " + this.f820c);
        }
        this.f820c.Z0();
        this.a.f(this.f820c, false);
    }

    void o(ClassLoader classLoader) {
        Bundle bundle = this.f820c.f673f;
        if (bundle == null) {
            return;
        }
        bundle.setClassLoader(classLoader);
        Fragment fragment = this.f820c;
        fragment.f674g = fragment.f673f.getSparseParcelableArray("android:view_state");
        Fragment fragment2 = this.f820c;
        fragment2.f675h = fragment2.f673f.getBundle("android:view_registry_state");
        Fragment fragment3 = this.f820c;
        fragment3.m = fragment3.f673f.getString("android:target_state");
        Fragment fragment4 = this.f820c;
        if (fragment4.m != null) {
            fragment4.n = fragment4.f673f.getInt("android:target_req_state", 0);
        }
        Fragment fragment5 = this.f820c;
        Boolean bool = fragment5.f676i;
        if (bool != null) {
            fragment5.N = bool.booleanValue();
            this.f820c.f676i = null;
        } else {
            fragment5.N = fragment5.f673f.getBoolean("android:user_visible_hint", true);
        }
        Fragment fragment6 = this.f820c;
        if (fragment6.N) {
            return;
        }
        fragment6.M = true;
    }

    void p() {
        if (m.D0(3)) {
            Log.d("FragmentManager", "moveto RESUMED: " + this.f820c);
        }
        View viewW = this.f820c.w();
        if (viewW != null && l(viewW)) {
            boolean zRequestFocus = viewW.requestFocus();
            if (m.D0(2)) {
                StringBuilder sb = new StringBuilder();
                sb.append("requestFocus: Restoring focused view ");
                sb.append(viewW);
                sb.append(" ");
                sb.append(zRequestFocus ? "succeeded" : "failed");
                sb.append(" on Fragment ");
                sb.append(this.f820c);
                sb.append(" resulting in focused view ");
                sb.append(this.f820c.L.findFocus());
                Log.v("FragmentManager", sb.toString());
            }
        }
        this.f820c.r1(null);
        this.f820c.d1();
        this.a.i(this.f820c, false);
        Fragment fragment = this.f820c;
        fragment.f673f = null;
        fragment.f674g = null;
        fragment.f675h = null;
    }

    s r() {
        s sVar = new s(this.f820c);
        if (this.f820c.f672b <= -1 || sVar.q != null) {
            sVar.q = this.f820c.f673f;
        } else {
            Bundle bundleQ = q();
            sVar.q = bundleQ;
            if (this.f820c.m != null) {
                if (bundleQ == null) {
                    sVar.q = new Bundle();
                }
                sVar.q.putString("android:target_state", this.f820c.m);
                int i2 = this.f820c.n;
                if (i2 != 0) {
                    sVar.q.putInt("android:target_req_state", i2);
                }
            }
        }
        return sVar;
    }

    void s() {
        if (this.f820c.L == null) {
            return;
        }
        SparseArray<Parcelable> sparseArray = new SparseArray<>();
        this.f820c.L.saveHierarchyState(sparseArray);
        if (sparseArray.size() > 0) {
            this.f820c.f674g = sparseArray;
        }
        Bundle bundle = new Bundle();
        this.f820c.W.h(bundle);
        if (bundle.isEmpty()) {
            return;
        }
        this.f820c.f675h = bundle;
    }

    void t(int i2) {
        this.f822e = i2;
    }

    void u() {
        if (m.D0(3)) {
            Log.d("FragmentManager", "moveto STARTED: " + this.f820c);
        }
        this.f820c.f1();
        this.a.k(this.f820c, false);
    }

    void v() {
        if (m.D0(3)) {
            Log.d("FragmentManager", "movefrom STARTED: " + this.f820c);
        }
        this.f820c.g1();
        this.a.l(this.f820c, false);
    }

    t(l lVar, u uVar, ClassLoader classLoader, i iVar, s sVar) {
        this.a = lVar;
        this.f819b = uVar;
        this.f820c = iVar.a(classLoader, sVar.f812b);
        Bundle bundle = sVar.n;
        if (bundle != null) {
            bundle.setClassLoader(classLoader);
        }
        this.f820c.q1(sVar.n);
        Fragment fragment = this.f820c;
        fragment.f677j = sVar.f813f;
        fragment.r = sVar.f814g;
        fragment.t = true;
        fragment.A = sVar.f815h;
        fragment.B = sVar.f816i;
        fragment.C = sVar.f817j;
        fragment.F = sVar.f818k;
        fragment.q = sVar.l;
        fragment.E = sVar.m;
        fragment.D = sVar.o;
        fragment.U = f.c.values()[sVar.p];
        Bundle bundle2 = sVar.q;
        if (bundle2 != null) {
            this.f820c.f673f = bundle2;
        } else {
            this.f820c.f673f = new Bundle();
        }
        if (m.D0(2)) {
            Log.v("FragmentManager", "Instantiated fragment " + this.f820c);
        }
    }

    t(l lVar, u uVar, Fragment fragment, s sVar) {
        this.a = lVar;
        this.f819b = uVar;
        this.f820c = fragment;
        fragment.f674g = null;
        fragment.f675h = null;
        fragment.v = 0;
        fragment.s = false;
        fragment.p = false;
        Fragment fragment2 = fragment.l;
        fragment.m = fragment2 != null ? fragment2.f677j : null;
        Fragment fragment3 = this.f820c;
        fragment3.l = null;
        Bundle bundle = sVar.q;
        if (bundle != null) {
            fragment3.f673f = bundle;
        } else {
            fragment3.f673f = new Bundle();
        }
    }
}
