package defpackage;

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.strmr.ps.R;
import defpackage.go7;
import java.lang.reflect.Method;
import java.util.WeakHashMap;

/* JADX INFO: loaded from: classes.dex */
public class jb5 implements ir8 {
    public static final Method ac;
    public static final Method ad;
    public final Context a;
    public boolean aa;
    public final PopupWindow ab;
    public ListAdapter b;
    public cg2 c;
    public final int d;
    public int e;
    public int f;
    public int i;
    public final int j;
    public boolean k;
    public boolean l;
    public boolean m;
    public int n;
    public final int o;
    public DataSetObserver p;
    public View q;
    public AdapterView.OnItemClickListener r;
    public AdapterView.OnItemSelectedListener s;
    public final g t;
    public final f u;
    public final e v;
    public final c w;
    public final Handler x;
    public final Rect y;
    public Rect z;

    @l18
    public static class a {
        @wd2
        public static int a(PopupWindow popupWindow, View view, int i, boolean z) {
            return popupWindow.getMaxAvailableHeight(view, i, z);
        }
    }

    @l18
    public static class b {
        @wd2
        public static void a(PopupWindow popupWindow, Rect rect) {
            popupWindow.setEpicenterBounds(rect);
        }

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

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

        @Override // java.lang.Runnable
        public final void run() {
            cg2 cg2Var = jb5.this.c;
            if (cg2Var != null) {
                cg2Var.setListSelectionHidden(true);
                cg2Var.requestLayout();
            }
        }
    }

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

        @Override // android.database.DataSetObserver
        public final void onChanged() {
            jb5 jb5Var = jb5.this;
            if (jb5Var.ab.isShowing()) {
                jb5Var.show();
            }
        }

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

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

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

