package ῲ.ῲ.ඓ.㳳;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Rect;
import android.os.Build;
import android.os.Handler;
import android.os.Parcelable;
import android.os.SystemClock;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
import android.widget.HeaderViewListAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import androidx.appcompat.R$dimen;
import androidx.appcompat.R$layout;
import androidx.appcompat.widget.MenuPopupWindow;
import java.util.ArrayList;
import java.util.List;
import ῲ.ᨨ.ᨨ.C1729;
import ῲ.ῲ.ඓ.㳳.AbstractC1897;
import ῲ.ῲ.㴹.AbstractC2043;
import K.K.K.ʬ.C2369;

/* renamed from: ῲ.ῲ.ඓ.㳳.㸾  reason: contains not printable characters */
/* compiled from: CascadingMenuPopup */
public final class View$OnKeyListenerC1925 extends AbstractC1916 implements AbstractC1897, View.OnKeyListener, PopupWindow.OnDismissListener {

    /* renamed from: 䃖  reason: contains not printable characters */
    public static final int f6552 = R$layout.abc_cascading_menu_item_layout;

    /* renamed from: ʬ  reason: contains not printable characters */
    public final Context f6553;

    /* renamed from: Ԕ  reason: contains not printable characters */
    public int f6554 = 0;

    /* renamed from: Ԛ  reason: contains not printable characters */
    public int f6555;

    /* renamed from: ؿ  reason: contains not printable characters */
    public View f6556;

    /* renamed from: ݜ  reason: contains not printable characters */
    public boolean f6557;

    /* renamed from: ඓ  reason: contains not printable characters */
    public final int f6558;

    /* renamed from: ᑓ  reason: contains not printable characters */
    public AbstractC1897.AbstractC1898 f6559;

    /* renamed from: ᓆ  reason: contains not printable characters */
    public boolean f6560;

    /* renamed from: ᓣ  reason: contains not printable characters */
    public View f6561;

    /* renamed from: ᕣ  reason: contains not printable characters */
    public final ViewTreeObserver.OnGlobalLayoutListener f6562 = new ViewTreeObserver$OnGlobalLayoutListenerC1929();

    /* renamed from: ᨨ  reason: contains not printable characters */
    public final List<C1912> f6563 = new ArrayList();

    /* renamed from: ⁱ  reason: contains not printable characters */
    public final boolean f6564;

    /* renamed from: ゎ  reason: contains not printable characters */
    public ViewTreeObserver f6565;

    /* renamed from: ト  reason: contains not printable characters */
    public PopupWindow.OnDismissListener f6566;

    /* renamed from: 㑃  reason: contains not printable characters */
    public final View.OnAttachStateChangeListener f6567 = new View$OnAttachStateChangeListenerC1928();

    /* renamed from: 㗗  reason: contains not printable characters */
    public int f6568 = 0;

    /* renamed from: 㗽  reason: contains not printable characters */
    public int f6569;

    /* renamed from: 㮔  reason: contains not printable characters */
    public boolean f6570;

    /* renamed from: 㳳  reason: contains not printable characters */
    public final List<C1930> f6571 = new ArrayList();

    /* renamed from: 㴪  reason: contains not printable characters */
    public boolean f6572;

    /* renamed from: 㴹  reason: contains not printable characters */
    public final int f6573;

    /* renamed from: 㶋  reason: contains not printable characters */
    public int f6574;

    /* renamed from: 㸾  reason: contains not printable characters */
    public final int f6575;

    /* renamed from: 㿎  reason: contains not printable characters */
    public final Handler f6576;

    /* renamed from: 䀟  reason: contains not printable characters */
    public boolean f6577;

    /* renamed from: 䁗  reason: contains not printable characters */
    public final AbstractC2043 f6578 = new C1926();

    /* renamed from: ῲ.ῲ.ඓ.㳳.㸾$ʬ  reason: contains not printable characters */
    /* compiled from: CascadingMenuPopup */
    public class C1926 implements AbstractC2043 {

