content: package androidx.appcompat.view.menu;
  
  import android.content.Context;
  import android.os.Bundle;
  import android.os.IBinder;
  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.appcompat.R;
  import androidx.appcompat.view.menu.MenuPresenter;
  import androidx.appcompat.view.menu.MenuView;
  import java.util.ArrayList;
  
  public class ListMenuPresenter implements MenuPresenter, AdapterView.OnItemClickListener {
      private static final String TAG = "ListMenuPresenter";
      public static final String VIEWS_TAG = "android:menu:list";
      MenuAdapter mAdapter;
      private MenuPresenter.Callback mCallback;
      Context mContext;
      private int mId;
      LayoutInflater mInflater;
      int mItemIndexOffset;
      int mItemLayoutRes;
      MenuBuilder mMenu;
      ExpandedMenuView mMenuView;
      int mThemeRes;
  
      private class MenuAdapter extends BaseAdapter {
          private int mExpandedIndex = -1;
  
          public MenuAdapter() {
              findExpandedIndex();
          }
  
          /* access modifiers changed from: package-private */
          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;
          }
  
          public int getCount() {
              int size = ListMenuPresenter.this.mMenu.getNonActionItems().size() - ListMenuPresenter.this.mItemIndexOffset;
              return this.mExpandedIndex < 0 ? size : size - 1;
          }
  
          public MenuItemImpl getItem(int position) {
              ArrayList<MenuItemImpl> nonActionItems = ListMenuPresenter.this.mMenu.getNonActionItems();
              int position2 = position + ListMenuPresenter.this.mItemIndexOffset;
              int i = this.mExpandedIndex;
              if (i >= 0 && position2 >= i) {
                  position2++;
              }
              return nonActionItems.get(position2);
          }
  
          public long getItemId(int position) {
              return (long) position;
          }
  
          public View getView(int position, View convertView, ViewGroup parent) {
              if (convertView == null) {
                  convertView = ListMenuPresenter.this.mInflater.inflate(ListMenuPresenter.this.mItemLayoutRes, parent, false);
              }
              ((MenuView.ItemView) convertView).initialize(getItem(position), 0);
              return convertView;
          }
  
          public void notifyDataSetChanged() {
              findExpandedIndex();
              super.notifyDataSetChanged();
          }
      }
  
      public ListMenuPresenter(int itemLayoutRes, int themeRes) {
          this.mItemLayoutRes = itemLayoutRes;
          this.mThemeRes = themeRes;
      }
  
      public ListMenuPresenter(Context context, int itemLayoutRes) {
          this(itemLayoutRes, 0);
          this.mContext = context;
          this.mInflater = LayoutInflater.from(context);
      }
  
      public boolean collapseItemActionView(MenuBuilder menu, MenuItemImpl item) {
          return false;
      }
  
      public boolean expandItemActionView(MenuBuilder menu, MenuItemImpl item) {
          return false;
      }
  
      public boolean flagActionItems() {
          return false;
      }
  
      public ListAdapter getAdapter() {
          if (this.mAdapter == null) {
              this.mAdapter = new MenuAdapter();
          }
          return this.mAdapter;
      }
  
      public int getId() {
          return this.mId;
      }
  
      /* access modifiers changed from: package-private */
      public int getItemIndexOffset() {
          return this.mItemIndexOffset;
      }
  
      public MenuView getMenuView(ViewGroup root) {
          if (this.mMenuView == null) {
              this.mMenuView = (ExpandedMenuView) this.mInflater.inflate(R.layout.abc_expanded_menu_layout, root, false);
              if (this.mAdapter == null) {
                  this.mAdapter = new MenuAdapter();
              }
              this.mMenuView.setAdapter(this.mAdapter);
              this.mMenuView.setOnItemClickListener(this);
          }
          return this.mMenuView;
      }
  
      public void initForMenu(Context context, MenuBuilder menu) {
          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 = menu;
          MenuAdapter menuAdapter = this.mAdapter;
          if (menuAdapter != null) {
              menuAdapter.notifyDataSetChanged();
          }
      }
  
      public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {
          MenuPresenter.Callback callback = this.mCallback;
          if (callback != null) {
              callback.onCloseMenu(menu, allMenusAreClosing);
          }
      }
  
      public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
          this.mMenu.performItemAction(this.mAdapter.getItem(position), this, 0);
      }
  
      public void onRestoreInstanceState(Parcelable state) {
          restoreHierarchyState((Bundle) state);
      }
  
      public Parcelable onSaveInstanceState() {
          if (this.mMenuView == null) {
              return null;
          }
          Bundle bundle = new Bundle();
          saveHierarchyState(bundle);
          return bundle;
      }
  
      public boolean onSubMenuSelected(SubMenuBuilder subMenu) {
          if (!subMenu.hasVisibleItems()) {
              return false;
          }
          new MenuDialogHelper(subMenu).show((IBinder) null);
          MenuPresenter.Callback callback = this.mCallback;
          if (callback == null) {
              return true;
          }
          callback.onOpenSubMenu(subMenu);
          return true;
      }
  
      public void restoreHierarchyState(Bundle inState) {
          SparseArray sparseParcelableArray = inState.getSparseParcelableArray(VIEWS_TAG);
          if (sparseParcelableArray != null) {
              this.mMenuView.restoreHierarchyState(sparseParcelableArray);
          }
      }
  
      public void saveHierarchyState(Bundle outState) {
          SparseArray sparseArray = new SparseArray();
          ExpandedMenuView expandedMenuView = this.mMenuView;
          if (expandedMenuView != null) {
              expandedMenuView.saveHierarchyState(sparseArray);
          }
          outState.putSparseParcelableArray(VIEWS_TAG, sparseArray);
      }
  
      public void setCallback(MenuPresenter.Callback cb) {
          this.mCallback = cb;
      }
  
      public void setId(int id) {
          this.mId = id;
      }
  
      public void setItemIndexOffset(int offset) {
          this.mItemIndexOffset = offset;
          if (this.mMenuView != null) {
              updateMenuView(false);
          }
      }
  
      public void updateMenuView(boolean cleared) {
          MenuAdapter menuAdapter = this.mAdapter;
          if (menuAdapter != null) {
              menuAdapter.notifyDataSetChanged();
          }
      }
  }
  