package android.support.v7.widget;

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.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;

/* JADX INFO: compiled from: ListPopupWindow.java */
/* JADX INFO: loaded from: classes.dex */
public class h0 implements android.support.v7.view.menu.s {
    private static Method G;
    private static Method H;
    private static Method I;
    private final b A;
    final Handler B;
    private final Rect C;
    private Rect D;
    private boolean E;
    PopupWindow F;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private Context f1042b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private ListAdapter f1043c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    d0 f1044d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private int f1045e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private int f1046f;
    private int g;
    private int h;
    private int i;
    private boolean j;
    private boolean k;
    private boolean l;
    private int m;
    private boolean n;
    private boolean o;
    int p;
    private View q;
    private int r;
    private DataSetObserver s;
    private View t;
    private Drawable u;
    private AdapterView.OnItemClickListener v;
    private AdapterView.OnItemSelectedListener w;
    final f x;
    private final e y;
    private final d z;

    /* JADX INFO: compiled from: ListPopupWindow.java */
    class a implements AdapterView.OnItemSelectedListener {
        a() {
        }

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

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

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

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

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

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

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

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

        @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 || h0.this.o() || h0.this.F.getContentView() == null) {
                return;
            }
            h0 h0Var = h0.this;
            h0Var.B.removeCallbacks(h0Var.x);
            h0.this.x.run();
        }
    }

    /* JADX INFO: compiled from: ListPopupWindow.java */
    private class e implements View.OnTouchListener {
        e() {
        }

        @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 = h0.this.F) != null && popupWindow.isShowing() && x >= 0 && x < h0.this.F.getWidth() && y >= 0 && y < h0.this.F.getHeight()) {
                h0 h0Var = h0.this;
                h0Var.B.postDelayed(h0Var.x, 250L);
                return false;
            }
            if (action != 1) {
                return false;
            }
            h0 h0Var2 = h0.this;
            h0Var2.B.removeCallbacks(h0Var2.x);
            return false;
        }
    }

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

        @Override // java.lang.Runnable
        public void run() {
            d0 d0Var = h0.this.f1044d;
            if (d0Var == null || !a.b.c.g.p.l(d0Var) || h0.this.f1044d.getCount() <= h0.this.f1044d.getChildCount()) {
                return;
            }
            int childCount = h0.this.f1044d.getChildCount();
            h0 h0Var = h0.this;
            if (childCount <= h0Var.p) {
                h0Var.F.setInputMethodMode(2);
                h0.this.c();
            }
        }
    }

    static {
        try {
            G = PopupWindow.class.getDeclaredMethod("setClipToScreenEnabled", Boolean.TYPE);
        } catch (NoSuchMethodException unused) {
            Log.i("ListPopupWindow", "Could not find method setClipToScreenEnabled() on PopupWindow. Oh well.");
        }
        try {
            H = PopupWindow.class.getDeclaredMethod("getMaxAvailableHeight", View.class, Integer.TYPE, Boolean.TYPE);
        } catch (NoSuchMethodException unused2) {
            Log.i("ListPopupWindow", "Could not find method getMaxAvailableHeight(View, int, boolean) on PopupWindow. Oh well.");
        }
        try {
            I = PopupWindow.class.getDeclaredMethod("setEpicenterBounds", Rect.class);
        } catch (NoSuchMethodException unused3) {
            Log.i("ListPopupWindow", "Could not find method setEpicenterBounds(Rect) on PopupWindow. Oh well.");
        }
    }

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

    private void E(boolean z) {
        Method method = G;
        if (method != null) {
            try {
                method.invoke(this.F, Boolean.valueOf(z));
            } catch (Exception unused) {
                Log.i("ListPopupWindow", "Could not call setClipToScreenEnabled() on PopupWindow. Oh well.");
            }
        }
    }

    private int d() {
        int measuredHeight;
        int i;
        int iMakeMeasureSpec;
        View view;
        int i2;
        if (this.f1044d == null) {
            Context context = this.f1042b;
            d0 d0VarF = f(context, !this.E);
            this.f1044d = d0VarF;
            Drawable drawable = this.u;
            if (drawable != null) {
                d0VarF.setSelector(drawable);
            }
            this.f1044d.setAdapter(this.f1043c);
            this.f1044d.setOnItemClickListener(this.v);
            this.f1044d.setFocusable(true);
            this.f1044d.setFocusableInTouchMode(true);
            this.f1044d.setOnItemSelectedListener(new a());
            this.f1044d.setOnScrollListener(this.z);
            AdapterView.OnItemSelectedListener onItemSelectedListener = this.w;
            if (onItemSelectedListener != null) {
                this.f1044d.setOnItemSelectedListener(onItemSelectedListener);
            }
            d0 d0Var = this.f1044d;
            View view2 = this.q;
            if (view2 != null) {
                LinearLayout linearLayout = new LinearLayout(context);
                linearLayout.setOrientation(1);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(-1, 0, 1.0f);
                int i3 = this.r;
                if (i3 == 0) {
                    linearLayout.addView(view2);
                    linearLayout.addView(d0Var, layoutParams);
                } else if (i3 != 1) {
                    Log.e("ListPopupWindow", "Invalid hint position " + this.r);
                } else {
                    linearLayout.addView(d0Var, layoutParams);
                    linearLayout.addView(view2);
                }
                int i4 = this.f1046f;
                if (i4 >= 0) {
                    i2 = Integer.MIN_VALUE;
                } else {
                    i4 = 0;
                    i2 = 0;
                }
                view2.measure(View.MeasureSpec.makeMeasureSpec(i4, i2), 0);
                LinearLayout.LayoutParams layoutParams2 = (LinearLayout.LayoutParams) view2.getLayoutParams();
                measuredHeight = view2.getMeasuredHeight() + layoutParams2.topMargin + layoutParams2.bottomMargin;
                view = linearLayout;
            } else {
                measuredHeight = 0;
                view = d0Var;
            }
            this.F.setContentView(view);
        } else {
            View view3 = this.q;
            if (view3 != null) {
                LinearLayout.LayoutParams layoutParams3 = (LinearLayout.LayoutParams) view3.getLayoutParams();
                measuredHeight = view3.getMeasuredHeight() + layoutParams3.topMargin + layoutParams3.bottomMargin;
            } else {
                measuredHeight = 0;
            }
        }
        Drawable background = this.F.getBackground();
        if (background != null) {
            background.getPadding(this.C);
            Rect rect = this.C;
            int i5 = rect.top;
            i = rect.bottom + i5;
            if (!this.j) {
                this.h = -i5;
            }
        } else {
            this.C.setEmpty();
            i = 0;
        }
        int iL = l(g(), this.h, this.F.getInputMethodMode() == 2);
        if (this.n || this.f1045e == -1) {
            return iL + i;
        }
        int i6 = this.f1046f;
        if (i6 == -2) {
            int i7 = this.f1042b.getResources().getDisplayMetrics().widthPixels;
            Rect rect2 = this.C;
            iMakeMeasureSpec = View.MeasureSpec.makeMeasureSpec(i7 - (rect2.left + rect2.right), Integer.MIN_VALUE);
        } else if (i6 != -1) {
            iMakeMeasureSpec = View.MeasureSpec.makeMeasureSpec(i6, 1073741824);
        } else {
            int i8 = this.f1042b.getResources().getDisplayMetrics().widthPixels;
            Rect rect3 = this.C;
            iMakeMeasureSpec = View.MeasureSpec.makeMeasureSpec(i8 - (rect3.left + rect3.right), 1073741824);
        }
        int iD = this.f1044d.d(iMakeMeasureSpec, 0, -1, iL - measuredHeight, -1);
        if (iD > 0) {
            measuredHeight += i + this.f1044d.getPaddingTop() + this.f1044d.getPaddingBottom();
        }
        return iD + measuredHeight;
    }

    private int l(View view, int i, boolean z) {
        Method method = H;
        if (method != null) {
            try {
                return ((Integer) method.invoke(this.F, 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.F.getMaxAvailableHeight(view, i);
    }

    private void q() {
        View view = this.q;
        if (view != null) {
            ViewParent parent = view.getParent();
            if (parent instanceof ViewGroup) {
                ((ViewGroup) parent).removeView(this.q);
            }
        }
    }

    public void A(boolean z) {
        this.E = z;
        this.F.setFocusable(z);
    }

    public void B(PopupWindow.OnDismissListener onDismissListener) {
        this.F.setOnDismissListener(onDismissListener);
    }

    public void C(AdapterView.OnItemClickListener onItemClickListener) {
        this.v = onItemClickListener;
    }

    public void D(boolean z) {
        this.l = true;
        this.k = z;
    }

    public void F(int i) {
        this.r = i;
    }

    public void G(int i) {
        d0 d0Var = this.f1044d;
        if (!h() || d0Var == null) {
            return;
        }
        d0Var.setListSelectionHidden(false);
        d0Var.setSelection(i);
        if (d0Var.getChoiceMode() != 0) {
            d0Var.setItemChecked(i, true);
        }
    }

    public void H(int i) {
        this.h = i;
        this.j = true;
    }

    public void I(int i) {
        this.f1046f = i;
    }

    @Override // android.support.v7.view.menu.s
    public void c() {
        int iD = d();
        boolean zO = o();
        android.support.v4.widget.j.b(this.F, this.i);
        if (this.F.isShowing()) {
            if (a.b.c.g.p.l(g())) {
                int width = this.f1046f;
                if (width == -1) {
                    width = -1;
                } else if (width == -2) {
                    width = g().getWidth();
                }
                int i = this.f1045e;
                if (i == -1) {
                    if (!zO) {
                        iD = -1;
                    }
                    if (zO) {
                        this.F.setWidth(this.f1046f == -1 ? -1 : 0);
                        this.F.setHeight(0);
                    } else {
                        this.F.setWidth(this.f1046f == -1 ? -1 : 0);
                        this.F.setHeight(-1);
                    }
                } else if (i != -2) {
                    iD = i;
                }
                this.F.setOutsideTouchable((this.o || this.n) ? false : true);
                this.F.update(g(), this.g, this.h, width < 0 ? -1 : width, iD < 0 ? -1 : iD);
                return;
            }
            return;
        }
        int width2 = this.f1046f;
        if (width2 == -1) {
            width2 = -1;
        } else if (width2 == -2) {
            width2 = g().getWidth();
        }
        int i2 = this.f1045e;
        if (i2 == -1) {
            iD = -1;
        } else if (i2 != -2) {
            iD = i2;
        }
        this.F.setWidth(width2);
        this.F.setHeight(iD);
        E(true);
        this.F.setOutsideTouchable((this.o || this.n) ? false : true);
        this.F.setTouchInterceptor(this.y);
        if (this.l) {
            android.support.v4.widget.j.a(this.F, this.k);
        }
        Method method = I;
        if (method != null) {
            try {
                method.invoke(this.F, this.D);
            } catch (Exception e2) {
                Log.e("ListPopupWindow", "Could not invoke setEpicenterBounds on PopupWindow", e2);
            }
        }
        android.support.v4.widget.j.c(this.F, g(), this.g, this.h, this.m);
        this.f1044d.setSelection(-1);
        if (!this.E || this.f1044d.isInTouchMode()) {
            e();
        }
        if (this.E) {
            return;
        }
        this.B.post(this.A);
    }

    @Override // android.support.v7.view.menu.s
    public void dismiss() {
        this.F.dismiss();
        q();
        this.F.setContentView(null);
        this.f1044d = null;
        this.B.removeCallbacks(this.x);
    }

    public void e() {
        d0 d0Var = this.f1044d;
        if (d0Var != null) {
            d0Var.setListSelectionHidden(true);
            d0Var.requestLayout();
        }
    }

    d0 f(Context context, boolean z) {
        return new d0(context, z);
    }

    public View g() {
        return this.t;
    }

    @Override // android.support.v7.view.menu.s
    public boolean h() {
        return this.F.isShowing();
    }

    @Override // android.support.v7.view.menu.s
    public ListView i() {
        return this.f1044d;
    }

    public Drawable j() {
        return this.F.getBackground();
    }

    public int k() {
        return this.g;
    }

    public int m() {
        if (this.j) {
            return this.h;
        }
        return 0;
    }

    public int n() {
        return this.f1046f;
    }

    public boolean o() {
        return this.F.getInputMethodMode() == 2;
    }

    public boolean p() {
        return this.E;
    }

    public void r(ListAdapter listAdapter) {
        DataSetObserver dataSetObserver = this.s;
        if (dataSetObserver == null) {
            this.s = new c();
        } else {
            ListAdapter listAdapter2 = this.f1043c;
            if (listAdapter2 != null) {
                listAdapter2.unregisterDataSetObserver(dataSetObserver);
            }
        }
        this.f1043c = listAdapter;
        if (listAdapter != null) {
            listAdapter.registerDataSetObserver(this.s);
        }
        d0 d0Var = this.f1044d;
        if (d0Var != null) {
            d0Var.setAdapter(this.f1043c);
        }
    }

    public void s(View view) {
        this.t = view;
    }

    public void t(int i) {
        this.F.setAnimationStyle(i);
    }

    public void u(Drawable drawable) {
        this.F.setBackgroundDrawable(drawable);
    }

    public void v(int i) {
        Drawable background = this.F.getBackground();
        if (background == null) {
            I(i);
            return;
        }
        background.getPadding(this.C);
        Rect rect = this.C;
        this.f1046f = rect.left + rect.right + i;
    }

    public void w(int i) {
        this.m = i;
    }

    public void x(Rect rect) {
        this.D = rect;
    }

    public void y(int i) {
        this.g = i;
    }

    public void z(int i) {
        this.F.setInputMethodMode(i);
    }

    public h0(Context context, AttributeSet attributeSet, int i, int i2) {
        this.f1045e = -2;
        this.f1046f = -2;
        this.i = 1002;
        this.m = 0;
        this.n = false;
        this.o = false;
        this.p = Integer.MAX_VALUE;
        this.r = 0;
        this.x = new f();
        this.y = new e();
        this.z = new d();
        this.A = new b();
        this.C = new Rect();
        this.f1042b = context;
        this.B = new Handler(context.getMainLooper());
        TypedArray typedArrayObtainStyledAttributes = context.obtainStyledAttributes(attributeSet, a.b.d.a.j.ListPopupWindow, i, i2);
        this.g = typedArrayObtainStyledAttributes.getDimensionPixelOffset(a.b.d.a.j.ListPopupWindow_android_dropDownHorizontalOffset, 0);
        int dimensionPixelOffset = typedArrayObtainStyledAttributes.getDimensionPixelOffset(a.b.d.a.j.ListPopupWindow_android_dropDownVerticalOffset, 0);
        this.h = dimensionPixelOffset;
        if (dimensionPixelOffset != 0) {
            this.j = true;
        }
        typedArrayObtainStyledAttributes.recycle();
        q qVar = new q(context, attributeSet, i, i2);
        this.F = qVar;
        qVar.setInputMethodMode(1);
    }
}
