blob: a574d26f9f5b3249619a04f83054e1fcb1be9920 [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
John Reckb9a051b2011-03-18 11:55:48 -070019import com.android.browser.BrowserWebView.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 }
Michael Kolbc831b632011-05-11 09:30:34 -0700158 mTabs.setSelectedTab(mTabControl.getCurrentPosition());
Michael Kolb1bf23132010-11-19 12:55:12 -0800159 }
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 Kolb46f987e2011-04-05 10:39:10 -0700205 mUi.editUrl(false);
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 Kolbc831b632011-05-11 09:30:34 -0700213 } else if (view instanceof TabView) {
214 final Tab tab = ((TabView) view).mTab;
Michael Kolbebba8b42010-09-30 12:57:59 -0700215 int ix = mTabs.getChildIndex(view);
216 if (ix >= 0) {
217 mTabs.setSelectedTab(ix);
Michael Kolbc831b632011-05-11 09:30:34 -0700218 mUiController.switchToTab(tab);
Michael Kolbebba8b42010-09-30 12:57:59 -0700219 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700220 }
221 }
222
Michael Kolbed217742010-08-10 17:52:34 -0700223 private void showUrlBar() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700224 mUi.stopWebViewScrolling();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800225 mUi.showTitleBar();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700226 }
227
Michael Kolb376b5412010-12-15 11:52:57 -0800228 void showTitleBarIndicator(boolean show) {
Michael Kolba0e2a752010-12-12 15:27:56 -0800229 Tab tab = mTabControl.getCurrentTab();
230 if (tab != null) {
Michael Kolb94827b62011-01-08 14:23:45 -0800231 TabView tv = mTabMap.get(tab);
232 if (tv != null) {
233 tv.showIndicator(show);
Michael Kolba0e2a752010-12-12 15:27:56 -0800234 }
235 }
Michael Kolbed217742010-08-10 17:52:34 -0700236 }
237
Michael Kolb7dad8f02011-01-21 11:06:18 -0800238 boolean showsTitleBarIndicator() {
239 Tab tab = mTabControl.getCurrentTab();
240 if (tab != null) {
241 TabView tv = mTabMap.get(tab);
242 if (tv != null) {
243 return tv.showsIndicator();
244 }
245 }
246 return false;
247 }
248
Michael Kolbed217742010-08-10 17:52:34 -0700249 // callback after fake titlebar is shown
250 void onShowTitleBar() {
Michael Kolba0e2a752010-12-12 15:27:56 -0800251 showTitleBarIndicator(false);
Michael Kolbed217742010-08-10 17:52:34 -0700252 }
253
254 // callback after fake titlebar is hidden
Michael Kolba2b2ba82010-08-04 17:54:03 -0700255 void onHideTitleBar() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700256 Tab tab = mTabControl.getCurrentTab();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800257 WebView w = tab.getWebView();
258 if (w != null) {
259 showTitleBarIndicator(w.getVisibleTitleHeight() == 0);
260 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700261 }
262
Michael Kolbed217742010-08-10 17:52:34 -0700263 // webview scroll listener
264
265 @Override
Michael Kolb5ee018e2011-02-18 15:47:21 -0800266 public void onScroll(int visibleTitleHeight, boolean userInitiated) {
Michael Kolb376b5412010-12-15 11:52:57 -0800267 if (mUseQuickControls) return;
Michael Kolb8233fac2010-10-26 16:08:53 -0700268 // isLoading is using the current tab, which initially might not be set yet
John Reckdcf911c2010-12-22 16:32:31 -0800269 if (mTabControl.getCurrentTab() != null
270 && !isLoading()) {
271 if (visibleTitleHeight == 0) {
Michael Kolb5ee018e2011-02-18 15:47:21 -0800272 if (!showsTitleBarIndicator()
273 && (!mUi.isEditingUrl() || userInitiated)) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800274 mUi.hideTitleBar();
Michael Kolb7dad8f02011-01-21 11:06:18 -0800275 showTitleBarIndicator(true);
276 }
John Reckdcf911c2010-12-22 16:32:31 -0800277 } else {
Michael Kolb7dad8f02011-01-21 11:06:18 -0800278 if (showsTitleBarIndicator()) {
279 showTitleBarIndicator(false);
280 }
Michael Kolbed217742010-08-10 17:52:34 -0700281 }
282 }
283 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700284
285 @Override
286 public void createContextMenu(ContextMenu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700287 MenuInflater inflater = mActivity.getMenuInflater();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700288 inflater.inflate(R.menu.title_context, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -0700289 mActivity.onCreateContextMenu(menu, this, null);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700290 }
291
Michael Kolb94827b62011-01-08 14:23:45 -0800292 private TabView buildTabView(Tab tab) {
293 TabView tabview = new TabView(mActivity, tab);
294 mTabMap.put(tab, tabview);
295 tabview.setOnClickListener(this);
Michael Kolb94827b62011-01-08 14:23:45 -0800296 return tabview;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700297 }
298
Romain Guyc8373212011-01-07 19:15:16 -0800299 private static Bitmap getDrawableAsBitmap(Drawable drawable, int width, int height) {
300 Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
301 Canvas c = new Canvas(b);
302 drawable.setBounds(0, 0, width, height);
303 drawable.draw(c);
304 return b;
Michael Kolbb4cafc52011-01-12 16:55:04 -0800305 }
306
Michael Kolba2b2ba82010-08-04 17:54:03 -0700307 /**
308 * View used in the tab bar
309 */
310 class TabView extends LinearLayout implements OnClickListener {
311
Michael Kolb94827b62011-01-08 14:23:45 -0800312 Tab mTab;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700313 View mTabContent;
314 TextView mTitle;
Michael Kolba0e2a752010-12-12 15:27:56 -0800315 View mIndicator;
Michael Kolbae62fd42010-08-18 16:33:28 -0700316 View mIncognito;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700317 ImageView mIconView;
318 ImageView mLock;
319 ImageView mClose;
320 boolean mSelected;
321 boolean mInLoad;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800322 Path mPath;
Michael Kolbeb7001c2011-02-16 16:07:33 -0800323 Path mFocusPath;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800324 int[] mWindowPos;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700325
326 /**
327 * @param context
328 */
Michael Kolb94827b62011-01-08 14:23:45 -0800329 public TabView(Context context, Tab tab) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700330 super(context);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800331 setWillNotDraw(false);
332 mPath = new Path();
Michael Kolbeb7001c2011-02-16 16:07:33 -0800333 mFocusPath = new Path();
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800334 mWindowPos = new int[2];
Michael Kolb94827b62011-01-08 14:23:45 -0800335 mTab = tab;
Michael Kolbed217742010-08-10 17:52:34 -0700336 setGravity(Gravity.CENTER_VERTICAL);
337 setOrientation(LinearLayout.HORIZONTAL);
Michael Kolb49f15832011-01-25 15:02:27 -0800338 setPadding(mTabOverlap, 0, mTabSliceWidth, 0);
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100339 LayoutInflater inflater = LayoutInflater.from(getContext());
Michael Kolbed217742010-08-10 17:52:34 -0700340 mTabContent = inflater.inflate(R.layout.tab_title, this, true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700341 mTitle = (TextView) mTabContent.findViewById(R.id.title);
342 mIconView = (ImageView) mTabContent.findViewById(R.id.favicon);
343 mLock = (ImageView) mTabContent.findViewById(R.id.lock);
344 mClose = (ImageView) mTabContent.findViewById(R.id.close);
345 mClose.setOnClickListener(this);
Michael Kolbae62fd42010-08-18 16:33:28 -0700346 mIncognito = mTabContent.findViewById(R.id.incognito);
Michael Kolba0e2a752010-12-12 15:27:56 -0800347 mIndicator = mTabContent.findViewById(R.id.chevron);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700348 mSelected = false;
349 mInLoad = false;
350 // update the status
Michael Kolb94827b62011-01-08 14:23:45 -0800351 updateFromTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700352 }
353
Michael Kolba0e2a752010-12-12 15:27:56 -0800354 void showIndicator(boolean show) {
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800355 if (mSelected) {
356 mIndicator.setVisibility(show ? View.VISIBLE : View.GONE);
Michael Kolb49f15832011-01-25 15:02:27 -0800357 LayoutParams lp = (LinearLayout.LayoutParams) getLayoutParams();
358 if (show) {
359 lp.width = mTabWidthSelected + mIndicator.getWidth();
360 } else {
361 lp.width = mTabWidthSelected;
362 }
363 lp.height = LayoutParams.MATCH_PARENT;
364 setLayoutParams(lp);
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800365 } else {
366 mIndicator.setVisibility(View.GONE);
367 }
Michael Kolba0e2a752010-12-12 15:27:56 -0800368 }
369
Michael Kolb7dad8f02011-01-21 11:06:18 -0800370 boolean showsIndicator() {
371 return (mIndicator.getVisibility() == View.VISIBLE);
372 }
373
Michael Kolba2b2ba82010-08-04 17:54:03 -0700374 @Override
375 public void onClick(View v) {
376 if (v == mClose) {
377 closeTab();
378 }
379 }
380
Michael Kolb94827b62011-01-08 14:23:45 -0800381 private void updateFromTab() {
382 String displayTitle = mTab.getTitle();
John Reck30c714c2010-12-16 17:30:34 -0800383 if (displayTitle == null) {
Michael Kolb94827b62011-01-08 14:23:45 -0800384 displayTitle = mTab.getUrl();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700385 }
John Reck30c714c2010-12-16 17:30:34 -0800386 setDisplayTitle(displayTitle);
Michael Kolb94827b62011-01-08 14:23:45 -0800387 setProgress(mTab.getLoadProgress());
388 if (mTab.getFavicon() != null) {
389 setFavicon(renderFavicon(mTab.getFavicon()));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700390 }
Michael Kolb94827b62011-01-08 14:23:45 -0800391 if (mTab != null) {
Michael Kolbae62fd42010-08-18 16:33:28 -0700392 mIncognito.setVisibility(
Michael Kolb94827b62011-01-08 14:23:45 -0800393 mTab.isPrivateBrowsingEnabled() ?
Michael Kolbae62fd42010-08-18 16:33:28 -0700394 View.VISIBLE : View.GONE);
395 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700396 }
397
398 @Override
Michael Kolbb7b115e2010-09-25 16:59:37 -0700399 public void setActivated(boolean selected) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700400 mSelected = selected;
401 mClose.setVisibility(mSelected ? View.VISIBLE : View.GONE);
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800402 mIndicator.setVisibility(View.GONE);
Michael Kolb8233fac2010-10-26 16:08:53 -0700403 mTitle.setTextAppearance(mActivity, mSelected ?
Michael Kolbc7485ae2010-09-03 10:10:58 -0700404 R.style.TabTitleSelected : R.style.TabTitleUnselected);
405 setHorizontalFadingEdgeEnabled(!mSelected);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700406 super.setActivated(selected);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800407 LayoutParams lp = (LinearLayout.LayoutParams) getLayoutParams();
408 lp.width = selected ? mTabWidthSelected : mTabWidthUnselected;
409 lp.height = LayoutParams.MATCH_PARENT;
410 setLayoutParams(lp);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800411 setFocusable(!selected);
412 postInvalidate();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700413 }
414
415 void setDisplayTitle(String title) {
416 mTitle.setText(title);
417 }
418
419 void setFavicon(Drawable d) {
420 mIconView.setImageDrawable(d);
421 }
422
423 void setLock(Drawable d) {
424 if (null == d) {
425 mLock.setVisibility(View.GONE);
426 } else {
427 mLock.setImageDrawable(d);
428 mLock.setVisibility(View.VISIBLE);
429 }
430 }
431
Michael Kolba2b2ba82010-08-04 17:54:03 -0700432 void setProgress(int newProgress) {
433 if (newProgress >= PROGRESS_MAX) {
434 mInLoad = false;
435 } else {
436 if (!mInLoad && getWindowToken() != null) {
437 mInLoad = true;
438 }
439 }
440 }
441
442 private void closeTab() {
Michael Kolb94827b62011-01-08 14:23:45 -0800443 if (mTab == mTabControl.getCurrentTab()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700444 mUiController.closeCurrentTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700445 } else {
Michael Kolb94827b62011-01-08 14:23:45 -0800446 mUiController.closeTab(mTab);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700447 }
448 }
449
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800450 @Override
451 protected void onLayout(boolean changed, int l, int t, int r, int b) {
452 super.onLayout(changed, l, t, r, b);
453 setTabPath(mPath, 0, 0, r - l, b - t);
Michael Kolbeb7001c2011-02-16 16:07:33 -0800454 setFocusPath(mFocusPath, 0, 0, r - l, b - t);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800455 }
Michael Kolbb4cafc52011-01-12 16:55:04 -0800456
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800457 @Override
458 protected void dispatchDraw(Canvas canvas) {
Romain Guyc8373212011-01-07 19:15:16 -0800459 if (mCurrentTextureWidth != mUi.getContentWidth() ||
460 mCurrentTextureHeight != getHeight()) {
461 mCurrentTextureWidth = mUi.getContentWidth();
462 mCurrentTextureHeight = getHeight();
463
464 if (mCurrentTextureWidth > 0 && mCurrentTextureHeight > 0) {
465 Bitmap activeTexture = getDrawableAsBitmap(mActiveDrawable,
466 mCurrentTextureWidth, mCurrentTextureHeight);
467 Bitmap inactiveTexture = getDrawableAsBitmap(mInactiveDrawable,
468 mCurrentTextureWidth, mCurrentTextureHeight);
469
470 mActiveShader = new BitmapShader(activeTexture,
471 Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
472 mActiveShaderPaint.setShader(mActiveShader);
Michael Kolbb4cafc52011-01-12 16:55:04 -0800473
Romain Guyc8373212011-01-07 19:15:16 -0800474 mInactiveShader = new BitmapShader(inactiveTexture,
475 Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
476 mInactiveShaderPaint.setShader(mInactiveShader);
477 }
478 }
Michael Kolb05902aa2011-02-22 17:43:38 -0800479 // add some monkey protection
480 if ((mActiveShader != null) && (mInactiveShader != null)) {
481 int state = canvas.save();
482 getLocationInWindow(mWindowPos);
483 Paint paint = mSelected ? mActiveShaderPaint : mInactiveShaderPaint;
484 drawClipped(canvas, paint, mPath, mWindowPos[0]);
485 canvas.restoreToCount(state);
486 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800487 super.dispatchDraw(canvas);
488 }
489
Romain Guyc8373212011-01-07 19:15:16 -0800490 private void drawClipped(Canvas canvas, Paint paint, Path clipPath, int left) {
491 // TODO: We should change the matrix/shader only when needed
492 final Matrix matrix = mSelected ? mActiveMatrix : mInactiveMatrix;
493 matrix.setTranslate(-left, 0.0f);
494 (mSelected ? mActiveShader : mInactiveShader).setLocalMatrix(matrix);
495 canvas.drawPath(clipPath, paint);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800496 if (isFocused()) {
Michael Kolbeb7001c2011-02-16 16:07:33 -0800497 canvas.drawPath(mFocusPath, mFocusPaint);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800498 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800499 }
500
501 private void setTabPath(Path path, int l, int t, int r, int b) {
502 path.reset();
503 path.moveTo(l, b);
504 path.lineTo(l, t);
505 path.lineTo(r - mTabSliceWidth, t);
506 path.lineTo(r, b);
507 path.close();
508 }
509
Michael Kolbeb7001c2011-02-16 16:07:33 -0800510 private void setFocusPath(Path path, int l, int t, int r, int b) {
511 path.reset();
512 path.moveTo(l, b);
513 path.lineTo(l, t);
514 path.lineTo(r - mTabSliceWidth, t);
515 path.lineTo(r, b);
516 }
517
Michael Kolba2b2ba82010-08-04 17:54:03 -0700518 }
519
Michael Kolb49f15832011-01-25 15:02:27 -0800520 static Drawable createFaviconBackground(Context context) {
521 PaintDrawable faviconBackground = new PaintDrawable();
522 Resources res = context.getResources();
523 faviconBackground.getPaint().setColor(context.getResources()
524 .getColor(R.color.tabFaviconBackground));
525 faviconBackground.setCornerRadius(
526 res.getDimension(R.dimen.tab_favicon_corner_radius));
527 return faviconBackground;
528 }
529
Michael Kolb94827b62011-01-08 14:23:45 -0800530 private Drawable renderFavicon(Bitmap icon) {
Michael Kolb49f15832011-01-25 15:02:27 -0800531 Drawable[] array = new Drawable[2];
532 array[0] = createFaviconBackground(getContext());
Michael Kolb94827b62011-01-08 14:23:45 -0800533 if (icon == null) {
Michael Kolb49f15832011-01-25 15:02:27 -0800534 array[1] = mGenericFavicon;
Michael Kolb94827b62011-01-08 14:23:45 -0800535 } else {
Michael Kolb49f15832011-01-25 15:02:27 -0800536 array[1] = new BitmapDrawable(icon);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700537 }
Michael Kolb94827b62011-01-08 14:23:45 -0800538 LayerDrawable d = new LayerDrawable(array);
Michael Kolb49f15832011-01-25 15:02:27 -0800539 d.setLayerInset(1, 2, 2, 2, 2);
Michael Kolb94827b62011-01-08 14:23:45 -0800540 return d;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700541 }
542
Michael Kolb2d59c322011-01-25 13:18:55 -0800543 private void animateTabOut(final Tab tab, final TabView tv) {
544 ObjectAnimator scalex = ObjectAnimator.ofFloat(tv, "scaleX", 1.0f, 0.0f);
545 ObjectAnimator scaley = ObjectAnimator.ofFloat(tv, "scaleY", 1.0f, 0.0f);
Michael Kolb49f15832011-01-25 15:02:27 -0800546 ObjectAnimator alpha = ObjectAnimator.ofFloat(tv, "alpha", 1.0f, 0.0f);
Michael Kolb2d59c322011-01-25 13:18:55 -0800547 AnimatorSet animator = new AnimatorSet();
Michael Kolb49f15832011-01-25 15:02:27 -0800548 animator.playTogether(scalex, scaley, alpha);
Michael Kolb2d59c322011-01-25 13:18:55 -0800549 animator.setDuration(150);
550 animator.addListener(new AnimatorListener() {
551
552 @Override
553 public void onAnimationCancel(Animator animation) {
554 }
555
556 @Override
557 public void onAnimationEnd(Animator animation) {
558 mTabs.removeTab(tv);
559 mTabMap.remove(tab);
Michael Kolb8814d732011-01-26 11:22:30 -0800560 mUi.onRemoveTabCompleted(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800561 }
562
563 @Override
564 public void onAnimationRepeat(Animator animation) {
565 }
566
567 @Override
568 public void onAnimationStart(Animator animation) {
569 }
570
571 });
572 animator.start();
573 }
574
575 private void animateTabIn(final Tab tab, final TabView tv) {
Michael Kolb49f15832011-01-25 15:02:27 -0800576 ObjectAnimator scalex = ObjectAnimator.ofFloat(tv, "scaleX", 0.0f, 1.0f);
Michael Kolb2d59c322011-01-25 13:18:55 -0800577 scalex.setDuration(150);
578 scalex.addListener(new AnimatorListener() {
579
580 @Override
581 public void onAnimationCancel(Animator animation) {
582 }
583
584 @Override
585 public void onAnimationEnd(Animator animation) {
Michael Kolb8814d732011-01-26 11:22:30 -0800586 mUi.onAddTabCompleted(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800587 }
588
589 @Override
590 public void onAnimationRepeat(Animator animation) {
591 }
592
593 @Override
594 public void onAnimationStart(Animator animation) {
595 mTabs.addTab(tv);
596 }
597
598 });
599 scalex.start();
600 }
601
Michael Kolba2b2ba82010-08-04 17:54:03 -0700602 // TabChangeListener implementation
603
Michael Kolb8233fac2010-10-26 16:08:53 -0700604 public void onSetActiveTab(Tab tab) {
Michael Kolbc831b632011-05-11 09:30:34 -0700605 mTabs.setSelectedTab(mTabControl.getTabPosition(tab));
Michael Kolb94827b62011-01-08 14:23:45 -0800606 TabView tv = mTabMap.get(tab);
607 if (tv != null) {
608 tv.setProgress(tv.mTab.getLoadProgress());
Michael Kolba2b2ba82010-08-04 17:54:03 -0700609 // update the scroll state
610 WebView webview = tab.getWebView();
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800611 if (webview != null) {
612 int h = webview.getVisibleTitleHeight();
Michael Kolb5ee018e2011-02-18 15:47:21 -0800613 onScroll(h, true);
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800614 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700615 }
616 }
617
Michael Kolba2b2ba82010-08-04 17:54:03 -0700618 public void onFavicon(Tab tab, Bitmap favicon) {
Michael Kolb94827b62011-01-08 14:23:45 -0800619 TabView tv = mTabMap.get(tab);
620 if (tv != null) {
621 tv.setFavicon(renderFavicon(favicon));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700622 }
623 }
624
Michael Kolba2b2ba82010-08-04 17:54:03 -0700625 public void onNewTab(Tab tab) {
Michael Kolb94827b62011-01-08 14:23:45 -0800626 TabView tv = buildTabView(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800627 animateTabIn(tab, tv);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700628 }
629
Michael Kolba2b2ba82010-08-04 17:54:03 -0700630 public void onProgress(Tab tab, int progress) {
Michael Kolb94827b62011-01-08 14:23:45 -0800631 TabView tv = mTabMap.get(tab);
632 if (tv != null) {
633 tv.setProgress(progress);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700634 }
635 }
636
Michael Kolba2b2ba82010-08-04 17:54:03 -0700637 public void onRemoveTab(Tab tab) {
Michael Kolb94827b62011-01-08 14:23:45 -0800638 TabView tv = mTabMap.get(tab);
639 if (tv != null) {
Michael Kolb2d59c322011-01-25 13:18:55 -0800640 animateTabOut(tab, tv);
641 } else {
642 mTabMap.remove(tab);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700643 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700644 }
645
Michael Kolba2b2ba82010-08-04 17:54:03 -0700646 public void onUrlAndTitle(Tab tab, String url, String title) {
Michael Kolb94827b62011-01-08 14:23:45 -0800647 TabView tv = mTabMap.get(tab);
648 if (tv != null) {
649 if (title != null) {
650 tv.setDisplayTitle(title);
651 } else if (url != null) {
652 tv.setDisplayTitle(UrlUtils.stripUrl(url));
653 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700654 }
655 }
656
Michael Kolba2b2ba82010-08-04 17:54:03 -0700657 private boolean isLoading() {
Michael Kolb94827b62011-01-08 14:23:45 -0800658 TabView tv = mTabMap.get(mTabControl.getCurrentTab());
659 if (tv != null) {
660 return tv.mInLoad;
Michael Kolb8233fac2010-10-26 16:08:53 -0700661 } else {
662 return false;
663 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700664 }
665
Michael Kolba2b2ba82010-08-04 17:54:03 -0700666}