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 com.cvte.vman.impl.FwUpgradeManager;
import java.lang.reflect.Method;

/* JADX INFO: compiled from: ListPopupWindow.java */
/* JADX INFO: loaded from: classes.dex */
public class h0 implements androidx.appcompat.view.menu.s {
    private static Method H;
    private static Method I;
    private static Method J;
    private final d A;
    private final b B;
    final Handler C;
    private final Rect D;
    private Rect E;
    private boolean F;
    PopupWindow G;
    private Context b;
    private ListAdapter d;
    d0 e;
    private int f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private int f171g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    private int f172h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    private int f173i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    private int f174j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    private boolean f175k;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    private boolean f176l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    private boolean f177m;
    private int n;
    private boolean o;
    private boolean p;
    int q;
    private View r;
    private int s;
    private DataSetObserver t;
    private View u;
    private Drawable v;
    private AdapterView.OnItemClickListener w;
    private AdapterView.OnItemSelectedListener x;
    final f y;
    private final e z;

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

        @Override // android.widget.AdapterView.OnItemSelectedListener
        public void onItemSelected(AdapterView<?> adapterView, View view, int i2, long j2) {
            d0 d0Var;
            if (i2 == -1 || (d0Var = h0.this.e) == null) {
                return;
            }
            d0Var.setListSelectionHidden(false);
        }

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

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

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

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

        @Override // android.database.DataSetObserver
        public void onChanged() {
            if (h0.this.i()) {
                h0.this.b();
            }
        }

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

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

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

