blob: d549ac3471910d9e808de10651cbd41f4c54e08e [file] [log] [blame]
Michael Kolba2b2ba82010-08-04 17:54:03 -07001/*
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.browser;
18
Michael Kolb2d59c322011-01-25 13:18:55 -080019import android.animation.Animator;
20import android.animation.Animator.AnimatorListener;
21import android.animation.AnimatorSet;
22import android.animation.ObjectAnimator;
Michael Kolb8233fac2010-10-26 16:08:53 -070023import android.app.Activity;
Michael Kolba2b2ba82010-08-04 17:54:03 -070024import android.content.Context;
Michael Kolb678afc82011-05-17 14:52:41 -070025import android.content.res.Configuration;
Michael Kolba2b2ba82010-08-04 17:54:03 -070026import android.content.res.Resources;
27import android.graphics.Bitmap;
Michael Kolb2b5a13a2010-12-09 14:13:42 -080028import android.graphics.BitmapShader;
29import android.graphics.Canvas;
Michael Kolb467af0a2011-01-11 13:09:49 -080030import android.graphics.Matrix;
Michael Kolb2b5a13a2010-12-09 14:13:42 -080031import android.graphics.Paint;
32import android.graphics.Path;
33import android.graphics.Shader;
Michael Kolba2b2ba82010-08-04 17:54:03 -070034import android.graphics.drawable.BitmapDrawable;
35import android.graphics.drawable.Drawable;
36import android.graphics.drawable.LayerDrawable;
37import android.graphics.drawable.PaintDrawable;
Michael Kolbed217742010-08-10 17:52:34 -070038import android.view.Gravity;
Michael Kolba2b2ba82010-08-04 17:54:03 -070039import android.view.LayoutInflater;
Michael Kolba2b2ba82010-08-04 17:54:03 -070040import android.view.View;
41import android.view.View.OnClickListener;
Michael Kolbed217742010-08-10 17:52:34 -070042import android.widget.ImageButton;
Michael Kolba2b2ba82010-08-04 17:54:03 -070043import android.widget.ImageView;
44import android.widget.LinearLayout;
45import android.widget.TextView;
46
Michael Kolba2b2ba82010-08-04 17:54:03 -070047import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -080048import java.util.List;
Michael Kolba2b2ba82010-08-04 17:54:03 -070049import java.util.Map;
50
51/**
52 * tabbed title bar for xlarge screen browser
53 */
Michael Kolbb14ff2f2011-07-01 15:33:56 -070054public class TabBar extends LinearLayout implements OnClickListener {
Michael Kolba2b2ba82010-08-04 17:54:03 -070055
56 private static final int PROGRESS_MAX = 100;
57
Michael Kolb8233fac2010-10-26 16:08:53 -070058 private Activity mActivity;
59 private UiController mUiController;
60 private TabControl mTabControl;
Michael Kolb66706532010-12-12 19:50:22 -080061 private XLargeUi mUi;
Michael Kolba2b2ba82010-08-04 17:54:03 -070062
John Reck034637c2011-08-11 11:34:44 -070063 private int mTabWidth;
Michael Kolbed217742010-08-10 17:52:34 -070064
Michael Kolba2b2ba82010-08-04 17:54:03 -070065 private TabScrollView mTabs;
Michael Kolb8233fac2010-10-26 16:08:53 -070066
Michael Kolbebba8b42010-09-30 12:57:59 -070067 private ImageButton mNewTab;
68 private int mButtonWidth;
Michael Kolba2b2ba82010-08-04 17:54:03 -070069
Michael Kolb94827b62011-01-08 14:23:45 -080070 private Map<Tab, TabView> mTabMap;
Michael Kolba2b2ba82010-08-04 17:54:03 -070071
Romain Guyc8373212011-01-07 19:15:16 -080072 private int mCurrentTextureWidth = 0;
73 private int mCurrentTextureHeight = 0;
74
Michael Kolb2b5a13a2010-12-09 14:13:42 -080075 private Drawable mActiveDrawable;
76 private Drawable mInactiveDrawable;
77
Romain Guyc8373212011-01-07 19:15:16 -080078 private final Paint mActiveShaderPaint = new Paint();
79 private final Paint mInactiveShaderPaint = new Paint();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080080 private final Paint mFocusPaint = new Paint();
Romain Guyc8373212011-01-07 19:15:16 -080081 private final Matrix mActiveMatrix = new Matrix();
82 private final Matrix mInactiveMatrix = new Matrix();
83
84 private BitmapShader mActiveShader;
85 private BitmapShader mInactiveShader;
86
Michael Kolb2b5a13a2010-12-09 14:13:42 -080087 private int mTabOverlap;
Michael Kolb5a72f182011-01-13 20:35:06 -080088 private int mAddTabOverlap;
Michael Kolb2b5a13a2010-12-09 14:13:42 -080089 private int mTabSliceWidth;
Michael Kolb376b5412010-12-15 11:52:57 -080090 private boolean mUseQuickControls;
Michael Kolb2b5a13a2010-12-09 14:13:42 -080091
Michael Kolb66706532010-12-12 19:50:22 -080092 public TabBar(Activity activity, UiController controller, XLargeUi ui) {
Michael Kolb8233fac2010-10-26 16:08:53 -070093 super(activity);
94 mActivity = activity;
95 mUiController = controller;
96 mTabControl = mUiController.getTabControl();
97 mUi = ui;
98 Resources res = activity.getResources();
John Reck034637c2011-08-11 11:34:44 -070099 mTabWidth = (int) res.getDimension(R.dimen.tab_width);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800100 mActiveDrawable = res.getDrawable(R.drawable.bg_urlbar);
101 mInactiveDrawable = res.getDrawable(R.drawable.browsertab_inactive);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700102
Michael Kolb94827b62011-01-08 14:23:45 -0800103 mTabMap = new HashMap<Tab, TabView>();
Michael Kolb8233fac2010-10-26 16:08:53 -0700104 LayoutInflater factory = LayoutInflater.from(activity);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700105 factory.inflate(R.layout.tab_bar, this);
Michael Kolbf558f0d2011-01-13 19:24:18 -0800106 setPadding(0, (int) res.getDimension(R.dimen.tab_padding_top), 0, 0);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700107 mTabs = (TabScrollView) findViewById(R.id.tabs);
Michael Kolbebba8b42010-09-30 12:57:59 -0700108 mNewTab = (ImageButton) findViewById(R.id.newtab);
109 mNewTab.setOnClickListener(this);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700110
Michael Kolb1bf23132010-11-19 12:55:12 -0800111 updateTabs(mUiController.getTabs());
Michael Kolbebba8b42010-09-30 12:57:59 -0700112 mButtonWidth = -1;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800113 // tab dimensions
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800114 mTabOverlap = (int) res.getDimension(R.dimen.tab_overlap);
Michael Kolb5a72f182011-01-13 20:35:06 -0800115 mAddTabOverlap = (int) res.getDimension(R.dimen.tab_addoverlap);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800116 mTabSliceWidth = (int) res.getDimension(R.dimen.tab_slice);
Romain Guyc8373212011-01-07 19:15:16 -0800117
118 mActiveShaderPaint.setStyle(Paint.Style.FILL);
119 mActiveShaderPaint.setAntiAlias(true);
120
121 mInactiveShaderPaint.setStyle(Paint.Style.FILL);
122 mInactiveShaderPaint.setAntiAlias(true);
Michael Kolbb4cafc52011-01-12 16:55:04 -0800123
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800124 mFocusPaint.setStyle(Paint.Style.STROKE);
Michael Kolbeb7001c2011-02-16 16:07:33 -0800125 mFocusPaint.setStrokeWidth(res.getDimension(R.dimen.tab_focus_stroke));
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800126 mFocusPaint.setAntiAlias(true);
127 mFocusPaint.setColor(res.getColor(R.color.tabFocusHighlight));
Michael Kolbebba8b42010-09-30 12:57:59 -0700128 }
129
Michael Kolb678afc82011-05-17 14:52:41 -0700130 @Override
131 public void onConfigurationChanged(Configuration config) {
132 super.onConfigurationChanged(config);
133 Resources res = mActivity.getResources();
John Reck034637c2011-08-11 11:34:44 -0700134 mTabWidth = (int) res.getDimension(R.dimen.tab_width);
Michael Kolb678afc82011-05-17 14:52:41 -0700135 // force update of tab bar
136 mTabs.updateLayout();
137 }
138
Michael Kolb376b5412010-12-15 11:52:57 -0800139 void setUseQuickControls(boolean useQuickControls) {
140 mUseQuickControls = useQuickControls;
Michael Kolb467af0a2011-01-11 13:09:49 -0800141 mNewTab.setVisibility(mUseQuickControls ? View.GONE
142 : View.VISIBLE);
Michael Kolb376b5412010-12-15 11:52:57 -0800143 }
144
145 int getTabCount() {
146 return mTabMap.size();
147 }
148
Michael Kolb1bf23132010-11-19 12:55:12 -0800149 void updateTabs(List<Tab> tabs) {
150 mTabs.clearTabs();
151 mTabMap.clear();
152 for (Tab tab : tabs) {
Michael Kolb94827b62011-01-08 14:23:45 -0800153 TabView tv = buildTabView(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800154 mTabs.addTab(tv);
Michael Kolb1bf23132010-11-19 12:55:12 -0800155 }
Michael Kolbc831b632011-05-11 09:30:34 -0700156 mTabs.setSelectedTab(mTabControl.getCurrentPosition());
Michael Kolb1bf23132010-11-19 12:55:12 -0800157 }
158
Michael Kolbebba8b42010-09-30 12:57:59 -0700159 @Override
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800160 protected void onMeasure(int hspec, int vspec) {
161 super.onMeasure(hspec, vspec);
162 int w = getMeasuredWidth();
163 // adjust for new tab overlap
Michael Kolb467af0a2011-01-11 13:09:49 -0800164 if (!mUseQuickControls) {
Michael Kolb5a72f182011-01-13 20:35:06 -0800165 w -= mAddTabOverlap;
Michael Kolb467af0a2011-01-11 13:09:49 -0800166 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800167 setMeasuredDimension(w, getMeasuredHeight());
168 }
169
170 @Override
Michael Kolbebba8b42010-09-30 12:57:59 -0700171 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800172 // use paddingLeft and paddingTop
173 int pl = getPaddingLeft();
174 int pt = getPaddingTop();
Michael Kolbebba8b42010-09-30 12:57:59 -0700175 int sw = mTabs.getMeasuredWidth();
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800176 int w = right - left - pl;
Michael Kolbb4cafc52011-01-12 16:55:04 -0800177 if (mUseQuickControls) {
178 mButtonWidth = 0;
179 } else {
Michael Kolb5a72f182011-01-13 20:35:06 -0800180 mButtonWidth = mNewTab.getMeasuredWidth() - mAddTabOverlap;
Michael Kolbb4cafc52011-01-12 16:55:04 -0800181 if (w-sw < mButtonWidth) {
182 sw = w - mButtonWidth;
183 }
Michael Kolbebba8b42010-09-30 12:57:59 -0700184 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800185 mTabs.layout(pl, pt, pl + sw, bottom - top);
186 // adjust for overlap
Michael Kolbb4cafc52011-01-12 16:55:04 -0800187 if (!mUseQuickControls) {
Michael Kolb5a72f182011-01-13 20:35:06 -0800188 mNewTab.layout(pl + sw - mAddTabOverlap, pt,
189 pl + sw + mButtonWidth - mAddTabOverlap, bottom - top);
Michael Kolbb4cafc52011-01-12 16:55:04 -0800190 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700191 }
192
193 public void onClick(View view) {
Michael Kolbebba8b42010-09-30 12:57:59 -0700194 if (mNewTab == view) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700195 mUiController.openTabToHomePage();
Michael Kolbebba8b42010-09-30 12:57:59 -0700196 } else if (mTabs.getSelectedTab() == view) {
Michael Kolb467af0a2011-01-11 13:09:49 -0800197 if (mUseQuickControls) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800198 if (mUi.isTitleBarShowing() && !isLoading()) {
199 mUi.stopEditingUrl();
200 mUi.hideTitleBar();
Michael Kolb467af0a2011-01-11 13:09:49 -0800201 } else {
202 mUi.stopWebViewScrolling();
Michael Kolb46f987e2011-04-05 10:39:10 -0700203 mUi.editUrl(false);
Michael Kolb467af0a2011-01-11 13:09:49 -0800204 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800205 } else if (mUi.isTitleBarShowing() && !isLoading()) {
206 mUi.stopEditingUrl();
207 mUi.hideTitleBar();
Michael Kolbed217742010-08-10 17:52:34 -0700208 } else {
209 showUrlBar();
210 }
Michael Kolbc831b632011-05-11 09:30:34 -0700211 } else if (view instanceof TabView) {
212 final Tab tab = ((TabView) view).mTab;
Michael Kolbebba8b42010-09-30 12:57:59 -0700213 int ix = mTabs.getChildIndex(view);
214 if (ix >= 0) {
215 mTabs.setSelectedTab(ix);
Michael Kolbc831b632011-05-11 09:30:34 -0700216 mUiController.switchToTab(tab);
Michael Kolbebba8b42010-09-30 12:57:59 -0700217 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700218 }
219 }
220
Michael Kolbed217742010-08-10 17:52:34 -0700221 private void showUrlBar() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700222 mUi.stopWebViewScrolling();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800223 mUi.showTitleBar();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700224 }
225
Michael Kolb94827b62011-01-08 14:23:45 -0800226 private TabView buildTabView(Tab tab) {
227 TabView tabview = new TabView(mActivity, tab);
228 mTabMap.put(tab, tabview);
229 tabview.setOnClickListener(this);
Michael Kolb94827b62011-01-08 14:23:45 -0800230 return tabview;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700231 }
232
Romain Guyc8373212011-01-07 19:15:16 -0800233 private static Bitmap getDrawableAsBitmap(Drawable drawable, int width, int height) {
234 Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
235 Canvas c = new Canvas(b);
236 drawable.setBounds(0, 0, width, height);
237 drawable.draw(c);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -0700238 c.setBitmap(null);
Romain Guyc8373212011-01-07 19:15:16 -0800239 return b;
Michael Kolbb4cafc52011-01-12 16:55:04 -0800240 }
241
Michael Kolba2b2ba82010-08-04 17:54:03 -0700242 /**
243 * View used in the tab bar
244 */
245 class TabView extends LinearLayout implements OnClickListener {
246
Michael Kolb94827b62011-01-08 14:23:45 -0800247 Tab mTab;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700248 View mTabContent;
249 TextView mTitle;
Michael Kolbae62fd42010-08-18 16:33:28 -0700250 View mIncognito;
John Reck541f55a2011-06-07 16:34:43 -0700251 View mSnapshot;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700252 ImageView mIconView;
253 ImageView mLock;
254 ImageView mClose;
255 boolean mSelected;
256 boolean mInLoad;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800257 Path mPath;
Michael Kolbeb7001c2011-02-16 16:07:33 -0800258 Path mFocusPath;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800259 int[] mWindowPos;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700260
261 /**
262 * @param context
263 */
Michael Kolb94827b62011-01-08 14:23:45 -0800264 public TabView(Context context, Tab tab) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700265 super(context);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800266 setWillNotDraw(false);
267 mPath = new Path();
Michael Kolbeb7001c2011-02-16 16:07:33 -0800268 mFocusPath = new Path();
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800269 mWindowPos = new int[2];
Michael Kolb94827b62011-01-08 14:23:45 -0800270 mTab = tab;
Michael Kolbed217742010-08-10 17:52:34 -0700271 setGravity(Gravity.CENTER_VERTICAL);
272 setOrientation(LinearLayout.HORIZONTAL);
Michael Kolb49f15832011-01-25 15:02:27 -0800273 setPadding(mTabOverlap, 0, mTabSliceWidth, 0);
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100274 LayoutInflater inflater = LayoutInflater.from(getContext());
Michael Kolbed217742010-08-10 17:52:34 -0700275 mTabContent = inflater.inflate(R.layout.tab_title, this, true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700276 mTitle = (TextView) mTabContent.findViewById(R.id.title);
277 mIconView = (ImageView) mTabContent.findViewById(R.id.favicon);
278 mLock = (ImageView) mTabContent.findViewById(R.id.lock);
279 mClose = (ImageView) mTabContent.findViewById(R.id.close);
280 mClose.setOnClickListener(this);
Michael Kolbae62fd42010-08-18 16:33:28 -0700281 mIncognito = mTabContent.findViewById(R.id.incognito);
John Reck541f55a2011-06-07 16:34:43 -0700282 mSnapshot = mTabContent.findViewById(R.id.snapshot);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700283 mSelected = false;
284 mInLoad = false;
285 // update the status
Michael Kolb94827b62011-01-08 14:23:45 -0800286 updateFromTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700287 }
288
289 @Override
290 public void onClick(View v) {
291 if (v == mClose) {
292 closeTab();
293 }
294 }
295
Michael Kolb94827b62011-01-08 14:23:45 -0800296 private void updateFromTab() {
297 String displayTitle = mTab.getTitle();
John Reck30c714c2010-12-16 17:30:34 -0800298 if (displayTitle == null) {
Michael Kolb94827b62011-01-08 14:23:45 -0800299 displayTitle = mTab.getUrl();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700300 }
John Reck30c714c2010-12-16 17:30:34 -0800301 setDisplayTitle(displayTitle);
Michael Kolb94827b62011-01-08 14:23:45 -0800302 setProgress(mTab.getLoadProgress());
303 if (mTab.getFavicon() != null) {
John Reck034637c2011-08-11 11:34:44 -0700304 setFavicon(mUi.getFaviconDrawable(mTab.getFavicon()));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700305 }
John Reck541f55a2011-06-07 16:34:43 -0700306 updateTabIcons();
307 }
308
309 private void updateTabIcons() {
310 mIncognito.setVisibility(
311 mTab.isPrivateBrowsingEnabled() ?
312 View.VISIBLE : View.GONE);
313 mSnapshot.setVisibility(mTab.isSnapshot()
314 ? View.VISIBLE : View.GONE);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700315 }
316
317 @Override
Michael Kolbb7b115e2010-09-25 16:59:37 -0700318 public void setActivated(boolean selected) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700319 mSelected = selected;
320 mClose.setVisibility(mSelected ? View.VISIBLE : View.GONE);
John Reck034637c2011-08-11 11:34:44 -0700321 mIconView.setVisibility(mSelected ? View.GONE : View.VISIBLE);
Michael Kolb8233fac2010-10-26 16:08:53 -0700322 mTitle.setTextAppearance(mActivity, mSelected ?
Michael Kolbc7485ae2010-09-03 10:10:58 -0700323 R.style.TabTitleSelected : R.style.TabTitleUnselected);
324 setHorizontalFadingEdgeEnabled(!mSelected);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700325 super.setActivated(selected);
Michael Kolb678afc82011-05-17 14:52:41 -0700326 updateLayoutParams();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800327 setFocusable(!selected);
328 postInvalidate();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700329 }
330
Michael Kolb678afc82011-05-17 14:52:41 -0700331 public void updateLayoutParams() {
332 LayoutParams lp = (LinearLayout.LayoutParams) getLayoutParams();
John Reck034637c2011-08-11 11:34:44 -0700333 lp.width = mTabWidth;
Michael Kolb678afc82011-05-17 14:52:41 -0700334 lp.height = LayoutParams.MATCH_PARENT;
335 setLayoutParams(lp);
336 }
337
Michael Kolba2b2ba82010-08-04 17:54:03 -0700338 void setDisplayTitle(String title) {
339 mTitle.setText(title);
340 }
341
342 void setFavicon(Drawable d) {
343 mIconView.setImageDrawable(d);
344 }
345
346 void setLock(Drawable d) {
347 if (null == d) {
348 mLock.setVisibility(View.GONE);
349 } else {
350 mLock.setImageDrawable(d);
351 mLock.setVisibility(View.VISIBLE);
352 }
353 }
354
Michael Kolba2b2ba82010-08-04 17:54:03 -0700355 void setProgress(int newProgress) {
356 if (newProgress >= PROGRESS_MAX) {
357 mInLoad = false;
358 } else {
359 if (!mInLoad && getWindowToken() != null) {
360 mInLoad = true;
361 }
362 }
363 }
364
365 private void closeTab() {
Michael Kolb94827b62011-01-08 14:23:45 -0800366 if (mTab == mTabControl.getCurrentTab()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700367 mUiController.closeCurrentTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700368 } else {
Michael Kolb94827b62011-01-08 14:23:45 -0800369 mUiController.closeTab(mTab);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700370 }
371 }
372
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800373 @Override
374 protected void onLayout(boolean changed, int l, int t, int r, int b) {
375 super.onLayout(changed, l, t, r, b);
376 setTabPath(mPath, 0, 0, r - l, b - t);
Michael Kolbeb7001c2011-02-16 16:07:33 -0800377 setFocusPath(mFocusPath, 0, 0, r - l, b - t);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800378 }
Michael Kolbb4cafc52011-01-12 16:55:04 -0800379
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800380 @Override
381 protected void dispatchDraw(Canvas canvas) {
Romain Guyc8373212011-01-07 19:15:16 -0800382 if (mCurrentTextureWidth != mUi.getContentWidth() ||
383 mCurrentTextureHeight != getHeight()) {
384 mCurrentTextureWidth = mUi.getContentWidth();
385 mCurrentTextureHeight = getHeight();
386
387 if (mCurrentTextureWidth > 0 && mCurrentTextureHeight > 0) {
388 Bitmap activeTexture = getDrawableAsBitmap(mActiveDrawable,
389 mCurrentTextureWidth, mCurrentTextureHeight);
390 Bitmap inactiveTexture = getDrawableAsBitmap(mInactiveDrawable,
391 mCurrentTextureWidth, mCurrentTextureHeight);
392
393 mActiveShader = new BitmapShader(activeTexture,
394 Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
395 mActiveShaderPaint.setShader(mActiveShader);
Michael Kolbb4cafc52011-01-12 16:55:04 -0800396
Romain Guyc8373212011-01-07 19:15:16 -0800397 mInactiveShader = new BitmapShader(inactiveTexture,
398 Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
399 mInactiveShaderPaint.setShader(mInactiveShader);
400 }
401 }
Michael Kolb05902aa2011-02-22 17:43:38 -0800402 // add some monkey protection
403 if ((mActiveShader != null) && (mInactiveShader != null)) {
404 int state = canvas.save();
405 getLocationInWindow(mWindowPos);
406 Paint paint = mSelected ? mActiveShaderPaint : mInactiveShaderPaint;
407 drawClipped(canvas, paint, mPath, mWindowPos[0]);
408 canvas.restoreToCount(state);
409 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800410 super.dispatchDraw(canvas);
411 }
412
Romain Guyc8373212011-01-07 19:15:16 -0800413 private void drawClipped(Canvas canvas, Paint paint, Path clipPath, int left) {
414 // TODO: We should change the matrix/shader only when needed
415 final Matrix matrix = mSelected ? mActiveMatrix : mInactiveMatrix;
416 matrix.setTranslate(-left, 0.0f);
417 (mSelected ? mActiveShader : mInactiveShader).setLocalMatrix(matrix);
418 canvas.drawPath(clipPath, paint);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800419 if (isFocused()) {
Michael Kolbeb7001c2011-02-16 16:07:33 -0800420 canvas.drawPath(mFocusPath, mFocusPaint);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800421 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800422 }
423
424 private void setTabPath(Path path, int l, int t, int r, int b) {
425 path.reset();
426 path.moveTo(l, b);
427 path.lineTo(l, t);
428 path.lineTo(r - mTabSliceWidth, t);
429 path.lineTo(r, b);
430 path.close();
431 }
432
Michael Kolbeb7001c2011-02-16 16:07:33 -0800433 private void setFocusPath(Path path, int l, int t, int r, int b) {
434 path.reset();
435 path.moveTo(l, b);
436 path.lineTo(l, t);
437 path.lineTo(r - mTabSliceWidth, t);
438 path.lineTo(r, b);
439 }
440
Michael Kolba2b2ba82010-08-04 17:54:03 -0700441 }
442
Michael Kolb2d59c322011-01-25 13:18:55 -0800443 private void animateTabOut(final Tab tab, final TabView tv) {
444 ObjectAnimator scalex = ObjectAnimator.ofFloat(tv, "scaleX", 1.0f, 0.0f);
445 ObjectAnimator scaley = ObjectAnimator.ofFloat(tv, "scaleY", 1.0f, 0.0f);
Michael Kolb49f15832011-01-25 15:02:27 -0800446 ObjectAnimator alpha = ObjectAnimator.ofFloat(tv, "alpha", 1.0f, 0.0f);
Michael Kolb2d59c322011-01-25 13:18:55 -0800447 AnimatorSet animator = new AnimatorSet();
Michael Kolb49f15832011-01-25 15:02:27 -0800448 animator.playTogether(scalex, scaley, alpha);
Michael Kolb2d59c322011-01-25 13:18:55 -0800449 animator.setDuration(150);
450 animator.addListener(new AnimatorListener() {
451
452 @Override
453 public void onAnimationCancel(Animator animation) {
454 }
455
456 @Override
457 public void onAnimationEnd(Animator animation) {
458 mTabs.removeTab(tv);
459 mTabMap.remove(tab);
Michael Kolb8814d732011-01-26 11:22:30 -0800460 mUi.onRemoveTabCompleted(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800461 }
462
463 @Override
464 public void onAnimationRepeat(Animator animation) {
465 }
466
467 @Override
468 public void onAnimationStart(Animator animation) {
469 }
470
471 });
472 animator.start();
473 }
474
475 private void animateTabIn(final Tab tab, final TabView tv) {
Michael Kolb49f15832011-01-25 15:02:27 -0800476 ObjectAnimator scalex = ObjectAnimator.ofFloat(tv, "scaleX", 0.0f, 1.0f);
Michael Kolb2d59c322011-01-25 13:18:55 -0800477 scalex.setDuration(150);
478 scalex.addListener(new AnimatorListener() {
479
480 @Override
481 public void onAnimationCancel(Animator animation) {
482 }
483
484 @Override
485 public void onAnimationEnd(Animator animation) {
Michael Kolb8814d732011-01-26 11:22:30 -0800486 mUi.onAddTabCompleted(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800487 }
488
489 @Override
490 public void onAnimationRepeat(Animator animation) {
491 }
492
493 @Override
494 public void onAnimationStart(Animator animation) {
495 mTabs.addTab(tv);
496 }
497
498 });
499 scalex.start();
500 }
501
Michael Kolba2b2ba82010-08-04 17:54:03 -0700502 // TabChangeListener implementation
503
Michael Kolb8233fac2010-10-26 16:08:53 -0700504 public void onSetActiveTab(Tab tab) {
Michael Kolbc831b632011-05-11 09:30:34 -0700505 mTabs.setSelectedTab(mTabControl.getTabPosition(tab));
Michael Kolb94827b62011-01-08 14:23:45 -0800506 TabView tv = mTabMap.get(tab);
507 if (tv != null) {
508 tv.setProgress(tv.mTab.getLoadProgress());
Michael Kolba2b2ba82010-08-04 17:54:03 -0700509 }
510 }
511
Michael Kolba2b2ba82010-08-04 17:54:03 -0700512 public void onFavicon(Tab tab, Bitmap favicon) {
Michael Kolb94827b62011-01-08 14:23:45 -0800513 TabView tv = mTabMap.get(tab);
514 if (tv != null) {
John Reck034637c2011-08-11 11:34:44 -0700515 tv.setFavicon(mUi.getFaviconDrawable(favicon));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700516 }
517 }
518
Michael Kolba2b2ba82010-08-04 17:54:03 -0700519 public void onNewTab(Tab tab) {
Michael Kolb94827b62011-01-08 14:23:45 -0800520 TabView tv = buildTabView(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800521 animateTabIn(tab, tv);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700522 }
523
Michael Kolba2b2ba82010-08-04 17:54:03 -0700524 public void onProgress(Tab tab, int progress) {
Michael Kolb94827b62011-01-08 14:23:45 -0800525 TabView tv = mTabMap.get(tab);
526 if (tv != null) {
527 tv.setProgress(progress);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700528 }
529 }
530
Michael Kolba2b2ba82010-08-04 17:54:03 -0700531 public void onRemoveTab(Tab tab) {
Michael Kolb94827b62011-01-08 14:23:45 -0800532 TabView tv = mTabMap.get(tab);
533 if (tv != null) {
Michael Kolb2d59c322011-01-25 13:18:55 -0800534 animateTabOut(tab, tv);
535 } else {
536 mTabMap.remove(tab);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700537 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700538 }
539
Michael Kolba2b2ba82010-08-04 17:54:03 -0700540 public void onUrlAndTitle(Tab tab, String url, String title) {
Michael Kolb94827b62011-01-08 14:23:45 -0800541 TabView tv = mTabMap.get(tab);
542 if (tv != null) {
543 if (title != null) {
544 tv.setDisplayTitle(title);
545 } else if (url != null) {
546 tv.setDisplayTitle(UrlUtils.stripUrl(url));
547 }
John Reck541f55a2011-06-07 16:34:43 -0700548 tv.updateTabIcons();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700549 }
550 }
551
Michael Kolba2b2ba82010-08-04 17:54:03 -0700552 private boolean isLoading() {
Michael Kolb94827b62011-01-08 14:23:45 -0800553 TabView tv = mTabMap.get(mTabControl.getCurrentTab());
554 if (tv != null) {
555 return tv.mInLoad;
Michael Kolb8233fac2010-10-26 16:08:53 -0700556 } else {
557 return false;
558 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700559 }
560
Michael Kolba2b2ba82010-08-04 17:54:03 -0700561}