package androidx.fragment.app;

import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes.dex */
public class j0 {

    /* renamed from: a, reason: collision with root package name */
    private final ArrayList f4561a = new ArrayList();

    /* renamed from: b, reason: collision with root package name */
    private final HashMap f4562b = new HashMap();

    /* renamed from: c, reason: collision with root package name */
    private final HashMap f4563c = new HashMap();

    /* renamed from: d, reason: collision with root package name */
    private c0 f4564d;

    /* JADX INFO: Access modifiers changed from: package-private */
    public void A(c0 c0Var) {
        this.f4564d = c0Var;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public g0 B(String str, g0 g0Var) {
        return g0Var != null ? (g0) this.f4563c.put(str, g0Var) : (g0) this.f4563c.remove(str);
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void a(f fVar) {
        if (this.f4561a.contains(fVar)) {
            throw new IllegalStateException("Fragment already added: " + fVar);
        }
        synchronized (this.f4561a) {
            this.f4561a.add(fVar);
        }
        fVar.mAdded = true;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void b() {
        this.f4562b.values().removeAll(Collections.singleton(null));
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public boolean c(String str) {
        return this.f4562b.get(str) != null;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void d(int i10) {
        for (h0 h0Var : this.f4562b.values()) {
            if (h0Var != null) {
                h0Var.u(i10);
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void e(String str, FileDescriptor fileDescriptor, PrintWriter printWriter, String[] strArr) {
        String str2 = str + "    ";
        if (!this.f4562b.isEmpty()) {
            printWriter.print(str);
            printWriter.println("Active Fragments:");
            for (h0 h0Var : this.f4562b.values()) {
                printWriter.print(str);
                if (h0Var != null) {
                    f k10 = h0Var.k();
                    printWriter.println(k10);
                    k10.dump(str2, fileDescriptor, printWriter, strArr);
                } else {
                    printWriter.println("null");
                }
            }
        }
        int size = this.f4561a.size();
        if (size > 0) {
            printWriter.print(str);
            printWriter.println("Added Fragments:");
            for (int i10 = 0; i10 < size; i10++) {
                f fVar = (f) this.f4561a.get(i10);
                printWriter.print(str);
                printWriter.print("  #");
                printWriter.print(i10);
                printWriter.print(": ");
                printWriter.println(fVar.toString());
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public f f(String str) {
        h0 h0Var = (h0) this.f4562b.get(str);
        if (h0Var != null) {
            return h0Var.k();
        }
        return null;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public f g(int i10) {
        for (int size = this.f4561a.size() - 1; size >= 0; size--) {
            f fVar = (f) this.f4561a.get(size);
            if (fVar != null && fVar.mFragmentId == i10) {
                return fVar;
            }
        }
        for (h0 h0Var : this.f4562b.values()) {
            if (h0Var != null) {
                f k10 = h0Var.k();
                if (k10.mFragmentId == i10) {
                    return k10;
                }
            }
        }
        return null;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public f h(String str) {
        if (str != null) {
            for (int size = this.f4561a.size() - 1; size >= 0; size--) {
                f fVar = (f) this.f4561a.get(size);
                if (fVar != null && str.equals(fVar.mTag)) {
                    return fVar;
                }
            }
        }
        if (str == null) {
            return null;
        }
        for (h0 h0Var : this.f4562b.values()) {
            if (h0Var != null) {
                f k10 = h0Var.k();
                if (str.equals(k10.mTag)) {
                    return k10;
                }
            }
        }
        return null;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public f i(String str) {
        f findFragmentByWho;
        for (h0 h0Var : this.f4562b.values()) {
            if (h0Var != null && (findFragmentByWho = h0Var.k().findFragmentByWho(str)) != null) {
                return findFragmentByWho;
            }
        }
        return null;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public int j(f fVar) {
        View view;
        View view2;
        ViewGroup viewGroup = fVar.mContainer;
        if (viewGroup == null) {
            return -1;
        }
        int indexOf = this.f4561a.indexOf(fVar);
        for (int i10 = indexOf - 1; i10 >= 0; i10--) {
            f fVar2 = (f) this.f4561a.get(i10);
            if (fVar2.mContainer == viewGroup && (view2 = fVar2.mView) != null) {
                return viewGroup.indexOfChild(view2) + 1;
            }
        }
        while (true) {
            indexOf++;
            if (indexOf >= this.f4561a.size()) {
                return -1;
            }
            f fVar3 = (f) this.f4561a.get(indexOf);
            if (fVar3.mContainer == viewGroup && (view = fVar3.mView) != null) {
                return viewGroup.indexOfChild(view);
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public List k() {
        ArrayList arrayList = new ArrayList();
        for (h0 h0Var : this.f4562b.values()) {
            if (h0Var != null) {
                arrayList.add(h0Var);
            }
        }
        return arrayList;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public List l() {
        ArrayList arrayList = new ArrayList();
        for (h0 h0Var : this.f4562b.values()) {
            if (h0Var != null) {
                arrayList.add(h0Var.k());
            } else {
                arrayList.add(null);
            }
        }
        return arrayList;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public ArrayList m() {
        return new ArrayList(this.f4563c.values());
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public h0 n(String str) {
        return (h0) this.f4562b.get(str);
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public List o() {
        ArrayList arrayList;
        if (this.f4561a.isEmpty()) {
            return Collections.emptyList();
        }
        synchronized (this.f4561a) {
            arrayList = new ArrayList(this.f4561a);
        }
        return arrayList;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public c0 p() {
        return this.f4564d;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public g0 q(String str) {
        return (g0) this.f4563c.get(str);
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void r(h0 h0Var) {
        f k10 = h0Var.k();
        if (c(k10.mWho)) {
            return;
        }
        this.f4562b.put(k10.mWho, h0Var);
        if (k10.mRetainInstanceChangedWhileDetached) {
            if (k10.mRetainInstance) {
                this.f4564d.P(k10);
            } else {
                this.f4564d.Z(k10);
            }
            k10.mRetainInstanceChangedWhileDetached = false;
        }
        if (z.N0(2)) {
            Log.v("FragmentManager", "Added fragment to active set " + k10);
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void s(h0 h0Var) {
        f k10 = h0Var.k();
        if (k10.mRetainInstance) {
            this.f4564d.Z(k10);
        }
        if (((h0) this.f4562b.put(k10.mWho, null)) != null && z.N0(2)) {
            Log.v("FragmentManager", "Removed fragment from active set " + k10);
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void t() {
        Iterator it = this.f4561a.iterator();
        while (it.hasNext()) {
            h0 h0Var = (h0) this.f4562b.get(((f) it.next()).mWho);
            if (h0Var != null) {
                h0Var.m();
            }
        }
        for (h0 h0Var2 : this.f4562b.values()) {
            if (h0Var2 != null) {
                h0Var2.m();
                f k10 = h0Var2.k();
                if (k10.mRemoving && !k10.isInBackStack()) {
                    if (k10.mBeingSaved && !this.f4563c.containsKey(k10.mWho)) {
                        h0Var2.s();
                    }
                    s(h0Var2);
                }
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void u(f fVar) {
        synchronized (this.f4561a) {
            this.f4561a.remove(fVar);
        }
        fVar.mAdded = false;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void v() {
        this.f4562b.clear();
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void w(List list) {
        this.f4561a.clear();
        if (list != null) {
            Iterator it = list.iterator();
            while (it.hasNext()) {
                String str = (String) it.next();
                f f10 = f(str);
                if (f10 == null) {
                    throw new IllegalStateException("No instantiated fragment for (" + str + ")");
                }
                if (z.N0(2)) {
                    Log.v("FragmentManager", "restoreSaveState: added (" + str + "): " + f10);
                }
                a(f10);
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void x(ArrayList arrayList) {
        this.f4563c.clear();
        Iterator it = arrayList.iterator();
        while (it.hasNext()) {
            g0 g0Var = (g0) it.next();
            this.f4563c.put(g0Var.f4533q, g0Var);
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public ArrayList y() {
        ArrayList arrayList = new ArrayList(this.f4562b.size());
        for (h0 h0Var : this.f4562b.values()) {
            if (h0Var != null) {
                f k10 = h0Var.k();
                h0Var.s();
                arrayList.add(k10.mWho);
                if (z.N0(2)) {
                    Log.v("FragmentManager", "Saved state of " + k10 + ": " + k10.mSavedFragmentState);
                }
            }
        }
        return arrayList;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public ArrayList z() {
        synchronized (this.f4561a) {
            if (this.f4561a.isEmpty()) {
                return null;
            }
            ArrayList arrayList = new ArrayList(this.f4561a.size());
            Iterator it = this.f4561a.iterator();
            while (it.hasNext()) {
                f fVar = (f) it.next();
                arrayList.add(fVar.mWho);
                if (z.N0(2)) {
                    Log.v("FragmentManager", "saveAllState: adding fragment (" + fVar.mWho + "): " + fVar);
                }
            }
            return arrayList;
        }
    }
}
