ActionBarContainer.java revision 640a66eac612b850b5dabd3b93bd94f83ed2d567
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 com.android.internal.widget;
18
19import android.content.Context;
20import android.content.res.TypedArray;
21import android.util.AttributeSet;
22import android.view.ActionMode;
23import android.view.MotionEvent;
24import android.view.View;
25import android.widget.FrameLayout;
26
27/**
28 * This class acts as a container for the action bar view and action mode context views.
29 * It applies special styles as needed to help handle animated transitions between them.
30 * @hide
31 */
32public class ActionBarContainer extends FrameLayout {
33    private boolean mIsTransitioning;
34    private View mTabContainer;
35
36    public ActionBarContainer(Context context) {
37        this(context, null);
38    }
39
40    public ActionBarContainer(Context context, AttributeSet attrs) {
41        super(context, attrs);
42
43        TypedArray a = context.obtainStyledAttributes(attrs,
44                com.android.internal.R.styleable.ActionBar);
45        setBackgroundDrawable(a.getDrawable(com.android.internal.R.styleable.ActionBar_background));
46        a.recycle();
47    }
48
49    /**
50     * Set the action bar into a "transitioning" state. While transitioning
51     * the bar will block focus and touch from all of its descendants. This
52     * prevents the user from interacting with the bar while it is animating
53     * in or out.
54     *
55     * @param isTransitioning true if the bar is currently transitioning, false otherwise.
56     */
57    public void setTransitioning(boolean isTransitioning) {
58        mIsTransitioning = isTransitioning;
59        setDescendantFocusability(isTransitioning ? FOCUS_BLOCK_DESCENDANTS
60                : FOCUS_AFTER_DESCENDANTS);
61    }
62
63    @Override
64    public boolean onInterceptTouchEvent(MotionEvent ev) {
65        return mIsTransitioning || super.onInterceptTouchEvent(ev);
66    }
67
68    @Override
69    public boolean onTouchEvent(MotionEvent ev) {
70        super.onTouchEvent(ev);
71
72        // An action bar always eats touch events.
73        return true;
74    }
75
76    public void setTabContainer(View tabView) {
77        if (mTabContainer != null) {
78            removeView(mTabContainer);
79        }
80        mTabContainer = tabView;
81        addView(tabView);
82    }
83
84    public View getTabContainer() {
85        return mTabContainer;
86    }
87
88    @Override
89    public ActionMode startActionModeForChild(View child, ActionMode.Callback callback) {
90        // No starting an action mode for an action bar child! (Where would it go?)
91        return null;
92    }
93
94    @Override
95    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
96        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
97        if (mTabContainer != null && mTabContainer.getVisibility() != GONE) {
98            final int mode = MeasureSpec.getMode(heightMeasureSpec);
99            if (mode == MeasureSpec.AT_MOST) {
100                final int measuredHeight = getMeasuredHeight();
101                final int maxHeight = MeasureSpec.getSize(heightMeasureSpec);
102                setMeasuredDimension(getMeasuredWidth(),
103                        Math.min(measuredHeight + mTabContainer.getMeasuredHeight(), maxHeight));
104            }
105        }
106    }
107
108    @Override
109    public void onLayout(boolean changed, int l, int t, int r, int b) {
110        super.onLayout(changed, l, t, r, b);
111        if (mTabContainer != null && mTabContainer.getVisibility() != GONE) {
112            final int containerHeight = getMeasuredHeight();
113            mTabContainer.layout(l, containerHeight - mTabContainer.getMeasuredHeight(),
114                    r, containerHeight);
115        }
116    }
117}
118