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 | |
| 19 | import android.content.Context; |
| 20 | import android.content.res.TypedArray; |
| 21 | import android.util.AttributeSet; |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 22 | import android.view.ActionMode; |
Adam Powell | 6ecf3d1 | 2010-12-19 13:14:58 -0800 | [diff] [blame] | 23 | import android.view.MotionEvent; |
Adam Powell | dae7824 | 2011-04-25 15:23:41 -0700 | [diff] [blame] | 24 | import android.view.View; |
Adam Powell | 45f1e08 | 2010-12-10 14:20:13 -0800 | [diff] [blame] | 25 | import android.widget.FrameLayout; |
| 26 | |
| 27 | /** |
| 28 | * This class acts as a container for the action bar view and action mode context views. |
| 29 | * It applies special styles as needed to help handle animated transitions between them. |
| 30 | * @hide |
| 31 | */ |
| 32 | public class ActionBarContainer extends FrameLayout { |
Adam Powell | 01feaee | 2011-02-10 15:05:05 -0800 | [diff] [blame] | 33 | private boolean mIsTransitioning; |
Adam Powell | dae7824 | 2011-04-25 15:23:41 -0700 | [diff] [blame] | 34 | private View mTabContainer; |
Adam Powell | 01feaee | 2011-02-10 15:05:05 -0800 | [diff] [blame] | 35 | |
Adam Powell | 45f1e08 | 2010-12-10 14:20:13 -0800 | [diff] [blame] | 36 | public ActionBarContainer(Context context) { |
| 37 | this(context, null); |
| 38 | } |
| 39 | |
| 40 | public ActionBarContainer(Context context, AttributeSet attrs) { |
| 41 | super(context, attrs); |
| 42 | |
| 43 | TypedArray a = context.obtainStyledAttributes(attrs, |
| 44 | com.android.internal.R.styleable.ActionBar); |
| 45 | setBackgroundDrawable(a.getDrawable(com.android.internal.R.styleable.ActionBar_background)); |
| 46 | a.recycle(); |
| 47 | } |
Adam Powell | 6ecf3d1 | 2010-12-19 13:14:58 -0800 | [diff] [blame] | 48 | |
Adam Powell | 01feaee | 2011-02-10 15:05:05 -0800 | [diff] [blame] | 49 | /** |
| 50 | * Set the action bar into a "transitioning" state. While transitioning |
| 51 | * the bar will block focus and touch from all of its descendants. This |
| 52 | * prevents the user from interacting with the bar while it is animating |
| 53 | * in or out. |
| 54 | * |
| 55 | * @param isTransitioning true if the bar is currently transitioning, false otherwise. |
| 56 | */ |
| 57 | public void setTransitioning(boolean isTransitioning) { |
| 58 | mIsTransitioning = isTransitioning; |
| 59 | setDescendantFocusability(isTransitioning ? FOCUS_BLOCK_DESCENDANTS |
| 60 | : FOCUS_AFTER_DESCENDANTS); |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 65 | return mIsTransitioning || super.onInterceptTouchEvent(ev); |
| 66 | } |
| 67 | |
Adam Powell | 6ecf3d1 | 2010-12-19 13:14:58 -0800 | [diff] [blame] | 68 | @Override |
| 69 | public boolean onTouchEvent(MotionEvent ev) { |
| 70 | super.onTouchEvent(ev); |
Adam Powell | dae7824 | 2011-04-25 15:23:41 -0700 | [diff] [blame] | 71 | |
| 72 | // An action bar always eats touch events. |
Adam Powell | 6ecf3d1 | 2010-12-19 13:14:58 -0800 | [diff] [blame] | 73 | return true; |
| 74 | } |
Adam Powell | dae7824 | 2011-04-25 15:23:41 -0700 | [diff] [blame] | 75 | |
| 76 | public void setTabContainer(View tabView) { |
| 77 | if (mTabContainer != null) { |
| 78 | removeView(mTabContainer); |
| 79 | } |
| 80 | mTabContainer = tabView; |
| 81 | addView(tabView); |
| 82 | } |
| 83 | |
| 84 | public View getTabContainer() { |
| 85 | return mTabContainer; |
| 86 | } |
| 87 | |
| 88 | @Override |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 89 | public ActionMode startActionModeForChild(View child, ActionMode.Callback callback) { |
| 90 | // No starting an action mode for an action bar child! (Where would it go?) |
| 91 | return null; |
| 92 | } |
| 93 | |
| 94 | @Override |
Adam Powell | dae7824 | 2011-04-25 15:23:41 -0700 | [diff] [blame] | 95 | public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 96 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 97 | if (mTabContainer != null && mTabContainer.getVisibility() != GONE) { |
| 98 | final int mode = MeasureSpec.getMode(heightMeasureSpec); |
| 99 | if (mode == MeasureSpec.AT_MOST) { |
| 100 | final int measuredHeight = getMeasuredHeight(); |
| 101 | final int maxHeight = MeasureSpec.getSize(heightMeasureSpec); |
| 102 | setMeasuredDimension(getMeasuredWidth(), |
| 103 | Math.min(measuredHeight + mTabContainer.getMeasuredHeight(), maxHeight)); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public void onLayout(boolean changed, int l, int t, int r, int b) { |
| 110 | super.onLayout(changed, l, t, r, b); |
| 111 | if (mTabContainer != null && mTabContainer.getVisibility() != GONE) { |
| 112 | final int containerHeight = getMeasuredHeight(); |
| 113 | mTabContainer.layout(l, containerHeight - mTabContainer.getMeasuredHeight(), |
| 114 | r, containerHeight); |
| 115 | } |
| 116 | } |
Adam Powell | 45f1e08 | 2010-12-10 14:20:13 -0800 | [diff] [blame] | 117 | } |