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 android.view;
18
19import android.content.Context;
20import com.android.ide.common.rendering.api.LayoutLog;
21import com.android.ide.common.rendering.api.ViewInfo;
22import com.android.internal.view.menu.BridgeMenuItemImpl;
23import com.android.internal.view.menu.MenuView;
24import com.android.layoutlib.bridge.Bridge;
25import com.android.layoutlib.bridge.android.BridgeContext;
26import com.android.tools.layoutlib.annotations.LayoutlibDelegate;
27
28import android.util.AttributeSet;
29
30/**
31 * Delegate used to provide new implementation of a select few methods of {@link MenuInflater}
32 * <p/>
33 * Through the layoutlib_create tool, the original  methods of MenuInflater have been
34 * replaced by calls to methods of the same name in this delegate class.
35 * <p/>
36 * The main purpose of the class is to get the view key from the menu xml parser and add it to
37 * the menu item. The view key is used by the IDE to match the individual view elements to the
38 * corresponding xml tag in the menu/layout file.
39 * <p/>
40 * For Menus, the views may be reused and the {@link MenuItem} is a better object to hold the
41 * view key than the {@link MenuView.ItemView}. At the time of computation of the rest of {@link
42 * ViewInfo}, we check the corresponding view key in the menu item for the view and add it
43 */
44public class MenuInflater_Delegate {
45
46    @LayoutlibDelegate
47    /*package*/ static void registerMenu(MenuInflater thisInflater, MenuItem menuItem,
48            AttributeSet attrs) {
49        if (menuItem instanceof BridgeMenuItemImpl) {
50            Context context = thisInflater.getContext();
51            context = BridgeContext.getBaseContext(context);
52            if (context instanceof BridgeContext) {
53                Object viewKey = BridgeInflater.getViewKeyFromParser(
54                        attrs, ((BridgeContext) context), null, false);
55                ((BridgeMenuItemImpl) menuItem).setViewCookie(viewKey);
56                return;
57            }
58        }
59        // This means that Bridge did not take over the instantiation of some object properly.
60        // This is most likely a bug in the LayoutLib code.
61        Bridge.getLog().warning(LayoutLog.TAG_BROKEN,
62                "Action Bar Menu rendering may be incorrect.", null);
63
64    }
65
66    @LayoutlibDelegate
67    /*package*/ static void registerMenu(MenuInflater thisInflater, SubMenu subMenu,
68            AttributeSet parser) {
69        registerMenu(thisInflater, subMenu.getItem(), parser);
70    }
71
72}
73