blob: d1652dda21cf9e4e3db3ca4bb966781cf445ee45 [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;
38import android.view.ContextMenu;
Michael Kolbed217742010-08-10 17:52:34 -070039import android.view.Gravity;
Michael Kolba2b2ba82010-08-04 17:54:03 -070040import android.view.LayoutInflater;
41import android.view.MenuInflater;
42import android.view.View;
43import android.view.View.OnClickListener;
Michael Kolbed217742010-08-10 17:52:34 -070044import android.widget.ImageButton;
Michael Kolba2b2ba82010-08-04 17:54:03 -070045import android.widget.ImageView;
46import android.widget.LinearLayout;
47import android.widget.TextView;
48
Michael Kolba2b2ba82010-08-04 17:54:03 -070049import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -080050import java.util.List;
Michael Kolba2b2ba82010-08-04 17:54:03 -070051import java.util.Map;
52
53/**
54 * tabbed title bar for xlarge screen browser
55 */
Michael Kolbb14ff2f2011-07-01 15:33:56 -070056public class TabBar extends LinearLayout implements OnClickListener {
Michael Kolba2b2ba82010-08-04 17:54:03 -070057
58 private static final int PROGRESS_MAX = 100;
59
Michael Kolb8233fac2010-10-26 16:08:53 -070060 private Activity mActivity;
61 private UiController mUiController;
62 private TabControl mTabControl;
Michael Kolb66706532010-12-12 19:50:22 -080063 private XLargeUi mUi;
Michael Kolba2b2ba82010-08-04 17:54:03 -070064
Michael Kolb678afc82011-05-17 14:52:41 -070065 private int mTabWidthSelected;
66 private int mTabWidthUnselected;
Michael Kolbed217742010-08-10 17:52:34 -070067
Michael Kolba2b2ba82010-08-04 17:54:03 -070068 private TabScrollView mTabs;
Michael Kolb8233fac2010-10-26 16:08:53 -070069
Michael Kolbebba8b42010-09-30 12:57:59 -070070 private ImageButton mNewTab;
71 private int mButtonWidth;
Michael Kolba2b2ba82010-08-04 17:54:03 -070072
Michael Kolb94827b62011-01-08 14:23:45 -080073 private Map<Tab, TabView> mTabMap;
Michael Kolba2b2ba82010-08-04 17:54:03 -070074
John Reckaff60fb2010-10-25 13:47:58 -070075 private Drawable mGenericFavicon;
76
Romain Guyc8373212011-01-07 19:15:16 -080077 private int mCurrentTextureWidth = 0;
78 private int mCurrentTextureHeight = 0;
79
Michael Kolb2b5a13a2010-12-09 14:13:42 -080080 private Drawable mActiveDrawable;
81 private Drawable mInactiveDrawable;
82
Romain Guyc8373212011-01-07 19:15:16 -080083 private final Paint mActiveShaderPaint = new Paint();
84 private final Paint mInactiveShaderPaint = new Paint();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080085 private final Paint mFocusPaint = new Paint();
Romain Guyc8373212011-01-07 19:15:16 -080086 private final Matrix mActiveMatrix = new Matrix();
87 private final Matrix mInactiveMatrix = new Matrix();
88
89 private BitmapShader mActiveShader;
90 private BitmapShader mInactiveShader;
91
Michael Kolb2b5a13a2010-12-09 14:13:42 -080092 private int mTabOverlap;
Michael Kolb5a72f182011-01-13 20:35:06 -080093 private int mAddTabOverlap;
Michael Kolb2b5a13a2010-12-09 14:13:42 -080094 private int mTabSliceWidth;
Michael Kolb376b5412010-12-15 11:52:57 -080095 private boolean mUseQuickControls;
Michael Kolb2b5a13a2010-12-09 14:13:42 -080096
Michael Kolb66706532010-12-12 19:50:22 -080097 public TabBar(Activity activity, UiController controller, XLargeUi ui) {
Michael Kolb8233fac2010-10-26 16:08:53 -070098 super(activity);
99 mActivity = activity;
100 mUiController = controller;
101 mTabControl = mUiController.getTabControl();
102 mUi = ui;
103 Resources res = activity.getResources();
Michael Kolbed217742010-08-10 17:52:34 -0700104 mTabWidthSelected = (int) res.getDimension(R.dimen.tab_width_selected);
105 mTabWidthUnselected = (int) res.getDimension(R.dimen.tab_width_unselected);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800106 mActiveDrawable = res.getDrawable(R.drawable.bg_urlbar);
107 mInactiveDrawable = res.getDrawable(R.drawable.browsertab_inactive);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700108
Michael Kolb94827b62011-01-08 14:23:45 -0800109 mTabMap = new HashMap<Tab, TabView>();
Michael Kolb8233fac2010-10-26 16:08:53 -0700110 LayoutInflater factory = LayoutInflater.from(activity);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700111 factory.inflate(R.layout.tab_bar, this);
Michael Kolbf558f0d2011-01-13 19:24:18 -0800112 setPadding(0, (int) res.getDimension(R.dimen.tab_padding_top), 0, 0);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700113 mTabs = (TabScrollView) findViewById(R.id.tabs);
Michael Kolbebba8b42010-09-30 12:57:59 -0700114 mNewTab = (ImageButton) findViewById(R.id.newtab);
115 mNewTab.setOnClickListener(this);
John Reckaff60fb2010-10-25 13:47:58 -0700116 mGenericFavicon = res.getDrawable(R.drawable.app_web_browser_sm);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700117
Michael Kolb1bf23132010-11-19 12:55:12 -0800118 updateTabs(mUiController.getTabs());
Michael Kolbebba8b42010-09-30 12:57:59 -0700119 mButtonWidth = -1;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800120 // tab dimensions
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800121 mTabOverlap = (int) res.getDimension(R.dimen.tab_overlap);
Michael Kolb5a72f182011-01-13 20:35:06 -0800122 mAddTabOverlap = (int) res.getDimension(R.dimen.tab_addoverlap);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800123 mTabSliceWidth = (int) res.getDimension(R.dimen.tab_slice);
Romain Guyc8373212011-01-07 19:15:16 -0800124
125 mActiveShaderPaint.setStyle(Paint.Style.FILL);
126 mActiveShaderPaint.setAntiAlias(true);
127
128 mInactiveShaderPaint.setStyle(Paint.Style.FILL);
129 mInactiveShaderPaint.setAntiAlias(true);
Michael Kolbb4cafc52011-01-12 16:55:04 -0800130
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800131 mFocusPaint.setStyle(Paint.Style.STROKE);
Michael Kolbeb7001c2011-02-16 16:07:33 -0800132 mFocusPaint.setStrokeWidth(res.getDimension(R.dimen.tab_focus_stroke));
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800133 mFocusPaint.setAntiAlias(true);
134 mFocusPaint.setColor(res.getColor(R.color.tabFocusHighlight));
Michael Kolbebba8b42010-09-30 12:57:59 -0700135 }
136
Michael Kolb678afc82011-05-17 14:52:41 -0700137 @Override
138 public void onConfigurationChanged(Configuration config) {
139 super.onConfigurationChanged(config);
140 Resources res = mActivity.getResources();
141 mTabWidthSelected = (int) res.getDimension(R.dimen.tab_width_selected);
142 mTabWidthUnselected = (int) res.getDimension(R.dimen.tab_width_unselected);
143 // force update of tab bar
144 mTabs.updateLayout();
145 }
146
Michael Kolb376b5412010-12-15 11:52:57 -0800147 void setUseQuickControls(boolean useQuickControls) {
148 mUseQuickControls = useQuickControls;
Michael Kolb467af0a2011-01-11 13:09:49 -0800149 mNewTab.setVisibility(mUseQuickControls ? View.GONE
150 : View.VISIBLE);
Michael Kolb376b5412010-12-15 11:52:57 -0800151 }
152
153 int getTabCount() {
154 return mTabMap.size();
155 }
156
Michael Kolb1bf23132010-11-19 12:55:12 -0800157 void updateTabs(List<Tab> tabs) {
158 mTabs.clearTabs();
159 mTabMap.clear();
160 for (Tab tab : tabs) {
Michael Kolb94827b62011-01-08 14:23:45 -0800161 TabView tv = buildTabView(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800162 mTabs.addTab(tv);
Michael Kolb1bf23132010-11-19 12:55:12 -0800163 }
Michael Kolbc831b632011-05-11 09:30:34 -0700164 mTabs.setSelectedTab(mTabControl.getCurrentPosition());
Michael Kolb1bf23132010-11-19 12:55:12 -0800165 }
166
Michael Kolbebba8b42010-09-30 12:57:59 -0700167 @Override
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800168 protected void onMeasure(int hspec, int vspec) {
169 super.onMeasure(hspec, vspec);
170 int w = getMeasuredWidth();
171 // adjust for new tab overlap
Michael Kolb467af0a2011-01-11 13:09:49 -0800172 if (!mUseQuickControls) {
Michael Kolb5a72f182011-01-13 20:35:06 -0800173 w -= mAddTabOverlap;
Michael Kolb467af0a2011-01-11 13:09:49 -0800174 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800175 setMeasuredDimension(w, getMeasuredHeight());
176 }
177
178 @Override
Michael Kolbebba8b42010-09-30 12:57:59 -0700179 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800180 // use paddingLeft and paddingTop
181 int pl = getPaddingLeft();
182 int pt = getPaddingTop();
Michael Kolbebba8b42010-09-30 12:57:59 -0700183 int sw = mTabs.getMeasuredWidth();
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800184 int w = right - left - pl;
Michael Kolbb4cafc52011-01-12 16:55:04 -0800185 if (mUseQuickControls) {
186 mButtonWidth = 0;
187 } else {
Michael Kolb5a72f182011-01-13 20:35:06 -0800188 mButtonWidth = mNewTab.getMeasuredWidth() - mAddTabOverlap;
Michael Kolbb4cafc52011-01-12 16:55:04 -0800189 if (w-sw < mButtonWidth) {
190 sw = w - mButtonWidth;
191 }
Michael Kolbebba8b42010-09-30 12:57:59 -0700192 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800193 mTabs.layout(pl, pt, pl + sw, bottom - top);
194 // adjust for overlap
Michael Kolbb4cafc52011-01-12 16:55:04 -0800195 if (!mUseQuickControls) {
Michael Kolb5a72f182011-01-13 20:35:06 -0800196 mNewTab.layout(pl + sw - mAddTabOverlap, pt,
197 pl + sw + mButtonWidth - mAddTabOverlap, bottom - top);
Michael Kolbb4cafc52011-01-12 16:55:04 -0800198 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700199 }
200
201 public void onClick(View view) {
Michael Kolbebba8b42010-09-30 12:57:59 -0700202 if (mNewTab == view) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700203 mUiController.openTabToHomePage();
Michael Kolbebba8b42010-09-30 12:57:59 -0700204 } else if (mTabs.getSelectedTab() == view) {
Michael Kolb467af0a2011-01-11 13:09:49 -0800205 if (mUseQuickControls) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800206 if (mUi.isTitleBarShowing() && !isLoading()) {
207 mUi.stopEditingUrl();
208 mUi.hideTitleBar();
Michael Kolb467af0a2011-01-11 13:09:49 -0800209 } else {
210 mUi.stopWebViewScrolling();
Michael Kolb46f987e2011-04-05 10:39:10 -0700211 mUi.editUrl(false);
Michael Kolb467af0a2011-01-11 13:09:49 -0800212 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800213 } else if (mUi.isTitleBarShowing() && !isLoading()) {
214 mUi.stopEditingUrl();
215 mUi.hideTitleBar();
Michael Kolbed217742010-08-10 17:52:34 -0700216 } else {
217 showUrlBar();
218 }
Michael Kolbc831b632011-05-11 09:30:34 -0700219 } else if (view instanceof TabView) {
220 final Tab tab = ((TabView) view).mTab;
Michael Kolbebba8b42010-09-30 12:57:59 -0700221 int ix = mTabs.getChildIndex(view);
222 if (ix >= 0) {
223 mTabs.setSelectedTab(ix);
Michael Kolbc831b632011-05-11 09:30:34 -0700224 mUiController.switchToTab(tab);
Michael Kolbebba8b42010-09-30 12:57:59 -0700225 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700226 }
227 }
228
Michael Kolbed217742010-08-10 17:52:34 -0700229 private void showUrlBar() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700230 mUi.stopWebViewScrolling();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800231 mUi.showTitleBar();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700232 }
233
Michael Kolb94827b62011-01-08 14:23:45 -0800234 private TabView buildTabView(Tab tab) {
235 TabView tabview = new TabView(mActivity, tab);
236 mTabMap.put(tab, tabview);
237 tabview.setOnClickListener(this);
Michael Kolb94827b62011-01-08 14:23:45 -0800238 return tabview;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700239 }
240
Romain Guyc8373212011-01-07 19:15:16 -0800241 private static Bitmap getDrawableAsBitmap(Drawable drawable, int width, int height) {
242 Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
243 Canvas c = new Canvas(b);
244 drawable.setBounds(0, 0, width, height);
245 drawable.draw(c);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -0700246 c.setBitmap(null);
Romain Guyc8373212011-01-07 19:15:16 -0800247 return b;
Michael Kolbb4cafc52011-01-12 16:55:04 -0800248 }
249
Michael Kolba2b2ba82010-08-04 17:54:03 -0700250 /**
251 * View used in the tab bar
252 */
253 class TabView extends LinearLayout implements OnClickListener {
254
Michael Kolb94827b62011-01-08 14:23:45 -0800255 Tab mTab;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700256 View mTabContent;
257 TextView mTitle;
Michael Kolbae62fd42010-08-18 16:33:28 -0700258 View mIncognito;
John Reck541f55a2011-06-07 16:34:43 -0700259 View mSnapshot;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700260 ImageView mIconView;
261 ImageView mLock;
262 ImageView mClose;
263 boolean mSelected;
264 boolean mInLoad;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800265 Path mPath;
Michael Kolbeb7001c2011-02-16 16:07:33 -0800266 Path mFocusPath;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800267 int[] mWindowPos;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700268
269 /**
270 * @param context
271 */
Michael Kolb94827b62011-01-08 14:23:45 -0800272 public TabView(Context context, Tab tab) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700273 super(context);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800274 setWillNotDraw(false);
275 mPath = new Path();
Michael Kolbeb7001c2011-02-16 16:07:33 -0800276 mFocusPath = new Path();
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800277 mWindowPos = new int[2];
Michael Kolb94827b62011-01-08 14:23:45 -0800278 mTab = tab;
Michael Kolbed217742010-08-10 17:52:34 -0700279 setGravity(Gravity.CENTER_VERTICAL);
280 setOrientation(LinearLayout.HORIZONTAL);
Michael Kolb49f15832011-01-25 15:02:27 -0800281 setPadding(mTabOverlap, 0, mTabSliceWidth, 0);
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100282 LayoutInflater inflater = LayoutInflater.from(getContext());
Michael Kolbed217742010-08-10 17:52:34 -0700283 mTabContent = inflater.inflate(R.layout.tab_title, this, true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700284 mTitle = (TextView) mTabContent.findViewById(R.id.title);
285 mIconView = (ImageView) mTabContent.findViewById(R.id.favicon);
286 mLock = (ImageView) mTabContent.findViewById(R.id.lock);
287 mClose = (ImageView) mTabContent.findViewById(R.id.close);
288 mClose.setOnClickListener(this);
Michael Kolbae62fd42010-08-18 16:33:28 -0700289 mIncognito = mTabContent.findViewById(R.id.incognito);
John Reck541f55a2011-06-07 16:34:43 -0700290 mSnapshot = mTabContent.findViewById(R.id.snapshot);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700291 mSelected = false;
292 mInLoad = false;
293 // update the status
Michael Kolb94827b62011-01-08 14:23:45 -0800294 updateFromTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700295 }
296
297 @Override
298 public void onClick(View v) {
299 if (v == mClose) {
300 closeTab();
301 }
302 }
303
Michael Kolb94827b62011-01-08 14:23:45 -0800304 private void updateFromTab() {
305 String displayTitle = mTab.getTitle();
John Reck30c714c2010-12-16 17:30:34 -0800306 if (displayTitle == null) {
Michael Kolb94827b62011-01-08 14:23:45 -0800307 displayTitle = mTab.getUrl();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700308 }
John Reck30c714c2010-12-16 17:30:34 -0800309 setDisplayTitle(displayTitle);
Michael Kolb94827b62011-01-08 14:23:45 -0800310 setProgress(mTab.getLoadProgress());
311 if (mTab.getFavicon() != null) {
312 setFavicon(renderFavicon(mTab.getFavicon()));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700313 }
John Reck541f55a2011-06-07 16:34:43 -0700314 updateTabIcons();
315 }
316
317 private void updateTabIcons() {
318 mIncognito.setVisibility(
319 mTab.isPrivateBrowsingEnabled() ?
320 View.VISIBLE : View.GONE);
321 mSnapshot.setVisibility(mTab.isSnapshot()
322 ? View.VISIBLE : View.GONE);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700323 }
324
325 @Override
Michael Kolbb7b115e2010-09-25 16:59:37 -0700326 public void setActivated(boolean selected) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700327 mSelected = selected;
328 mClose.setVisibility(mSelected ? View.VISIBLE : View.GONE);
Michael Kolb8233fac2010-10-26 16:08:53 -0700329 mTitle.setTextAppearance(mActivity, mSelected ?
Michael Kolbc7485ae2010-09-03 10:10:58 -0700330 R.style.TabTitleSelected : R.style.TabTitleUnselected);
331 setHorizontalFadingEdgeEnabled(!mSelected);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700332 super.setActivated(selected);
Michael Kolb678afc82011-05-17 14:52:41 -0700333 updateLayoutParams();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800334 setFocusable(!selected);
335 postInvalidate();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700336 }
337
Michael Kolb678afc82011-05-17 14:52:41 -0700338 public void updateLayoutParams() {
339 LayoutParams lp = (LinearLayout.LayoutParams) getLayoutParams();
340 lp.width = mSelected ? mTabWidthSelected : mTabWidthUnselected;
341 lp.height = LayoutParams.MATCH_PARENT;
342 setLayoutParams(lp);
343 }
344
Michael Kolba2b2ba82010-08-04 17:54:03 -0700345 void setDisplayTitle(String title) {
346 mTitle.setText(title);
347 }
348
349 void setFavicon(Drawable d) {
350 mIconView.setImageDrawable(d);
351 }
352
353 void setLock(Drawable d) {
354 if (null == d) {
355 mLock.setVisibility(View.GONE);
356 } else {
357 mLock.setImageDrawable(d);
358 mLock.setVisibility(View.VISIBLE);
359 }
360 }
361
Michael Kolba2b2ba82010-08-04 17:54:03 -0700362 void setProgress(int newProgress) {
363 if (newProgress >= PROGRESS_MAX) {
364 mInLoad = false;
365 } else {
366 if (!mInLoad && getWindowToken() != null) {
367 mInLoad = true;
368 }
369 }
370 }
371
372 private void closeTab() {
Michael Kolb94827b62011-01-08 14:23:45 -0800373 if (mTab == mTabControl.getCurrentTab()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700374 mUiController.closeCurrentTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700375 } else {
Michael Kolb94827b62011-01-08 14:23:45 -0800376 mUiController.closeTab(mTab);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700377 }
378 }
379
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800380 @Override
381 protected void onLayout(boolean changed, int l, int t, int r, int b) {
382 super.onLayout(changed, l, t, r, b);
383 setTabPath(mPath, 0, 0, r - l, b - t);
Michael Kolbeb7001c2011-02-16 16:07:33 -0800384 setFocusPath(mFocusPath, 0, 0, r - l, b - t);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800385 }
Michael Kolbb4cafc52011-01-12 16:55:04 -0800386
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800387 @Override
388 protected void dispatchDraw(Canvas canvas) {
Romain Guyc8373212011-01-07 19:15:16 -0800389 if (mCurrentTextureWidth != mUi.getContentWidth() ||
390 mCurrentTextureHeight != getHeight()) {
391 mCurrentTextureWidth = mUi.getContentWidth();
392 mCurrentTextureHeight = getHeight();
393
394 if (mCurrentTextureWidth > 0 && mCurrentTextureHeight > 0) {
395 Bitmap activeTexture = getDrawableAsBitmap(mActiveDrawable,
396 mCurrentTextureWidth, mCurrentTextureHeight);
397 Bitmap inactiveTexture = getDrawableAsBitmap(mInactiveDrawable,
398 mCurrentTextureWidth, mCurrentTextureHeight);
399
400 mActiveShader = new BitmapShader(activeTexture,
401 Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
402 mActiveShaderPaint.setShader(mActiveShader);
Michael Kolbb4cafc52011-01-12 16:55:04 -0800403
Romain Guyc8373212011-01-07 19:15:16 -0800404 mInactiveShader = new BitmapShader(inactiveTexture,
405 Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
406 mInactiveShaderPaint.setShader(mInactiveShader);
407 }
408 }
Michael Kolb05902aa2011-02-22 17:43:38 -0800409 // add some monkey protection
410 if ((mActiveShader != null) && (mInactiveShader != null)) {
411 int state = canvas.save();
412 getLocationInWindow(mWindowPos);
413 Paint paint = mSelected ? mActiveShaderPaint : mInactiveShaderPaint;
414 drawClipped(canvas, paint, mPath, mWindowPos[0]);
415 canvas.restoreToCount(state);
416 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800417 super.dispatchDraw(canvas);
418 }
419
Romain Guyc8373212011-01-07 19:15:16 -0800420 private void drawClipped(Canvas canvas, Paint paint, Path clipPath, int left) {
421 // TODO: We should change the matrix/shader only when needed
422 final Matrix matrix = mSelected ? mActiveMatrix : mInactiveMatrix;
423 matrix.setTranslate(-left, 0.0f);
424 (mSelected ? mActiveShader : mInactiveShader).setLocalMatrix(matrix);
425 canvas.drawPath(clipPath, paint);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800426 if (isFocused()) {
Michael Kolbeb7001c2011-02-16 16:07:33 -0800427 canvas.drawPath(mFocusPath, mFocusPaint);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800428 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800429 }
430
431 private void setTabPath(Path path, int l, int t, int r, int b) {
432 path.reset();
433 path.moveTo(l, b);
434 path.lineTo(l, t);
435 path.lineTo(r - mTabSliceWidth, t);
436 path.lineTo(r, b);
437 path.close();
438 }
439
Michael Kolbeb7001c2011-02-16 16:07:33 -0800440 private void setFocusPath(Path path, int l, int t, int r, int b) {
441 path.reset();
442 path.moveTo(l, b);
443 path.lineTo(l, t);
444 path.lineTo(r - mTabSliceWidth, t);
445 path.lineTo(r, b);
446 }
447
Michael Kolba2b2ba82010-08-04 17:54:03 -0700448 }
449
Michael Kolb49f15832011-01-25 15:02:27 -0800450 static Drawable createFaviconBackground(Context context) {
451 PaintDrawable faviconBackground = new PaintDrawable();
452 Resources res = context.getResources();
453 faviconBackground.getPaint().setColor(context.getResources()
454 .getColor(R.color.tabFaviconBackground));
455 faviconBackground.setCornerRadius(
456 res.getDimension(R.dimen.tab_favicon_corner_radius));
457 return faviconBackground;
458 }
459
Michael Kolb94827b62011-01-08 14:23:45 -0800460 private Drawable renderFavicon(Bitmap icon) {
Michael Kolb49f15832011-01-25 15:02:27 -0800461 Drawable[] array = new Drawable[2];
462 array[0] = createFaviconBackground(getContext());
Michael Kolb94827b62011-01-08 14:23:45 -0800463 if (icon == null) {
Michael Kolb49f15832011-01-25 15:02:27 -0800464 array[1] = mGenericFavicon;
Michael Kolb94827b62011-01-08 14:23:45 -0800465 } else {
Michael Kolb49f15832011-01-25 15:02:27 -0800466 array[1] = new BitmapDrawable(icon);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700467 }
Michael Kolb94827b62011-01-08 14:23:45 -0800468 LayerDrawable d = new LayerDrawable(array);
Michael Kolb49f15832011-01-25 15:02:27 -0800469 d.setLayerInset(1, 2, 2, 2, 2);
Michael Kolb94827b62011-01-08 14:23:45 -0800470 return d;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700471 }
472
Michael Kolb2d59c322011-01-25 13:18:55 -0800473 private void animateTabOut(final Tab tab, final TabView tv) {
474 ObjectAnimator scalex = ObjectAnimator.ofFloat(tv, "scaleX", 1.0f, 0.0f);
475 ObjectAnimator scaley = ObjectAnimator.ofFloat(tv, "scaleY", 1.0f, 0.0f);
Michael Kolb49f15832011-01-25 15:02:27 -0800476 ObjectAnimator alpha = ObjectAnimator.ofFloat(tv, "alpha", 1.0f, 0.0f);
Michael Kolb2d59c322011-01-25 13:18:55 -0800477 AnimatorSet animator = new AnimatorSet();
Michael Kolb49f15832011-01-25 15:02:27 -0800478 animator.playTogether(scalex, scaley, alpha);
Michael Kolb2d59c322011-01-25 13:18:55 -0800479 animator.setDuration(150);
480 animator.addListener(new AnimatorListener() {
481
482 @Override
483 public void onAnimationCancel(Animator animation) {
484 }
485
486 @Override
487 public void onAnimationEnd(Animator animation) {
488 mTabs.removeTab(tv);
489 mTabMap.remove(tab);
Michael Kolb8814d732011-01-26 11:22:30 -0800490 mUi.onRemoveTabCompleted(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800491 }
492
493 @Override
494 public void onAnimationRepeat(Animator animation) {
495 }
496
497 @Override
498 public void onAnimationStart(Animator animation) {
499 }
500
501 });
502 animator.start();
503 }
504
505 private void animateTabIn(final Tab tab, final TabView tv) {
Michael Kolb49f15832011-01-25 15:02:27 -0800506 ObjectAnimator scalex = ObjectAnimator.ofFloat(tv, "scaleX", 0.0f, 1.0f);
Michael Kolb2d59c322011-01-25 13:18:55 -0800507 scalex.setDuration(150);
508 scalex.addListener(new AnimatorListener() {
509
510 @Override
511 public void onAnimationCancel(Animator animation) {
512 }
513
514 @Override
515 public void onAnimationEnd(Animator animation) {
Michael Kolb8814d732011-01-26 11:22:30 -0800516 mUi.onAddTabCompleted(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800517 }
518
519 @Override
520 public void onAnimationRepeat(Animator animation) {
521 }
522
523 @Override
524 public void onAnimationStart(Animator animation) {
525 mTabs.addTab(tv);
526 }
527
528 });
529 scalex.start();
530 }
531
Michael Kolba2b2ba82010-08-04 17:54:03 -0700532 // TabChangeListener implementation
533
Michael Kolb8233fac2010-10-26 16:08:53 -0700534 public void onSetActiveTab(Tab tab) {
Michael Kolbc831b632011-05-11 09:30:34 -0700535 mTabs.setSelectedTab(mTabControl.getTabPosition(tab));
Michael Kolb94827b62011-01-08 14:23:45 -0800536 TabView tv = mTabMap.get(tab);
537 if (tv != null) {
538 tv.setProgress(tv.mTab.getLoadProgress());
Michael Kolba2b2ba82010-08-04 17:54:03 -0700539 }
540 }
541
Michael Kolba2b2ba82010-08-04 17:54:03 -0700542 public void onFavicon(Tab tab, Bitmap favicon) {
Michael Kolb94827b62011-01-08 14:23:45 -0800543 TabView tv = mTabMap.get(tab);
544 if (tv != null) {
545 tv.setFavicon(renderFavicon(favicon));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700546 }
547 }
548
Michael Kolba2b2ba82010-08-04 17:54:03 -0700549 public void onNewTab(Tab tab) {
Michael Kolb94827b62011-01-08 14:23:45 -0800550 TabView tv = buildTabView(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800551 animateTabIn(tab, tv);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700552 }
553
Michael Kolba2b2ba82010-08-04 17:54:03 -0700554 public void onProgress(Tab tab, int progress) {
Michael Kolb94827b62011-01-08 14:23:45 -0800555 TabView tv = mTabMap.get(tab);
556 if (tv != null) {
557 tv.setProgress(progress);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700558 }
559 }
560
Michael Kolba2b2ba82010-08-04 17:54:03 -0700561 public void onRemoveTab(Tab tab) {
Michael Kolb94827b62011-01-08 14:23:45 -0800562 TabView tv = mTabMap.get(tab);
563 if (tv != null) {
Michael Kolb2d59c322011-01-25 13:18:55 -0800564 animateTabOut(tab, tv);
565 } else {
566 mTabMap.remove(tab);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700567 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700568 }
569
Michael Kolba2b2ba82010-08-04 17:54:03 -0700570 public void onUrlAndTitle(Tab tab, String url, String title) {
Michael Kolb94827b62011-01-08 14:23:45 -0800571 TabView tv = mTabMap.get(tab);
572 if (tv != null) {
573 if (title != null) {
574 tv.setDisplayTitle(title);
575 } else if (url != null) {
576 tv.setDisplayTitle(UrlUtils.stripUrl(url));
577 }
John Reck541f55a2011-06-07 16:34:43 -0700578 tv.updateTabIcons();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700579 }
580 }
581
Michael Kolba2b2ba82010-08-04 17:54:03 -0700582 private boolean isLoading() {
Michael Kolb94827b62011-01-08 14:23:45 -0800583 TabView tv = mTabMap.get(mTabControl.getCurrentTab());
584 if (tv != null) {
585 return tv.mInLoad;
Michael Kolb8233fac2010-10-26 16:08:53 -0700586 } else {
587 return false;
588 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700589 }
590
Michael Kolba2b2ba82010-08-04 17:54:03 -0700591}