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