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 com.android.internal.view.menu;
181001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta
191f5e678c08ca7e0b6734b7856187f1739fb4dbefDeepanshu Guptaimport com.android.layoutlib.bridge.android.BridgeContext;
201f5e678c08ca7e0b6734b7856187f1739fb4dbefDeepanshu Gupta
211f5e678c08ca7e0b6734b7856187f1739fb4dbefDeepanshu Guptaimport android.content.Context;
221f5e678c08ca7e0b6734b7856187f1739fb4dbefDeepanshu Guptaimport android.view.ContextThemeWrapper;
231f5e678c08ca7e0b6734b7856187f1739fb4dbefDeepanshu Guptaimport android.view.View;
241f5e678c08ca7e0b6734b7856187f1739fb4dbefDeepanshu Gupta
251001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta/**
261001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta * An extension of the {@link MenuItemImpl} to store the view cookie also.
271001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta */
281001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Guptapublic class BridgeMenuItemImpl extends MenuItemImpl {
291001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta
301001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta    /**
311001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta     * An object returned by the IDE that helps mapping each View to the corresponding XML tag in
321001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta     * the layout. For Menus, we store this cookie here and attach it to the corresponding view
331001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta     * at the time of rendering.
341001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta     */
351001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta    private Object viewCookie;
361f5e678c08ca7e0b6734b7856187f1739fb4dbefDeepanshu Gupta    private BridgeContext mContext;
371001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta
381001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta    /**
391001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta     * Instantiates this menu item.
401001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta     */
411001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta    BridgeMenuItemImpl(MenuBuilder menu, int group, int id, int categoryOrder, int ordering,
421001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta            CharSequence title, int showAsAction) {
431001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta        super(menu, group, id, categoryOrder, ordering, title, showAsAction);
441f5e678c08ca7e0b6734b7856187f1739fb4dbefDeepanshu Gupta        Context context = menu.getContext();
45b54b78e21554b8450893d4c28e0fe3e9b8c4425eDeepanshu Gupta        while (context instanceof ContextThemeWrapper) {
461f5e678c08ca7e0b6734b7856187f1739fb4dbefDeepanshu Gupta            context = ((ContextThemeWrapper) context).getBaseContext();
471f5e678c08ca7e0b6734b7856187f1739fb4dbefDeepanshu Gupta        }
481f5e678c08ca7e0b6734b7856187f1739fb4dbefDeepanshu Gupta        if (context instanceof BridgeContext) {
491f5e678c08ca7e0b6734b7856187f1739fb4dbefDeepanshu Gupta            mContext = ((BridgeContext) context);
501f5e678c08ca7e0b6734b7856187f1739fb4dbefDeepanshu Gupta        }
511001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta    }
521001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta
531001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta    public Object getViewCookie() {
541001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta        return viewCookie;
551001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta    }
561001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta
571001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta    public void setViewCookie(Object viewCookie) {
581f5e678c08ca7e0b6734b7856187f1739fb4dbefDeepanshu Gupta        // If the menu item has an associated action provider view,
591f5e678c08ca7e0b6734b7856187f1739fb4dbefDeepanshu Gupta        // directly set the cookie in the view to cookie map stored in BridgeContext.
601f5e678c08ca7e0b6734b7856187f1739fb4dbefDeepanshu Gupta        View actionView = getActionView();
611f5e678c08ca7e0b6734b7856187f1739fb4dbefDeepanshu Gupta        if (actionView != null && mContext != null) {
621f5e678c08ca7e0b6734b7856187f1739fb4dbefDeepanshu Gupta            mContext.addViewKey(actionView, viewCookie);
631f5e678c08ca7e0b6734b7856187f1739fb4dbefDeepanshu Gupta            // We don't need to add the view cookie to the this item now. But there's no harm in
641f5e678c08ca7e0b6734b7856187f1739fb4dbefDeepanshu Gupta            // storing it, in case we need it in the future.
651f5e678c08ca7e0b6734b7856187f1739fb4dbefDeepanshu Gupta        }
661001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta        this.viewCookie = viewCookie;
671001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta    }
681001961f904bac5294aaf73a47c2497aa764bf7fDeepanshu Gupta}
69