1 /*
2  * Copyright (C) 2006 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 com.android.internal.view.menu;
18 
19 import android.compat.annotation.UnsupportedAppUsage;
20 import android.content.Context;
21 import android.graphics.drawable.Drawable;
22 import android.view.Menu;
23 import android.view.MenuItem;
24 import android.view.SubMenu;
25 import android.view.View;
26 
27 /**
28  * The model for a sub menu, which is an extension of the menu.  Most methods are proxied to
29  * the parent menu.
30  */
31 public class SubMenuBuilder extends MenuBuilder implements SubMenu {
32     private MenuBuilder mParentMenu;
33     private MenuItemImpl mItem;
34 
SubMenuBuilder(Context context, MenuBuilder parentMenu, MenuItemImpl item)35     public SubMenuBuilder(Context context, MenuBuilder parentMenu, MenuItemImpl item) {
36         super(context);
37 
38         mParentMenu = parentMenu;
39         mItem = item;
40     }
41 
42     @Override
setQwertyMode(boolean isQwerty)43     public void setQwertyMode(boolean isQwerty) {
44         mParentMenu.setQwertyMode(isQwerty);
45     }
46 
47     @Override
isQwertyMode()48     public boolean isQwertyMode() {
49         return mParentMenu.isQwertyMode();
50     }
51 
52     @Override
setShortcutsVisible(boolean shortcutsVisible)53     public void setShortcutsVisible(boolean shortcutsVisible) {
54         mParentMenu.setShortcutsVisible(shortcutsVisible);
55     }
56 
57     @Override
isShortcutsVisible()58     public boolean isShortcutsVisible() {
59         return mParentMenu.isShortcutsVisible();
60     }
61 
getParentMenu()62     public Menu getParentMenu() {
63         return mParentMenu;
64     }
65 
getItem()66     public MenuItem getItem() {
67         return mItem;
68     }
69 
70     @Override
71     @UnsupportedAppUsage
setCallback(Callback callback)72     public void setCallback(Callback callback) {
73         mParentMenu.setCallback(callback);
74     }
75 
76     @Override
77     @UnsupportedAppUsage
getRootMenu()78     public MenuBuilder getRootMenu() {
79         return mParentMenu.getRootMenu();
80     }
81 
82     @Override
dispatchMenuItemSelected(MenuBuilder menu, MenuItem item)83     boolean dispatchMenuItemSelected(MenuBuilder menu, MenuItem item) {
84         return super.dispatchMenuItemSelected(menu, item) ||
85                 mParentMenu.dispatchMenuItemSelected(menu, item);
86     }
87 
setIcon(Drawable icon)88     public SubMenu setIcon(Drawable icon) {
89         mItem.setIcon(icon);
90         return this;
91     }
92 
setIcon(int iconRes)93     public SubMenu setIcon(int iconRes) {
94         mItem.setIcon(iconRes);
95         return this;
96     }
97 
setHeaderIcon(Drawable icon)98     public SubMenu setHeaderIcon(Drawable icon) {
99         return (SubMenu) super.setHeaderIconInt(icon);
100     }
101 
setHeaderIcon(int iconRes)102     public SubMenu setHeaderIcon(int iconRes) {
103         return (SubMenu) super.setHeaderIconInt(iconRes);
104     }
105 
setHeaderTitle(CharSequence title)106     public SubMenu setHeaderTitle(CharSequence title) {
107         return (SubMenu) super.setHeaderTitleInt(title);
108     }
109 
setHeaderTitle(int titleRes)110     public SubMenu setHeaderTitle(int titleRes) {
111         return (SubMenu) super.setHeaderTitleInt(titleRes);
112     }
113 
setHeaderView(View view)114     public SubMenu setHeaderView(View view) {
115         return (SubMenu) super.setHeaderViewInt(view);
116     }
117 
118     @Override
expandItemActionView(MenuItemImpl item)119     public boolean expandItemActionView(MenuItemImpl item) {
120         return mParentMenu.expandItemActionView(item);
121     }
122 
123     @Override
collapseItemActionView(MenuItemImpl item)124     public boolean collapseItemActionView(MenuItemImpl item) {
125         return mParentMenu.collapseItemActionView(item);
126     }
127 
128     @Override
getActionViewStatesKey()129     public String getActionViewStatesKey() {
130         final int itemId = mItem != null ? mItem.getItemId() : 0;
131         if (itemId == 0) {
132             return null;
133         }
134         return super.getActionViewStatesKey() + ":" + itemId;
135     }
136 
137     @Override
setGroupDividerEnabled(boolean groupDividerEnabled)138     public void setGroupDividerEnabled(boolean groupDividerEnabled) {
139         mParentMenu.setGroupDividerEnabled(groupDividerEnabled);
140     }
141 
142     @Override
isGroupDividerEnabled()143     public boolean isGroupDividerEnabled() {
144         return mParentMenu.isGroupDividerEnabled();
145     }
146 }
147