package c.b.e.i;

import android.content.Context;
import android.content.res.Resources;
import android.os.Handler;
import android.os.SystemClock;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.HeaderViewListAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import c.b.e.i.m;
import c.b.f.r0;
import c.b.f.s0;
import c.f.j.a0;
import c.f.j.i0;
import com.ifpdos.debugmenu.R;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.WeakHashMap;

/* JADX INFO: compiled from: CascadingMenuPopup.java */
/* JADX INFO: loaded from: classes.dex */
public final class d extends k implements m, View.OnKeyListener, PopupWindow.OnDismissListener {
    public m.a A;
    public ViewTreeObserver B;
    public PopupWindow.OnDismissListener C;
    public boolean D;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final Context f681e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final int f682f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final int f683g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final int f684h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public final boolean f685i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public final Handler f686j;
    public View r;
    public View s;
    public int t;
    public boolean u;
    public boolean v;
    public int w;
    public int x;
    public boolean z;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public final List<g> f687k = new ArrayList();

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public final List<C0004d> f688l = new ArrayList();
    public final ViewTreeObserver.OnGlobalLayoutListener m = new a();
    public final View.OnAttachStateChangeListener n = new b();
    public final r0 o = new c();
    public int p = 0;
    public int q = 0;
    public boolean y = false;

    /* JADX INFO: compiled from: CascadingMenuPopup.java */
    public class a implements ViewTreeObserver.OnGlobalLayoutListener {
        public a() {
        }

        @Override // android.view.ViewTreeObserver.OnGlobalLayoutListener
        public void onGlobalLayout() {
            if (!d.this.a() || d.this.f688l.size() <= 0 || d.this.f688l.get(0).a.D) {
                return;
            }
            View view = d.this.s;
            if (view == null || !view.isShown()) {
                d.this.dismiss();
                return;
            }
            Iterator<C0004d> it = d.this.f688l.iterator();
            while (it.hasNext()) {
                it.next().a.h();
            }
        }
    }

    /* JADX INFO: compiled from: CascadingMenuPopup.java */
    public class b implements View.OnAttachStateChangeListener {
        public b() {
        }

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

        @Override // android.view.View.OnAttachStateChangeListener
        public void onViewDetachedFromWindow(View view) {
            ViewTreeObserver viewTreeObserver = d.this.B;
            if (viewTreeObserver != null) {
                if (!viewTreeObserver.isAlive()) {
                    d.this.B = view.getViewTreeObserver();
                }
                d dVar = d.this;
                dVar.B.removeGlobalOnLayoutListener(dVar.m);
            }
            view.removeOnAttachStateChangeListener(this);
        }
    }

    /* JADX INFO: compiled from: CascadingMenuPopup.java */
    public class c implements r0 {

        /* JADX INFO: compiled from: CascadingMenuPopup.java */
        public class a implements Runnable {

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

            /* JADX INFO: renamed from: e, reason: collision with root package name */
            public final /* synthetic */ MenuItem f693e;

            /* JADX INFO: renamed from: f, reason: collision with root package name */
            public final /* synthetic */ g f694f;

            public a(C0004d c0004d, MenuItem menuItem, g gVar) {
                this.f692b = c0004d;
                this.f693e = menuItem;
                this.f694f = gVar;
            }

            @Override // java.lang.Runnable
            public void run() {
                C0004d c0004d = this.f692b;
                if (c0004d != null) {
                    d.this.D = true;
                    c0004d.f696b.c(false);
                    d.this.D = false;
                }
                if (this.f693e.isEnabled() && this.f693e.hasSubMenu()) {
                    this.f694f.r(this.f693e, 4);
                }
            }
        }

        public c() {
        }

        @Override // c.b.f.r0
        public void c(g gVar, MenuItem menuItem) {
            d.this.f686j.removeCallbacksAndMessages(null);
            int size = d.this.f688l.size();
            int i2 = 0;
            while (true) {
                if (i2 >= size) {
                    i2 = -1;
                    break;
                } else if (gVar == d.this.f688l.get(i2).f696b) {
                    break;
                } else {
                    i2++;
                }
            }
            if (i2 == -1) {
                return;
            }
            int i3 = i2 + 1;
            d.this.f686j.postAtTime(new a(i3 < d.this.f688l.size() ? d.this.f688l.get(i3) : null, menuItem, gVar), gVar, SystemClock.uptimeMillis() + 200);
        }

