11001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta/* 21001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta * Copyright (C) 2014 The Android Open Source Project 31001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta * 41001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta * Licensed under the Apache License, Version 2.0 (the "License"); 51001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta * you may not use this file except in compliance with the License. 61001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta * You may obtain a copy of the License at 71001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta * 81001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta * http://www.apache.org/licenses/LICENSE-2.0 91001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta * 101001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta * Unless required by applicable law or agreed to in writing, software 111001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta * distributed under the License is distributed on an "AS IS" BASIS, 121001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 131001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta * See the License for the specific language governing permissions and 141001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta * limitations under the License. 151001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta */ 161001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta 171001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Guptapackage android.view; 181001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta 191001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Guptaimport android.content.Context; 201001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Guptaimport com.android.ide.common.rendering.api.LayoutLog; 211001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Guptaimport com.android.ide.common.rendering.api.ViewInfo; 221001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Guptaimport com.android.internal.view.menu.BridgeMenuItemImpl; 231001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Guptaimport com.android.internal.view.menu.MenuView; 241001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Guptaimport com.android.layoutlib.bridge.Bridge; 251001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Guptaimport com.android.layoutlib.bridge.android.BridgeContext; 261001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Guptaimport com.android.tools.layoutlib.annotations.LayoutlibDelegate; 271001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta 281001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Guptaimport android.util.AttributeSet; 291001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta 301001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta/** 311001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta * Delegate used to provide new implementation of a select few methods of {@link MenuInflater} 321001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta * <p/> 331001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta * Through the layoutlib_create tool, the original methods of MenuInflater have been 341001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta * replaced by calls to methods of the same name in this delegate class. 351001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta * <p/> 361001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta * The main purpose of the class is to get the view key from the menu xml parser and add it to 371001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta * the menu item. The view key is used by the IDE to match the individual view elements to the 381001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta * corresponding xml tag in the menu/layout file. 391001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta * <p/> 401001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta * For Menus, the views may be reused and the {@link MenuItem} is a better object to hold the 411001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta * view key than the {@link MenuView.ItemView}. At the time of computation of the rest of {@link 421001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta * ViewInfo}, we check the corresponding view key in the menu item for the view and add it 431001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta */ 441001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Guptapublic class MenuInflater_Delegate { 451001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta 461001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta @LayoutlibDelegate 477d3ae4f17d93125f8c45e17d3a6a22d5ab23b624Deepanshu Gupta /*package*/ static void registerMenu(MenuInflater thisInflater, MenuItem menuItem, 481001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta AttributeSet attrs) { 491001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta if (menuItem instanceof BridgeMenuItemImpl) { 501001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta Context context = thisInflater.getContext(); 51b54b78e21554b8450893d4c28e0fe3e9b8c4425eDeepanshu Gupta while (context instanceof ContextThemeWrapper) { 521001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta context = ((ContextThemeWrapper) context).getBaseContext(); 531001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta } 541001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta if (context instanceof BridgeContext) { 551001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta Object viewKey = BridgeInflater.getViewKeyFromParser( 561001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta attrs, ((BridgeContext) context), null, false); 571001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta ((BridgeMenuItemImpl) menuItem).setViewCookie(viewKey); 581001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta return; 591001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta } 601001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta } 611001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta // This means that Bridge did not take over the instantiation of some object properly. 621001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta // This is most likely a bug in the LayoutLib code. 631001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta Bridge.getLog().warning(LayoutLog.TAG_BROKEN, 641001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta "Action Bar Menu rendering may be incorrect.", null); 651001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta 661001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta } 671001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta 681001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta @LayoutlibDelegate 697d3ae4f17d93125f8c45e17d3a6a22d5ab23b624Deepanshu Gupta /*package*/ static void registerMenu(MenuInflater thisInflater, SubMenu subMenu, 701001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta AttributeSet parser) { 717d3ae4f17d93125f8c45e17d3a6a22d5ab23b624Deepanshu Gupta registerMenu(thisInflater, subMenu.getItem(), parser); 721001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta } 731001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta 741001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta} 75