package androidx.appcompat.view.menu;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Rect;
import android.os.Parcelable;
import android.view.Gravity;
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.AdapterView;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import androidx.appcompat.view.menu.o;
import com.strmr.ps.R;
import defpackage.cg2;
import defpackage.mu5;
import defpackage.sia;
import java.util.WeakHashMap;

/* JADX INFO: loaded from: classes.dex */
final class r extends m implements PopupWindow.OnDismissListener, AdapterView.OnItemClickListener, o, View.OnKeyListener {
    public final Context b;
    public final g c;
    public final f d;
    public final boolean e;
    public final int f;
    public final int i;
    public final mu5 j;
    public PopupWindow.OnDismissListener m;
    public View n;
    public View o;
    public o.a p;
    public ViewTreeObserver q;
    public boolean r;
    public boolean s;
    public int t;
    public boolean v;
    public final ViewTreeObserver.OnGlobalLayoutListener k = new a();
    public final View.OnAttachStateChangeListener l = new b();
    public int u = 0;

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

        @Override // android.view.ViewTreeObserver.OnGlobalLayoutListener
        public final void onGlobalLayout() {
            r rVar = r.this;
            mu5 mu5Var = rVar.j;
            if (!rVar.a() || mu5Var.aa) {
                return;
            }
            View view = rVar.o;
            if (view == null || !view.isShown()) {
                rVar.dismiss();
            } else {
                mu5Var.show();
            }
        }
    }

    public class b implements View.OnAttachStateChangeListener {
        public b() {
        }

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

        @Override // android.view.View.OnAttachStateChangeListener
        public final void onViewDetachedFromWindow(View view) {
            r rVar = r.this;
            ViewTreeObserver viewTreeObserver = rVar.q;
            if (viewTreeObserver != null) {
                if (!viewTreeObserver.isAlive()) {
                    rVar.q = view.getViewTreeObserver();
                }
                rVar.q.removeGlobalOnLayoutListener(rVar.k);
            }
            view.removeOnAttachStateChangeListener(this);
        }
    }

    public r(Context context, g gVar, View view, int i, boolean z) {
        this.b = context;
        this.c = gVar;
        this.e = z;
        this.d = new f(gVar, LayoutInflater.from(context), z, R.layout.abc_popup_menu_item_layout);
        this.i = i;
        Resources resources = context.getResources();
        this.f = Math.max(resources.getDisplayMetrics().widthPixels / 2, resources.getDimensionPixelSize(R.dimen.abc_config_prefDialogWidth));
        this.n = view;
        this.j = new mu5(context, null, i, 0);
        gVar.addMenuPresenter(this, context);
    }

    @Override // defpackage.ir8
    public final boolean a() {
        return !this.r && this.j.ab.isShowing();
    }

    @Override // androidx.appcompat.view.menu.m
    public final void b(g gVar) {
    }

    @Override // androidx.appcompat.view.menu.m
    public final void d(View view) {
        this.n = view;
    }

    @Override // defpackage.ir8
    public final void dismiss() {
        if (a()) {
            this.j.dismiss();
        }
    }

    @Override // androidx.appcompat.view.menu.m
    public final void e(boolean z) {
        this.d.c = z;
    }

    @Override // androidx.appcompat.view.menu.m
    public final void f(int i) {
        this.u = i;
    }

    @Override // androidx.appcompat.view.menu.o
    public final boolean flagActionItems() {
        return false;
    }

    @Override // androidx.appcompat.view.menu.m
    public final void g(int i) {
        this.j.f = i;
    }

    @Override // androidx.appcompat.view.menu.m
    public final void h(PopupWindow.OnDismissListener onDismissListener) {
        this.m = onDismissListener;
    }

    @Override // androidx.appcompat.view.menu.m
    public final void i(boolean z) {
        this.v = z;
    }

    @Override // androidx.appcompat.view.menu.m
    public final void j(int i) {
        this.j.h(i);
    }

    @Override // defpackage.ir8
    public final ListView n() {
        return this.j.c;
    }

    @Override // androidx.appcompat.view.menu.o
    public final void onCloseMenu(g gVar, boolean z) {
        if (gVar != this.c) {
            return;
        }
        dismiss();
        o.a aVar = this.p;
        if (aVar != null) {
            aVar.onCloseMenu(gVar, z);
        }
    }

    @Override // android.widget.PopupWindow.OnDismissListener
    public final void onDismiss() {
        this.r = true;
        this.c.close();
        ViewTreeObserver viewTreeObserver = this.q;
        if (viewTreeObserver != null) {
            if (!viewTreeObserver.isAlive()) {
                this.q = this.o.getViewTreeObserver();
            }
            this.q.removeGlobalOnLayoutListener(this.k);
            this.q = null;
        }
        this.o.removeOnAttachStateChangeListener(this.l);
        PopupWindow.OnDismissListener onDismissListener = this.m;
        if (onDismissListener != null) {
            onDismissListener.onDismiss();
        }
    }

    @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 // androidx.appcompat.view.menu.o
    public final void onRestoreInstanceState(Parcelable parcelable) {
    }

    @Override // androidx.appcompat.view.menu.o
    public final Parcelable onSaveInstanceState() {
        return null;
    }

    @Override // androidx.appcompat.view.menu.o
    public final boolean onSubMenuSelected(s sVar) {
        boolean z;
        if (sVar.hasVisibleItems()) {
            n nVar = new n(this.b, sVar, this.o, this.e, this.i, 0);
            o.a aVar = this.p;
            nVar.h = aVar;
            m mVar = nVar.i;
            if (mVar != null) {
                mVar.setCallback(aVar);
            }
            int size = sVar.size();
            int i = 0;
            while (true) {
                if (i >= size) {
                    z = false;
                    break;
                }
                MenuItem item = sVar.getItem(i);
                if (item.isVisible() && item.getIcon() != null) {
                    z = true;
                    break;
                }
                i++;
            }
            nVar.d(z);
            nVar.j = this.m;
            this.m = null;
            this.c.close(false);
            mu5 mu5Var = this.j;
            int width = mu5Var.f;
            int iK = mu5Var.k();
            int i2 = this.u;
            View view = this.n;
            WeakHashMap weakHashMap = sia.a;
            if ((Gravity.getAbsoluteGravity(i2, view.getLayoutDirection()) & 7) == 5) {
                width += this.n.getWidth();
            }
            if (!nVar.b()) {
                if (nVar.e != null) {
                    nVar.e(width, iK, true, true);
                }
            }
            o.a aVar2 = this.p;
            if (aVar2 != null) {
                aVar2.a(sVar);
            }
            return true;
        }
        return false;
    }

    @Override // androidx.appcompat.view.menu.o
    public final void setCallback(o.a aVar) {
        this.p = aVar;
    }

    @Override // defpackage.ir8
    public final void show() {
        View view;
        if (a()) {
            return;
        }
        if (this.r || (view = this.n) == null) {
            throw new IllegalStateException("StandardMenuPopup cannot be used without an anchor");
        }
        this.o = view;
        mu5 mu5Var = this.j;
        PopupWindow popupWindow = mu5Var.ab;
        PopupWindow popupWindow2 = mu5Var.ab;
        popupWindow.setOnDismissListener(this);
        mu5Var.r = this;
        mu5Var.aa = true;
        popupWindow2.setFocusable(true);
        View view2 = this.o;
        boolean z = this.q == null;
        ViewTreeObserver viewTreeObserver = view2.getViewTreeObserver();
        this.q = viewTreeObserver;
        if (z) {
            viewTreeObserver.addOnGlobalLayoutListener(this.k);
        }
        view2.addOnAttachStateChangeListener(this.l);
        mu5Var.q = view2;
        mu5Var.n = this.u;
        boolean z2 = this.s;
        Context context = this.b;
        f fVar = this.d;
        if (!z2) {
            this.t = m.c(fVar, context, this.f);
            this.s = true;
        }
        mu5Var.p(this.t);
        popupWindow2.setInputMethodMode(2);
        Rect rect = this.a;
        mu5Var.z = rect != null ? new Rect(rect) : null;
        mu5Var.show();
        cg2 cg2Var = mu5Var.c;
        cg2Var.setOnKeyListener(this);
        if (this.v) {
            g gVar = this.c;
            if (gVar.getHeaderTitle() != null) {
                FrameLayout frameLayout = (FrameLayout) LayoutInflater.from(context).inflate(R.layout.abc_popup_menu_header_item_layout, (ViewGroup) cg2Var, false);
                TextView textView = (TextView) frameLayout.findViewById(android.R.id.title);
                if (textView != null) {
                    textView.setText(gVar.getHeaderTitle());
                }
                frameLayout.setEnabled(false);
                cg2Var.addHeaderView(frameLayout, null, false);
            }
        }
        mu5Var.l(fVar);
        mu5Var.show();
    }

    @Override // androidx.appcompat.view.menu.o
    public final void updateMenuView(boolean z) {
        this.s = false;
        f fVar = this.d;
        if (fVar != null) {
            fVar.notifyDataSetChanged();
        }
    }
}
