package p028j;

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.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 java.lang.reflect.Method;
import p007c.C0494j;
import p023h0.C0829h1;
import p025i.InterfaceC1015g;
import p032k0.C1346z;

/* renamed from: j.m2 */
/* loaded from: classes.dex */
public class C1147m2 implements InterfaceC1015g {

    /* renamed from: J */
    public static Method f2779J;

    /* renamed from: K */
    public static Method f2780K;

    /* renamed from: L */
    public static Method f2781L;

    /* renamed from: A */
    public final View$OnTouchListenerC1155h f2782A;

    /* renamed from: B */
    public final C1154g f2783B;

    /* renamed from: C */
    public final RunnableC1152e f2784C;

    /* renamed from: D */
    public Runnable f2785D;

    /* renamed from: E */
    public final Handler f2786E;

    /* renamed from: F */
    public final Rect f2787F;

    /* renamed from: G */
    public Rect f2788G;

    /* renamed from: H */
    public boolean f2789H;

    /* renamed from: I */
    public PopupWindow f2790I;

    /* renamed from: d */
    public Context f2791d;

    /* renamed from: e */
    public ListAdapter f2792e;

    /* renamed from: f */
    public C1127k2 f2793f;

    /* renamed from: g */
    public int f2794g;

    /* renamed from: h */
    public int f2795h;

    /* renamed from: i */
    public int f2796i;

    /* renamed from: j */
    public int f2797j;

    /* renamed from: k */
    public int f2798k;

    /* renamed from: l */
    public boolean f2799l;

    /* renamed from: m */
    public boolean f2800m;

    /* renamed from: n */
    public boolean f2801n;

    /* renamed from: o */
    public int f2802o;

    /* renamed from: p */
    public boolean f2803p;

    /* renamed from: q */
    public boolean f2804q;

    /* renamed from: r */
    public int f2805r;

    /* renamed from: s */
    public View f2806s;

    /* renamed from: t */
    public int f2807t;

    /* renamed from: u */
    public DataSetObserver f2808u;

    /* renamed from: v */
    public View f2809v;

    /* renamed from: w */
    public Drawable f2810w;

    /* renamed from: x */
    public AdapterView.OnItemClickListener f2811x;

    /* renamed from: y */
    public AdapterView.OnItemSelectedListener f2812y;

    /* renamed from: z */
    public final RunnableC1156i f2813z;

    /* renamed from: j.m2$a */
    /* loaded from: classes.dex */
    public class RunnableC1148a implements Runnable {
        public RunnableC1148a() {
        }

        @Override // java.lang.Runnable
        public void run() {
            View m2014t = C1147m2.this.m2014t();
            if (m2014t == null || m2014t.getWindowToken() == null) {
                return;
            }
            C1147m2.this.mo2022f();
        }
    }

    /* renamed from: j.m2$b */
    /* loaded from: classes.dex */
    public class C1149b implements AdapterView.OnItemSelectedListener {
        public C1149b() {
        }

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

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

    /* renamed from: j.m2$c */
    /* loaded from: classes.dex */
    public static class C1150c {
        /* renamed from: a */
        public static int m2007a(PopupWindow popupWindow, View view, int i, boolean z) {
            int maxAvailableHeight;
            maxAvailableHeight = popupWindow.getMaxAvailableHeight(view, i, z);
            return maxAvailableHeight;
        }
    }

    /* renamed from: j.m2$d */
    /* loaded from: classes.dex */
    public static class C1151d {
        /* renamed from: a */
        public static void m2006a(PopupWindow popupWindow, Rect rect) {
            popupWindow.setEpicenterBounds(rect);
        }

        /* renamed from: b */
        public static void m2005b(PopupWindow popupWindow, boolean z) {
            popupWindow.setIsClippedToScreen(z);
        }
    }

    /* renamed from: j.m2$e */
    /* loaded from: classes.dex */
    public class RunnableC1152e implements Runnable {
        public RunnableC1152e() {
        }

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

    /* renamed from: j.m2$f */
    /* loaded from: classes.dex */
    public class C1153f extends DataSetObserver {
        public C1153f() {
        }

        @Override // android.database.DataSetObserver
        public void onChanged() {
            if (C1147m2.this.mo2024b()) {
                C1147m2.this.mo2022f();
            }
        }

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

    /* renamed from: j.m2$g */
    /* loaded from: classes.dex */
    public class C1154g implements AbsListView.OnScrollListener {
        public C1154g() {
        }

