blob: b2c2af84fbee7b07a80e871ca8c5421b912c6b26 [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 Kolba2b2ba82010-08-04 17:54:03 -0700234 @Override
235 public void createContextMenu(ContextMenu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700236 MenuInflater inflater = mActivity.getMenuInflater();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700237 inflater.inflate(R.menu.title_context, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -0700238 mActivity.onCreateContextMenu(menu, this, null);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700239 }
240
Michael Kolb94827b62011-01-08 14:23:45 -0800241 private TabView buildTabView(Tab tab) {
242 TabView tabview = new TabView(mActivity, tab);
243 mTabMap.put(tab, tabview);
244 tabview.setOnClickListener(this);
Michael Kolb94827b62011-01-08 14:23:45 -0800245 return tabview;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700246 }
247
Romain Guyc8373212011-01-07 19:15:16 -0800248 private static Bitmap getDrawableAsBitmap(Drawable drawable, int width, int height) {
249 Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
250 Canvas c = new Canvas(b);
251 drawable.setBounds(0, 0, width, height);
252 drawable.draw(c);
253 return b;
Michael Kolbb4cafc52011-01-12 16:55:04 -0800254 }
255
Michael Kolba2b2ba82010-08-04 17:54:03 -0700256 /**
257 * View used in the tab bar
258 */
259 class TabView extends LinearLayout implements OnClickListener {
260
Michael Kolb94827b62011-01-08 14:23:45 -0800261 Tab mTab;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700262 View mTabContent;
263 TextView mTitle;
Michael Kolbae62fd42010-08-18 16:33:28 -0700264 View mIncognito;
John Reck541f55a2011-06-07 16:34:43 -0700265 View mSnapshot;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700266 ImageView mIconView;
267 ImageView mLock;
268 ImageView mClose;
269 boolean mSelected;
270 boolean mInLoad;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800271 Path mPath;
Michael Kolbeb7001c2011-02-16 16:07:33 -0800272 Path mFocusPath;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800273 int[] mWindowPos;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700274
275 /**
276 * @param context
277 */
Michael Kolb94827b62011-01-08 14:23:45 -0800278 public TabView(Context context, Tab tab) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700279 super(context);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800280 setWillNotDraw(false);
281 mPath = new Path();
Michael Kolbeb7001c2011-02-16 16:07:33 -0800282 mFocusPath = new Path();
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800283 mWindowPos = new int[2];
Michael Kolb94827b62011-01-08 14:23:45 -0800284 mTab = tab;
Michael Kolbed217742010-08-10 17:52:34 -0700285 setGravity(Gravity.CENTER_VERTICAL);
286 setOrientation(LinearLayout.HORIZONTAL);
Michael Kolb49f15832011-01-25 15:02:27 -0800287 setPadding(mTabOverlap, 0, mTabSliceWidth, 0);
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100288 LayoutInflater inflater = LayoutInflater.from(getContext());
Michael Kolbed217742010-08-10 17:52:34 -0700289 mTabContent = inflater.inflate(R.layout.tab_title, this, true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700290 mTitle = (TextView) mTabContent.findViewById(R.id.title);
291 mIconView = (ImageView) mTabContent.findViewById(R.id.favicon);
292 mLock = (ImageView) mTabContent.findViewById(R.id.lock);
293 mClose = (ImageView) mTabContent.findViewById(R.id.close);
294 mClose.setOnClickListener(this);
Michael Kolbae62fd42010-08-18 16:33:28 -0700295 mIncognito = mTabContent.findViewById(R.id.incognito);
John Reck541f55a2011-06-07 16:34:43 -0700296 mSnapshot = mTabContent.findViewById(R.id.snapshot);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700297 mSelected = false;
298 mInLoad = false;
299 // update the status
Michael Kolb94827b62011-01-08 14:23:45 -0800300 updateFromTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700301 }
302
303 @Override
304 public void onClick(View v) {
305 if (v == mClose) {
306 closeTab();
307 }
308 }
309
Michael Kolb94827b62011-01-08 14:23:45 -0800310 private void updateFromTab() {
311 String displayTitle = mTab.getTitle();
John Reck30c714c2010-12-16 17:30:34 -0800312 if (displayTitle == null) {
Michael Kolb94827b62011-01-08 14:23:45 -0800313 displayTitle = mTab.getUrl();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700314 }
John Reck30c714c2010-12-16 17:30:34 -0800315 setDisplayTitle(displayTitle);
Michael Kolb94827b62011-01-08 14:23:45 -0800316 setProgress(mTab.getLoadProgress());
317 if (mTab.getFavicon() != null) {
318 setFavicon(renderFavicon(mTab.getFavicon()));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700319 }
John Reck541f55a2011-06-07 16:34:43 -0700320 updateTabIcons();
321 }
322
323 private void updateTabIcons() {
324 mIncognito.setVisibility(
325 mTab.isPrivateBrowsingEnabled() ?
326 View.VISIBLE : View.GONE);
327 mSnapshot.setVisibility(mTab.isSnapshot()
328 ? View.VISIBLE : View.GONE);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700329 }
330
331 @Override
Michael Kolbb7b115e2010-09-25 16:59:37 -0700332 public void setActivated(boolean selected) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700333 mSelected = selected;
334 mClose.setVisibility(mSelected ? View.VISIBLE : View.GONE);
Michael Kolb8233fac2010-10-26 16:08:53 -0700335 mTitle.setTextAppearance(mActivity, mSelected ?
Michael Kolbc7485ae2010-09-03 10:10:58 -0700336 R.style.TabTitleSelected : R.style.TabTitleUnselected);
337 setHorizontalFadingEdgeEnabled(!mSelected);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700338 super.setActivated(selected);
Michael Kolb678afc82011-05-17 14:52:41 -0700339 updateLayoutParams();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800340 setFocusable(!selected);
341 postInvalidate();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700342 }
343
Michael Kolb678afc82011-05-17 14:52:41 -0700344 public void updateLayoutParams() {
345 LayoutParams lp = (LinearLayout.LayoutParams) getLayoutParams();
346 lp.width = mSelected ? mTabWidthSelected : mTabWidthUnselected;
347 lp.height = LayoutParams.MATCH_PARENT;
348 setLayoutParams(lp);
349 }
350
Michael Kolba2b2ba82010-08-04 17:54:03 -0700351 void setDisplayTitle(String title) {
352 mTitle.setText(title);
353 }
354
355 void setFavicon(Drawable d) {
356 mIconView.setImageDrawable(d);
357 }
358
359 void setLock(Drawable d) {
360 if (null == d) {
361 mLock.setVisibility(View.GONE);
362 } else {
363 mLock.setImageDrawable(d);
364 mLock.setVisibility(View.VISIBLE);
365 }
366 }
367
Michael Kolba2b2ba82010-08-04 17:54:03 -0700368 void setProgress(int newProgress) {
369 if (newProgress >= PROGRESS_MAX) {
370 mInLoad = false;
371 } else {
372 if (!mInLoad && getWindowToken() != null) {
373 mInLoad = true;
374 }
375 }
376 }
377
378 private void closeTab() {
Michael Kolb94827b62011-01-08 14:23:45 -0800379 if (mTab == mTabControl.getCurrentTab()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700380 mUiController.closeCurrentTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700381 } else {
Michael Kolb94827b62011-01-08 14:23:45 -0800382 mUiController.closeTab(mTab);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700383 }
384 }
385
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800386 @Override
387 protected void onLayout(boolean changed, int l, int t, int r, int b) {
388 super.onLayout(changed, l, t, r, b);
389 setTabPath(mPath, 0, 0, r - l, b - t);
Michael Kolbeb7001c2011-02-16 16:07:33 -0800390 setFocusPath(mFocusPath, 0, 0, r - l, b - t);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800391 }
Michael Kolbb4cafc52011-01-12 16:55:04 -0800392
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800393 @Override
394 protected void dispatchDraw(Canvas canvas) {
Romain Guyc8373212011-01-07 19:15:16 -0800395 if (mCurrentTextureWidth != mUi.getContentWidth() ||
396 mCurrentTextureHeight != getHeight()) {
397 mCurrentTextureWidth = mUi.getContentWidth();
398 mCurrentTextureHeight = getHeight();
399
400 if (mCurrentTextureWidth > 0 && mCurrentTextureHeight > 0) {
401 Bitmap activeTexture = getDrawableAsBitmap(mActiveDrawable,
402 mCurrentTextureWidth, mCurrentTextureHeight);
403 Bitmap inactiveTexture = getDrawableAsBitmap(mInactiveDrawable,
404 mCurrentTextureWidth, mCurrentTextureHeight);
405
406 mActiveShader = new BitmapShader(activeTexture,
407 Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
408 mActiveShaderPaint.setShader(mActiveShader);
Michael Kolbb4cafc52011-01-12 16:55:04 -0800409
Romain Guyc8373212011-01-07 19:15:16 -0800410 mInactiveShader = new BitmapShader(inactiveTexture,
411 Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
412 mInactiveShaderPaint.setShader(mInactiveShader);
413 }
414 }
Michael Kolb05902aa2011-02-22 17:43:38 -0800415 // add some monkey protection
416 if ((mActiveShader != null) && (mInactiveShader != null)) {
417 int state = canvas.save();
418 getLocationInWindow(mWindowPos);
419 Paint paint = mSelected ? mActiveShaderPaint : mInactiveShaderPaint;
420 drawClipped(canvas, paint, mPath, mWindowPos[0]);
421 canvas.restoreToCount(state);
422 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800423 super.dispatchDraw(canvas);
424 }
425
Romain Guyc8373212011-01-07 19:15:16 -0800426 private void drawClipped(Canvas canvas, Paint paint, Path clipPath, int left) {
427 // TODO: We should change the matrix/shader only when needed
428 final Matrix matrix = mSelected ? mActiveMatrix : mInactiveMatrix;
429 matrix.setTranslate(-left, 0.0f);
430 (mSelected ? mActiveShader : mInactiveShader).setLocalMatrix(matrix);
431 canvas.drawPath(clipPath, paint);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800432 if (isFocused()) {
Michael Kolbeb7001c2011-02-16 16:07:33 -0800433 canvas.drawPath(mFocusPath, mFocusPaint);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800434 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800435 }
436
437 private void setTabPath(Path path, int l, int t, int r, int b) {
438 path.reset();
439 path.moveTo(l, b);
440 path.lineTo(l, t);
441 path.lineTo(r - mTabSliceWidth, t);
442 path.lineTo(r, b);
443 path.close();
444 }
445
Michael Kolbeb7001c2011-02-16 16:07:33 -0800446 private void setFocusPath(Path path, int l, int t, int r, int b) {
447 path.reset();
448 path.moveTo(l, b);
449 path.lineTo(l, t);
450 path.lineTo(r - mTabSliceWidth, t);
451 path.lineTo(r, b);
452 }
453
Michael Kolba2b2ba82010-08-04 17:54:03 -0700454 }
455
Michael Kolb49f15832011-01-25 15:02:27 -0800456 static Drawable createFaviconBackground(Context context) {
457 PaintDrawable faviconBackground = new PaintDrawable();
458 Resources res = context.getResources();
459 faviconBackground.getPaint().setColor(context.getResources()
460 .getColor(R.color.tabFaviconBackground));
461 faviconBackground.setCornerRadius(
462 res.getDimension(R.dimen.tab_favicon_corner_radius));
463 return faviconBackground;
464 }
465
Michael Kolb94827b62011-01-08 14:23:45 -0800466 private Drawable renderFavicon(Bitmap icon) {
Michael Kolb49f15832011-01-25 15:02:27 -0800467 Drawable[] array = new Drawable[2];
468 array[0] = createFaviconBackground(getContext());
Michael Kolb94827b62011-01-08 14:23:45 -0800469 if (icon == null) {
Michael Kolb49f15832011-01-25 15:02:27 -0800470 array[1] = mGenericFavicon;
Michael Kolb94827b62011-01-08 14:23:45 -0800471 } else {
Michael Kolb49f15832011-01-25 15:02:27 -0800472 array[1] = new BitmapDrawable(icon);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700473 }
Michael Kolb94827b62011-01-08 14:23:45 -0800474 LayerDrawable d = new LayerDrawable(array);
Michael Kolb49f15832011-01-25 15:02:27 -0800475 d.setLayerInset(1, 2, 2, 2, 2);
Michael Kolb94827b62011-01-08 14:23:45 -0800476 return d;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700477 }
478
Michael Kolb2d59c322011-01-25 13:18:55 -0800479 private void animateTabOut(final Tab tab, final TabView tv) {
480 ObjectAnimator scalex = ObjectAnimator.ofFloat(tv, "scaleX", 1.0f, 0.0f);
481 ObjectAnimator scaley = ObjectAnimator.ofFloat(tv, "scaleY", 1.0f, 0.0f);
Michael Kolb49f15832011-01-25 15:02:27 -0800482 ObjectAnimator alpha = ObjectAnimator.ofFloat(tv, "alpha", 1.0f, 0.0f);
Michael Kolb2d59c322011-01-25 13:18:55 -0800483 AnimatorSet animator = new AnimatorSet();
Michael Kolb49f15832011-01-25 15:02:27 -0800484 animator.playTogether(scalex, scaley, alpha);
Michael Kolb2d59c322011-01-25 13:18:55 -0800485 animator.setDuration(150);
486 animator.addListener(new AnimatorListener() {
487
488 @Override
489 public void onAnimationCancel(Animator animation) {
490 }
491
492 @Override
493 public void onAnimationEnd(Animator animation) {
494 mTabs.removeTab(tv);
495 mTabMap.remove(tab);
Michael Kolb8814d732011-01-26 11:22:30 -0800496 mUi.onRemoveTabCompleted(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800497 }
498
499 @Override
500 public void onAnimationRepeat(Animator animation) {
501 }
502
503 @Override
504 public void onAnimationStart(Animator animation) {
505 }
506
507 });
508 animator.start();
509 }
510
511 private void animateTabIn(final Tab tab, final TabView tv) {
Michael Kolb49f15832011-01-25 15:02:27 -0800512 ObjectAnimator scalex = ObjectAnimator.ofFloat(tv, "scaleX", 0.0f, 1.0f);
Michael Kolb2d59c322011-01-25 13:18:55 -0800513 scalex.setDuration(150);
514 scalex.addListener(new AnimatorListener() {
515
516 @Override
517 public void onAnimationCancel(Animator animation) {
518 }
519
520 @Override
521 public void onAnimationEnd(Animator animation) {
Michael Kolb8814d732011-01-26 11:22:30 -0800522 mUi.onAddTabCompleted(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800523 }
524
525 @Override
526 public void onAnimationRepeat(Animator animation) {
527 }
528
529 @Override
530 public void onAnimationStart(Animator animation) {
531 mTabs.addTab(tv);
532 }
533
534 });
535 scalex.start();
536 }
537
Michael Kolba2b2ba82010-08-04 17:54:03 -0700538 // TabChangeListener implementation
539
Michael Kolb8233fac2010-10-26 16:08:53 -0700540 public void onSetActiveTab(Tab tab) {
Michael Kolbc831b632011-05-11 09:30:34 -0700541 mTabs.setSelectedTab(mTabControl.getTabPosition(tab));
Michael Kolb94827b62011-01-08 14:23:45 -0800542 TabView tv = mTabMap.get(tab);
543 if (tv != null) {
544 tv.setProgress(tv.mTab.getLoadProgress());
Michael Kolba2b2ba82010-08-04 17:54:03 -0700545 }
546 }
547
Michael Kolba2b2ba82010-08-04 17:54:03 -0700548 public void onFavicon(Tab tab, Bitmap favicon) {
Michael Kolb94827b62011-01-08 14:23:45 -0800549 TabView tv = mTabMap.get(tab);
550 if (tv != null) {
551 tv.setFavicon(renderFavicon(favicon));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700552 }
553 }
554
Michael Kolba2b2ba82010-08-04 17:54:03 -0700555 public void onNewTab(Tab tab) {
Michael Kolb94827b62011-01-08 14:23:45 -0800556 TabView tv = buildTabView(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800557 animateTabIn(tab, tv);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700558 }
559
Michael Kolba2b2ba82010-08-04 17:54:03 -0700560 public void onProgress(Tab tab, int progress) {
Michael Kolb94827b62011-01-08 14:23:45 -0800561 TabView tv = mTabMap.get(tab);
562 if (tv != null) {
563 tv.setProgress(progress);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700564 }
565 }
566
Michael Kolba2b2ba82010-08-04 17:54:03 -0700567 public void onRemoveTab(Tab tab) {
Michael Kolb94827b62011-01-08 14:23:45 -0800568 TabView tv = mTabMap.get(tab);
569 if (tv != null) {
Michael Kolb2d59c322011-01-25 13:18:55 -0800570 animateTabOut(tab, tv);
571 } else {
572 mTabMap.remove(tab);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700573 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700574 }
575
Michael Kolba2b2ba82010-08-04 17:54:03 -0700576 public void onUrlAndTitle(Tab tab, String url, String title) {
Michael Kolb94827b62011-01-08 14:23:45 -0800577 TabView tv = mTabMap.get(tab);
578 if (tv != null) {
579 if (title != null) {
580 tv.setDisplayTitle(title);
581 } else if (url != null) {
582 tv.setDisplayTitle(UrlUtils.stripUrl(url));
583 }
John Reck541f55a2011-06-07 16:34:43 -0700584 tv.updateTabIcons();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700585 }
586 }
587
Michael Kolba2b2ba82010-08-04 17:54:03 -0700588 private boolean isLoading() {
Michael Kolb94827b62011-01-08 14:23:45 -0800589 TabView tv = mTabMap.get(mTabControl.getCurrentTab());
590 if (tv != null) {
591 return tv.mInLoad;
Michael Kolb8233fac2010-10-26 16:08:53 -0700592 } else {
593 return false;
594 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700595 }
596
Michael Kolba2b2ba82010-08-04 17:54:03 -0700597}