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