ActionBarContainer.java revision f88b915567a37c481c4c50a6cc57e1ec0e7cf50d
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 17package com.android.internal.widget; 18 19import android.app.ActionBar; 20import android.content.Context; 21import android.content.res.TypedArray; 22import android.graphics.Canvas; 23import android.graphics.drawable.Drawable; 24import android.util.AttributeSet; 25import android.view.ActionMode; 26import android.view.MotionEvent; 27import android.view.View; 28import android.view.ViewGroup; 29import 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 */ 36public class ActionBarContainer extends FrameLayout { 37 private boolean mIsTransitioning; 38 private View mTabContainer; 39 private ActionBarView mActionBarView; 40 41 private Drawable mBackground; 42 private Drawable mStackedBackground; 43 private Drawable mSplitBackground; 44 private boolean mIsSplit; 45 private boolean mIsStacked; 46 47 public ActionBarContainer(Context context) { 48 this(context, null); 49 } 50 51 public ActionBarContainer(Context context, AttributeSet attrs) { 52 super(context, attrs); 53 54 setBackgroundDrawable(null); 55 56 TypedArray a = context.obtainStyledAttributes(attrs, 57 com.android.internal.R.styleable.ActionBar); 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 } 67 a.recycle(); 68 69 setWillNotDraw(mIsSplit ? mSplitBackground == null : 70 mBackground == null && mStackedBackground == null); 71 } 72 73 @Override 74 public void onFinishInflate() { 75 super.onFinishInflate(); 76 mActionBarView = (ActionBarView) findViewById(com.android.internal.R.id.action_bar); 77 } 78 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 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 113 @Override 114 public boolean onTouchEvent(MotionEvent ev) { 115 super.onTouchEvent(ev); 116 117 // An action bar always eats touch events. 118 return true; 119 } 120 121 public void setTabContainer(ScrollingTabContainerView tabView) { 122 if (mTabContainer != null) { 123 removeView(mTabContainer); 124 } 125 mTabContainer = tabView; 126 if (tabView != null) { 127 addView(tabView); 128 final ViewGroup.LayoutParams lp = tabView.getLayoutParams(); 129 lp.width = LayoutParams.MATCH_PARENT; 130 lp.height = LayoutParams.WRAP_CONTENT; 131 tabView.setAllowCollapse(false); 132 } 133 } 134 135 public View getTabContainer() { 136 return mTabContainer; 137 } 138 139 @Override 140 public void onDraw(Canvas canvas) { 141 if (getWidth() == 0 || getHeight() == 0) { 142 return; 143 } 144 145 if (mIsSplit) { 146 if (mSplitBackground != null) mSplitBackground.draw(canvas); 147 } else { 148 if (mBackground != null) { 149 mBackground.draw(canvas); 150 } 151 if (mStackedBackground != null && mIsStacked) { 152 mStackedBackground.draw(canvas); 153 } 154 } 155 } 156 157 @Override 158 public ActionMode startActionModeForChild(View child, ActionMode.Callback callback) { 159 // No starting an action mode for an action bar child! (Where would it go?) 160 return null; 161 } 162 163 @Override 164 public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 165 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 166 167 if (mActionBarView == null) return; 168 169 final LayoutParams lp = (LayoutParams) mActionBarView.getLayoutParams(); 170 final int actionBarViewHeight = mActionBarView.isCollapsed() ? 0 : 171 mActionBarView.getMeasuredHeight() + lp.topMargin + lp.bottomMargin; 172 173 if (mTabContainer != null && mTabContainer.getVisibility() != GONE) { 174 final int mode = MeasureSpec.getMode(heightMeasureSpec); 175 if (mode == MeasureSpec.AT_MOST) { 176 final int maxHeight = MeasureSpec.getSize(heightMeasureSpec); 177 setMeasuredDimension(getMeasuredWidth(), 178 Math.min(actionBarViewHeight + mTabContainer.getMeasuredHeight(), 179 maxHeight)); 180 } 181 } 182 } 183 184 @Override 185 public void onLayout(boolean changed, int l, int t, int r, int b) { 186 super.onLayout(changed, l, t, r, b); 187 188 final boolean hasTabs = mTabContainer != null && mTabContainer.getVisibility() != GONE; 189 190 if (mTabContainer != null && mTabContainer.getVisibility() != GONE) { 191 final int containerHeight = getMeasuredHeight(); 192 final int tabHeight = mTabContainer.getMeasuredHeight(); 193 194 if ((mActionBarView.getDisplayOptions() & ActionBar.DISPLAY_SHOW_HOME) == 0) { 195 // Not showing home, put tabs on top. 196 final int count = getChildCount(); 197 for (int i = 0; i < count; i++) { 198 final View child = getChildAt(i); 199 200 if (child == mTabContainer) continue; 201 202 if (!mActionBarView.isCollapsed()) { 203 child.offsetTopAndBottom(tabHeight); 204 } 205 } 206 mTabContainer.layout(l, 0, r, tabHeight); 207 } else { 208 mTabContainer.layout(l, containerHeight - tabHeight, r, containerHeight); 209 } 210 } 211 212 boolean needsInvalidate = false; 213 if (mIsSplit) { 214 if (mSplitBackground != null) { 215 mSplitBackground.setBounds(0, 0, getMeasuredWidth(), getMeasuredHeight()); 216 needsInvalidate = true; 217 } 218 } else { 219 if (mBackground != null) { 220 mBackground.setBounds(mActionBarView.getLeft(), mActionBarView.getTop(), 221 mActionBarView.getRight(), mActionBarView.getBottom()); 222 needsInvalidate = true; 223 } 224 if ((mIsStacked = hasTabs && mStackedBackground != null)) { 225 mStackedBackground.setBounds(mTabContainer.getLeft(), mTabContainer.getTop(), 226 mTabContainer.getRight(), mTabContainer.getBottom()); 227 needsInvalidate = true; 228 } 229 } 230 231 if (needsInvalidate) { 232 invalidate(); 233 } 234 } 235} 236