        /* renamed from: ῲ.ῲ.ඓ.㳳.㸾$ʬ$K  reason: contains not printable characters */
        /* compiled from: CascadingMenuPopup */
        public class RunnableC1927 implements Runnable {

            /* renamed from: ʬ  reason: contains not printable characters */
            public final /* synthetic */ MenuItem f6580;

            /* renamed from: ῲ  reason: contains not printable characters */
            public final /* synthetic */ C1930 f6582;

            /* renamed from: 㸾  reason: contains not printable characters */
            public final /* synthetic */ C1912 f6583;

            public RunnableC1927(C1930 r2, MenuItem menuItem, C1912 r4) {
                this.f6582 = r2;
                this.f6580 = menuItem;
                this.f6583 = r4;
            }

            public void run() {
                C1930 r0 = this.f6582;
                if (r0 != null) {
                    View$OnKeyListenerC1925.this.f6570 = true;
                    r0.f6587.close(false);
                    View$OnKeyListenerC1925.this.f6570 = false;
                }
                if (this.f6580.isEnabled() && this.f6580.hasSubMenu()) {
                    this.f6583.performItemAction(this.f6580, 4);
                }
            }
        }

        public C1926() {
        }

        @Override // ῲ.ῲ.㴹.AbstractC2043
        /* renamed from: ῲ  reason: contains not printable characters */
        public void m4989(C1912 r1, MenuItem menuItem) {
            View$OnKeyListenerC1925.this.f6576.removeCallbacksAndMessages(r1);
        }

        @Override // ῲ.ῲ.㴹.AbstractC2043
        /* renamed from: K  reason: contains not printable characters */
        public void m4990(C1912 r6, MenuItem menuItem) {
            C1930 r1 = null;
            View$OnKeyListenerC1925.this.f6576.removeCallbacksAndMessages(null);
            int size = View$OnKeyListenerC1925.this.f6571.size();
            int i = 0;
            while (true) {
                if (i >= size) {
                    i = -1;
                    break;
                } else if (r6 == View$OnKeyListenerC1925.this.f6571.get(i).f6587) {
                    break;
                } else {
                    i++;
                }
            }
            if (i != -1) {
                int i2 = i + 1;
                if (i2 < View$OnKeyListenerC1925.this.f6571.size()) {
                    r1 = View$OnKeyListenerC1925.this.f6571.get(i2);
                }
                View$OnKeyListenerC1925.this.f6576.postAtTime(new RunnableC1927(r1, menuItem, r6), r6, SystemClock.uptimeMillis() + 200);
            }
        }
    }

    /* renamed from: ῲ.ῲ.ඓ.㳳.㸾$ῲ  reason: contains not printable characters */
    /* compiled from: CascadingMenuPopup */
    public class View$OnAttachStateChangeListenerC1928 implements View.OnAttachStateChangeListener {
        public View$OnAttachStateChangeListenerC1928() {
        }

        public void onViewAttachedToWindow(View view) {
        }

        public void onViewDetachedFromWindow(View view) {
            ViewTreeObserver viewTreeObserver = View$OnKeyListenerC1925.this.f6565;
            if (viewTreeObserver != null) {
                if (!viewTreeObserver.isAlive()) {
                    View$OnKeyListenerC1925.this.f6565 = view.getViewTreeObserver();
                }
                View$OnKeyListenerC1925 r0 = View$OnKeyListenerC1925.this;
                r0.f6565.removeGlobalOnLayoutListener(r0.f6562);
            }
            view.removeOnAttachStateChangeListener(this);
        }
    }

    /* renamed from: ῲ.ῲ.ඓ.㳳.㸾$K  reason: contains not printable characters */
    /* compiled from: CascadingMenuPopup */
    public class ViewTreeObserver$OnGlobalLayoutListenerC1929 implements ViewTreeObserver.OnGlobalLayoutListener {
        public ViewTreeObserver$OnGlobalLayoutListenerC1929() {
        }

