ActionBarContainer.java revision d21aa12e8eb9d46ee92bf408b4b48386c6bf062d
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; 8125151e4792e9be8dbb892b7eba7349773b8127afAdam Powell if (tabView != null) { 8225151e4792e9be8dbb892b7eba7349773b8127afAdam Powell addView(tabView); 8325151e4792e9be8dbb892b7eba7349773b8127afAdam Powell } 84dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell } 85dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell 86dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell public View getTabContainer() { 87dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell return mTabContainer; 88dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell } 89dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell 90dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell @Override 91640a66eac612b850b5dabd3b93bd94f83ed2d567Adam Powell public ActionMode startActionModeForChild(View child, ActionMode.Callback callback) { 92640a66eac612b850b5dabd3b93bd94f83ed2d567Adam Powell // No starting an action mode for an action bar child! (Where would it go?) 93640a66eac612b850b5dabd3b93bd94f83ed2d567Adam Powell return null; 94640a66eac612b850b5dabd3b93bd94f83ed2d567Adam Powell } 95640a66eac612b850b5dabd3b93bd94f83ed2d567Adam Powell 96640a66eac612b850b5dabd3b93bd94f83ed2d567Adam Powell @Override 97dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 98dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell super.onMeasure(widthMeasureSpec, heightMeasureSpec); 99d21aa12e8eb9d46ee92bf408b4b48386c6bf062dAdam Powell 100d21aa12e8eb9d46ee92bf408b4b48386c6bf062dAdam Powell int nonTabHeight = 0; 101d21aa12e8eb9d46ee92bf408b4b48386c6bf062dAdam Powell final int count = getChildCount(); 102d21aa12e8eb9d46ee92bf408b4b48386c6bf062dAdam Powell for (int i = 0; i < count; i++) { 103d21aa12e8eb9d46ee92bf408b4b48386c6bf062dAdam Powell final View child = getChildAt(i); 104d21aa12e8eb9d46ee92bf408b4b48386c6bf062dAdam Powell if (child == mTabContainer) { 105d21aa12e8eb9d46ee92bf408b4b48386c6bf062dAdam Powell continue; 106d21aa12e8eb9d46ee92bf408b4b48386c6bf062dAdam Powell } 107d21aa12e8eb9d46ee92bf408b4b48386c6bf062dAdam Powell 108d21aa12e8eb9d46ee92bf408b4b48386c6bf062dAdam Powell final LayoutParams lp = (LayoutParams) child.getLayoutParams(); 109d21aa12e8eb9d46ee92bf408b4b48386c6bf062dAdam Powell nonTabHeight = Math.max(nonTabHeight, 110d21aa12e8eb9d46ee92bf408b4b48386c6bf062dAdam Powell child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin); 111d21aa12e8eb9d46ee92bf408b4b48386c6bf062dAdam Powell } 112d21aa12e8eb9d46ee92bf408b4b48386c6bf062dAdam Powell 113dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell if (mTabContainer != null && mTabContainer.getVisibility() != GONE) { 114dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell final int mode = MeasureSpec.getMode(heightMeasureSpec); 115dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell if (mode == MeasureSpec.AT_MOST) { 116dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell final int maxHeight = MeasureSpec.getSize(heightMeasureSpec); 117dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell setMeasuredDimension(getMeasuredWidth(), 118d21aa12e8eb9d46ee92bf408b4b48386c6bf062dAdam Powell Math.min(nonTabHeight + mTabContainer.getMeasuredHeight(), maxHeight)); 119dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell } 120dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell } 121dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell } 122dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell 123dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell @Override 124dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell public void onLayout(boolean changed, int l, int t, int r, int b) { 125dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell super.onLayout(changed, l, t, r, b); 126dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell if (mTabContainer != null && mTabContainer.getVisibility() != GONE) { 127dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell final int containerHeight = getMeasuredHeight(); 128dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell mTabContainer.layout(l, containerHeight - mTabContainer.getMeasuredHeight(), 129dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell r, containerHeight); 130dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell } 131dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell } 13245f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell} 133