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 android.support.v7.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.support.v7.appcompat.R; 25import android.support.v7.internal.widget.CompatTextView; 26import android.text.TextUtils; 27import android.text.method.TransformationMethod; 28import android.util.AttributeSet; 29import android.view.Gravity; 30import android.view.View; 31import android.widget.TextView; 32import android.widget.Toast; 33 34import java.util.Locale; 35 36/** 37 * @hide 38 */ 39public class ActionMenuItemView extends CompatTextView 40 implements MenuView.ItemView, View.OnClickListener, View.OnLongClickListener, 41 ActionMenuView.ActionMenuChildView { 42 43 private static final String TAG = "ActionMenuItemView"; 44 45 private MenuItemImpl mItemData; 46 private CharSequence mTitle; 47 private Drawable mIcon; 48 private MenuBuilder.ItemInvoker mItemInvoker; 49 50 private boolean mAllowTextWithIcon; 51 private boolean mExpandedFormat; 52 private int mMinWidth; 53 private int mSavedPaddingLeft; 54 55 public ActionMenuItemView(Context context) { 56 this(context, null); 57 } 58 59 public ActionMenuItemView(Context context, AttributeSet attrs) { 60 this(context, attrs, 0); 61 } 62 63 public ActionMenuItemView(Context context, AttributeSet attrs, int defStyle) { 64 super(context, attrs, defStyle); 65 final Resources res = context.getResources(); 66 mAllowTextWithIcon = res.getBoolean( 67 android.support.v7.appcompat.R.bool.abc_config_allowActionMenuItemTextWithIcon); 68 TypedArray a = context.obtainStyledAttributes(attrs, 69 android.support.v7.appcompat.R.styleable.ActionMenuItemView, 0, 0); 70 mMinWidth = a.getDimensionPixelSize( 71 R.styleable.ActionMenuItemView_android_minWidth, 0); 72 a.recycle(); 73 74 setOnClickListener(this); 75 setOnLongClickListener(this); 76 77 setTransformationMethod(new AllCapsTransformationMethod()); 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 setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null); 145 146 updateTextButtonVisibility(); 147 } 148 149 public boolean hasText() { 150 return !TextUtils.isEmpty(getText()); 151 } 152 153 public void setShortcut(boolean showShortcut, char shortcutKey) { 154 // Action buttons don't show text for shortcut keys. 155 } 156 157 public void setTitle(CharSequence title) { 158 mTitle = title; 159 160 setContentDescription(mTitle); 161 updateTextButtonVisibility(); 162 } 163 164 public boolean showsIcon() { 165 return true; 166 } 167 168 public boolean needsDividerBefore() { 169 return hasText() && mItemData.getIcon() == null; 170 } 171 172 public boolean needsDividerAfter() { 173 return hasText(); 174 } 175 176 @Override 177 public boolean onLongClick(View v) { 178 if (hasText()) { 179 // Don't show the cheat sheet for items that already show text. 180 return false; 181 } 182 183 final int[] screenPos = new int[2]; 184 final Rect displayFrame = new Rect(); 185 getLocationOnScreen(screenPos); 186 getWindowVisibleDisplayFrame(displayFrame); 187 188 final Context context = getContext(); 189 final int width = getWidth(); 190 final int height = getHeight(); 191 final int midy = screenPos[1] + height / 2; 192 final int screenWidth = context.getResources().getDisplayMetrics().widthPixels; 193 194 Toast cheatSheet = Toast.makeText(context, mItemData.getTitle(), Toast.LENGTH_SHORT); 195 if (midy < displayFrame.height()) { 196 // Show along the top; follow action buttons 197 cheatSheet.setGravity(Gravity.TOP | Gravity.RIGHT, 198 screenWidth - screenPos[0] - width / 2, height); 199 } else { 200 // Show along the bottom center 201 cheatSheet.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, height); 202 } 203 cheatSheet.show(); 204 return true; 205 } 206 207 @Override 208 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 209 final boolean textVisible = hasText(); 210 if (textVisible && mSavedPaddingLeft >= 0) { 211 super.setPadding(mSavedPaddingLeft, getPaddingTop(), 212 getPaddingRight(), getPaddingBottom()); 213 } 214 215 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 216 217 final int widthMode = MeasureSpec.getMode(widthMeasureSpec); 218 final int widthSize = MeasureSpec.getSize(widthMeasureSpec); 219 final int oldMeasuredWidth = getMeasuredWidth(); 220 final int targetWidth = widthMode == MeasureSpec.AT_MOST ? Math.min(widthSize, mMinWidth) 221 : mMinWidth; 222 223 if (widthMode != MeasureSpec.EXACTLY && mMinWidth > 0 && oldMeasuredWidth < targetWidth) { 224 // Remeasure at exactly the minimum width. 225 super.onMeasure(MeasureSpec.makeMeasureSpec(targetWidth, MeasureSpec.EXACTLY), 226 heightMeasureSpec); 227 } 228 229 if (!textVisible && mIcon != null) { 230 // TextView won't center compound drawables in both dimensions without 231 // a little coercion. Pad in to center the icon after we've measured. 232 final int w = getMeasuredWidth(); 233 final int dw = mIcon.getIntrinsicWidth(); 234 super.setPadding((w - dw) / 2, getPaddingTop(), getPaddingRight(), getPaddingBottom()); 235 } 236 } 237 238 private class AllCapsTransformationMethod implements TransformationMethod { 239 private Locale mLocale; 240 241 public AllCapsTransformationMethod() { 242 mLocale = getContext().getResources().getConfiguration().locale; 243 } 244 245 @Override 246 public CharSequence getTransformation(CharSequence source, View view) { 247 return source != null ? source.toString().toUpperCase(mLocale) : null; 248 } 249 250 @Override 251 public void onFocusChanged(View view, CharSequence sourceText, boolean focused, 252 int direction, Rect previouslyFocusedRect) { 253 } 254 } 255} 256