        public void onGlobalLayout() {
            if (View$OnKeyListenerC1925.this.m4987() && View$OnKeyListenerC1925.this.f6571.size() > 0 && !View$OnKeyListenerC1925.this.f6571.get(0).f6588.m5182()) {
                View view = View$OnKeyListenerC1925.this.f6561;
                if (view == null || !view.isShown()) {
                    View$OnKeyListenerC1925.this.dismiss();
                    return;
                }
                for (C1930 r1 : View$OnKeyListenerC1925.this.f6571) {
                    r1.f6588.show();
                }
            }
        }
    }

    /* renamed from: ῲ.ῲ.ඓ.㳳.㸾$㸾  reason: contains not printable characters */
    /* compiled from: CascadingMenuPopup */
    public static class C1930 {

        /* renamed from: ʬ  reason: contains not printable characters */
        public final int f6586;

        /* renamed from: ῲ  reason: contains not printable characters */
        public final C1912 f6587;

        /* renamed from: K  reason: contains not printable characters */
        public final MenuPopupWindow f6588;

        public C1930(MenuPopupWindow menuPopupWindow, C1912 r2, int i) {
            this.f6588 = menuPopupWindow;
            this.f6587 = r2;
            this.f6586 = i;
        }

        /* renamed from: K  reason: contains not printable characters */
        public ListView m4991() {
            return this.f6588.m5185();
        }
    }

    public View$OnKeyListenerC1925(Context context, View view, int i, int i2, boolean z) {
        this.f6553 = context;
        this.f6556 = view;
        this.f6558 = i;
        this.f6573 = i2;
        this.f6564 = z;
        this.f6560 = false;
        this.f6569 = C1729.m4511(this.f6556) == 1 ? 0 : 1;
        Resources resources = context.getResources();
        this.f6575 = Math.max(resources.getDisplayMetrics().widthPixels / 2, resources.getDimensionPixelSize(R$dimen.abc_config_prefDialogWidth));
        this.f6576 = new Handler();
    }

    @Override // ῲ.ῲ.ඓ.㳳.AbstractC1903
    public void dismiss() {
        int size = this.f6571.size();
        if (size > 0) {
            C1930[] r1 = (C1930[]) this.f6571.toArray(new C1930[size]);
            for (int i = size - 1; i >= 0; i--) {
                C1930 r2 = r1[i];
                if (r2.f6588.m5180()) {
                    r2.f6588.dismiss();
                }
            }
        }
    }

    @Override // ῲ.ῲ.ඓ.㳳.AbstractC1897
    public boolean flagActionItems() {
        return false;
    }

    @Override // ῲ.ῲ.ඓ.㳳.AbstractC1897
    public void onCloseMenu(C1912 r7, boolean z) {
        int size = this.f6571.size();
        int i = 0;
        while (true) {
            if (i >= size) {
                i = -1;
                break;
            } else if (r7 == this.f6571.get(i).f6587) {
                break;
            } else {
                i++;
            }
        }
        if (i >= 0) {
            int i2 = i + 1;
            if (i2 < this.f6571.size()) {
                this.f6571.get(i2).f6587.close(false);
            }
            C1930 remove = this.f6571.remove(i);
            remove.f6587.removeMenuPresenter(this);
            if (this.f6570) {
                remove.f6588.m325((Object) null);
                remove.f6588.m5188(0);
            }
            remove.f6588.dismiss();
            int size2 = this.f6571.size();
            if (size2 > 0) {
                this.f6569 = this.f6571.get(size2 - 1).f6586;
            } else {
                this.f6569 = C1729.m4511(this.f6556) == 1 ? 0 : 1;
            }
            if (size2 == 0) {
                dismiss();
                AbstractC1897.AbstractC1898 k = this.f6559;
                if (k != null) {
                    k.onCloseMenu(r7, true);
                }
                ViewTreeObserver viewTreeObserver = this.f6565;
                if (viewTreeObserver != null) {
                    if (viewTreeObserver.isAlive()) {
                        this.f6565.removeGlobalOnLayoutListener(this.f6562);
                    }
                    this.f6565 = null;
                }
                this.f6561.removeOnAttachStateChangeListener(this.f6567);
                this.f6566.onDismiss();
            } else if (z) {
                this.f6571.get(0).f6587.close(false);
            }
        }
    }

