package o;

import android.content.Context;
import android.content.res.Resources;
import android.os.Handler;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.HeaderViewListAdapter;
import android.widget.ListAdapter;
import android.widget.PopupWindow;
import com.google.android.gms.common.R;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;

/* compiled from: r8-map-id-3b7cc9102578c509342fa2f0fabed2bfc72239cd258d579fc679bfcbc198b048 */
/* renamed from: o.Mg, reason: case insensitive filesystem */
/* loaded from: classes.dex */
public final class ViewOnKeyListenerC0317Mg extends UO implements View.OnKeyListener, PopupWindow.OnDismissListener {
    public int A;
    public boolean B;
    public boolean C;
    public int D;
    public int E;
    public boolean G;
    public InterfaceC0998dP H;
    public ViewTreeObserver I;
    public PopupWindow.OnDismissListener J;
    public boolean K;
    public final Context m;
    public final int n;

    /* renamed from: o, reason: collision with root package name */
    public final int f239o;
    public final boolean p;
    public final Handler q;
    public final ViewTreeObserverOnGlobalLayoutListenerC1647k8 t;
    public final ViewOnAttachStateChangeListenerC2491t3 u;
    public View y;
    public View z;
    public final ArrayList r = new ArrayList();
    public final ArrayList s = new ArrayList();
    public final OD v = new OD(21, this);
    public int w = 0;
    public int x = 0;
    public boolean F = false;

    public ViewOnKeyListenerC0317Mg(Context context, View view, int i, boolean z) {
        int i2 = 2;
        this.t = new ViewTreeObserverOnGlobalLayoutListenerC1647k8(i2, this);
        this.u = new ViewOnAttachStateChangeListenerC2491t3(i2, this);
        this.m = context;
        this.y = view;
        this.f239o = i;
        this.p = z;
        this.A = view.getLayoutDirection() != 1 ? 1 : 0;
        Resources resources = context.getResources();
        this.n = Math.max(resources.getDisplayMetrics().widthPixels / 2, resources.getDimensionPixelSize(R.dimen.abc_config_prefDialogWidth));
        this.q = new Handler();
    }

    @Override // o.InterfaceC1093eP
    public final void a(CO co, boolean z) {
        ArrayList arrayList = this.s;
        int size = arrayList.size();
        int i = 0;
        while (true) {
            if (i >= size) {
                i = -1;
                break;
            } else if (co == ((C0292Lg) arrayList.get(i)).b) {
                break;
            } else {
                i++;
            }
        }
        if (i < 0) {
            return;
        }
        int i2 = i + 1;
        if (i2 < arrayList.size()) {
            ((C0292Lg) arrayList.get(i2)).b.c(false);
        }
        C0292Lg c0292Lg = (C0292Lg) arrayList.remove(i);
        CO co2 = c0292Lg.b;
        C0711aP c0711aP = c0292Lg.a;
        C0979d8 c0979d8 = c0711aP.K;
        co2.r(this);
        if (this.K) {
            XO.b(c0979d8, null);
            c0979d8.setAnimationStyle(0);
        }
        c0711aP.dismiss();
        int size2 = arrayList.size();
        if (size2 > 0) {
            this.A = ((C0292Lg) arrayList.get(size2 - 1)).c;
        } else {
            this.A = this.y.getLayoutDirection() == 1 ? 0 : 1;
        }
        if (size2 != 0) {
            if (z) {
                ((C0292Lg) arrayList.get(0)).b.c(false);
                return;
            }
            return;
        }
        dismiss();
        InterfaceC0998dP interfaceC0998dP = this.H;
        if (interfaceC0998dP != null) {
            interfaceC0998dP.a(co, true);
        }
        ViewTreeObserver viewTreeObserver = this.I;
        if (viewTreeObserver != null) {
            if (viewTreeObserver.isAlive()) {
                this.I.removeGlobalOnLayoutListener(this.t);
            }
            this.I = null;
        }
        this.z.removeOnAttachStateChangeListener(this.u);
        this.J.onDismiss();
    }

    @Override // o.D80
    public final boolean b() {
        ArrayList arrayList = this.s;
        return arrayList.size() > 0 && ((C0292Lg) arrayList.get(0)).a.K.isShowing();
    }

    @Override // o.InterfaceC1093eP
    public final void c() {
        ArrayList arrayList = this.s;
        int size = arrayList.size();
        int i = 0;
        while (i < size) {
            Object obj = arrayList.get(i);
            i++;
            ListAdapter adapter = ((C0292Lg) obj).a.n.getAdapter();
            if (adapter instanceof HeaderViewListAdapter) {
                adapter = ((HeaderViewListAdapter) adapter).getWrappedAdapter();
            }
            ((C3089zO) adapter).notifyDataSetChanged();
        }
    }

