Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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; |
Adam Powell | d5c81db | 2012-08-02 14:35:26 -0700 | [diff] [blame] | 20 | import android.content.res.Configuration; |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 21 | import android.content.res.Resources; |
Adam Powell | 24340af | 2011-10-31 22:07:44 -0700 | [diff] [blame] | 22 | import android.content.res.TypedArray; |
Adam Powell | 7b5e9e6 | 2011-08-15 19:04:08 -0700 | [diff] [blame] | 23 | import android.graphics.Rect; |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 24 | import android.graphics.drawable.Drawable; |
Gilles Debunne | c619e74 | 2011-05-31 15:49:51 -0700 | [diff] [blame] | 25 | import android.text.TextUtils; |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 26 | import android.util.AttributeSet; |
Adam Powell | 7b5e9e6 | 2011-08-15 19:04:08 -0700 | [diff] [blame] | 27 | import android.view.Gravity; |
Adam Powell | 7bc3ca0 | 2011-08-26 18:29:58 -0700 | [diff] [blame] | 28 | import android.view.MotionEvent; |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 29 | import android.view.View; |
Adam Powell | 7bc3ca0 | 2011-08-26 18:29:58 -0700 | [diff] [blame] | 30 | import android.view.accessibility.AccessibilityEvent; |
Adam Powell | 75d022a | 2012-03-06 12:04:07 -0800 | [diff] [blame] | 31 | import android.widget.TextView; |
Adam Powell | 7b5e9e6 | 2011-08-15 19:04:08 -0700 | [diff] [blame] | 32 | import android.widget.Toast; |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * @hide |
| 36 | */ |
Adam Powell | 75d022a | 2012-03-06 12:04:07 -0800 | [diff] [blame] | 37 | public class ActionMenuItemView extends TextView |
Adam Powell | 7b5e9e6 | 2011-08-15 19:04:08 -0700 | [diff] [blame] | 38 | implements MenuView.ItemView, View.OnClickListener, View.OnLongClickListener, |
| 39 | ActionMenuView.ActionMenuChildView { |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 40 | private static final String TAG = "ActionMenuItemView"; |
Jeff Sharkey | a8a72c3 | 2010-07-31 18:27:13 -0700 | [diff] [blame] | 41 | |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 42 | private MenuItemImpl mItemData; |
| 43 | private CharSequence mTitle; |
Adam Powell | 75d022a | 2012-03-06 12:04:07 -0800 | [diff] [blame] | 44 | private Drawable mIcon; |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 45 | private MenuBuilder.ItemInvoker mItemInvoker; |
Jeff Sharkey | a8a72c3 | 2010-07-31 18:27:13 -0700 | [diff] [blame] | 46 | |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 47 | private boolean mAllowTextWithIcon; |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 48 | private boolean mExpandedFormat; |
Adam Powell | 24340af | 2011-10-31 22:07:44 -0700 | [diff] [blame] | 49 | private int mMinWidth; |
Adam Powell | 75d022a | 2012-03-06 12:04:07 -0800 | [diff] [blame] | 50 | private int mSavedPaddingLeft; |
Adam Powell | 1f9c7af | 2010-08-03 11:26:07 -0700 | [diff] [blame] | 51 | |
Adam Powell | c0047d4 | 2012-07-30 12:18:15 -0700 | [diff] [blame] | 52 | private static final int MAX_ICON_SIZE = 32; // dp |
| 53 | private int mMaxIconSize; |
| 54 | |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 55 | public ActionMenuItemView(Context context) { |
| 56 | this(context, null); |
| 57 | } |
Jeff Sharkey | a8a72c3 | 2010-07-31 18:27:13 -0700 | [diff] [blame] | 58 | |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 59 | public ActionMenuItemView(Context context, AttributeSet attrs) { |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 60 | this(context, attrs, 0); |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 61 | } |
Jeff Sharkey | a8a72c3 | 2010-07-31 18:27:13 -0700 | [diff] [blame] | 62 | |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 63 | public ActionMenuItemView(Context context, AttributeSet attrs, int defStyle) { |
| 64 | super(context, attrs, defStyle); |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 65 | final Resources res = context.getResources(); |
| 66 | mAllowTextWithIcon = res.getBoolean( |
| 67 | com.android.internal.R.bool.config_allowActionMenuItemTextWithIcon); |
Adam Powell | 24340af | 2011-10-31 22:07:44 -0700 | [diff] [blame] | 68 | TypedArray a = context.obtainStyledAttributes(attrs, |
| 69 | com.android.internal.R.styleable.ActionMenuItemView, 0, 0); |
| 70 | mMinWidth = a.getDimensionPixelSize( |
| 71 | com.android.internal.R.styleable.ActionMenuItemView_minWidth, 0); |
| 72 | a.recycle(); |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 73 | |
Adam Powell | c0047d4 | 2012-07-30 12:18:15 -0700 | [diff] [blame] | 74 | final float density = res.getDisplayMetrics().density; |
| 75 | mMaxIconSize = (int) (MAX_ICON_SIZE * density + 0.5f); |
| 76 | |
Adam Powell | 6bddd87 | 2011-03-24 15:34:54 -0700 | [diff] [blame] | 77 | setOnClickListener(this); |
Adam Powell | 7b5e9e6 | 2011-08-15 19:04:08 -0700 | [diff] [blame] | 78 | setOnLongClickListener(this); |
Adam Powell | 75d022a | 2012-03-06 12:04:07 -0800 | [diff] [blame] | 79 | |
Adam Powell | 367ee32 | 2012-05-06 18:32:33 -0700 | [diff] [blame] | 80 | mSavedPaddingLeft = -1; |
| 81 | } |
| 82 | |
| 83 | @Override |
Adam Powell | d5c81db | 2012-08-02 14:35:26 -0700 | [diff] [blame] | 84 | public void onConfigurationChanged(Configuration newConfig) { |
| 85 | super.onConfigurationChanged(newConfig); |
| 86 | |
| 87 | mAllowTextWithIcon = getContext().getResources().getBoolean( |
| 88 | com.android.internal.R.bool.config_allowActionMenuItemTextWithIcon); |
| 89 | updateTextButtonVisibility(); |
| 90 | } |
| 91 | |
| 92 | @Override |
Adam Powell | 367ee32 | 2012-05-06 18:32:33 -0700 | [diff] [blame] | 93 | public void setPadding(int l, int t, int r, int b) { |
| 94 | mSavedPaddingLeft = l; |
| 95 | super.setPadding(l, t, r, b); |
Adam Powell | 1f9c7af | 2010-08-03 11:26:07 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 98 | public MenuItemImpl getItemData() { |
| 99 | return mItemData; |
| 100 | } |
| 101 | |
| 102 | public void initialize(MenuItemImpl itemData, int menuType) { |
| 103 | mItemData = itemData; |
Jeff Sharkey | a8a72c3 | 2010-07-31 18:27:13 -0700 | [diff] [blame] | 104 | |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 105 | setIcon(itemData.getIcon()); |
Adam Powell | e7d4684 | 2011-01-13 21:36:09 -0800 | [diff] [blame] | 106 | setTitle(itemData.getTitleForItemView(this)); // Title only takes effect if there is no icon |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 107 | setId(itemData.getItemId()); |
Jeff Sharkey | a8a72c3 | 2010-07-31 18:27:13 -0700 | [diff] [blame] | 108 | |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 109 | setVisibility(itemData.isVisible() ? View.VISIBLE : View.GONE); |
| 110 | setEnabled(itemData.isEnabled()); |
| 111 | } |
Jeff Sharkey | a8a72c3 | 2010-07-31 18:27:13 -0700 | [diff] [blame] | 112 | |
Adam Powell | 1f9c7af | 2010-08-03 11:26:07 -0700 | [diff] [blame] | 113 | public void onClick(View v) { |
| 114 | if (mItemInvoker != null) { |
| 115 | mItemInvoker.invokeItem(mItemData); |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 116 | } |
| 117 | } |
Jeff Sharkey | a8a72c3 | 2010-07-31 18:27:13 -0700 | [diff] [blame] | 118 | |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 119 | public void setItemInvoker(MenuBuilder.ItemInvoker invoker) { |
| 120 | mItemInvoker = invoker; |
| 121 | } |
| 122 | |
| 123 | public boolean prefersCondensedTitle() { |
Adam Powell | e7d4684 | 2011-01-13 21:36:09 -0800 | [diff] [blame] | 124 | return true; |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | public void setCheckable(boolean checkable) { |
| 128 | // TODO Support checkable action items |
| 129 | } |
| 130 | |
| 131 | public void setChecked(boolean checked) { |
| 132 | // TODO Support checkable action items |
| 133 | } |
| 134 | |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 135 | public void setExpandedFormat(boolean expandedFormat) { |
| 136 | if (mExpandedFormat != expandedFormat) { |
| 137 | mExpandedFormat = expandedFormat; |
| 138 | if (mItemData != null) { |
| 139 | mItemData.actionFormatChanged(); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
Gilles Debunne | c619e74 | 2011-05-31 15:49:51 -0700 | [diff] [blame] | 144 | private void updateTextButtonVisibility() { |
Adam Powell | 75d022a | 2012-03-06 12:04:07 -0800 | [diff] [blame] | 145 | boolean visible = !TextUtils.isEmpty(mTitle); |
| 146 | visible &= mIcon == null || |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 147 | (mItemData.showsTextAsAction() && (mAllowTextWithIcon || mExpandedFormat)); |
| 148 | |
Adam Powell | 75d022a | 2012-03-06 12:04:07 -0800 | [diff] [blame] | 149 | setText(visible ? mTitle : null); |
Gilles Debunne | c619e74 | 2011-05-31 15:49:51 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 152 | public void setIcon(Drawable icon) { |
Adam Powell | 75d022a | 2012-03-06 12:04:07 -0800 | [diff] [blame] | 153 | mIcon = icon; |
Adam Powell | c1eea13 | 2012-07-31 11:27:19 -0700 | [diff] [blame] | 154 | if (icon != null) { |
| 155 | int width = icon.getIntrinsicWidth(); |
| 156 | int height = icon.getIntrinsicHeight(); |
| 157 | if (width > mMaxIconSize) { |
| 158 | final float scale = (float) mMaxIconSize / width; |
| 159 | width = mMaxIconSize; |
| 160 | height *= scale; |
| 161 | } |
| 162 | if (height > mMaxIconSize) { |
| 163 | final float scale = (float) mMaxIconSize / height; |
| 164 | height = mMaxIconSize; |
| 165 | width *= scale; |
| 166 | } |
| 167 | icon.setBounds(0, 0, width, height); |
Adam Powell | c0047d4 | 2012-07-30 12:18:15 -0700 | [diff] [blame] | 168 | } |
Adam Powell | c0047d4 | 2012-07-30 12:18:15 -0700 | [diff] [blame] | 169 | setCompoundDrawables(icon, null, null, null); |
Adam Powell | 9f125d3 | 2011-01-14 15:36:56 -0800 | [diff] [blame] | 170 | |
Gilles Debunne | c619e74 | 2011-05-31 15:49:51 -0700 | [diff] [blame] | 171 | updateTextButtonVisibility(); |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 172 | } |
Gilles Debunne | c619e74 | 2011-05-31 15:49:51 -0700 | [diff] [blame] | 173 | |
Adam Powell | be4d68e | 2010-10-08 18:16:34 -0700 | [diff] [blame] | 174 | public boolean hasText() { |
Adam Powell | 75d022a | 2012-03-06 12:04:07 -0800 | [diff] [blame] | 175 | return !TextUtils.isEmpty(getText()); |
Adam Powell | be4d68e | 2010-10-08 18:16:34 -0700 | [diff] [blame] | 176 | } |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 177 | |
| 178 | public void setShortcut(boolean showShortcut, char shortcutKey) { |
| 179 | // Action buttons don't show text for shortcut keys. |
| 180 | } |
| 181 | |
| 182 | public void setTitle(CharSequence title) { |
| 183 | mTitle = title; |
Jeff Sharkey | a8a72c3 | 2010-07-31 18:27:13 -0700 | [diff] [blame] | 184 | |
Adam Powell | 7bc3ca0 | 2011-08-26 18:29:58 -0700 | [diff] [blame] | 185 | setContentDescription(mTitle); |
Gilles Debunne | c619e74 | 2011-05-31 15:49:51 -0700 | [diff] [blame] | 186 | updateTextButtonVisibility(); |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Adam Powell | 7bc3ca0 | 2011-08-26 18:29:58 -0700 | [diff] [blame] | 189 | @Override |
| 190 | public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) { |
| 191 | onPopulateAccessibilityEvent(event); |
| 192 | return true; |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public void onPopulateAccessibilityEvent(AccessibilityEvent event) { |
| 197 | super.onPopulateAccessibilityEvent(event); |
| 198 | final CharSequence cdesc = getContentDescription(); |
| 199 | if (!TextUtils.isEmpty(cdesc)) { |
| 200 | event.getText().add(cdesc); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public boolean dispatchHoverEvent(MotionEvent event) { |
| 206 | // Don't allow children to hover; we want this to be treated as a single component. |
| 207 | return onHoverEvent(event); |
| 208 | } |
| 209 | |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 210 | public boolean showsIcon() { |
| 211 | return true; |
| 212 | } |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 213 | |
| 214 | public boolean needsDividerBefore() { |
| 215 | return hasText() && mItemData.getIcon() == null; |
| 216 | } |
| 217 | |
| 218 | public boolean needsDividerAfter() { |
| 219 | return hasText(); |
| 220 | } |
Adam Powell | 7b5e9e6 | 2011-08-15 19:04:08 -0700 | [diff] [blame] | 221 | |
| 222 | @Override |
| 223 | public boolean onLongClick(View v) { |
| 224 | if (hasText()) { |
| 225 | // Don't show the cheat sheet for items that already show text. |
| 226 | return false; |
| 227 | } |
| 228 | |
| 229 | final int[] screenPos = new int[2]; |
| 230 | final Rect displayFrame = new Rect(); |
| 231 | getLocationOnScreen(screenPos); |
| 232 | getWindowVisibleDisplayFrame(displayFrame); |
| 233 | |
| 234 | final Context context = getContext(); |
| 235 | final int width = getWidth(); |
| 236 | final int height = getHeight(); |
| 237 | final int midy = screenPos[1] + height / 2; |
| 238 | final int screenWidth = context.getResources().getDisplayMetrics().widthPixels; |
| 239 | |
| 240 | Toast cheatSheet = Toast.makeText(context, mItemData.getTitle(), Toast.LENGTH_SHORT); |
| 241 | if (midy < displayFrame.height()) { |
| 242 | // Show along the top; follow action buttons |
Fabrice Di Meglio | aac0d4e | 2012-07-19 19:21:26 -0700 | [diff] [blame] | 243 | cheatSheet.setGravity(Gravity.TOP | Gravity.END, |
Adam Powell | 7b5e9e6 | 2011-08-15 19:04:08 -0700 | [diff] [blame] | 244 | screenWidth - screenPos[0] - width / 2, height); |
| 245 | } else { |
| 246 | // Show along the bottom center |
| 247 | cheatSheet.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, height); |
| 248 | } |
| 249 | cheatSheet.show(); |
| 250 | return true; |
| 251 | } |
Adam Powell | 24340af | 2011-10-31 22:07:44 -0700 | [diff] [blame] | 252 | |
| 253 | @Override |
| 254 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Adam Powell | d5c81db | 2012-08-02 14:35:26 -0700 | [diff] [blame] | 255 | if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST) { |
| 256 | // Fill all available height. |
| 257 | heightMeasureSpec = MeasureSpec.makeMeasureSpec( |
| 258 | MeasureSpec.getSize(heightMeasureSpec), MeasureSpec.EXACTLY); |
| 259 | } |
Adam Powell | 75d022a | 2012-03-06 12:04:07 -0800 | [diff] [blame] | 260 | final boolean textVisible = hasText(); |
Adam Powell | 367ee32 | 2012-05-06 18:32:33 -0700 | [diff] [blame] | 261 | if (textVisible && mSavedPaddingLeft >= 0) { |
| 262 | super.setPadding(mSavedPaddingLeft, getPaddingTop(), |
| 263 | getPaddingRight(), getPaddingBottom()); |
Adam Powell | 75d022a | 2012-03-06 12:04:07 -0800 | [diff] [blame] | 264 | } |
| 265 | |
Adam Powell | 24340af | 2011-10-31 22:07:44 -0700 | [diff] [blame] | 266 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 267 | |
| 268 | final int widthMode = MeasureSpec.getMode(widthMeasureSpec); |
Adam Powell | 75d022a | 2012-03-06 12:04:07 -0800 | [diff] [blame] | 269 | final int widthSize = MeasureSpec.getSize(widthMeasureSpec); |
Adam Powell | 24340af | 2011-10-31 22:07:44 -0700 | [diff] [blame] | 270 | final int oldMeasuredWidth = getMeasuredWidth(); |
Adam Powell | 75d022a | 2012-03-06 12:04:07 -0800 | [diff] [blame] | 271 | final int targetWidth = widthMode == MeasureSpec.AT_MOST ? Math.min(widthSize, mMinWidth) |
Adam Powell | 24340af | 2011-10-31 22:07:44 -0700 | [diff] [blame] | 272 | : mMinWidth; |
| 273 | |
| 274 | if (widthMode != MeasureSpec.EXACTLY && mMinWidth > 0 && oldMeasuredWidth < targetWidth) { |
| 275 | // Remeasure at exactly the minimum width. |
| 276 | super.onMeasure(MeasureSpec.makeMeasureSpec(targetWidth, MeasureSpec.EXACTLY), |
| 277 | heightMeasureSpec); |
| 278 | } |
Adam Powell | 75d022a | 2012-03-06 12:04:07 -0800 | [diff] [blame] | 279 | |
| 280 | if (!textVisible && mIcon != null) { |
| 281 | // TextView won't center compound drawables in both dimensions without |
| 282 | // a little coercion. Pad in to center the icon after we've measured. |
| 283 | final int w = getMeasuredWidth(); |
Adam Powell | c0047d4 | 2012-07-30 12:18:15 -0700 | [diff] [blame] | 284 | final int dw = mIcon.getBounds().width(); |
Adam Powell | 367ee32 | 2012-05-06 18:32:33 -0700 | [diff] [blame] | 285 | super.setPadding((w - dw) / 2, getPaddingTop(), getPaddingRight(), getPaddingBottom()); |
Adam Powell | 75d022a | 2012-03-06 12:04:07 -0800 | [diff] [blame] | 286 | } |
Adam Powell | 24340af | 2011-10-31 22:07:44 -0700 | [diff] [blame] | 287 | } |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 288 | } |