    public void onDismiss() {
        C1930 r3;
        int size = this.f6571.size();
        int i = 0;
        while (true) {
            if (i >= size) {
                r3 = null;
                break;
            }
            r3 = this.f6571.get(i);
            if (!r3.f6588.m5180()) {
                break;
            }
            i++;
        }
        if (r3 != null) {
            r3.f6587.close(false);
        }
    }

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

    @Override // ῲ.ῲ.ඓ.㳳.AbstractC1897
    public void onRestoreInstanceState(Parcelable parcelable) {
    }

    @Override // ῲ.ῲ.ඓ.㳳.AbstractC1897
    public Parcelable onSaveInstanceState() {
        return null;
    }

    @Override // ῲ.ῲ.ඓ.㳳.AbstractC1897
    public boolean onSubMenuSelected(SubMenuC1900 r5) {
        for (C1930 r1 : this.f6571) {
            if (r5 == r1.f6587) {
                r1.m4991().requestFocus();
                return true;
            }
        }
        if (!r5.hasVisibleItems()) {
            return false;
        }
        m4985(r5);
        AbstractC1897.AbstractC1898 k = this.f6559;
        if (k != null) {
            k.m4922(r5);
        }
        return true;
    }

    @Override // ῲ.ῲ.ඓ.㳳.AbstractC1897
    public void setCallback(AbstractC1897.AbstractC1898 k) {
        this.f6559 = k;
    }

    @Override // ῲ.ῲ.ඓ.㳳.AbstractC1916
    public void setOnDismissListener(PopupWindow.OnDismissListener onDismissListener) {
        this.f6566 = onDismissListener;
    }

    @Override // ῲ.ῲ.ඓ.㳳.AbstractC1903
    public void show() {
        if (!m4987()) {
            for (C1912 r1 : this.f6563) {
                m4979(r1);
            }
            this.f6563.clear();
            this.f6561 = this.f6556;
            if (this.f6561 != null) {
                boolean z = this.f6565 == null;
                this.f6565 = this.f6561.getViewTreeObserver();
                if (z) {
                    this.f6565.addOnGlobalLayoutListener(this.f6562);
                }
                this.f6561.addOnAttachStateChangeListener(this.f6567);
            }
        }
    }

    @Override // ῲ.ῲ.ඓ.㳳.AbstractC1897
    public void updateMenuView(boolean z) {
        for (C1930 r0 : this.f6571) {
            ListAdapter adapter = r0.m4991().getAdapter();
            if (adapter instanceof HeaderViewListAdapter) {
                adapter = ((HeaderViewListAdapter) adapter).getWrappedAdapter();
            }
            ((C1924) adapter).notifyDataSetChanged();
        }
    }

