blob: 1d17cb3f5cd1cae7f7e75f91c2c7b979ea80b9d8 [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);
Michael Kolbeb7001c2011-02-16 16:07:33 -0800136 mFocusPaint.setStrokeWidth(res.getDimension(R.dimen.tab_focus_stroke));
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800137 mFocusPaint.setAntiAlias(true);
138 mFocusPaint.setColor(res.getColor(R.color.tabFocusHighlight));
Michael Kolbebba8b42010-09-30 12:57:59 -0700139 }
140
Michael Kolb376b5412010-12-15 11:52:57 -0800141 void setUseQuickControls(boolean useQuickControls) {
142 mUseQuickControls = useQuickControls;
Michael Kolb467af0a2011-01-11 13:09:49 -0800143 mNewTab.setVisibility(mUseQuickControls ? View.GONE
144 : View.VISIBLE);
Michael Kolb376b5412010-12-15 11:52:57 -0800145 }
146
147 int getTabCount() {
148 return mTabMap.size();
149 }
150
Michael Kolb1bf23132010-11-19 12:55:12 -0800151 void updateTabs(List<Tab> tabs) {
152 mTabs.clearTabs();
153 mTabMap.clear();
154 for (Tab tab : tabs) {
Michael Kolb94827b62011-01-08 14:23:45 -0800155 TabView tv = buildTabView(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800156 mTabs.addTab(tv);
Michael Kolb1bf23132010-11-19 12:55:12 -0800157 }
158 mTabs.setSelectedTab(mTabControl.getCurrentIndex());
159 }
160
Michael Kolbebba8b42010-09-30 12:57:59 -0700161 @Override
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800162 protected void onMeasure(int hspec, int vspec) {
163 super.onMeasure(hspec, vspec);
164 int w = getMeasuredWidth();
165 // adjust for new tab overlap
Michael Kolb467af0a2011-01-11 13:09:49 -0800166 if (!mUseQuickControls) {
Michael Kolb5a72f182011-01-13 20:35:06 -0800167 w -= mAddTabOverlap;
Michael Kolb467af0a2011-01-11 13:09:49 -0800168 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800169 setMeasuredDimension(w, getMeasuredHeight());
170 }
171
172 @Override
Michael Kolbebba8b42010-09-30 12:57:59 -0700173 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800174 // use paddingLeft and paddingTop
175 int pl = getPaddingLeft();
176 int pt = getPaddingTop();
Michael Kolbebba8b42010-09-30 12:57:59 -0700177 int sw = mTabs.getMeasuredWidth();
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800178 int w = right - left - pl;
Michael Kolbb4cafc52011-01-12 16:55:04 -0800179 if (mUseQuickControls) {
180 mButtonWidth = 0;
181 } else {
Michael Kolb5a72f182011-01-13 20:35:06 -0800182 mButtonWidth = mNewTab.getMeasuredWidth() - mAddTabOverlap;
Michael Kolbb4cafc52011-01-12 16:55:04 -0800183 if (w-sw < mButtonWidth) {
184 sw = w - mButtonWidth;
185 }
Michael Kolbebba8b42010-09-30 12:57:59 -0700186 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800187 mTabs.layout(pl, pt, pl + sw, bottom - top);
188 // adjust for overlap
Michael Kolbb4cafc52011-01-12 16:55:04 -0800189 if (!mUseQuickControls) {
Michael Kolb5a72f182011-01-13 20:35:06 -0800190 mNewTab.layout(pl + sw - mAddTabOverlap, pt,
191 pl + sw + mButtonWidth - mAddTabOverlap, bottom - top);
Michael Kolbb4cafc52011-01-12 16:55:04 -0800192 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700193 }
194
195 public void onClick(View view) {
Michael Kolbebba8b42010-09-30 12:57:59 -0700196 if (mNewTab == view) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700197 mUiController.openTabToHomePage();
Michael Kolbebba8b42010-09-30 12:57:59 -0700198 } else if (mTabs.getSelectedTab() == view) {
Michael Kolb467af0a2011-01-11 13:09:49 -0800199 if (mUseQuickControls) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800200 if (mUi.isTitleBarShowing() && !isLoading()) {
201 mUi.stopEditingUrl();
202 mUi.hideTitleBar();
Michael Kolb467af0a2011-01-11 13:09:49 -0800203 } else {
204 mUi.stopWebViewScrolling();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800205 mUi.showTitleBarAndEdit();
Michael Kolb467af0a2011-01-11 13:09:49 -0800206 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800207 } else if (mUi.isTitleBarShowing() && !isLoading()) {
208 mUi.stopEditingUrl();
209 mUi.hideTitleBar();
Michael Kolbed217742010-08-10 17:52:34 -0700210 } else {
211 showUrlBar();
212 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700213 } else {
Michael Kolbebba8b42010-09-30 12:57:59 -0700214 int ix = mTabs.getChildIndex(view);
215 if (ix >= 0) {
216 mTabs.setSelectedTab(ix);
Michael Kolb8233fac2010-10-26 16:08:53 -0700217 mUiController.switchToTab(ix);
Michael Kolbebba8b42010-09-30 12:57:59 -0700218 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700219 }
220 }
221
Michael Kolbed217742010-08-10 17:52:34 -0700222 private void showUrlBar() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700223 mUi.stopWebViewScrolling();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800224 mUi.showTitleBar();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700225 }
226
Michael Kolb376b5412010-12-15 11:52:57 -0800227 void showTitleBarIndicator(boolean show) {
Michael Kolba0e2a752010-12-12 15:27:56 -0800228 Tab tab = mTabControl.getCurrentTab();
229 if (tab != null) {
Michael Kolb94827b62011-01-08 14:23:45 -0800230 TabView tv = mTabMap.get(tab);
231 if (tv != null) {
232 tv.showIndicator(show);
Michael Kolba0e2a752010-12-12 15:27:56 -0800233 }
234 }
Michael Kolbed217742010-08-10 17:52:34 -0700235 }
236
Michael Kolb7dad8f02011-01-21 11:06:18 -0800237 boolean showsTitleBarIndicator() {
238 Tab tab = mTabControl.getCurrentTab();
239 if (tab != null) {
240 TabView tv = mTabMap.get(tab);
241 if (tv != null) {
242 return tv.showsIndicator();
243 }
244 }
245 return false;
246 }
247
Michael Kolbed217742010-08-10 17:52:34 -0700248 // callback after fake titlebar is shown
249 void onShowTitleBar() {
Michael Kolba0e2a752010-12-12 15:27:56 -0800250 showTitleBarIndicator(false);
Michael Kolbed217742010-08-10 17:52:34 -0700251 }
252
253 // callback after fake titlebar is hidden
Michael Kolba2b2ba82010-08-04 17:54:03 -0700254 void onHideTitleBar() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700255 Tab tab = mTabControl.getCurrentTab();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800256 WebView w = tab.getWebView();
257 if (w != null) {
258 showTitleBarIndicator(w.getVisibleTitleHeight() == 0);
259 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700260 }
261
Michael Kolbed217742010-08-10 17:52:34 -0700262 // webview scroll listener
263
264 @Override
Michael Kolb5ee018e2011-02-18 15:47:21 -0800265 public void onScroll(int visibleTitleHeight, boolean userInitiated) {
Michael Kolb376b5412010-12-15 11:52:57 -0800266 if (mUseQuickControls) return;
Michael Kolb8233fac2010-10-26 16:08:53 -0700267 // isLoading is using the current tab, which initially might not be set yet
John Reckdcf911c2010-12-22 16:32:31 -0800268 if (mTabControl.getCurrentTab() != null
269 && !isLoading()) {
270 if (visibleTitleHeight == 0) {
Michael Kolb5ee018e2011-02-18 15:47:21 -0800271 if (!showsTitleBarIndicator()
272 && (!mUi.isEditingUrl() || userInitiated)) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800273 mUi.hideTitleBar();
Michael Kolb7dad8f02011-01-21 11:06:18 -0800274 showTitleBarIndicator(true);
275 }
John Reckdcf911c2010-12-22 16:32:31 -0800276 } else {
Michael Kolb7dad8f02011-01-21 11:06:18 -0800277 if (showsTitleBarIndicator()) {
278 showTitleBarIndicator(false);
279 }
Michael Kolbed217742010-08-10 17:52:34 -0700280 }
281 }
282 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700283
284 @Override
285 public void createContextMenu(ContextMenu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700286 MenuInflater inflater = mActivity.getMenuInflater();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700287 inflater.inflate(R.menu.title_context, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -0700288 mActivity.onCreateContextMenu(menu, this, null);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700289 }
290
Michael Kolb94827b62011-01-08 14:23:45 -0800291 private TabView buildTabView(Tab tab) {
292 TabView tabview = new TabView(mActivity, tab);
293 mTabMap.put(tab, tabview);
294 tabview.setOnClickListener(this);
Michael Kolb94827b62011-01-08 14:23:45 -0800295 return tabview;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700296 }
297
Romain Guyc8373212011-01-07 19:15:16 -0800298 private static Bitmap getDrawableAsBitmap(Drawable drawable, int width, int height) {
299 Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
300 Canvas c = new Canvas(b);
301 drawable.setBounds(0, 0, width, height);
302 drawable.draw(c);
303 return b;
Michael Kolbb4cafc52011-01-12 16:55:04 -0800304 }
305
Michael Kolba2b2ba82010-08-04 17:54:03 -0700306 /**
307 * View used in the tab bar
308 */
309 class TabView extends LinearLayout implements OnClickListener {
310
Michael Kolb94827b62011-01-08 14:23:45 -0800311 Tab mTab;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700312 View mTabContent;
313 TextView mTitle;
Michael Kolba0e2a752010-12-12 15:27:56 -0800314 View mIndicator;
Michael Kolbae62fd42010-08-18 16:33:28 -0700315 View mIncognito;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700316 ImageView mIconView;
317 ImageView mLock;
318 ImageView mClose;
319 boolean mSelected;
320 boolean mInLoad;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800321 Path mPath;
Michael Kolbeb7001c2011-02-16 16:07:33 -0800322 Path mFocusPath;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800323 int[] mWindowPos;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700324
325 /**
326 * @param context
327 */
Michael Kolb94827b62011-01-08 14:23:45 -0800328 public TabView(Context context, Tab tab) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700329 super(context);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800330 setWillNotDraw(false);
331 mPath = new Path();
Michael Kolbeb7001c2011-02-16 16:07:33 -0800332 mFocusPath = new Path();
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800333 mWindowPos = new int[2];
Michael Kolb94827b62011-01-08 14:23:45 -0800334 mTab = tab;
Michael Kolbed217742010-08-10 17:52:34 -0700335 setGravity(Gravity.CENTER_VERTICAL);
336 setOrientation(LinearLayout.HORIZONTAL);
Michael Kolb49f15832011-01-25 15:02:27 -0800337 setPadding(mTabOverlap, 0, mTabSliceWidth, 0);
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100338 LayoutInflater inflater = LayoutInflater.from(getContext());
Michael Kolbed217742010-08-10 17:52:34 -0700339 mTabContent = inflater.inflate(R.layout.tab_title, this, true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700340 mTitle = (TextView) mTabContent.findViewById(R.id.title);
341 mIconView = (ImageView) mTabContent.findViewById(R.id.favicon);
342 mLock = (ImageView) mTabContent.findViewById(R.id.lock);
343 mClose = (ImageView) mTabContent.findViewById(R.id.close);
344 mClose.setOnClickListener(this);
Michael Kolbae62fd42010-08-18 16:33:28 -0700345 mIncognito = mTabContent.findViewById(R.id.incognito);
Michael Kolba0e2a752010-12-12 15:27:56 -0800346 mIndicator = mTabContent.findViewById(R.id.chevron);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700347 mSelected = false;
348 mInLoad = false;
349 // update the status
Michael Kolb94827b62011-01-08 14:23:45 -0800350 updateFromTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700351 }
352
Michael Kolba0e2a752010-12-12 15:27:56 -0800353 void showIndicator(boolean show) {
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800354 if (mSelected) {
355 mIndicator.setVisibility(show ? View.VISIBLE : View.GONE);
Michael Kolb49f15832011-01-25 15:02:27 -0800356 LayoutParams lp = (LinearLayout.LayoutParams) getLayoutParams();
357 if (show) {
358 lp.width = mTabWidthSelected + mIndicator.getWidth();
359 } else {
360 lp.width = mTabWidthSelected;
361 }
362 lp.height = LayoutParams.MATCH_PARENT;
363 setLayoutParams(lp);
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800364 } else {
365 mIndicator.setVisibility(View.GONE);
366 }
Michael Kolba0e2a752010-12-12 15:27:56 -0800367 }
368
Michael Kolb7dad8f02011-01-21 11:06:18 -0800369 boolean showsIndicator() {
370 return (mIndicator.getVisibility() == View.VISIBLE);
371 }
372
Michael Kolba2b2ba82010-08-04 17:54:03 -0700373 @Override
374 public void onClick(View v) {
375 if (v == mClose) {
376 closeTab();
377 }
378 }
379
Michael Kolb94827b62011-01-08 14:23:45 -0800380 private void updateFromTab() {
381 String displayTitle = mTab.getTitle();
John Reck30c714c2010-12-16 17:30:34 -0800382 if (displayTitle == null) {
Michael Kolb94827b62011-01-08 14:23:45 -0800383 displayTitle = mTab.getUrl();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700384 }
John Reck30c714c2010-12-16 17:30:34 -0800385 setDisplayTitle(displayTitle);
Michael Kolb94827b62011-01-08 14:23:45 -0800386 setProgress(mTab.getLoadProgress());
387 if (mTab.getFavicon() != null) {
388 setFavicon(renderFavicon(mTab.getFavicon()));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700389 }
Michael Kolb94827b62011-01-08 14:23:45 -0800390 if (mTab != null) {
Michael Kolbae62fd42010-08-18 16:33:28 -0700391 mIncognito.setVisibility(
Michael Kolb94827b62011-01-08 14:23:45 -0800392 mTab.isPrivateBrowsingEnabled() ?
Michael Kolbae62fd42010-08-18 16:33:28 -0700393 View.VISIBLE : View.GONE);
394 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700395 }
396
397 @Override
Michael Kolbb7b115e2010-09-25 16:59:37 -0700398 public void setActivated(boolean selected) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700399 mSelected = selected;
400 mClose.setVisibility(mSelected ? View.VISIBLE : View.GONE);
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800401 mIndicator.setVisibility(View.GONE);
Michael Kolb8233fac2010-10-26 16:08:53 -0700402 mTitle.setTextAppearance(mActivity, mSelected ?
Michael Kolbc7485ae2010-09-03 10:10:58 -0700403 R.style.TabTitleSelected : R.style.TabTitleUnselected);
404 setHorizontalFadingEdgeEnabled(!mSelected);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700405 super.setActivated(selected);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800406 LayoutParams lp = (LinearLayout.LayoutParams) getLayoutParams();
407 lp.width = selected ? mTabWidthSelected : mTabWidthUnselected;
408 lp.height = LayoutParams.MATCH_PARENT;
409 setLayoutParams(lp);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800410 setFocusable(!selected);
411 postInvalidate();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700412 }
413
414 void setDisplayTitle(String title) {
415 mTitle.setText(title);
416 }
417
418 void setFavicon(Drawable d) {
419 mIconView.setImageDrawable(d);
420 }
421
422 void setLock(Drawable d) {
423 if (null == d) {
424 mLock.setVisibility(View.GONE);
425 } else {
426 mLock.setImageDrawable(d);
427 mLock.setVisibility(View.VISIBLE);
428 }
429 }
430
Michael Kolba2b2ba82010-08-04 17:54:03 -0700431 void setProgress(int newProgress) {
432 if (newProgress >= PROGRESS_MAX) {
433 mInLoad = false;
434 } else {
435 if (!mInLoad && getWindowToken() != null) {
436 mInLoad = true;
437 }
438 }
439 }
440
441 private void closeTab() {
Michael Kolb94827b62011-01-08 14:23:45 -0800442 if (mTab == mTabControl.getCurrentTab()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700443 mUiController.closeCurrentTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700444 } else {
Michael Kolb94827b62011-01-08 14:23:45 -0800445 mUiController.closeTab(mTab);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700446 }
447 }
448
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800449 @Override
450 protected void onLayout(boolean changed, int l, int t, int r, int b) {
451 super.onLayout(changed, l, t, r, b);
452 setTabPath(mPath, 0, 0, r - l, b - t);
Michael Kolbeb7001c2011-02-16 16:07:33 -0800453 setFocusPath(mFocusPath, 0, 0, r - l, b - t);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800454 }
Michael Kolbb4cafc52011-01-12 16:55:04 -0800455
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800456 @Override
457 protected void dispatchDraw(Canvas canvas) {
Romain Guyc8373212011-01-07 19:15:16 -0800458 if (mCurrentTextureWidth != mUi.getContentWidth() ||
459 mCurrentTextureHeight != getHeight()) {
460 mCurrentTextureWidth = mUi.getContentWidth();
461 mCurrentTextureHeight = getHeight();
462
463 if (mCurrentTextureWidth > 0 && mCurrentTextureHeight > 0) {
464 Bitmap activeTexture = getDrawableAsBitmap(mActiveDrawable,
465 mCurrentTextureWidth, mCurrentTextureHeight);
466 Bitmap inactiveTexture = getDrawableAsBitmap(mInactiveDrawable,
467 mCurrentTextureWidth, mCurrentTextureHeight);
468
469 mActiveShader = new BitmapShader(activeTexture,
470 Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
471 mActiveShaderPaint.setShader(mActiveShader);
Michael Kolbb4cafc52011-01-12 16:55:04 -0800472
Romain Guyc8373212011-01-07 19:15:16 -0800473 mInactiveShader = new BitmapShader(inactiveTexture,
474 Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
475 mInactiveShaderPaint.setShader(mInactiveShader);
476 }
477 }
Michael Kolb05902aa2011-02-22 17:43:38 -0800478 // add some monkey protection
479 if ((mActiveShader != null) && (mInactiveShader != null)) {
480 int state = canvas.save();
481 getLocationInWindow(mWindowPos);
482 Paint paint = mSelected ? mActiveShaderPaint : mInactiveShaderPaint;
483 drawClipped(canvas, paint, mPath, mWindowPos[0]);
484 canvas.restoreToCount(state);
485 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800486 super.dispatchDraw(canvas);
487 }
488
Romain Guyc8373212011-01-07 19:15:16 -0800489 private void drawClipped(Canvas canvas, Paint paint, Path clipPath, int left) {
490 // TODO: We should change the matrix/shader only when needed
491 final Matrix matrix = mSelected ? mActiveMatrix : mInactiveMatrix;
492 matrix.setTranslate(-left, 0.0f);
493 (mSelected ? mActiveShader : mInactiveShader).setLocalMatrix(matrix);
494 canvas.drawPath(clipPath, paint);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800495 if (isFocused()) {
Michael Kolbeb7001c2011-02-16 16:07:33 -0800496 canvas.drawPath(mFocusPath, mFocusPaint);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800497 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800498 }
499
500 private void setTabPath(Path path, int l, int t, int r, int b) {
501 path.reset();
502 path.moveTo(l, b);
503 path.lineTo(l, t);
504 path.lineTo(r - mTabSliceWidth, t);
505 path.lineTo(r, b);
506 path.close();
507 }
508
Michael Kolbeb7001c2011-02-16 16:07:33 -0800509 private void setFocusPath(Path path, int l, int t, int r, int b) {
510 path.reset();
511 path.moveTo(l, b);
512 path.lineTo(l, t);
513 path.lineTo(r - mTabSliceWidth, t);
514 path.lineTo(r, b);
515 }
516
Michael Kolba2b2ba82010-08-04 17:54:03 -0700517 }
518
Michael Kolb49f15832011-01-25 15:02:27 -0800519 static Drawable createFaviconBackground(Context context) {
520 PaintDrawable faviconBackground = new PaintDrawable();
521 Resources res = context.getResources();
522 faviconBackground.getPaint().setColor(context.getResources()
523 .getColor(R.color.tabFaviconBackground));
524 faviconBackground.setCornerRadius(
525 res.getDimension(R.dimen.tab_favicon_corner_radius));
526 return faviconBackground;
527 }
528
Michael Kolb94827b62011-01-08 14:23:45 -0800529 private Drawable renderFavicon(Bitmap icon) {
Michael Kolb49f15832011-01-25 15:02:27 -0800530 Drawable[] array = new Drawable[2];
531 array[0] = createFaviconBackground(getContext());
Michael Kolb94827b62011-01-08 14:23:45 -0800532 if (icon == null) {
Michael Kolb49f15832011-01-25 15:02:27 -0800533 array[1] = mGenericFavicon;
Michael Kolb94827b62011-01-08 14:23:45 -0800534 } else {
Michael Kolb49f15832011-01-25 15:02:27 -0800535 array[1] = new BitmapDrawable(icon);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700536 }
Michael Kolb94827b62011-01-08 14:23:45 -0800537 LayerDrawable d = new LayerDrawable(array);
Michael Kolb49f15832011-01-25 15:02:27 -0800538 d.setLayerInset(1, 2, 2, 2, 2);
Michael Kolb94827b62011-01-08 14:23:45 -0800539 return d;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700540 }
541
Michael Kolb2d59c322011-01-25 13:18:55 -0800542 private void animateTabOut(final Tab tab, final TabView tv) {
543 ObjectAnimator scalex = ObjectAnimator.ofFloat(tv, "scaleX", 1.0f, 0.0f);
544 ObjectAnimator scaley = ObjectAnimator.ofFloat(tv, "scaleY", 1.0f, 0.0f);
Michael Kolb49f15832011-01-25 15:02:27 -0800545 ObjectAnimator alpha = ObjectAnimator.ofFloat(tv, "alpha", 1.0f, 0.0f);
Michael Kolb2d59c322011-01-25 13:18:55 -0800546 AnimatorSet animator = new AnimatorSet();
Michael Kolb49f15832011-01-25 15:02:27 -0800547 animator.playTogether(scalex, scaley, alpha);
Michael Kolb2d59c322011-01-25 13:18:55 -0800548 animator.setDuration(150);
549 animator.addListener(new AnimatorListener() {
550
551 @Override
552 public void onAnimationCancel(Animator animation) {
553 }
554
555 @Override
556 public void onAnimationEnd(Animator animation) {
557 mTabs.removeTab(tv);
558 mTabMap.remove(tab);
Michael Kolb8814d732011-01-26 11:22:30 -0800559 mUi.onRemoveTabCompleted(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800560 }
561
562 @Override
563 public void onAnimationRepeat(Animator animation) {
564 }
565
566 @Override
567 public void onAnimationStart(Animator animation) {
568 }
569
570 });
571 animator.start();
572 }
573
574 private void animateTabIn(final Tab tab, final TabView tv) {
Michael Kolb49f15832011-01-25 15:02:27 -0800575 ObjectAnimator scalex = ObjectAnimator.ofFloat(tv, "scaleX", 0.0f, 1.0f);
Michael Kolb2d59c322011-01-25 13:18:55 -0800576 scalex.setDuration(150);
577 scalex.addListener(new AnimatorListener() {
578
579 @Override
580 public void onAnimationCancel(Animator animation) {
581 }
582
583 @Override
584 public void onAnimationEnd(Animator animation) {
Michael Kolb8814d732011-01-26 11:22:30 -0800585 mUi.onAddTabCompleted(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800586 }
587
588 @Override
589 public void onAnimationRepeat(Animator animation) {
590 }
591
592 @Override
593 public void onAnimationStart(Animator animation) {
594 mTabs.addTab(tv);
595 }
596
597 });
598 scalex.start();
599 }
600
Michael Kolba2b2ba82010-08-04 17:54:03 -0700601 // TabChangeListener implementation
602
Michael Kolb8233fac2010-10-26 16:08:53 -0700603 public void onSetActiveTab(Tab tab) {
604 mTabs.setSelectedTab(mTabControl.getTabIndex(tab));
Michael Kolb94827b62011-01-08 14:23:45 -0800605 TabView tv = mTabMap.get(tab);
606 if (tv != null) {
607 tv.setProgress(tv.mTab.getLoadProgress());
Michael Kolba2b2ba82010-08-04 17:54:03 -0700608 // update the scroll state
609 WebView webview = tab.getWebView();
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800610 if (webview != null) {
611 int h = webview.getVisibleTitleHeight();
Michael Kolb5ee018e2011-02-18 15:47:21 -0800612 onScroll(h, true);
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800613 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700614 }
615 }
616
Michael Kolba2b2ba82010-08-04 17:54:03 -0700617 public void onFavicon(Tab tab, Bitmap favicon) {
Michael Kolb94827b62011-01-08 14:23:45 -0800618 TabView tv = mTabMap.get(tab);
619 if (tv != null) {
620 tv.setFavicon(renderFavicon(favicon));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700621 }
622 }
623
Michael Kolba2b2ba82010-08-04 17:54:03 -0700624 public void onNewTab(Tab tab) {
Michael Kolb94827b62011-01-08 14:23:45 -0800625 TabView tv = buildTabView(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800626 animateTabIn(tab, tv);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700627 }
628
Michael Kolba2b2ba82010-08-04 17:54:03 -0700629 public void onProgress(Tab tab, int progress) {
Michael Kolb94827b62011-01-08 14:23:45 -0800630 TabView tv = mTabMap.get(tab);
631 if (tv != null) {
632 tv.setProgress(progress);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700633 }
634 }
635
Michael Kolba2b2ba82010-08-04 17:54:03 -0700636 public void onRemoveTab(Tab tab) {
Michael Kolb94827b62011-01-08 14:23:45 -0800637 TabView tv = mTabMap.get(tab);
638 if (tv != null) {
Michael Kolb2d59c322011-01-25 13:18:55 -0800639 animateTabOut(tab, tv);
640 } else {
641 mTabMap.remove(tab);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700642 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700643 }
644
Michael Kolba2b2ba82010-08-04 17:54:03 -0700645 public void onUrlAndTitle(Tab tab, String url, String title) {
Michael Kolb94827b62011-01-08 14:23:45 -0800646 TabView tv = mTabMap.get(tab);
647 if (tv != null) {
648 if (title != null) {
649 tv.setDisplayTitle(title);
650 } else if (url != null) {
651 tv.setDisplayTitle(UrlUtils.stripUrl(url));
652 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700653 }
654 }
655
Michael Kolba2b2ba82010-08-04 17:54:03 -0700656 private boolean isLoading() {
Michael Kolb94827b62011-01-08 14:23:45 -0800657 TabView tv = mTabMap.get(mTabControl.getCurrentTab());
658 if (tv != null) {
659 return tv.mInLoad;
Michael Kolb8233fac2010-10-26 16:08:53 -0700660 } else {
661 return false;
662 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700663 }
664
Michael Kolba2b2ba82010-08-04 17:54:03 -0700665}