blob: c00e1f510f7f145df33cf3786d9d3d1ece36a4e2 [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();
88 private final Matrix mActiveMatrix = new Matrix();
89 private final Matrix mInactiveMatrix = new Matrix();
90
91 private BitmapShader mActiveShader;
92 private BitmapShader mInactiveShader;
93
Michael Kolb2b5a13a2010-12-09 14:13:42 -080094 private int mTabOverlap;
Michael Kolb5a72f182011-01-13 20:35:06 -080095 private int mAddTabOverlap;
Michael Kolb2b5a13a2010-12-09 14:13:42 -080096 private int mTabSliceWidth;
Michael Kolb376b5412010-12-15 11:52:57 -080097 private boolean mUseQuickControls;
Michael Kolb2b5a13a2010-12-09 14:13:42 -080098
Michael Kolb66706532010-12-12 19:50:22 -080099 public TabBar(Activity activity, UiController controller, XLargeUi ui) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700100 super(activity);
101 mActivity = activity;
102 mUiController = controller;
103 mTabControl = mUiController.getTabControl();
104 mUi = ui;
105 Resources res = activity.getResources();
Michael Kolbed217742010-08-10 17:52:34 -0700106 mTabWidthSelected = (int) res.getDimension(R.dimen.tab_width_selected);
107 mTabWidthUnselected = (int) res.getDimension(R.dimen.tab_width_unselected);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800108 mActiveDrawable = res.getDrawable(R.drawable.bg_urlbar);
109 mInactiveDrawable = res.getDrawable(R.drawable.browsertab_inactive);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700110
Michael Kolb94827b62011-01-08 14:23:45 -0800111 mTabMap = new HashMap<Tab, TabView>();
112 Resources resources = activity.getResources();
Michael Kolb8233fac2010-10-26 16:08:53 -0700113 LayoutInflater factory = LayoutInflater.from(activity);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700114 factory.inflate(R.layout.tab_bar, this);
Michael Kolbf558f0d2011-01-13 19:24:18 -0800115 setPadding(0, (int) res.getDimension(R.dimen.tab_padding_top), 0, 0);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700116 mTabs = (TabScrollView) findViewById(R.id.tabs);
Michael Kolbebba8b42010-09-30 12:57:59 -0700117 mNewTab = (ImageButton) findViewById(R.id.newtab);
118 mNewTab.setOnClickListener(this);
John Reckaff60fb2010-10-25 13:47:58 -0700119 mGenericFavicon = res.getDrawable(R.drawable.app_web_browser_sm);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700120
Michael Kolb1bf23132010-11-19 12:55:12 -0800121 updateTabs(mUiController.getTabs());
Michael Kolbebba8b42010-09-30 12:57:59 -0700122 mButtonWidth = -1;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800123 // tab dimensions
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800124 mTabOverlap = (int) res.getDimension(R.dimen.tab_overlap);
Michael Kolb5a72f182011-01-13 20:35:06 -0800125 mAddTabOverlap = (int) res.getDimension(R.dimen.tab_addoverlap);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800126 mTabSliceWidth = (int) res.getDimension(R.dimen.tab_slice);
Romain Guyc8373212011-01-07 19:15:16 -0800127
128 mActiveShaderPaint.setStyle(Paint.Style.FILL);
129 mActiveShaderPaint.setAntiAlias(true);
130
131 mInactiveShaderPaint.setStyle(Paint.Style.FILL);
132 mInactiveShaderPaint.setAntiAlias(true);
Michael Kolbb4cafc52011-01-12 16:55:04 -0800133
Michael Kolbebba8b42010-09-30 12:57:59 -0700134 }
135
Michael Kolb376b5412010-12-15 11:52:57 -0800136 void setUseQuickControls(boolean useQuickControls) {
137 mUseQuickControls = useQuickControls;
Michael Kolb467af0a2011-01-11 13:09:49 -0800138 mNewTab.setVisibility(mUseQuickControls ? View.GONE
139 : View.VISIBLE);
Michael Kolb376b5412010-12-15 11:52:57 -0800140 }
141
142 int getTabCount() {
143 return mTabMap.size();
144 }
145
Michael Kolb1bf23132010-11-19 12:55:12 -0800146 void updateTabs(List<Tab> tabs) {
147 mTabs.clearTabs();
148 mTabMap.clear();
149 for (Tab tab : tabs) {
Michael Kolb94827b62011-01-08 14:23:45 -0800150 TabView tv = buildTabView(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800151 mTabs.addTab(tv);
Michael Kolb1bf23132010-11-19 12:55:12 -0800152 }
153 mTabs.setSelectedTab(mTabControl.getCurrentIndex());
154 }
155
Michael Kolbebba8b42010-09-30 12:57:59 -0700156 @Override
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800157 protected void onMeasure(int hspec, int vspec) {
158 super.onMeasure(hspec, vspec);
159 int w = getMeasuredWidth();
160 // adjust for new tab overlap
Michael Kolb467af0a2011-01-11 13:09:49 -0800161 if (!mUseQuickControls) {
Michael Kolb5a72f182011-01-13 20:35:06 -0800162 w -= mAddTabOverlap;
Michael Kolb467af0a2011-01-11 13:09:49 -0800163 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800164 setMeasuredDimension(w, getMeasuredHeight());
165 }
166
167 @Override
Michael Kolbebba8b42010-09-30 12:57:59 -0700168 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800169 // use paddingLeft and paddingTop
170 int pl = getPaddingLeft();
171 int pt = getPaddingTop();
Michael Kolbebba8b42010-09-30 12:57:59 -0700172 int sw = mTabs.getMeasuredWidth();
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800173 int w = right - left - pl;
Michael Kolbb4cafc52011-01-12 16:55:04 -0800174 if (mUseQuickControls) {
175 mButtonWidth = 0;
176 } else {
Michael Kolb5a72f182011-01-13 20:35:06 -0800177 mButtonWidth = mNewTab.getMeasuredWidth() - mAddTabOverlap;
Michael Kolbb4cafc52011-01-12 16:55:04 -0800178 if (w-sw < mButtonWidth) {
179 sw = w - mButtonWidth;
180 }
Michael Kolbebba8b42010-09-30 12:57:59 -0700181 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800182 mTabs.layout(pl, pt, pl + sw, bottom - top);
183 // adjust for overlap
Michael Kolbb4cafc52011-01-12 16:55:04 -0800184 if (!mUseQuickControls) {
Michael Kolb5a72f182011-01-13 20:35:06 -0800185 mNewTab.layout(pl + sw - mAddTabOverlap, pt,
186 pl + sw + mButtonWidth - mAddTabOverlap, bottom - top);
Michael Kolbb4cafc52011-01-12 16:55:04 -0800187 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700188 }
189
190 public void onClick(View view) {
Michael Kolbebba8b42010-09-30 12:57:59 -0700191 if (mNewTab == view) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700192 mUiController.openTabToHomePage();
Michael Kolbebba8b42010-09-30 12:57:59 -0700193 } else if (mTabs.getSelectedTab() == view) {
Michael Kolb467af0a2011-01-11 13:09:49 -0800194 if (mUseQuickControls) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800195 if (mUi.isTitleBarShowing() && !isLoading()) {
196 mUi.stopEditingUrl();
197 mUi.hideTitleBar();
Michael Kolb467af0a2011-01-11 13:09:49 -0800198 } else {
199 mUi.stopWebViewScrolling();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800200 mUi.showTitleBarAndEdit();
Michael Kolb467af0a2011-01-11 13:09:49 -0800201 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800202 } else if (mUi.isTitleBarShowing() && !isLoading()) {
203 mUi.stopEditingUrl();
204 mUi.hideTitleBar();
Michael Kolbed217742010-08-10 17:52:34 -0700205 } else {
206 showUrlBar();
207 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700208 } else {
Michael Kolbebba8b42010-09-30 12:57:59 -0700209 int ix = mTabs.getChildIndex(view);
210 if (ix >= 0) {
211 mTabs.setSelectedTab(ix);
Michael Kolb8233fac2010-10-26 16:08:53 -0700212 mUiController.switchToTab(ix);
Michael Kolbebba8b42010-09-30 12:57:59 -0700213 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700214 }
215 }
216
Michael Kolbed217742010-08-10 17:52:34 -0700217 private void showUrlBar() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700218 mUi.stopWebViewScrolling();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800219 mUi.showTitleBar();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700220 }
221
Michael Kolb376b5412010-12-15 11:52:57 -0800222 void showTitleBarIndicator(boolean show) {
Michael Kolba0e2a752010-12-12 15:27:56 -0800223 Tab tab = mTabControl.getCurrentTab();
224 if (tab != null) {
Michael Kolb94827b62011-01-08 14:23:45 -0800225 TabView tv = mTabMap.get(tab);
226 if (tv != null) {
227 tv.showIndicator(show);
Michael Kolba0e2a752010-12-12 15:27:56 -0800228 }
229 }
Michael Kolbed217742010-08-10 17:52:34 -0700230 }
231
Michael Kolb7dad8f02011-01-21 11:06:18 -0800232 boolean showsTitleBarIndicator() {
233 Tab tab = mTabControl.getCurrentTab();
234 if (tab != null) {
235 TabView tv = mTabMap.get(tab);
236 if (tv != null) {
237 return tv.showsIndicator();
238 }
239 }
240 return false;
241 }
242
Michael Kolbed217742010-08-10 17:52:34 -0700243 // callback after fake titlebar is shown
244 void onShowTitleBar() {
Michael Kolba0e2a752010-12-12 15:27:56 -0800245 showTitleBarIndicator(false);
Michael Kolbed217742010-08-10 17:52:34 -0700246 }
247
248 // callback after fake titlebar is hidden
Michael Kolba2b2ba82010-08-04 17:54:03 -0700249 void onHideTitleBar() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700250 Tab tab = mTabControl.getCurrentTab();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800251 WebView w = tab.getWebView();
252 if (w != null) {
253 showTitleBarIndicator(w.getVisibleTitleHeight() == 0);
254 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700255 }
256
Michael Kolbed217742010-08-10 17:52:34 -0700257 // webview scroll listener
258
259 @Override
Michael Kolba0e2a752010-12-12 15:27:56 -0800260 public void onScroll(int visibleTitleHeight) {
Michael Kolb376b5412010-12-15 11:52:57 -0800261 if (mUseQuickControls) return;
Michael Kolb8233fac2010-10-26 16:08:53 -0700262 // isLoading is using the current tab, which initially might not be set yet
John Reckdcf911c2010-12-22 16:32:31 -0800263 if (mTabControl.getCurrentTab() != null
264 && !isLoading()) {
265 if (visibleTitleHeight == 0) {
Michael Kolb7dad8f02011-01-21 11:06:18 -0800266 if (!showsTitleBarIndicator()) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800267 mUi.hideTitleBar();
Michael Kolb7dad8f02011-01-21 11:06:18 -0800268 showTitleBarIndicator(true);
269 }
John Reckdcf911c2010-12-22 16:32:31 -0800270 } else {
Michael Kolb7dad8f02011-01-21 11:06:18 -0800271 if (showsTitleBarIndicator()) {
272 showTitleBarIndicator(false);
273 }
Michael Kolbed217742010-08-10 17:52:34 -0700274 }
275 }
276 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700277
278 @Override
279 public void createContextMenu(ContextMenu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700280 MenuInflater inflater = mActivity.getMenuInflater();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700281 inflater.inflate(R.menu.title_context, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -0700282 mActivity.onCreateContextMenu(menu, this, null);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700283 }
284
Michael Kolb94827b62011-01-08 14:23:45 -0800285 private TabView buildTabView(Tab tab) {
286 TabView tabview = new TabView(mActivity, tab);
287 mTabMap.put(tab, tabview);
288 tabview.setOnClickListener(this);
Michael Kolb94827b62011-01-08 14:23:45 -0800289 return tabview;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700290 }
291
Romain Guyc8373212011-01-07 19:15:16 -0800292 private static Bitmap getDrawableAsBitmap(Drawable drawable, int width, int height) {
293 Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
294 Canvas c = new Canvas(b);
295 drawable.setBounds(0, 0, width, height);
296 drawable.draw(c);
297 return b;
Michael Kolbb4cafc52011-01-12 16:55:04 -0800298 }
299
Michael Kolba2b2ba82010-08-04 17:54:03 -0700300 /**
301 * View used in the tab bar
302 */
303 class TabView extends LinearLayout implements OnClickListener {
304
Michael Kolb94827b62011-01-08 14:23:45 -0800305 Tab mTab;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700306 View mTabContent;
307 TextView mTitle;
Michael Kolba0e2a752010-12-12 15:27:56 -0800308 View mIndicator;
Michael Kolbae62fd42010-08-18 16:33:28 -0700309 View mIncognito;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700310 ImageView mIconView;
311 ImageView mLock;
312 ImageView mClose;
313 boolean mSelected;
314 boolean mInLoad;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800315 Path mPath;
316 int[] mWindowPos;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700317
318 /**
319 * @param context
320 */
Michael Kolb94827b62011-01-08 14:23:45 -0800321 public TabView(Context context, Tab tab) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700322 super(context);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800323 setWillNotDraw(false);
324 mPath = new Path();
325 mWindowPos = new int[2];
Michael Kolb94827b62011-01-08 14:23:45 -0800326 mTab = tab;
Michael Kolbed217742010-08-10 17:52:34 -0700327 setGravity(Gravity.CENTER_VERTICAL);
328 setOrientation(LinearLayout.HORIZONTAL);
Michael Kolb49f15832011-01-25 15:02:27 -0800329 setPadding(mTabOverlap, 0, mTabSliceWidth, 0);
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100330 LayoutInflater inflater = LayoutInflater.from(getContext());
Michael Kolbed217742010-08-10 17:52:34 -0700331 mTabContent = inflater.inflate(R.layout.tab_title, this, true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700332 mTitle = (TextView) mTabContent.findViewById(R.id.title);
333 mIconView = (ImageView) mTabContent.findViewById(R.id.favicon);
334 mLock = (ImageView) mTabContent.findViewById(R.id.lock);
335 mClose = (ImageView) mTabContent.findViewById(R.id.close);
336 mClose.setOnClickListener(this);
Michael Kolbae62fd42010-08-18 16:33:28 -0700337 mIncognito = mTabContent.findViewById(R.id.incognito);
Michael Kolba0e2a752010-12-12 15:27:56 -0800338 mIndicator = mTabContent.findViewById(R.id.chevron);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700339 mSelected = false;
340 mInLoad = false;
341 // update the status
Michael Kolb94827b62011-01-08 14:23:45 -0800342 updateFromTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700343 }
344
Michael Kolba0e2a752010-12-12 15:27:56 -0800345 void showIndicator(boolean show) {
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800346 if (mSelected) {
347 mIndicator.setVisibility(show ? View.VISIBLE : View.GONE);
Michael Kolb49f15832011-01-25 15:02:27 -0800348 LayoutParams lp = (LinearLayout.LayoutParams) getLayoutParams();
349 if (show) {
350 lp.width = mTabWidthSelected + mIndicator.getWidth();
351 } else {
352 lp.width = mTabWidthSelected;
353 }
354 lp.height = LayoutParams.MATCH_PARENT;
355 setLayoutParams(lp);
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800356 } else {
357 mIndicator.setVisibility(View.GONE);
358 }
Michael Kolba0e2a752010-12-12 15:27:56 -0800359 }
360
Michael Kolb7dad8f02011-01-21 11:06:18 -0800361 boolean showsIndicator() {
362 return (mIndicator.getVisibility() == View.VISIBLE);
363 }
364
Michael Kolba2b2ba82010-08-04 17:54:03 -0700365 @Override
366 public void onClick(View v) {
367 if (v == mClose) {
368 closeTab();
369 }
370 }
371
Michael Kolb94827b62011-01-08 14:23:45 -0800372 private void updateFromTab() {
373 String displayTitle = mTab.getTitle();
John Reck30c714c2010-12-16 17:30:34 -0800374 if (displayTitle == null) {
Michael Kolb94827b62011-01-08 14:23:45 -0800375 displayTitle = mTab.getUrl();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700376 }
John Reck30c714c2010-12-16 17:30:34 -0800377 setDisplayTitle(displayTitle);
Michael Kolb94827b62011-01-08 14:23:45 -0800378 setProgress(mTab.getLoadProgress());
379 if (mTab.getFavicon() != null) {
380 setFavicon(renderFavicon(mTab.getFavicon()));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700381 }
Michael Kolb94827b62011-01-08 14:23:45 -0800382 if (mTab != null) {
Michael Kolbae62fd42010-08-18 16:33:28 -0700383 mIncognito.setVisibility(
Michael Kolb94827b62011-01-08 14:23:45 -0800384 mTab.isPrivateBrowsingEnabled() ?
Michael Kolbae62fd42010-08-18 16:33:28 -0700385 View.VISIBLE : View.GONE);
386 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700387 }
388
389 @Override
Michael Kolbb7b115e2010-09-25 16:59:37 -0700390 public void setActivated(boolean selected) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700391 mSelected = selected;
392 mClose.setVisibility(mSelected ? View.VISIBLE : View.GONE);
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800393 mIndicator.setVisibility(View.GONE);
Michael Kolb8233fac2010-10-26 16:08:53 -0700394 mTitle.setTextAppearance(mActivity, mSelected ?
Michael Kolbc7485ae2010-09-03 10:10:58 -0700395 R.style.TabTitleSelected : R.style.TabTitleUnselected);
396 setHorizontalFadingEdgeEnabled(!mSelected);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700397 super.setActivated(selected);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800398 LayoutParams lp = (LinearLayout.LayoutParams) getLayoutParams();
399 lp.width = selected ? mTabWidthSelected : mTabWidthUnselected;
400 lp.height = LayoutParams.MATCH_PARENT;
401 setLayoutParams(lp);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700402 }
403
404 void setDisplayTitle(String title) {
405 mTitle.setText(title);
406 }
407
408 void setFavicon(Drawable d) {
409 mIconView.setImageDrawable(d);
410 }
411
412 void setLock(Drawable d) {
413 if (null == d) {
414 mLock.setVisibility(View.GONE);
415 } else {
416 mLock.setImageDrawable(d);
417 mLock.setVisibility(View.VISIBLE);
418 }
419 }
420
Michael Kolba2b2ba82010-08-04 17:54:03 -0700421 void setProgress(int newProgress) {
422 if (newProgress >= PROGRESS_MAX) {
423 mInLoad = false;
424 } else {
425 if (!mInLoad && getWindowToken() != null) {
426 mInLoad = true;
427 }
428 }
429 }
430
431 private void closeTab() {
Michael Kolb94827b62011-01-08 14:23:45 -0800432 if (mTab == mTabControl.getCurrentTab()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700433 mUiController.closeCurrentTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700434 } else {
Michael Kolb94827b62011-01-08 14:23:45 -0800435 mUiController.closeTab(mTab);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700436 }
437 }
438
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800439 @Override
440 protected void onLayout(boolean changed, int l, int t, int r, int b) {
441 super.onLayout(changed, l, t, r, b);
442 setTabPath(mPath, 0, 0, r - l, b - t);
443 }
Michael Kolbb4cafc52011-01-12 16:55:04 -0800444
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800445 @Override
446 protected void dispatchDraw(Canvas canvas) {
Romain Guyc8373212011-01-07 19:15:16 -0800447 if (mCurrentTextureWidth != mUi.getContentWidth() ||
448 mCurrentTextureHeight != getHeight()) {
449 mCurrentTextureWidth = mUi.getContentWidth();
450 mCurrentTextureHeight = getHeight();
451
452 if (mCurrentTextureWidth > 0 && mCurrentTextureHeight > 0) {
453 Bitmap activeTexture = getDrawableAsBitmap(mActiveDrawable,
454 mCurrentTextureWidth, mCurrentTextureHeight);
455 Bitmap inactiveTexture = getDrawableAsBitmap(mInactiveDrawable,
456 mCurrentTextureWidth, mCurrentTextureHeight);
457
458 mActiveShader = new BitmapShader(activeTexture,
459 Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
460 mActiveShaderPaint.setShader(mActiveShader);
Michael Kolbb4cafc52011-01-12 16:55:04 -0800461
Romain Guyc8373212011-01-07 19:15:16 -0800462 mInactiveShader = new BitmapShader(inactiveTexture,
463 Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
464 mInactiveShaderPaint.setShader(mInactiveShader);
465 }
466 }
Michael Kolbb4cafc52011-01-12 16:55:04 -0800467
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800468 int state = canvas.save();
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800469 getLocationInWindow(mWindowPos);
Romain Guyc8373212011-01-07 19:15:16 -0800470 Paint paint = mSelected ? mActiveShaderPaint : mInactiveShaderPaint;
471 drawClipped(canvas, paint, mPath, mWindowPos[0]);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800472 canvas.restoreToCount(state);
473 super.dispatchDraw(canvas);
474 }
475
Romain Guyc8373212011-01-07 19:15:16 -0800476 private void drawClipped(Canvas canvas, Paint paint, Path clipPath, int left) {
477 // TODO: We should change the matrix/shader only when needed
478 final Matrix matrix = mSelected ? mActiveMatrix : mInactiveMatrix;
479 matrix.setTranslate(-left, 0.0f);
480 (mSelected ? mActiveShader : mInactiveShader).setLocalMatrix(matrix);
481 canvas.drawPath(clipPath, paint);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800482 }
483
484 private void setTabPath(Path path, int l, int t, int r, int b) {
485 path.reset();
486 path.moveTo(l, b);
487 path.lineTo(l, t);
488 path.lineTo(r - mTabSliceWidth, t);
489 path.lineTo(r, b);
490 path.close();
491 }
492
Michael Kolba2b2ba82010-08-04 17:54:03 -0700493 }
494
Michael Kolb49f15832011-01-25 15:02:27 -0800495 static Drawable createFaviconBackground(Context context) {
496 PaintDrawable faviconBackground = new PaintDrawable();
497 Resources res = context.getResources();
498 faviconBackground.getPaint().setColor(context.getResources()
499 .getColor(R.color.tabFaviconBackground));
500 faviconBackground.setCornerRadius(
501 res.getDimension(R.dimen.tab_favicon_corner_radius));
502 return faviconBackground;
503 }
504
Michael Kolb94827b62011-01-08 14:23:45 -0800505 private Drawable renderFavicon(Bitmap icon) {
Michael Kolb49f15832011-01-25 15:02:27 -0800506 Drawable[] array = new Drawable[2];
507 array[0] = createFaviconBackground(getContext());
Michael Kolb94827b62011-01-08 14:23:45 -0800508 if (icon == null) {
Michael Kolb49f15832011-01-25 15:02:27 -0800509 array[1] = mGenericFavicon;
Michael Kolb94827b62011-01-08 14:23:45 -0800510 } else {
Michael Kolb49f15832011-01-25 15:02:27 -0800511 array[1] = new BitmapDrawable(icon);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700512 }
Michael Kolb94827b62011-01-08 14:23:45 -0800513 LayerDrawable d = new LayerDrawable(array);
Michael Kolb49f15832011-01-25 15:02:27 -0800514 d.setLayerInset(1, 2, 2, 2, 2);
Michael Kolb94827b62011-01-08 14:23:45 -0800515 return d;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700516 }
517
Michael Kolb2d59c322011-01-25 13:18:55 -0800518 private void animateTabOut(final Tab tab, final TabView tv) {
519 ObjectAnimator scalex = ObjectAnimator.ofFloat(tv, "scaleX", 1.0f, 0.0f);
520 ObjectAnimator scaley = ObjectAnimator.ofFloat(tv, "scaleY", 1.0f, 0.0f);
Michael Kolb49f15832011-01-25 15:02:27 -0800521 ObjectAnimator alpha = ObjectAnimator.ofFloat(tv, "alpha", 1.0f, 0.0f);
Michael Kolb2d59c322011-01-25 13:18:55 -0800522 AnimatorSet animator = new AnimatorSet();
Michael Kolb49f15832011-01-25 15:02:27 -0800523 animator.playTogether(scalex, scaley, alpha);
Michael Kolb2d59c322011-01-25 13:18:55 -0800524 animator.setDuration(150);
525 animator.addListener(new AnimatorListener() {
526
527 @Override
528 public void onAnimationCancel(Animator animation) {
529 }
530
531 @Override
532 public void onAnimationEnd(Animator animation) {
533 mTabs.removeTab(tv);
534 mTabMap.remove(tab);
Michael Kolb8814d732011-01-26 11:22:30 -0800535 mUi.onRemoveTabCompleted(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800536 }
537
538 @Override
539 public void onAnimationRepeat(Animator animation) {
540 }
541
542 @Override
543 public void onAnimationStart(Animator animation) {
544 }
545
546 });
547 animator.start();
548 }
549
550 private void animateTabIn(final Tab tab, final TabView tv) {
Michael Kolb49f15832011-01-25 15:02:27 -0800551 ObjectAnimator scalex = ObjectAnimator.ofFloat(tv, "scaleX", 0.0f, 1.0f);
Michael Kolb2d59c322011-01-25 13:18:55 -0800552 scalex.setDuration(150);
553 scalex.addListener(new AnimatorListener() {
554
555 @Override
556 public void onAnimationCancel(Animator animation) {
557 }
558
559 @Override
560 public void onAnimationEnd(Animator animation) {
Michael Kolb8814d732011-01-26 11:22:30 -0800561 mUi.onAddTabCompleted(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800562 }
563
564 @Override
565 public void onAnimationRepeat(Animator animation) {
566 }
567
568 @Override
569 public void onAnimationStart(Animator animation) {
570 mTabs.addTab(tv);
571 }
572
573 });
574 scalex.start();
575 }
576
Michael Kolba2b2ba82010-08-04 17:54:03 -0700577 // TabChangeListener implementation
578
Michael Kolb8233fac2010-10-26 16:08:53 -0700579 public void onSetActiveTab(Tab tab) {
580 mTabs.setSelectedTab(mTabControl.getTabIndex(tab));
Michael Kolb94827b62011-01-08 14:23:45 -0800581 TabView tv = mTabMap.get(tab);
582 if (tv != null) {
583 tv.setProgress(tv.mTab.getLoadProgress());
Michael Kolba2b2ba82010-08-04 17:54:03 -0700584 // update the scroll state
585 WebView webview = tab.getWebView();
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800586 if (webview != null) {
587 int h = webview.getVisibleTitleHeight();
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800588 onScroll(h);
589 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700590 }
591 }
592
Michael Kolba2b2ba82010-08-04 17:54:03 -0700593 public void onFavicon(Tab tab, Bitmap favicon) {
Michael Kolb94827b62011-01-08 14:23:45 -0800594 TabView tv = mTabMap.get(tab);
595 if (tv != null) {
596 tv.setFavicon(renderFavicon(favicon));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700597 }
598 }
599
Michael Kolba2b2ba82010-08-04 17:54:03 -0700600 public void onNewTab(Tab tab) {
Michael Kolb94827b62011-01-08 14:23:45 -0800601 TabView tv = buildTabView(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800602 animateTabIn(tab, tv);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700603 }
604
Michael Kolba2b2ba82010-08-04 17:54:03 -0700605 public void onProgress(Tab tab, int progress) {
Michael Kolb94827b62011-01-08 14:23:45 -0800606 TabView tv = mTabMap.get(tab);
607 if (tv != null) {
608 tv.setProgress(progress);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700609 }
610 }
611
Michael Kolba2b2ba82010-08-04 17:54:03 -0700612 public void onRemoveTab(Tab tab) {
Michael Kolb94827b62011-01-08 14:23:45 -0800613 TabView tv = mTabMap.get(tab);
614 if (tv != null) {
Michael Kolb2d59c322011-01-25 13:18:55 -0800615 animateTabOut(tab, tv);
616 } else {
617 mTabMap.remove(tab);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700618 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700619 }
620
Michael Kolba2b2ba82010-08-04 17:54:03 -0700621 public void onUrlAndTitle(Tab tab, String url, String title) {
Michael Kolb94827b62011-01-08 14:23:45 -0800622 TabView tv = mTabMap.get(tab);
623 if (tv != null) {
624 if (title != null) {
625 tv.setDisplayTitle(title);
626 } else if (url != null) {
627 tv.setDisplayTitle(UrlUtils.stripUrl(url));
628 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700629 }
630 }
631
Michael Kolba2b2ba82010-08-04 17:54:03 -0700632 private boolean isLoading() {
Michael Kolb94827b62011-01-08 14:23:45 -0800633 TabView tv = mTabMap.get(mTabControl.getCurrentTab());
634 if (tv != null) {
635 return tv.mInLoad;
Michael Kolb8233fac2010-10-26 16:08:53 -0700636 } else {
637 return false;
638 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700639 }
640
Michael Kolba2b2ba82010-08-04 17:54:03 -0700641}