package d.b.g;

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.Build;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import com.cvte.vman.impl.FwUpgradeManager;
import d.h.j.y;
import io.netty.util.internal.shaded.org.jctools.util.Pow2;
import java.lang.reflect.Method;
import java.util.concurrent.atomic.AtomicInteger;

/* JADX INFO: loaded from: classes.dex */
public class m0 implements d.b.f.i.p {

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static Method f2022f;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public static Method f2023m;
    public DataSetObserver B;
    public View C;
    public AdapterView.OnItemClickListener D;
    public final Handler I;
    public Rect K;
    public boolean L;
    public PopupWindow M;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public Context f2024n;

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public ListAdapter f2025o;

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    public h0 f2026p;

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public int f2029s;

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public int f2030t;

    /* JADX INFO: renamed from: v, reason: collision with root package name */
    public boolean f2032v;

    /* JADX INFO: renamed from: w, reason: collision with root package name */
    public boolean f2033w;

    /* JADX INFO: renamed from: x, reason: collision with root package name */
    public boolean f2034x;

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public int f2027q = -2;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public int f2028r = -2;

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public int f2031u = FwUpgradeManager.TYPE_FW_SXCAM_MC1_32_CAMERA;

    /* JADX INFO: renamed from: y, reason: collision with root package name */
    public int f2035y = 0;

    /* JADX INFO: renamed from: z, reason: collision with root package name */
    public int f2036z = Integer.MAX_VALUE;
    public int A = 0;
    public final e E = new e();
    public final d F = new d();
    public final c G = new c();
    public final a H = new a();
    public final Rect J = new Rect();

    public class a implements Runnable {
        public a() {
        }

        @Override // java.lang.Runnable
        public void run() {
            h0 h0Var = m0.this.f2026p;
            if (h0Var != null) {
                h0Var.setListSelectionHidden(true);
                h0Var.requestLayout();
            }
        }
    }

    public class b extends DataSetObserver {
        public b() {
        }

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

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

    public class c implements AbsListView.OnScrollListener {
        public c() {
        }

