blob: d115f1ac80efad587439bb68987bc953e648a450 [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
Michael Kolba0e2a752010-12-12 15:27:56 -080078 private int mVisibleTitleHeight;
Michael Kolba2b2ba82010-08-04 17:54:03 -070079
John Reckaff60fb2010-10-25 13:47:58 -070080 private Drawable mGenericFavicon;
81
Romain Guyc8373212011-01-07 19:15:16 -080082 private int mCurrentTextureWidth = 0;
83 private int mCurrentTextureHeight = 0;
84
Michael Kolb2b5a13a2010-12-09 14:13:42 -080085 private Drawable mActiveDrawable;
86 private Drawable mInactiveDrawable;
87
Romain Guyc8373212011-01-07 19:15:16 -080088 private final Paint mActiveShaderPaint = new Paint();
89 private final Paint mInactiveShaderPaint = new Paint();
90 private final Matrix mActiveMatrix = new Matrix();
91 private final Matrix mInactiveMatrix = new Matrix();
92
93 private BitmapShader mActiveShader;
94 private BitmapShader mInactiveShader;
95
Michael Kolb2b5a13a2010-12-09 14:13:42 -080096 private int mTabOverlap;
Michael Kolb5a72f182011-01-13 20:35:06 -080097 private int mAddTabOverlap;
Michael Kolb2b5a13a2010-12-09 14:13:42 -080098 private int mTabSliceWidth;
99 private int mTabPadding;
Michael Kolb376b5412010-12-15 11:52:57 -0800100 private boolean mUseQuickControls;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800101
Michael Kolb66706532010-12-12 19:50:22 -0800102 public TabBar(Activity activity, UiController controller, XLargeUi ui) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700103 super(activity);
104 mActivity = activity;
105 mUiController = controller;
106 mTabControl = mUiController.getTabControl();
107 mUi = ui;
108 Resources res = activity.getResources();
Michael Kolbed217742010-08-10 17:52:34 -0700109 mTabWidthSelected = (int) res.getDimension(R.dimen.tab_width_selected);
110 mTabWidthUnselected = (int) res.getDimension(R.dimen.tab_width_unselected);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800111 mActiveDrawable = res.getDrawable(R.drawable.bg_urlbar);
112 mInactiveDrawable = res.getDrawable(R.drawable.browsertab_inactive);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700113
Michael Kolb94827b62011-01-08 14:23:45 -0800114 mTabMap = new HashMap<Tab, TabView>();
115 Resources resources = activity.getResources();
Michael Kolb8233fac2010-10-26 16:08:53 -0700116 LayoutInflater factory = LayoutInflater.from(activity);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700117 factory.inflate(R.layout.tab_bar, this);
Michael Kolbf558f0d2011-01-13 19:24:18 -0800118 setPadding(0, (int) res.getDimension(R.dimen.tab_padding_top), 0, 0);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700119 mTabs = (TabScrollView) findViewById(R.id.tabs);
Michael Kolbebba8b42010-09-30 12:57:59 -0700120 mNewTab = (ImageButton) findViewById(R.id.newtab);
121 mNewTab.setOnClickListener(this);
John Reckaff60fb2010-10-25 13:47:58 -0700122 mGenericFavicon = res.getDrawable(R.drawable.app_web_browser_sm);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700123
Michael Kolb1bf23132010-11-19 12:55:12 -0800124 updateTabs(mUiController.getTabs());
Michael Kolba2b2ba82010-08-04 17:54:03 -0700125
Michael Kolba0e2a752010-12-12 15:27:56 -0800126 mVisibleTitleHeight = 1;
Michael Kolbebba8b42010-09-30 12:57:59 -0700127 mButtonWidth = -1;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800128 // tab dimensions
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800129 mTabOverlap = (int) res.getDimension(R.dimen.tab_overlap);
Michael Kolb5a72f182011-01-13 20:35:06 -0800130 mAddTabOverlap = (int) res.getDimension(R.dimen.tab_addoverlap);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800131 mTabSliceWidth = (int) res.getDimension(R.dimen.tab_slice);
132 mTabPadding = (int) res.getDimension(R.dimen.tab_padding);
Romain Guyc8373212011-01-07 19:15:16 -0800133
134 mActiveShaderPaint.setStyle(Paint.Style.FILL);
135 mActiveShaderPaint.setAntiAlias(true);
136
137 mInactiveShaderPaint.setStyle(Paint.Style.FILL);
138 mInactiveShaderPaint.setAntiAlias(true);
Michael Kolbb4cafc52011-01-12 16:55:04 -0800139
Michael Kolbebba8b42010-09-30 12:57:59 -0700140 }
141
Michael Kolb376b5412010-12-15 11:52:57 -0800142 void setUseQuickControls(boolean useQuickControls) {
143 mUseQuickControls = useQuickControls;
Michael Kolb467af0a2011-01-11 13:09:49 -0800144 mNewTab.setVisibility(mUseQuickControls ? View.GONE
145 : View.VISIBLE);
Michael Kolb376b5412010-12-15 11:52:57 -0800146 }
147
148 int getTabCount() {
149 return mTabMap.size();
150 }
151
Michael Kolb1bf23132010-11-19 12:55:12 -0800152 void updateTabs(List<Tab> tabs) {
153 mTabs.clearTabs();
154 mTabMap.clear();
155 for (Tab tab : tabs) {
Michael Kolb94827b62011-01-08 14:23:45 -0800156 TabView tv = buildTabView(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800157 mTabs.addTab(tv);
Michael Kolb1bf23132010-11-19 12:55:12 -0800158 }
159 mTabs.setSelectedTab(mTabControl.getCurrentIndex());
160 }
161
Michael Kolbebba8b42010-09-30 12:57:59 -0700162 @Override
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800163 protected void onMeasure(int hspec, int vspec) {
164 super.onMeasure(hspec, vspec);
165 int w = getMeasuredWidth();
166 // adjust for new tab overlap
Michael Kolb467af0a2011-01-11 13:09:49 -0800167 if (!mUseQuickControls) {
Michael Kolb5a72f182011-01-13 20:35:06 -0800168 w -= mAddTabOverlap;
Michael Kolb467af0a2011-01-11 13:09:49 -0800169 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800170 setMeasuredDimension(w, getMeasuredHeight());
171 }
172
173 @Override
Michael Kolbebba8b42010-09-30 12:57:59 -0700174 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800175 // use paddingLeft and paddingTop
176 int pl = getPaddingLeft();
177 int pt = getPaddingTop();
Michael Kolbebba8b42010-09-30 12:57:59 -0700178 int sw = mTabs.getMeasuredWidth();
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800179 int w = right - left - pl;
Michael Kolbb4cafc52011-01-12 16:55:04 -0800180 if (mUseQuickControls) {
181 mButtonWidth = 0;
182 } else {
Michael Kolb5a72f182011-01-13 20:35:06 -0800183 mButtonWidth = mNewTab.getMeasuredWidth() - mAddTabOverlap;
Michael Kolbb4cafc52011-01-12 16:55:04 -0800184 if (w-sw < mButtonWidth) {
185 sw = w - mButtonWidth;
186 }
Michael Kolbebba8b42010-09-30 12:57:59 -0700187 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800188 mTabs.layout(pl, pt, pl + sw, bottom - top);
189 // adjust for overlap
Michael Kolbb4cafc52011-01-12 16:55:04 -0800190 if (!mUseQuickControls) {
Michael Kolb5a72f182011-01-13 20:35:06 -0800191 mNewTab.layout(pl + sw - mAddTabOverlap, pt,
192 pl + sw + mButtonWidth - mAddTabOverlap, bottom - top);
Michael Kolbb4cafc52011-01-12 16:55:04 -0800193 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700194 }
195
196 public void onClick(View view) {
Michael Kolbebba8b42010-09-30 12:57:59 -0700197 if (mNewTab == view) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700198 mUiController.openTabToHomePage();
Michael Kolbebba8b42010-09-30 12:57:59 -0700199 } else if (mTabs.getSelectedTab() == view) {
Michael Kolb467af0a2011-01-11 13:09:49 -0800200 if (mUseQuickControls) {
201 if (mUi.isFakeTitleBarShowing() && !isLoading()) {
202 mUi.hideFakeTitleBar();
203 } else {
204 mUi.stopWebViewScrolling();
205 mUi.showFakeTitleBarAndEdit();
206 }
207 } else if (mUi.isFakeTitleBarShowing() && !isLoading()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700208 mUi.hideFakeTitleBar();
Michael Kolbed217742010-08-10 17:52:34 -0700209 } else {
210 showUrlBar();
211 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700212 } else {
Michael Kolbebba8b42010-09-30 12:57:59 -0700213 int ix = mTabs.getChildIndex(view);
214 if (ix >= 0) {
215 mTabs.setSelectedTab(ix);
Michael Kolb8233fac2010-10-26 16:08:53 -0700216 mUiController.switchToTab(ix);
Michael Kolbebba8b42010-09-30 12:57:59 -0700217 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700218 }
219 }
220
Michael Kolbed217742010-08-10 17:52:34 -0700221 private void showUrlBar() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700222 mUi.stopWebViewScrolling();
223 mUi.showFakeTitleBar();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700224 }
225
Michael Kolb376b5412010-12-15 11:52:57 -0800226 void showTitleBarIndicator(boolean show) {
Michael Kolba0e2a752010-12-12 15:27:56 -0800227 Tab tab = mTabControl.getCurrentTab();
228 if (tab != null) {
Michael Kolb94827b62011-01-08 14:23:45 -0800229 TabView tv = mTabMap.get(tab);
230 if (tv != null) {
231 tv.showIndicator(show);
Michael Kolba0e2a752010-12-12 15:27:56 -0800232 }
233 }
Michael Kolbed217742010-08-10 17:52:34 -0700234 }
235
Michael Kolb7dad8f02011-01-21 11:06:18 -0800236 boolean showsTitleBarIndicator() {
237 Tab tab = mTabControl.getCurrentTab();
238 if (tab != null) {
239 TabView tv = mTabMap.get(tab);
240 if (tv != null) {
241 return tv.showsIndicator();
242 }
243 }
244 return false;
245 }
246
Michael Kolbed217742010-08-10 17:52:34 -0700247 // callback after fake titlebar is shown
248 void onShowTitleBar() {
Michael Kolba0e2a752010-12-12 15:27:56 -0800249 showTitleBarIndicator(false);
Michael Kolbed217742010-08-10 17:52:34 -0700250 }
251
252 // callback after fake titlebar is hidden
Michael Kolba2b2ba82010-08-04 17:54:03 -0700253 void onHideTitleBar() {
Michael Kolba0e2a752010-12-12 15:27:56 -0800254 showTitleBarIndicator(mVisibleTitleHeight == 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700255 Tab tab = mTabControl.getCurrentTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700256 tab.getWebView().requestFocus();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700257 }
258
Michael Kolbed217742010-08-10 17:52:34 -0700259 // webview scroll listener
260
261 @Override
Michael Kolba0e2a752010-12-12 15:27:56 -0800262 public void onScroll(int visibleTitleHeight) {
Michael Kolb376b5412010-12-15 11:52:57 -0800263 if (mUseQuickControls) return;
Michael Kolb8233fac2010-10-26 16:08:53 -0700264 // isLoading is using the current tab, which initially might not be set yet
John Reckdcf911c2010-12-22 16:32:31 -0800265 if (mTabControl.getCurrentTab() != null
266 && !isLoading()) {
267 if (visibleTitleHeight == 0) {
Michael Kolb7dad8f02011-01-21 11:06:18 -0800268 if (!showsTitleBarIndicator()) {
269 mUi.hideFakeTitleBar();
270 showTitleBarIndicator(true);
271 }
John Reckdcf911c2010-12-22 16:32:31 -0800272 } else {
Michael Kolb7dad8f02011-01-21 11:06:18 -0800273 if (showsTitleBarIndicator()) {
274 showTitleBarIndicator(false);
275 }
Michael Kolbed217742010-08-10 17:52:34 -0700276 }
277 }
Michael Kolba0e2a752010-12-12 15:27:56 -0800278 mVisibleTitleHeight = visibleTitleHeight;
Michael Kolbed217742010-08-10 17:52:34 -0700279 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700280
281 @Override
282 public void createContextMenu(ContextMenu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700283 MenuInflater inflater = mActivity.getMenuInflater();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700284 inflater.inflate(R.menu.title_context, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -0700285 mActivity.onCreateContextMenu(menu, this, null);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700286 }
287
Michael Kolb94827b62011-01-08 14:23:45 -0800288 private TabView buildTabView(Tab tab) {
289 TabView tabview = new TabView(mActivity, tab);
290 mTabMap.put(tab, tabview);
291 tabview.setOnClickListener(this);
Michael Kolb94827b62011-01-08 14:23:45 -0800292 return tabview;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700293 }
294
Romain Guyc8373212011-01-07 19:15:16 -0800295 private static Bitmap getDrawableAsBitmap(Drawable drawable, int width, int height) {
296 Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
297 Canvas c = new Canvas(b);
298 drawable.setBounds(0, 0, width, height);
299 drawable.draw(c);
300 return b;
Michael Kolbb4cafc52011-01-12 16:55:04 -0800301 }
302
Michael Kolba2b2ba82010-08-04 17:54:03 -0700303 /**
304 * View used in the tab bar
305 */
306 class TabView extends LinearLayout implements OnClickListener {
307
Michael Kolb94827b62011-01-08 14:23:45 -0800308 Tab mTab;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700309 View mTabContent;
310 TextView mTitle;
Michael Kolba0e2a752010-12-12 15:27:56 -0800311 View mIndicator;
Michael Kolbae62fd42010-08-18 16:33:28 -0700312 View mIncognito;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700313 ImageView mIconView;
314 ImageView mLock;
315 ImageView mClose;
316 boolean mSelected;
317 boolean mInLoad;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800318 Path mPath;
319 int[] mWindowPos;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700320
321 /**
322 * @param context
323 */
Michael Kolb94827b62011-01-08 14:23:45 -0800324 public TabView(Context context, Tab tab) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700325 super(context);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800326 setWillNotDraw(false);
327 mPath = new Path();
328 mWindowPos = new int[2];
Michael Kolb94827b62011-01-08 14:23:45 -0800329 mTab = tab;
Michael Kolbed217742010-08-10 17:52:34 -0700330 setGravity(Gravity.CENTER_VERTICAL);
331 setOrientation(LinearLayout.HORIZONTAL);
Michael Kolb49f15832011-01-25 15:02:27 -0800332 setPadding(mTabOverlap, 0, mTabSliceWidth, 0);
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100333 LayoutInflater inflater = LayoutInflater.from(getContext());
Michael Kolbed217742010-08-10 17:52:34 -0700334 mTabContent = inflater.inflate(R.layout.tab_title, this, true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700335 mTitle = (TextView) mTabContent.findViewById(R.id.title);
336 mIconView = (ImageView) mTabContent.findViewById(R.id.favicon);
337 mLock = (ImageView) mTabContent.findViewById(R.id.lock);
338 mClose = (ImageView) mTabContent.findViewById(R.id.close);
339 mClose.setOnClickListener(this);
Michael Kolbae62fd42010-08-18 16:33:28 -0700340 mIncognito = mTabContent.findViewById(R.id.incognito);
Michael Kolba0e2a752010-12-12 15:27:56 -0800341 mIndicator = mTabContent.findViewById(R.id.chevron);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700342 mSelected = false;
343 mInLoad = false;
344 // update the status
Michael Kolb94827b62011-01-08 14:23:45 -0800345 updateFromTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700346 }
347
Michael Kolba0e2a752010-12-12 15:27:56 -0800348 void showIndicator(boolean show) {
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800349 if (mSelected) {
350 mIndicator.setVisibility(show ? View.VISIBLE : View.GONE);
Michael Kolb49f15832011-01-25 15:02:27 -0800351 LayoutParams lp = (LinearLayout.LayoutParams) getLayoutParams();
352 if (show) {
353 lp.width = mTabWidthSelected + mIndicator.getWidth();
354 } else {
355 lp.width = mTabWidthSelected;
356 }
357 lp.height = LayoutParams.MATCH_PARENT;
358 setLayoutParams(lp);
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800359 } else {
360 mIndicator.setVisibility(View.GONE);
361 }
Michael Kolba0e2a752010-12-12 15:27:56 -0800362 }
363
Michael Kolb7dad8f02011-01-21 11:06:18 -0800364 boolean showsIndicator() {
365 return (mIndicator.getVisibility() == View.VISIBLE);
366 }
367
Michael Kolba2b2ba82010-08-04 17:54:03 -0700368 @Override
369 public void onClick(View v) {
370 if (v == mClose) {
371 closeTab();
372 }
373 }
374
Michael Kolb94827b62011-01-08 14:23:45 -0800375 private void updateFromTab() {
376 String displayTitle = mTab.getTitle();
John Reck30c714c2010-12-16 17:30:34 -0800377 if (displayTitle == null) {
Michael Kolb94827b62011-01-08 14:23:45 -0800378 displayTitle = mTab.getUrl();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700379 }
John Reck30c714c2010-12-16 17:30:34 -0800380 setDisplayTitle(displayTitle);
Michael Kolb94827b62011-01-08 14:23:45 -0800381 setProgress(mTab.getLoadProgress());
382 if (mTab.getFavicon() != null) {
383 setFavicon(renderFavicon(mTab.getFavicon()));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700384 }
Michael Kolb94827b62011-01-08 14:23:45 -0800385 if (mTab != null) {
Michael Kolbae62fd42010-08-18 16:33:28 -0700386 mIncognito.setVisibility(
Michael Kolb94827b62011-01-08 14:23:45 -0800387 mTab.isPrivateBrowsingEnabled() ?
Michael Kolbae62fd42010-08-18 16:33:28 -0700388 View.VISIBLE : View.GONE);
389 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700390 }
391
392 @Override
Michael Kolbb7b115e2010-09-25 16:59:37 -0700393 public void setActivated(boolean selected) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700394 mSelected = selected;
395 mClose.setVisibility(mSelected ? View.VISIBLE : View.GONE);
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800396 mIndicator.setVisibility(View.GONE);
Michael Kolb8233fac2010-10-26 16:08:53 -0700397 mTitle.setTextAppearance(mActivity, mSelected ?
Michael Kolbc7485ae2010-09-03 10:10:58 -0700398 R.style.TabTitleSelected : R.style.TabTitleUnselected);
399 setHorizontalFadingEdgeEnabled(!mSelected);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700400 super.setActivated(selected);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800401 LayoutParams lp = (LinearLayout.LayoutParams) getLayoutParams();
402 lp.width = selected ? mTabWidthSelected : mTabWidthUnselected;
403 lp.height = LayoutParams.MATCH_PARENT;
404 setLayoutParams(lp);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700405 }
406
407 void setDisplayTitle(String title) {
408 mTitle.setText(title);
409 }
410
411 void setFavicon(Drawable d) {
412 mIconView.setImageDrawable(d);
413 }
414
415 void setLock(Drawable d) {
416 if (null == d) {
417 mLock.setVisibility(View.GONE);
418 } else {
419 mLock.setImageDrawable(d);
420 mLock.setVisibility(View.VISIBLE);
421 }
422 }
423
Michael Kolba2b2ba82010-08-04 17:54:03 -0700424 void setProgress(int newProgress) {
425 if (newProgress >= PROGRESS_MAX) {
426 mInLoad = false;
427 } else {
428 if (!mInLoad && getWindowToken() != null) {
429 mInLoad = true;
430 }
431 }
432 }
433
434 private void closeTab() {
Michael Kolb94827b62011-01-08 14:23:45 -0800435 if (mTab == mTabControl.getCurrentTab()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700436 mUiController.closeCurrentTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700437 } else {
Michael Kolb94827b62011-01-08 14:23:45 -0800438 mUiController.closeTab(mTab);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700439 }
440 }
441
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800442 @Override
443 protected void onLayout(boolean changed, int l, int t, int r, int b) {
444 super.onLayout(changed, l, t, r, b);
445 setTabPath(mPath, 0, 0, r - l, b - t);
446 }
Michael Kolbb4cafc52011-01-12 16:55:04 -0800447
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800448 @Override
449 protected void dispatchDraw(Canvas canvas) {
Romain Guyc8373212011-01-07 19:15:16 -0800450 if (mCurrentTextureWidth != mUi.getContentWidth() ||
451 mCurrentTextureHeight != getHeight()) {
452 mCurrentTextureWidth = mUi.getContentWidth();
453 mCurrentTextureHeight = getHeight();
454
455 if (mCurrentTextureWidth > 0 && mCurrentTextureHeight > 0) {
456 Bitmap activeTexture = getDrawableAsBitmap(mActiveDrawable,
457 mCurrentTextureWidth, mCurrentTextureHeight);
458 Bitmap inactiveTexture = getDrawableAsBitmap(mInactiveDrawable,
459 mCurrentTextureWidth, mCurrentTextureHeight);
460
461 mActiveShader = new BitmapShader(activeTexture,
462 Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
463 mActiveShaderPaint.setShader(mActiveShader);
Michael Kolbb4cafc52011-01-12 16:55:04 -0800464
Romain Guyc8373212011-01-07 19:15:16 -0800465 mInactiveShader = new BitmapShader(inactiveTexture,
466 Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
467 mInactiveShaderPaint.setShader(mInactiveShader);
468 }
469 }
Michael Kolbb4cafc52011-01-12 16:55:04 -0800470
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800471 int state = canvas.save();
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800472 getLocationInWindow(mWindowPos);
Romain Guyc8373212011-01-07 19:15:16 -0800473 Paint paint = mSelected ? mActiveShaderPaint : mInactiveShaderPaint;
474 drawClipped(canvas, paint, mPath, mWindowPos[0]);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800475 canvas.restoreToCount(state);
476 super.dispatchDraw(canvas);
477 }
478
Romain Guyc8373212011-01-07 19:15:16 -0800479 private void drawClipped(Canvas canvas, Paint paint, Path clipPath, int left) {
480 // TODO: We should change the matrix/shader only when needed
481 final Matrix matrix = mSelected ? mActiveMatrix : mInactiveMatrix;
482 matrix.setTranslate(-left, 0.0f);
483 (mSelected ? mActiveShader : mInactiveShader).setLocalMatrix(matrix);
484 canvas.drawPath(clipPath, paint);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800485 }
486
487 private void setTabPath(Path path, int l, int t, int r, int b) {
488 path.reset();
489 path.moveTo(l, b);
490 path.lineTo(l, t);
491 path.lineTo(r - mTabSliceWidth, t);
492 path.lineTo(r, b);
493 path.close();
494 }
495
Michael Kolba2b2ba82010-08-04 17:54:03 -0700496 }
497
Michael Kolb49f15832011-01-25 15:02:27 -0800498 static Drawable createFaviconBackground(Context context) {
499 PaintDrawable faviconBackground = new PaintDrawable();
500 Resources res = context.getResources();
501 faviconBackground.getPaint().setColor(context.getResources()
502 .getColor(R.color.tabFaviconBackground));
503 faviconBackground.setCornerRadius(
504 res.getDimension(R.dimen.tab_favicon_corner_radius));
505 return faviconBackground;
506 }
507
Michael Kolb94827b62011-01-08 14:23:45 -0800508 private Drawable renderFavicon(Bitmap icon) {
Michael Kolb49f15832011-01-25 15:02:27 -0800509 Drawable[] array = new Drawable[2];
510 array[0] = createFaviconBackground(getContext());
Michael Kolb94827b62011-01-08 14:23:45 -0800511 if (icon == null) {
Michael Kolb49f15832011-01-25 15:02:27 -0800512 array[1] = mGenericFavicon;
Michael Kolb94827b62011-01-08 14:23:45 -0800513 } else {
Michael Kolb49f15832011-01-25 15:02:27 -0800514 array[1] = new BitmapDrawable(icon);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700515 }
Michael Kolb94827b62011-01-08 14:23:45 -0800516 LayerDrawable d = new LayerDrawable(array);
Michael Kolb49f15832011-01-25 15:02:27 -0800517 d.setLayerInset(1, 2, 2, 2, 2);
Michael Kolb94827b62011-01-08 14:23:45 -0800518 return d;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700519 }
520
Michael Kolb2d59c322011-01-25 13:18:55 -0800521 private void animateTabOut(final Tab tab, final TabView tv) {
522 ObjectAnimator scalex = ObjectAnimator.ofFloat(tv, "scaleX", 1.0f, 0.0f);
523 ObjectAnimator scaley = ObjectAnimator.ofFloat(tv, "scaleY", 1.0f, 0.0f);
Michael Kolb49f15832011-01-25 15:02:27 -0800524 ObjectAnimator alpha = ObjectAnimator.ofFloat(tv, "alpha", 1.0f, 0.0f);
Michael Kolb2d59c322011-01-25 13:18:55 -0800525 AnimatorSet animator = new AnimatorSet();
Michael Kolb49f15832011-01-25 15:02:27 -0800526 animator.playTogether(scalex, scaley, alpha);
Michael Kolb2d59c322011-01-25 13:18:55 -0800527 animator.setDuration(150);
528 animator.addListener(new AnimatorListener() {
529
530 @Override
531 public void onAnimationCancel(Animator animation) {
532 }
533
534 @Override
535 public void onAnimationEnd(Animator animation) {
536 mTabs.removeTab(tv);
537 mTabMap.remove(tab);
Michael Kolb8814d732011-01-26 11:22:30 -0800538 mUi.onRemoveTabCompleted(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800539 }
540
541 @Override
542 public void onAnimationRepeat(Animator animation) {
543 }
544
545 @Override
546 public void onAnimationStart(Animator animation) {
547 }
548
549 });
550 animator.start();
551 }
552
553 private void animateTabIn(final Tab tab, final TabView tv) {
Michael Kolb49f15832011-01-25 15:02:27 -0800554 ObjectAnimator scalex = ObjectAnimator.ofFloat(tv, "scaleX", 0.0f, 1.0f);
Michael Kolb2d59c322011-01-25 13:18:55 -0800555 scalex.setDuration(150);
556 scalex.addListener(new AnimatorListener() {
557
558 @Override
559 public void onAnimationCancel(Animator animation) {
560 }
561
562 @Override
563 public void onAnimationEnd(Animator animation) {
Michael Kolb8814d732011-01-26 11:22:30 -0800564 mUi.onAddTabCompleted(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800565 }
566
567 @Override
568 public void onAnimationRepeat(Animator animation) {
569 }
570
571 @Override
572 public void onAnimationStart(Animator animation) {
573 mTabs.addTab(tv);
574 }
575
576 });
577 scalex.start();
578 }
579
Michael Kolba2b2ba82010-08-04 17:54:03 -0700580 // TabChangeListener implementation
581
Michael Kolb8233fac2010-10-26 16:08:53 -0700582 public void onSetActiveTab(Tab tab) {
583 mTabs.setSelectedTab(mTabControl.getTabIndex(tab));
Michael Kolb94827b62011-01-08 14:23:45 -0800584 TabView tv = mTabMap.get(tab);
585 if (tv != null) {
586 tv.setProgress(tv.mTab.getLoadProgress());
Michael Kolba2b2ba82010-08-04 17:54:03 -0700587 // update the scroll state
588 WebView webview = tab.getWebView();
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800589 if (webview != null) {
590 int h = webview.getVisibleTitleHeight();
591 mVisibleTitleHeight = h -1;
592 onScroll(h);
593 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700594 }
595 }
596
Michael Kolba2b2ba82010-08-04 17:54:03 -0700597 public void onFavicon(Tab tab, Bitmap favicon) {
Michael Kolb94827b62011-01-08 14:23:45 -0800598 TabView tv = mTabMap.get(tab);
599 if (tv != null) {
600 tv.setFavicon(renderFavicon(favicon));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700601 }
602 }
603
Michael Kolba2b2ba82010-08-04 17:54:03 -0700604 public void onNewTab(Tab tab) {
Michael Kolb94827b62011-01-08 14:23:45 -0800605 TabView tv = buildTabView(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800606 animateTabIn(tab, tv);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700607 }
608
Michael Kolba2b2ba82010-08-04 17:54:03 -0700609 public void onProgress(Tab tab, int progress) {
Michael Kolb94827b62011-01-08 14:23:45 -0800610 TabView tv = mTabMap.get(tab);
611 if (tv != null) {
612 tv.setProgress(progress);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700613 }
614 }
615
Michael Kolba2b2ba82010-08-04 17:54:03 -0700616 public void onRemoveTab(Tab tab) {
Michael Kolb94827b62011-01-08 14:23:45 -0800617 TabView tv = mTabMap.get(tab);
618 if (tv != null) {
Michael Kolb2d59c322011-01-25 13:18:55 -0800619 animateTabOut(tab, tv);
620 } else {
621 mTabMap.remove(tab);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700622 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700623 }
624
Michael Kolba2b2ba82010-08-04 17:54:03 -0700625 public void onUrlAndTitle(Tab tab, String url, String title) {
Michael Kolb94827b62011-01-08 14:23:45 -0800626 TabView tv = mTabMap.get(tab);
627 if (tv != null) {
628 if (title != null) {
629 tv.setDisplayTitle(title);
630 } else if (url != null) {
631 tv.setDisplayTitle(UrlUtils.stripUrl(url));
632 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700633 }
634 }
635
Michael Kolba2b2ba82010-08-04 17:54:03 -0700636 private boolean isLoading() {
Michael Kolb94827b62011-01-08 14:23:45 -0800637 TabView tv = mTabMap.get(mTabControl.getCurrentTab());
638 if (tv != null) {
639 return tv.mInLoad;
Michael Kolb8233fac2010-10-26 16:08:53 -0700640 } else {
641 return false;
642 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700643 }
644
Michael Kolba2b2ba82010-08-04 17:54:03 -0700645}