        @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 || C1147m2.this.m2011w() || C1147m2.this.f2790I.getContentView() == null) {
                return;
            }
            C1147m2 c1147m2 = C1147m2.this;
            c1147m2.f2786E.removeCallbacks(c1147m2.f2813z);
            C1147m2.this.f2813z.run();
        }
    }

    /* renamed from: j.m2$h */
    /* loaded from: classes.dex */
    public class View$OnTouchListenerC1155h implements View.OnTouchListener {
        public View$OnTouchListenerC1155h() {
        }

        @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 = C1147m2.this.f2790I) != null && popupWindow.isShowing() && x >= 0 && x < C1147m2.this.f2790I.getWidth() && y >= 0 && y < C1147m2.this.f2790I.getHeight()) {
                C1147m2 c1147m2 = C1147m2.this;
                c1147m2.f2786E.postDelayed(c1147m2.f2813z, 250L);
                return false;
            } else if (action == 1) {
                C1147m2 c1147m22 = C1147m2.this;
                c1147m22.f2786E.removeCallbacks(c1147m22.f2813z);
                return false;
            } else {
                return false;
            }
        }
    }

    /* renamed from: j.m2$i */
    /* loaded from: classes.dex */
    public class RunnableC1156i implements Runnable {
        public RunnableC1156i() {
        }

        @Override // java.lang.Runnable
        public void run() {
            C1127k2 c1127k2 = C1147m2.this.f2793f;
            if (c1127k2 == null || !C0829h1.m2862A(c1127k2) || C1147m2.this.f2793f.getCount() <= C1147m2.this.f2793f.getChildCount()) {
                return;
            }
            int childCount = C1147m2.this.f2793f.getChildCount();
            C1147m2 c1147m2 = C1147m2.this;
            if (childCount <= c1147m2.f2805r) {
                c1147m2.f2790I.setInputMethodMode(2);
                C1147m2.this.mo2022f();
            }
        }
    }

    static {
        if (Build.VERSION.SDK_INT <= 28) {
            try {
                f2779J = PopupWindow.class.getDeclaredMethod("setClipToScreenEnabled", Boolean.TYPE);
            } catch (NoSuchMethodException unused) {
                Log.i("ListPopupWindow", "Could not find method setClipToScreenEnabled() on PopupWindow. Oh well.");
            }
            try {
                f2781L = PopupWindow.class.getDeclaredMethod("setEpicenterBounds", Rect.class);
            } catch (NoSuchMethodException unused2) {
                Log.i("ListPopupWindow", "Could not find method setEpicenterBounds(Rect) on PopupWindow. Oh well.");
            }
        }
        if (Build.VERSION.SDK_INT <= 23) {
            try {
                f2780K = PopupWindow.class.getDeclaredMethod("getMaxAvailableHeight", View.class, Integer.TYPE, Boolean.TYPE);
            } catch (NoSuchMethodException unused3) {
                Log.i("ListPopupWindow", "Could not find method getMaxAvailableHeight(View, int, boolean) on PopupWindow. Oh well.");
            }
        }
    }

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

    public C1147m2(Context context, AttributeSet attributeSet, int i, int i2) {
        this.f2794g = -2;
        this.f2795h = -2;
        this.f2798k = 1002;
        this.f2802o = 0;
        this.f2803p = false;
        this.f2804q = false;
        this.f2805r = Integer.MAX_VALUE;
        this.f2807t = 0;
        this.f2813z = new RunnableC1156i();
        this.f2782A = new View$OnTouchListenerC1155h();
        this.f2783B = new C1154g();
        this.f2784C = new RunnableC1152e();
        this.f2787F = new Rect();
        this.f2791d = context;
        this.f2786E = new Handler(context.getMainLooper());
        TypedArray obtainStyledAttributes = context.obtainStyledAttributes(attributeSet, C0494j.f1858l1, i, i2);
        this.f2796i = obtainStyledAttributes.getDimensionPixelOffset(C0494j.f1863m1, 0);
        int dimensionPixelOffset = obtainStyledAttributes.getDimensionPixelOffset(C0494j.f1868n1, 0);
        this.f2797j = dimensionPixelOffset;
        if (dimensionPixelOffset != 0) {
            this.f2799l = true;
        }
        obtainStyledAttributes.recycle();
        C1193s c1193s = new C1193s(context, attributeSet, i, i2);
        this.f2790I = c1193s;
        c1193s.setInputMethodMode(1);
    }

    /* renamed from: A */
    public void m2038A(int i) {
        this.f2790I.setAnimationStyle(i);
    }

    /* renamed from: B */
    public void m2037B(int i) {
        Drawable background = this.f2790I.getBackground();
        if (background == null) {
            m2026M(i);
            return;
        }
        background.getPadding(this.f2787F);
        Rect rect = this.f2787F;
        this.f2795h = rect.left + rect.right + i;
    }

    /* renamed from: C */
    public void m2036C(int i) {
        this.f2802o = i;
    }

    /* renamed from: D */
    public void m2035D(Rect rect) {
        this.f2788G = rect != null ? new Rect(rect) : null;
    }

    /* renamed from: E */
    public void m2034E(int i) {
        this.f2790I.setInputMethodMode(i);
    }

    /* renamed from: F */
    public void m2033F(boolean z) {
        this.f2789H = z;
        this.f2790I.setFocusable(z);
    }

    /* renamed from: G */
    public void m2032G(PopupWindow.OnDismissListener onDismissListener) {
        this.f2790I.setOnDismissListener(onDismissListener);
    }

    /* renamed from: H */
    public void m2031H(AdapterView.OnItemClickListener onItemClickListener) {
        this.f2811x = onItemClickListener;
    }

    /* renamed from: I */
    public void m2030I(boolean z) {
        this.f2801n = true;
        this.f2800m = z;
    }

    /* renamed from: J */
    public final void m2029J(boolean z) {
        if (Build.VERSION.SDK_INT > 28) {
            C1151d.m2005b(this.f2790I, z);
            return;
        }
        Method method = f2779J;
        if (method != null) {
            try {
                method.invoke(this.f2790I, Boolean.valueOf(z));
            } catch (Exception unused) {
                Log.i("ListPopupWindow", "Could not call setClipToScreenEnabled() on PopupWindow. Oh well.");
            }
        }
    }

    /* renamed from: K */
    public void m2028K(int i) {
        this.f2807t = i;
    }

    /* renamed from: L */
    public void m2027L(int i) {
        C1127k2 c1127k2 = this.f2793f;
        if (!mo2024b() || c1127k2 == null) {
            return;
        }
        c1127k2.setListSelectionHidden(false);
        c1127k2.setSelection(i);
        if (c1127k2.getChoiceMode() != 0) {
            c1127k2.setItemChecked(i, true);
        }
    }

    /* renamed from: M */
    public void m2026M(int i) {
        this.f2795h = i;
    }

    /* renamed from: a */
    public void m2025a(int i) {
        this.f2796i = i;
    }

    @Override // p025i.InterfaceC1015g
    /* renamed from: b */
    public boolean mo2024b() {
        return this.f2790I.isShowing();
    }

    /* renamed from: c */
    public int m2023c() {
        return this.f2796i;
    }

    @Override // p025i.InterfaceC1015g
    public void dismiss() {
        this.f2790I.dismiss();
        m2009y();
        this.f2790I.setContentView(null);
        this.f2793f = null;
        this.f2786E.removeCallbacks(this.f2813z);
    }

    @Override // p025i.InterfaceC1015g
    /* renamed from: f */
    public void mo2022f() {
        int m2016q = m2016q();
        boolean m2011w = m2011w();
        C1346z.m1584b(this.f2790I, this.f2798k);
        boolean z = true;
        if (this.f2790I.isShowing()) {
            if (C0829h1.m2862A(m2014t())) {
                int i = this.f2795h;
                if (i == -1) {
                    i = -1;
                } else if (i == -2) {
                    i = m2014t().getWidth();
                }
                int i2 = this.f2794g;
                if (i2 == -1) {
                    if (!m2011w) {
                        m2016q = -1;
                    }
                    if (m2011w) {
                        this.f2790I.setWidth(this.f2795h == -1 ? -1 : 0);
                        this.f2790I.setHeight(0);
                    } else {
                        this.f2790I.setWidth(this.f2795h == -1 ? -1 : 0);
                        this.f2790I.setHeight(-1);
                    }
                } else if (i2 != -2) {
                    m2016q = i2;
                }
                this.f2790I.setOutsideTouchable((this.f2804q || this.f2803p) ? false : false);
                this.f2790I.update(m2014t(), this.f2796i, this.f2797j, i < 0 ? -1 : i, m2016q < 0 ? -1 : m2016q);
                return;
            }
            return;
        }
        int i3 = this.f2795h;
        if (i3 == -1) {
            i3 = -1;
        } else if (i3 == -2) {
            i3 = m2014t().getWidth();
        }
        int i4 = this.f2794g;
        if (i4 == -1) {
            m2016q = -1;
        } else if (i4 != -2) {
            m2016q = i4;
        }
        this.f2790I.setWidth(i3);
        this.f2790I.setHeight(m2016q);
        m2029J(true);
        this.f2790I.setOutsideTouchable((this.f2804q || this.f2803p) ? false : true);
        this.f2790I.setTouchInterceptor(this.f2782A);
        if (this.f2801n) {
            C1346z.m1585a(this.f2790I, this.f2800m);
        }
        if (Build.VERSION.SDK_INT <= 28) {
            Method method = f2781L;
            if (method != null) {
                try {
                    method.invoke(this.f2790I, this.f2788G);
                } catch (Exception e) {
                    Log.e("ListPopupWindow", "Could not invoke setEpicenterBounds on PopupWindow", e);
                }
            }
        } else {
            C1151d.m2006a(this.f2790I, this.f2788G);
        }
        C1346z.m1583c(this.f2790I, m2014t(), this.f2796i, this.f2797j, this.f2802o);
        this.f2793f.setSelection(-1);
        if (!this.f2789H || this.f2793f.isInTouchMode()) {
            m2015r();
        }
        if (this.f2789H) {
            return;
        }
        this.f2786E.post(this.f2784C);
    }

    /* renamed from: g */
    public int m2021g() {
        if (this.f2799l) {
            return this.f2797j;
        }
        return 0;
    }

    /* renamed from: i */
    public Drawable m2020i() {
        return this.f2790I.getBackground();
    }

    @Override // p025i.InterfaceC1015g
    /* renamed from: k */
    public ListView mo2019k() {
        return this.f2793f;
    }

    /* renamed from: m */
    public void m2018m(Drawable drawable) {
        this.f2790I.setBackgroundDrawable(drawable);
    }

    /* renamed from: n */
    public void m2017n(int i) {
        this.f2797j = i;
        this.f2799l = true;
    }

    /* renamed from: o */
    public void mo1865o(ListAdapter listAdapter) {
        DataSetObserver dataSetObserver = this.f2808u;
        if (dataSetObserver == null) {
            this.f2808u = new C1153f();
        } else {
            ListAdapter listAdapter2 = this.f2792e;
            if (listAdapter2 != null) {
                listAdapter2.unregisterDataSetObserver(dataSetObserver);
            }
        }
        this.f2792e = listAdapter;
        if (listAdapter != null) {
            listAdapter.registerDataSetObserver(this.f2808u);
        }
        C1127k2 c1127k2 = this.f2793f;
        if (c1127k2 != null) {
            c1127k2.setAdapter(this.f2792e);
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:52:0x0151  */
    /* renamed from: q */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final int m2016q() {
        int i;
        int i2;
        int makeMeasureSpec;
        int mo1848d;
        int i3;
        int i4 = Integer.MIN_VALUE;
        if (this.f2793f == null) {
            Context context = this.f2791d;
            this.f2785D = new RunnableC1148a();
            C1127k2 mo1852s = mo1852s(context, !this.f2789H);
            this.f2793f = mo1852s;
            Drawable drawable = this.f2810w;
            if (drawable != null) {
                mo1852s.setSelector(drawable);
            }
            this.f2793f.setAdapter(this.f2792e);
            this.f2793f.setOnItemClickListener(this.f2811x);
            this.f2793f.setFocusable(true);
            this.f2793f.setFocusableInTouchMode(true);
            this.f2793f.setOnItemSelectedListener(new C1149b());
            this.f2793f.setOnScrollListener(this.f2783B);
            AdapterView.OnItemSelectedListener onItemSelectedListener = this.f2812y;
            if (onItemSelectedListener != null) {
                this.f2793f.setOnItemSelectedListener(onItemSelectedListener);
            }
            C1127k2 c1127k2 = this.f2793f;
            View view = this.f2806s;
            if (view != null) {
                LinearLayout linearLayout = new LinearLayout(context);
                linearLayout.setOrientation(1);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(-1, 0, 1.0f);
                int i5 = this.f2807t;
                if (i5 == 0) {
                    linearLayout.addView(view);
                    linearLayout.addView(c1127k2, layoutParams);
                } else if (i5 != 1) {
                    Log.e("ListPopupWindow", "Invalid hint position " + this.f2807t);
                } else {
                    linearLayout.addView(c1127k2, layoutParams);
                    linearLayout.addView(view);
                }
                int i6 = this.f2795h;
                if (i6 >= 0) {
                    i3 = Integer.MIN_VALUE;
                } else {
                    i6 = 0;
                    i3 = 0;
                }
                view.measure(View.MeasureSpec.makeMeasureSpec(i6, i3), 0);
                LinearLayout.LayoutParams layoutParams2 = (LinearLayout.LayoutParams) view.getLayoutParams();
                i = view.getMeasuredHeight() + layoutParams2.topMargin + layoutParams2.bottomMargin;
                c1127k2 = linearLayout;
            } else {
                i = 0;
            }
            this.f2790I.setContentView(c1127k2);
        } else {
            ViewGroup viewGroup = (ViewGroup) this.f2790I.getContentView();
            View view2 = this.f2806s;
            if (view2 != null) {
                LinearLayout.LayoutParams layoutParams3 = (LinearLayout.LayoutParams) view2.getLayoutParams();
                i = view2.getMeasuredHeight() + layoutParams3.topMargin + layoutParams3.bottomMargin;
            } else {
                i = 0;
            }
        }
        Drawable background = this.f2790I.getBackground();
        if (background != null) {
            background.getPadding(this.f2787F);
            Rect rect = this.f2787F;
            int i7 = rect.top;
            i2 = rect.bottom + i7;
            if (!this.f2799l) {
                this.f2797j = -i7;
            }
        } else {
            this.f2787F.setEmpty();
            i2 = 0;
        }
        int m2013u = m2013u(m2014t(), this.f2797j, this.f2790I.getInputMethodMode() == 2);
        if (this.f2803p || this.f2794g == -1) {
            return m2013u + i2;
        }
        int i8 = this.f2795h;
        if (i8 != -2) {
            i4 = 1073741824;
            if (i8 != -1) {
                makeMeasureSpec = View.MeasureSpec.makeMeasureSpec(i8, 1073741824);
                mo1848d = this.f2793f.mo1848d(makeMeasureSpec, 0, -1, m2013u - i, -1);
                if (mo1848d > 0) {
                    i += i2 + this.f2793f.getPaddingTop() + this.f2793f.getPaddingBottom();
                }
                return mo1848d + i;
            }
        }
        int i9 = this.f2791d.getResources().getDisplayMetrics().widthPixels;
        Rect rect2 = this.f2787F;
        makeMeasureSpec = View.MeasureSpec.makeMeasureSpec(i9 - (rect2.left + rect2.right), i4);
        mo1848d = this.f2793f.mo1848d(makeMeasureSpec, 0, -1, m2013u - i, -1);
        if (mo1848d > 0) {
        }
        return mo1848d + i;
    }

    /* renamed from: r */
    public void m2015r() {
        C1127k2 c1127k2 = this.f2793f;
        if (c1127k2 != null) {
            c1127k2.setListSelectionHidden(true);
            c1127k2.requestLayout();
        }
    }

    /* renamed from: s */
    public C1127k2 mo1852s(Context context, boolean z) {
        return new C1127k2(context, z);
    }

    /* renamed from: t */
    public View m2014t() {
        return this.f2809v;
    }

    /* renamed from: u */
    public final int m2013u(View view, int i, boolean z) {
        if (Build.VERSION.SDK_INT <= 23) {
            Method method = f2780K;
            if (method != null) {
                try {
                    return ((Integer) method.invoke(this.f2790I, 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.f2790I.getMaxAvailableHeight(view, i);
        }
        return C1150c.m2007a(this.f2790I, view, i, z);
    }

    /* renamed from: v */
    public int m2012v() {
        return this.f2795h;
    }

    /* renamed from: w */
    public boolean m2011w() {
        return this.f2790I.getInputMethodMode() == 2;
    }

    /* renamed from: x */
    public boolean m2010x() {
        return this.f2789H;
    }

    /* renamed from: y */
    public final void m2009y() {
        View view = this.f2806s;
        if (view != null) {
            ViewParent parent = view.getParent();
            if (parent instanceof ViewGroup) {
                ((ViewGroup) parent).removeView(this.f2806s);
            }
        }
    }

    /* renamed from: z */
    public void m2008z(View view) {
        this.f2809v = view;
    }
}