ActionBarContextView.java revision a05aba9c506cd12a753c53e060c289095c3477e9
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 setSplitActionBar(boolean split) {
98        if (mSplitActionBar != split) {
99            if (mActionMenuPresenter != null) {
100                // Mode is already active; move everything over and adjust the menu itself.
101                final LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
102                        LayoutParams.MATCH_PARENT);
103                if (!split) {
104                    mMenuView = (ActionMenuView) mActionMenuPresenter.getMenuView(this);
105                    mMenuView.setBackgroundDrawable(null);
106                    final ViewGroup oldParent = (ViewGroup) mMenuView.getParent();
107                    if (oldParent != null) oldParent.removeView(mMenuView);
108                    addView(mMenuView, layoutParams);
109                } else {
110                    // Allow full screen width in split mode.
111                    mActionMenuPresenter.setWidthLimit(
112                            getContext().getResources().getDisplayMetrics().widthPixels, true);
113                    // No limit to the item count; use whatever will fit.
114                    mActionMenuPresenter.setItemLimit(Integer.MAX_VALUE);
115                    // Span the whole width
116                    layoutParams.width = LayoutParams.MATCH_PARENT;
117                    layoutParams.height = mContentHeight;
118                    mMenuView = (ActionMenuView) mActionMenuPresenter.getMenuView(this);
119                    mMenuView.setBackgroundDrawable(mSplitBackground);
120                    final ViewGroup oldParent = (ViewGroup) mMenuView.getParent();
121                    if (oldParent != null) oldParent.removeView(mMenuView);
122                    mSplitView.addView(mMenuView, layoutParams);
123                }
124            }
125            super.setSplitActionBar(split);
126        }
127    }
128
129    public void setContentHeight(int height) {
130        mContentHeight = height;
131    }
132
133    public void setCustomView(View view) {
134        if (mCustomView != null) {
135            removeView(mCustomView);
136        }
137        mCustomView = view;
138        if (mTitleLayout != null) {
139            removeView(mTitleLayout);
140            mTitleLayout = null;
141        }
142        if (view != null) {
143            addView(view);
144        }
145        requestLayout();
146    }
147
148    public void setTitle(CharSequence title) {
149        mTitle = title;
150        initTitle();
151    }
152
153    public void setSubtitle(CharSequence subtitle) {
154        mSubtitle = subtitle;
155        initTitle();
156    }
157
158    public CharSequence getTitle() {
159        return mTitle;
160    }
161
162    public CharSequence getSubtitle() {
163        return mSubtitle;
164    }
165
166    private void initTitle() {
167        if (mTitleLayout == null) {
168            LayoutInflater inflater = LayoutInflater.from(getContext());
169            inflater.inflate(R.layout.action_bar_title_item, this);
170            mTitleLayout = (LinearLayout) getChildAt(getChildCount() - 1);
171            mTitleView = (TextView) mTitleLayout.findViewById(R.id.action_bar_title);
172            mSubtitleView = (TextView) mTitleLayout.findViewById(R.id.action_bar_subtitle);
173            if (mTitleStyleRes != 0) {
174                mTitleView.setTextAppearance(mContext, mTitleStyleRes);
175            }
176            if (mSubtitleStyleRes != 0) {
177                mSubtitleView.setTextAppearance(mContext, mSubtitleStyleRes);
178            }
179        }
180
181        mTitleView.setText(mTitle);
182        mSubtitleView.setText(mSubtitle);
183
184        final boolean hasTitle = !TextUtils.isEmpty(mTitle);
185        final boolean hasSubtitle = !TextUtils.isEmpty(mSubtitle);
186        mSubtitleView.setVisibility(hasSubtitle ? VISIBLE : GONE);
187        mTitleLayout.setVisibility(hasTitle || hasSubtitle ? VISIBLE : GONE);
188        if (mTitleLayout.getParent() == null) {
189            addView(mTitleLayout);
190        }
191    }
192
193    public void initForMode(final ActionMode mode) {
194        if (mClose == null) {
195            LayoutInflater inflater = LayoutInflater.from(mContext);
196            mClose = inflater.inflate(R.layout.action_mode_close_item, this, false);
197            addView(mClose);
198        } else if (mClose.getParent() == null) {
199            addView(mClose);
200        }
201
202        View closeButton = mClose.findViewById(R.id.action_mode_close_button);
203        closeButton.setOnClickListener(new OnClickListener() {
204            public void onClick(View v) {
205                mode.finish();
206            }
207        });
208
209        final MenuBuilder menu = (MenuBuilder) mode.getMenu();
210        mActionMenuPresenter = new ActionMenuPresenter();
211        mActionMenuPresenter.setReserveOverflow(true);
212
213        final LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
214                LayoutParams.MATCH_PARENT);
215        if (!mSplitActionBar) {
216            menu.addMenuPresenter(mActionMenuPresenter);
217            mMenuView = (ActionMenuView) mActionMenuPresenter.getMenuView(this);
218            mMenuView.setBackgroundDrawable(null);
219            addView(mMenuView, layoutParams);
220        } else {
221            // Allow full screen width in split mode.
222            mActionMenuPresenter.setWidthLimit(
223                    getContext().getResources().getDisplayMetrics().widthPixels, true);
224            // No limit to the item count; use whatever will fit.
225            mActionMenuPresenter.setItemLimit(Integer.MAX_VALUE);
226            // Span the whole width
227            layoutParams.width = LayoutParams.MATCH_PARENT;
228            layoutParams.height = mContentHeight;
229            menu.addMenuPresenter(mActionMenuPresenter);
230            mMenuView = (ActionMenuView) mActionMenuPresenter.getMenuView(this);
231            mMenuView.setBackgroundDrawable(mSplitBackground);
232            mSplitView.addView(mMenuView, layoutParams);
233        }
234
235        mAnimateInOnLayout = true;
236    }
237
238    public void closeMode() {
239        if (mAnimationMode == ANIMATE_OUT) {
240            // Called again during close; just finish what we were doing.
241            return;
242        }
243        if (mClose == null) {
244            killMode();
245            return;
246        }
247
248        finishAnimation();
249        mAnimationMode = ANIMATE_OUT;
250        mCurrentAnimation = makeOutAnimation();
251        mCurrentAnimation.start();
252    }
253
254    private void finishAnimation() {
255        final Animator a = mCurrentAnimation;
256        if (a != null) {
257            mCurrentAnimation = null;
258            a.end();
259        }
260    }
261
262    public void killMode() {
263        finishAnimation();
264        removeAllViews();
265        if (mSplitView != null) {
266            mSplitView.removeView(mMenuView);
267        }
268        mCustomView = null;
269        mMenuView = null;
270        mAnimateInOnLayout = false;
271    }
272
273    @Override
274    public boolean showOverflowMenu() {
275        if (mActionMenuPresenter != null) {
276            return mActionMenuPresenter.showOverflowMenu();
277        }
278        return false;
279    }
280
281    @Override
282    public boolean hideOverflowMenu() {
283        if (mActionMenuPresenter != null) {
284            return mActionMenuPresenter.hideOverflowMenu();
285        }
286        return false;
287    }
288
289    @Override
290    public boolean isOverflowMenuShowing() {
291        if (mActionMenuPresenter != null) {
292            return mActionMenuPresenter.isOverflowMenuShowing();
293        }
294        return false;
295    }
296
297    @Override
298    protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
299        // Used by custom views if they don't supply layout params. Everything else
300        // added to an ActionBarContextView should have them already.
301        return new MarginLayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
302    }
303
304    @Override
305    public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
306        return new MarginLayoutParams(getContext(), attrs);
307    }
308
309    @Override
310    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
311        final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
312        if (widthMode != MeasureSpec.EXACTLY) {
313            throw new IllegalStateException(getClass().getSimpleName() + " can only be used " +
314                    "with android:layout_width=\"match_parent\" (or fill_parent)");
315        }
316
317        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
318        if (heightMode == MeasureSpec.UNSPECIFIED) {
319            throw new IllegalStateException(getClass().getSimpleName() + " can only be used " +
320                    "with android:layout_height=\"wrap_content\"");
321        }
322
323        final int contentWidth = MeasureSpec.getSize(widthMeasureSpec);
324
325        int maxHeight = mContentHeight > 0 ?
326                mContentHeight : MeasureSpec.getSize(heightMeasureSpec);
327
328        final int verticalPadding = getPaddingTop() + getPaddingBottom();
329        int availableWidth = contentWidth - getPaddingLeft() - getPaddingRight();
330        final int height = maxHeight - verticalPadding;
331        final int childSpecHeight = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST);
332
333        if (mClose != null) {
334            availableWidth = measureChildView(mClose, availableWidth, childSpecHeight, 0);
335            MarginLayoutParams lp = (MarginLayoutParams) mClose.getLayoutParams();
336            availableWidth -= lp.leftMargin + lp.rightMargin;
337        }
338
339        if (mMenuView != null && mMenuView.getParent() == this) {
340            availableWidth = measureChildView(mMenuView, availableWidth,
341                    childSpecHeight, 0);
342        }
343
344        if (mTitleLayout != null && mCustomView == null) {
345            availableWidth = measureChildView(mTitleLayout, availableWidth, childSpecHeight, 0);
346        }
347
348        if (mCustomView != null) {
349            ViewGroup.LayoutParams lp = mCustomView.getLayoutParams();
350            final int customWidthMode = lp.width != LayoutParams.WRAP_CONTENT ?
351                    MeasureSpec.EXACTLY : MeasureSpec.AT_MOST;
352            final int customWidth = lp.width >= 0 ?
353                    Math.min(lp.width, availableWidth) : availableWidth;
354            final int customHeightMode = lp.height != LayoutParams.WRAP_CONTENT ?
355                    MeasureSpec.EXACTLY : MeasureSpec.AT_MOST;
356            final int customHeight = lp.height >= 0 ?
357                    Math.min(lp.height, height) : height;
358            mCustomView.measure(MeasureSpec.makeMeasureSpec(customWidth, customWidthMode),
359                    MeasureSpec.makeMeasureSpec(customHeight, customHeightMode));
360        }
361
362        if (mContentHeight <= 0) {
363            int measuredHeight = 0;
364            final int count = getChildCount();
365            for (int i = 0; i < count; i++) {
366                View v = getChildAt(i);
367                int paddedViewHeight = v.getMeasuredHeight() + verticalPadding;
368                if (paddedViewHeight > measuredHeight) {
369                    measuredHeight = paddedViewHeight;
370                }
371            }
372            setMeasuredDimension(contentWidth, measuredHeight);
373        } else {
374            setMeasuredDimension(contentWidth, maxHeight);
375        }
376    }
377
378    private Animator makeInAnimation() {
379        mClose.setTranslationX(-mClose.getWidth() -
380                ((MarginLayoutParams) mClose.getLayoutParams()).leftMargin);
381        ObjectAnimator buttonAnimator = ObjectAnimator.ofFloat(mClose, "translationX", 0);
382        buttonAnimator.setDuration(200);
383        buttonAnimator.addListener(this);
384        buttonAnimator.setInterpolator(new DecelerateInterpolator());
385
386        AnimatorSet set = new AnimatorSet();
387        AnimatorSet.Builder b = set.play(buttonAnimator);
388
389        if (mMenuView != null) {
390            final int count = mMenuView.getChildCount();
391            if (count > 0) {
392                for (int i = count - 1, j = 0; i >= 0; i--, j++) {
393                    View child = mMenuView.getChildAt(i);
394                    child.setScaleY(0);
395                    ObjectAnimator a = ObjectAnimator.ofFloat(child, "scaleY", 0, 1);
396                    a.setDuration(100);
397                    a.setStartDelay(j * 70);
398                    b.with(a);
399                }
400            }
401        }
402
403        return set;
404    }
405
406    private Animator makeOutAnimation() {
407        ObjectAnimator buttonAnimator = ObjectAnimator.ofFloat(mClose, "translationX",
408                -mClose.getWidth() - ((MarginLayoutParams) mClose.getLayoutParams()).leftMargin);
409        buttonAnimator.setDuration(200);
410        buttonAnimator.addListener(this);
411        buttonAnimator.setInterpolator(new DecelerateInterpolator());
412
413        AnimatorSet set = new AnimatorSet();
414        AnimatorSet.Builder b = set.play(buttonAnimator);
415
416        if (mMenuView != null) {
417            final int count = mMenuView.getChildCount();
418            if (count > 0) {
419                for (int i = 0; i < 0; i++) {
420                    View child = mMenuView.getChildAt(i);
421                    child.setScaleY(0);
422                    ObjectAnimator a = ObjectAnimator.ofFloat(child, "scaleY", 0);
423                    a.setDuration(100);
424                    a.setStartDelay(i * 70);
425                    b.with(a);
426                }
427            }
428        }
429
430        return set;
431    }
432
433    @Override
434    protected void onLayout(boolean changed, int l, int t, int r, int b) {
435        int x = getPaddingLeft();
436        final int y = getPaddingTop();
437        final int contentHeight = b - t - getPaddingTop() - getPaddingBottom();
438
439        if (mClose != null && mClose.getVisibility() != GONE) {
440            MarginLayoutParams lp = (MarginLayoutParams) mClose.getLayoutParams();
441            x += lp.leftMargin;
442            x += positionChild(mClose, x, y, contentHeight);
443            x += lp.rightMargin;
444
445            if (mAnimateInOnLayout) {
446                mAnimationMode = ANIMATE_IN;
447                mCurrentAnimation = makeInAnimation();
448                mCurrentAnimation.start();
449                mAnimateInOnLayout = false;
450            }
451        }
452
453        if (mTitleLayout != null && mCustomView == null) {
454            x += positionChild(mTitleLayout, x, y, contentHeight);
455        }
456
457        if (mCustomView != null) {
458            x += positionChild(mCustomView, x, y, contentHeight);
459        }
460
461        x = r - l - getPaddingRight();
462
463        if (mMenuView != null) {
464            x -= positionChildInverse(mMenuView, x, y, contentHeight);
465        }
466    }
467
468    @Override
469    public void onAnimationStart(Animator animation) {
470    }
471
472    @Override
473    public void onAnimationEnd(Animator animation) {
474        if (mAnimationMode == ANIMATE_OUT) {
475            killMode();
476        }
477        mAnimationMode = ANIMATE_IDLE;
478    }
479
480    @Override
481    public void onAnimationCancel(Animator animation) {
482    }
483
484    @Override
485    public void onAnimationRepeat(Animator animation) {
486    }
487
488    @Override
489    public boolean shouldDelayChildPressedState() {
490        return false;
491    }
492
493    @Override
494    public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
495        if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
496            // Action mode started
497            event.setSource(this);
498            event.setClassName(getClass().getName());
499            event.setPackageName(getContext().getPackageName());
500            event.setContentDescription(mTitle);
501        } else {
502            super.onInitializeAccessibilityEvent(event);
503        }
504    }
505}
506