ActionBarContainer.java revision af6b97ebe0e6a67d1691c4d7789c7bc312c1e13e
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 19310849abb1a0763ea9912b59187f0a4271efaa2eAdam Powellimport android.app.ActionBar; 2045f1e08c348ccb129bcc25e438c05421f7123f41Adam Powellimport android.content.Context; 2145f1e08c348ccb129bcc25e438c05421f7123f41Adam Powellimport android.content.res.TypedArray; 22a72ef62e5c329a19eab9935acba816fc1369c637Adam Powellimport android.graphics.Canvas; 23a72ef62e5c329a19eab9935acba816fc1369c637Adam Powellimport android.graphics.drawable.Drawable; 2445f1e08c348ccb129bcc25e438c05421f7123f41Adam Powellimport android.util.AttributeSet; 25640a66eac612b850b5dabd3b93bd94f83ed2d567Adam Powellimport android.view.ActionMode; 266ecf3d1690789ace1a667093ad6bbdd6cd35bda7Adam Powellimport android.view.MotionEvent; 27dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powellimport android.view.View; 28af6b97ebe0e6a67d1691c4d7789c7bc312c1e13eAdam Powellimport android.view.ViewGroup; 2945f1e08c348ccb129bcc25e438c05421f7123f41Adam Powellimport android.widget.FrameLayout; 3045f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell 3145f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell/** 3245f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell * This class acts as a container for the action bar view and action mode context views. 3345f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell * It applies special styles as needed to help handle animated transitions between them. 3445f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell * @hide 3545f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell */ 3645f1e08c348ccb129bcc25e438c05421f7123f41Adam Powellpublic class ActionBarContainer extends FrameLayout { 3701feaee3d9767ef1185783877e92244f14d7d4baAdam Powell private boolean mIsTransitioning; 38dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell private View mTabContainer; 39310849abb1a0763ea9912b59187f0a4271efaa2eAdam Powell private ActionBarView mActionBarView; 4001feaee3d9767ef1185783877e92244f14d7d4baAdam Powell 41a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell private Drawable mBackground; 42a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell private Drawable mStackedBackground; 43a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell private Drawable mSplitBackground; 44a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell private boolean mIsSplit; 45a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell private boolean mIsStacked; 46a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell 4745f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell public ActionBarContainer(Context context) { 4845f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell this(context, null); 4945f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell } 5045f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell 5145f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell public ActionBarContainer(Context context, AttributeSet attrs) { 5245f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell super(context, attrs); 5345f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell 54a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell setBackgroundDrawable(null); 55a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell 5645f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell TypedArray a = context.obtainStyledAttributes(attrs, 5745f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell com.android.internal.R.styleable.ActionBar); 58a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell mBackground = a.getDrawable(com.android.internal.R.styleable.ActionBar_background); 59a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell mStackedBackground = a.getDrawable( 60a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell com.android.internal.R.styleable.ActionBar_backgroundStacked); 61a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell 62a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell if (getId() == com.android.internal.R.id.split_action_bar) { 63a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell mIsSplit = true; 64a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell mSplitBackground = a.getDrawable( 65a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell com.android.internal.R.styleable.ActionBar_backgroundSplit); 66a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell } 6745f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell a.recycle(); 68a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell 69a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell setWillNotDraw(mIsSplit ? mSplitBackground == null : 70a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell mBackground == null && mStackedBackground == null); 7145f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell } 726ecf3d1690789ace1a667093ad6bbdd6cd35bda7Adam Powell 73310849abb1a0763ea9912b59187f0a4271efaa2eAdam Powell @Override 74310849abb1a0763ea9912b59187f0a4271efaa2eAdam Powell public void onFinishInflate() { 75310849abb1a0763ea9912b59187f0a4271efaa2eAdam Powell super.onFinishInflate(); 76310849abb1a0763ea9912b59187f0a4271efaa2eAdam Powell mActionBarView = (ActionBarView) findViewById(com.android.internal.R.id.action_bar); 77310849abb1a0763ea9912b59187f0a4271efaa2eAdam Powell } 78310849abb1a0763ea9912b59187f0a4271efaa2eAdam Powell 7901feaee3d9767ef1185783877e92244f14d7d4baAdam Powell /** 8001feaee3d9767ef1185783877e92244f14d7d4baAdam Powell * Set the action bar into a "transitioning" state. While transitioning 8101feaee3d9767ef1185783877e92244f14d7d4baAdam Powell * the bar will block focus and touch from all of its descendants. This 8201feaee3d9767ef1185783877e92244f14d7d4baAdam Powell * prevents the user from interacting with the bar while it is animating 8301feaee3d9767ef1185783877e92244f14d7d4baAdam Powell * in or out. 8401feaee3d9767ef1185783877e92244f14d7d4baAdam Powell * 8501feaee3d9767ef1185783877e92244f14d7d4baAdam Powell * @param isTransitioning true if the bar is currently transitioning, false otherwise. 8601feaee3d9767ef1185783877e92244f14d7d4baAdam Powell */ 8701feaee3d9767ef1185783877e92244f14d7d4baAdam Powell public void setTransitioning(boolean isTransitioning) { 8801feaee3d9767ef1185783877e92244f14d7d4baAdam Powell mIsTransitioning = isTransitioning; 8901feaee3d9767ef1185783877e92244f14d7d4baAdam Powell setDescendantFocusability(isTransitioning ? FOCUS_BLOCK_DESCENDANTS 9001feaee3d9767ef1185783877e92244f14d7d4baAdam Powell : FOCUS_AFTER_DESCENDANTS); 9101feaee3d9767ef1185783877e92244f14d7d4baAdam Powell } 9201feaee3d9767ef1185783877e92244f14d7d4baAdam Powell 9301feaee3d9767ef1185783877e92244f14d7d4baAdam Powell @Override 9401feaee3d9767ef1185783877e92244f14d7d4baAdam Powell public boolean onInterceptTouchEvent(MotionEvent ev) { 9501feaee3d9767ef1185783877e92244f14d7d4baAdam Powell return mIsTransitioning || super.onInterceptTouchEvent(ev); 9601feaee3d9767ef1185783877e92244f14d7d4baAdam Powell } 9701feaee3d9767ef1185783877e92244f14d7d4baAdam Powell 986ecf3d1690789ace1a667093ad6bbdd6cd35bda7Adam Powell @Override 996ecf3d1690789ace1a667093ad6bbdd6cd35bda7Adam Powell public boolean onTouchEvent(MotionEvent ev) { 1006ecf3d1690789ace1a667093ad6bbdd6cd35bda7Adam Powell super.onTouchEvent(ev); 101dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell 102dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell // An action bar always eats touch events. 1036ecf3d1690789ace1a667093ad6bbdd6cd35bda7Adam Powell return true; 1046ecf3d1690789ace1a667093ad6bbdd6cd35bda7Adam Powell } 105dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell 106f5645cbafe7eed33452d888f16726bee8a0cd9feAdam Powell public void setTabContainer(ScrollingTabContainerView tabView) { 107dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell if (mTabContainer != null) { 108dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell removeView(mTabContainer); 109dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell } 110dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell mTabContainer = tabView; 11125151e4792e9be8dbb892b7eba7349773b8127afAdam Powell if (tabView != null) { 11225151e4792e9be8dbb892b7eba7349773b8127afAdam Powell addView(tabView); 113af6b97ebe0e6a67d1691c4d7789c7bc312c1e13eAdam Powell final ViewGroup.LayoutParams lp = tabView.getLayoutParams(); 114af6b97ebe0e6a67d1691c4d7789c7bc312c1e13eAdam Powell lp.width = LayoutParams.MATCH_PARENT; 115af6b97ebe0e6a67d1691c4d7789c7bc312c1e13eAdam Powell lp.height = LayoutParams.WRAP_CONTENT; 116f5645cbafe7eed33452d888f16726bee8a0cd9feAdam Powell tabView.setAllowCollapse(false); 11725151e4792e9be8dbb892b7eba7349773b8127afAdam Powell } 118dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell } 119dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell 120dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell public View getTabContainer() { 121dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell return mTabContainer; 122dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell } 123dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell 124dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell @Override 125a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell public void onDraw(Canvas canvas) { 126a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell if (getWidth() == 0 || getHeight() == 0) { 127a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell return; 128a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell } 129a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell 130a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell if (mIsSplit) { 131a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell if (mSplitBackground != null) mSplitBackground.draw(canvas); 132a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell } else { 133a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell if (mBackground != null) { 134a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell mBackground.draw(canvas); 135a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell } 136a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell if (mStackedBackground != null && mIsStacked) { 137a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell mStackedBackground.draw(canvas); 138a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell } 139a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell } 140a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell } 141a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell 142a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell @Override 143640a66eac612b850b5dabd3b93bd94f83ed2d567Adam Powell public ActionMode startActionModeForChild(View child, ActionMode.Callback callback) { 144640a66eac612b850b5dabd3b93bd94f83ed2d567Adam Powell // No starting an action mode for an action bar child! (Where would it go?) 145640a66eac612b850b5dabd3b93bd94f83ed2d567Adam Powell return null; 146640a66eac612b850b5dabd3b93bd94f83ed2d567Adam Powell } 147640a66eac612b850b5dabd3b93bd94f83ed2d567Adam Powell 148640a66eac612b850b5dabd3b93bd94f83ed2d567Adam Powell @Override 149dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 150dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell super.onMeasure(widthMeasureSpec, heightMeasureSpec); 151d21aa12e8eb9d46ee92bf408b4b48386c6bf062dAdam Powell 152f6ce6a9bacbb220c6ea7b552c481237f23e64ae7Adam Powell if (mActionBarView == null) return; 153310849abb1a0763ea9912b59187f0a4271efaa2eAdam Powell 154f6ce6a9bacbb220c6ea7b552c481237f23e64ae7Adam Powell final LayoutParams lp = (LayoutParams) mActionBarView.getLayoutParams(); 155f6ce6a9bacbb220c6ea7b552c481237f23e64ae7Adam Powell final int actionBarViewHeight = mActionBarView.isCollapsed() ? 0 : 156f6ce6a9bacbb220c6ea7b552c481237f23e64ae7Adam Powell mActionBarView.getMeasuredHeight() + lp.topMargin + lp.bottomMargin; 157d21aa12e8eb9d46ee92bf408b4b48386c6bf062dAdam Powell 158dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell if (mTabContainer != null && mTabContainer.getVisibility() != GONE) { 159dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell final int mode = MeasureSpec.getMode(heightMeasureSpec); 160dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell if (mode == MeasureSpec.AT_MOST) { 161dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell final int maxHeight = MeasureSpec.getSize(heightMeasureSpec); 162dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell setMeasuredDimension(getMeasuredWidth(), 163f6ce6a9bacbb220c6ea7b552c481237f23e64ae7Adam Powell Math.min(actionBarViewHeight + mTabContainer.getMeasuredHeight(), 164f6ce6a9bacbb220c6ea7b552c481237f23e64ae7Adam Powell maxHeight)); 165dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell } 166dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell } 167dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell } 168dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell 169dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell @Override 170dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell public void onLayout(boolean changed, int l, int t, int r, int b) { 171dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell super.onLayout(changed, l, t, r, b); 172a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell 173a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell final boolean hasTabs = mTabContainer != null && mTabContainer.getVisibility() != GONE; 174a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell 175dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell if (mTabContainer != null && mTabContainer.getVisibility() != GONE) { 176dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell final int containerHeight = getMeasuredHeight(); 177310849abb1a0763ea9912b59187f0a4271efaa2eAdam Powell final int tabHeight = mTabContainer.getMeasuredHeight(); 178310849abb1a0763ea9912b59187f0a4271efaa2eAdam Powell 179310849abb1a0763ea9912b59187f0a4271efaa2eAdam Powell if ((mActionBarView.getDisplayOptions() & ActionBar.DISPLAY_SHOW_HOME) == 0) { 180310849abb1a0763ea9912b59187f0a4271efaa2eAdam Powell // Not showing home, put tabs on top. 181310849abb1a0763ea9912b59187f0a4271efaa2eAdam Powell final int count = getChildCount(); 182f6ce6a9bacbb220c6ea7b552c481237f23e64ae7Adam Powell for (int i = 0; i < count; i++) { 183310849abb1a0763ea9912b59187f0a4271efaa2eAdam Powell final View child = getChildAt(i); 184310849abb1a0763ea9912b59187f0a4271efaa2eAdam Powell 185310849abb1a0763ea9912b59187f0a4271efaa2eAdam Powell if (child == mTabContainer) continue; 186310849abb1a0763ea9912b59187f0a4271efaa2eAdam Powell 187f6ce6a9bacbb220c6ea7b552c481237f23e64ae7Adam Powell if (!mActionBarView.isCollapsed()) { 188f6ce6a9bacbb220c6ea7b552c481237f23e64ae7Adam Powell child.offsetTopAndBottom(tabHeight); 189f6ce6a9bacbb220c6ea7b552c481237f23e64ae7Adam Powell } 190310849abb1a0763ea9912b59187f0a4271efaa2eAdam Powell } 191310849abb1a0763ea9912b59187f0a4271efaa2eAdam Powell mTabContainer.layout(l, 0, r, tabHeight); 192310849abb1a0763ea9912b59187f0a4271efaa2eAdam Powell } else { 193310849abb1a0763ea9912b59187f0a4271efaa2eAdam Powell mTabContainer.layout(l, containerHeight - tabHeight, r, containerHeight); 194310849abb1a0763ea9912b59187f0a4271efaa2eAdam Powell } 195dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell } 196a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell 197a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell boolean needsInvalidate = false; 198a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell if (mIsSplit) { 199a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell if (mSplitBackground != null) { 200a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell mSplitBackground.setBounds(0, 0, getMeasuredWidth(), getMeasuredHeight()); 201a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell needsInvalidate = true; 202a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell } 203a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell } else { 204a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell if (mBackground != null) { 205a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell mBackground.setBounds(mActionBarView.getLeft(), mActionBarView.getTop(), 206a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell mActionBarView.getRight(), mActionBarView.getBottom()); 207a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell needsInvalidate = true; 208a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell } 209a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell if ((mIsStacked = hasTabs && mStackedBackground != null)) { 210a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell mStackedBackground.setBounds(mTabContainer.getLeft(), mTabContainer.getTop(), 211a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell mTabContainer.getRight(), mTabContainer.getBottom()); 212a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell needsInvalidate = true; 213a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell } 214a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell } 215a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell 216a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell if (needsInvalidate) { 217a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell invalidate(); 218a72ef62e5c329a19eab9935acba816fc1369c637Adam Powell } 219dae7824c4b78d02159c6ad0896f246942d7b3d8cAdam Powell } 22045f1e08c348ccb129bcc25e438c05421f7123f41Adam Powell} 221