package c.b.f;

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 c.f.j.a0;
import java.lang.reflect.Method;
import java.util.WeakHashMap;

/* JADX INFO: compiled from: ListPopupWindow.java */
/* JADX INFO: loaded from: classes.dex */
public class q0 implements c.b.e.i.p {

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

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public static Method f934e;
    public final Handler A;
    public Rect C;
    public boolean D;
    public PopupWindow E;

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

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

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public l0 f937h;

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

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public int f941l;
    public boolean n;
    public boolean o;
    public boolean p;
    public DataSetObserver t;
    public View u;
    public AdapterView.OnItemClickListener v;

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

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public int f939j = -2;
    public int m = 1002;
    public int q = 0;
    public int r = Integer.MAX_VALUE;
    public int s = 0;
    public final g w = new g();
    public final f x = new f();
    public final e y = new e();
    public final c z = new c();
    public final Rect B = new Rect();

    /* JADX INFO: compiled from: ListPopupWindow.java */
    public static class a {
        public static int a(PopupWindow popupWindow, View view, int i2, boolean z) {
            return popupWindow.getMaxAvailableHeight(view, i2, z);
        }
    }

    /* JADX INFO: compiled from: ListPopupWindow.java */
    public static class b {
        public static void a(PopupWindow popupWindow, Rect rect) {
            popupWindow.setEpicenterBounds(rect);
        }

        public static void b(PopupWindow popupWindow, boolean z) {
            popupWindow.setIsClippedToScreen(z);
        }
    }

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

