ActionMenuItemView.java revision c1eea136eaae62091b44d7b06a3d61ac694e4fd7
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 if (icon != null) { 145 int width = icon.getIntrinsicWidth(); 146 int height = icon.getIntrinsicHeight(); 147 if (width > mMaxIconSize) { 148 final float scale = (float) mMaxIconSize / width; 149 width = mMaxIconSize; 150 height *= scale; 151 } 152 if (height > mMaxIconSize) { 153 final float scale = (float) mMaxIconSize / height; 154 height = mMaxIconSize; 155 width *= scale; 156 } 157 icon.setBounds(0, 0, width, height); 158 } 159 setCompoundDrawables(icon, null, null, null); 160 161 updateTextButtonVisibility(); 162 } 163 164 public boolean hasText() { 165 return !TextUtils.isEmpty(getText()); 166 } 167 168 public void setShortcut(boolean showShortcut, char shortcutKey) { 169 // Action buttons don't show text for shortcut keys. 170 } 171 172 public void setTitle(CharSequence title) { 173 mTitle = title; 174 175 setContentDescription(mTitle); 176 updateTextButtonVisibility(); 177 } 178 179 @Override 180 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) { 181 onPopulateAccessibilityEvent(event); 182 return true; 183 } 184 185 @Override 186 public void onPopulateAccessibilityEvent(AccessibilityEvent event) { 187 super.onPopulateAccessibilityEvent(event); 188 final CharSequence cdesc = getContentDescription(); 189 if (!TextUtils.isEmpty(cdesc)) { 190 event.getText().add(cdesc); 191 } 192 } 193 194 @Override 195 public boolean dispatchHoverEvent(MotionEvent event) { 196 // Don't allow children to hover; we want this to be treated as a single component. 197 return onHoverEvent(event); 198 } 199 200 public boolean showsIcon() { 201 return true; 202 } 203 204 public boolean needsDividerBefore() { 205 return hasText() && mItemData.getIcon() == null; 206 } 207 208 public boolean needsDividerAfter() { 209 return hasText(); 210 } 211 212 @Override 213 public boolean onLongClick(View v) { 214 if (hasText()) { 215 // Don't show the cheat sheet for items that already show text. 216 return false; 217 } 218 219 final int[] screenPos = new int[2]; 220 final Rect displayFrame = new Rect(); 221 getLocationOnScreen(screenPos); 222 getWindowVisibleDisplayFrame(displayFrame); 223 224 final Context context = getContext(); 225 final int width = getWidth(); 226 final int height = getHeight(); 227 final int midy = screenPos[1] + height / 2; 228 final int screenWidth = context.getResources().getDisplayMetrics().widthPixels; 229 230 Toast cheatSheet = Toast.makeText(context, mItemData.getTitle(), Toast.LENGTH_SHORT); 231 if (midy < displayFrame.height()) { 232 // Show along the top; follow action buttons 233 cheatSheet.setGravity(Gravity.TOP | Gravity.END, 234 screenWidth - screenPos[0] - width / 2, height); 235 } else { 236 // Show along the bottom center 237 cheatSheet.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, height); 238 } 239 cheatSheet.show(); 240 return true; 241 } 242 243 @Override 244 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 245 final boolean textVisible = hasText(); 246 if (textVisible && mSavedPaddingLeft >= 0) { 247 super.setPadding(mSavedPaddingLeft, getPaddingTop(), 248 getPaddingRight(), getPaddingBottom()); 249 } 250 251 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 252 253 final int widthMode = MeasureSpec.getMode(widthMeasureSpec); 254 final int widthSize = MeasureSpec.getSize(widthMeasureSpec); 255 final int oldMeasuredWidth = getMeasuredWidth(); 256 final int targetWidth = widthMode == MeasureSpec.AT_MOST ? Math.min(widthSize, mMinWidth) 257 : mMinWidth; 258 259 if (widthMode != MeasureSpec.EXACTLY && mMinWidth > 0 && oldMeasuredWidth < targetWidth) { 260 // Remeasure at exactly the minimum width. 261 super.onMeasure(MeasureSpec.makeMeasureSpec(targetWidth, MeasureSpec.EXACTLY), 262 heightMeasureSpec); 263 } 264 265 if (!textVisible && mIcon != null) { 266 // TextView won't center compound drawables in both dimensions without 267 // a little coercion. Pad in to center the icon after we've measured. 268 final int w = getMeasuredWidth(); 269 final int dw = mIcon.getBounds().width(); 270 super.setPadding((w - dw) / 2, getPaddingTop(), getPaddingRight(), getPaddingBottom()); 271 } 272 } 273} 274