1 /*
2  * Copyright (C) 2014 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 com.android.layoutlib.bridge.android.BridgeContext;
20 
21 import android.content.Context;
22 import android.view.View;
23 
24 /**
25  * An extension of the {@link MenuItemImpl} to store the view cookie also.
26  */
27 public class BridgeMenuItemImpl extends MenuItemImpl {
28 
29     /**
30      * An object returned by the IDE that helps mapping each View to the corresponding XML tag in
31      * the layout. For Menus, we store this cookie here and attach it to the corresponding view
32      * at the time of rendering.
33      */
34     private Object viewCookie;
35     private BridgeContext mContext;
36 
37     /**
38      * Instantiates this menu item.
39      */
BridgeMenuItemImpl(MenuBuilder menu, int group, int id, int categoryOrder, int ordering, CharSequence title, int showAsAction)40     BridgeMenuItemImpl(MenuBuilder menu, int group, int id, int categoryOrder, int ordering,
41             CharSequence title, int showAsAction) {
42         super(menu, group, id, categoryOrder, ordering, title, showAsAction);
43         Context context = menu.getContext();
44         context = BridgeContext.getBaseContext(context);
45         if (context instanceof BridgeContext) {
46             mContext = ((BridgeContext) context);
47         }
48     }
49 
getViewCookie()50     public Object getViewCookie() {
51         return viewCookie;
52     }
53 
setViewCookie(Object viewCookie)54     public void setViewCookie(Object viewCookie) {
55         // If the menu item has an associated action provider view,
56         // directly set the cookie in the view to cookie map stored in BridgeContext.
57         View actionView = getActionView();
58         if (actionView != null && mContext != null) {
59             mContext.addViewKey(actionView, viewCookie);
60             // We don't need to add the view cookie to the this item now. But there's no harm in
61             // storing it, in case we need it in the future.
62         }
63         this.viewCookie = viewCookie;
64     }
65 }
66