        @Override // java.lang.Runnable
        public void run() {
            l0 l0Var = q0.this.f937h;
            if (l0Var != null) {
                l0Var.setListSelectionHidden(true);
                l0Var.requestLayout();
            }
        }
    }

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

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

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

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

        @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 ((q0.this.E.getInputMethodMode() == 2) || q0.this.E.getContentView() == null) {
                    return;
                }
                q0 q0Var = q0.this;
                q0Var.A.removeCallbacks(q0Var.w);
                q0.this.w.run();
            }
        }
    }

    /* JADX INFO: compiled from: ListPopupWindow.java */
    public class f implements View.OnTouchListener {
        public 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 = q0.this.E) != null && popupWindow.isShowing() && x >= 0 && x < q0.this.E.getWidth() && y >= 0 && y < q0.this.E.getHeight()) {
                q0 q0Var = q0.this;
                q0Var.A.postDelayed(q0Var.w, 250L);
                return false;
            }
            if (action != 1) {
                return false;
            }
            q0 q0Var2 = q0.this;
            q0Var2.A.removeCallbacks(q0Var2.w);
            return false;
        }
    }

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

        @Override // java.lang.Runnable
        public void run() {
            l0 l0Var = q0.this.f937h;
            if (l0Var != null) {
                WeakHashMap<View, c.f.j.i0> weakHashMap = c.f.j.a0.a;
                if (!a0.f.b(l0Var) || q0.this.f937h.getCount() <= q0.this.f937h.getChildCount()) {
                    return;
                }
                int childCount = q0.this.f937h.getChildCount();
                q0 q0Var = q0.this;
                if (childCount <= q0Var.r) {
                    q0Var.E.setInputMethodMode(2);
                    q0.this.h();
                }
            }
        }
    }

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

    public q0(Context context, AttributeSet attributeSet, int i2, int i3) {
        this.f935f = context;
        this.A = new Handler(context.getMainLooper());
        TypedArray typedArrayObtainStyledAttributes = context.obtainStyledAttributes(attributeSet, c.b.b.o, i2, i3);
        this.f940k = typedArrayObtainStyledAttributes.getDimensionPixelOffset(0, 0);
        int dimensionPixelOffset = typedArrayObtainStyledAttributes.getDimensionPixelOffset(1, 0);
        this.f941l = dimensionPixelOffset;
        if (dimensionPixelOffset != 0) {
            this.n = true;
        }
        typedArrayObtainStyledAttributes.recycle();
        u uVar = new u(context, attributeSet, i2, i3);
        this.E = uVar;
        uVar.setInputMethodMode(1);
    }

    @Override // c.b.e.i.p
    public boolean a() {
        return this.E.isShowing();
    }

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

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

    @Override // c.b.e.i.p
    public void dismiss() {
        this.E.dismiss();
        this.E.setContentView(null);
        this.f937h = null;
        this.A.removeCallbacks(this.w);
    }

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

    @Override // c.b.e.i.p
    public void h() {
        int i2;
        int iMakeMeasureSpec;
        int paddingBottom;
        l0 l0Var;
        if (this.f937h == null) {
            l0 l0VarQ = q(this.f935f, !this.D);
            this.f937h = l0VarQ;
            l0VarQ.setAdapter(this.f936g);
            this.f937h.setOnItemClickListener(this.v);
            this.f937h.setFocusable(true);
            this.f937h.setFocusableInTouchMode(true);
            this.f937h.setOnItemSelectedListener(new p0(this));
            this.f937h.setOnScrollListener(this.y);
            this.E.setContentView(this.f937h);
        }
        Drawable background = this.E.getBackground();
        if (background != null) {
            background.getPadding(this.B);
            Rect rect = this.B;
            int i3 = rect.top;
            i2 = rect.bottom + i3;
            if (!this.n) {
                this.f941l = -i3;
            }
        } else {
            this.B.setEmpty();
            i2 = 0;
        }
        int iA = a.a(this.E, this.u, this.f941l, this.E.getInputMethodMode() == 2);
        if (this.f938i == -1) {
            paddingBottom = iA + i2;
        } else {
            int i4 = this.f939j;
            if (i4 == -2) {
                int i5 = this.f935f.getResources().getDisplayMetrics().widthPixels;
                Rect rect2 = this.B;
                iMakeMeasureSpec = View.MeasureSpec.makeMeasureSpec(i5 - (rect2.left + rect2.right), Integer.MIN_VALUE);
            } else if (i4 != -1) {
                iMakeMeasureSpec = View.MeasureSpec.makeMeasureSpec(i4, 1073741824);
            } else {
                int i6 = this.f935f.getResources().getDisplayMetrics().widthPixels;
                Rect rect3 = this.B;
                iMakeMeasureSpec = View.MeasureSpec.makeMeasureSpec(i6 - (rect3.left + rect3.right), 1073741824);
            }
            int iA2 = this.f937h.a(iMakeMeasureSpec, iA - 0, -1);
            paddingBottom = iA2 + (iA2 > 0 ? this.f937h.getPaddingBottom() + this.f937h.getPaddingTop() + i2 + 0 : 0);
        }
        boolean z = this.E.getInputMethodMode() == 2;
        c.f.k.j.d(this.E, this.m);
        if (this.E.isShowing()) {
            View view = this.u;
            WeakHashMap<View, c.f.j.i0> weakHashMap = c.f.j.a0.a;
            if (a0.f.b(view)) {
                int width = this.f939j;
                if (width == -1) {
                    width = -1;
                } else if (width == -2) {
                    width = this.u.getWidth();
                }
                int i7 = this.f938i;
                if (i7 == -1) {
                    if (!z) {
                        paddingBottom = -1;
                    }
                    if (z) {
                        this.E.setWidth(this.f939j == -1 ? -1 : 0);
                        this.E.setHeight(0);
                    } else {
                        this.E.setWidth(this.f939j == -1 ? -1 : 0);
                        this.E.setHeight(-1);
                    }
                } else if (i7 != -2) {
                    paddingBottom = i7;
                }
                this.E.setOutsideTouchable(true);
                this.E.update(this.u, this.f940k, this.f941l, width < 0 ? -1 : width, paddingBottom < 0 ? -1 : paddingBottom);
                return;
            }
            return;
        }
        int width2 = this.f939j;
        if (width2 == -1) {
            width2 = -1;
        } else if (width2 == -2) {
            width2 = this.u.getWidth();
        }
        int i8 = this.f938i;
        if (i8 == -1) {
            paddingBottom = -1;
        } else if (i8 != -2) {
            paddingBottom = i8;
        }
        this.E.setWidth(width2);
        this.E.setHeight(paddingBottom);
        if (Build.VERSION.SDK_INT <= 28) {
            Method method = f933b;
            if (method != null) {
                try {
                    method.invoke(this.E, Boolean.TRUE);
                } catch (Exception unused) {
                    Log.i("ListPopupWindow", "Could not call setClipToScreenEnabled() on PopupWindow. Oh well.");
                }
            }
        } else {
            b.b(this.E, true);
        }
        this.E.setOutsideTouchable(true);
        this.E.setTouchInterceptor(this.x);
        if (this.p) {
            c.f.k.j.c(this.E, this.o);
        }
        if (Build.VERSION.SDK_INT <= 28) {
            Method method2 = f934e;
            if (method2 != null) {
                try {
                    method2.invoke(this.E, this.C);
                } catch (Exception e2) {
                    Log.e("ListPopupWindow", "Could not invoke setEpicenterBounds on PopupWindow", e2);
                }
            }
        } else {
            b.a(this.E, this.C);
        }
        c.f.k.i.a(this.E, this.u, this.f940k, this.f941l, this.q);
        this.f937h.setSelection(-1);
        if ((!this.D || this.f937h.isInTouchMode()) && (l0Var = this.f937h) != null) {
            l0Var.setListSelectionHidden(true);
            l0Var.requestLayout();
        }
        if (this.D) {
            return;
        }
        this.A.post(this.z);
    }

    @Override // c.b.e.i.p
    public ListView j() {
        return this.f937h;
    }

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

    public void l(int i2) {
        this.f941l = i2;
        this.n = true;
    }

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

    public void p(ListAdapter listAdapter) {
        DataSetObserver dataSetObserver = this.t;
        if (dataSetObserver == null) {
            this.t = new d();
        } else {
            ListAdapter listAdapter2 = this.f936g;
            if (listAdapter2 != null) {
                listAdapter2.unregisterDataSetObserver(dataSetObserver);
            }
        }
        this.f936g = listAdapter;
        if (listAdapter != null) {
            listAdapter.registerDataSetObserver(this.t);
        }
        l0 l0Var = this.f937h;
        if (l0Var != null) {
            l0Var.setAdapter(this.f936g);
        }
    }

    public l0 q(Context context, boolean z) {
        return new l0(context, z);
    }

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

    public void s(boolean z) {
        this.D = z;
        this.E.setFocusable(z);
    }
}
