jimu-decompiled/sources/androidx/appcompat/widget/ListPopupWindow.java
2025-05-13 19:24:51 +02:00

593 lines
19 KiB
Java

package androidx.appcompat.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 androidx.appcompat.R$styleable;
import androidx.appcompat.view.menu.ShowableListMenu;
import androidx.core.view.ViewCompat;
import androidx.core.widget.PopupWindowCompat;
import java.lang.reflect.Method;
/* loaded from: classes.dex */
public class ListPopupWindow implements ShowableListMenu {
private static Method G;
private static Method H;
private static Method I;
private final ListSelectorHider A;
final Handler B;
private final Rect C;
private Rect D;
private boolean E;
PopupWindow F;
private Context a;
private ListAdapter b;
DropDownListView c;
private int d;
private int e;
private int f;
private int g;
private int h;
private boolean i;
private boolean j;
private boolean k;
private int l;
private boolean m;
private boolean n;
int o;
private View p;
private int q;
private DataSetObserver r;
private View s;
private Drawable t;
private AdapterView.OnItemClickListener u;
private AdapterView.OnItemSelectedListener v;
final ResizePopupRunnable x;
private final PopupTouchInterceptor y;
private final PopupScrollListener z;
private class ListSelectorHider implements Runnable {
ListSelectorHider() {
}
@Override // java.lang.Runnable
public void run() {
ListPopupWindow.this.b();
}
}
private class PopupDataSetObserver extends DataSetObserver {
PopupDataSetObserver() {
}
@Override // android.database.DataSetObserver
public void onChanged() {
if (ListPopupWindow.this.c()) {
ListPopupWindow.this.a();
}
}
@Override // android.database.DataSetObserver
public void onInvalidated() {
ListPopupWindow.this.dismiss();
}
}
private class PopupScrollListener implements AbsListView.OnScrollListener {
PopupScrollListener() {
}
@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 || ListPopupWindow.this.j() || ListPopupWindow.this.F.getContentView() == null) {
return;
}
ListPopupWindow listPopupWindow = ListPopupWindow.this;
listPopupWindow.B.removeCallbacks(listPopupWindow.x);
ListPopupWindow.this.x.run();
}
}
private class PopupTouchInterceptor implements View.OnTouchListener {
PopupTouchInterceptor() {
}
@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 = ListPopupWindow.this.F) != null && popupWindow.isShowing() && x >= 0 && x < ListPopupWindow.this.F.getWidth() && y >= 0 && y < ListPopupWindow.this.F.getHeight()) {
ListPopupWindow listPopupWindow = ListPopupWindow.this;
listPopupWindow.B.postDelayed(listPopupWindow.x, 250L);
return false;
}
if (action != 1) {
return false;
}
ListPopupWindow listPopupWindow2 = ListPopupWindow.this;
listPopupWindow2.B.removeCallbacks(listPopupWindow2.x);
return false;
}
}
private class ResizePopupRunnable implements Runnable {
ResizePopupRunnable() {
}
@Override // java.lang.Runnable
public void run() {
DropDownListView dropDownListView = ListPopupWindow.this.c;
if (dropDownListView == null || !ViewCompat.w(dropDownListView) || ListPopupWindow.this.c.getCount() <= ListPopupWindow.this.c.getChildCount()) {
return;
}
int childCount = ListPopupWindow.this.c.getChildCount();
ListPopupWindow listPopupWindow = ListPopupWindow.this;
if (childCount <= listPopupWindow.o) {
listPopupWindow.F.setInputMethodMode(2);
ListPopupWindow.this.a();
}
}
}
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 ListPopupWindow(Context context, AttributeSet attributeSet, int i) {
this(context, attributeSet, i, 0);
}
private int l() {
int i;
int i2;
int makeMeasureSpec;
int i3;
if (this.c == null) {
Context context = this.a;
new Runnable() { // from class: androidx.appcompat.widget.ListPopupWindow.2
@Override // java.lang.Runnable
public void run() {
View e = ListPopupWindow.this.e();
if (e == null || e.getWindowToken() == null) {
return;
}
ListPopupWindow.this.a();
}
};
this.c = a(context, !this.E);
Drawable drawable = this.t;
if (drawable != null) {
this.c.setSelector(drawable);
}
this.c.setAdapter(this.b);
this.c.setOnItemClickListener(this.u);
this.c.setFocusable(true);
this.c.setFocusableInTouchMode(true);
this.c.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { // from class: androidx.appcompat.widget.ListPopupWindow.3
@Override // android.widget.AdapterView.OnItemSelectedListener
public void onItemSelected(AdapterView<?> adapterView, View view, int i4, long j) {
DropDownListView dropDownListView;
if (i4 == -1 || (dropDownListView = ListPopupWindow.this.c) == null) {
return;
}
dropDownListView.setListSelectionHidden(false);
}
@Override // android.widget.AdapterView.OnItemSelectedListener
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
this.c.setOnScrollListener(this.z);
AdapterView.OnItemSelectedListener onItemSelectedListener = this.v;
if (onItemSelectedListener != null) {
this.c.setOnItemSelectedListener(onItemSelectedListener);
}
View view = this.c;
View view2 = this.p;
if (view2 != null) {
LinearLayout linearLayout = new LinearLayout(context);
linearLayout.setOrientation(1);
ViewGroup.LayoutParams layoutParams = new LinearLayout.LayoutParams(-1, 0, 1.0f);
int i4 = this.q;
if (i4 == 0) {
linearLayout.addView(view2);
linearLayout.addView(view, layoutParams);
} else if (i4 != 1) {
Log.e("ListPopupWindow", "Invalid hint position " + this.q);
} else {
linearLayout.addView(view, layoutParams);
linearLayout.addView(view2);
}
int i5 = this.e;
if (i5 >= 0) {
i3 = Integer.MIN_VALUE;
} else {
i5 = 0;
i3 = 0;
}
view2.measure(View.MeasureSpec.makeMeasureSpec(i5, i3), 0);
LinearLayout.LayoutParams layoutParams2 = (LinearLayout.LayoutParams) view2.getLayoutParams();
i = view2.getMeasuredHeight() + layoutParams2.topMargin + layoutParams2.bottomMargin;
view = linearLayout;
} else {
i = 0;
}
this.F.setContentView(view);
} else {
View view3 = this.p;
if (view3 != null) {
LinearLayout.LayoutParams layoutParams3 = (LinearLayout.LayoutParams) view3.getLayoutParams();
i = view3.getMeasuredHeight() + layoutParams3.topMargin + layoutParams3.bottomMargin;
} else {
i = 0;
}
}
Drawable background = this.F.getBackground();
if (background != null) {
background.getPadding(this.C);
Rect rect = this.C;
int i6 = rect.top;
i2 = rect.bottom + i6;
if (!this.i) {
this.g = -i6;
}
} else {
this.C.setEmpty();
i2 = 0;
}
int a = a(e(), this.g, this.F.getInputMethodMode() == 2);
if (this.m || this.d == -1) {
return a + i2;
}
int i7 = this.e;
if (i7 == -2) {
int i8 = this.a.getResources().getDisplayMetrics().widthPixels;
Rect rect2 = this.C;
makeMeasureSpec = View.MeasureSpec.makeMeasureSpec(i8 - (rect2.left + rect2.right), Integer.MIN_VALUE);
} else if (i7 != -1) {
makeMeasureSpec = View.MeasureSpec.makeMeasureSpec(i7, 1073741824);
} else {
int i9 = this.a.getResources().getDisplayMetrics().widthPixels;
Rect rect3 = this.C;
makeMeasureSpec = View.MeasureSpec.makeMeasureSpec(i9 - (rect3.left + rect3.right), 1073741824);
}
int a2 = this.c.a(makeMeasureSpec, 0, -1, a - i, -1);
if (a2 > 0) {
i += i2 + this.c.getPaddingTop() + this.c.getPaddingBottom();
}
return a2 + i;
}
private void m() {
View view = this.p;
if (view != null) {
ViewParent parent = view.getParent();
if (parent instanceof ViewGroup) {
((ViewGroup) parent).removeView(this.p);
}
}
}
public void a(ListAdapter listAdapter) {
DataSetObserver dataSetObserver = this.r;
if (dataSetObserver == null) {
this.r = new PopupDataSetObserver();
} else {
ListAdapter listAdapter2 = this.b;
if (listAdapter2 != null) {
listAdapter2.unregisterDataSetObserver(dataSetObserver);
}
}
this.b = listAdapter;
if (listAdapter != null) {
listAdapter.registerDataSetObserver(this.r);
}
DropDownListView dropDownListView = this.c;
if (dropDownListView != null) {
dropDownListView.setAdapter(this.b);
}
}
public void b(int i) {
Drawable background = this.F.getBackground();
if (background == null) {
i(i);
return;
}
background.getPadding(this.C);
Rect rect = this.C;
this.e = rect.left + rect.right + i;
}
public void c(int i) {
this.l = i;
}
public void d(int i) {
this.f = i;
}
@Override // androidx.appcompat.view.menu.ShowableListMenu
public void dismiss() {
this.F.dismiss();
m();
this.F.setContentView(null);
this.c = null;
this.B.removeCallbacks(this.x);
}
public View e() {
return this.s;
}
public void f(int i) {
this.q = i;
}
public int g() {
return this.f;
}
public int h() {
if (this.i) {
return this.g;
}
return 0;
}
public int i() {
return this.e;
}
public boolean j() {
return this.F.getInputMethodMode() == 2;
}
public boolean k() {
return this.E;
}
public ListPopupWindow(Context context, AttributeSet attributeSet, int i, int i2) {
this.d = -2;
this.e = -2;
this.h = 1002;
this.l = 0;
this.m = false;
this.n = false;
this.o = Integer.MAX_VALUE;
this.q = 0;
this.x = new ResizePopupRunnable();
this.y = new PopupTouchInterceptor();
this.z = new PopupScrollListener();
this.A = new ListSelectorHider();
this.C = new Rect();
this.a = context;
this.B = new Handler(context.getMainLooper());
TypedArray obtainStyledAttributes = context.obtainStyledAttributes(attributeSet, R$styleable.ListPopupWindow, i, i2);
this.f = obtainStyledAttributes.getDimensionPixelOffset(R$styleable.ListPopupWindow_android_dropDownHorizontalOffset, 0);
this.g = obtainStyledAttributes.getDimensionPixelOffset(R$styleable.ListPopupWindow_android_dropDownVerticalOffset, 0);
if (this.g != 0) {
this.i = true;
}
obtainStyledAttributes.recycle();
this.F = new AppCompatPopupWindow(context, attributeSet, i, i2);
this.F.setInputMethodMode(1);
}
@Override // androidx.appcompat.view.menu.ShowableListMenu
public boolean c() {
return this.F.isShowing();
}
@Override // androidx.appcompat.view.menu.ShowableListMenu
public ListView d() {
return this.c;
}
public void e(int i) {
this.F.setInputMethodMode(i);
}
public Drawable f() {
return this.F.getBackground();
}
public void g(int i) {
DropDownListView dropDownListView = this.c;
if (!c() || dropDownListView == null) {
return;
}
dropDownListView.setListSelectionHidden(false);
dropDownListView.setSelection(i);
if (dropDownListView.getChoiceMode() != 0) {
dropDownListView.setItemChecked(i, true);
}
}
public void i(int i) {
this.e = i;
}
private void c(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.");
}
}
}
public void h(int i) {
this.g = i;
this.i = true;
}
public void b() {
DropDownListView dropDownListView = this.c;
if (dropDownListView != null) {
dropDownListView.setListSelectionHidden(true);
dropDownListView.requestLayout();
}
}
public void b(boolean z) {
this.k = true;
this.j = z;
}
public void a(boolean z) {
this.E = z;
this.F.setFocusable(z);
}
public void a(Drawable drawable) {
this.F.setBackgroundDrawable(drawable);
}
public void a(int i) {
this.F.setAnimationStyle(i);
}
public void a(View view) {
this.s = view;
}
public void a(Rect rect) {
this.D = rect;
}
public void a(AdapterView.OnItemClickListener onItemClickListener) {
this.u = onItemClickListener;
}
@Override // androidx.appcompat.view.menu.ShowableListMenu
public void a() {
int l = l();
boolean j = j();
PopupWindowCompat.a(this.F, this.h);
if (this.F.isShowing()) {
if (ViewCompat.w(e())) {
int i = this.e;
if (i == -1) {
i = -1;
} else if (i == -2) {
i = e().getWidth();
}
int i2 = this.d;
if (i2 == -1) {
if (!j) {
l = -1;
}
if (j) {
this.F.setWidth(this.e == -1 ? -1 : 0);
this.F.setHeight(0);
} else {
this.F.setWidth(this.e == -1 ? -1 : 0);
this.F.setHeight(-1);
}
} else if (i2 != -2) {
l = i2;
}
this.F.setOutsideTouchable((this.n || this.m) ? false : true);
this.F.update(e(), this.f, this.g, i < 0 ? -1 : i, l < 0 ? -1 : l);
return;
}
return;
}
int i3 = this.e;
if (i3 == -1) {
i3 = -1;
} else if (i3 == -2) {
i3 = e().getWidth();
}
int i4 = this.d;
if (i4 == -1) {
l = -1;
} else if (i4 != -2) {
l = i4;
}
this.F.setWidth(i3);
this.F.setHeight(l);
c(true);
this.F.setOutsideTouchable((this.n || this.m) ? false : true);
this.F.setTouchInterceptor(this.y);
if (this.k) {
PopupWindowCompat.a(this.F, this.j);
}
Method method = I;
if (method != null) {
try {
method.invoke(this.F, this.D);
} catch (Exception e) {
Log.e("ListPopupWindow", "Could not invoke setEpicenterBounds on PopupWindow", e);
}
}
PopupWindowCompat.a(this.F, e(), this.f, this.g, this.l);
this.c.setSelection(-1);
if (!this.E || this.c.isInTouchMode()) {
b();
}
if (this.E) {
return;
}
this.B.post(this.A);
}
public void a(PopupWindow.OnDismissListener onDismissListener) {
this.F.setOnDismissListener(onDismissListener);
}
DropDownListView a(Context context, boolean z) {
return new DropDownListView(context, z);
}
private int a(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);
}
}