Home | History | Annotate | Download | only in menu
      1 /*
      2  * Copyright (C) 2012 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package androidx.appcompat.view.menu;
     18 
     19 import android.content.Context;
     20 import android.view.MenuItem;
     21 import android.view.SubMenu;
     22 
     23 import androidx.collection.ArrayMap;
     24 import androidx.core.internal.view.SupportMenuItem;
     25 import androidx.core.internal.view.SupportSubMenu;
     26 
     27 import java.util.Iterator;
     28 import java.util.Map;
     29 
     30 abstract class BaseMenuWrapper<T> extends BaseWrapper<T> {
     31 
     32     final Context mContext;
     33 
     34     private Map<SupportMenuItem, MenuItem> mMenuItems;
     35     private Map<SupportSubMenu, SubMenu> mSubMenus;
     36 
     37     BaseMenuWrapper(Context context, T object) {
     38         super(object);
     39         mContext = context;
     40     }
     41 
     42     final MenuItem getMenuItemWrapper(final MenuItem menuItem) {
     43         if (menuItem instanceof SupportMenuItem) {
     44             final SupportMenuItem supportMenuItem = (SupportMenuItem) menuItem;
     45 
     46             // Instantiate Map if null
     47             if (mMenuItems == null) {
     48                 mMenuItems = new ArrayMap<>();
     49             }
     50 
     51             // First check if we already have a wrapper for this item
     52             MenuItem wrappedItem = mMenuItems.get(menuItem);
     53 
     54             if (null == wrappedItem) {
     55                 // ... if not, create one and add it to our map
     56                 wrappedItem = MenuWrapperFactory.wrapSupportMenuItem(mContext, supportMenuItem);
     57                 mMenuItems.put(supportMenuItem, wrappedItem);
     58             }
     59 
     60             return wrappedItem;
     61         }
     62         return menuItem;
     63     }
     64 
     65     final SubMenu getSubMenuWrapper(final SubMenu subMenu) {
     66         if (subMenu instanceof SupportSubMenu) {
     67             final SupportSubMenu supportSubMenu = (SupportSubMenu) subMenu;
     68 
     69             // Instantiate Map if null
     70             if (mSubMenus == null) {
     71                 mSubMenus = new ArrayMap<>();
     72             }
     73 
     74             SubMenu wrappedMenu = mSubMenus.get(supportSubMenu);
     75 
     76             if (null == wrappedMenu) {
     77                 wrappedMenu = MenuWrapperFactory.wrapSupportSubMenu(mContext, supportSubMenu);
     78                 mSubMenus.put(supportSubMenu, wrappedMenu);
     79             }
     80             return wrappedMenu;
     81         }
     82         return subMenu;
     83     }
     84 
     85 
     86     final void internalClear() {
     87         if (mMenuItems != null) {
     88             mMenuItems.clear();
     89         }
     90         if (mSubMenus != null) {
     91             mSubMenus.clear();
     92         }
     93     }
     94 
     95     final void internalRemoveGroup(final int groupId) {
     96         if (mMenuItems == null) {
     97             return;
     98         }
     99 
    100         Iterator<SupportMenuItem> iterator = mMenuItems.keySet().iterator();
    101         android.view.MenuItem menuItem;
    102 
    103         while (iterator.hasNext()) {
    104             menuItem = iterator.next();
    105             if (groupId == menuItem.getGroupId()) {
    106                 iterator.remove();
    107             }
    108         }
    109     }
    110 
    111     final void internalRemoveItem(final int id) {
    112         if (mMenuItems == null) {
    113             return;
    114         }
    115 
    116         Iterator<SupportMenuItem> iterator = mMenuItems.keySet().iterator();
    117         android.view.MenuItem menuItem;
    118 
    119         while (iterator.hasNext()) {
    120             menuItem = iterator.next();
    121             if (id == menuItem.getItemId()) {
    122                 iterator.remove();
    123                 break;
    124             }
    125         }
    126     }
    127 }
    128