ActionBarContextView.java revision 08c7116ab9cd04ad6dd3c04aa1017237e7f409ac
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 */
16package com.android.internal.widget;
17
18import com.android.internal.R;
19
20import android.widget.ActionMenuPresenter;
21import android.widget.ActionMenuView;
22import com.android.internal.view.menu.MenuBuilder;
23
24import android.animation.Animator;
25import android.animation.Animator.AnimatorListener;
26import android.animation.AnimatorSet;
27import android.animation.ObjectAnimator;
28import android.content.Context;
29import android.content.res.TypedArray;
30import android.graphics.drawable.Drawable;
31import android.text.TextUtils;
32import android.util.AttributeSet;
33import android.view.ActionMode;
34import android.view.LayoutInflater;
35import android.view.View;
36import android.view.ViewGroup;
37import android.view.accessibility.AccessibilityEvent;
38import android.view.animation.DecelerateInterpolator;
39import android.widget.LinearLayout;
40import android.widget.TextView;
41
42/**
43 * @hide
44 */
45public class ActionBarContextView extends AbsActionBarView implements AnimatorListener {
46    private static final String TAG = "ActionBarContextView";
47
48    private CharSequence mTitle;
49    private CharSequence mSubtitle;
50
51    private View mClose;
52    private View mCustomView;
53    private LinearLayout mTitleLayout;
54    private TextView mTitleView;
55    private TextView mSubtitleView;
56    private int mTitleStyleRes;
57    private int mSubtitleStyleRes;
58    private Drawable mSplitBackground;
59    private boolean mTitleOptional;
60    private int mCloseItemLayout;
61
62    private Animator mCurrentAnimation;
63    private boolean mAnimateInOnLayout;
64    private int mAnimationMode;
65
66    private static final int ANIMATE_IDLE = 0;
67    private static final int ANIMATE_IN = 1;
68    private static final int ANIMATE_OUT = 2;
69
70    public ActionBarContextView(Context context) {
71        this(context, null);
72    }
73
74    public ActionBarContextView(Context context, AttributeSet attrs) {
75        this(context, attrs, com.android.internal.R.attr.actionModeStyle);
76    }
77
78    public ActionBarContextView(Context context, AttributeSet attrs, int defStyleAttr) {
79        this(context, attrs, defStyleAttr, 0);
80    }
81
82    public ActionBarContextView(
83            Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
84        super(context, attrs, defStyleAttr, defStyleRes);
85
86        final TypedArray a = context.obtainStyledAttributes(
87                attrs, R.styleable.ActionMode, defStyleAttr, defStyleRes);
88        setBackground(a.getDrawable(
89                com.android.internal.R.styleable.ActionMode_background));
90        mTitleStyleRes = a.getResourceId(
91                com.android.internal.R.styleable.ActionMode_titleTextStyle, 0);
92        mSubtitleStyleRes = a.getResourceId(
93                com.android.internal.R.styleable.ActionMode_subtitleTextStyle, 0);
94
95        mContentHeight = a.getLayoutDimension(
96                com.android.internal.R.styleable.ActionMode_height, 0);
97
98        mSplitBackground = a.getDrawable(
99                com.android.internal.R.styleable.ActionMode_backgroundSplit);
100
101        mCloseItemLayout = a.getResourceId(
102                com.android.internal.R.styleable.ActionMode_closeItemLayout,
103                R.layout.action_mode_close_item);
104
105        a.recycle();
106    }
107
108    @Override
109    public void onDetachedFromWindow() {
110        super.onDetachedFromWindow();
111        if (mActionMenuPresenter != null) {
112            mActionMenuPresenter.hideOverflowMenu();
113            mActionMenuPresenter.hideSubMenus();
114        }
115    }
116
117    @Override
118    public void setSplitToolbar(boolean split) {
119        if (mSplitActionBar != split) {
120            if (mActionMenuPresenter != null) {
121                // Mode is already active; move everything over and adjust the menu itself.
122                final LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
123                        LayoutParams.MATCH_PARENT);
124                if (!split) {
125                    mMenuView = (ActionMenuView) mActionMenuPresenter.getMenuView(this);
126                    mMenuView.setBackground(null);
127                    final ViewGroup oldParent = (ViewGroup) mMenuView.getParent();
128                    if (oldParent != null) oldParent.removeView(mMenuView);
129                    addView(mMenuView, layoutParams);
130                } else {
131                    // Allow full screen width in split mode.
132                    mActionMenuPresenter.setWidthLimit(
133                            getContext().getResources().getDisplayMetrics().widthPixels, true);
134                    // No limit to the item count; use whatever will fit.
135                    mActionMenuPresenter.setItemLimit(Integer.MAX_VALUE);
136                    // Span the whole width
137                    layoutParams.width = LayoutParams.MATCH_PARENT;
138                    layoutParams.height = mContentHeight;
139                    mMenuView = (ActionMenuView) mActionMenuPresenter.getMenuView(this);
140                    mMenuView.setBackground(mSplitBackground);
141                    final ViewGroup oldParent = (ViewGroup) mMenuView.getParent();
142                    if (oldParent != null) oldParent.removeView(mMenuView);
143                    mSplitView.addView(mMenuView, layoutParams);
144                }
145            }
146            super.setSplitToolbar(split);
147        }
148    }
149
150    public void setContentHeight(int height) {
151        mContentHeight = height;
152    }
153
154    public void setCustomView(View view) {
155        if (mCustomView != null) {
156            removeView(mCustomView);
157        }
158        mCustomView = view;
159        if (view != null && mTitleLayout != null) {
160            removeView(mTitleLayout);
161            mTitleLayout = null;
162        }
163        if (view != null) {
164            addView(view);
165        }
166        requestLayout();
167    }
168
169    public void setTitle(CharSequence title) {
170        mTitle = title;
171        initTitle();
172    }
173
174    public void setSubtitle(CharSequence subtitle) {
175        mSubtitle = subtitle;
176        initTitle();
177    }
178
179    public CharSequence getTitle() {
180        return mTitle;
181    }
182
183    public CharSequence getSubtitle() {
184        return mSubtitle;
185    }
186
187    private void initTitle() {
188        if (mTitleLayout == null) {
189            LayoutInflater inflater = LayoutInflater.from(getContext());
190            inflater.inflate(R.layout.action_bar_title_item, this);
191            mTitleLayout = (LinearLayout) getChildAt(getChildCount() - 1);
192            mTitleView = (TextView) mTitleLayout.findViewById(R.id.action_bar_title);
193            mSubtitleView = (TextView) mTitleLayout.findViewById(R.id.action_bar_subtitle);
194            if (mTitleStyleRes != 0) {
195                mTitleView.setTextAppearance(mContext, mTitleStyleRes);
196            }
197            if (mSubtitleStyleRes != 0) {
198                mSubtitleView.setTextAppearance(mContext, mSubtitleStyleRes);
199            }
200        }
201
202        mTitleView.setText(mTitle);
203        mSubtitleView.setText(mSubtitle);
204
205        final boolean hasTitle = !TextUtils.isEmpty(mTitle);
206        final boolean hasSubtitle = !TextUtils.isEmpty(mSubtitle);
207        mSubtitleView.setVisibility(hasSubtitle ? VISIBLE : GONE);
208        mTitleLayout.setVisibility(hasTitle || hasSubtitle ? VISIBLE : GONE);
209        if (mTitleLayout.getParent() == null) {
210            addView(mTitleLayout);
211        }
212    }
213
214    public void initForMode(final ActionMode mode) {
215        if (mClose == null) {
216            LayoutInflater inflater = LayoutInflater.from(mContext);
217            mClose = inflater.inflate(mCloseItemLayout, this, false);
218            addView(mClose);
219        } else if (mClose.getParent() == null) {
220            addView(mClose);
221        }
222
223        View closeButton = mClose.findViewById(R.id.action_mode_close_button);
224        closeButton.setOnClickListener(new OnClickListener() {
225            public void onClick(View v) {
226                mode.finish();
227            }
228        });
229
230        final MenuBuilder menu = (MenuBuilder) mode.getMenu();
231        if (mActionMenuPresenter != null) {
232            mActionMenuPresenter.dismissPopupMenus();
233        }
234        mActionMenuPresenter = new ActionMenuPresenter(mContext);
235        mActionMenuPresenter.setReserveOverflow(true);
236
237        final LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
238                LayoutParams.MATCH_PARENT);
239        if (!mSplitActionBar) {
240            menu.addMenuPresenter(mActionMenuPresenter, mPopupContext);
241            mMenuView = (ActionMenuView) mActionMenuPresenter.getMenuView(this);
242            mMenuView.setBackground(null);
243            addView(mMenuView, layoutParams);
244        } else {
245            // Allow full screen width in split mode.
246            mActionMenuPresenter.setWidthLimit(
247                    getContext().getResources().getDisplayMetrics().widthPixels, true);
248            // No limit to the item count; use whatever will fit.
249            mActionMenuPresenter.setItemLimit(Integer.MAX_VALUE);
250            // Span the whole width
251            layoutParams.width = LayoutParams.MATCH_PARENT;
252            layoutParams.height = mContentHeight;
253            menu.addMenuPresenter(mActionMenuPresenter, mPopupContext);
254            mMenuView = (ActionMenuView) mActionMenuPresenter.getMenuView(this);
255            mMenuView.setBackgroundDrawable(mSplitBackground);
256            mSplitView.addView(mMenuView, layoutParams);
257        }
258
259        mAnimateInOnLayout = true;
260    }
261
262    public void closeMode() {
263        if (mAnimationMode == ANIMATE_OUT) {
264            // Called again during close; just finish what we were doing.
265            return;
266        }
267        if (mClose == null) {
268            killMode();
269            return;
270        }
271
272        finishAnimation();
273        mAnimationMode = ANIMATE_OUT;
274        mCurrentAnimation = makeOutAnimation();
275        mCurrentAnimation.start();
276    }
277
278    private void finishAnimation() {
279        final Animator a = mCurrentAnimation;
280        if (a != null) {
281            mCurrentAnimation = null;
282            a.end();
283        }
284    }
285
286    public void killMode() {
287        finishAnimation();
288        removeAllViews();
289        if (mSplitView != null) {
290            mSplitView.removeView(mMenuView);
291        }
292        mCustomView = null;
293        mMenuView = null;
294        mAnimateInOnLayout = false;
295    }
296
297    @Override
298    public boolean showOverflowMenu() {
299        if (mActionMenuPresenter != null) {
300            return mActionMenuPresenter.showOverflowMenu();
301        }
302        return false;
303    }
304
305    @Override
306    public boolean hideOverflowMenu() {
307        if (mActionMenuPresenter != null) {
308            return mActionMenuPresenter.hideOverflowMenu();
309        }
310        return false;
311    }
312
313    @Override
314    public boolean isOverflowMenuShowing() {
315        if (mActionMenuPresenter != null) {
316            return mActionMenuPresenter.isOverflowMenuShowing();
317        }
318        return false;
319    }
320
321    @Override
322    protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
323        // Used by custom views if they don't supply layout params. Everything else
324        // added to an ActionBarContextView should have them already.
325        return new MarginLayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
326    }
327
328    @Override
329    public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
330        return new MarginLayoutParams(getContext(), attrs);
331    }
332
333    @Override
334    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
335        final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
336        if (widthMode != MeasureSpec.EXACTLY) {
337            throw new IllegalStateException(getClass().getSimpleName() + " can only be used " +
338                    "with android:layout_width=\"match_parent\" (or fill_parent)");
339        }
340
341        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
342        if (heightMode == MeasureSpec.UNSPECIFIED) {
343            throw new IllegalStateException(getClass().getSimpleName() + " can only be used " +
344                    "with android:layout_height=\"wrap_content\"");
345        }
346
347        final int contentWidth = MeasureSpec.getSize(widthMeasureSpec);
348
349        int maxHeight = mContentHeight > 0 ?
350                mContentHeight : MeasureSpec.getSize(heightMeasureSpec);
351
352        final int verticalPadding = getPaddingTop() + getPaddingBottom();
353        int availableWidth = contentWidth - getPaddingLeft() - getPaddingRight();
354        final int height = maxHeight - verticalPadding;
355        final int childSpecHeight = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST);
356
357        if (mClose != null) {
358            availableWidth = measureChildView(mClose, availableWidth, childSpecHeight, 0);
359            MarginLayoutParams lp = (MarginLayoutParams) mClose.getLayoutParams();
360            availableWidth -= lp.leftMargin + lp.rightMargin;
361        }
362
363        if (mMenuView != null && mMenuView.getParent() == this) {
364            availableWidth = measureChildView(mMenuView, availableWidth,
365                    childSpecHeight, 0);
366        }
367
368        if (mTitleLayout != null && mCustomView == null) {
369            if (mTitleOptional) {
370                final int titleWidthSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
371                mTitleLayout.measure(titleWidthSpec, childSpecHeight);
372                final int titleWidth = mTitleLayout.getMeasuredWidth();
373                final boolean titleFits = titleWidth <= availableWidth;
374                if (titleFits) {
375                    availableWidth -= titleWidth;
376                }
377                mTitleLayout.setVisibility(titleFits ? VISIBLE : GONE);
378            } else {
379                availableWidth = measureChildView(mTitleLayout, availableWidth, childSpecHeight, 0);
380            }
381        }
382
383        if (mCustomView != null) {
384            ViewGroup.LayoutParams lp = mCustomView.getLayoutParams();
385            final int customWidthMode = lp.width != LayoutParams.WRAP_CONTENT ?
386                    MeasureSpec.EXACTLY : MeasureSpec.AT_MOST;
387            final int customWidth = lp.width >= 0 ?
388                    Math.min(lp.width, availableWidth) : availableWidth;
389            final int customHeightMode = lp.height != LayoutParams.WRAP_CONTENT ?
390                    MeasureSpec.EXACTLY : MeasureSpec.AT_MOST;
391            final int customHeight = lp.height >= 0 ?
392                    Math.min(lp.height, height) : height;
393            mCustomView.measure(MeasureSpec.makeMeasureSpec(customWidth, customWidthMode),
394                    MeasureSpec.makeMeasureSpec(customHeight, customHeightMode));
395        }
396
397        if (mContentHeight <= 0) {
398            int measuredHeight = 0;
399            final int count = getChildCount();
400            for (int i = 0; i < count; i++) {
401                View v = getChildAt(i);
402                int paddedViewHeight = v.getMeasuredHeight() + verticalPadding;
403                if (paddedViewHeight > measuredHeight) {
404                    measuredHeight = paddedViewHeight;
405                }
406            }
407            setMeasuredDimension(contentWidth, measuredHeight);
408        } else {
409            setMeasuredDimension(contentWidth, maxHeight);
410        }
411    }
412
413    private Animator makeInAnimation() {
414        mClose.setTranslationX(-mClose.getWidth() -
415                ((MarginLayoutParams) mClose.getLayoutParams()).leftMargin);
416        ObjectAnimator buttonAnimator = ObjectAnimator.ofFloat(mClose, "translationX", 0);
417        buttonAnimator.setDuration(200);
418        buttonAnimator.addListener(this);
419        buttonAnimator.setInterpolator(new DecelerateInterpolator());
420
421        AnimatorSet set = new AnimatorSet();
422        AnimatorSet.Builder b = set.play(buttonAnimator);
423
424        if (mMenuView != null) {
425            final int count = mMenuView.getChildCount();
426            if (count > 0) {
427                for (int i = count - 1, j = 0; i >= 0; i--, j++) {
428                    View child = mMenuView.getChildAt(i);
429                    child.setScaleY(0);
430                    ObjectAnimator a = ObjectAnimator.ofFloat(child, "scaleY", 0, 1);
431                    a.setDuration(300);
432                    b.with(a);
433                }
434            }
435        }
436
437        return set;
438    }
439
440    private Animator makeOutAnimation() {
441        ObjectAnimator buttonAnimator = ObjectAnimator.ofFloat(mClose, "translationX",
442                -mClose.getWidth() - ((MarginLayoutParams) mClose.getLayoutParams()).leftMargin);
443        buttonAnimator.setDuration(200);
444        buttonAnimator.addListener(this);
445        buttonAnimator.setInterpolator(new DecelerateInterpolator());
446
447        AnimatorSet set = new AnimatorSet();
448        AnimatorSet.Builder b = set.play(buttonAnimator);
449
450        if (mMenuView != null) {
451            final int count = mMenuView.getChildCount();
452            if (count > 0) {
453                for (int i = 0; i < 0; i++) {
454                    View child = mMenuView.getChildAt(i);
455                    child.setScaleY(0);
456                    ObjectAnimator a = ObjectAnimator.ofFloat(child, "scaleY", 0);
457                    a.setDuration(300);
458                    b.with(a);
459                }
460            }
461        }
462
463        return set;
464    }
465
466    @Override
467    protected void onLayout(boolean changed, int l, int t, int r, int b) {
468        final boolean isLayoutRtl = isLayoutRtl();
469        int x = isLayoutRtl ? r - l - getPaddingRight() : getPaddingLeft();
470        final int y = getPaddingTop();
471        final int contentHeight = b - t - getPaddingTop() - getPaddingBottom();
472
473        if (mClose != null && mClose.getVisibility() != GONE) {
474            MarginLayoutParams lp = (MarginLayoutParams) mClose.getLayoutParams();
475            final int startMargin = (isLayoutRtl ? lp.rightMargin : lp.leftMargin);
476            final int endMargin = (isLayoutRtl ? lp.leftMargin : lp.rightMargin);
477            x = next(x, startMargin, isLayoutRtl);
478            x += positionChild(mClose, x, y, contentHeight, isLayoutRtl);
479            x = next(x, endMargin, isLayoutRtl);
480
481            if (mAnimateInOnLayout) {
482                mAnimationMode = ANIMATE_IN;
483                mCurrentAnimation = makeInAnimation();
484                mCurrentAnimation.start();
485                mAnimateInOnLayout = false;
486            }
487        }
488
489        if (mTitleLayout != null && mCustomView == null && mTitleLayout.getVisibility() != GONE) {
490            x += positionChild(mTitleLayout, x, y, contentHeight, isLayoutRtl);
491        }
492
493        if (mCustomView != null) {
494            x += positionChild(mCustomView, x, y, contentHeight, isLayoutRtl);
495        }
496
497        x = isLayoutRtl ? getPaddingLeft() : r - l - getPaddingRight();
498
499        if (mMenuView != null) {
500            x += positionChild(mMenuView, x, y, contentHeight, !isLayoutRtl);
501        }
502    }
503
504    @Override
505    public void onAnimationStart(Animator animation) {
506    }
507
508    @Override
509    public void onAnimationEnd(Animator animation) {
510        if (mAnimationMode == ANIMATE_OUT) {
511            killMode();
512        }
513        mAnimationMode = ANIMATE_IDLE;
514    }
515
516    @Override
517    public void onAnimationCancel(Animator animation) {
518    }
519
520    @Override
521    public void onAnimationRepeat(Animator animation) {
522    }
523
524    @Override
525    public boolean shouldDelayChildPressedState() {
526        return false;
527    }
528
529    @Override
530    public void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
531        if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
532            // Action mode started
533            event.setSource(this);
534            event.setClassName(getClass().getName());
535            event.setPackageName(getContext().getPackageName());
536            event.setContentDescription(mTitle);
537        } else {
538            super.onInitializeAccessibilityEventInternal(event);
539        }
540    }
541
542    public void setTitleOptional(boolean titleOptional) {
543        if (titleOptional != mTitleOptional) {
544            requestLayout();
545        }
546        mTitleOptional = titleOptional;
547    }
548
549    public boolean isTitleOptional() {
550        return mTitleOptional;
551    }
552}
553