package c.b.f.d;

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.f.d.k;
import c.b.g.a0;
import c.b.g.b0;
import com.ifpdos.factory.R;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.WeakHashMap;

/* JADX INFO: loaded from: classes.dex */
public final class d extends i implements k, View.OnKeyListener, PopupWindow.OnDismissListener {
    public PopupWindow.OnDismissListener A;
    public boolean B;

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

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

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

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

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

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final Handler f4822h;
    public View p;
    public View q;
    public int r;
    public boolean s;
    public boolean t;
    public int u;
    public int v;
    public boolean x;
    public k.a y;
    public ViewTreeObserver z;

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

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public final List<C0060d> f4824j = new ArrayList();
    public final ViewTreeObserver.OnGlobalLayoutListener k = new a();
    public final View.OnAttachStateChangeListener l = new b();
    public final a0 m = new c();
    public int n = 0;
    public int o = 0;
    public boolean w = false;

    public class a implements ViewTreeObserver.OnGlobalLayoutListener {
        public a() {
        }

        @Override // android.view.ViewTreeObserver.OnGlobalLayoutListener
        public void onGlobalLayout() {
            if (!d.this.j() || d.this.f4824j.size() <= 0 || d.this.f4824j.get(0).f4832a.B) {
                return;
            }
            View view = d.this.q;
            if (view == null || !view.isShown()) {
                d.this.d();
                return;
            }
            Iterator<C0060d> it = d.this.f4824j.iterator();
            while (it.hasNext()) {
                it.next().f4832a.b();
            }
        }
    }

    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.z;
            if (viewTreeObserver != null) {
                if (!viewTreeObserver.isAlive()) {
                    d.this.z = view.getViewTreeObserver();
                }
                d dVar = d.this;
                dVar.z.removeGlobalOnLayoutListener(dVar.k);
            }
            view.removeOnAttachStateChangeListener(this);
        }
    }

    public class c implements a0 {

        public class a implements Runnable {

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

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

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

            public a(C0060d c0060d, MenuItem menuItem, f fVar) {
                this.f4828b = c0060d;
                this.f4829c = menuItem;
                this.f4830d = fVar;
            }

            @Override // java.lang.Runnable
            public void run() {
                C0060d c0060d = this.f4828b;
                if (c0060d != null) {
                    d.this.B = true;
                    c0060d.f4833b.c(false);
                    d.this.B = false;
                }
                if (this.f4829c.isEnabled() && this.f4829c.hasSubMenu()) {
                    this.f4830d.q(this.f4829c, 4);
                }
            }
        }

        public c() {
        }

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

        @Override // c.b.g.a0
        public void c(f fVar, MenuItem menuItem) {
            d.this.f4822h.removeCallbacksAndMessages(fVar);
        }
    }

    /* JADX INFO: renamed from: c.b.f.d.d$d, reason: collision with other inner class name */
    public static class C0060d {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        public final b0 f4832a;

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

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

        public C0060d(b0 b0Var, f fVar, int i2) {
            this.f4832a = b0Var;
            this.f4833b = fVar;
            this.f4834c = i2;
        }
    }

    public d(Context context, View view, int i2, int i3, boolean z) {
        this.f4817c = context;
        this.p = view;
        this.f4819e = i2;
        this.f4820f = i3;
        this.f4821g = z;
        WeakHashMap<View, c.e.i.q> weakHashMap = c.e.i.l.f5278a;
        this.r = view.getLayoutDirection() != 1 ? 1 : 0;
        Resources resources = context.getResources();
        this.f4818d = Math.max(resources.getDisplayMetrics().widthPixels / 2, resources.getDimensionPixelSize(R.dimen.abc_config_prefDialogWidth));
        this.f4822h = new Handler();
    }

    @Override // c.b.f.d.k
    public void a(f fVar, boolean z) {
        int i2;
        int size = this.f4824j.size();
        int i3 = 0;
        while (true) {
            if (i3 >= size) {
                i3 = -1;
                break;
            } else if (fVar == this.f4824j.get(i3).f4833b) {
                break;
            } else {
                i3++;
            }
        }
        if (i3 < 0) {
            return;
        }
        int i4 = i3 + 1;
        if (i4 < this.f4824j.size()) {
            this.f4824j.get(i4).f4833b.c(false);
        }
        C0060d c0060dRemove = this.f4824j.remove(i3);
        c0060dRemove.f4833b.t(this);
        if (this.B) {
            c0060dRemove.f4832a.C.setExitTransition(null);
            c0060dRemove.f4832a.C.setAnimationStyle(0);
        }
        c0060dRemove.f4832a.d();
        int size2 = this.f4824j.size();
        if (size2 > 0) {
            i2 = this.f4824j.get(size2 - 1).f4834c;
        } else {
            View view = this.p;
            WeakHashMap<View, c.e.i.q> weakHashMap = c.e.i.l.f5278a;
            i2 = view.getLayoutDirection() == 1 ? 0 : 1;
        }
        this.r = i2;
        if (size2 != 0) {
            if (z) {
                this.f4824j.get(0).f4833b.c(false);
                return;
            }
            return;
        }
        d();
        k.a aVar = this.y;
        if (aVar != null) {
            aVar.a(fVar, true);
        }
        ViewTreeObserver viewTreeObserver = this.z;
        if (viewTreeObserver != null) {
            if (viewTreeObserver.isAlive()) {
                this.z.removeGlobalOnLayoutListener(this.k);
            }
            this.z = null;
        }
        this.q.removeOnAttachStateChangeListener(this.l);
        this.A.onDismiss();
    }

    @Override // c.b.f.d.n
    public void b() {
        if (j()) {
            return;
        }
        Iterator<f> it = this.f4823i.iterator();
        while (it.hasNext()) {
            w(it.next());
        }
        this.f4823i.clear();
        View view = this.p;
        this.q = view;
        if (view != null) {
            boolean z = this.z == null;
            ViewTreeObserver viewTreeObserver = view.getViewTreeObserver();
            this.z = viewTreeObserver;
            if (z) {
                viewTreeObserver.addOnGlobalLayoutListener(this.k);
            }
            this.q.addOnAttachStateChangeListener(this.l);
        }
    }

    @Override // c.b.f.d.n
    public void d() {
        int size = this.f4824j.size();
        if (size > 0) {
            C0060d[] c0060dArr = (C0060d[]) this.f4824j.toArray(new C0060d[size]);
            for (int i2 = size - 1; i2 >= 0; i2--) {
                C0060d c0060d = c0060dArr[i2];
                if (c0060d.f4832a.j()) {
                    c0060d.f4832a.d();
                }
            }
        }
    }

    @Override // c.b.f.d.n
    public ListView e() {
        if (this.f4824j.isEmpty()) {
            return null;
        }
        return this.f4824j.get(r0.size() - 1).f4832a.f5074f;
    }

    @Override // c.b.f.d.k
    public boolean f(p pVar) {
        for (C0060d c0060d : this.f4824j) {
            if (pVar == c0060d.f4833b) {
                c0060d.f4832a.f5074f.requestFocus();
                return true;
            }
        }
        if (!pVar.hasVisibleItems()) {
            return false;
        }
        pVar.b(this, this.f4817c);
        if (j()) {
            w(pVar);
        } else {
            this.f4823i.add(pVar);
        }
        k.a aVar = this.y;
        if (aVar != null) {
            aVar.b(pVar);
        }
        return true;
    }

    @Override // c.b.f.d.k
    public void g(boolean z) {
        Iterator<C0060d> it = this.f4824j.iterator();
        while (it.hasNext()) {
            ListAdapter adapter = it.next().f4832a.f5074f.getAdapter();
            if (adapter instanceof HeaderViewListAdapter) {
                adapter = ((HeaderViewListAdapter) adapter).getWrappedAdapter();
            }
            ((e) adapter).notifyDataSetChanged();
        }
    }

    @Override // c.b.f.d.k
    public boolean h() {
        return false;
    }

    @Override // c.b.f.d.n
    public boolean j() {
        return this.f4824j.size() > 0 && this.f4824j.get(0).f4832a.j();
    }

    @Override // c.b.f.d.k
    public void l(k.a aVar) {
        this.y = aVar;
    }

    @Override // c.b.f.d.i
    public void m(f fVar) {
        fVar.b(this, this.f4817c);
        if (j()) {
            w(fVar);
        } else {
            this.f4823i.add(fVar);
        }
    }

    @Override // c.b.f.d.i
    public void o(View view) {
        if (this.p != view) {
            this.p = view;
            int i2 = this.n;
            WeakHashMap<View, c.e.i.q> weakHashMap = c.e.i.l.f5278a;
            this.o = Gravity.getAbsoluteGravity(i2, view.getLayoutDirection());
        }
    }

    @Override // android.widget.PopupWindow.OnDismissListener
    public void onDismiss() {
        C0060d c0060d;
        int size = this.f4824j.size();
        int i2 = 0;
        while (true) {
            if (i2 >= size) {
                c0060d = null;
                break;
            }
            c0060d = this.f4824j.get(i2);
            if (!c0060d.f4832a.j()) {
                break;
            } else {
                i2++;
            }
        }
        if (c0060d != null) {
            c0060d.f4833b.c(false);
        }
    }

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

    @Override // c.b.f.d.i
    public void p(boolean z) {
        this.w = z;
    }

    @Override // c.b.f.d.i
    public void q(int i2) {
        if (this.n != i2) {
            this.n = i2;
            View view = this.p;
            WeakHashMap<View, c.e.i.q> weakHashMap = c.e.i.l.f5278a;
            this.o = Gravity.getAbsoluteGravity(i2, view.getLayoutDirection());
        }
    }

    @Override // c.b.f.d.i
    public void r(int i2) {
        this.s = true;
        this.u = i2;
    }

    @Override // c.b.f.d.i
    public void s(PopupWindow.OnDismissListener onDismissListener) {
        this.A = onDismissListener;
    }

    @Override // c.b.f.d.i
    public void t(boolean z) {
        this.x = z;
    }

    @Override // c.b.f.d.i
    public void u(int i2) {
        this.t = true;
        this.v = 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 w(c.b.f.d.f 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.f.d.d.w(c.b.f.d.f):void");
    }
}