        @Override // android.widget.AbsListView.OnScrollListener
        public void onScrollStateChanged(AbsListView absListView, int i2) {
            if (i2 != 1 || h0.this.o() || h0.this.G.getContentView() == null) {
                return;
            }
            h0 h0Var = h0.this;
            h0Var.C.removeCallbacks(h0Var.y);
            h0.this.y.run();
        }
    }

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

        @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 = h0.this.G) != null && popupWindow.isShowing() && x >= 0 && x < h0.this.G.getWidth() && y >= 0 && y < h0.this.G.getHeight()) {
                h0 h0Var = h0.this;
                h0Var.C.postDelayed(h0Var.y, 250L);
                return false;
            }
            if (action != 1) {
                return false;
            }
            h0 h0Var2 = h0.this;
            h0Var2.C.removeCallbacks(h0Var2.y);
            return false;
        }
    }

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

        @Override // java.lang.Runnable
        public void run() {
            d0 d0Var = h0.this.e;
            if (d0Var == null || !g.e.j.o.l(d0Var) || h0.this.e.getCount() <= h0.this.e.getChildCount()) {
                return;
            }
            int childCount = h0.this.e.getChildCount();
            h0 h0Var = h0.this;
            if (childCount <= h0Var.q) {
                h0Var.G.setInputMethodMode(2);
                h0.this.b();
            }
        }
    }

    static {
        try {
            H = PopupWindow.class.getDeclaredMethod("setClipToScreenEnabled", Boolean.TYPE);
        } catch (NoSuchMethodException unused) {
            Log.i("ListPopupWindow", "Could not find method setClipToScreenEnabled() on PopupWindow. Oh well.");
        }
        try {
            I = 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 {
            J = PopupWindow.class.getDeclaredMethod("setEpicenterBounds", Rect.class);
        } catch (NoSuchMethodException unused3) {
            Log.i("ListPopupWindow", "Could not find method setEpicenterBounds(Rect) on PopupWindow. Oh well.");
        }
    }

    public h0(Context context, AttributeSet attributeSet, int i2) {
        this(context, attributeSet, i2, 0);
    }

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

    private int e() {
        int measuredHeight;
        int i2;
        int iMakeMeasureSpec;
        View view;
        int i3;
        if (this.e == null) {
            Context context = this.b;
            d0 d0VarG = g(context, !this.F);
            this.e = d0VarG;
            Drawable drawable = this.v;
            if (drawable != null) {
                d0VarG.setSelector(drawable);
            }
            this.e.setAdapter(this.d);
            this.e.setOnItemClickListener(this.w);
            this.e.setFocusable(true);
            this.e.setFocusableInTouchMode(true);
            this.e.setOnItemSelectedListener(new a());
            this.e.setOnScrollListener(this.A);
            AdapterView.OnItemSelectedListener onItemSelectedListener = this.x;
            if (onItemSelectedListener != null) {
                this.e.setOnItemSelectedListener(onItemSelectedListener);
            }
            d0 d0Var = this.e;
            View view2 = this.r;
            if (view2 != null) {
                LinearLayout linearLayout = new LinearLayout(context);
                linearLayout.setOrientation(1);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(-1, 0, 1.0f);
                int i4 = this.s;
                if (i4 == 0) {
                    linearLayout.addView(view2);
                    linearLayout.addView(d0Var, layoutParams);
                } else if (i4 != 1) {
                    Log.e("ListPopupWindow", "Invalid hint position " + this.s);
                } else {
                    linearLayout.addView(d0Var, layoutParams);
                    linearLayout.addView(view2);
                }
                int i5 = this.f171g;
                if (i5 >= 0) {
                    i3 = Integer.MIN_VALUE;
                } else {
                    i5 = 0;
                    i3 = 0;
                }
                view2.measure(View.MeasureSpec.makeMeasureSpec(i5, i3), 0);
                LinearLayout.LayoutParams layoutParams2 = (LinearLayout.LayoutParams) view2.getLayoutParams();
                measuredHeight = view2.getMeasuredHeight() + layoutParams2.topMargin + layoutParams2.bottomMargin;
                view = linearLayout;
            } else {
                measuredHeight = 0;
                view = d0Var;
            }
            this.G.setContentView(view);
        } else {
            View view3 = this.r;
            if (view3 != null) {
                LinearLayout.LayoutParams layoutParams3 = (LinearLayout.LayoutParams) view3.getLayoutParams();
                measuredHeight = view3.getMeasuredHeight() + layoutParams3.topMargin + layoutParams3.bottomMargin;
            } else {
                measuredHeight = 0;
            }
        }
        Drawable background = this.G.getBackground();
        if (background != null) {
            background.getPadding(this.D);
            Rect rect = this.D;
            int i6 = rect.top;
            i2 = rect.bottom + i6;
            if (!this.f175k) {
                this.f173i = -i6;
            }
        } else {
            this.D.setEmpty();
            i2 = 0;
        }
        int iL = l(h(), this.f173i, this.G.getInputMethodMode() == 2);
        if (this.o || this.f == -1) {
            return iL + i2;
        }
        int i7 = this.f171g;
        if (i7 == -2) {
            int i8 = this.b.getResources().getDisplayMetrics().widthPixels;
            Rect rect2 = this.D;
            iMakeMeasureSpec = View.MeasureSpec.makeMeasureSpec(i8 - (rect2.left + rect2.right), Integer.MIN_VALUE);
        } else if (i7 != -1) {
            iMakeMeasureSpec = View.MeasureSpec.makeMeasureSpec(i7, 1073741824);
        } else {
            int i9 = this.b.getResources().getDisplayMetrics().widthPixels;
            Rect rect3 = this.D;
            iMakeMeasureSpec = View.MeasureSpec.makeMeasureSpec(i9 - (rect3.left + rect3.right), 1073741824);
        }
        int iD = this.e.d(iMakeMeasureSpec, 0, -1, iL - measuredHeight, -1);
        if (iD > 0) {
            measuredHeight += i2 + this.e.getPaddingTop() + this.e.getPaddingBottom();
        }
        return iD + measuredHeight;
    }

    private int l(View view, int i2, boolean z) {
        Method method = I;
        if (method != null) {
            try {
                return ((Integer) method.invoke(this.G, view, Integer.valueOf(i2), 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.G.getMaxAvailableHeight(view, i2);
    }

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

    public void A(boolean z) {
        this.F = z;
        this.G.setFocusable(z);
    }

    public void B(PopupWindow.OnDismissListener onDismissListener) {
        this.G.setOnDismissListener(onDismissListener);
    }

    public void C(AdapterView.OnItemClickListener onItemClickListener) {
        this.w = onItemClickListener;
    }

    public void D(boolean z) {
        this.f177m = true;
        this.f176l = z;
    }

    public void F(int i2) {
        this.s = i2;
    }

    public void G(int i2) {
        d0 d0Var = this.e;
        if (!i() || d0Var == null) {
            return;
        }
        d0Var.setListSelectionHidden(false);
        d0Var.setSelection(i2);
        if (d0Var.getChoiceMode() != 0) {
            d0Var.setItemChecked(i2, true);
        }
    }

    public void H(int i2) {
        this.f173i = i2;
        this.f175k = true;
    }

    public void I(int i2) {
        this.f171g = i2;
    }

    @Override // androidx.appcompat.view.menu.s
    public void b() {
        int iE = e();
        boolean zO = o();
        androidx.core.widget.h.b(this.G, this.f174j);
        if (this.G.isShowing()) {
            if (g.e.j.o.l(h())) {
                int width = this.f171g;
                if (width == -1) {
                    width = -1;
                } else if (width == -2) {
                    width = h().getWidth();
                }
                int i2 = this.f;
                if (i2 == -1) {
                    if (!zO) {
                        iE = -1;
                    }
                    if (zO) {
                        this.G.setWidth(this.f171g == -1 ? -1 : 0);
                        this.G.setHeight(0);
                    } else {
                        this.G.setWidth(this.f171g == -1 ? -1 : 0);
                        this.G.setHeight(-1);
                    }
                } else if (i2 != -2) {
                    iE = i2;
                }
                this.G.setOutsideTouchable((this.p || this.o) ? false : true);
                this.G.update(h(), this.f172h, this.f173i, width < 0 ? -1 : width, iE < 0 ? -1 : iE);
                return;
            }
            return;
        }
        int width2 = this.f171g;
        if (width2 == -1) {
            width2 = -1;
        } else if (width2 == -2) {
            width2 = h().getWidth();
        }
        int i3 = this.f;
        if (i3 == -1) {
            iE = -1;
        } else if (i3 != -2) {
            iE = i3;
        }
        this.G.setWidth(width2);
        this.G.setHeight(iE);
        E(true);
        this.G.setOutsideTouchable((this.p || this.o) ? false : true);
        this.G.setTouchInterceptor(this.z);
        if (this.f177m) {
            androidx.core.widget.h.a(this.G, this.f176l);
        }
        Method method = J;
        if (method != null) {
            try {
                method.invoke(this.G, this.E);
            } catch (Exception e2) {
                Log.e("ListPopupWindow", "Could not invoke setEpicenterBounds on PopupWindow", e2);
            }
        }
        androidx.core.widget.h.c(this.G, h(), this.f172h, this.f173i, this.n);
        this.e.setSelection(-1);
        if (!this.F || this.e.isInTouchMode()) {
            f();
        }
        if (this.F) {
            return;
        }
        this.C.post(this.B);
    }

    @Override // androidx.appcompat.view.menu.s
    public ListView d() {
        return this.e;
    }

    @Override // androidx.appcompat.view.menu.s
    public void dismiss() {
        this.G.dismiss();
        q();
        this.G.setContentView(null);
        this.e = null;
        this.C.removeCallbacks(this.y);
    }

    public void f() {
        d0 d0Var = this.e;
        if (d0Var != null) {
            d0Var.setListSelectionHidden(true);
            d0Var.requestLayout();
        }
    }

    d0 g(Context context, boolean z) {
        return new d0(context, z);
    }

    public View h() {
        return this.u;
    }

    @Override // androidx.appcompat.view.menu.s
    public boolean i() {
        return this.G.isShowing();
    }

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

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

    public int m() {
        if (this.f175k) {
            return this.f173i;
        }
        return 0;
    }

    public int n() {
        return this.f171g;
    }

    public boolean o() {
        return this.G.getInputMethodMode() == 2;
    }

    public boolean p() {
        return this.F;
    }

    public void r(ListAdapter listAdapter) {
        DataSetObserver dataSetObserver = this.t;
        if (dataSetObserver == null) {
            this.t = new c();
        } else {
            ListAdapter listAdapter2 = this.d;
            if (listAdapter2 != null) {
                listAdapter2.unregisterDataSetObserver(dataSetObserver);
            }
        }
        this.d = listAdapter;
        if (listAdapter != null) {
            listAdapter.registerDataSetObserver(this.t);
        }
        d0 d0Var = this.e;
        if (d0Var != null) {
            d0Var.setAdapter(this.d);
        }
    }

    public void s(View view) {
        this.u = view;
    }

    public void t(int i2) {
        this.G.setAnimationStyle(i2);
    }

    public void u(Drawable drawable) {
        this.G.setBackgroundDrawable(drawable);
    }

    public void v(int i2) {
        Drawable background = this.G.getBackground();
        if (background == null) {
            I(i2);
            return;
        }
        background.getPadding(this.D);
        Rect rect = this.D;
        this.f171g = rect.left + rect.right + i2;
    }

    public void w(int i2) {
        this.n = i2;
    }

    public void x(Rect rect) {
        this.E = rect;
    }

    public void y(int i2) {
        this.f172h = i2;
    }

    public void z(int i2) {
        this.G.setInputMethodMode(i2);
    }

    public h0(Context context, AttributeSet attributeSet, int i2, int i3) {
        this.f = -2;
        this.f171g = -2;
        this.f174j = FwUpgradeManager.TYPE_FW_SXCAM_MC1_32_CAMERA;
        this.n = 0;
        this.o = false;
        this.p = false;
        this.q = Integer.MAX_VALUE;
        this.s = 0;
        this.y = new f();
        this.z = new e();
        this.A = new d();
        this.B = new b();
        this.D = new Rect();
        this.b = context;
        this.C = new Handler(context.getMainLooper());
        TypedArray typedArrayObtainStyledAttributes = context.obtainStyledAttributes(attributeSet, g.a.j.ListPopupWindow, i2, i3);
        this.f172h = typedArrayObtainStyledAttributes.getDimensionPixelOffset(g.a.j.ListPopupWindow_android_dropDownHorizontalOffset, 0);
        int dimensionPixelOffset = typedArrayObtainStyledAttributes.getDimensionPixelOffset(g.a.j.ListPopupWindow_android_dropDownVerticalOffset, 0);
        this.f173i = dimensionPixelOffset;
        if (dimensionPixelOffset != 0) {
            this.f175k = true;
        }
        typedArrayObtainStyledAttributes.recycle();
        q qVar = new q(context, attributeSet, i2, i3);
        this.G = qVar;
        qVar.setInputMethodMode(1);
    }
}
