ActionBarContextView.java revision 678ed0c5a32bac1ea18a5ac99b0b814421ea79b4
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;
19import com.android.internal.view.menu.ActionMenuPresenter;
20import com.android.internal.view.menu.ActionMenuView;
21import com.android.internal.view.menu.MenuBuilder;
22
23import android.animation.Animator;
24import android.animation.Animator.AnimatorListener;
25import android.animation.AnimatorSet;
26import android.animation.ObjectAnimator;
27import android.content.Context;
28import android.content.res.Configuration;
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
60    private Animator 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, com.android.internal.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                com.android.internal.R.styleable.ActionMode_background));
82        mTitleStyleRes = a.getResourceId(
83                com.android.internal.R.styleable.ActionMode_titleTextStyle, 0);
84        mSubtitleStyleRes = a.getResourceId(
85                com.android.internal.R.styleable.ActionMode_subtitleTextStyle, 0);
86
87        mContentHeight = a.getLayoutDimension(
88                com.android.internal.R.styleable.ActionMode_height, 0);
89
90        mSplitBackground = a.getDrawable(
91                com.android.internal.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 setSplitActionBar(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.setSplitActionBar(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.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(mContext, mTitleStyleRes);
184            }
185            if (mSubtitleStyleRes != 0) {
186                mSubtitleView.setTextAppearance(mContext, 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(mContext);
205            mClose = inflater.inflate(R.layout.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(mContext);
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);
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);
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        finishAnimation();
261        mAnimationMode = ANIMATE_OUT;
262        mCurrentAnimation = makeOutAnimation();
263        mCurrentAnimation.start();
264    }
265
266    private void finishAnimation() {
267        final Animator a = mCurrentAnimation;
268        if (a != null) {
269            mCurrentAnimation = null;
270            a.end();
271        }
272    }
273
274    public void killMode() {
275        finishAnimation();
276        removeAllViews();
277        if (mSplitView != null) {
278            mSplitView.removeView(mMenuView);
279        }
280        mCustomView = null;
281        mMenuView = null;
282        mAnimateInOnLayout = false;
283    }
284
285    @Override
286    public boolean showOverflowMenu() {
287        if (mActionMenuPresenter != null) {
288            return mActionMenuPresenter.showOverflowMenu();
289        }
290        return false;
291    }
292
293    @Override
294    public boolean hideOverflowMenu() {
295        if (mActionMenuPresenter != null) {
296            return mActionMenuPresenter.hideOverflowMenu();
297        }
298        return false;
299    }
300
301    @Override
302    public boolean isOverflowMenuShowing() {
303        if (mActionMenuPresenter != null) {
304            return mActionMenuPresenter.isOverflowMenuShowing();
305        }
306        return false;
307    }
308
309    @Override
310    protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
311        // Used by custom views if they don't supply layout params. Everything else
312        // added to an ActionBarContextView should have them already.
313        return new MarginLayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
314    }
315
316    @Override
317    public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
318        return new MarginLayoutParams(getContext(), attrs);
319    }
320
321    @Override
322    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
323        final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
324        if (widthMode != MeasureSpec.EXACTLY) {
325            throw new IllegalStateException(getClass().getSimpleName() + " can only be used " +
326                    "with android:layout_width=\"match_parent\" (or fill_parent)");
327        }
328
329        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
330        if (heightMode == MeasureSpec.UNSPECIFIED) {
331            throw new IllegalStateException(getClass().getSimpleName() + " can only be used " +
332                    "with android:layout_height=\"wrap_content\"");
333        }
334
335        final int contentWidth = MeasureSpec.getSize(widthMeasureSpec);
336
337        int maxHeight = mContentHeight > 0 ?
338                mContentHeight : MeasureSpec.getSize(heightMeasureSpec);
339
340        final int verticalPadding = getPaddingTop() + getPaddingBottom();
341        int availableWidth = contentWidth - getPaddingLeft() - getPaddingRight();
342        final int height = maxHeight - verticalPadding;
343        final int childSpecHeight = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST);
344
345        if (mClose != null) {
346            availableWidth = measureChildView(mClose, availableWidth, childSpecHeight, 0);
347            MarginLayoutParams lp = (MarginLayoutParams) mClose.getLayoutParams();
348            availableWidth -= lp.leftMargin + lp.rightMargin;
349        }
350
351        if (mMenuView != null && mMenuView.getParent() == this) {
352            availableWidth = measureChildView(mMenuView, availableWidth,
353                    childSpecHeight, 0);
354        }
355
356        if (mTitleLayout != null && mCustomView == null) {
357            availableWidth = measureChildView(mTitleLayout, availableWidth, childSpecHeight, 0);
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 Animator makeInAnimation() {
391        mClose.setTranslationX(-mClose.getWidth() -
392                ((MarginLayoutParams) mClose.getLayoutParams()).leftMargin);
393        ObjectAnimator buttonAnimator = ObjectAnimator.ofFloat(mClose, "translationX", 0);
394        buttonAnimator.setDuration(200);
395        buttonAnimator.addListener(this);
396        buttonAnimator.setInterpolator(new DecelerateInterpolator());
397
398        AnimatorSet set = new AnimatorSet();
399        AnimatorSet.Builder b = set.play(buttonAnimator);
400
401        if (mMenuView != null) {
402            final int count = mMenuView.getChildCount();
403            if (count > 0) {
404                for (int i = count - 1, j = 0; i >= 0; i--, j++) {
405                    View child = mMenuView.getChildAt(i);
406                    child.setScaleY(0);
407                    ObjectAnimator a = ObjectAnimator.ofFloat(child, "scaleY", 0, 1);
408                    a.setDuration(100);
409                    a.setStartDelay(j * 70);
410                    b.with(a);
411                }
412            }
413        }
414
415        return set;
416    }
417
418    private Animator makeOutAnimation() {
419        ObjectAnimator buttonAnimator = ObjectAnimator.ofFloat(mClose, "translationX",
420                -mClose.getWidth() - ((MarginLayoutParams) mClose.getLayoutParams()).leftMargin);
421        buttonAnimator.setDuration(200);
422        buttonAnimator.addListener(this);
423        buttonAnimator.setInterpolator(new DecelerateInterpolator());
424
425        AnimatorSet set = new AnimatorSet();
426        AnimatorSet.Builder b = set.play(buttonAnimator);
427
428        if (mMenuView != null) {
429            final int count = mMenuView.getChildCount();
430            if (count > 0) {
431                for (int i = 0; i < 0; i++) {
432                    View child = mMenuView.getChildAt(i);
433                    child.setScaleY(0);
434                    ObjectAnimator a = ObjectAnimator.ofFloat(child, "scaleY", 0);
435                    a.setDuration(100);
436                    a.setStartDelay(i * 70);
437                    b.with(a);
438                }
439            }
440        }
441
442        return set;
443    }
444
445    @Override
446    protected void onLayout(boolean changed, int l, int t, int r, int b) {
447        int x = getPaddingLeft();
448        final int y = getPaddingTop();
449        final int contentHeight = b - t - getPaddingTop() - getPaddingBottom();
450
451        if (mClose != null && mClose.getVisibility() != GONE) {
452            MarginLayoutParams lp = (MarginLayoutParams) mClose.getLayoutParams();
453            x += lp.leftMargin;
454            x += positionChild(mClose, x, y, contentHeight);
455            x += lp.rightMargin;
456
457            if (mAnimateInOnLayout) {
458                mAnimationMode = ANIMATE_IN;
459                mCurrentAnimation = makeInAnimation();
460                mCurrentAnimation.start();
461                mAnimateInOnLayout = false;
462            }
463        }
464
465        if (mTitleLayout != null && mCustomView == null) {
466            x += positionChild(mTitleLayout, x, y, contentHeight);
467        }
468
469        if (mCustomView != null) {
470            x += positionChild(mCustomView, x, y, contentHeight);
471        }
472
473        x = r - l - getPaddingRight();
474
475        if (mMenuView != null) {
476            x -= positionChildInverse(mMenuView, x, y, contentHeight);
477        }
478    }
479
480    @Override
481    public void onAnimationStart(Animator animation) {
482    }
483
484    @Override
485    public void onAnimationEnd(Animator animation) {
486        if (mAnimationMode == ANIMATE_OUT) {
487            killMode();
488        }
489        mAnimationMode = ANIMATE_IDLE;
490    }
491
492    @Override
493    public void onAnimationCancel(Animator animation) {
494    }
495
496    @Override
497    public void onAnimationRepeat(Animator animation) {
498    }
499
500    @Override
501    public boolean shouldDelayChildPressedState() {
502        return false;
503    }
504
505    @Override
506    public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
507        if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
508            // Action mode started
509            event.setSource(this);
510            event.setClassName(getClass().getName());
511            event.setPackageName(getContext().getPackageName());
512            event.setContentDescription(mTitle);
513        } else {
514            super.onInitializeAccessibilityEvent(event);
515        }
516    }
517}
518