    /* JADX WARNING: Code restructure failed: missing block: B:42:0x0122, code lost:
        if (((r8.getWidth() + r12[0]) + r4) > r10.right) goto L_0x012d;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:44:0x0128, code lost:
        if ((r12[0] - r4) < 0) goto L_0x012a;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:47:0x012d, code lost:
        r8 = 0;
     */
    /* renamed from: ʬ  reason: contains not printable characters */
    public final void m4979(C1912 r17) {
        View view;
        C1930 r3;
        int i;
        int i2;
        int i3;
        MenuItem menuItem;
        int i4;
        C1924 r11;
        int firstVisiblePosition;
        LayoutInflater from = LayoutInflater.from(this.f6553);
        C1924 r32 = new C1924(r17, from, this.f6564, f6552);
        if (!m4987() && this.f6560) {
            r32.f6551 = true;
        } else if (m4987()) {
            r32.f6551 = AbstractC1916.m4941(r17);
        }
        int r4 = AbstractC1916.m4942(r32, null, this.f6553, this.f6575);
        MenuPopupWindow menuPopupWindow = new MenuPopupWindow(this.f6553, null, this.f6558, this.f6573);
        menuPopupWindow.m330(this.f6578);
        menuPopupWindow.setOnItemClickListener(this);
        menuPopupWindow.setOnDismissListener(this);
        menuPopupWindow.m5170(this.f6556);
        menuPopupWindow.m5186(this.f6568);
        menuPopupWindow.m5179(true);
        menuPopupWindow.m5190(2);
        menuPopupWindow.m5178((ListAdapter) r32);
        menuPopupWindow.m5163(r4);
        menuPopupWindow.m5186(this.f6568);
        if (this.f6571.size() > 0) {
            List<C1930> list = this.f6571;
            r3 = list.get(list.size() - 1);
            C1912 r10 = r3.f6587;
            int size = r10.size();
            int i5 = 0;
            while (true) {
                if (i5 >= size) {
                    menuItem = null;
                    break;
                }
                menuItem = r10.getItem(i5);
                if (menuItem.hasSubMenu() && r17 == menuItem.getSubMenu()) {
                    break;
                }
                i5++;
            }
            if (menuItem != null) {
                ListView r102 = r3.m4991();
                ListAdapter adapter = r102.getAdapter();
                if (adapter instanceof HeaderViewListAdapter) {
                    HeaderViewListAdapter headerViewListAdapter = (HeaderViewListAdapter) adapter;
                    i4 = headerViewListAdapter.getHeadersCount();
                    r11 = (C1924) headerViewListAdapter.getWrappedAdapter();
                } else {
                    r11 = (C1924) adapter;
                    i4 = 0;
                }
                int count = r11.getCount();
                int i6 = 0;
                while (true) {
                    if (i6 >= count) {
                        i6 = -1;
                        break;
                    } else if (menuItem == r11.getItem(i6)) {
                        break;
                    } else {
                        i6++;
                    }
                }
                if (i6 != -1 && (firstVisiblePosition = (i6 + i4) - r102.getFirstVisiblePosition()) >= 0 && firstVisiblePosition < r102.getChildCount()) {
                    view = r102.getChildAt(firstVisiblePosition);
                }
            }
            view = null;
        } else {
            r3 = null;
            view = null;
        }
        if (view != null) {
            menuPopupWindow.m324(false);
            menuPopupWindow.m328((Object) null);
            List<C1930> list2 = this.f6571;
            ListView r8 = list2.get(list2.size() - 1).m4991();
            int[] iArr = new int[2];
            r8.getLocationOnScreen(iArr);
            Rect rect = new Rect();
            this.f6561.getWindowVisibleDisplayFrame(rect);
            if (this.f6569 == 1) {
            }
            int i7 = 1;
            boolean z = i7 == 1;
            this.f6569 = i7;
            if (Build.VERSION.SDK_INT >= 26) {
                menuPopupWindow.m5170(view);
                i2 = 0;
                i = 0;
            } else {
                int[] iArr2 = new int[2];
                this.f6556.getLocationOnScreen(iArr2);
                int[] iArr3 = new int[2];
                view.getLocationOnScreen(iArr3);
                if ((this.f6568 & 7) == 5) {
                    iArr2[0] = this.f6556.getWidth() + iArr2[0];
                    iArr3[0] = view.getWidth() + iArr3[0];
                }
                i = iArr3[0] - iArr2[0];
                i2 = iArr3[1] - iArr2[1];
            }
            if ((this.f6568 & 5) != 5) {
                if (z) {
                    r4 = view.getWidth();
                }
                i3 = i - r4;
                menuPopupWindow.m5176(i3);
                menuPopupWindow.m5171(true);
                menuPopupWindow.m5169(i2);
            } else if (!z) {
                r4 = view.getWidth();
                i3 = i - r4;
                menuPopupWindow.m5176(i3);
                menuPopupWindow.m5171(true);
                menuPopupWindow.m5169(i2);
            }
            i3 = i + r4;
            menuPopupWindow.m5176(i3);
            menuPopupWindow.m5171(true);
            menuPopupWindow.m5169(i2);
        } else {
            if (this.f6557) {
                menuPopupWindow.m5176(this.f6555);
            }
            if (this.f6577) {
                menuPopupWindow.m5169(this.f6574);
            }
            menuPopupWindow.m5177(m4943());
        }
        this.f6571.add(new C1930(menuPopupWindow, r17, this.f6569));
        menuPopupWindow.show();
        ListView r42 = menuPopupWindow.m5185();
        r42.setOnKeyListener(this);
        if (r3 == null && this.f6572 && r17.getHeaderTitle() != null) {
            FrameLayout frameLayout = (FrameLayout) from.inflate(R$layout.abc_popup_menu_header_item_layout, (ViewGroup) r42, false);
            frameLayout.setEnabled(false);
            ((TextView) frameLayout.findViewById(16908310)).setText(r17.getHeaderTitle());
            r42.addHeaderView(frameLayout, null, false);
            menuPopupWindow.show();
        }
    }

