package c.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 com.seewo.vcommons.constants.Platform;
import java.lang.reflect.Method;
import java.util.WeakHashMap;

/* JADX INFO: loaded from: classes.dex */
public class z implements c.b.f.d.n {

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public static Method f5071c;
    public Rect A;
    public boolean B;
    public PopupWindow C;

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

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

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

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

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public int f5078j;
    public boolean l;
    public boolean m;
    public boolean n;
    public DataSetObserver r;
    public View s;
    public AdapterView.OnItemClickListener t;
    public final Handler y;

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

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public int f5076h = -2;
    public int k = FwUpgradeManager.TYPE_FW_SXCAM_MC1_32_CAMERA;
    public int o = 0;
    public int p = Platform.UNKNOWN;
    public int q = 0;
    public final e u = new e();
    public final d v = new d();
    public final c w = new c();
    public final a x = new a();
    public final Rect z = new Rect();

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

        @Override // java.lang.Runnable
        public void run() {
            u uVar = z.this.f5074f;
            if (uVar != null) {
                uVar.setListSelectionHidden(true);
                uVar.requestLayout();
            }
        }
    }

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

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

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

    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 ((z.this.C.getInputMethodMode() == 2) || z.this.C.getContentView() == null) {
                    return;
                }
                z zVar = z.this;
                zVar.y.removeCallbacks(zVar.u);
                z.this.u.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 x = (int) motionEvent.getX();
            int y = (int) motionEvent.getY();
            if (action == 0 && (popupWindow = z.this.C) != null && popupWindow.isShowing() && x >= 0 && x < z.this.C.getWidth() && y >= 0 && y < z.this.C.getHeight()) {
                z zVar = z.this;
                zVar.y.postDelayed(zVar.u, 250L);
                return false;
            }
            if (action != 1) {
                return false;
            }
            z zVar2 = z.this;
            zVar2.y.removeCallbacks(zVar2.u);
            return false;
        }
    }

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

        @Override // java.lang.Runnable
        public void run() {
            u uVar = z.this.f5074f;
            if (uVar != null) {
                WeakHashMap<View, c.e.i.q> weakHashMap = c.e.i.l.f5278a;
                if (!uVar.isAttachedToWindow() || z.this.f5074f.getCount() <= z.this.f5074f.getChildCount()) {
                    return;
                }
                int childCount = z.this.f5074f.getChildCount();
                z zVar = z.this;
                if (childCount <= zVar.p) {
                    zVar.C.setInputMethodMode(2);
                    z.this.b();
                }
            }
        }
    }

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

    public z(Context context, AttributeSet attributeSet, int i2, int i3) {
        this.f5072d = context;
        this.y = new Handler(context.getMainLooper());
        TypedArray typedArrayObtainStyledAttributes = context.obtainStyledAttributes(attributeSet, c.b.b.n, i2, i3);
        this.f5077i = typedArrayObtainStyledAttributes.getDimensionPixelOffset(0, 0);
        int dimensionPixelOffset = typedArrayObtainStyledAttributes.getDimensionPixelOffset(1, 0);
        this.f5078j = dimensionPixelOffset;
        if (dimensionPixelOffset != 0) {
            this.l = true;
        }
        typedArrayObtainStyledAttributes.recycle();
        k kVar = new k(context, attributeSet, i2, i3);
        this.C = kVar;
        kVar.setInputMethodMode(1);
    }

    @Override // c.b.f.d.n
    public void b() {
        int i2;
        int i3;
        int paddingBottom;
        u uVar;
        if (this.f5074f == null) {
            u uVarF = f(this.f5072d, !this.B);
            this.f5074f = uVarF;
            uVarF.setAdapter(this.f5073e);
            this.f5074f.setOnItemClickListener(this.t);
            this.f5074f.setFocusable(true);
            this.f5074f.setFocusableInTouchMode(true);
            this.f5074f.setOnItemSelectedListener(new y(this));
            this.f5074f.setOnScrollListener(this.w);
            this.C.setContentView(this.f5074f);
        }
        Drawable background = this.C.getBackground();
        if (background != null) {
            background.getPadding(this.z);
            Rect rect = this.z;
            int i4 = rect.top;
            i2 = rect.bottom + i4;
            if (!this.l) {
                this.f5078j = -i4;
            }
        } else {
            this.z.setEmpty();
            i2 = 0;
        }
        int maxAvailableHeight = this.C.getMaxAvailableHeight(this.s, this.f5078j, this.C.getInputMethodMode() == 2);
        if (this.f5075g == -1) {
            paddingBottom = maxAvailableHeight + i2;
        } else {
            int i5 = this.f5076h;
            if (i5 != -2) {
                i3 = 1073741824;
                if (i5 == -1) {
                    int i6 = this.f5072d.getResources().getDisplayMetrics().widthPixels;
                    Rect rect2 = this.z;
                    i5 = i6 - (rect2.left + rect2.right);
                }
            } else {
                int i7 = this.f5072d.getResources().getDisplayMetrics().widthPixels;
                Rect rect3 = this.z;
                i5 = i7 - (rect3.left + rect3.right);
                i3 = Integer.MIN_VALUE;
            }
            int iA = this.f5074f.a(View.MeasureSpec.makeMeasureSpec(i5, i3), maxAvailableHeight - 0, -1);
            paddingBottom = iA + (iA > 0 ? this.f5074f.getPaddingBottom() + this.f5074f.getPaddingTop() + i2 + 0 : 0);
        }
        boolean z = this.C.getInputMethodMode() == 2;
        this.C.setWindowLayoutType(this.k);
        if (this.C.isShowing()) {
            View view = this.s;
            WeakHashMap<View, c.e.i.q> weakHashMap = c.e.i.l.f5278a;
            if (view.isAttachedToWindow()) {
                int width = this.f5076h;
                if (width == -1) {
                    width = -1;
                } else if (width == -2) {
                    width = this.s.getWidth();
                }
                int i8 = this.f5075g;
                if (i8 == -1) {
                    if (!z) {
                        paddingBottom = -1;
                    }
                    if (z) {
                        this.C.setWidth(this.f5076h == -1 ? -1 : 0);
                        this.C.setHeight(0);
                    } else {
                        this.C.setWidth(this.f5076h == -1 ? -1 : 0);
                        this.C.setHeight(-1);
                    }
                } else if (i8 != -2) {
                    paddingBottom = i8;
                }
                this.C.setOutsideTouchable(true);
                this.C.update(this.s, this.f5077i, this.f5078j, width < 0 ? -1 : width, paddingBottom < 0 ? -1 : paddingBottom);
                return;
            }
            return;
        }
        int width2 = this.f5076h;
        if (width2 == -1) {
            width2 = -1;
        } else if (width2 == -2) {
            width2 = this.s.getWidth();
        }
        int i9 = this.f5075g;
        if (i9 == -1) {
            paddingBottom = -1;
        } else if (i9 != -2) {
            paddingBottom = i9;
        }
        this.C.setWidth(width2);
        this.C.setHeight(paddingBottom);
        if (Build.VERSION.SDK_INT <= 28) {
            Method method = f5070b;
            if (method != null) {
                try {
                    method.invoke(this.C, Boolean.TRUE);
                } catch (Exception unused) {
                    Log.i("ListPopupWindow", "Could not call setClipToScreenEnabled() on PopupWindow. Oh well.");
                }
            }
        } else {
            this.C.setIsClippedToScreen(true);
        }
        this.C.setOutsideTouchable(true);
        this.C.setTouchInterceptor(this.v);
        if (this.n) {
            this.C.setOverlapAnchor(this.m);
        }
        if (Build.VERSION.SDK_INT <= 28) {
            Method method2 = f5071c;
            if (method2 != null) {
                try {
                    method2.invoke(this.C, this.A);
                } catch (Exception e2) {
                    Log.e("ListPopupWindow", "Could not invoke setEpicenterBounds on PopupWindow", e2);
                }
            }
        } else {
            this.C.setEpicenterBounds(this.A);
        }
        this.C.showAsDropDown(this.s, this.f5077i, this.f5078j, this.o);
        this.f5074f.setSelection(-1);
        if ((!this.B || this.f5074f.isInTouchMode()) && (uVar = this.f5074f) != null) {
            uVar.setListSelectionHidden(true);
            uVar.requestLayout();
        }
        if (this.B) {
            return;
        }
        this.y.post(this.x);
    }

    @Override // c.b.f.d.n
    public void d() {
        this.C.dismiss();
        this.C.setContentView(null);
        this.f5074f = null;
        this.y.removeCallbacks(this.u);
    }

    @Override // c.b.f.d.n
    public ListView e() {
        return this.f5074f;
    }

    public u f(Context context, boolean z) {
        throw null;
    }

    public void g(ListAdapter listAdapter) {
        DataSetObserver dataSetObserver = this.r;
        if (dataSetObserver == null) {
            this.r = new b();
        } else {
            ListAdapter listAdapter2 = this.f5073e;
            if (listAdapter2 != null) {
                listAdapter2.unregisterDataSetObserver(dataSetObserver);
            }
        }
        this.f5073e = listAdapter;
        if (listAdapter != null) {
            listAdapter.registerDataSetObserver(this.r);
        }
        u uVar = this.f5074f;
        if (uVar != null) {
            uVar.setAdapter(this.f5073e);
        }
    }

    public void h(int i2) {
        Drawable background = this.C.getBackground();
        if (background == null) {
            this.f5076h = i2;
            return;
        }
        background.getPadding(this.z);
        Rect rect = this.z;
        this.f5076h = rect.left + rect.right + i2;
    }

    public void i(boolean z) {
        this.B = z;
        this.C.setFocusable(z);
    }

    @Override // c.b.f.d.n
    public boolean j() {
        return this.C.isShowing();
    }

    public void k(int i2) {
        this.f5078j = i2;
        this.l = true;
    }
}