        @Override // c.b.f.r0
        public void f(g gVar, MenuItem menuItem) {
            d.this.f686j.removeCallbacksAndMessages(gVar);
        }
    }

    /* JADX INFO: renamed from: c.b.e.i.d$d, reason: collision with other inner class name */
    /* JADX INFO: compiled from: CascadingMenuPopup.java */
    public static class C0004d {
        public final s0 a;

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

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

        public C0004d(s0 s0Var, g gVar, int i2) {
            this.a = s0Var;
            this.f696b = gVar;
            this.f697c = i2;
        }
    }

    public d(Context context, View view, int i2, int i3, boolean z) {
        this.f681e = context;
        this.r = view;
        this.f683g = i2;
        this.f684h = i3;
        this.f685i = z;
        WeakHashMap<View, i0> weakHashMap = a0.a;
        this.t = a0.e.d(view) != 1 ? 1 : 0;
        Resources resources = context.getResources();
        this.f682f = Math.max(resources.getDisplayMetrics().widthPixels / 2, resources.getDimensionPixelSize(R.dimen.abc_config_prefDialogWidth));
        this.f686j = new Handler();
    }

    @Override // c.b.e.i.p
    public boolean a() {
        return this.f688l.size() > 0 && this.f688l.get(0).a.a();
    }

    @Override // c.b.e.i.m
    public void b(g gVar, boolean z) {
        int size = this.f688l.size();
        int i2 = 0;
        while (true) {
            if (i2 >= size) {
                i2 = -1;
                break;
            } else if (gVar == this.f688l.get(i2).f696b) {
                break;
            } else {
                i2++;
            }
        }
        if (i2 < 0) {
            return;
        }
        int i3 = i2 + 1;
        if (i3 < this.f688l.size()) {
            this.f688l.get(i3).f696b.c(false);
        }
        C0004d c0004dRemove = this.f688l.remove(i2);
        c0004dRemove.f696b.u(this);
        if (this.D) {
            s0.a.b(c0004dRemove.a.E, null);
            c0004dRemove.a.E.setAnimationStyle(0);
        }
        c0004dRemove.a.dismiss();
        int size2 = this.f688l.size();
        if (size2 > 0) {
            this.t = this.f688l.get(size2 - 1).f697c;
        } else {
            View view = this.r;
            WeakHashMap<View, i0> weakHashMap = a0.a;
            this.t = a0.e.d(view) == 1 ? 0 : 1;
        }
        if (size2 != 0) {
            if (z) {
                this.f688l.get(0).f696b.c(false);
                return;
            }
            return;
        }
        dismiss();
        m.a aVar = this.A;
        if (aVar != null) {
            aVar.b(gVar, true);
        }
        ViewTreeObserver viewTreeObserver = this.B;
        if (viewTreeObserver != null) {
            if (viewTreeObserver.isAlive()) {
                this.B.removeGlobalOnLayoutListener(this.m);
            }
            this.B = null;
        }
        this.s.removeOnAttachStateChangeListener(this.n);
        this.C.onDismiss();
    }

    @Override // c.b.e.i.m
    public void c(boolean z) {
        Iterator<C0004d> it = this.f688l.iterator();
        while (it.hasNext()) {
            ListAdapter adapter = it.next().a.f937h.getAdapter();
            if (adapter instanceof HeaderViewListAdapter) {
                adapter = ((HeaderViewListAdapter) adapter).getWrappedAdapter();
            }
            ((f) adapter).notifyDataSetChanged();
        }
    }

    @Override // c.b.e.i.m
    public boolean d() {
        return false;
    }

    @Override // c.b.e.i.p
    public void dismiss() {
        int size = this.f688l.size();
        if (size > 0) {
            C0004d[] c0004dArr = (C0004d[]) this.f688l.toArray(new C0004d[size]);
            for (int i2 = size - 1; i2 >= 0; i2--) {
                C0004d c0004d = c0004dArr[i2];
                if (c0004d.a.a()) {
                    c0004d.a.dismiss();
                }
            }
        }
    }

