package androidx.appcompat.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.database.DataSetObserver;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import io.netty.util.internal.shaded.org.jctools.util.Pow2;
import java.lang.reflect.Method;

/* JADX INFO: compiled from: ListPopupWindow.java */
/* JADX INFO: loaded from: classes.dex */
public class w implements androidx.appcompat.view.menu.q {

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private static Method f700c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private static Method f701d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private static Method f702e;
    private AdapterView.OnItemClickListener A;
    private AdapterView.OnItemSelectedListener B;
    final g C;
    private final f D;
    private final e E;
    private final c F;
    private Runnable G;
    final Handler H;
    private final Rect I;
    private Rect J;
    private boolean K;
    PopupWindow L;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private Context f703f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private ListAdapter f704g;
    s h;
    private int i;
    private int j;
    private int k;
    private int l;
    private int m;
    private boolean n;
    private boolean o;
    private boolean p;
    private boolean q;
    private int r;
    private boolean s;
    private boolean t;
    int u;
    private View v;
    private int w;
    private DataSetObserver x;
    private View y;
    private Drawable z;

    /* JADX INFO: compiled from: ListPopupWindow.java */
    class a implements Runnable {
        a() {
        }

        @Override // java.lang.Runnable
        public void run() {
            View viewG = w.this.g();
            if (viewG == null || viewG.getWindowToken() == null) {
                return;
            }
            w.this.h();
        }
    }

    /* JADX INFO: compiled from: ListPopupWindow.java */
    class b implements AdapterView.OnItemSelectedListener {
        b() {
        }

        @Override // android.widget.AdapterView.OnItemSelectedListener
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long j) {
            s sVar;
            if (i == -1 || (sVar = w.this.h) == null) {
                return;
            }
            sVar.setListSelectionHidden(false);
        }

