ActionBarContainer.java revision 640a66eac612b850b5dabd3b93bd94f83ed2d567
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;
22640a66eac612b850b5dabd3b93bd94f83ed2d567Adam Powellimport android.view.ActionMode;
236ecf3d1690789ace1a667093ad6bbdd6cd35bda7Adam Powellimport android.view.MotionEvent;
24dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powellimport android.view.View;
2545f1e08c348ccb129bcc25e438c05421f7123f41Adam Powellimport android.widget.FrameLayout;
2645f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell
2745f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell/**
2845f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell * This class acts as a container for the action bar view and action mode context views.
2945f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell * It applies special styles as needed to help handle animated transitions between them.
3045f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell * @hide
3145f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell */
3245f1e08c348ccb129bcc25e438c05421f7123f41Adam Powellpublic class ActionBarContainer extends FrameLayout {
3301feaee3d9767ef1185783877e92244f14d7d4baAdam Powell    private boolean mIsTransitioning;
34dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell    private View mTabContainer;
3501feaee3d9767ef1185783877e92244f14d7d4baAdam Powell
3645f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell    public ActionBarContainer(Context context) {
3745f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell        this(context, null);
3845f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell    }
3945f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell
4045f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell    public ActionBarContainer(Context context, AttributeSet attrs) {
4145f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell        super(context, attrs);
4245f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell
4345f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell        TypedArray a = context.obtainStyledAttributes(attrs,
4445f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell                com.android.internal.R.styleable.ActionBar);
4545f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell        setBackgroundDrawable(a.getDrawable(com.android.internal.R.styleable.ActionBar_background));
4645f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell        a.recycle();
4745f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell    }
486ecf3d1690789ace1a667093ad6bbdd6cd35bda7Adam Powell
4901feaee3d9767ef1185783877e92244f14d7d4baAdam Powell    /**
5001feaee3d9767ef1185783877e92244f14d7d4baAdam Powell     * Set the action bar into a "transitioning" state. While transitioning
5101feaee3d9767ef1185783877e92244f14d7d4baAdam Powell     * the bar will block focus and touch from all of its descendants. This
5201feaee3d9767ef1185783877e92244f14d7d4baAdam Powell     * prevents the user from interacting with the bar while it is animating
5301feaee3d9767ef1185783877e92244f14d7d4baAdam Powell     * in or out.
5401feaee3d9767ef1185783877e92244f14d7d4baAdam Powell     *
5501feaee3d9767ef1185783877e92244f14d7d4baAdam Powell     * @param isTransitioning true if the bar is currently transitioning, false otherwise.
5601feaee3d9767ef1185783877e92244f14d7d4baAdam Powell     */
5701feaee3d9767ef1185783877e92244f14d7d4baAdam Powell    public void setTransitioning(boolean isTransitioning) {
5801feaee3d9767ef1185783877e92244f14d7d4baAdam Powell        mIsTransitioning = isTransitioning;
5901feaee3d9767ef1185783877e92244f14d7d4baAdam Powell        setDescendantFocusability(isTransitioning ? FOCUS_BLOCK_DESCENDANTS
6001feaee3d9767ef1185783877e92244f14d7d4baAdam Powell                : FOCUS_AFTER_DESCENDANTS);
6101feaee3d9767ef1185783877e92244f14d7d4baAdam Powell    }
6201feaee3d9767ef1185783877e92244f14d7d4baAdam Powell
6301feaee3d9767ef1185783877e92244f14d7d4baAdam Powell    @Override
6401feaee3d9767ef1185783877e92244f14d7d4baAdam Powell    public boolean onInterceptTouchEvent(MotionEvent ev) {
6501feaee3d9767ef1185783877e92244f14d7d4baAdam Powell        return mIsTransitioning || super.onInterceptTouchEvent(ev);
6601feaee3d9767ef1185783877e92244f14d7d4baAdam Powell    }
6701feaee3d9767ef1185783877e92244f14d7d4baAdam Powell
686ecf3d1690789ace1a667093ad6bbdd6cd35bda7Adam Powell    @Override
696ecf3d1690789ace1a667093ad6bbdd6cd35bda7Adam Powell    public boolean onTouchEvent(MotionEvent ev) {
706ecf3d1690789ace1a667093ad6bbdd6cd35bda7Adam Powell        super.onTouchEvent(ev);
71dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell
72dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell        // An action bar always eats touch events.
736ecf3d1690789ace1a667093ad6bbdd6cd35bda7Adam Powell        return true;
746ecf3d1690789ace1a667093ad6bbdd6cd35bda7Adam Powell    }
75dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell
76dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell    public void setTabContainer(View tabView) {
77dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell        if (mTabContainer != null) {
78dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell            removeView(mTabContainer);
79dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell        }
80dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell        mTabContainer = tabView;
81dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell        addView(tabView);
82dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell    }
83dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell
84dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell    public View getTabContainer() {
85dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell        return mTabContainer;
86dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell    }
87dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell
88dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell    @Override
89640a66eac612b850b5dabd3b93bd94f83ed2d567Adam Powell    public ActionMode startActionModeForChild(View child, ActionMode.Callback callback) {
90640a66eac612b850b5dabd3b93bd94f83ed2d567Adam Powell        // No starting an action mode for an action bar child! (Where would it go?)
91640a66eac612b850b5dabd3b93bd94f83ed2d567Adam Powell        return null;
92640a66eac612b850b5dabd3b93bd94f83ed2d567Adam Powell    }
93640a66eac612b850b5dabd3b93bd94f83ed2d567Adam Powell
94640a66eac612b850b5dabd3b93bd94f83ed2d567Adam Powell    @Override
95dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
96dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
97dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell        if (mTabContainer != null && mTabContainer.getVisibility() != GONE) {
98dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell            final int mode = MeasureSpec.getMode(heightMeasureSpec);
99dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell            if (mode == MeasureSpec.AT_MOST) {
100dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell                final int measuredHeight = getMeasuredHeight();
101dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell                final int maxHeight = MeasureSpec.getSize(heightMeasureSpec);
102dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell                setMeasuredDimension(getMeasuredWidth(),
103dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell                        Math.min(measuredHeight + mTabContainer.getMeasuredHeight(), maxHeight));
104dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell            }
105dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell        }
106dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell    }
107dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell
108dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell    @Override
109dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell    public void onLayout(boolean changed, int l, int t, int r, int b) {
110dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell        super.onLayout(changed, l, t, r, b);
111dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell        if (mTabContainer != null && mTabContainer.getVisibility() != GONE) {
112dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell            final int containerHeight = getMeasuredHeight();
113dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell            mTabContainer.layout(l, containerHeight - mTabContainer.getMeasuredHeight(),
114dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell                    r, containerHeight);
115dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell        }
116dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell    }
11745f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell}
118