1bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell/*
2bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * Copyright (C) 2006 The Android Open Source Project
3bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell *
4bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * Licensed under the Apache License, Version 2.0 (the "License");
5bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * you may not use this file except in compliance with the License.
6bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * You may obtain a copy of the License at
7bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell *
8bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell *      http://www.apache.org/licenses/LICENSE-2.0
9bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell *
10bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * Unless required by applicable law or agreed to in writing, software
11bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * distributed under the License is distributed on an "AS IS" BASIS,
12bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * See the License for the specific language governing permissions and
14bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * limitations under the License.
15bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell */
16bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
1766698bb15ba0f873aa1c2290cc50d6bb839a474aChris Banespackage android.support.v7.view.menu;
18bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
19bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.content.Context;
20bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.graphics.drawable.Drawable;
21c39d9c75590eca86a5e7e32a8824ba04a0d42e9bAlan Viveretteimport android.support.annotation.RestrictTo;
2247082c30c630c34829439a9eecd1cf7e8d255a86Aurimas Liutikasimport android.support.v4.view.ViewCompat;
23da10fdd1400ecfd8d7f2e55651dd528d0614dfc5Jeff Brownimport android.support.v7.appcompat.R;
246142a54baae3289f734947c6b5375b12eb0fb722Chris Banesimport android.support.v7.widget.TintTypedArray;
25bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.util.AttributeSet;
26bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.view.LayoutInflater;
27bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.view.View;
28bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.view.ViewGroup;
29bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.widget.CheckBox;
30bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.widget.CompoundButton;
31bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.widget.ImageView;
32bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.widget.LinearLayout;
33bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.widget.RadioButton;
34bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellimport android.widget.TextView;
35bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
36c39d9c75590eca86a5e7e32a8824ba04a0d42e9bAlan Viveretteimport static android.support.annotation.RestrictTo.Scope.GROUP_ID;
37c39d9c75590eca86a5e7e32a8824ba04a0d42e9bAlan Viverette
38bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell/**
39bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell * The item view for each item in the ListView-based MenuViews.
4089208232f3b5d1451408d787872504a190bc7ee0Chris Banes *
4189208232f3b5d1451408d787872504a190bc7ee0Chris Banes * @hide
42bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell */
43c39d9c75590eca86a5e7e32a8824ba04a0d42e9bAlan Viverette@RestrictTo(GROUP_ID)
44bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powellpublic class ListMenuItemView extends LinearLayout implements MenuView.ItemView {
45ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private static final String TAG = "ListMenuItemView";
46ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private MenuItemImpl mItemData;
47bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
48ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private ImageView mIconView;
49ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private RadioButton mRadioButton;
50ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private TextView mTitleView;
51ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private CheckBox mCheckBox;
52ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private TextView mShortcutView;
536142a54baae3289f734947c6b5375b12eb0fb722Chris Banes    private ImageView mSubMenuArrowView;
54bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
55ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private Drawable mBackground;
56ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private int mTextAppearance;
57ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private Context mTextAppearanceContext;
58ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private boolean mPreserveIconSpacing;
596142a54baae3289f734947c6b5375b12eb0fb722Chris Banes    private Drawable mSubMenuArrow;
60bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
61ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private int mMenuType;
62bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
63ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private LayoutInflater mInflater;
64bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
65ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private boolean mForceShowIcon;
66bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
676142a54baae3289f734947c6b5375b12eb0fb722Chris Banes    public ListMenuItemView(Context context, AttributeSet attrs) {
686142a54baae3289f734947c6b5375b12eb0fb722Chris Banes        this(context, attrs, R.attr.listMenuViewStyle);
696142a54baae3289f734947c6b5375b12eb0fb722Chris Banes    }
706142a54baae3289f734947c6b5375b12eb0fb722Chris Banes
716142a54baae3289f734947c6b5375b12eb0fb722Chris Banes    public ListMenuItemView(Context context, AttributeSet attrs, int defStyleAttr) {
72ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        super(context, attrs);
73bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
746142a54baae3289f734947c6b5375b12eb0fb722Chris Banes        final TintTypedArray a = TintTypedArray.obtainStyledAttributes(getContext(),
756142a54baae3289f734947c6b5375b12eb0fb722Chris Banes                attrs, R.styleable.MenuView, defStyleAttr, 0);
76bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
778262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns        mBackground = a.getDrawable(R.styleable.MenuView_android_itemBackground);
78ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mTextAppearance = a.getResourceId(R.styleable.
798262949c1a6b7e191020b31fc914972bb0b58ab0Trevor Johns                MenuView_android_itemTextAppearance, -1);
80ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mPreserveIconSpacing = a.getBoolean(
81692b70462703c0c0c9e6c5dec315a9aa783b5f55Chris Banes                R.styleable.MenuView_preserveIconSpacing, false);
82ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mTextAppearanceContext = context;
836142a54baae3289f734947c6b5375b12eb0fb722Chris Banes        mSubMenuArrow = a.getDrawable(R.styleable.MenuView_subMenuArrow);
84bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
85ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        a.recycle();
86ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
87bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
88ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    @Override
89ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    protected void onFinishInflate() {
90ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        super.onFinishInflate();
91bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
9247082c30c630c34829439a9eecd1cf7e8d255a86Aurimas Liutikas        ViewCompat.setBackground(this, mBackground);
93bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
94ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mTitleView = (TextView) findViewById(R.id.title);
95ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (mTextAppearance != -1) {
96ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            mTitleView.setTextAppearance(mTextAppearanceContext,
9720ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns                    mTextAppearance);
98ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
99bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
100ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mShortcutView = (TextView) findViewById(R.id.shortcut);
1016142a54baae3289f734947c6b5375b12eb0fb722Chris Banes        mSubMenuArrowView = (ImageView) findViewById(R.id.submenuarrow);
1026142a54baae3289f734947c6b5375b12eb0fb722Chris Banes        if (mSubMenuArrowView != null) {
1036142a54baae3289f734947c6b5375b12eb0fb722Chris Banes            mSubMenuArrowView.setImageDrawable(mSubMenuArrow);
1046142a54baae3289f734947c6b5375b12eb0fb722Chris Banes        }
105ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
106bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
107ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public void initialize(MenuItemImpl itemData, int menuType) {
108ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mItemData = itemData;
109ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mMenuType = menuType;
110bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
111ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        setVisibility(itemData.isVisible() ? View.VISIBLE : View.GONE);
112bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
113ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        setTitle(itemData.getTitleForItemView(this));
114ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        setCheckable(itemData.isCheckable());
115ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        setShortcut(itemData.shouldShowShortcut(), itemData.getShortcut());
116ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        setIcon(itemData.getIcon());
117ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        setEnabled(itemData.isEnabled());
1186142a54baae3289f734947c6b5375b12eb0fb722Chris Banes        setSubMenuArrowVisible(itemData.hasSubMenu());
119bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell    }
120bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
121ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public void setForceShowIcon(boolean forceShow) {
122ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mPreserveIconSpacing = mForceShowIcon = forceShow;
123ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
124ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
125ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public void setTitle(CharSequence title) {
126ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (title != null) {
127ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            mTitleView.setText(title);
128bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
12949c78900da0d43140fb602431fb93212bd7f6c70Chris Banes            if (mTitleView.getVisibility() != VISIBLE) mTitleView.setVisibility(VISIBLE);
130ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        } else {
13149c78900da0d43140fb602431fb93212bd7f6c70Chris Banes            if (mTitleView.getVisibility() != GONE) mTitleView.setVisibility(GONE);
132ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
133bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell    }
134bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
135ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public MenuItemImpl getItemData() {
136ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return mItemData;
137bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell    }
138bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
139ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public void setCheckable(boolean checkable) {
140ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (!checkable && mRadioButton == null && mCheckBox == null) {
141ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            return;
142ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
143ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
144ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        // Depending on whether its exclusive check or not, the checkbox or
145ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        // radio button will be the one in use (and the other will be otherCompoundButton)
146ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        final CompoundButton compoundButton;
147ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        final CompoundButton otherCompoundButton;
148ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
149ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (mItemData.isExclusiveCheckable()) {
150ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            if (mRadioButton == null) {
151ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani                insertRadioButton();
152ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            }
153ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            compoundButton = mRadioButton;
154ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            otherCompoundButton = mCheckBox;
155ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        } else {
156ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            if (mCheckBox == null) {
157ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani                insertCheckBox();
158ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            }
159ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            compoundButton = mCheckBox;
160ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            otherCompoundButton = mRadioButton;
161ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
162ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
163ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (checkable) {
164ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            compoundButton.setChecked(mItemData.isChecked());
165ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
166ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            final int newVisibility = checkable ? VISIBLE : GONE;
167ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            if (compoundButton.getVisibility() != newVisibility) {
168ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani                compoundButton.setVisibility(newVisibility);
169ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            }
170ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
171ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            // Make sure the other compound button isn't visible
172ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            if (otherCompoundButton != null && otherCompoundButton.getVisibility() != GONE) {
173ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani                otherCompoundButton.setVisibility(GONE);
174ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            }
175ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        } else {
17620ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns            if (mCheckBox != null) {
17720ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns                mCheckBox.setVisibility(GONE);
17820ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns            }
17920ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns            if (mRadioButton != null) {
18020ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns                mRadioButton.setVisibility(GONE);
18120ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns            }
182ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
183bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell    }
184ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
185ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public void setChecked(boolean checked) {
186ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        CompoundButton compoundButton;
187ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
188ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (mItemData.isExclusiveCheckable()) {
189ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            if (mRadioButton == null) {
190ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani                insertRadioButton();
191ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            }
192ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            compoundButton = mRadioButton;
193ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        } else {
194ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            if (mCheckBox == null) {
195ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani                insertCheckBox();
196ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            }
197ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            compoundButton = mCheckBox;
198ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
199ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
200ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        compoundButton.setChecked(checked);
201bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell    }
202bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
2036142a54baae3289f734947c6b5375b12eb0fb722Chris Banes    private void setSubMenuArrowVisible(boolean hasSubmenu) {
2046142a54baae3289f734947c6b5375b12eb0fb722Chris Banes        if (mSubMenuArrowView != null) {
2056142a54baae3289f734947c6b5375b12eb0fb722Chris Banes            mSubMenuArrowView.setVisibility(hasSubmenu ? View.VISIBLE : View.GONE);
2066142a54baae3289f734947c6b5375b12eb0fb722Chris Banes        }
2076142a54baae3289f734947c6b5375b12eb0fb722Chris Banes    }
2086142a54baae3289f734947c6b5375b12eb0fb722Chris Banes
209ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public void setShortcut(boolean showShortcut, char shortcutKey) {
210ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        final int newVisibility = (showShortcut && mItemData.shouldShowShortcut())
21120ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns                ? VISIBLE : GONE;
212bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
213ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (newVisibility == VISIBLE) {
214ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            mShortcutView.setText(mItemData.getShortcutLabel());
215ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
216bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
217ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (mShortcutView.getVisibility() != newVisibility) {
218ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            mShortcutView.setVisibility(newVisibility);
219ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
220bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell    }
221bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
222ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public void setIcon(Drawable icon) {
223ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        final boolean showIcon = mItemData.shouldShowIcon() || mForceShowIcon;
224ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (!showIcon && !mPreserveIconSpacing) {
225ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            return;
226ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
227ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
228ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (mIconView == null && icon == null && !mPreserveIconSpacing) {
229ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            return;
230ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
231ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
232ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (mIconView == null) {
233ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            insertIconView();
234ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
235ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
236ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (icon != null || mPreserveIconSpacing) {
237ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            mIconView.setImageDrawable(showIcon ? icon : null);
238ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
239ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            if (mIconView.getVisibility() != VISIBLE) {
240ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani                mIconView.setVisibility(VISIBLE);
241ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            }
242ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        } else {
243ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            mIconView.setVisibility(GONE);
244ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
245bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell    }
246bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
247ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    @Override
248ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
249ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (mIconView != null && mPreserveIconSpacing) {
250ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            // Enforce minimum icon spacing
251ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            ViewGroup.LayoutParams lp = getLayoutParams();
252ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            LayoutParams iconLp = (LayoutParams) mIconView.getLayoutParams();
253ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            if (lp.height > 0 && iconLp.width <= 0) {
254ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani                iconLp.width = lp.height;
255ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani            }
256ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
257ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
258bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell    }
259bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
260ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private void insertIconView() {
261ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        LayoutInflater inflater = getInflater();
262ee7c9fb199e9b9af8d40a1f9e27d85465acf8301Chris Banes        mIconView = (ImageView) inflater.inflate(R.layout.abc_list_menu_item_icon,
26320ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns                this, false);
264ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        addView(mIconView, 0);
265bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell    }
266bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
267ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private void insertRadioButton() {
268ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        LayoutInflater inflater = getInflater();
269ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mRadioButton =
270ee7c9fb199e9b9af8d40a1f9e27d85465acf8301Chris Banes                (RadioButton) inflater.inflate(R.layout.abc_list_menu_item_radio,
27120ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns                        this, false);
272ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        addView(mRadioButton);
273bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell    }
274bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
275ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private void insertCheckBox() {
276ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        LayoutInflater inflater = getInflater();
277ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        mCheckBox =
278ee7c9fb199e9b9af8d40a1f9e27d85465acf8301Chris Banes                (CheckBox) inflater.inflate(R.layout.abc_list_menu_item_checkbox,
27920ac724a3a836bfd362c911f7dc55a61c02b4d44Trevor Johns                        this, false);
280ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        addView(mCheckBox);
281bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell    }
282bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
283ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public boolean prefersCondensedTitle() {
284ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return false;
285ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
286bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
287ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    public boolean showsIcon() {
288ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return mForceShowIcon;
289ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    }
290bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell
291ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani    private LayoutInflater getInflater() {
292ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        if (mInflater == null) {
2936142a54baae3289f734947c6b5375b12eb0fb722Chris Banes            mInflater = LayoutInflater.from(getContext());
294ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        }
295ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani        return mInflater;
296bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell    }
297bbbb8f39d1b1d1b317c5f9237f20fe6b1d9eb17fAdam Powell}
298ced50ab7536cd3d3573f03310fc899f10c414d37Anirudh Dewani
299