        @Override // android.widget.AdapterView.OnItemSelectedListener
        public void onNothingSelected(AdapterView<?> adapterView) {
        }
    }

    /* JADX INFO: compiled from: ListPopupWindow.java */
    private class c implements Runnable {
        c() {
        }

        @Override // java.lang.Runnable
        public void run() {
            w.this.d();
        }
    }

    /* JADX INFO: compiled from: ListPopupWindow.java */
    private class d extends DataSetObserver {
        d() {
        }

        @Override // android.database.DataSetObserver
        public void onChanged() {
            if (w.this.e()) {
                w.this.h();
            }
        }

        @Override // android.database.DataSetObserver
        public void onInvalidated() {
            w.this.i();
        }
    }

    /* JADX INFO: compiled from: ListPopupWindow.java */
    private class e implements AbsListView.OnScrollListener {
        e() {
        }

        @Override // android.widget.AbsListView.OnScrollListener
        public void onScroll(AbsListView absListView, int i, int i2, int i3) {
        }

        @Override // android.widget.AbsListView.OnScrollListener
        public void onScrollStateChanged(AbsListView absListView, int i) {
            if (i != 1 || w.this.p() || w.this.L.getContentView() == null) {
                return;
            }
            w wVar = w.this;
            wVar.H.removeCallbacks(wVar.C);
            w.this.C.run();
        }
    }

    /* JADX INFO: compiled from: ListPopupWindow.java */
    private class f implements View.OnTouchListener {
        f() {
        }

        @Override // android.view.View.OnTouchListener
        public boolean onTouch(View view, MotionEvent motionEvent) {
            PopupWindow popupWindow;
            int action = motionEvent.getAction();
            int x = (int) motionEvent.getX();
            int y = (int) motionEvent.getY();
            if (action == 0 && (popupWindow = w.this.L) != null && popupWindow.isShowing() && x >= 0 && x < w.this.L.getWidth() && y >= 0 && y < w.this.L.getHeight()) {
                w wVar = w.this;
                wVar.H.postDelayed(wVar.C, 250L);
                return false;
            }
            if (action != 1) {
                return false;
            }
            w wVar2 = w.this;
            wVar2.H.removeCallbacks(wVar2.C);
            return false;
        }
    }

    /* JADX INFO: compiled from: ListPopupWindow.java */
    private class g implements Runnable {
        g() {
        }

        @Override // java.lang.Runnable
        public void run() {
            s sVar = w.this.h;
            if (sVar == null || !a.c.i.l.f(sVar) || w.this.h.getCount() <= w.this.h.getChildCount()) {
                return;
            }
            int childCount = w.this.h.getChildCount();
            w wVar = w.this;
            if (childCount <= wVar.u) {
                wVar.L.setInputMethodMode(2);
                w.this.h();
            }
        }
    }

    static {
        try {
            f700c = PopupWindow.class.getDeclaredMethod("setClipToScreenEnabled", Boolean.TYPE);
        } catch (NoSuchMethodException unused) {
            Log.i("ListPopupWindow", "Could not find method setClipToScreenEnabled() on PopupWindow. Oh well.");
        }
        try {
            f701d = PopupWindow.class.getDeclaredMethod("getMaxAvailableHeight", View.class, Integer.TYPE, Boolean.TYPE);
        } catch (NoSuchMethodException unused2) {
            Log.i("ListPopupWindow", "Could not find method getMaxAvailableHeight(View, int, boolean) on PopupWindow. Oh well.");
        }
        try {
            f702e = PopupWindow.class.getDeclaredMethod("setEpicenterBounds", Rect.class);
        } catch (NoSuchMethodException unused3) {
            Log.i("ListPopupWindow", "Could not find method setEpicenterBounds(Rect) on PopupWindow. Oh well.");
        }
    }

    public w(Context context, AttributeSet attributeSet, int i) {
        this(context, attributeSet, i, 0);
    }

    private void F(boolean z) {
        Method method = f700c;
        if (method != null) {
            try {
                method.invoke(this.L, Boolean.valueOf(z));
            } catch (Exception unused) {
                Log.i("ListPopupWindow", "Could not call setClipToScreenEnabled() on PopupWindow. Oh well.");
            }
        }
    }

    private int c() {
        int measuredHeight;
        int i;
        int iMakeMeasureSpec;
        View view;
        int i2;
        if (this.h == null) {
            Context context = this.f703f;
            this.G = new a();
            s sVarF = f(context, !this.K);
            this.h = sVarF;
            Drawable drawable = this.z;
            if (drawable != null) {
                sVarF.setSelector(drawable);
            }
            this.h.setAdapter(this.f704g);
            this.h.setOnItemClickListener(this.A);
            this.h.setFocusable(true);
            this.h.setFocusableInTouchMode(true);
            this.h.setOnItemSelectedListener(new b());
            this.h.setOnScrollListener(this.E);
            AdapterView.OnItemSelectedListener onItemSelectedListener = this.B;
            if (onItemSelectedListener != null) {
                this.h.setOnItemSelectedListener(onItemSelectedListener);
            }
            s sVar = this.h;
            View view2 = this.v;
            if (view2 != null) {
                LinearLayout linearLayout = new LinearLayout(context);
                linearLayout.setOrientation(1);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(-1, 0, 1.0f);
                int i3 = this.w;
                if (i3 == 0) {
                    linearLayout.addView(view2);
                    linearLayout.addView(sVar, layoutParams);
                } else if (i3 != 1) {
                    Log.e("ListPopupWindow", "Invalid hint position " + this.w);
                } else {
                    linearLayout.addView(sVar, layoutParams);
                    linearLayout.addView(view2);
                }
                int i4 = this.j;
                if (i4 >= 0) {
                    i2 = Integer.MIN_VALUE;
                } else {
                    i4 = 0;
                    i2 = 0;
                }
                view2.measure(View.MeasureSpec.makeMeasureSpec(i4, i2), 0);
                LinearLayout.LayoutParams layoutParams2 = (LinearLayout.LayoutParams) view2.getLayoutParams();
                measuredHeight = view2.getMeasuredHeight() + layoutParams2.topMargin + layoutParams2.bottomMargin;
                view = linearLayout;
            } else {
                measuredHeight = 0;
                view = sVar;
            }
            this.L.setContentView(view);
        } else {
            View view3 = this.v;
            if (view3 != null) {
                LinearLayout.LayoutParams layoutParams3 = (LinearLayout.LayoutParams) view3.getLayoutParams();
                measuredHeight = view3.getMeasuredHeight() + layoutParams3.topMargin + layoutParams3.bottomMargin;
            } else {
                measuredHeight = 0;
            }
        }
        Drawable background = this.L.getBackground();
        if (background != null) {
            background.getPadding(this.I);
            Rect rect = this.I;
            int i5 = rect.top;
            i = rect.bottom + i5;
            if (!this.n) {
                this.l = -i5;
            }
        } else {
            this.I.setEmpty();
            i = 0;
        }
        int iM = m(g(), this.l, this.L.getInputMethodMode() == 2);
        if (this.s || this.i == -1) {
            return iM + i;
        }
        int i6 = this.j;
        if (i6 == -2) {
            int i7 = this.f703f.getResources().getDisplayMetrics().widthPixels;
            Rect rect2 = this.I;
            iMakeMeasureSpec = View.MeasureSpec.makeMeasureSpec(i7 - (rect2.left + rect2.right), Integer.MIN_VALUE);
        } else if (i6 != -1) {
            iMakeMeasureSpec = View.MeasureSpec.makeMeasureSpec(i6, Pow2.MAX_POW2);
        } else {
            int i8 = this.f703f.getResources().getDisplayMetrics().widthPixels;
            Rect rect3 = this.I;
            iMakeMeasureSpec = View.MeasureSpec.makeMeasureSpec(i8 - (rect3.left + rect3.right), Pow2.MAX_POW2);
        }
        int iD = this.h.d(iMakeMeasureSpec, 0, -1, iM - measuredHeight, -1);
        if (iD > 0) {
            measuredHeight += i + this.h.getPaddingTop() + this.h.getPaddingBottom();
        }
        return iD + measuredHeight;
    }

    private int m(View view, int i, boolean z) {
        Method method = f701d;
        if (method != null) {
            try {
                return ((Integer) method.invoke(this.L, view, Integer.valueOf(i), Boolean.valueOf(z))).intValue();
            } catch (Exception unused) {
                Log.i("ListPopupWindow", "Could not call getMaxAvailableHeightMethod(View, int, boolean) on PopupWindow. Using the public version.");
            }
        }
        return this.L.getMaxAvailableHeight(view, i);
    }

    private void r() {
        View view = this.v;
        if (view != null) {
            ViewParent parent = view.getParent();
            if (parent instanceof ViewGroup) {
                ((ViewGroup) parent).removeView(this.v);
            }
        }
    }

    public void A(int i) {
        this.L.setInputMethodMode(i);
    }

    public void B(boolean z) {
        this.K = z;
        this.L.setFocusable(z);
    }

    public void C(PopupWindow.OnDismissListener onDismissListener) {
        this.L.setOnDismissListener(onDismissListener);
    }

    public void D(AdapterView.OnItemClickListener onItemClickListener) {
        this.A = onItemClickListener;
    }

    public void E(boolean z) {
        this.q = true;
        this.p = z;
    }

    public void G(int i) {
        this.w = i;
    }

    public void H(int i) {
        s sVar = this.h;
        if (!e() || sVar == null) {
            return;
        }
        sVar.setListSelectionHidden(false);
        sVar.setSelection(i);
        if (sVar.getChoiceMode() != 0) {
            sVar.setItemChecked(i, true);
        }
    }

    public void I(int i) {
        this.l = i;
        this.n = true;
    }

    public void J(int i) {
        this.j = i;
    }

    public void d() {
        s sVar = this.h;
        if (sVar != null) {
            sVar.setListSelectionHidden(true);
            sVar.requestLayout();
        }
    }

    @Override // androidx.appcompat.view.menu.q
    public boolean e() {
        return this.L.isShowing();
    }

    s f(Context context, boolean z) {
        return new s(context, z);
    }

    public View g() {
        return this.y;
    }

    @Override // androidx.appcompat.view.menu.q
    public void h() {
        int iC = c();
        boolean zP = p();
        androidx.core.widget.g.b(this.L, this.m);
        if (this.L.isShowing()) {
            if (a.c.i.l.f(g())) {
                int width = this.j;
                if (width == -1) {
                    width = -1;
                } else if (width == -2) {
                    width = g().getWidth();
                }
                int i = this.i;
                if (i == -1) {
                    if (!zP) {
                        iC = -1;
                    }
                    if (zP) {
                        this.L.setWidth(this.j == -1 ? -1 : 0);
                        this.L.setHeight(0);
                    } else {
                        this.L.setWidth(this.j == -1 ? -1 : 0);
                        this.L.setHeight(-1);
                    }
                } else if (i != -2) {
                    iC = i;
                }
                this.L.setOutsideTouchable((this.t || this.s) ? false : true);
                this.L.update(g(), this.k, this.l, width < 0 ? -1 : width, iC < 0 ? -1 : iC);
                return;
            }
            return;
        }
        int width2 = this.j;
        if (width2 == -1) {
            width2 = -1;
        } else if (width2 == -2) {
            width2 = g().getWidth();
        }
        int i2 = this.i;
        if (i2 == -1) {
            iC = -1;
        } else if (i2 != -2) {
            iC = i2;
        }
        this.L.setWidth(width2);
        this.L.setHeight(iC);
        F(true);
        this.L.setOutsideTouchable((this.t || this.s) ? false : true);
        this.L.setTouchInterceptor(this.D);
        if (this.q) {
            androidx.core.widget.g.a(this.L, this.p);
        }
        Method method = f702e;
        if (method != null) {
            try {
                method.invoke(this.L, this.J);
            } catch (Exception e2) {
                Log.e("ListPopupWindow", "Could not invoke setEpicenterBounds on PopupWindow", e2);
            }
        }
        androidx.core.widget.g.c(this.L, g(), this.k, this.l, this.r);
        this.h.setSelection(-1);
        if (!this.K || this.h.isInTouchMode()) {
            d();
        }
        if (this.K) {
            return;
        }
        this.H.post(this.F);
    }

    @Override // androidx.appcompat.view.menu.q
    public void i() {
        this.L.dismiss();
        r();
        this.L.setContentView(null);
        this.h = null;
        this.H.removeCallbacks(this.C);
    }

    public Drawable j() {
        return this.L.getBackground();
    }

    @Override // androidx.appcompat.view.menu.q
    public ListView k() {
        return this.h;
    }

    public int l() {
        return this.k;
    }

    public int n() {
        if (this.n) {
            return this.l;
        }
        return 0;
    }

    public int o() {
        return this.j;
    }

    public boolean p() {
        return this.L.getInputMethodMode() == 2;
    }

    public boolean q() {
        return this.K;
    }

    public void s(ListAdapter listAdapter) {
        DataSetObserver dataSetObserver = this.x;
        if (dataSetObserver == null) {
            this.x = new d();
        } else {
            ListAdapter listAdapter2 = this.f704g;
            if (listAdapter2 != null) {
                listAdapter2.unregisterDataSetObserver(dataSetObserver);
            }
        }
        this.f704g = listAdapter;
        if (listAdapter != null) {
            listAdapter.registerDataSetObserver(this.x);
        }
        s sVar = this.h;
        if (sVar != null) {
            sVar.setAdapter(this.f704g);
        }
    }

    public void t(View view) {
        this.y = view;
    }

    public void u(int i) {
        this.L.setAnimationStyle(i);
    }

    public void v(Drawable drawable) {
        this.L.setBackgroundDrawable(drawable);
    }

    public void w(int i) {
        Drawable background = this.L.getBackground();
        if (background == null) {
            J(i);
            return;
        }
        background.getPadding(this.I);
        Rect rect = this.I;
        this.j = rect.left + rect.right + i;
    }

    public void x(int i) {
        this.r = i;
    }

    public void y(Rect rect) {
        this.J = rect;
    }

    public void z(int i) {
        this.k = i;
    }

    public w(Context context, AttributeSet attributeSet, int i, int i2) {
        this.i = -2;
        this.j = -2;
        this.m = 1002;
        this.o = true;
        this.r = 0;
        this.s = false;
        this.t = false;
        this.u = Integer.MAX_VALUE;
        this.w = 0;
        this.C = new g();
        this.D = new f();
        this.E = new e();
        this.F = new c();
        this.I = new Rect();
        this.f703f = context;
        this.H = new Handler(context.getMainLooper());
        TypedArray typedArrayObtainStyledAttributes = context.obtainStyledAttributes(attributeSet, a.a.j.O0, i, i2);
        this.k = typedArrayObtainStyledAttributes.getDimensionPixelOffset(a.a.j.P0, 0);
        int dimensionPixelOffset = typedArrayObtainStyledAttributes.getDimensionPixelOffset(a.a.j.Q0, 0);
        this.l = dimensionPixelOffset;
        if (dimensionPixelOffset != 0) {
            this.n = true;
        }
        typedArrayObtainStyledAttributes.recycle();
        l lVar = new l(context, attributeSet, i, i2);
        this.L = lVar;
        lVar.setInputMethodMode(1);
    }
}
