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