ActionBarContextView.java revision b51cc1dd63808da505cd8beb7300f57dc98ff1d3
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.TypedArray;
29import android.util.AttributeSet;
30import android.view.ActionMode;
31import android.view.LayoutInflater;
32import android.view.View;
33import android.view.ViewGroup.LayoutParams;
34import android.view.animation.DecelerateInterpolator;
35import android.widget.LinearLayout;
36import android.widget.TextView;
37
38/**
39 * @hide
40 */
41public class ActionBarContextView extends AbsActionBarView implements AnimatorListener {
42    private static final String TAG = "ActionBarContextView";
43
44    private int mContentHeight;
45
46    private CharSequence mTitle;
47    private CharSequence mSubtitle;
48
49    private View mClose;
50    private View mCustomView;
51    private LinearLayout mTitleLayout;
52    private TextView mTitleView;
53    private TextView mSubtitleView;
54    private int mTitleStyleRes;
55    private int mSubtitleStyleRes;
56
57    private Animator mCurrentAnimation;
58    private boolean mAnimateInOnLayout;
59    private int mAnimationMode;
60
61    private static final int ANIMATE_IDLE = 0;
62    private static final int ANIMATE_IN = 1;
63    private static final int ANIMATE_OUT = 2;
64
65    public ActionBarContextView(Context context) {
66        this(context, null);
67    }
68
69    public ActionBarContextView(Context context, AttributeSet attrs) {
70        this(context, attrs, com.android.internal.R.attr.actionModeStyle);
71    }
72
73    public ActionBarContextView(Context context, AttributeSet attrs, int defStyle) {
74        super(context, attrs, defStyle);
75
76        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ActionMode, defStyle, 0);
77        setBackgroundDrawable(a.getDrawable(
78                com.android.internal.R.styleable.ActionMode_background));
79        mTitleStyleRes = a.getResourceId(
80                com.android.internal.R.styleable.ActionMode_titleTextStyle, 0);
81        mSubtitleStyleRes = a.getResourceId(
82                com.android.internal.R.styleable.ActionMode_subtitleTextStyle, 0);
83
84        mContentHeight = a.getLayoutDimension(
85                com.android.internal.R.styleable.ActionMode_height, 0);
86        a.recycle();
87    }
88
89    public void setHeight(int height) {
90        mContentHeight = height;
91    }
92
93    public void setCustomView(View view) {
94        if (mCustomView != null) {
95            removeView(mCustomView);
96        }
97        mCustomView = view;
98        if (mTitleLayout != null) {
99            removeView(mTitleLayout);
100            mTitleLayout = null;
101        }
102        if (view != null) {
103            addView(view);
104        }
105        requestLayout();
106    }
107
108    public void setTitle(CharSequence title) {
109        mTitle = title;
110        initTitle();
111    }
112
113    public void setSubtitle(CharSequence subtitle) {
114        mSubtitle = subtitle;
115        initTitle();
116    }
117
118    public CharSequence getTitle() {
119        return mTitle;
120    }
121
122    public CharSequence getSubtitle() {
123        return mSubtitle;
124    }
125
126    private void initTitle() {
127        if (mTitleLayout == null) {
128            LayoutInflater inflater = LayoutInflater.from(getContext());
129            inflater.inflate(R.layout.action_bar_title_item, this);
130            mTitleLayout = (LinearLayout) getChildAt(getChildCount() - 1);
131            mTitleView = (TextView) mTitleLayout.findViewById(R.id.action_bar_title);
132            mSubtitleView = (TextView) mTitleLayout.findViewById(R.id.action_bar_subtitle);
133            if (mTitle != null) {
134                mTitleView.setText(mTitle);
135                if (mTitleStyleRes != 0) {
136                    mTitleView.setTextAppearance(mContext, mTitleStyleRes);
137                }
138            }
139            if (mSubtitle != null) {
140                mSubtitleView.setText(mSubtitle);
141                if (mSubtitleStyleRes != 0) {
142                    mSubtitleView.setTextAppearance(mContext, mSubtitleStyleRes);
143                }
144                mSubtitleView.setVisibility(VISIBLE);
145            }
146        } else {
147            mTitleView.setText(mTitle);
148            mSubtitleView.setText(mSubtitle);
149            mSubtitleView.setVisibility(mSubtitle != null ? VISIBLE : GONE);
150            if (mTitleLayout.getParent() == null) {
151                addView(mTitleLayout);
152            }
153        }
154    }
155
156    public void initForMode(final ActionMode mode) {
157        if (mClose == null) {
158            LayoutInflater inflater = LayoutInflater.from(mContext);
159            mClose = inflater.inflate(R.layout.action_mode_close_item, this, false);
160            addView(mClose);
161        } else if (mClose.getParent() == null) {
162            addView(mClose);
163        }
164
165        View closeButton = mClose.findViewById(R.id.action_mode_close_button);
166        closeButton.setOnClickListener(new OnClickListener() {
167            public void onClick(View v) {
168                mode.finish();
169            }
170        });
171
172        final MenuBuilder menu = (MenuBuilder) mode.getMenu();
173        mMenuPresenter = new ActionMenuPresenter();
174        menu.addMenuPresenter(mMenuPresenter);
175        mMenuView = (ActionMenuView) mMenuPresenter.getMenuView(this);
176
177        final LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
178                LayoutParams.MATCH_PARENT);
179        mMenuView.setLayoutParams(layoutParams);
180        if (mSplitView == null) {
181            addView(mMenuView);
182        } else {
183            // Allow full screen width in split mode.
184            mMenuPresenter.setWidthLimit(
185                    getContext().getResources().getDisplayMetrics().widthPixels, true);
186            // No limit to the item count; use whatever will fit.
187            mMenuPresenter.setItemLimit(Integer.MAX_VALUE);
188            // Span the whole width
189            layoutParams.width = LayoutParams.MATCH_PARENT;
190            mSplitView.addView(mMenuView);
191        }
192
193        mAnimateInOnLayout = true;
194    }
195
196    public void closeMode() {
197        if (mAnimationMode == ANIMATE_OUT) {
198            // Called again during close; just finish what we were doing.
199            return;
200        }
201        if (mClose == null) {
202            killMode();
203            return;
204        }
205
206        finishAnimation();
207        mAnimationMode = ANIMATE_OUT;
208        mCurrentAnimation = makeOutAnimation();
209        mCurrentAnimation.start();
210    }
211
212    private void finishAnimation() {
213        final Animator a = mCurrentAnimation;
214        if (a != null) {
215            mCurrentAnimation = null;
216            a.end();
217        }
218    }
219
220    public void killMode() {
221        finishAnimation();
222        removeAllViews();
223        if (mSplitView != null) {
224            mSplitView.removeView(mMenuView);
225        }
226        mCustomView = null;
227        mMenuView = null;
228        mAnimateInOnLayout = false;
229    }
230
231    public boolean showOverflowMenu() {
232        if (mMenuPresenter != null) {
233            return mMenuPresenter.showOverflowMenu();
234        }
235        return false;
236    }
237
238    public boolean hideOverflowMenu() {
239        if (mMenuPresenter != null) {
240            return mMenuPresenter.hideOverflowMenu();
241        }
242        return false;
243    }
244
245    public boolean isOverflowMenuShowing() {
246        if (mMenuPresenter != null) {
247            return mMenuPresenter.isOverflowMenuShowing();
248        }
249        return false;
250    }
251
252    @Override
253    protected LayoutParams generateDefaultLayoutParams() {
254        // Used by custom views if they don't supply layout params. Everything else
255        // added to an ActionBarContextView should have them already.
256        return new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
257    }
258
259    @Override
260    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
261        final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
262        if (widthMode != MeasureSpec.EXACTLY) {
263            throw new IllegalStateException(getClass().getSimpleName() + " can only be used " +
264                    "with android:layout_width=\"match_parent\" (or fill_parent)");
265        }
266
267        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
268        if (heightMode == MeasureSpec.UNSPECIFIED) {
269            throw new IllegalStateException(getClass().getSimpleName() + " can only be used " +
270                    "with android:layout_height=\"wrap_content\"");
271        }
272
273        final int contentWidth = MeasureSpec.getSize(widthMeasureSpec);
274
275        int maxHeight = mContentHeight > 0 ?
276                mContentHeight : MeasureSpec.getSize(heightMeasureSpec);
277
278        final int verticalPadding = getPaddingTop() + getPaddingBottom();
279        int availableWidth = contentWidth - getPaddingLeft() - getPaddingRight();
280        final int height = maxHeight - verticalPadding;
281        final int childSpecHeight = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST);
282
283        if (mClose != null) {
284            availableWidth = measureChildView(mClose, availableWidth, childSpecHeight, 0);
285        }
286
287        if (mMenuView != null) {
288            availableWidth = measureChildView(mMenuView, availableWidth,
289                    childSpecHeight, 0);
290        }
291
292        if (mTitleLayout != null && mCustomView == null) {
293            availableWidth = measureChildView(mTitleLayout, availableWidth, childSpecHeight, 0);
294        }
295
296        if (mCustomView != null) {
297            LayoutParams lp = mCustomView.getLayoutParams();
298            final int customWidthMode = lp.width != LayoutParams.WRAP_CONTENT ?
299                    MeasureSpec.EXACTLY : MeasureSpec.AT_MOST;
300            final int customWidth = lp.width >= 0 ?
301                    Math.min(lp.width, availableWidth) : availableWidth;
302            final int customHeightMode = lp.height != LayoutParams.WRAP_CONTENT ?
303                    MeasureSpec.EXACTLY : MeasureSpec.AT_MOST;
304            final int customHeight = lp.height >= 0 ?
305                    Math.min(lp.height, height) : height;
306            mCustomView.measure(MeasureSpec.makeMeasureSpec(customWidth, customWidthMode),
307                    MeasureSpec.makeMeasureSpec(customHeight, customHeightMode));
308        }
309
310        if (mContentHeight <= 0) {
311            int measuredHeight = 0;
312            final int count = getChildCount();
313            for (int i = 0; i < count; i++) {
314                View v = getChildAt(i);
315                int paddedViewHeight = v.getMeasuredHeight() + verticalPadding;
316                if (paddedViewHeight > measuredHeight) {
317                    measuredHeight = paddedViewHeight;
318                }
319            }
320            setMeasuredDimension(contentWidth, measuredHeight);
321        } else {
322            setMeasuredDimension(contentWidth, maxHeight);
323        }
324    }
325
326    private Animator makeInAnimation() {
327        mClose.setTranslationX(-mClose.getWidth());
328        ObjectAnimator buttonAnimator = ObjectAnimator.ofFloat(mClose, "translationX", 0);
329        buttonAnimator.setDuration(200);
330        buttonAnimator.addListener(this);
331        buttonAnimator.setInterpolator(new DecelerateInterpolator());
332
333        AnimatorSet set = new AnimatorSet();
334        AnimatorSet.Builder b = set.play(buttonAnimator);
335
336        if (mMenuView != null) {
337            final int count = mMenuView.getChildCount();
338            if (count > 0) {
339                for (int i = count - 1, j = 0; i >= 0; i--, j++) {
340                    View child = mMenuView.getChildAt(i);
341                    child.setScaleY(0);
342                    ObjectAnimator a = ObjectAnimator.ofFloat(child, "scaleY", 0, 1);
343                    a.setDuration(100);
344                    a.setStartDelay(j * 70);
345                    b.with(a);
346                }
347            }
348        }
349
350        return set;
351    }
352
353    private Animator makeOutAnimation() {
354        ObjectAnimator buttonAnimator = ObjectAnimator.ofFloat(mClose, "translationX",
355                -mClose.getWidth());
356        buttonAnimator.setDuration(200);
357        buttonAnimator.addListener(this);
358        buttonAnimator.setInterpolator(new DecelerateInterpolator());
359
360        AnimatorSet set = new AnimatorSet();
361        AnimatorSet.Builder b = set.play(buttonAnimator);
362
363        if (mMenuView != null) {
364            final int count = mMenuView.getChildCount();
365            if (count > 0) {
366                for (int i = 0; i < 0; i++) {
367                    View child = mMenuView.getChildAt(i);
368                    child.setScaleY(0);
369                    ObjectAnimator a = ObjectAnimator.ofFloat(child, "scaleY", 0);
370                    a.setDuration(100);
371                    a.setStartDelay(i * 70);
372                    b.with(a);
373                }
374            }
375        }
376
377        return set;
378    }
379
380    @Override
381    protected void onLayout(boolean changed, int l, int t, int r, int b) {
382        int x = getPaddingLeft();
383        final int y = getPaddingTop();
384        final int contentHeight = b - t - getPaddingTop() - getPaddingBottom();
385
386        if (mClose != null && mClose.getVisibility() != GONE) {
387            x += positionChild(mClose, x, y, contentHeight);
388
389            if (mAnimateInOnLayout) {
390                mAnimationMode = ANIMATE_IN;
391                mCurrentAnimation = makeInAnimation();
392                mCurrentAnimation.start();
393                mAnimateInOnLayout = false;
394            }
395        }
396
397        if (mTitleLayout != null && mCustomView == null) {
398            x += positionChild(mTitleLayout, x, y, contentHeight);
399        }
400
401        if (mCustomView != null) {
402            x += positionChild(mCustomView, x, y, contentHeight);
403        }
404
405        x = r - l - getPaddingRight();
406
407        if (mMenuView != null) {
408            x -= positionChildInverse(mMenuView, x, y, contentHeight);
409        }
410    }
411
412    @Override
413    public void onAnimationStart(Animator animation) {
414    }
415
416    @Override
417    public void onAnimationEnd(Animator animation) {
418        if (mAnimationMode == ANIMATE_OUT) {
419            killMode();
420        }
421        mAnimationMode = ANIMATE_IDLE;
422    }
423
424    @Override
425    public void onAnimationCancel(Animator animation) {
426    }
427
428    @Override
429    public void onAnimationRepeat(Animator animation) {
430    }
431
432    @Override
433    public boolean shouldDelayChildPressedState() {
434        return false;
435    }
436}
437