250 lines
8.5 KiB
Java
250 lines
8.5 KiB
Java
package androidx.appcompat.view.menu;
|
|
|
|
import android.content.Context;
|
|
import android.os.Bundle;
|
|
import android.os.Parcelable;
|
|
import android.util.SparseArray;
|
|
import android.view.ContextThemeWrapper;
|
|
import android.view.LayoutInflater;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
import android.widget.AdapterView;
|
|
import android.widget.BaseAdapter;
|
|
import android.widget.ListAdapter;
|
|
import androidx.annotation.RestrictTo;
|
|
import androidx.appcompat.R;
|
|
import androidx.appcompat.view.menu.MenuPresenter;
|
|
import androidx.appcompat.view.menu.MenuView;
|
|
import java.util.ArrayList;
|
|
@RestrictTo({RestrictTo.Scope.LIBRARY_GROUP_PREFIX})
|
|
public class ListMenuPresenter implements MenuPresenter, AdapterView.OnItemClickListener {
|
|
private static final String TAG = "ListMenuPresenter";
|
|
public static final String VIEWS_TAG = "android:menu:list";
|
|
public MenuAdapter mAdapter;
|
|
private MenuPresenter.Callback mCallback;
|
|
public Context mContext;
|
|
private int mId;
|
|
public LayoutInflater mInflater;
|
|
public int mItemIndexOffset;
|
|
public int mItemLayoutRes;
|
|
public MenuBuilder mMenu;
|
|
public ExpandedMenuView mMenuView;
|
|
public int mThemeRes;
|
|
|
|
public class MenuAdapter extends BaseAdapter {
|
|
private int mExpandedIndex = -1;
|
|
|
|
public MenuAdapter() {
|
|
findExpandedIndex();
|
|
}
|
|
|
|
public void findExpandedIndex() {
|
|
MenuItemImpl expandedItem = ListMenuPresenter.this.mMenu.getExpandedItem();
|
|
if (expandedItem != null) {
|
|
ArrayList<MenuItemImpl> nonActionItems = ListMenuPresenter.this.mMenu.getNonActionItems();
|
|
int size = nonActionItems.size();
|
|
for (int i = 0; i < size; i++) {
|
|
if (nonActionItems.get(i) == expandedItem) {
|
|
this.mExpandedIndex = i;
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
this.mExpandedIndex = -1;
|
|
}
|
|
|
|
@Override // android.widget.Adapter
|
|
public int getCount() {
|
|
int size = ListMenuPresenter.this.mMenu.getNonActionItems().size() - ListMenuPresenter.this.mItemIndexOffset;
|
|
return this.mExpandedIndex < 0 ? size : size - 1;
|
|
}
|
|
|
|
@Override // android.widget.Adapter
|
|
public MenuItemImpl getItem(int i) {
|
|
ArrayList<MenuItemImpl> nonActionItems = ListMenuPresenter.this.mMenu.getNonActionItems();
|
|
int i2 = i + ListMenuPresenter.this.mItemIndexOffset;
|
|
int i3 = this.mExpandedIndex;
|
|
if (i3 >= 0 && i2 >= i3) {
|
|
i2++;
|
|
}
|
|
return nonActionItems.get(i2);
|
|
}
|
|
|
|
@Override // android.widget.Adapter
|
|
public long getItemId(int i) {
|
|
return (long) i;
|
|
}
|
|
|
|
@Override // android.widget.Adapter
|
|
public View getView(int i, View view, ViewGroup viewGroup) {
|
|
if (view == null) {
|
|
ListMenuPresenter listMenuPresenter = ListMenuPresenter.this;
|
|
view = listMenuPresenter.mInflater.inflate(listMenuPresenter.mItemLayoutRes, viewGroup, false);
|
|
}
|
|
((MenuView.ItemView) view).initialize(getItem(i), 0);
|
|
return view;
|
|
}
|
|
|
|
@Override // android.widget.BaseAdapter
|
|
public void notifyDataSetChanged() {
|
|
findExpandedIndex();
|
|
super.notifyDataSetChanged();
|
|
}
|
|
}
|
|
|
|
public ListMenuPresenter(int i, int i2) {
|
|
this.mItemLayoutRes = i;
|
|
this.mThemeRes = i2;
|
|
}
|
|
|
|
public ListMenuPresenter(Context context, int i) {
|
|
this(i, 0);
|
|
this.mContext = context;
|
|
this.mInflater = LayoutInflater.from(context);
|
|
}
|
|
|
|
@Override // androidx.appcompat.view.menu.MenuPresenter
|
|
public boolean collapseItemActionView(MenuBuilder menuBuilder, MenuItemImpl menuItemImpl) {
|
|
return false;
|
|
}
|
|
|
|
@Override // androidx.appcompat.view.menu.MenuPresenter
|
|
public boolean expandItemActionView(MenuBuilder menuBuilder, MenuItemImpl menuItemImpl) {
|
|
return false;
|
|
}
|
|
|
|
@Override // androidx.appcompat.view.menu.MenuPresenter
|
|
public boolean flagActionItems() {
|
|
return false;
|
|
}
|
|
|
|
public ListAdapter getAdapter() {
|
|
if (this.mAdapter == null) {
|
|
this.mAdapter = new MenuAdapter();
|
|
}
|
|
return this.mAdapter;
|
|
}
|
|
|
|
@Override // androidx.appcompat.view.menu.MenuPresenter
|
|
public int getId() {
|
|
return this.mId;
|
|
}
|
|
|
|
public int getItemIndexOffset() {
|
|
return this.mItemIndexOffset;
|
|
}
|
|
|
|
@Override // androidx.appcompat.view.menu.MenuPresenter
|
|
public MenuView getMenuView(ViewGroup viewGroup) {
|
|
if (this.mMenuView == null) {
|
|
this.mMenuView = (ExpandedMenuView) this.mInflater.inflate(R.layout.abc_expanded_menu_layout, viewGroup, false);
|
|
if (this.mAdapter == null) {
|
|
this.mAdapter = new MenuAdapter();
|
|
}
|
|
this.mMenuView.setAdapter((ListAdapter) this.mAdapter);
|
|
this.mMenuView.setOnItemClickListener(this);
|
|
}
|
|
return this.mMenuView;
|
|
}
|
|
|
|
@Override // androidx.appcompat.view.menu.MenuPresenter
|
|
public void initForMenu(Context context, MenuBuilder menuBuilder) {
|
|
if (this.mThemeRes != 0) {
|
|
ContextThemeWrapper contextThemeWrapper = new ContextThemeWrapper(context, this.mThemeRes);
|
|
this.mContext = contextThemeWrapper;
|
|
this.mInflater = LayoutInflater.from(contextThemeWrapper);
|
|
} else if (this.mContext != null) {
|
|
this.mContext = context;
|
|
if (this.mInflater == null) {
|
|
this.mInflater = LayoutInflater.from(context);
|
|
}
|
|
}
|
|
this.mMenu = menuBuilder;
|
|
MenuAdapter menuAdapter = this.mAdapter;
|
|
if (menuAdapter != null) {
|
|
menuAdapter.notifyDataSetChanged();
|
|
}
|
|
}
|
|
|
|
@Override // androidx.appcompat.view.menu.MenuPresenter
|
|
public void onCloseMenu(MenuBuilder menuBuilder, boolean z2) {
|
|
MenuPresenter.Callback callback = this.mCallback;
|
|
if (callback != null) {
|
|
callback.onCloseMenu(menuBuilder, z2);
|
|
}
|
|
}
|
|
|
|
@Override // android.widget.AdapterView.OnItemClickListener
|
|
public void onItemClick(AdapterView<?> adapterView, View view, int i, long j) {
|
|
this.mMenu.performItemAction(this.mAdapter.getItem(i), this, 0);
|
|
}
|
|
|
|
@Override // androidx.appcompat.view.menu.MenuPresenter
|
|
public void onRestoreInstanceState(Parcelable parcelable) {
|
|
restoreHierarchyState((Bundle) parcelable);
|
|
}
|
|
|
|
@Override // androidx.appcompat.view.menu.MenuPresenter
|
|
public Parcelable onSaveInstanceState() {
|
|
if (this.mMenuView == null) {
|
|
return null;
|
|
}
|
|
Bundle bundle = new Bundle();
|
|
saveHierarchyState(bundle);
|
|
return bundle;
|
|
}
|
|
|
|
@Override // androidx.appcompat.view.menu.MenuPresenter
|
|
public boolean onSubMenuSelected(SubMenuBuilder subMenuBuilder) {
|
|
if (!subMenuBuilder.hasVisibleItems()) {
|
|
return false;
|
|
}
|
|
new MenuDialogHelper(subMenuBuilder).show(null);
|
|
MenuPresenter.Callback callback = this.mCallback;
|
|
if (callback == null) {
|
|
return true;
|
|
}
|
|
callback.onOpenSubMenu(subMenuBuilder);
|
|
return true;
|
|
}
|
|
|
|
public void restoreHierarchyState(Bundle bundle) {
|
|
SparseArray<Parcelable> sparseParcelableArray = bundle.getSparseParcelableArray("android:menu:list");
|
|
if (sparseParcelableArray != null) {
|
|
this.mMenuView.restoreHierarchyState(sparseParcelableArray);
|
|
}
|
|
}
|
|
|
|
public void saveHierarchyState(Bundle bundle) {
|
|
SparseArray<Parcelable> sparseArray = new SparseArray<>();
|
|
ExpandedMenuView expandedMenuView = this.mMenuView;
|
|
if (expandedMenuView != null) {
|
|
expandedMenuView.saveHierarchyState(sparseArray);
|
|
}
|
|
bundle.putSparseParcelableArray("android:menu:list", sparseArray);
|
|
}
|
|
|
|
@Override // androidx.appcompat.view.menu.MenuPresenter
|
|
public void setCallback(MenuPresenter.Callback callback) {
|
|
this.mCallback = callback;
|
|
}
|
|
|
|
public void setId(int i) {
|
|
this.mId = i;
|
|
}
|
|
|
|
public void setItemIndexOffset(int i) {
|
|
this.mItemIndexOffset = i;
|
|
if (this.mMenuView != null) {
|
|
updateMenuView(false);
|
|
}
|
|
}
|
|
|
|
@Override // androidx.appcompat.view.menu.MenuPresenter
|
|
public void updateMenuView(boolean z2) {
|
|
MenuAdapter menuAdapter = this.mAdapter;
|
|
if (menuAdapter != null) {
|
|
menuAdapter.notifyDataSetChanged();
|
|
}
|
|
}
|
|
}
|