    @Override // o.D80
    public final void dismiss() {
        ArrayList arrayList = this.s;
        int size = arrayList.size();
        if (size > 0) {
            C0292Lg[] c0292LgArr = (C0292Lg[]) arrayList.toArray(new C0292Lg[size]);
            for (int i = size - 1; i >= 0; i--) {
                C0292Lg c0292Lg = c0292LgArr[i];
                if (c0292Lg.a.K.isShowing()) {
                    c0292Lg.a.dismiss();
                }
            }
        }
    }

    @Override // o.D80
    public final void e() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        if (b()) {
            return;
        }
        ArrayList arrayList = this.r;
        int size = arrayList.size();
        int i = 0;
        while (i < size) {
            Object obj = arrayList.get(i);
            i++;
            u((CO) obj);
        }
        arrayList.clear();
        View view = this.y;
        this.z = view;
        if (view != null) {
            boolean z = this.I == null;
            ViewTreeObserver viewTreeObserver = view.getViewTreeObserver();
            this.I = viewTreeObserver;
            if (z) {
                viewTreeObserver.addOnGlobalLayoutListener(this.t);
            }
            this.z.addOnAttachStateChangeListener(this.u);
        }
    }

    @Override // o.InterfaceC1093eP
    public final void f(InterfaceC0998dP interfaceC0998dP) {
        this.H = interfaceC0998dP;
    }

    @Override // o.D80
    public final C0097Dt h() {
        ArrayList arrayList = this.s;
        if (arrayList.isEmpty()) {
            return null;
        }
        return ((C0292Lg) arrayList.get(arrayList.size() - 1)).a.n;
    }

    @Override // o.InterfaceC1093eP
    public final boolean i() {
        return false;
    }

    @Override // o.InterfaceC1093eP
    public final boolean j(SubMenuC2155pb0 subMenuC2155pb0) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        ArrayList arrayList = this.s;
        int size = arrayList.size();
        int i = 0;
        while (i < size) {
            Object obj = arrayList.get(i);
            i++;
            C0292Lg c0292Lg = (C0292Lg) obj;
            if (subMenuC2155pb0 == c0292Lg.b) {
                c0292Lg.a.n.requestFocus();
                return true;
            }
        }
        if (!subMenuC2155pb0.hasVisibleItems()) {
            return false;
        }
        l(subMenuC2155pb0);
        InterfaceC0998dP interfaceC0998dP = this.H;
        if (interfaceC0998dP != null) {
            interfaceC0998dP.k(subMenuC2155pb0);
        }
        return true;
    }

    @Override // o.UO
    public final void l(CO co) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        co.b(this, this.m);
        if (b()) {
            u(co);
        } else {
            this.r.add(co);
        }
    }

    @Override // o.UO
    public final void n(View view) {
        if (this.y != view) {
            this.y = view;
            this.x = Gravity.getAbsoluteGravity(this.w, view.getLayoutDirection());
        }
    }

    @Override // o.UO
    public final void o(boolean z) {
        this.F = z;
    }

    @Override // android.widget.PopupWindow.OnDismissListener
    public final void onDismiss() {
        C0292Lg c0292Lg;
        ArrayList arrayList = this.s;
        int size = arrayList.size();
        int i = 0;
        while (true) {
            if (i >= size) {
                c0292Lg = null;
                break;
            }
            c0292Lg = (C0292Lg) arrayList.get(i);
            if (!c0292Lg.a.K.isShowing()) {
                break;
            } else {
                i++;
            }
        }
        if (c0292Lg != null) {
            c0292Lg.b.c(false);
        }
    }

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

    @Override // o.UO
    public final void p(int i) {
        if (this.w != i) {
            this.w = i;
            this.x = Gravity.getAbsoluteGravity(i, this.y.getLayoutDirection());
        }
    }

    @Override // o.UO
    public final void q(int i) {
        this.B = true;
        this.D = i;
    }

    @Override // o.UO
    public final void r(PopupWindow.OnDismissListener onDismissListener) {
        this.J = onDismissListener;
    }

    @Override // o.UO
    public final void s(boolean z) {
        this.G = z;
    }

    @Override // o.UO
    public final void t(int i) {
        this.C = true;
        this.E = i;
    }

    /* JADX WARN: Removed duplicated region for block: B:55:0x010a  */
    /* JADX WARN: Removed duplicated region for block: B:66:0x015b  */
    /* JADX WARN: Removed duplicated region for block: B:68:0x015e  */
    /* JADX WARN: Removed duplicated region for block: B:91:0x01cd  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public final void u(o.CO r18) throws java.lang.IllegalAccessException, java.lang.IllegalArgumentException, java.lang.reflect.InvocationTargetException {
        /*
            Method dump skipped, instructions count: 553
            To view this dump add '--comments-level debug' option
        */
        throw new UnsupportedOperationException("Method not decompiled: o.ViewOnKeyListenerC0317Mg.u(o.CO):void");
    }
}
