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.Configuration;
21import android.content.res.Resources;
22import android.content.res.TypedArray;
23import android.graphics.Rect;
24import android.graphics.drawable.Drawable;
25import android.os.Build;
26import android.support.v4.view.GravityCompat;
27import android.support.v4.view.ViewCompat;
28import android.support.v7.appcompat.R;
29import android.support.v7.widget.ActionMenuView;
30import android.support.v7.widget.AppCompatTextView;
31import android.support.v7.widget.ListPopupWindow;
32import android.text.TextUtils;
33import android.util.AttributeSet;
34import android.view.Gravity;
35import android.view.MotionEvent;
36import android.view.View;
37import android.widget.Toast;
38
39/**
40 * @hide
41 */
42public class ActionMenuItemView extends AppCompatTextView
43        implements MenuView.ItemView, View.OnClickListener, View.OnLongClickListener,
44        ActionMenuView.ActionMenuChildView {
45
46    private static final String TAG = "ActionMenuItemView";
47
48    private MenuItemImpl mItemData;
49    private CharSequence mTitle;
50    private Drawable mIcon;
51    private MenuBuilder.ItemInvoker mItemInvoker;
52    private ListPopupWindow.ForwardingListener mForwardingListener;
53    private PopupCallback mPopupCallback;
54
55    private boolean mAllowTextWithIcon;
56    private boolean mExpandedFormat;
57    private int mMinWidth;
58    private int mSavedPaddingLeft;
59
60    private static final int MAX_ICON_SIZE = 32; // dp
61    private int mMaxIconSize;
62
63    public ActionMenuItemView(Context context) {
64        this(context, null);
65    }
66
67    public ActionMenuItemView(Context context, AttributeSet attrs) {
68        this(context, attrs, 0);
69    }
70
71    public ActionMenuItemView(Context context, AttributeSet attrs, int defStyle) {
72        super(context, attrs, defStyle);
73        final Resources res = context.getResources();
74        mAllowTextWithIcon = res.getBoolean(
75                R.bool.abc_config_allowActionMenuItemTextWithIcon);
76        TypedArray a = context.obtainStyledAttributes(attrs,
77                R.styleable.ActionMenuItemView, defStyle, 0);
78        mMinWidth = a.getDimensionPixelSize(
79                R.styleable.ActionMenuItemView_android_minWidth, 0);
80        a.recycle();
81
82        final float density = res.getDisplayMetrics().density;
83        mMaxIconSize = (int) (MAX_ICON_SIZE * density + 0.5f);
84
85        setOnClickListener(this);
86        setOnLongClickListener(this);
87
88        mSavedPaddingLeft = -1;
89    }
90
91    public void onConfigurationChanged(Configuration newConfig) {
92        if (Build.VERSION.SDK_INT >= 8) {
93            super.onConfigurationChanged(newConfig);
94        }
95
96        mAllowTextWithIcon = getContext().getResources().getBoolean(
97                R.bool.abc_config_allowActionMenuItemTextWithIcon);
98        updateTextButtonVisibility();
99    }
100
101    @Override
102    public void setPadding(int l, int t, int r, int b) {
103        mSavedPaddingLeft = l;
104        super.setPadding(l, t, r, b);
105    }
106
107    public MenuItemImpl getItemData() {
108        return mItemData;
109    }
110
111    public void initialize(MenuItemImpl itemData, int menuType) {
112        mItemData = itemData;
113
114        setIcon(itemData.getIcon());
115        setTitle(itemData.getTitleForItemView(this)); // Title only takes effect if there is no icon
116        setId(itemData.getItemId());
117
118        setVisibility(itemData.isVisible() ? View.VISIBLE : View.GONE);
119        setEnabled(itemData.isEnabled());
120        if (itemData.hasSubMenu()) {
121            if (mForwardingListener == null) {
122                mForwardingListener = new ActionMenuItemForwardingListener();
123            }
124        }
125    }
126
127    @Override
128    public boolean onTouchEvent(MotionEvent e) {
129        if (mItemData.hasSubMenu() && mForwardingListener != null
130                && mForwardingListener.onTouch(this, e)) {
131            return true;
132        }
133        return super.onTouchEvent(e);
134    }
135
136    @Override
137    public void onClick(View v) {
138        if (mItemInvoker != null) {
139            mItemInvoker.invokeItem(mItemData);
140        }
141    }
142
143    public void setItemInvoker(MenuBuilder.ItemInvoker invoker) {
144        mItemInvoker = invoker;
145    }
146
147    public void setPopupCallback(PopupCallback popupCallback) {
148        mPopupCallback = popupCallback;
149    }
150
151    public boolean prefersCondensedTitle() {
152        return true;
153    }
154
155    public void setCheckable(boolean checkable) {
156        // TODO Support checkable action items
157    }
158
159    public void setChecked(boolean checked) {
160        // TODO Support checkable action items
161    }
162
163    public void setExpandedFormat(boolean expandedFormat) {
164        if (mExpandedFormat != expandedFormat) {
165            mExpandedFormat = expandedFormat;
166            if (mItemData != null) {
167                mItemData.actionFormatChanged();
168            }
169        }
170    }
171
172    private void updateTextButtonVisibility() {
173        boolean visible = !TextUtils.isEmpty(mTitle);
174        visible &= mIcon == null ||
175                (mItemData.showsTextAsAction() && (mAllowTextWithIcon || mExpandedFormat));
176
177        setText(visible ? mTitle : null);
178    }
179
180    public void setIcon(Drawable icon) {
181        mIcon = icon;
182        if (icon != null) {
183            int width = icon.getIntrinsicWidth();
184            int height = icon.getIntrinsicHeight();
185            if (width > mMaxIconSize) {
186                final float scale = (float) mMaxIconSize / width;
187                width = mMaxIconSize;
188                height *= scale;
189            }
190            if (height > mMaxIconSize) {
191                final float scale = (float) mMaxIconSize / height;
192                height = mMaxIconSize;
193                width *= scale;
194            }
195            icon.setBounds(0, 0, width, height);
196        }
197        setCompoundDrawables(icon, null, null, null);
198
199        updateTextButtonVisibility();
200    }
201
202    public boolean hasText() {
203        return !TextUtils.isEmpty(getText());
204    }
205
206    public void setShortcut(boolean showShortcut, char shortcutKey) {
207        // Action buttons don't show text for shortcut keys.
208    }
209
210    public void setTitle(CharSequence title) {
211        mTitle = title;
212
213        setContentDescription(mTitle);
214        updateTextButtonVisibility();
215    }
216
217    public boolean showsIcon() {
218        return true;
219    }
220
221    public boolean needsDividerBefore() {
222        return hasText() && mItemData.getIcon() == null;
223    }
224
225    public boolean needsDividerAfter() {
226        return hasText();
227    }
228
229    @Override
230    public boolean onLongClick(View v) {
231        if (hasText()) {
232            // Don't show the cheat sheet for items that already show text.
233            return false;
234        }
235
236        final int[] screenPos = new int[2];
237        final Rect displayFrame = new Rect();
238        getLocationOnScreen(screenPos);
239        getWindowVisibleDisplayFrame(displayFrame);
240
241        final Context context = getContext();
242        final int width = getWidth();
243        final int height = getHeight();
244        final int midy = screenPos[1] + height / 2;
245        int referenceX = screenPos[0] + width / 2;
246        if (ViewCompat.getLayoutDirection(v) == ViewCompat.LAYOUT_DIRECTION_LTR) {
247            final int screenWidth = context.getResources().getDisplayMetrics().widthPixels;
248            referenceX = screenWidth - referenceX; // mirror
249        }
250        Toast cheatSheet = Toast.makeText(context, mItemData.getTitle(), Toast.LENGTH_SHORT);
251        if (midy < displayFrame.height()) {
252            // Show along the top; follow action buttons
253            cheatSheet.setGravity(Gravity.TOP | GravityCompat.END, referenceX,
254                    screenPos[1] + height - displayFrame.top);
255        } else {
256            // Show along the bottom center
257            cheatSheet.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, height);
258        }
259        cheatSheet.show();
260        return true;
261    }
262
263    @Override
264    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
265        final boolean textVisible = hasText();
266        if (textVisible && mSavedPaddingLeft >= 0) {
267            super.setPadding(mSavedPaddingLeft, getPaddingTop(),
268                    getPaddingRight(), getPaddingBottom());
269        }
270
271        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
272
273        final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
274        final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
275        final int oldMeasuredWidth = getMeasuredWidth();
276        final int targetWidth = widthMode == MeasureSpec.AT_MOST ? Math.min(widthSize, mMinWidth)
277                : mMinWidth;
278
279        if (widthMode != MeasureSpec.EXACTLY && mMinWidth > 0 && oldMeasuredWidth < targetWidth) {
280            // Remeasure at exactly the minimum width.
281            super.onMeasure(MeasureSpec.makeMeasureSpec(targetWidth, MeasureSpec.EXACTLY),
282                    heightMeasureSpec);
283        }
284
285        if (!textVisible && mIcon != null) {
286            // TextView won't center compound drawables in both dimensions without
287            // a little coercion. Pad in to center the icon after we've measured.
288            final int w = getMeasuredWidth();
289            final int dw = mIcon.getBounds().width();
290            super.setPadding((w - dw) / 2, getPaddingTop(), getPaddingRight(), getPaddingBottom());
291        }
292    }
293
294    private class ActionMenuItemForwardingListener extends ListPopupWindow.ForwardingListener {
295        public ActionMenuItemForwardingListener() {
296            super(ActionMenuItemView.this);
297        }
298
299        @Override
300        public ListPopupWindow getPopup() {
301            if (mPopupCallback != null) {
302                return mPopupCallback.getPopup();
303            }
304            return null;
305        }
306
307        @Override
308        protected boolean onForwardingStarted() {
309            // Call the invoker, then check if the expected popup is showing.
310            if (mItemInvoker != null && mItemInvoker.invokeItem(mItemData)) {
311                final ListPopupWindow popup = getPopup();
312                return popup != null && popup.isShowing();
313            }
314            return false;
315        }
316
317        // Do not backport the framework impl here.
318        // The framework's ListPopupWindow uses an animation before performing the item click
319        // after selecting an item. As AppCompat doesn't use an animation, the popup is
320        // dismissed and thus null'ed out before onForwardingStopped() has been called.
321        // This messes up ActionMenuItemView's onForwardingStopped() impl since it will now
322        // return false and make ListPopupWindow think it's still forwarding.
323    }
324
325    public static abstract class PopupCallback {
326        public abstract ListPopupWindow getPopup();
327    }
328}
329