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
17package android.support.v7.internal.view.menu;
18
19import android.content.Context;
20import android.support.v4.internal.view.SupportMenuItem;
21import android.support.v4.internal.view.SupportSubMenu;
22import android.support.v4.util.ArrayMap;
23import android.view.MenuItem;
24import android.view.SubMenu;
25
26import java.util.Iterator;
27import java.util.Map;
28
29abstract class BaseMenuWrapper<T> extends BaseWrapper<T> {
30
31    final Context mContext;
32
33    private Map<SupportMenuItem, MenuItem> mMenuItems;
34    private Map<SupportSubMenu, SubMenu> mSubMenus;
35
36    BaseMenuWrapper(Context context, T object) {
37        super(object);
38        mContext = context;
39    }
40
41    final MenuItem getMenuItemWrapper(final MenuItem menuItem) {
42        if (menuItem instanceof SupportMenuItem) {
43            final SupportMenuItem supportMenuItem = (SupportMenuItem) menuItem;
44
45            // Instantiate Map if null
46            if (mMenuItems == null) {
47                mMenuItems = new ArrayMap<>();
48            }
49
50            // First check if we already have a wrapper for this item
51            MenuItem wrappedItem = mMenuItems.get(menuItem);
52
53            if (null == wrappedItem) {
54                // ... if not, create one and add it to our map
55                wrappedItem = MenuWrapperFactory.wrapSupportMenuItem(mContext, supportMenuItem);
56                mMenuItems.put(supportMenuItem, wrappedItem);
57            }
58
59            return wrappedItem;
60        }
61        return menuItem;
62    }
63
64    final SubMenu getSubMenuWrapper(final SubMenu subMenu) {
65        if (subMenu instanceof SupportSubMenu) {
66            final SupportSubMenu supportSubMenu = (SupportSubMenu) subMenu;
67
68            // Instantiate Map if null
69            if (mSubMenus == null) {
70                mSubMenus = new ArrayMap<>();
71            }
72
73            SubMenu wrappedMenu = mSubMenus.get(supportSubMenu);
74
75            if (null == wrappedMenu) {
76                wrappedMenu = MenuWrapperFactory.wrapSupportSubMenu(mContext, supportSubMenu);
77                mSubMenus.put(supportSubMenu, wrappedMenu);
78            }
79            return wrappedMenu;
80        }
81        return subMenu;
82    }
83
84
85    final void internalClear() {
86        if (mMenuItems != null) {
87            mMenuItems.clear();
88        }
89        if (mSubMenus != null) {
90            mSubMenus.clear();
91        }
92    }
93
94    final void internalRemoveGroup(final int groupId) {
95        if (mMenuItems == null) {
96            return;
97        }
98
99        Iterator<SupportMenuItem> iterator = mMenuItems.keySet().iterator();
100        android.view.MenuItem menuItem;
101
102        while (iterator.hasNext()) {
103            menuItem = iterator.next();
104            if (groupId == menuItem.getGroupId()) {
105                iterator.remove();
106            }
107        }
108    }
109
110    final void internalRemoveItem(final int id) {
111        if (mMenuItems == null) {
112            return;
113        }
114
115        Iterator<SupportMenuItem> iterator = mMenuItems.keySet().iterator();
116        android.view.MenuItem menuItem;
117
118        while (iterator.hasNext()) {
119            menuItem = iterator.next();
120            if (id == menuItem.getItemId()) {
121                iterator.remove();
122                break;
123            }
124        }
125    }
126}
127