package c.k.b;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import java.util.Iterator;
import java.util.Objects;

/* JADX INFO: compiled from: FragmentLayoutInflaterFactory.java */
/* JADX INFO: loaded from: classes.dex */
public class w implements LayoutInflater.Factory2 {

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final FragmentManager f1743b;

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

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        public final /* synthetic */ b0 f1744b;

        public a(b0 b0Var) {
            this.f1744b = b0Var;
        }

        @Override // android.view.View.OnAttachStateChangeListener
        public void onViewAttachedToWindow(View view) {
            b0 b0Var = this.f1744b;
            Fragment fragment = b0Var.f1600c;
            b0Var.k();
            r0.f((ViewGroup) fragment.mView.getParent(), w.this.f1743b).e();
        }

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

    public w(FragmentManager fragmentManager) {
        this.f1743b = fragmentManager;
    }

    @Override // android.view.LayoutInflater.Factory
    public View onCreateView(String str, Context context, AttributeSet attributeSet) {
        return onCreateView(null, str, context, attributeSet);
    }

    @Override // android.view.LayoutInflater.Factory2
    public View onCreateView(View view, String str, Context context, AttributeSet attributeSet) {
        boolean zIsAssignableFrom;
        b0 b0VarH;
        if (s.class.getName().equals(str)) {
            return new s(context, attributeSet, this.f1743b);
        }
        Fragment fragment = null;
        if (!"fragment".equals(str)) {
            return null;
        }
        String attributeValue = attributeSet.getAttributeValue(null, "class");
        TypedArray typedArrayObtainStyledAttributes = context.obtainStyledAttributes(attributeSet, c.k.a.a);
        if (attributeValue == null) {
            attributeValue = typedArrayObtainStyledAttributes.getString(0);
        }
        int resourceId = typedArrayObtainStyledAttributes.getResourceId(1, -1);
        String string = typedArrayObtainStyledAttributes.getString(2);
        typedArrayObtainStyledAttributes.recycle();
        if (attributeValue != null) {
            ClassLoader classLoader = context.getClassLoader();
            c.d.h<ClassLoader, c.d.h<String, Class<?>>> hVar = u.a;
            try {
                zIsAssignableFrom = Fragment.class.isAssignableFrom(u.b(classLoader, attributeValue));
            } catch (ClassNotFoundException unused) {
                zIsAssignableFrom = false;
            }
            if (zIsAssignableFrom) {
                int id = view != null ? view.getId() : 0;
                if (id == -1 && resourceId == -1 && string == null) {
                    throw new IllegalArgumentException(attributeSet.getPositionDescription() + ": Must specify unique android:id, android:tag, or have a parent with an id for " + attributeValue);
                }
                Fragment fragmentH = resourceId != -1 ? this.f1743b.H(resourceId) : null;
                if (fragmentH == null && string != null) {
                    c0 c0Var = this.f1743b.f336c;
                    Objects.requireNonNull(c0Var);
                    int size = c0Var.a.size();
                    while (true) {
                        size--;
                        if (size >= 0) {
                            Fragment fragment2 = c0Var.a.get(size);
                            if (fragment2 != null && string.equals(fragment2.mTag)) {
                                fragment = fragment2;
                                break;
                            }
                        } else {
                            Iterator<b0> it = c0Var.f1608b.values().iterator();
                            while (true) {
                                if (!it.hasNext()) {
                                    break;
                                }
                                b0 next = it.next();
                                if (next != null) {
                                    Fragment fragment3 = next.f1600c;
                                    if (string.equals(fragment3.mTag)) {
                                        fragment = fragment3;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    fragmentH = fragment;
                }
                if (fragmentH == null && id != -1) {
                    fragmentH = this.f1743b.H(id);
                }
                if (fragmentH == null) {
                    fragmentH = this.f1743b.J().a(context.getClassLoader(), attributeValue);
                    fragmentH.mFromLayout = true;
                    fragmentH.mFragmentId = resourceId != 0 ? resourceId : id;
                    fragmentH.mContainerId = id;
                    fragmentH.mTag = string;
                    fragmentH.mInLayout = true;
                    FragmentManager fragmentManager = this.f1743b;
                    fragmentH.mFragmentManager = fragmentManager;
                    v<?> vVar = fragmentManager.q;
                    fragmentH.mHost = vVar;
                    fragmentH.onInflate(vVar.f1740e, attributeSet, fragmentH.mSavedFragmentState);
                    b0VarH = this.f1743b.a(fragmentH);
                    if (FragmentManager.M(2)) {
                        Log.v("FragmentManager", "Fragment " + fragmentH + " has been inflated via the <fragment> tag: id=0x" + Integer.toHexString(resourceId));
                    }
                } else {
                    if (fragmentH.mInLayout) {
                        throw new IllegalArgumentException(attributeSet.getPositionDescription() + ": Duplicate id 0x" + Integer.toHexString(resourceId) + ", tag " + string + ", or parent id 0x" + Integer.toHexString(id) + " with another fragment for " + attributeValue);
                    }
                    fragmentH.mInLayout = true;
                    FragmentManager fragmentManager2 = this.f1743b;
                    fragmentH.mFragmentManager = fragmentManager2;
                    v<?> vVar2 = fragmentManager2.q;
                    fragmentH.mHost = vVar2;
                    fragmentH.onInflate(vVar2.f1740e, attributeSet, fragmentH.mSavedFragmentState);
                    b0VarH = this.f1743b.h(fragmentH);
                    if (FragmentManager.M(2)) {
                        Log.v("FragmentManager", "Retained Fragment " + fragmentH + " has been re-attached via the <fragment> tag: id=0x" + Integer.toHexString(resourceId));
                    }
                }
                fragmentH.mContainer = (ViewGroup) view;
                b0VarH.k();
                b0VarH.j();
                View view2 = fragmentH.mView;
                if (view2 == null) {
                    throw new IllegalStateException(d.b.a.a.a.q("Fragment ", attributeValue, " did not create a view."));
                }
                if (resourceId != 0) {
                    view2.setId(resourceId);
                }
                if (fragmentH.mView.getTag() == null) {
                    fragmentH.mView.setTag(string);
                }
                fragmentH.mView.addOnAttachStateChangeListener(new a(b0VarH));
                return fragmentH.mView;
            }
        }
        return null;
    }
}
