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