blob: 990d68ffc1637a18155370ffc44423f35c9be608 [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 Kolbebba8b42010-09-30 12:57:59 -070019import com.android.browser.ScrollWebView.ScrollListener;
Michael Kolbebba8b42010-09-30 12:57:59 -070020
Michael Kolb2d59c322011-01-25 13:18:55 -080021import android.animation.Animator;
22import android.animation.Animator.AnimatorListener;
23import android.animation.AnimatorSet;
24import android.animation.ObjectAnimator;
Michael Kolb8233fac2010-10-26 16:08:53 -070025import android.app.Activity;
Michael Kolba2b2ba82010-08-04 17:54:03 -070026import android.content.Context;
27import android.content.res.Resources;
28import android.graphics.Bitmap;
Michael Kolb2b5a13a2010-12-09 14:13:42 -080029import android.graphics.BitmapShader;
30import android.graphics.Canvas;
Michael Kolb467af0a2011-01-11 13:09:49 -080031import android.graphics.Matrix;
Michael Kolb2b5a13a2010-12-09 14:13:42 -080032import android.graphics.Paint;
33import android.graphics.Path;
34import android.graphics.Shader;
Michael Kolba2b2ba82010-08-04 17:54:03 -070035import android.graphics.drawable.BitmapDrawable;
36import android.graphics.drawable.Drawable;
37import android.graphics.drawable.LayerDrawable;
38import android.graphics.drawable.PaintDrawable;
39import android.view.ContextMenu;
Michael Kolbed217742010-08-10 17:52:34 -070040import android.view.Gravity;
Michael Kolba2b2ba82010-08-04 17:54:03 -070041import android.view.LayoutInflater;
42import android.view.MenuInflater;
43import android.view.View;
44import android.view.View.OnClickListener;
45import android.webkit.WebView;
Michael Kolbed217742010-08-10 17:52:34 -070046import android.widget.ImageButton;
Michael Kolba2b2ba82010-08-04 17:54:03 -070047import android.widget.ImageView;
48import android.widget.LinearLayout;
49import android.widget.TextView;
50
Michael Kolba2b2ba82010-08-04 17:54:03 -070051import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -080052import java.util.List;
Michael Kolba2b2ba82010-08-04 17:54:03 -070053import java.util.Map;
54
55/**
56 * tabbed title bar for xlarge screen browser
57 */
58public class TabBar extends LinearLayout
Michael Kolb8233fac2010-10-26 16:08:53 -070059 implements ScrollListener, OnClickListener {
Michael Kolba2b2ba82010-08-04 17:54:03 -070060
61 private static final int PROGRESS_MAX = 100;
62
Michael Kolb8233fac2010-10-26 16:08:53 -070063 private Activity mActivity;
64 private UiController mUiController;
65 private TabControl mTabControl;
Michael Kolb66706532010-12-12 19:50:22 -080066 private XLargeUi mUi;
Michael Kolba2b2ba82010-08-04 17:54:03 -070067
Michael Kolbed217742010-08-10 17:52:34 -070068 private final int mTabWidthSelected;
69 private final int mTabWidthUnselected;
70
Michael Kolba2b2ba82010-08-04 17:54:03 -070071 private TabScrollView mTabs;
Michael Kolb8233fac2010-10-26 16:08:53 -070072
Michael Kolbebba8b42010-09-30 12:57:59 -070073 private ImageButton mNewTab;
74 private int mButtonWidth;
Michael Kolba2b2ba82010-08-04 17:54:03 -070075
Michael Kolb94827b62011-01-08 14:23:45 -080076 private Map<Tab, TabView> mTabMap;
Michael Kolba2b2ba82010-08-04 17:54:03 -070077
John Reckaff60fb2010-10-25 13:47:58 -070078 private Drawable mGenericFavicon;
79
Romain Guyc8373212011-01-07 19:15:16 -080080 private int mCurrentTextureWidth = 0;
81 private int mCurrentTextureHeight = 0;
82
Michael Kolb2b5a13a2010-12-09 14:13:42 -080083 private Drawable mActiveDrawable;
84 private Drawable mInactiveDrawable;
85
Romain Guyc8373212011-01-07 19:15:16 -080086 private final Paint mActiveShaderPaint = new Paint();
87 private final Paint mInactiveShaderPaint = new Paint();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080088 private final Paint mFocusPaint = new Paint();
Romain Guyc8373212011-01-07 19:15:16 -080089 private final Matrix mActiveMatrix = new Matrix();
90 private final Matrix mInactiveMatrix = new Matrix();
91
92 private BitmapShader mActiveShader;
93 private BitmapShader mInactiveShader;
94
Michael Kolb2b5a13a2010-12-09 14:13:42 -080095 private int mTabOverlap;
Michael Kolb5a72f182011-01-13 20:35:06 -080096 private int mAddTabOverlap;
Michael Kolb2b5a13a2010-12-09 14:13:42 -080097 private int mTabSliceWidth;
Michael Kolb376b5412010-12-15 11:52:57 -080098 private boolean mUseQuickControls;
Michael Kolb2b5a13a2010-12-09 14:13:42 -080099
Michael Kolb66706532010-12-12 19:50:22 -0800100 public TabBar(Activity activity, UiController controller, XLargeUi ui) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700101 super(activity);
102 mActivity = activity;
103 mUiController = controller;
104 mTabControl = mUiController.getTabControl();
105 mUi = ui;
106 Resources res = activity.getResources();
Michael Kolbed217742010-08-10 17:52:34 -0700107 mTabWidthSelected = (int) res.getDimension(R.dimen.tab_width_selected);
108 mTabWidthUnselected = (int) res.getDimension(R.dimen.tab_width_unselected);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800109 mActiveDrawable = res.getDrawable(R.drawable.bg_urlbar);
110 mInactiveDrawable = res.getDrawable(R.drawable.browsertab_inactive);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700111
Michael Kolb94827b62011-01-08 14:23:45 -0800112 mTabMap = new HashMap<Tab, TabView>();
113 Resources resources = activity.getResources();
Michael Kolb8233fac2010-10-26 16:08:53 -0700114 LayoutInflater factory = LayoutInflater.from(activity);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700115 factory.inflate(R.layout.tab_bar, this);
Michael Kolbf558f0d2011-01-13 19:24:18 -0800116 setPadding(0, (int) res.getDimension(R.dimen.tab_padding_top), 0, 0);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700117 mTabs = (TabScrollView) findViewById(R.id.tabs);
Michael Kolbebba8b42010-09-30 12:57:59 -0700118 mNewTab = (ImageButton) findViewById(R.id.newtab);
119 mNewTab.setOnClickListener(this);
John Reckaff60fb2010-10-25 13:47:58 -0700120 mGenericFavicon = res.getDrawable(R.drawable.app_web_browser_sm);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700121
Michael Kolb1bf23132010-11-19 12:55:12 -0800122 updateTabs(mUiController.getTabs());
Michael Kolbebba8b42010-09-30 12:57:59 -0700123 mButtonWidth = -1;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800124 // tab dimensions
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800125 mTabOverlap = (int) res.getDimension(R.dimen.tab_overlap);
Michael Kolb5a72f182011-01-13 20:35:06 -0800126 mAddTabOverlap = (int) res.getDimension(R.dimen.tab_addoverlap);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800127 mTabSliceWidth = (int) res.getDimension(R.dimen.tab_slice);
Romain Guyc8373212011-01-07 19:15:16 -0800128
129 mActiveShaderPaint.setStyle(Paint.Style.FILL);
130 mActiveShaderPaint.setAntiAlias(true);
131
132 mInactiveShaderPaint.setStyle(Paint.Style.FILL);
133 mInactiveShaderPaint.setAntiAlias(true);
Michael Kolbb4cafc52011-01-12 16:55:04 -0800134
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800135 mFocusPaint.setStyle(Paint.Style.STROKE);
136 mFocusPaint.setAntiAlias(true);
137 mFocusPaint.setColor(res.getColor(R.color.tabFocusHighlight));
Michael Kolbebba8b42010-09-30 12:57:59 -0700138 }
139
Michael Kolb376b5412010-12-15 11:52:57 -0800140 void setUseQuickControls(boolean useQuickControls) {
141 mUseQuickControls = useQuickControls;
Michael Kolb467af0a2011-01-11 13:09:49 -0800142 mNewTab.setVisibility(mUseQuickControls ? View.GONE
143 : View.VISIBLE);
Michael Kolb376b5412010-12-15 11:52:57 -0800144 }
145
146 int getTabCount() {
147 return mTabMap.size();
148 }
149
Michael Kolb1bf23132010-11-19 12:55:12 -0800150 void updateTabs(List<Tab> tabs) {
151 mTabs.clearTabs();
152 mTabMap.clear();
153 for (Tab tab : tabs) {
Michael Kolb94827b62011-01-08 14:23:45 -0800154 TabView tv = buildTabView(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800155 mTabs.addTab(tv);
Michael Kolb1bf23132010-11-19 12:55:12 -0800156 }
157 mTabs.setSelectedTab(mTabControl.getCurrentIndex());
158 }
159
Michael Kolbebba8b42010-09-30 12:57:59 -0700160 @Override
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800161 protected void onMeasure(int hspec, int vspec) {
162 super.onMeasure(hspec, vspec);
163 int w = getMeasuredWidth();
164 // adjust for new tab overlap
Michael Kolb467af0a2011-01-11 13:09:49 -0800165 if (!mUseQuickControls) {
Michael Kolb5a72f182011-01-13 20:35:06 -0800166 w -= mAddTabOverlap;
Michael Kolb467af0a2011-01-11 13:09:49 -0800167 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800168 setMeasuredDimension(w, getMeasuredHeight());
169 }
170
171 @Override
Michael Kolbebba8b42010-09-30 12:57:59 -0700172 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800173 // use paddingLeft and paddingTop
174 int pl = getPaddingLeft();
175 int pt = getPaddingTop();
Michael Kolbebba8b42010-09-30 12:57:59 -0700176 int sw = mTabs.getMeasuredWidth();
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800177 int w = right - left - pl;
Michael Kolbb4cafc52011-01-12 16:55:04 -0800178 if (mUseQuickControls) {
179 mButtonWidth = 0;
180 } else {
Michael Kolb5a72f182011-01-13 20:35:06 -0800181 mButtonWidth = mNewTab.getMeasuredWidth() - mAddTabOverlap;
Michael Kolbb4cafc52011-01-12 16:55:04 -0800182 if (w-sw < mButtonWidth) {
183 sw = w - mButtonWidth;
184 }
Michael Kolbebba8b42010-09-30 12:57:59 -0700185 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800186 mTabs.layout(pl, pt, pl + sw, bottom - top);
187 // adjust for overlap
Michael Kolbb4cafc52011-01-12 16:55:04 -0800188 if (!mUseQuickControls) {
Michael Kolb5a72f182011-01-13 20:35:06 -0800189 mNewTab.layout(pl + sw - mAddTabOverlap, pt,
190 pl + sw + mButtonWidth - mAddTabOverlap, bottom - top);
Michael Kolbb4cafc52011-01-12 16:55:04 -0800191 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700192 }
193
194 public void onClick(View view) {
Michael Kolbebba8b42010-09-30 12:57:59 -0700195 if (mNewTab == view) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700196 mUiController.openTabToHomePage();
Michael Kolbebba8b42010-09-30 12:57:59 -0700197 } else if (mTabs.getSelectedTab() == view) {
Michael Kolb467af0a2011-01-11 13:09:49 -0800198 if (mUseQuickControls) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800199 if (mUi.isTitleBarShowing() && !isLoading()) {
200 mUi.stopEditingUrl();
201 mUi.hideTitleBar();
Michael Kolb467af0a2011-01-11 13:09:49 -0800202 } else {
203 mUi.stopWebViewScrolling();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800204 mUi.showTitleBarAndEdit();
Michael Kolb467af0a2011-01-11 13:09:49 -0800205 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800206 } else if (mUi.isTitleBarShowing() && !isLoading()) {
207 mUi.stopEditingUrl();
208 mUi.hideTitleBar();
Michael Kolbed217742010-08-10 17:52:34 -0700209 } else {
210 showUrlBar();
211 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700212 } else {
Michael Kolbebba8b42010-09-30 12:57:59 -0700213 int ix = mTabs.getChildIndex(view);
214 if (ix >= 0) {
215 mTabs.setSelectedTab(ix);
Michael Kolb8233fac2010-10-26 16:08:53 -0700216 mUiController.switchToTab(ix);
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 Kolb376b5412010-12-15 11:52:57 -0800226 void showTitleBarIndicator(boolean show) {
Michael Kolba0e2a752010-12-12 15:27:56 -0800227 Tab tab = mTabControl.getCurrentTab();
228 if (tab != null) {
Michael Kolb94827b62011-01-08 14:23:45 -0800229 TabView tv = mTabMap.get(tab);
230 if (tv != null) {
231 tv.showIndicator(show);
Michael Kolba0e2a752010-12-12 15:27:56 -0800232 }
233 }
Michael Kolbed217742010-08-10 17:52:34 -0700234 }
235
Michael Kolb7dad8f02011-01-21 11:06:18 -0800236 boolean showsTitleBarIndicator() {
237 Tab tab = mTabControl.getCurrentTab();
238 if (tab != null) {
239 TabView tv = mTabMap.get(tab);
240 if (tv != null) {
241 return tv.showsIndicator();
242 }
243 }
244 return false;
245 }
246
Michael Kolbed217742010-08-10 17:52:34 -0700247 // callback after fake titlebar is shown
248 void onShowTitleBar() {
Michael Kolba0e2a752010-12-12 15:27:56 -0800249 showTitleBarIndicator(false);
Michael Kolbed217742010-08-10 17:52:34 -0700250 }
251
252 // callback after fake titlebar is hidden
Michael Kolba2b2ba82010-08-04 17:54:03 -0700253 void onHideTitleBar() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700254 Tab tab = mTabControl.getCurrentTab();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800255 WebView w = tab.getWebView();
256 if (w != null) {
257 showTitleBarIndicator(w.getVisibleTitleHeight() == 0);
258 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700259 }
260
Michael Kolbed217742010-08-10 17:52:34 -0700261 // webview scroll listener
262
263 @Override
Michael Kolba0e2a752010-12-12 15:27:56 -0800264 public void onScroll(int visibleTitleHeight) {
Michael Kolb376b5412010-12-15 11:52:57 -0800265 if (mUseQuickControls) return;
Michael Kolb8233fac2010-10-26 16:08:53 -0700266 // isLoading is using the current tab, which initially might not be set yet
John Reckdcf911c2010-12-22 16:32:31 -0800267 if (mTabControl.getCurrentTab() != null
268 && !isLoading()) {
269 if (visibleTitleHeight == 0) {
Michael Kolb7dad8f02011-01-21 11:06:18 -0800270 if (!showsTitleBarIndicator()) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800271 mUi.hideTitleBar();
Michael Kolb7dad8f02011-01-21 11:06:18 -0800272 showTitleBarIndicator(true);
273 }
John Reckdcf911c2010-12-22 16:32:31 -0800274 } else {
Michael Kolb7dad8f02011-01-21 11:06:18 -0800275 if (showsTitleBarIndicator()) {
276 showTitleBarIndicator(false);
277 }
Michael Kolbed217742010-08-10 17:52:34 -0700278 }
279 }
280 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700281
282 @Override
283 public void createContextMenu(ContextMenu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700284 MenuInflater inflater = mActivity.getMenuInflater();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700285 inflater.inflate(R.menu.title_context, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -0700286 mActivity.onCreateContextMenu(menu, this, null);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700287 }
288
Michael Kolb94827b62011-01-08 14:23:45 -0800289 private TabView buildTabView(Tab tab) {
290 TabView tabview = new TabView(mActivity, tab);
291 mTabMap.put(tab, tabview);
292 tabview.setOnClickListener(this);
Michael Kolb94827b62011-01-08 14:23:45 -0800293 return tabview;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700294 }
295
Romain Guyc8373212011-01-07 19:15:16 -0800296 private static Bitmap getDrawableAsBitmap(Drawable drawable, int width, int height) {
297 Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
298 Canvas c = new Canvas(b);
299 drawable.setBounds(0, 0, width, height);
300 drawable.draw(c);
301 return b;
Michael Kolbb4cafc52011-01-12 16:55:04 -0800302 }
303
Michael Kolba2b2ba82010-08-04 17:54:03 -0700304 /**
305 * View used in the tab bar
306 */
307 class TabView extends LinearLayout implements OnClickListener {
308
Michael Kolb94827b62011-01-08 14:23:45 -0800309 Tab mTab;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700310 View mTabContent;
311 TextView mTitle;
Michael Kolba0e2a752010-12-12 15:27:56 -0800312 View mIndicator;
Michael Kolbae62fd42010-08-18 16:33:28 -0700313 View mIncognito;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700314 ImageView mIconView;
315 ImageView mLock;
316 ImageView mClose;
317 boolean mSelected;
318 boolean mInLoad;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800319 Path mPath;
320 int[] mWindowPos;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700321
322 /**
323 * @param context
324 */
Michael Kolb94827b62011-01-08 14:23:45 -0800325 public TabView(Context context, Tab tab) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700326 super(context);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800327 setWillNotDraw(false);
328 mPath = new Path();
329 mWindowPos = new int[2];
Michael Kolb94827b62011-01-08 14:23:45 -0800330 mTab = tab;
Michael Kolbed217742010-08-10 17:52:34 -0700331 setGravity(Gravity.CENTER_VERTICAL);
332 setOrientation(LinearLayout.HORIZONTAL);
Michael Kolb49f15832011-01-25 15:02:27 -0800333 setPadding(mTabOverlap, 0, mTabSliceWidth, 0);
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100334 LayoutInflater inflater = LayoutInflater.from(getContext());
Michael Kolbed217742010-08-10 17:52:34 -0700335 mTabContent = inflater.inflate(R.layout.tab_title, this, true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700336 mTitle = (TextView) mTabContent.findViewById(R.id.title);
337 mIconView = (ImageView) mTabContent.findViewById(R.id.favicon);
338 mLock = (ImageView) mTabContent.findViewById(R.id.lock);
339 mClose = (ImageView) mTabContent.findViewById(R.id.close);
340 mClose.setOnClickListener(this);
Michael Kolbae62fd42010-08-18 16:33:28 -0700341 mIncognito = mTabContent.findViewById(R.id.incognito);
Michael Kolba0e2a752010-12-12 15:27:56 -0800342 mIndicator = mTabContent.findViewById(R.id.chevron);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700343 mSelected = false;
344 mInLoad = false;
345 // update the status
Michael Kolb94827b62011-01-08 14:23:45 -0800346 updateFromTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700347 }
348
Michael Kolba0e2a752010-12-12 15:27:56 -0800349 void showIndicator(boolean show) {
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800350 if (mSelected) {
351 mIndicator.setVisibility(show ? View.VISIBLE : View.GONE);
Michael Kolb49f15832011-01-25 15:02:27 -0800352 LayoutParams lp = (LinearLayout.LayoutParams) getLayoutParams();
353 if (show) {
354 lp.width = mTabWidthSelected + mIndicator.getWidth();
355 } else {
356 lp.width = mTabWidthSelected;
357 }
358 lp.height = LayoutParams.MATCH_PARENT;
359 setLayoutParams(lp);
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800360 } else {
361 mIndicator.setVisibility(View.GONE);
362 }
Michael Kolba0e2a752010-12-12 15:27:56 -0800363 }
364
Michael Kolb7dad8f02011-01-21 11:06:18 -0800365 boolean showsIndicator() {
366 return (mIndicator.getVisibility() == View.VISIBLE);
367 }
368
Michael Kolba2b2ba82010-08-04 17:54:03 -0700369 @Override
370 public void onClick(View v) {
371 if (v == mClose) {
372 closeTab();
373 }
374 }
375
Michael Kolb94827b62011-01-08 14:23:45 -0800376 private void updateFromTab() {
377 String displayTitle = mTab.getTitle();
John Reck30c714c2010-12-16 17:30:34 -0800378 if (displayTitle == null) {
Michael Kolb94827b62011-01-08 14:23:45 -0800379 displayTitle = mTab.getUrl();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700380 }
John Reck30c714c2010-12-16 17:30:34 -0800381 setDisplayTitle(displayTitle);
Michael Kolb94827b62011-01-08 14:23:45 -0800382 setProgress(mTab.getLoadProgress());
383 if (mTab.getFavicon() != null) {
384 setFavicon(renderFavicon(mTab.getFavicon()));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700385 }
Michael Kolb94827b62011-01-08 14:23:45 -0800386 if (mTab != null) {
Michael Kolbae62fd42010-08-18 16:33:28 -0700387 mIncognito.setVisibility(
Michael Kolb94827b62011-01-08 14:23:45 -0800388 mTab.isPrivateBrowsingEnabled() ?
Michael Kolbae62fd42010-08-18 16:33:28 -0700389 View.VISIBLE : View.GONE);
390 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700391 }
392
393 @Override
Michael Kolbb7b115e2010-09-25 16:59:37 -0700394 public void setActivated(boolean selected) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700395 mSelected = selected;
396 mClose.setVisibility(mSelected ? View.VISIBLE : View.GONE);
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800397 mIndicator.setVisibility(View.GONE);
Michael Kolb8233fac2010-10-26 16:08:53 -0700398 mTitle.setTextAppearance(mActivity, mSelected ?
Michael Kolbc7485ae2010-09-03 10:10:58 -0700399 R.style.TabTitleSelected : R.style.TabTitleUnselected);
400 setHorizontalFadingEdgeEnabled(!mSelected);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700401 super.setActivated(selected);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800402 LayoutParams lp = (LinearLayout.LayoutParams) getLayoutParams();
403 lp.width = selected ? mTabWidthSelected : mTabWidthUnselected;
404 lp.height = LayoutParams.MATCH_PARENT;
405 setLayoutParams(lp);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800406 setFocusable(!selected);
407 postInvalidate();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700408 }
409
410 void setDisplayTitle(String title) {
411 mTitle.setText(title);
412 }
413
414 void setFavicon(Drawable d) {
415 mIconView.setImageDrawable(d);
416 }
417
418 void setLock(Drawable d) {
419 if (null == d) {
420 mLock.setVisibility(View.GONE);
421 } else {
422 mLock.setImageDrawable(d);
423 mLock.setVisibility(View.VISIBLE);
424 }
425 }
426
Michael Kolba2b2ba82010-08-04 17:54:03 -0700427 void setProgress(int newProgress) {
428 if (newProgress >= PROGRESS_MAX) {
429 mInLoad = false;
430 } else {
431 if (!mInLoad && getWindowToken() != null) {
432 mInLoad = true;
433 }
434 }
435 }
436
437 private void closeTab() {
Michael Kolb94827b62011-01-08 14:23:45 -0800438 if (mTab == mTabControl.getCurrentTab()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700439 mUiController.closeCurrentTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700440 } else {
Michael Kolb94827b62011-01-08 14:23:45 -0800441 mUiController.closeTab(mTab);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700442 }
443 }
444
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800445 @Override
446 protected void onLayout(boolean changed, int l, int t, int r, int b) {
447 super.onLayout(changed, l, t, r, b);
448 setTabPath(mPath, 0, 0, r - l, b - t);
449 }
Michael Kolbb4cafc52011-01-12 16:55:04 -0800450
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800451 @Override
452 protected void dispatchDraw(Canvas canvas) {
Romain Guyc8373212011-01-07 19:15:16 -0800453 if (mCurrentTextureWidth != mUi.getContentWidth() ||
454 mCurrentTextureHeight != getHeight()) {
455 mCurrentTextureWidth = mUi.getContentWidth();
456 mCurrentTextureHeight = getHeight();
457
458 if (mCurrentTextureWidth > 0 && mCurrentTextureHeight > 0) {
459 Bitmap activeTexture = getDrawableAsBitmap(mActiveDrawable,
460 mCurrentTextureWidth, mCurrentTextureHeight);
461 Bitmap inactiveTexture = getDrawableAsBitmap(mInactiveDrawable,
462 mCurrentTextureWidth, mCurrentTextureHeight);
463
464 mActiveShader = new BitmapShader(activeTexture,
465 Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
466 mActiveShaderPaint.setShader(mActiveShader);
Michael Kolbb4cafc52011-01-12 16:55:04 -0800467
Romain Guyc8373212011-01-07 19:15:16 -0800468 mInactiveShader = new BitmapShader(inactiveTexture,
469 Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
470 mInactiveShaderPaint.setShader(mInactiveShader);
471 }
472 }
Michael Kolbb4cafc52011-01-12 16:55:04 -0800473
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800474 int state = canvas.save();
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800475 getLocationInWindow(mWindowPos);
Romain Guyc8373212011-01-07 19:15:16 -0800476 Paint paint = mSelected ? mActiveShaderPaint : mInactiveShaderPaint;
477 drawClipped(canvas, paint, mPath, mWindowPos[0]);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800478 canvas.restoreToCount(state);
479 super.dispatchDraw(canvas);
480 }
481
Romain Guyc8373212011-01-07 19:15:16 -0800482 private void drawClipped(Canvas canvas, Paint paint, Path clipPath, int left) {
483 // TODO: We should change the matrix/shader only when needed
484 final Matrix matrix = mSelected ? mActiveMatrix : mInactiveMatrix;
485 matrix.setTranslate(-left, 0.0f);
486 (mSelected ? mActiveShader : mInactiveShader).setLocalMatrix(matrix);
487 canvas.drawPath(clipPath, paint);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800488 if (isFocused()) {
489 canvas.drawPath(clipPath, mFocusPaint);
490 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800491 }
492
493 private void setTabPath(Path path, int l, int t, int r, int b) {
494 path.reset();
495 path.moveTo(l, b);
496 path.lineTo(l, t);
497 path.lineTo(r - mTabSliceWidth, t);
498 path.lineTo(r, b);
499 path.close();
500 }
501
Michael Kolba2b2ba82010-08-04 17:54:03 -0700502 }
503
Michael Kolb49f15832011-01-25 15:02:27 -0800504 static Drawable createFaviconBackground(Context context) {
505 PaintDrawable faviconBackground = new PaintDrawable();
506 Resources res = context.getResources();
507 faviconBackground.getPaint().setColor(context.getResources()
508 .getColor(R.color.tabFaviconBackground));
509 faviconBackground.setCornerRadius(
510 res.getDimension(R.dimen.tab_favicon_corner_radius));
511 return faviconBackground;
512 }
513
Michael Kolb94827b62011-01-08 14:23:45 -0800514 private Drawable renderFavicon(Bitmap icon) {
Michael Kolb49f15832011-01-25 15:02:27 -0800515 Drawable[] array = new Drawable[2];
516 array[0] = createFaviconBackground(getContext());
Michael Kolb94827b62011-01-08 14:23:45 -0800517 if (icon == null) {
Michael Kolb49f15832011-01-25 15:02:27 -0800518 array[1] = mGenericFavicon;
Michael Kolb94827b62011-01-08 14:23:45 -0800519 } else {
Michael Kolb49f15832011-01-25 15:02:27 -0800520 array[1] = new BitmapDrawable(icon);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700521 }
Michael Kolb94827b62011-01-08 14:23:45 -0800522 LayerDrawable d = new LayerDrawable(array);
Michael Kolb49f15832011-01-25 15:02:27 -0800523 d.setLayerInset(1, 2, 2, 2, 2);
Michael Kolb94827b62011-01-08 14:23:45 -0800524 return d;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700525 }
526
Michael Kolb2d59c322011-01-25 13:18:55 -0800527 private void animateTabOut(final Tab tab, final TabView tv) {
528 ObjectAnimator scalex = ObjectAnimator.ofFloat(tv, "scaleX", 1.0f, 0.0f);
529 ObjectAnimator scaley = ObjectAnimator.ofFloat(tv, "scaleY", 1.0f, 0.0f);
Michael Kolb49f15832011-01-25 15:02:27 -0800530 ObjectAnimator alpha = ObjectAnimator.ofFloat(tv, "alpha", 1.0f, 0.0f);
Michael Kolb2d59c322011-01-25 13:18:55 -0800531 AnimatorSet animator = new AnimatorSet();
Michael Kolb49f15832011-01-25 15:02:27 -0800532 animator.playTogether(scalex, scaley, alpha);
Michael Kolb2d59c322011-01-25 13:18:55 -0800533 animator.setDuration(150);
534 animator.addListener(new AnimatorListener() {
535
536 @Override
537 public void onAnimationCancel(Animator animation) {
538 }
539
540 @Override
541 public void onAnimationEnd(Animator animation) {
542 mTabs.removeTab(tv);
543 mTabMap.remove(tab);
Michael Kolb8814d732011-01-26 11:22:30 -0800544 mUi.onRemoveTabCompleted(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800545 }
546
547 @Override
548 public void onAnimationRepeat(Animator animation) {
549 }
550
551 @Override
552 public void onAnimationStart(Animator animation) {
553 }
554
555 });
556 animator.start();
557 }
558
559 private void animateTabIn(final Tab tab, final TabView tv) {
Michael Kolb49f15832011-01-25 15:02:27 -0800560 ObjectAnimator scalex = ObjectAnimator.ofFloat(tv, "scaleX", 0.0f, 1.0f);
Michael Kolb2d59c322011-01-25 13:18:55 -0800561 scalex.setDuration(150);
562 scalex.addListener(new AnimatorListener() {
563
564 @Override
565 public void onAnimationCancel(Animator animation) {
566 }
567
568 @Override
569 public void onAnimationEnd(Animator animation) {
Michael Kolb8814d732011-01-26 11:22:30 -0800570 mUi.onAddTabCompleted(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800571 }
572
573 @Override
574 public void onAnimationRepeat(Animator animation) {
575 }
576
577 @Override
578 public void onAnimationStart(Animator animation) {
579 mTabs.addTab(tv);
580 }
581
582 });
583 scalex.start();
584 }
585
Michael Kolba2b2ba82010-08-04 17:54:03 -0700586 // TabChangeListener implementation
587
Michael Kolb8233fac2010-10-26 16:08:53 -0700588 public void onSetActiveTab(Tab tab) {
589 mTabs.setSelectedTab(mTabControl.getTabIndex(tab));
Michael Kolb94827b62011-01-08 14:23:45 -0800590 TabView tv = mTabMap.get(tab);
591 if (tv != null) {
592 tv.setProgress(tv.mTab.getLoadProgress());
Michael Kolba2b2ba82010-08-04 17:54:03 -0700593 // update the scroll state
594 WebView webview = tab.getWebView();
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800595 if (webview != null) {
596 int h = webview.getVisibleTitleHeight();
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800597 onScroll(h);
598 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700599 }
600 }
601
Michael Kolba2b2ba82010-08-04 17:54:03 -0700602 public void onFavicon(Tab tab, Bitmap favicon) {
Michael Kolb94827b62011-01-08 14:23:45 -0800603 TabView tv = mTabMap.get(tab);
604 if (tv != null) {
605 tv.setFavicon(renderFavicon(favicon));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700606 }
607 }
608
Michael Kolba2b2ba82010-08-04 17:54:03 -0700609 public void onNewTab(Tab tab) {
Michael Kolb94827b62011-01-08 14:23:45 -0800610 TabView tv = buildTabView(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800611 animateTabIn(tab, tv);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700612 }
613
Michael Kolba2b2ba82010-08-04 17:54:03 -0700614 public void onProgress(Tab tab, int progress) {
Michael Kolb94827b62011-01-08 14:23:45 -0800615 TabView tv = mTabMap.get(tab);
616 if (tv != null) {
617 tv.setProgress(progress);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700618 }
619 }
620
Michael Kolba2b2ba82010-08-04 17:54:03 -0700621 public void onRemoveTab(Tab tab) {
Michael Kolb94827b62011-01-08 14:23:45 -0800622 TabView tv = mTabMap.get(tab);
623 if (tv != null) {
Michael Kolb2d59c322011-01-25 13:18:55 -0800624 animateTabOut(tab, tv);
625 } else {
626 mTabMap.remove(tab);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700627 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700628 }
629
Michael Kolba2b2ba82010-08-04 17:54:03 -0700630 public void onUrlAndTitle(Tab tab, String url, String title) {
Michael Kolb94827b62011-01-08 14:23:45 -0800631 TabView tv = mTabMap.get(tab);
632 if (tv != null) {
633 if (title != null) {
634 tv.setDisplayTitle(title);
635 } else if (url != null) {
636 tv.setDisplayTitle(UrlUtils.stripUrl(url));
637 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700638 }
639 }
640
Michael Kolba2b2ba82010-08-04 17:54:03 -0700641 private boolean isLoading() {
Michael Kolb94827b62011-01-08 14:23:45 -0800642 TabView tv = mTabMap.get(mTabControl.getCurrentTab());
643 if (tv != null) {
644 return tv.mInLoad;
Michael Kolb8233fac2010-10-26 16:08:53 -0700645 } else {
646 return false;
647 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700648 }
649
Michael Kolba2b2ba82010-08-04 17:54:03 -0700650}