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