    @Override // c.b.e.i.m
    public void g(m.a aVar) {
        this.A = aVar;
    }

    @Override // c.b.e.i.p
    public void h() {
        if (a()) {
            return;
        }
        Iterator<g> it = this.f687k.iterator();
        while (it.hasNext()) {
            v(it.next());
        }
        this.f687k.clear();
        View view = this.r;
        this.s = view;
        if (view != null) {
            boolean z = this.B == null;
            ViewTreeObserver viewTreeObserver = view.getViewTreeObserver();
            this.B = viewTreeObserver;
            if (z) {
                viewTreeObserver.addOnGlobalLayoutListener(this.m);
            }
            this.s.addOnAttachStateChangeListener(this.n);
        }
    }

    @Override // c.b.e.i.p
    public ListView j() {
        if (this.f688l.isEmpty()) {
            return null;
        }
        return this.f688l.get(r0.size() - 1).a.f937h;
    }

    @Override // c.b.e.i.m
    public boolean k(r rVar) {
        for (C0004d c0004d : this.f688l) {
            if (rVar == c0004d.f696b) {
                c0004d.a.f937h.requestFocus();
                return true;
            }
        }
        if (!rVar.hasVisibleItems()) {
            return false;
        }
        rVar.b(this, this.f681e);
        if (a()) {
            v(rVar);
        } else {
            this.f687k.add(rVar);
        }
        m.a aVar = this.A;
        if (aVar != null) {
            aVar.c(rVar);
        }
        return true;
    }

    @Override // c.b.e.i.k
    public void l(g gVar) {
        gVar.b(this, this.f681e);
        if (a()) {
            v(gVar);
        } else {
            this.f687k.add(gVar);
        }
    }

    @Override // c.b.e.i.k
    public void n(View view) {
        if (this.r != view) {
            this.r = view;
            int i2 = this.p;
            WeakHashMap<View, i0> weakHashMap = a0.a;
            this.q = Gravity.getAbsoluteGravity(i2, a0.e.d(view));
        }
    }

    @Override // c.b.e.i.k
    public void o(boolean z) {
        this.y = z;
    }

    @Override // android.widget.PopupWindow.OnDismissListener
    public void onDismiss() {
        C0004d c0004d;
        int size = this.f688l.size();
        int i2 = 0;
        while (true) {
            if (i2 >= size) {
                c0004d = null;
                break;
            }
            c0004d = this.f688l.get(i2);
            if (!c0004d.a.a()) {
                break;
            } else {
                i2++;
            }
        }
        if (c0004d != null) {
            c0004d.f696b.c(false);
        }
    }

    @Override // android.view.View.OnKeyListener
    public boolean onKey(View view, int i2, KeyEvent keyEvent) {
        if (keyEvent.getAction() != 1 || i2 != 82) {
            return false;
        }
        dismiss();
        return true;
    }

    @Override // c.b.e.i.k
    public void p(int i2) {
        if (this.p != i2) {
            this.p = i2;
            View view = this.r;
            WeakHashMap<View, i0> weakHashMap = a0.a;
            this.q = Gravity.getAbsoluteGravity(i2, a0.e.d(view));
        }
    }

    @Override // c.b.e.i.k
    public void q(int i2) {
        this.u = true;
        this.w = i2;
    }

    @Override // c.b.e.i.k
    public void r(PopupWindow.OnDismissListener onDismissListener) {
        this.C = onDismissListener;
    }

    @Override // c.b.e.i.k
    public void s(boolean z) {
        this.z = z;
    }

    @Override // c.b.e.i.k
    public void t(int i2) {
        this.v = true;
        this.x = i2;
    }

    /* JADX WARN: Removed duplicated region for block: B:46:0x00ea  */
    /* JADX WARN: Removed duplicated region for block: B:76:0x0178  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public final void v(c.b.e.i.g r17) {
        /*
            Method dump skipped, instruction units count: 470
            To view this dump add '--comments-level debug' option
        */
        throw new UnsupportedOperationException("Method not decompiled: c.b.e.i.d.v(c.b.e.i.g):void");
    }
}
