ActionBarContainer.java revision dae7824c4b78d02159c6ad0896f246942d7b3d8c
145f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell/*
245f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell * Copyright (C) 2010 The Android Open Source Project
345f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell *
445f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell * Licensed under the Apache License, Version 2.0 (the "License");
545f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell * you may not use this file except in compliance with the License.
645f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell * You may obtain a copy of the License at
745f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell *
845f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell *      http://www.apache.org/licenses/LICENSE-2.0
945f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell *
1045f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell * Unless required by applicable law or agreed to in writing, software
1145f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell * distributed under the License is distributed on an "AS IS" BASIS,
1245f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1345f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell * See the License for the specific language governing permissions and
1445f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell * limitations under the License.
1545f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell */
1645f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell
1745f1e08c348ccb129bcc25e438c05421f7123f41Adam Powellpackage com.android.internal.widget;
1845f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell
1945f1e08c348ccb129bcc25e438c05421f7123f41Adam Powellimport android.content.Context;
2045f1e08c348ccb129bcc25e438c05421f7123f41Adam Powellimport android.content.res.TypedArray;
2145f1e08c348ccb129bcc25e438c05421f7123f41Adam Powellimport android.util.AttributeSet;
226ecf3d1690789ace1a667093ad6bbdd6cd35bda7Adam Powellimport android.view.MotionEvent;
23dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powellimport android.view.View;
2445f1e08c348ccb129bcc25e438c05421f7123f41Adam Powellimport android.widget.FrameLayout;
2545f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell
2645f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell/**
2745f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell * This class acts as a container for the action bar view and action mode context views.
2845f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell * It applies special styles as needed to help handle animated transitions between them.
2945f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell * @hide
3045f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell */
3145f1e08c348ccb129bcc25e438c05421f7123f41Adam Powellpublic class ActionBarContainer extends FrameLayout {
3201feaee3d9767ef1185783877e92244f14d7d4baAdam Powell    private boolean mIsTransitioning;
33dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell    private View mTabContainer;
3401feaee3d9767ef1185783877e92244f14d7d4baAdam Powell
3545f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell    public ActionBarContainer(Context context) {
3645f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell        this(context, null);
3745f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell    }
3845f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell
3945f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell    public ActionBarContainer(Context context, AttributeSet attrs) {
4045f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell        super(context, attrs);
4145f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell
4245f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell        TypedArray a = context.obtainStyledAttributes(attrs,
4345f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell                com.android.internal.R.styleable.ActionBar);
4445f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell        setBackgroundDrawable(a.getDrawable(com.android.internal.R.styleable.ActionBar_background));
4545f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell        a.recycle();
4645f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell    }
476ecf3d1690789ace1a667093ad6bbdd6cd35bda7Adam Powell
4801feaee3d9767ef1185783877e92244f14d7d4baAdam Powell    /**
4901feaee3d9767ef1185783877e92244f14d7d4baAdam Powell     * Set the action bar into a "transitioning" state. While transitioning
5001feaee3d9767ef1185783877e92244f14d7d4baAdam Powell     * the bar will block focus and touch from all of its descendants. This
5101feaee3d9767ef1185783877e92244f14d7d4baAdam Powell     * prevents the user from interacting with the bar while it is animating
5201feaee3d9767ef1185783877e92244f14d7d4baAdam Powell     * in or out.
5301feaee3d9767ef1185783877e92244f14d7d4baAdam Powell     *
5401feaee3d9767ef1185783877e92244f14d7d4baAdam Powell     * @param isTransitioning true if the bar is currently transitioning, false otherwise.
5501feaee3d9767ef1185783877e92244f14d7d4baAdam Powell     */
5601feaee3d9767ef1185783877e92244f14d7d4baAdam Powell    public void setTransitioning(boolean isTransitioning) {
5701feaee3d9767ef1185783877e92244f14d7d4baAdam Powell        mIsTransitioning = isTransitioning;
5801feaee3d9767ef1185783877e92244f14d7d4baAdam Powell        setDescendantFocusability(isTransitioning ? FOCUS_BLOCK_DESCENDANTS
5901feaee3d9767ef1185783877e92244f14d7d4baAdam Powell                : FOCUS_AFTER_DESCENDANTS);
6001feaee3d9767ef1185783877e92244f14d7d4baAdam Powell    }
6101feaee3d9767ef1185783877e92244f14d7d4baAdam Powell
6201feaee3d9767ef1185783877e92244f14d7d4baAdam Powell    @Override
6301feaee3d9767ef1185783877e92244f14d7d4baAdam Powell    public boolean onInterceptTouchEvent(MotionEvent ev) {
6401feaee3d9767ef1185783877e92244f14d7d4baAdam Powell        return mIsTransitioning || super.onInterceptTouchEvent(ev);
6501feaee3d9767ef1185783877e92244f14d7d4baAdam Powell    }
6601feaee3d9767ef1185783877e92244f14d7d4baAdam Powell
676ecf3d1690789ace1a667093ad6bbdd6cd35bda7Adam Powell    @Override
686ecf3d1690789ace1a667093ad6bbdd6cd35bda7Adam Powell    public boolean onTouchEvent(MotionEvent ev) {
696ecf3d1690789ace1a667093ad6bbdd6cd35bda7Adam Powell        super.onTouchEvent(ev);
70dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell
71dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell        // An action bar always eats touch events.
726ecf3d1690789ace1a667093ad6bbdd6cd35bda7Adam Powell        return true;
736ecf3d1690789ace1a667093ad6bbdd6cd35bda7Adam Powell    }
74dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell
75dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell    public void setTabContainer(View tabView) {
76dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell        if (mTabContainer != null) {
77dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell            removeView(mTabContainer);
78dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell        }
79dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell        mTabContainer = tabView;
80dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell        addView(tabView);
81dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell    }
82dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell
83dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell    public View getTabContainer() {
84dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell        return mTabContainer;
85dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell    }
86dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell
87dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell    @Override
88dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
89dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
90dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell        if (mTabContainer != null && mTabContainer.getVisibility() != GONE) {
91dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell            final int mode = MeasureSpec.getMode(heightMeasureSpec);
92dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell            if (mode == MeasureSpec.AT_MOST) {
93dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell                final int measuredHeight = getMeasuredHeight();
94dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell                final int maxHeight = MeasureSpec.getSize(heightMeasureSpec);
95dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell                setMeasuredDimension(getMeasuredWidth(),
96dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell                        Math.min(measuredHeight + mTabContainer.getMeasuredHeight(), maxHeight));
97dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell            }
98dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell        }
99dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell    }
100dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell
101dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell    @Override
102dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell    public void onLayout(boolean changed, int l, int t, int r, int b) {
103dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell        super.onLayout(changed, l, t, r, b);
104dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell        if (mTabContainer != null && mTabContainer.getVisibility() != GONE) {
105dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell            final int containerHeight = getMeasuredHeight();
106dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell            mTabContainer.layout(l, containerHeight - mTabContainer.getMeasuredHeight(),
107dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell                    r, containerHeight);
108dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell        }
109dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell    }
11045f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell}
111