The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 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 | |
| 17 | package com.android.internal.view.menu; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.content.res.TypedArray; |
| 21 | import android.graphics.drawable.Drawable; |
| 22 | import android.util.AttributeSet; |
| 23 | import android.view.LayoutInflater; |
| 24 | import android.view.View; |
Adam Powell | 9151103 | 2011-07-13 10:24:06 -0700 | [diff] [blame] | 25 | import android.view.ViewGroup; |
Alan Viverette | 058ac7c | 2013-08-19 16:44:30 -0700 | [diff] [blame] | 26 | import android.view.accessibility.AccessibilityNodeInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import android.widget.CheckBox; |
| 28 | import android.widget.CompoundButton; |
| 29 | import android.widget.ImageView; |
Alan Viverette | d9ddf52 | 2013-07-15 16:43:58 -0700 | [diff] [blame] | 30 | import android.widget.LinearLayout; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | import android.widget.RadioButton; |
| 32 | import android.widget.TextView; |
| 33 | |
| 34 | /** |
| 35 | * The item view for each item in the ListView-based MenuViews. |
| 36 | */ |
Alan Viverette | d9ddf52 | 2013-07-15 16:43:58 -0700 | [diff] [blame] | 37 | public class ListMenuItemView extends LinearLayout implements MenuView.ItemView { |
| 38 | private static final String TAG = "ListMenuItemView"; |
| 39 | private MenuItemImpl mItemData; |
| 40 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | private ImageView mIconView; |
| 42 | private RadioButton mRadioButton; |
| 43 | private TextView mTitleView; |
| 44 | private CheckBox mCheckBox; |
| 45 | private TextView mShortcutView; |
Alan Viverette | d9ddf52 | 2013-07-15 16:43:58 -0700 | [diff] [blame] | 46 | |
| 47 | private Drawable mBackground; |
| 48 | private int mTextAppearance; |
| 49 | private Context mTextAppearanceContext; |
| 50 | private boolean mPreserveIconSpacing; |
| 51 | |
| 52 | private int mMenuType; |
| 53 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 54 | private LayoutInflater mInflater; |
| 55 | |
Adam Powell | 9151103 | 2011-07-13 10:24:06 -0700 | [diff] [blame] | 56 | private boolean mForceShowIcon; |
| 57 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | public ListMenuItemView(Context context, AttributeSet attrs, int defStyle) { |
| 59 | super(context, attrs); |
Alan Viverette | d9ddf52 | 2013-07-15 16:43:58 -0700 | [diff] [blame] | 60 | |
| 61 | TypedArray a = |
| 62 | context.obtainStyledAttributes( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | attrs, com.android.internal.R.styleable.MenuView, defStyle, 0); |
Alan Viverette | d9ddf52 | 2013-07-15 16:43:58 -0700 | [diff] [blame] | 64 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | mBackground = a.getDrawable(com.android.internal.R.styleable.MenuView_itemBackground); |
Alan Viverette | d9ddf52 | 2013-07-15 16:43:58 -0700 | [diff] [blame] | 66 | mTextAppearance = a.getResourceId(com.android.internal.R.styleable. |
| 67 | MenuView_itemTextAppearance, -1); |
Adam Powell | e7d4684 | 2011-01-13 21:36:09 -0800 | [diff] [blame] | 68 | mPreserveIconSpacing = a.getBoolean( |
| 69 | com.android.internal.R.styleable.MenuView_preserveIconSpacing, false); |
Alan Viverette | d9ddf52 | 2013-07-15 16:43:58 -0700 | [diff] [blame] | 70 | mTextAppearanceContext = context; |
| 71 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | a.recycle(); |
| 73 | } |
| 74 | |
| 75 | public ListMenuItemView(Context context, AttributeSet attrs) { |
| 76 | this(context, attrs, 0); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | protected void onFinishInflate() { |
| 81 | super.onFinishInflate(); |
Alan Viverette | d9ddf52 | 2013-07-15 16:43:58 -0700 | [diff] [blame] | 82 | |
| 83 | setBackgroundDrawable(mBackground); |
| 84 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | mTitleView = (TextView) findViewById(com.android.internal.R.id.title); |
Alan Viverette | d9ddf52 | 2013-07-15 16:43:58 -0700 | [diff] [blame] | 86 | if (mTextAppearance != -1) { |
| 87 | mTitleView.setTextAppearance(mTextAppearanceContext, |
| 88 | mTextAppearance); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | } |
Alan Viverette | d9ddf52 | 2013-07-15 16:43:58 -0700 | [diff] [blame] | 90 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | mShortcutView = (TextView) findViewById(com.android.internal.R.id.shortcut); |
| 92 | } |
| 93 | |
| 94 | public void initialize(MenuItemImpl itemData, int menuType) { |
| 95 | mItemData = itemData; |
Alan Viverette | d9ddf52 | 2013-07-15 16:43:58 -0700 | [diff] [blame] | 96 | mMenuType = menuType; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | |
| 98 | setVisibility(itemData.isVisible() ? View.VISIBLE : View.GONE); |
Alan Viverette | d9ddf52 | 2013-07-15 16:43:58 -0700 | [diff] [blame] | 99 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | setTitle(itemData.getTitleForItemView(this)); |
| 101 | setCheckable(itemData.isCheckable()); |
| 102 | setShortcut(itemData.shouldShowShortcut(), itemData.getShortcut()); |
| 103 | setIcon(itemData.getIcon()); |
| 104 | setEnabled(itemData.isEnabled()); |
| 105 | } |
| 106 | |
Adam Powell | 9151103 | 2011-07-13 10:24:06 -0700 | [diff] [blame] | 107 | public void setForceShowIcon(boolean forceShow) { |
| 108 | mPreserveIconSpacing = mForceShowIcon = forceShow; |
| 109 | } |
| 110 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | public void setTitle(CharSequence title) { |
| 112 | if (title != null) { |
| 113 | mTitleView.setText(title); |
Alan Viverette | d9ddf52 | 2013-07-15 16:43:58 -0700 | [diff] [blame] | 114 | |
| 115 | if (mTitleView.getVisibility() != VISIBLE) mTitleView.setVisibility(VISIBLE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | } else { |
Alan Viverette | d9ddf52 | 2013-07-15 16:43:58 -0700 | [diff] [blame] | 117 | if (mTitleView.getVisibility() != GONE) mTitleView.setVisibility(GONE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | } |
| 119 | } |
Alan Viverette | d9ddf52 | 2013-07-15 16:43:58 -0700 | [diff] [blame] | 120 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | public MenuItemImpl getItemData() { |
| 122 | return mItemData; |
| 123 | } |
| 124 | |
| 125 | public void setCheckable(boolean checkable) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | if (!checkable && mRadioButton == null && mCheckBox == null) { |
| 127 | return; |
| 128 | } |
Alan Viverette | d9ddf52 | 2013-07-15 16:43:58 -0700 | [diff] [blame] | 129 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | // Depending on whether its exclusive check or not, the checkbox or |
| 131 | // radio button will be the one in use (and the other will be otherCompoundButton) |
| 132 | final CompoundButton compoundButton; |
Alan Viverette | d9ddf52 | 2013-07-15 16:43:58 -0700 | [diff] [blame] | 133 | final CompoundButton otherCompoundButton; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 134 | |
| 135 | if (mItemData.isExclusiveCheckable()) { |
Adam Powell | 8eeb4d1 | 2011-10-12 17:48:05 -0700 | [diff] [blame] | 136 | if (mRadioButton == null) { |
| 137 | insertRadioButton(); |
| 138 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 139 | compoundButton = mRadioButton; |
| 140 | otherCompoundButton = mCheckBox; |
| 141 | } else { |
Adam Powell | 8eeb4d1 | 2011-10-12 17:48:05 -0700 | [diff] [blame] | 142 | if (mCheckBox == null) { |
| 143 | insertCheckBox(); |
| 144 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | compoundButton = mCheckBox; |
| 146 | otherCompoundButton = mRadioButton; |
| 147 | } |
Alan Viverette | d9ddf52 | 2013-07-15 16:43:58 -0700 | [diff] [blame] | 148 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | if (checkable) { |
| 150 | compoundButton.setChecked(mItemData.isChecked()); |
Alan Viverette | d9ddf52 | 2013-07-15 16:43:58 -0700 | [diff] [blame] | 151 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | final int newVisibility = checkable ? VISIBLE : GONE; |
| 153 | if (compoundButton.getVisibility() != newVisibility) { |
| 154 | compoundButton.setVisibility(newVisibility); |
| 155 | } |
Alan Viverette | d9ddf52 | 2013-07-15 16:43:58 -0700 | [diff] [blame] | 156 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 157 | // Make sure the other compound button isn't visible |
Adam Powell | 8eeb4d1 | 2011-10-12 17:48:05 -0700 | [diff] [blame] | 158 | if (otherCompoundButton != null && otherCompoundButton.getVisibility() != GONE) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | otherCompoundButton.setVisibility(GONE); |
| 160 | } |
| 161 | } else { |
Alan Viverette | d9ddf52 | 2013-07-15 16:43:58 -0700 | [diff] [blame] | 162 | if (mCheckBox != null) mCheckBox.setVisibility(GONE); |
| 163 | if (mRadioButton != null) mRadioButton.setVisibility(GONE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | } |
| 165 | } |
Alan Viverette | d9ddf52 | 2013-07-15 16:43:58 -0700 | [diff] [blame] | 166 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 167 | public void setChecked(boolean checked) { |
| 168 | CompoundButton compoundButton; |
Alan Viverette | d9ddf52 | 2013-07-15 16:43:58 -0700 | [diff] [blame] | 169 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 170 | if (mItemData.isExclusiveCheckable()) { |
| 171 | if (mRadioButton == null) { |
| 172 | insertRadioButton(); |
| 173 | } |
| 174 | compoundButton = mRadioButton; |
| 175 | } else { |
| 176 | if (mCheckBox == null) { |
| 177 | insertCheckBox(); |
| 178 | } |
| 179 | compoundButton = mCheckBox; |
| 180 | } |
Alan Viverette | d9ddf52 | 2013-07-15 16:43:58 -0700 | [diff] [blame] | 181 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 182 | compoundButton.setChecked(checked); |
| 183 | } |
| 184 | |
| 185 | public void setShortcut(boolean showShortcut, char shortcutKey) { |
Alan Viverette | d9ddf52 | 2013-07-15 16:43:58 -0700 | [diff] [blame] | 186 | final int newVisibility = (showShortcut && mItemData.shouldShowShortcut()) |
| 187 | ? VISIBLE : GONE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 188 | |
| 189 | if (newVisibility == VISIBLE) { |
| 190 | mShortcutView.setText(mItemData.getShortcutLabel()); |
| 191 | } |
| 192 | |
| 193 | if (mShortcutView.getVisibility() != newVisibility) { |
| 194 | mShortcutView.setVisibility(newVisibility); |
| 195 | } |
| 196 | } |
Alan Viverette | d9ddf52 | 2013-07-15 16:43:58 -0700 | [diff] [blame] | 197 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | public void setIcon(Drawable icon) { |
Adam Powell | 9151103 | 2011-07-13 10:24:06 -0700 | [diff] [blame] | 199 | final boolean showIcon = mItemData.shouldShowIcon() || mForceShowIcon; |
Adam Powell | e7d4684 | 2011-01-13 21:36:09 -0800 | [diff] [blame] | 200 | if (!showIcon && !mPreserveIconSpacing) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | return; |
| 202 | } |
Alan Viverette | d9ddf52 | 2013-07-15 16:43:58 -0700 | [diff] [blame] | 203 | |
Adam Powell | 9151103 | 2011-07-13 10:24:06 -0700 | [diff] [blame] | 204 | if (mIconView == null && icon == null && !mPreserveIconSpacing) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 205 | return; |
| 206 | } |
Alan Viverette | d9ddf52 | 2013-07-15 16:43:58 -0700 | [diff] [blame] | 207 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | if (mIconView == null) { |
| 209 | insertIconView(); |
| 210 | } |
Alan Viverette | d9ddf52 | 2013-07-15 16:43:58 -0700 | [diff] [blame] | 211 | |
Adam Powell | e7d4684 | 2011-01-13 21:36:09 -0800 | [diff] [blame] | 212 | if (icon != null || mPreserveIconSpacing) { |
| 213 | mIconView.setImageDrawable(showIcon ? icon : null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | |
| 215 | if (mIconView.getVisibility() != VISIBLE) { |
| 216 | mIconView.setVisibility(VISIBLE); |
| 217 | } |
| 218 | } else { |
| 219 | mIconView.setVisibility(GONE); |
| 220 | } |
| 221 | } |
Alan Viverette | d9ddf52 | 2013-07-15 16:43:58 -0700 | [diff] [blame] | 222 | |
Adam Powell | 9151103 | 2011-07-13 10:24:06 -0700 | [diff] [blame] | 223 | @Override |
| 224 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 225 | if (mIconView != null && mPreserveIconSpacing) { |
| 226 | // Enforce minimum icon spacing |
Alan Viverette | d9ddf52 | 2013-07-15 16:43:58 -0700 | [diff] [blame] | 227 | ViewGroup.LayoutParams lp = getLayoutParams(); |
| 228 | LayoutParams iconLp = (LayoutParams) mIconView.getLayoutParams(); |
Adam Powell | 9151103 | 2011-07-13 10:24:06 -0700 | [diff] [blame] | 229 | if (lp.height > 0 && iconLp.width <= 0) { |
| 230 | iconLp.width = lp.height; |
| 231 | } |
| 232 | } |
| 233 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 234 | } |
| 235 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 236 | private void insertIconView() { |
Alan Viverette | d9ddf52 | 2013-07-15 16:43:58 -0700 | [diff] [blame] | 237 | LayoutInflater inflater = getInflater(); |
| 238 | mIconView = (ImageView) inflater.inflate(com.android.internal.R.layout.list_menu_item_icon, |
| 239 | this, false); |
| 240 | addView(mIconView, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 241 | } |
Alan Viverette | d9ddf52 | 2013-07-15 16:43:58 -0700 | [diff] [blame] | 242 | |
Alan Viverette | 8933efd | 2013-06-26 17:38:17 -0700 | [diff] [blame] | 243 | private void insertRadioButton() { |
Alan Viverette | d9ddf52 | 2013-07-15 16:43:58 -0700 | [diff] [blame] | 244 | LayoutInflater inflater = getInflater(); |
| 245 | mRadioButton = |
| 246 | (RadioButton) inflater.inflate(com.android.internal.R.layout.list_menu_item_radio, |
| 247 | this, false); |
Adam Powell | c394512 | 2013-07-10 11:51:33 -0700 | [diff] [blame] | 248 | addView(mRadioButton); |
Alan Viverette | 8933efd | 2013-06-26 17:38:17 -0700 | [diff] [blame] | 249 | } |
Alan Viverette | d9ddf52 | 2013-07-15 16:43:58 -0700 | [diff] [blame] | 250 | |
Alan Viverette | 8933efd | 2013-06-26 17:38:17 -0700 | [diff] [blame] | 251 | private void insertCheckBox() { |
Alan Viverette | d9ddf52 | 2013-07-15 16:43:58 -0700 | [diff] [blame] | 252 | LayoutInflater inflater = getInflater(); |
| 253 | mCheckBox = |
| 254 | (CheckBox) inflater.inflate(com.android.internal.R.layout.list_menu_item_checkbox, |
| 255 | this, false); |
Adam Powell | c394512 | 2013-07-10 11:51:33 -0700 | [diff] [blame] | 256 | addView(mCheckBox); |
Alan Viverette | 8933efd | 2013-06-26 17:38:17 -0700 | [diff] [blame] | 257 | } |
| 258 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 259 | public boolean prefersCondensedTitle() { |
| 260 | return false; |
| 261 | } |
| 262 | |
| 263 | public boolean showsIcon() { |
Adam Powell | 9151103 | 2011-07-13 10:24:06 -0700 | [diff] [blame] | 264 | return mForceShowIcon; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 265 | } |
Alan Viverette | d9ddf52 | 2013-07-15 16:43:58 -0700 | [diff] [blame] | 266 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 267 | private LayoutInflater getInflater() { |
| 268 | if (mInflater == null) { |
| 269 | mInflater = LayoutInflater.from(mContext); |
| 270 | } |
| 271 | return mInflater; |
| 272 | } |
Alan Viverette | 058ac7c | 2013-08-19 16:44:30 -0700 | [diff] [blame] | 273 | |
| 274 | @Override |
| 275 | public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { |
| 276 | super.onInitializeAccessibilityNodeInfo(info); |
| 277 | |
| 278 | if (mItemData != null && mItemData.hasSubMenu()) { |
Svetoslav Ganov | cb8ed39 | 2013-08-23 20:37:28 -0700 | [diff] [blame] | 279 | info.setCanOpenPopup(true); |
Alan Viverette | 058ac7c | 2013-08-19 16:44:30 -0700 | [diff] [blame] | 280 | } |
| 281 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 282 | } |