Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame^] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | * use this file except in compliance with the License. You may obtain a copy of |
| 6 | * the License at |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 7 | * |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame^] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame^] | 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | * License for the specific language governing permissions and limitations under |
| 14 | * the License. |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | package com.android.browser; |
| 18 | |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame^] | 19 | import android.animation.Animator; |
| 20 | import android.animation.Animator.AnimatorListener; |
| 21 | import android.animation.ObjectAnimator; |
| 22 | import android.animation.PropertyValuesHolder; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 23 | import android.content.Context; |
Michael Kolb | c350376 | 2010-10-03 14:45:11 -0700 | [diff] [blame] | 24 | import android.graphics.Canvas; |
| 25 | import android.graphics.drawable.Drawable; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 26 | import android.util.AttributeSet; |
| 27 | import android.view.View; |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame^] | 28 | import android.view.animation.AccelerateInterpolator; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 29 | import android.widget.HorizontalScrollView; |
| 30 | import android.widget.LinearLayout; |
| 31 | |
| 32 | /** |
| 33 | * custom view for displaying tabs in the tabbed title bar |
| 34 | */ |
| 35 | public class TabScrollView extends HorizontalScrollView { |
| 36 | |
Michael Kolb | ebba8b4 | 2010-09-30 12:57:59 -0700 | [diff] [blame] | 37 | private BrowserActivity mBrowserActivity; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 38 | private LinearLayout mContentView; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 39 | private int mSelected; |
Michael Kolb | c350376 | 2010-10-03 14:45:11 -0700 | [diff] [blame] | 40 | private Drawable mArrowLeft; |
| 41 | private Drawable mArrowRight; |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame^] | 42 | private int mAnimationDuration; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * @param context |
| 46 | * @param attrs |
| 47 | * @param defStyle |
| 48 | */ |
| 49 | public TabScrollView(Context context, AttributeSet attrs, int defStyle) { |
| 50 | super(context, attrs, defStyle); |
| 51 | init(context); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @param context |
| 56 | * @param attrs |
| 57 | */ |
| 58 | public TabScrollView(Context context, AttributeSet attrs) { |
| 59 | super(context, attrs); |
| 60 | init(context); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @param context |
| 65 | */ |
| 66 | public TabScrollView(Context context) { |
| 67 | super(context); |
| 68 | init(context); |
| 69 | } |
| 70 | |
| 71 | private void init(Context ctx) { |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame^] | 72 | mBrowserActivity = (BrowserActivity) ctx; |
| 73 | mAnimationDuration = ctx.getResources().getInteger( |
| 74 | R.integer.tab_animation_duration); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 75 | setHorizontalScrollBarEnabled(false); |
Michael Kolb | ebba8b4 | 2010-09-30 12:57:59 -0700 | [diff] [blame] | 76 | mContentView = new LinearLayout(mBrowserActivity); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 77 | mContentView.setOrientation(LinearLayout.HORIZONTAL); |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame^] | 78 | mContentView.setLayoutParams( |
| 79 | new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT)); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 80 | addView(mContentView); |
| 81 | mSelected = -1; |
Michael Kolb | c350376 | 2010-10-03 14:45:11 -0700 | [diff] [blame] | 82 | mArrowLeft = ctx.getResources().getDrawable(R.drawable.ic_arrow_left); |
| 83 | mArrowRight = ctx.getResources().getDrawable(R.drawable.ic_arrow_right); |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame^] | 84 | // prevent ProGuard from removing the property methods |
| 85 | setScroll(getScroll()); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | @Override |
| 89 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 90 | super.onLayout(changed, left, top, right, bottom); |
| 91 | ensureChildVisible(getSelectedTab()); |
| 92 | } |
| 93 | |
| 94 | void setSelectedTab(int position) { |
| 95 | View v = getSelectedTab(); |
| 96 | if (v != null) { |
Michael Kolb | b7b115e | 2010-09-25 16:59:37 -0700 | [diff] [blame] | 97 | v.setActivated(false); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 98 | } |
| 99 | mSelected = position; |
| 100 | v = getSelectedTab(); |
| 101 | if (v != null) { |
Michael Kolb | b7b115e | 2010-09-25 16:59:37 -0700 | [diff] [blame] | 102 | v.setActivated(true); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 103 | } |
| 104 | requestLayout(); |
| 105 | } |
| 106 | |
Michael Kolb | ebba8b4 | 2010-09-30 12:57:59 -0700 | [diff] [blame] | 107 | int getChildIndex(View v) { |
| 108 | return mContentView.indexOfChild(v); |
| 109 | } |
| 110 | |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 111 | View getSelectedTab() { |
| 112 | if ((mSelected >= 0) && (mSelected < mContentView.getChildCount())) { |
| 113 | return mContentView.getChildAt(mSelected); |
| 114 | } else { |
| 115 | return null; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | void clearTabs() { |
| 120 | mContentView.removeAllViews(); |
| 121 | } |
| 122 | |
| 123 | void addTab(View tab) { |
| 124 | mContentView.addView(tab); |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame^] | 125 | animateIn(tab); |
Michael Kolb | b7b115e | 2010-09-25 16:59:37 -0700 | [diff] [blame] | 126 | tab.setActivated(false); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 129 | void removeTab(View tab) { |
Michael Kolb | bdc2a24 | 2010-09-07 13:48:07 -0700 | [diff] [blame] | 130 | int ix = mContentView.indexOfChild(tab); |
| 131 | if (ix == mSelected) { |
| 132 | mSelected = -1; |
| 133 | } else if (ix < mSelected) { |
| 134 | mSelected--; |
| 135 | } |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame^] | 136 | animateOut(tab); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame^] | 139 | private void ensureChildVisible(View child) { |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 140 | if (child != null) { |
| 141 | int childl = child.getLeft(); |
| 142 | int childr = childl + child.getWidth(); |
| 143 | int viewl = getScrollX(); |
| 144 | int viewr = viewl + getWidth(); |
| 145 | if (childl < viewl) { |
| 146 | // need scrolling to left |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame^] | 147 | animateScroll(childl); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 148 | } else if (childr > viewr) { |
| 149 | // need scrolling to right |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame^] | 150 | animateScroll(childr - viewr + viewl); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
Michael Kolb | c350376 | 2010-10-03 14:45:11 -0700 | [diff] [blame] | 155 | @Override |
| 156 | protected void dispatchDraw(Canvas canvas) { |
| 157 | super.dispatchDraw(canvas); |
| 158 | int l = getScrollX(); |
| 159 | int r = l + getWidth(); |
| 160 | int dis = 8; |
| 161 | if (l > 0) { |
| 162 | int aw = mArrowLeft.getIntrinsicWidth(); |
| 163 | mArrowLeft.setBounds(l + dis, 0, l + dis + aw, getHeight()); |
| 164 | mArrowLeft.draw(canvas); |
| 165 | } |
| 166 | if (r < mContentView.getWidth()) { |
| 167 | int aw = mArrowRight.getIntrinsicWidth(); |
| 168 | mArrowRight.setBounds(r - dis - aw, 0, r - dis, getHeight()); |
| 169 | mArrowRight.draw(canvas); |
| 170 | } |
| 171 | } |
| 172 | |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame^] | 173 | private void animateIn(View tab) { |
| 174 | ObjectAnimator animator = new ObjectAnimator<PropertyValuesHolder>( |
| 175 | mAnimationDuration, tab, |
| 176 | new PropertyValuesHolder<Integer>("TranslationX", 500, 0)); |
| 177 | animator.start(); |
| 178 | } |
| 179 | |
| 180 | private void animateOut(final View tab) { |
| 181 | ObjectAnimator animator = new ObjectAnimator<PropertyValuesHolder>( |
| 182 | mAnimationDuration, tab, |
| 183 | new PropertyValuesHolder<Integer>("TranslationX", 0, |
| 184 | getScrollX() - tab.getRight())); |
| 185 | animator.addListener(new AnimatorListener() { |
| 186 | |
| 187 | @Override |
| 188 | public void onAnimationCancel(Animator animation) { |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public void onAnimationEnd(Animator animation) { |
| 193 | mContentView.removeView(tab); |
| 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public void onAnimationRepeat(Animator animation) { |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public void onAnimationStart(Animator animation) { |
| 202 | } |
| 203 | }); |
| 204 | animator.setInterpolator(new AccelerateInterpolator()); |
| 205 | animator.start(); |
| 206 | } |
| 207 | |
| 208 | private void animateScroll(int newscroll) { |
| 209 | ObjectAnimator animator = new ObjectAnimator<PropertyValuesHolder>( |
| 210 | mAnimationDuration, this, |
| 211 | new PropertyValuesHolder<Integer>("scroll", getScrollX(), newscroll)); |
| 212 | animator.start(); |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * required for animation |
| 217 | */ |
| 218 | public void setScroll(int newscroll) { |
| 219 | scrollTo(newscroll, getScrollY()); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * required for animation |
| 224 | */ |
| 225 | public int getScroll() { |
| 226 | return getScrollX(); |
| 227 | } |
| 228 | |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 229 | } |