Adam Powell | 45f1e08 | 2010-12-10 14:20:13 -0800 | [diff] [blame] | 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 | |
| 17 | package com.android.internal.widget; |
| 18 | |
Adam Powell | 310849a | 2011-06-08 20:07:17 -0700 | [diff] [blame] | 19 | import android.app.ActionBar; |
Adam Powell | 45f1e08 | 2010-12-10 14:20:13 -0800 | [diff] [blame] | 20 | import android.content.Context; |
| 21 | import android.content.res.TypedArray; |
Adam Powell | a72ef62 | 2011-07-06 22:53:11 -0700 | [diff] [blame] | 22 | import android.graphics.Canvas; |
| 23 | import android.graphics.drawable.Drawable; |
Adam Powell | 45f1e08 | 2010-12-10 14:20:13 -0800 | [diff] [blame] | 24 | import android.util.AttributeSet; |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 25 | import android.view.ActionMode; |
Adam Powell | 6ecf3d1 | 2010-12-19 13:14:58 -0800 | [diff] [blame] | 26 | import android.view.MotionEvent; |
Adam Powell | dae7824 | 2011-04-25 15:23:41 -0700 | [diff] [blame] | 27 | import android.view.View; |
Adam Powell | af6b97e | 2011-08-11 17:40:57 -0700 | [diff] [blame] | 28 | import android.view.ViewGroup; |
Adam Powell | 45f1e08 | 2010-12-10 14:20:13 -0800 | [diff] [blame] | 29 | import android.widget.FrameLayout; |
| 30 | |
| 31 | /** |
| 32 | * This class acts as a container for the action bar view and action mode context views. |
| 33 | * It applies special styles as needed to help handle animated transitions between them. |
| 34 | * @hide |
| 35 | */ |
| 36 | public class ActionBarContainer extends FrameLayout { |
Adam Powell | 01feaee | 2011-02-10 15:05:05 -0800 | [diff] [blame] | 37 | private boolean mIsTransitioning; |
Adam Powell | dae7824 | 2011-04-25 15:23:41 -0700 | [diff] [blame] | 38 | private View mTabContainer; |
Adam Powell | 310849a | 2011-06-08 20:07:17 -0700 | [diff] [blame] | 39 | private ActionBarView mActionBarView; |
Adam Powell | 01feaee | 2011-02-10 15:05:05 -0800 | [diff] [blame] | 40 | |
Adam Powell | a72ef62 | 2011-07-06 22:53:11 -0700 | [diff] [blame] | 41 | private Drawable mBackground; |
| 42 | private Drawable mStackedBackground; |
| 43 | private Drawable mSplitBackground; |
| 44 | private boolean mIsSplit; |
| 45 | private boolean mIsStacked; |
| 46 | |
Adam Powell | 45f1e08 | 2010-12-10 14:20:13 -0800 | [diff] [blame] | 47 | public ActionBarContainer(Context context) { |
| 48 | this(context, null); |
| 49 | } |
| 50 | |
| 51 | public ActionBarContainer(Context context, AttributeSet attrs) { |
| 52 | super(context, attrs); |
| 53 | |
Adam Powell | a72ef62 | 2011-07-06 22:53:11 -0700 | [diff] [blame] | 54 | setBackgroundDrawable(null); |
| 55 | |
Adam Powell | 45f1e08 | 2010-12-10 14:20:13 -0800 | [diff] [blame] | 56 | TypedArray a = context.obtainStyledAttributes(attrs, |
| 57 | com.android.internal.R.styleable.ActionBar); |
Adam Powell | a72ef62 | 2011-07-06 22:53:11 -0700 | [diff] [blame] | 58 | mBackground = a.getDrawable(com.android.internal.R.styleable.ActionBar_background); |
| 59 | mStackedBackground = a.getDrawable( |
| 60 | com.android.internal.R.styleable.ActionBar_backgroundStacked); |
| 61 | |
| 62 | if (getId() == com.android.internal.R.id.split_action_bar) { |
| 63 | mIsSplit = true; |
| 64 | mSplitBackground = a.getDrawable( |
| 65 | com.android.internal.R.styleable.ActionBar_backgroundSplit); |
| 66 | } |
Adam Powell | 45f1e08 | 2010-12-10 14:20:13 -0800 | [diff] [blame] | 67 | a.recycle(); |
Adam Powell | a72ef62 | 2011-07-06 22:53:11 -0700 | [diff] [blame] | 68 | |
| 69 | setWillNotDraw(mIsSplit ? mSplitBackground == null : |
| 70 | mBackground == null && mStackedBackground == null); |
Adam Powell | 45f1e08 | 2010-12-10 14:20:13 -0800 | [diff] [blame] | 71 | } |
Adam Powell | 6ecf3d1 | 2010-12-19 13:14:58 -0800 | [diff] [blame] | 72 | |
Adam Powell | 310849a | 2011-06-08 20:07:17 -0700 | [diff] [blame] | 73 | @Override |
| 74 | public void onFinishInflate() { |
| 75 | super.onFinishInflate(); |
| 76 | mActionBarView = (ActionBarView) findViewById(com.android.internal.R.id.action_bar); |
| 77 | } |
| 78 | |
Adam Powell | f88b915 | 2011-09-07 14:54:32 -0700 | [diff] [blame] | 79 | public void setPrimaryBackground(Drawable bg) { |
| 80 | mBackground = bg; |
| 81 | invalidate(); |
| 82 | } |
| 83 | |
| 84 | public void setStackedBackground(Drawable bg) { |
| 85 | mStackedBackground = bg; |
| 86 | invalidate(); |
| 87 | } |
| 88 | |
| 89 | public void setSplitBackground(Drawable bg) { |
| 90 | mSplitBackground = bg; |
| 91 | invalidate(); |
| 92 | } |
| 93 | |
Adam Powell | 01feaee | 2011-02-10 15:05:05 -0800 | [diff] [blame] | 94 | /** |
| 95 | * Set the action bar into a "transitioning" state. While transitioning |
| 96 | * the bar will block focus and touch from all of its descendants. This |
| 97 | * prevents the user from interacting with the bar while it is animating |
| 98 | * in or out. |
| 99 | * |
| 100 | * @param isTransitioning true if the bar is currently transitioning, false otherwise. |
| 101 | */ |
| 102 | public void setTransitioning(boolean isTransitioning) { |
| 103 | mIsTransitioning = isTransitioning; |
| 104 | setDescendantFocusability(isTransitioning ? FOCUS_BLOCK_DESCENDANTS |
| 105 | : FOCUS_AFTER_DESCENDANTS); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 110 | return mIsTransitioning || super.onInterceptTouchEvent(ev); |
| 111 | } |
| 112 | |
Adam Powell | 6ecf3d1 | 2010-12-19 13:14:58 -0800 | [diff] [blame] | 113 | @Override |
| 114 | public boolean onTouchEvent(MotionEvent ev) { |
| 115 | super.onTouchEvent(ev); |
Adam Powell | dae7824 | 2011-04-25 15:23:41 -0700 | [diff] [blame] | 116 | |
| 117 | // An action bar always eats touch events. |
Adam Powell | 6ecf3d1 | 2010-12-19 13:14:58 -0800 | [diff] [blame] | 118 | return true; |
| 119 | } |
Adam Powell | dae7824 | 2011-04-25 15:23:41 -0700 | [diff] [blame] | 120 | |
Adam Powell | 7d09f04 | 2011-10-10 11:15:56 -0700 | [diff] [blame^] | 121 | @Override |
| 122 | public boolean onHoverEvent(MotionEvent ev) { |
| 123 | super.onHoverEvent(ev); |
| 124 | |
| 125 | // An action bar always eats hover events. |
| 126 | return true; |
| 127 | } |
| 128 | |
Adam Powell | f5645cb | 2011-08-10 22:49:02 -0700 | [diff] [blame] | 129 | public void setTabContainer(ScrollingTabContainerView tabView) { |
Adam Powell | dae7824 | 2011-04-25 15:23:41 -0700 | [diff] [blame] | 130 | if (mTabContainer != null) { |
| 131 | removeView(mTabContainer); |
| 132 | } |
| 133 | mTabContainer = tabView; |
Adam Powell | 25151e4 | 2011-05-26 15:44:59 -0700 | [diff] [blame] | 134 | if (tabView != null) { |
| 135 | addView(tabView); |
Adam Powell | af6b97e | 2011-08-11 17:40:57 -0700 | [diff] [blame] | 136 | final ViewGroup.LayoutParams lp = tabView.getLayoutParams(); |
| 137 | lp.width = LayoutParams.MATCH_PARENT; |
| 138 | lp.height = LayoutParams.WRAP_CONTENT; |
Adam Powell | f5645cb | 2011-08-10 22:49:02 -0700 | [diff] [blame] | 139 | tabView.setAllowCollapse(false); |
Adam Powell | 25151e4 | 2011-05-26 15:44:59 -0700 | [diff] [blame] | 140 | } |
Adam Powell | dae7824 | 2011-04-25 15:23:41 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | public View getTabContainer() { |
| 144 | return mTabContainer; |
| 145 | } |
| 146 | |
| 147 | @Override |
Adam Powell | a72ef62 | 2011-07-06 22:53:11 -0700 | [diff] [blame] | 148 | public void onDraw(Canvas canvas) { |
| 149 | if (getWidth() == 0 || getHeight() == 0) { |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | if (mIsSplit) { |
| 154 | if (mSplitBackground != null) mSplitBackground.draw(canvas); |
| 155 | } else { |
| 156 | if (mBackground != null) { |
| 157 | mBackground.draw(canvas); |
| 158 | } |
| 159 | if (mStackedBackground != null && mIsStacked) { |
| 160 | mStackedBackground.draw(canvas); |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | @Override |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 166 | public ActionMode startActionModeForChild(View child, ActionMode.Callback callback) { |
| 167 | // No starting an action mode for an action bar child! (Where would it go?) |
| 168 | return null; |
| 169 | } |
| 170 | |
| 171 | @Override |
Adam Powell | dae7824 | 2011-04-25 15:23:41 -0700 | [diff] [blame] | 172 | public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 173 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
Adam Powell | d21aa12 | 2011-05-27 13:09:52 -0700 | [diff] [blame] | 174 | |
Adam Powell | f6ce6a9 | 2011-06-29 10:25:01 -0700 | [diff] [blame] | 175 | if (mActionBarView == null) return; |
Adam Powell | 310849a | 2011-06-08 20:07:17 -0700 | [diff] [blame] | 176 | |
Adam Powell | f6ce6a9 | 2011-06-29 10:25:01 -0700 | [diff] [blame] | 177 | final LayoutParams lp = (LayoutParams) mActionBarView.getLayoutParams(); |
| 178 | final int actionBarViewHeight = mActionBarView.isCollapsed() ? 0 : |
| 179 | mActionBarView.getMeasuredHeight() + lp.topMargin + lp.bottomMargin; |
Adam Powell | d21aa12 | 2011-05-27 13:09:52 -0700 | [diff] [blame] | 180 | |
Adam Powell | dae7824 | 2011-04-25 15:23:41 -0700 | [diff] [blame] | 181 | if (mTabContainer != null && mTabContainer.getVisibility() != GONE) { |
| 182 | final int mode = MeasureSpec.getMode(heightMeasureSpec); |
| 183 | if (mode == MeasureSpec.AT_MOST) { |
Adam Powell | dae7824 | 2011-04-25 15:23:41 -0700 | [diff] [blame] | 184 | final int maxHeight = MeasureSpec.getSize(heightMeasureSpec); |
| 185 | setMeasuredDimension(getMeasuredWidth(), |
Adam Powell | f6ce6a9 | 2011-06-29 10:25:01 -0700 | [diff] [blame] | 186 | Math.min(actionBarViewHeight + mTabContainer.getMeasuredHeight(), |
| 187 | maxHeight)); |
Adam Powell | dae7824 | 2011-04-25 15:23:41 -0700 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public void onLayout(boolean changed, int l, int t, int r, int b) { |
| 194 | super.onLayout(changed, l, t, r, b); |
Adam Powell | a72ef62 | 2011-07-06 22:53:11 -0700 | [diff] [blame] | 195 | |
| 196 | final boolean hasTabs = mTabContainer != null && mTabContainer.getVisibility() != GONE; |
| 197 | |
Adam Powell | dae7824 | 2011-04-25 15:23:41 -0700 | [diff] [blame] | 198 | if (mTabContainer != null && mTabContainer.getVisibility() != GONE) { |
| 199 | final int containerHeight = getMeasuredHeight(); |
Adam Powell | 310849a | 2011-06-08 20:07:17 -0700 | [diff] [blame] | 200 | final int tabHeight = mTabContainer.getMeasuredHeight(); |
| 201 | |
| 202 | if ((mActionBarView.getDisplayOptions() & ActionBar.DISPLAY_SHOW_HOME) == 0) { |
| 203 | // Not showing home, put tabs on top. |
| 204 | final int count = getChildCount(); |
Adam Powell | f6ce6a9 | 2011-06-29 10:25:01 -0700 | [diff] [blame] | 205 | for (int i = 0; i < count; i++) { |
Adam Powell | 310849a | 2011-06-08 20:07:17 -0700 | [diff] [blame] | 206 | final View child = getChildAt(i); |
| 207 | |
| 208 | if (child == mTabContainer) continue; |
| 209 | |
Adam Powell | f6ce6a9 | 2011-06-29 10:25:01 -0700 | [diff] [blame] | 210 | if (!mActionBarView.isCollapsed()) { |
| 211 | child.offsetTopAndBottom(tabHeight); |
| 212 | } |
Adam Powell | 310849a | 2011-06-08 20:07:17 -0700 | [diff] [blame] | 213 | } |
| 214 | mTabContainer.layout(l, 0, r, tabHeight); |
| 215 | } else { |
| 216 | mTabContainer.layout(l, containerHeight - tabHeight, r, containerHeight); |
| 217 | } |
Adam Powell | dae7824 | 2011-04-25 15:23:41 -0700 | [diff] [blame] | 218 | } |
Adam Powell | a72ef62 | 2011-07-06 22:53:11 -0700 | [diff] [blame] | 219 | |
| 220 | boolean needsInvalidate = false; |
| 221 | if (mIsSplit) { |
| 222 | if (mSplitBackground != null) { |
| 223 | mSplitBackground.setBounds(0, 0, getMeasuredWidth(), getMeasuredHeight()); |
| 224 | needsInvalidate = true; |
| 225 | } |
| 226 | } else { |
| 227 | if (mBackground != null) { |
| 228 | mBackground.setBounds(mActionBarView.getLeft(), mActionBarView.getTop(), |
| 229 | mActionBarView.getRight(), mActionBarView.getBottom()); |
| 230 | needsInvalidate = true; |
| 231 | } |
| 232 | if ((mIsStacked = hasTabs && mStackedBackground != null)) { |
| 233 | mStackedBackground.setBounds(mTabContainer.getLeft(), mTabContainer.getTop(), |
| 234 | mTabContainer.getRight(), mTabContainer.getBottom()); |
| 235 | needsInvalidate = true; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | if (needsInvalidate) { |
| 240 | invalidate(); |
| 241 | } |
Adam Powell | dae7824 | 2011-04-25 15:23:41 -0700 | [diff] [blame] | 242 | } |
Adam Powell | 45f1e08 | 2010-12-10 14:20:13 -0800 | [diff] [blame] | 243 | } |