        @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) {
                if ((m0.this.M.getInputMethodMode() == 2) || m0.this.M.getContentView() == null) {
                    return;
                }
                m0 m0Var = m0.this;
                m0Var.I.removeCallbacks(m0Var.E);
                m0.this.E.run();
            }
        }
    }

    public class d implements View.OnTouchListener {
        public d() {
        }

        @Override // android.view.View.OnTouchListener
        public boolean onTouch(View view, MotionEvent motionEvent) {
            PopupWindow popupWindow;
            int action = motionEvent.getAction();
            int x2 = (int) motionEvent.getX();
            int y2 = (int) motionEvent.getY();
            if (action == 0 && (popupWindow = m0.this.M) != null && popupWindow.isShowing() && x2 >= 0 && x2 < m0.this.M.getWidth() && y2 >= 0 && y2 < m0.this.M.getHeight()) {
                m0 m0Var = m0.this;
                m0Var.I.postDelayed(m0Var.E, 250L);
                return false;
            }
            if (action != 1) {
                return false;
            }
            m0 m0Var2 = m0.this;
            m0Var2.I.removeCallbacks(m0Var2.E);
            return false;
        }
    }

    public class e implements Runnable {
        public e() {
        }

        @Override // java.lang.Runnable
        public void run() {
            h0 h0Var = m0.this.f2026p;
            if (h0Var != null) {
                AtomicInteger atomicInteger = d.h.j.y.a;
                if (!y.f.b(h0Var) || m0.this.f2026p.getCount() <= m0.this.f2026p.getChildCount()) {
                    return;
                }
                int childCount = m0.this.f2026p.getChildCount();
                m0 m0Var = m0.this;
                if (childCount <= m0Var.f2036z) {
                    m0Var.M.setInputMethodMode(2);
                    m0.this.h();
                }
            }
        }
    }

    static {
        if (Build.VERSION.SDK_INT <= 28) {
            try {
                f2022f = PopupWindow.class.getDeclaredMethod("setClipToScreenEnabled", Boolean.TYPE);
            } catch (NoSuchMethodException unused) {
                Log.i("ListPopupWindow", "Could not find method setClipToScreenEnabled() on PopupWindow. Oh well.");
            }
            try {
                f2023m = PopupWindow.class.getDeclaredMethod("setEpicenterBounds", Rect.class);
            } catch (NoSuchMethodException unused2) {
                Log.i("ListPopupWindow", "Could not find method setEpicenterBounds(Rect) on PopupWindow. Oh well.");
            }
        }
    }

    public m0(Context context, AttributeSet attributeSet, int i2, int i3) {
        this.f2024n = context;
        this.I = new Handler(context.getMainLooper());
        TypedArray typedArrayObtainStyledAttributes = context.obtainStyledAttributes(attributeSet, d.b.b.f1541n, i2, i3);
        this.f2029s = typedArrayObtainStyledAttributes.getDimensionPixelOffset(0, 0);
        int dimensionPixelOffset = typedArrayObtainStyledAttributes.getDimensionPixelOffset(1, 0);
        this.f2030t = dimensionPixelOffset;
        if (dimensionPixelOffset != 0) {
            this.f2032v = true;
        }
        typedArrayObtainStyledAttributes.recycle();
        p pVar = new p(context, attributeSet, i2, i3);
        this.M = pVar;
        pVar.setInputMethodMode(1);
    }

    @Override // d.b.f.i.p
    public boolean a() {
        return this.M.isShowing();
    }

    public int b() {
        return this.f2029s;
    }

    public void d(int i2) {
        this.f2029s = i2;
    }

    @Override // d.b.f.i.p
    public void dismiss() {
        this.M.dismiss();
        this.M.setContentView(null);
        this.f2026p = null;
        this.I.removeCallbacks(this.E);
    }

    public Drawable g() {
        return this.M.getBackground();
    }

    @Override // d.b.f.i.p
    public void h() {
        int i2;
        int i3;
        int paddingBottom;
        h0 h0Var;
        if (this.f2026p == null) {
            h0 h0VarQ = q(this.f2024n, !this.L);
            this.f2026p = h0VarQ;
            h0VarQ.setAdapter(this.f2025o);
            this.f2026p.setOnItemClickListener(this.D);
            this.f2026p.setFocusable(true);
            this.f2026p.setFocusableInTouchMode(true);
            this.f2026p.setOnItemSelectedListener(new l0(this));
            this.f2026p.setOnScrollListener(this.G);
            this.M.setContentView(this.f2026p);
        }
        Drawable background = this.M.getBackground();
        if (background != null) {
            background.getPadding(this.J);
            Rect rect = this.J;
            int i4 = rect.top;
            i2 = rect.bottom + i4;
            if (!this.f2032v) {
                this.f2030t = -i4;
            }
        } else {
            this.J.setEmpty();
            i2 = 0;
        }
        int maxAvailableHeight = this.M.getMaxAvailableHeight(this.C, this.f2030t, this.M.getInputMethodMode() == 2);
        if (this.f2027q == -1) {
            paddingBottom = maxAvailableHeight + i2;
        } else {
            int i5 = this.f2028r;
            if (i5 != -2) {
                i3 = Pow2.MAX_POW2;
                if (i5 == -1) {
                    int i6 = this.f2024n.getResources().getDisplayMetrics().widthPixels;
                    Rect rect2 = this.J;
                    i5 = i6 - (rect2.left + rect2.right);
                }
            } else {
                int i7 = this.f2024n.getResources().getDisplayMetrics().widthPixels;
                Rect rect3 = this.J;
                i5 = i7 - (rect3.left + rect3.right);
                i3 = Integer.MIN_VALUE;
            }
            int iA = this.f2026p.a(View.MeasureSpec.makeMeasureSpec(i5, i3), maxAvailableHeight - 0, -1);
            paddingBottom = iA + (iA > 0 ? this.f2026p.getPaddingBottom() + this.f2026p.getPaddingTop() + i2 + 0 : 0);
        }
        boolean z2 = this.M.getInputMethodMode() == 2;
        d.h.k.k.d(this.M, this.f2031u);
        if (this.M.isShowing()) {
            View view = this.C;
            AtomicInteger atomicInteger = d.h.j.y.a;
            if (y.f.b(view)) {
                int width = this.f2028r;
                if (width == -1) {
                    width = -1;
                } else if (width == -2) {
                    width = this.C.getWidth();
                }
                int i8 = this.f2027q;
                if (i8 == -1) {
                    if (!z2) {
                        paddingBottom = -1;
                    }
                    if (z2) {
                        this.M.setWidth(this.f2028r == -1 ? -1 : 0);
                        this.M.setHeight(0);
                    } else {
                        this.M.setWidth(this.f2028r == -1 ? -1 : 0);
                        this.M.setHeight(-1);
                    }
                } else if (i8 != -2) {
                    paddingBottom = i8;
                }
                this.M.setOutsideTouchable(true);
                this.M.update(this.C, this.f2029s, this.f2030t, width < 0 ? -1 : width, paddingBottom < 0 ? -1 : paddingBottom);
                return;
            }
            return;
        }
        int width2 = this.f2028r;
        if (width2 == -1) {
            width2 = -1;
        } else if (width2 == -2) {
            width2 = this.C.getWidth();
        }
        int i9 = this.f2027q;
        if (i9 == -1) {
            paddingBottom = -1;
        } else if (i9 != -2) {
            paddingBottom = i9;
        }
        this.M.setWidth(width2);
        this.M.setHeight(paddingBottom);
        if (Build.VERSION.SDK_INT <= 28) {
            Method method = f2022f;
            if (method != null) {
                try {
                    method.invoke(this.M, Boolean.TRUE);
                } catch (Exception unused) {
                    Log.i("ListPopupWindow", "Could not call setClipToScreenEnabled() on PopupWindow. Oh well.");
                }
            }
        } else {
            this.M.setIsClippedToScreen(true);
        }
        this.M.setOutsideTouchable(true);
        this.M.setTouchInterceptor(this.F);
        if (this.f2034x) {
            d.h.k.k.c(this.M, this.f2033w);
        }
        if (Build.VERSION.SDK_INT <= 28) {
            Method method2 = f2023m;
            if (method2 != null) {
                try {
                    method2.invoke(this.M, this.K);
                } catch (Exception e2) {
                    Log.e("ListPopupWindow", "Could not invoke setEpicenterBounds on PopupWindow", e2);
                }
            }
        } else {
            this.M.setEpicenterBounds(this.K);
        }
        d.h.k.j.a(this.M, this.C, this.f2029s, this.f2030t, this.f2035y);
        this.f2026p.setSelection(-1);
        if ((!this.L || this.f2026p.isInTouchMode()) && (h0Var = this.f2026p) != null) {
            h0Var.setListSelectionHidden(true);
            h0Var.requestLayout();
        }
        if (this.L) {
            return;
        }
        this.I.post(this.H);
    }

    @Override // d.b.f.i.p
    public ListView j() {
        return this.f2026p;
    }

    public void k(Drawable drawable) {
        this.M.setBackgroundDrawable(drawable);
    }

    public void l(int i2) {
        this.f2030t = i2;
        this.f2032v = true;
    }

    public int o() {
        if (this.f2032v) {
            return this.f2030t;
        }
        return 0;
    }

    public void p(ListAdapter listAdapter) {
        DataSetObserver dataSetObserver = this.B;
        if (dataSetObserver == null) {
            this.B = new b();
        } else {
            ListAdapter listAdapter2 = this.f2025o;
            if (listAdapter2 != null) {
                listAdapter2.unregisterDataSetObserver(dataSetObserver);
            }
        }
        this.f2025o = listAdapter;
        if (listAdapter != null) {
            listAdapter.registerDataSetObserver(this.B);
        }
        h0 h0Var = this.f2026p;
        if (h0Var != null) {
            h0Var.setAdapter(this.f2025o);
        }
    }

    public h0 q(Context context, boolean z2) {
        return new h0(context, z2);
    }

    public void r(int i2) {
        Drawable background = this.M.getBackground();
        if (background == null) {
            this.f2028r = i2;
            return;
        }
        background.getPadding(this.J);
        Rect rect = this.J;
        this.f2028r = rect.left + rect.right + i2;
    }

    public void s(boolean z2) {
        this.L = z2;
        this.M.setFocusable(z2);
    }
}