    @Override // ῲ.ῲ.ඓ.㳳.AbstractC1916
    /* renamed from: ῲ  reason: contains not printable characters */
    public void m4980(int i) {
        this.f6557 = true;
        this.f6555 = i;
    }

    @Override // ῲ.ῲ.ඓ.㳳.AbstractC1916
    /* renamed from: ῲ  reason: contains not printable characters */
    public boolean m4982() {
        return false;
    }

    @Override // ῲ.ῲ.ඓ.㳳.AbstractC1916
    /* renamed from: K  reason: contains not printable characters */
    public void m4986(boolean z) {
        this.f6560 = z;
    }

    @Override // ῲ.ῲ.ඓ.㳳.AbstractC1903
    /* renamed from: 㴹  reason: contains not printable characters */
    public ListView m4988() {
        if (this.f6571.isEmpty()) {
            return null;
        }
        List<C1930> list = this.f6571;
        return list.get(list.size() - 1).m4991();
    }

    @Override // ῲ.ῲ.ඓ.㳳.AbstractC1916
    /* renamed from: K  reason: contains not printable characters */
    public void m4985(C1912 r2) {
        r2.addMenuPresenter(this, this.f6553);
        if (m4987()) {
            m4979(r2);
        } else {
            this.f6563.add(r2);
        }
    }

    @Override // ῲ.ῲ.ඓ.㳳.AbstractC1916
    /* renamed from: ῲ  reason: contains not printable characters */
    public void m4981(boolean z) {
        this.f6572 = z;
    }

    @Override // ῲ.ῲ.ඓ.㳳.AbstractC1903
    /* renamed from: K  reason: contains not printable characters */
    public boolean m4987() {
        return this.f6571.size() > 0 && this.f6571.get(0).f6588.m5180();
    }

    @Override // ῲ.ῲ.ඓ.㳳.AbstractC1916
    /* renamed from: K  reason: contains not printable characters */
    public void m4983(int i) {
        if (this.f6554 != i) {
            this.f6554 = i;
            this.f6568 = C2369.m6293(i, C1729.m4511(this.f6556));
        }
    }

    @Override // ῲ.ῲ.ඓ.㳳.AbstractC1916
    /* renamed from: K  reason: contains not printable characters */
    public void m4984(View view) {
        if (this.f6556 != view) {
            this.f6556 = view;
            this.f6568 = C2369.m6293(this.f6554, C1729.m4511(this.f6556));
        }
    }

    @Override // ῲ.ῲ.ඓ.㳳.AbstractC1916
    /* renamed from: ʬ  reason: contains not printable characters */
    public void m4978(int i) {
        this.f6577 = true;
        this.f6574 = i;
    }
}