        @Override // android.widget.AbsListView.OnScrollListener
        public final void onScrollStateChanged(AbsListView absListView, int i) {
            jb5 jb5Var = jb5.this;
            g gVar = jb5Var.t;
            PopupWindow popupWindow = jb5Var.ab;
            if (i != 1 || popupWindow.getInputMethodMode() == 2 || popupWindow.getContentView() == null) {
                return;
            }
            jb5Var.x.removeCallbacks(gVar);
            gVar.run();
        }
    }

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

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

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

        @Override // java.lang.Runnable
        public final void run() {
            jb5 jb5Var = jb5.this;
            cg2 cg2Var = jb5Var.c;
            if (cg2Var != null) {
                WeakHashMap weakHashMap = sia.a;
                if (!cg2Var.isAttachedToWindow() || jb5Var.c.getCount() <= jb5Var.c.getChildCount() || jb5Var.c.getChildCount() > jb5Var.o) {
                    return;
                }
                jb5Var.ab.setInputMethodMode(2);
                jb5Var.show();
            }
        }
    }

    static {
        if (Build.VERSION.SDK_INT <= 28) {
            try {
                ac = PopupWindow.class.getDeclaredMethod("setClipToScreenEnabled", Boolean.TYPE);
            } catch (NoSuchMethodException unused) {
            }
            try {
                ad = PopupWindow.class.getDeclaredMethod("setEpicenterBounds", Rect.class);
            } catch (NoSuchMethodException unused2) {
            }
        }
    }

    public jb5(Context context) {
        this(context, null, R.attr.listPopupWindowStyle, 0);
    }

    @Override // defpackage.ir8
    public final boolean a() {
        return this.ab.isShowing();
    }

    public final int b() {
        return this.f;
    }

    public final void d(int i) {
        this.f = i;
    }

    @Override // defpackage.ir8
    public final void dismiss() {
        PopupWindow popupWindow = this.ab;
        popupWindow.dismiss();
        popupWindow.setContentView(null);
        this.c = null;
        this.x.removeCallbacks(this.t);
    }

    public final Drawable f() {
        return this.ab.getBackground();
    }

    public final void h(int i) {
        this.i = i;
        this.k = true;
    }

    public final int k() {
        if (this.k) {
            return this.i;
        }
        return 0;
    }

    public void l(ListAdapter listAdapter) {
        DataSetObserver dataSetObserver = this.p;
        if (dataSetObserver == null) {
            this.p = new d();
        } else {
            ListAdapter listAdapter2 = this.b;
            if (listAdapter2 != null) {
                listAdapter2.unregisterDataSetObserver(dataSetObserver);
            }
        }
        this.b = listAdapter;
        if (listAdapter != null) {
            listAdapter.registerDataSetObserver(this.p);
        }
        cg2 cg2Var = this.c;
        if (cg2Var != null) {
            cg2Var.setAdapter(this.b);
        }
    }

    @Override // defpackage.ir8
    public final ListView n() {
        return this.c;
    }

    public cg2 o(Context context, boolean z) {
        return new cg2(context, z);
    }

    public final void p(int i) {
        Drawable background = this.ab.getBackground();
        if (background == null) {
            this.e = i;
            return;
        }
        Rect rect = this.y;
        background.getPadding(rect);
        this.e = rect.left + rect.right + i;
    }

    public final void setBackgroundDrawable(Drawable drawable) {
        this.ab.setBackgroundDrawable(drawable);
    }

    @Override // defpackage.ir8
    public final void show() {
        int i;
        int paddingBottom;
        cg2 cg2Var;
        cg2 cg2Var2 = this.c;
        Context context = this.a;
        PopupWindow popupWindow = this.ab;
        if (cg2Var2 == null) {
            cg2 cg2VarO = o(context, !this.aa);
            this.c = cg2VarO;
            cg2VarO.setAdapter(this.b);
            this.c.setOnItemClickListener(this.r);
            this.c.setFocusable(true);
            this.c.setFocusableInTouchMode(true);
            this.c.setOnItemSelectedListener(new ib5(this));
            this.c.setOnScrollListener(this.v);
            AdapterView.OnItemSelectedListener onItemSelectedListener = this.s;
            if (onItemSelectedListener != null) {
                this.c.setOnItemSelectedListener(onItemSelectedListener);
            }
            popupWindow.setContentView(this.c);
        }
        Drawable background = popupWindow.getBackground();
        Rect rect = this.y;
        if (background != null) {
            background.getPadding(rect);
            int i2 = rect.top;
            i = rect.bottom + i2;
            if (!this.k) {
                this.i = -i2;
            }
        } else {
            rect.setEmpty();
            i = 0;
        }
        int iA = a.a(popupWindow, this.q, this.i, popupWindow.getInputMethodMode() == 2);
        int i3 = this.d;
        if (i3 == -1) {
            paddingBottom = iA + i;
        } else {
            int i4 = this.e;
            int iA2 = this.c.a(i4 != -2 ? i4 != -1 ? View.MeasureSpec.makeMeasureSpec(i4, 1073741824) : View.MeasureSpec.makeMeasureSpec(context.getResources().getDisplayMetrics().widthPixels - (rect.left + rect.right), 1073741824) : View.MeasureSpec.makeMeasureSpec(context.getResources().getDisplayMetrics().widthPixels - (rect.left + rect.right), Integer.MIN_VALUE), iA);
            paddingBottom = iA2 + (iA2 > 0 ? this.c.getPaddingBottom() + this.c.getPaddingTop() + i : 0);
        }
        boolean z = popupWindow.getInputMethodMode() == 2;
        popupWindow.setWindowLayoutType(this.j);
        if (popupWindow.isShowing()) {
            View view = this.q;
            WeakHashMap weakHashMap = sia.a;
            if (view.isAttachedToWindow()) {
                int width = this.e;
                if (width == -1) {
                    width = -1;
                } else if (width == -2) {
                    width = this.q.getWidth();
                }
                if (i3 == -1) {
                    i3 = z ? paddingBottom : -1;
                    if (z) {
                        popupWindow.setWidth(this.e == -1 ? -1 : 0);
                        popupWindow.setHeight(0);
                    } else {
                        popupWindow.setWidth(this.e == -1 ? -1 : 0);
                        popupWindow.setHeight(-1);
                    }
                } else if (i3 == -2) {
                    i3 = paddingBottom;
                }
                popupWindow.setOutsideTouchable(true);
                int i5 = width;
                View view2 = this.q;
                int i6 = this.f;
                int i7 = this.i;
                int i8 = i5 < 0 ? -1 : i5;
                if (i3 < 0) {
                    i3 = -1;
                }
                popupWindow.update(view2, i6, i7, i8, i3);
                return;
            }
            return;
        }
        int width2 = this.e;
        if (width2 == -1) {
            width2 = -1;
        } else if (width2 == -2) {
            width2 = this.q.getWidth();
        }
        if (i3 == -1) {
            i3 = -1;
        } else if (i3 == -2) {
            i3 = paddingBottom;
        }
        popupWindow.setWidth(width2);
        popupWindow.setHeight(i3);
        if (Build.VERSION.SDK_INT <= 28) {
            Method method = ac;
            if (method != null) {
                try {
                    method.invoke(popupWindow, Boolean.TRUE);
                } catch (Exception unused) {
                }
            }
        } else {
            b.b(popupWindow, true);
        }
        popupWindow.setOutsideTouchable(true);
        popupWindow.setTouchInterceptor(this.u);
        if (this.m) {
            popupWindow.setOverlapAnchor(this.l);
        }
        if (Build.VERSION.SDK_INT <= 28) {
            Method method2 = ad;
            if (method2 != null) {
                try {
                    method2.invoke(popupWindow, this.z);
                } catch (Exception e2) {
                    Log.e("ListPopupWindow", "Could not invoke setEpicenterBounds on PopupWindow", e2);
                }
            }
        } else {
            b.a(popupWindow, this.z);
        }
        popupWindow.showAsDropDown(this.q, this.f, this.i, this.n);
        this.c.setSelection(-1);
        if ((!this.aa || this.c.isInTouchMode()) && (cg2Var = this.c) != null) {
            cg2Var.setListSelectionHidden(true);
            cg2Var.requestLayout();
        }
        if (this.aa) {
            return;
        }
        this.x.post(this.w);
    }

    public jb5(Context context, AttributeSet attributeSet, int i, int i2) {
        this.d = -2;
        this.e = -2;
        this.j = 1002;
        this.n = 0;
        this.o = Integer.MAX_VALUE;
        this.t = new g();
        this.u = new f();
        this.v = new e();
        this.w = new c();
        this.y = new Rect();
        this.a = context;
        this.x = new Handler(context.getMainLooper());
        TypedArray typedArrayObtainStyledAttributes = context.obtainStyledAttributes(attributeSet, go7.m.p, i, 0);
        this.f = typedArrayObtainStyledAttributes.getDimensionPixelOffset(0, 0);
        int dimensionPixelOffset = typedArrayObtainStyledAttributes.getDimensionPixelOffset(1, 0);
        this.i = dimensionPixelOffset;
        if (dimensionPixelOffset != 0) {
            this.k = true;
        }
        typedArrayObtainStyledAttributes.recycle();
        yo yoVar = new yo(context, attributeSet, i, 0);
        TypedArray typedArrayObtainStyledAttributes2 = context.obtainStyledAttributes(attributeSet, go7.m.t, i, 0);
        fp9 fp9Var = new fp9(context, typedArrayObtainStyledAttributes2);
        if (typedArrayObtainStyledAttributes2.hasValue(2)) {
            yoVar.setOverlapAnchor(typedArrayObtainStyledAttributes2.getBoolean(2, false));
        }
        yoVar.setBackgroundDrawable(fp9Var.b(0));
        fp9Var.g();
        this.ab = yoVar;
        yoVar.setInputMethodMode(1);
    }
}
