blob: 6c3949ac403ec50a39a31be711b676a7d33d51ac [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;
Michael Kolb678afc82011-05-17 14:52:41 -070027import android.content.res.Configuration;
Michael Kolba2b2ba82010-08-04 17:54:03 -070028import android.content.res.Resources;
29import android.graphics.Bitmap;
Michael Kolb2b5a13a2010-12-09 14:13:42 -080030import android.graphics.BitmapShader;
31import android.graphics.Canvas;
Michael Kolb467af0a2011-01-11 13:09:49 -080032import android.graphics.Matrix;
Michael Kolb2b5a13a2010-12-09 14:13:42 -080033import android.graphics.Paint;
34import android.graphics.Path;
35import android.graphics.Shader;
Michael Kolba2b2ba82010-08-04 17:54:03 -070036import android.graphics.drawable.BitmapDrawable;
37import android.graphics.drawable.Drawable;
38import android.graphics.drawable.LayerDrawable;
39import android.graphics.drawable.PaintDrawable;
40import android.view.ContextMenu;
Michael Kolbed217742010-08-10 17:52:34 -070041import android.view.Gravity;
Michael Kolba2b2ba82010-08-04 17:54:03 -070042import android.view.LayoutInflater;
43import android.view.MenuInflater;
44import android.view.View;
45import android.view.View.OnClickListener;
46import android.webkit.WebView;
Michael Kolbed217742010-08-10 17:52:34 -070047import android.widget.ImageButton;
Michael Kolba2b2ba82010-08-04 17:54:03 -070048import android.widget.ImageView;
49import android.widget.LinearLayout;
50import android.widget.TextView;
51
Michael Kolba2b2ba82010-08-04 17:54:03 -070052import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -080053import java.util.List;
Michael Kolba2b2ba82010-08-04 17:54:03 -070054import java.util.Map;
55
56/**
57 * tabbed title bar for xlarge screen browser
58 */
59public class TabBar extends LinearLayout
Michael Kolb8233fac2010-10-26 16:08:53 -070060 implements ScrollListener, OnClickListener {
Michael Kolba2b2ba82010-08-04 17:54:03 -070061
62 private static final int PROGRESS_MAX = 100;
63
Michael Kolb8233fac2010-10-26 16:08:53 -070064 private Activity mActivity;
65 private UiController mUiController;
66 private TabControl mTabControl;
Michael Kolb66706532010-12-12 19:50:22 -080067 private XLargeUi mUi;
Michael Kolba2b2ba82010-08-04 17:54:03 -070068
Michael Kolb678afc82011-05-17 14:52:41 -070069 private int mTabWidthSelected;
70 private int mTabWidthUnselected;
Michael Kolbed217742010-08-10 17:52:34 -070071
Michael Kolba2b2ba82010-08-04 17:54:03 -070072 private TabScrollView mTabs;
Michael Kolb8233fac2010-10-26 16:08:53 -070073
Michael Kolbebba8b42010-09-30 12:57:59 -070074 private ImageButton mNewTab;
75 private int mButtonWidth;
Michael Kolba2b2ba82010-08-04 17:54:03 -070076
Michael Kolb94827b62011-01-08 14:23:45 -080077 private Map<Tab, TabView> mTabMap;
Michael Kolba2b2ba82010-08-04 17:54:03 -070078
John Reckaff60fb2010-10-25 13:47:58 -070079 private Drawable mGenericFavicon;
80
Romain Guyc8373212011-01-07 19:15:16 -080081 private int mCurrentTextureWidth = 0;
82 private int mCurrentTextureHeight = 0;
83
Michael Kolb2b5a13a2010-12-09 14:13:42 -080084 private Drawable mActiveDrawable;
85 private Drawable mInactiveDrawable;
86
Romain Guyc8373212011-01-07 19:15:16 -080087 private final Paint mActiveShaderPaint = new Paint();
88 private final Paint mInactiveShaderPaint = new Paint();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080089 private final Paint mFocusPaint = new Paint();
Romain Guyc8373212011-01-07 19:15:16 -080090 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;
Michael Kolb376b5412010-12-15 11:52:57 -080099 private boolean mUseQuickControls;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800100
Michael Kolb66706532010-12-12 19:50:22 -0800101 public TabBar(Activity activity, UiController controller, XLargeUi ui) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700102 super(activity);
103 mActivity = activity;
104 mUiController = controller;
105 mTabControl = mUiController.getTabControl();
106 mUi = ui;
107 Resources res = activity.getResources();
Michael Kolbed217742010-08-10 17:52:34 -0700108 mTabWidthSelected = (int) res.getDimension(R.dimen.tab_width_selected);
109 mTabWidthUnselected = (int) res.getDimension(R.dimen.tab_width_unselected);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800110 mActiveDrawable = res.getDrawable(R.drawable.bg_urlbar);
111 mInactiveDrawable = res.getDrawable(R.drawable.browsertab_inactive);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700112
Michael Kolb94827b62011-01-08 14:23:45 -0800113 mTabMap = new HashMap<Tab, TabView>();
114 Resources resources = activity.getResources();
Michael Kolb8233fac2010-10-26 16:08:53 -0700115 LayoutInflater factory = LayoutInflater.from(activity);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700116 factory.inflate(R.layout.tab_bar, this);
Michael Kolbf558f0d2011-01-13 19:24:18 -0800117 setPadding(0, (int) res.getDimension(R.dimen.tab_padding_top), 0, 0);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700118 mTabs = (TabScrollView) findViewById(R.id.tabs);
Michael Kolbebba8b42010-09-30 12:57:59 -0700119 mNewTab = (ImageButton) findViewById(R.id.newtab);
120 mNewTab.setOnClickListener(this);
John Reckaff60fb2010-10-25 13:47:58 -0700121 mGenericFavicon = res.getDrawable(R.drawable.app_web_browser_sm);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700122
Michael Kolb1bf23132010-11-19 12:55:12 -0800123 updateTabs(mUiController.getTabs());
Michael Kolbebba8b42010-09-30 12:57:59 -0700124 mButtonWidth = -1;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800125 // tab dimensions
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800126 mTabOverlap = (int) res.getDimension(R.dimen.tab_overlap);
Michael Kolb5a72f182011-01-13 20:35:06 -0800127 mAddTabOverlap = (int) res.getDimension(R.dimen.tab_addoverlap);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800128 mTabSliceWidth = (int) res.getDimension(R.dimen.tab_slice);
Romain Guyc8373212011-01-07 19:15:16 -0800129
130 mActiveShaderPaint.setStyle(Paint.Style.FILL);
131 mActiveShaderPaint.setAntiAlias(true);
132
133 mInactiveShaderPaint.setStyle(Paint.Style.FILL);
134 mInactiveShaderPaint.setAntiAlias(true);
Michael Kolbb4cafc52011-01-12 16:55:04 -0800135
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800136 mFocusPaint.setStyle(Paint.Style.STROKE);
Michael Kolbeb7001c2011-02-16 16:07:33 -0800137 mFocusPaint.setStrokeWidth(res.getDimension(R.dimen.tab_focus_stroke));
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800138 mFocusPaint.setAntiAlias(true);
139 mFocusPaint.setColor(res.getColor(R.color.tabFocusHighlight));
Michael Kolbebba8b42010-09-30 12:57:59 -0700140 }
141
Michael Kolb678afc82011-05-17 14:52:41 -0700142 @Override
143 public void onConfigurationChanged(Configuration config) {
144 super.onConfigurationChanged(config);
145 Resources res = mActivity.getResources();
146 mTabWidthSelected = (int) res.getDimension(R.dimen.tab_width_selected);
147 mTabWidthUnselected = (int) res.getDimension(R.dimen.tab_width_unselected);
148 // force update of tab bar
149 mTabs.updateLayout();
150 }
151
Michael Kolb376b5412010-12-15 11:52:57 -0800152 void setUseQuickControls(boolean useQuickControls) {
153 mUseQuickControls = useQuickControls;
Michael Kolb467af0a2011-01-11 13:09:49 -0800154 mNewTab.setVisibility(mUseQuickControls ? View.GONE
155 : View.VISIBLE);
Michael Kolb376b5412010-12-15 11:52:57 -0800156 }
157
158 int getTabCount() {
159 return mTabMap.size();
160 }
161
Michael Kolb1bf23132010-11-19 12:55:12 -0800162 void updateTabs(List<Tab> tabs) {
163 mTabs.clearTabs();
164 mTabMap.clear();
165 for (Tab tab : tabs) {
Michael Kolb94827b62011-01-08 14:23:45 -0800166 TabView tv = buildTabView(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800167 mTabs.addTab(tv);
Michael Kolb1bf23132010-11-19 12:55:12 -0800168 }
Michael Kolbc831b632011-05-11 09:30:34 -0700169 mTabs.setSelectedTab(mTabControl.getCurrentPosition());
Michael Kolb1bf23132010-11-19 12:55:12 -0800170 }
171
Michael Kolbebba8b42010-09-30 12:57:59 -0700172 @Override
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800173 protected void onMeasure(int hspec, int vspec) {
174 super.onMeasure(hspec, vspec);
175 int w = getMeasuredWidth();
176 // adjust for new tab overlap
Michael Kolb467af0a2011-01-11 13:09:49 -0800177 if (!mUseQuickControls) {
Michael Kolb5a72f182011-01-13 20:35:06 -0800178 w -= mAddTabOverlap;
Michael Kolb467af0a2011-01-11 13:09:49 -0800179 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800180 setMeasuredDimension(w, getMeasuredHeight());
181 }
182
183 @Override
Michael Kolbebba8b42010-09-30 12:57:59 -0700184 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800185 // use paddingLeft and paddingTop
186 int pl = getPaddingLeft();
187 int pt = getPaddingTop();
Michael Kolbebba8b42010-09-30 12:57:59 -0700188 int sw = mTabs.getMeasuredWidth();
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800189 int w = right - left - pl;
Michael Kolbb4cafc52011-01-12 16:55:04 -0800190 if (mUseQuickControls) {
191 mButtonWidth = 0;
192 } else {
Michael Kolb5a72f182011-01-13 20:35:06 -0800193 mButtonWidth = mNewTab.getMeasuredWidth() - mAddTabOverlap;
Michael Kolbb4cafc52011-01-12 16:55:04 -0800194 if (w-sw < mButtonWidth) {
195 sw = w - mButtonWidth;
196 }
Michael Kolbebba8b42010-09-30 12:57:59 -0700197 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800198 mTabs.layout(pl, pt, pl + sw, bottom - top);
199 // adjust for overlap
Michael Kolbb4cafc52011-01-12 16:55:04 -0800200 if (!mUseQuickControls) {
Michael Kolb5a72f182011-01-13 20:35:06 -0800201 mNewTab.layout(pl + sw - mAddTabOverlap, pt,
202 pl + sw + mButtonWidth - mAddTabOverlap, bottom - top);
Michael Kolbb4cafc52011-01-12 16:55:04 -0800203 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700204 }
205
206 public void onClick(View view) {
Michael Kolbebba8b42010-09-30 12:57:59 -0700207 if (mNewTab == view) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700208 mUiController.openTabToHomePage();
Michael Kolbebba8b42010-09-30 12:57:59 -0700209 } else if (mTabs.getSelectedTab() == view) {
Michael Kolb467af0a2011-01-11 13:09:49 -0800210 if (mUseQuickControls) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800211 if (mUi.isTitleBarShowing() && !isLoading()) {
212 mUi.stopEditingUrl();
213 mUi.hideTitleBar();
Michael Kolb467af0a2011-01-11 13:09:49 -0800214 } else {
215 mUi.stopWebViewScrolling();
Michael Kolb46f987e2011-04-05 10:39:10 -0700216 mUi.editUrl(false);
Michael Kolb467af0a2011-01-11 13:09:49 -0800217 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800218 } else if (mUi.isTitleBarShowing() && !isLoading()) {
219 mUi.stopEditingUrl();
220 mUi.hideTitleBar();
Michael Kolbed217742010-08-10 17:52:34 -0700221 } else {
222 showUrlBar();
223 }
Michael Kolbc831b632011-05-11 09:30:34 -0700224 } else if (view instanceof TabView) {
225 final Tab tab = ((TabView) view).mTab;
Michael Kolbebba8b42010-09-30 12:57:59 -0700226 int ix = mTabs.getChildIndex(view);
227 if (ix >= 0) {
228 mTabs.setSelectedTab(ix);
Michael Kolbc831b632011-05-11 09:30:34 -0700229 mUiController.switchToTab(tab);
Michael Kolbebba8b42010-09-30 12:57:59 -0700230 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700231 }
232 }
233
Michael Kolbed217742010-08-10 17:52:34 -0700234 private void showUrlBar() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700235 mUi.stopWebViewScrolling();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800236 mUi.showTitleBar();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700237 }
238
Michael Kolb376b5412010-12-15 11:52:57 -0800239 void showTitleBarIndicator(boolean show) {
Michael Kolba0e2a752010-12-12 15:27:56 -0800240 Tab tab = mTabControl.getCurrentTab();
241 if (tab != null) {
Michael Kolb94827b62011-01-08 14:23:45 -0800242 TabView tv = mTabMap.get(tab);
243 if (tv != null) {
244 tv.showIndicator(show);
Michael Kolba0e2a752010-12-12 15:27:56 -0800245 }
246 }
Michael Kolbed217742010-08-10 17:52:34 -0700247 }
248
Michael Kolb7dad8f02011-01-21 11:06:18 -0800249 boolean showsTitleBarIndicator() {
250 Tab tab = mTabControl.getCurrentTab();
251 if (tab != null) {
252 TabView tv = mTabMap.get(tab);
253 if (tv != null) {
254 return tv.showsIndicator();
255 }
256 }
257 return false;
258 }
259
Michael Kolbed217742010-08-10 17:52:34 -0700260 // callback after fake titlebar is shown
261 void onShowTitleBar() {
Michael Kolba0e2a752010-12-12 15:27:56 -0800262 showTitleBarIndicator(false);
Michael Kolbed217742010-08-10 17:52:34 -0700263 }
264
265 // callback after fake titlebar is hidden
Michael Kolba2b2ba82010-08-04 17:54:03 -0700266 void onHideTitleBar() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700267 Tab tab = mTabControl.getCurrentTab();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800268 WebView w = tab.getWebView();
269 if (w != null) {
270 showTitleBarIndicator(w.getVisibleTitleHeight() == 0);
271 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700272 }
273
Michael Kolbed217742010-08-10 17:52:34 -0700274 // webview scroll listener
275
276 @Override
Michael Kolb5ee018e2011-02-18 15:47:21 -0800277 public void onScroll(int visibleTitleHeight, boolean userInitiated) {
Michael Kolb376b5412010-12-15 11:52:57 -0800278 if (mUseQuickControls) return;
Michael Kolb8233fac2010-10-26 16:08:53 -0700279 // isLoading is using the current tab, which initially might not be set yet
John Reckdcf911c2010-12-22 16:32:31 -0800280 if (mTabControl.getCurrentTab() != null
281 && !isLoading()) {
282 if (visibleTitleHeight == 0) {
Michael Kolb5ee018e2011-02-18 15:47:21 -0800283 if (!showsTitleBarIndicator()
284 && (!mUi.isEditingUrl() || userInitiated)) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800285 mUi.hideTitleBar();
Michael Kolb7dad8f02011-01-21 11:06:18 -0800286 showTitleBarIndicator(true);
287 }
John Reckdcf911c2010-12-22 16:32:31 -0800288 } else {
Michael Kolb7dad8f02011-01-21 11:06:18 -0800289 if (showsTitleBarIndicator()) {
290 showTitleBarIndicator(false);
291 }
Michael Kolbed217742010-08-10 17:52:34 -0700292 }
293 }
294 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700295
296 @Override
297 public void createContextMenu(ContextMenu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700298 MenuInflater inflater = mActivity.getMenuInflater();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700299 inflater.inflate(R.menu.title_context, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -0700300 mActivity.onCreateContextMenu(menu, this, null);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700301 }
302
Michael Kolb94827b62011-01-08 14:23:45 -0800303 private TabView buildTabView(Tab tab) {
304 TabView tabview = new TabView(mActivity, tab);
305 mTabMap.put(tab, tabview);
306 tabview.setOnClickListener(this);
Michael Kolb94827b62011-01-08 14:23:45 -0800307 return tabview;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700308 }
309
Romain Guyc8373212011-01-07 19:15:16 -0800310 private static Bitmap getDrawableAsBitmap(Drawable drawable, int width, int height) {
311 Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
312 Canvas c = new Canvas(b);
313 drawable.setBounds(0, 0, width, height);
314 drawable.draw(c);
315 return b;
Michael Kolbb4cafc52011-01-12 16:55:04 -0800316 }
317
Michael Kolba2b2ba82010-08-04 17:54:03 -0700318 /**
319 * View used in the tab bar
320 */
321 class TabView extends LinearLayout implements OnClickListener {
322
Michael Kolb94827b62011-01-08 14:23:45 -0800323 Tab mTab;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700324 View mTabContent;
325 TextView mTitle;
Michael Kolba0e2a752010-12-12 15:27:56 -0800326 View mIndicator;
Michael Kolbae62fd42010-08-18 16:33:28 -0700327 View mIncognito;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700328 ImageView mIconView;
329 ImageView mLock;
330 ImageView mClose;
331 boolean mSelected;
332 boolean mInLoad;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800333 Path mPath;
Michael Kolbeb7001c2011-02-16 16:07:33 -0800334 Path mFocusPath;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800335 int[] mWindowPos;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700336
337 /**
338 * @param context
339 */
Michael Kolb94827b62011-01-08 14:23:45 -0800340 public TabView(Context context, Tab tab) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700341 super(context);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800342 setWillNotDraw(false);
343 mPath = new Path();
Michael Kolbeb7001c2011-02-16 16:07:33 -0800344 mFocusPath = new Path();
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800345 mWindowPos = new int[2];
Michael Kolb94827b62011-01-08 14:23:45 -0800346 mTab = tab;
Michael Kolbed217742010-08-10 17:52:34 -0700347 setGravity(Gravity.CENTER_VERTICAL);
348 setOrientation(LinearLayout.HORIZONTAL);
Michael Kolb49f15832011-01-25 15:02:27 -0800349 setPadding(mTabOverlap, 0, mTabSliceWidth, 0);
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100350 LayoutInflater inflater = LayoutInflater.from(getContext());
Michael Kolbed217742010-08-10 17:52:34 -0700351 mTabContent = inflater.inflate(R.layout.tab_title, this, true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700352 mTitle = (TextView) mTabContent.findViewById(R.id.title);
353 mIconView = (ImageView) mTabContent.findViewById(R.id.favicon);
354 mLock = (ImageView) mTabContent.findViewById(R.id.lock);
355 mClose = (ImageView) mTabContent.findViewById(R.id.close);
356 mClose.setOnClickListener(this);
Michael Kolbae62fd42010-08-18 16:33:28 -0700357 mIncognito = mTabContent.findViewById(R.id.incognito);
Michael Kolba0e2a752010-12-12 15:27:56 -0800358 mIndicator = mTabContent.findViewById(R.id.chevron);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700359 mSelected = false;
360 mInLoad = false;
361 // update the status
Michael Kolb94827b62011-01-08 14:23:45 -0800362 updateFromTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700363 }
364
Michael Kolba0e2a752010-12-12 15:27:56 -0800365 void showIndicator(boolean show) {
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800366 if (mSelected) {
367 mIndicator.setVisibility(show ? View.VISIBLE : View.GONE);
Michael Kolb49f15832011-01-25 15:02:27 -0800368 LayoutParams lp = (LinearLayout.LayoutParams) getLayoutParams();
369 if (show) {
370 lp.width = mTabWidthSelected + mIndicator.getWidth();
371 } else {
372 lp.width = mTabWidthSelected;
373 }
374 lp.height = LayoutParams.MATCH_PARENT;
375 setLayoutParams(lp);
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800376 } else {
377 mIndicator.setVisibility(View.GONE);
378 }
Michael Kolba0e2a752010-12-12 15:27:56 -0800379 }
380
Michael Kolb7dad8f02011-01-21 11:06:18 -0800381 boolean showsIndicator() {
382 return (mIndicator.getVisibility() == View.VISIBLE);
383 }
384
Michael Kolba2b2ba82010-08-04 17:54:03 -0700385 @Override
386 public void onClick(View v) {
387 if (v == mClose) {
388 closeTab();
389 }
390 }
391
Michael Kolb94827b62011-01-08 14:23:45 -0800392 private void updateFromTab() {
393 String displayTitle = mTab.getTitle();
John Reck30c714c2010-12-16 17:30:34 -0800394 if (displayTitle == null) {
Michael Kolb94827b62011-01-08 14:23:45 -0800395 displayTitle = mTab.getUrl();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700396 }
John Reck30c714c2010-12-16 17:30:34 -0800397 setDisplayTitle(displayTitle);
Michael Kolb94827b62011-01-08 14:23:45 -0800398 setProgress(mTab.getLoadProgress());
399 if (mTab.getFavicon() != null) {
400 setFavicon(renderFavicon(mTab.getFavicon()));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700401 }
Michael Kolb94827b62011-01-08 14:23:45 -0800402 if (mTab != null) {
Michael Kolbae62fd42010-08-18 16:33:28 -0700403 mIncognito.setVisibility(
Michael Kolb94827b62011-01-08 14:23:45 -0800404 mTab.isPrivateBrowsingEnabled() ?
Michael Kolbae62fd42010-08-18 16:33:28 -0700405 View.VISIBLE : View.GONE);
406 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700407 }
408
409 @Override
Michael Kolbb7b115e2010-09-25 16:59:37 -0700410 public void setActivated(boolean selected) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700411 mSelected = selected;
412 mClose.setVisibility(mSelected ? View.VISIBLE : View.GONE);
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800413 mIndicator.setVisibility(View.GONE);
Michael Kolb8233fac2010-10-26 16:08:53 -0700414 mTitle.setTextAppearance(mActivity, mSelected ?
Michael Kolbc7485ae2010-09-03 10:10:58 -0700415 R.style.TabTitleSelected : R.style.TabTitleUnselected);
416 setHorizontalFadingEdgeEnabled(!mSelected);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700417 super.setActivated(selected);
Michael Kolb678afc82011-05-17 14:52:41 -0700418 updateLayoutParams();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800419 setFocusable(!selected);
420 postInvalidate();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700421 }
422
Michael Kolb678afc82011-05-17 14:52:41 -0700423 public void updateLayoutParams() {
424 LayoutParams lp = (LinearLayout.LayoutParams) getLayoutParams();
425 lp.width = mSelected ? mTabWidthSelected : mTabWidthUnselected;
426 lp.height = LayoutParams.MATCH_PARENT;
427 setLayoutParams(lp);
428 }
429
Michael Kolba2b2ba82010-08-04 17:54:03 -0700430 void setDisplayTitle(String title) {
431 mTitle.setText(title);
432 }
433
434 void setFavicon(Drawable d) {
435 mIconView.setImageDrawable(d);
436 }
437
438 void setLock(Drawable d) {
439 if (null == d) {
440 mLock.setVisibility(View.GONE);
441 } else {
442 mLock.setImageDrawable(d);
443 mLock.setVisibility(View.VISIBLE);
444 }
445 }
446
Michael Kolba2b2ba82010-08-04 17:54:03 -0700447 void setProgress(int newProgress) {
448 if (newProgress >= PROGRESS_MAX) {
449 mInLoad = false;
450 } else {
451 if (!mInLoad && getWindowToken() != null) {
452 mInLoad = true;
453 }
454 }
455 }
456
457 private void closeTab() {
Michael Kolb94827b62011-01-08 14:23:45 -0800458 if (mTab == mTabControl.getCurrentTab()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700459 mUiController.closeCurrentTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700460 } else {
Michael Kolb94827b62011-01-08 14:23:45 -0800461 mUiController.closeTab(mTab);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700462 }
463 }
464
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800465 @Override
466 protected void onLayout(boolean changed, int l, int t, int r, int b) {
467 super.onLayout(changed, l, t, r, b);
468 setTabPath(mPath, 0, 0, r - l, b - t);
Michael Kolbeb7001c2011-02-16 16:07:33 -0800469 setFocusPath(mFocusPath, 0, 0, r - l, b - t);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800470 }
Michael Kolbb4cafc52011-01-12 16:55:04 -0800471
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800472 @Override
473 protected void dispatchDraw(Canvas canvas) {
Romain Guyc8373212011-01-07 19:15:16 -0800474 if (mCurrentTextureWidth != mUi.getContentWidth() ||
475 mCurrentTextureHeight != getHeight()) {
476 mCurrentTextureWidth = mUi.getContentWidth();
477 mCurrentTextureHeight = getHeight();
478
479 if (mCurrentTextureWidth > 0 && mCurrentTextureHeight > 0) {
480 Bitmap activeTexture = getDrawableAsBitmap(mActiveDrawable,
481 mCurrentTextureWidth, mCurrentTextureHeight);
482 Bitmap inactiveTexture = getDrawableAsBitmap(mInactiveDrawable,
483 mCurrentTextureWidth, mCurrentTextureHeight);
484
485 mActiveShader = new BitmapShader(activeTexture,
486 Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
487 mActiveShaderPaint.setShader(mActiveShader);
Michael Kolbb4cafc52011-01-12 16:55:04 -0800488
Romain Guyc8373212011-01-07 19:15:16 -0800489 mInactiveShader = new BitmapShader(inactiveTexture,
490 Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
491 mInactiveShaderPaint.setShader(mInactiveShader);
492 }
493 }
Michael Kolb05902aa2011-02-22 17:43:38 -0800494 // add some monkey protection
495 if ((mActiveShader != null) && (mInactiveShader != null)) {
496 int state = canvas.save();
497 getLocationInWindow(mWindowPos);
498 Paint paint = mSelected ? mActiveShaderPaint : mInactiveShaderPaint;
499 drawClipped(canvas, paint, mPath, mWindowPos[0]);
500 canvas.restoreToCount(state);
501 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800502 super.dispatchDraw(canvas);
503 }
504
Romain Guyc8373212011-01-07 19:15:16 -0800505 private void drawClipped(Canvas canvas, Paint paint, Path clipPath, int left) {
506 // TODO: We should change the matrix/shader only when needed
507 final Matrix matrix = mSelected ? mActiveMatrix : mInactiveMatrix;
508 matrix.setTranslate(-left, 0.0f);
509 (mSelected ? mActiveShader : mInactiveShader).setLocalMatrix(matrix);
510 canvas.drawPath(clipPath, paint);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800511 if (isFocused()) {
Michael Kolbeb7001c2011-02-16 16:07:33 -0800512 canvas.drawPath(mFocusPath, mFocusPaint);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800513 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800514 }
515
516 private void setTabPath(Path path, int l, int t, int r, int b) {
517 path.reset();
518 path.moveTo(l, b);
519 path.lineTo(l, t);
520 path.lineTo(r - mTabSliceWidth, t);
521 path.lineTo(r, b);
522 path.close();
523 }
524
Michael Kolbeb7001c2011-02-16 16:07:33 -0800525 private void setFocusPath(Path path, int l, int t, int r, int b) {
526 path.reset();
527 path.moveTo(l, b);
528 path.lineTo(l, t);
529 path.lineTo(r - mTabSliceWidth, t);
530 path.lineTo(r, b);
531 }
532
Michael Kolba2b2ba82010-08-04 17:54:03 -0700533 }
534
Michael Kolb49f15832011-01-25 15:02:27 -0800535 static Drawable createFaviconBackground(Context context) {
536 PaintDrawable faviconBackground = new PaintDrawable();
537 Resources res = context.getResources();
538 faviconBackground.getPaint().setColor(context.getResources()
539 .getColor(R.color.tabFaviconBackground));
540 faviconBackground.setCornerRadius(
541 res.getDimension(R.dimen.tab_favicon_corner_radius));
542 return faviconBackground;
543 }
544
Michael Kolb94827b62011-01-08 14:23:45 -0800545 private Drawable renderFavicon(Bitmap icon) {
Michael Kolb49f15832011-01-25 15:02:27 -0800546 Drawable[] array = new Drawable[2];
547 array[0] = createFaviconBackground(getContext());
Michael Kolb94827b62011-01-08 14:23:45 -0800548 if (icon == null) {
Michael Kolb49f15832011-01-25 15:02:27 -0800549 array[1] = mGenericFavicon;
Michael Kolb94827b62011-01-08 14:23:45 -0800550 } else {
Michael Kolb49f15832011-01-25 15:02:27 -0800551 array[1] = new BitmapDrawable(icon);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700552 }
Michael Kolb94827b62011-01-08 14:23:45 -0800553 LayerDrawable d = new LayerDrawable(array);
Michael Kolb49f15832011-01-25 15:02:27 -0800554 d.setLayerInset(1, 2, 2, 2, 2);
Michael Kolb94827b62011-01-08 14:23:45 -0800555 return d;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700556 }
557
Michael Kolb2d59c322011-01-25 13:18:55 -0800558 private void animateTabOut(final Tab tab, final TabView tv) {
559 ObjectAnimator scalex = ObjectAnimator.ofFloat(tv, "scaleX", 1.0f, 0.0f);
560 ObjectAnimator scaley = ObjectAnimator.ofFloat(tv, "scaleY", 1.0f, 0.0f);
Michael Kolb49f15832011-01-25 15:02:27 -0800561 ObjectAnimator alpha = ObjectAnimator.ofFloat(tv, "alpha", 1.0f, 0.0f);
Michael Kolb2d59c322011-01-25 13:18:55 -0800562 AnimatorSet animator = new AnimatorSet();
Michael Kolb49f15832011-01-25 15:02:27 -0800563 animator.playTogether(scalex, scaley, alpha);
Michael Kolb2d59c322011-01-25 13:18:55 -0800564 animator.setDuration(150);
565 animator.addListener(new AnimatorListener() {
566
567 @Override
568 public void onAnimationCancel(Animator animation) {
569 }
570
571 @Override
572 public void onAnimationEnd(Animator animation) {
573 mTabs.removeTab(tv);
574 mTabMap.remove(tab);
Michael Kolb8814d732011-01-26 11:22:30 -0800575 mUi.onRemoveTabCompleted(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800576 }
577
578 @Override
579 public void onAnimationRepeat(Animator animation) {
580 }
581
582 @Override
583 public void onAnimationStart(Animator animation) {
584 }
585
586 });
587 animator.start();
588 }
589
590 private void animateTabIn(final Tab tab, final TabView tv) {
Michael Kolb49f15832011-01-25 15:02:27 -0800591 ObjectAnimator scalex = ObjectAnimator.ofFloat(tv, "scaleX", 0.0f, 1.0f);
Michael Kolb2d59c322011-01-25 13:18:55 -0800592 scalex.setDuration(150);
593 scalex.addListener(new AnimatorListener() {
594
595 @Override
596 public void onAnimationCancel(Animator animation) {
597 }
598
599 @Override
600 public void onAnimationEnd(Animator animation) {
Michael Kolb8814d732011-01-26 11:22:30 -0800601 mUi.onAddTabCompleted(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800602 }
603
604 @Override
605 public void onAnimationRepeat(Animator animation) {
606 }
607
608 @Override
609 public void onAnimationStart(Animator animation) {
610 mTabs.addTab(tv);
611 }
612
613 });
614 scalex.start();
615 }
616
Michael Kolba2b2ba82010-08-04 17:54:03 -0700617 // TabChangeListener implementation
618
Michael Kolb8233fac2010-10-26 16:08:53 -0700619 public void onSetActiveTab(Tab tab) {
Michael Kolbc831b632011-05-11 09:30:34 -0700620 mTabs.setSelectedTab(mTabControl.getTabPosition(tab));
Michael Kolb94827b62011-01-08 14:23:45 -0800621 TabView tv = mTabMap.get(tab);
622 if (tv != null) {
623 tv.setProgress(tv.mTab.getLoadProgress());
Michael Kolba2b2ba82010-08-04 17:54:03 -0700624 // update the scroll state
625 WebView webview = tab.getWebView();
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800626 if (webview != null) {
627 int h = webview.getVisibleTitleHeight();
Michael Kolb5ee018e2011-02-18 15:47:21 -0800628 onScroll(h, true);
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800629 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700630 }
631 }
632
Michael Kolba2b2ba82010-08-04 17:54:03 -0700633 public void onFavicon(Tab tab, Bitmap favicon) {
Michael Kolb94827b62011-01-08 14:23:45 -0800634 TabView tv = mTabMap.get(tab);
635 if (tv != null) {
636 tv.setFavicon(renderFavicon(favicon));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700637 }
638 }
639
Michael Kolba2b2ba82010-08-04 17:54:03 -0700640 public void onNewTab(Tab tab) {
Michael Kolb94827b62011-01-08 14:23:45 -0800641 TabView tv = buildTabView(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800642 animateTabIn(tab, tv);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700643 }
644
Michael Kolba2b2ba82010-08-04 17:54:03 -0700645 public void onProgress(Tab tab, int progress) {
Michael Kolb94827b62011-01-08 14:23:45 -0800646 TabView tv = mTabMap.get(tab);
647 if (tv != null) {
648 tv.setProgress(progress);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700649 }
650 }
651
Michael Kolba2b2ba82010-08-04 17:54:03 -0700652 public void onRemoveTab(Tab tab) {
Michael Kolb94827b62011-01-08 14:23:45 -0800653 TabView tv = mTabMap.get(tab);
654 if (tv != null) {
Michael Kolb2d59c322011-01-25 13:18:55 -0800655 animateTabOut(tab, tv);
656 } else {
657 mTabMap.remove(tab);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700658 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700659 }
660
Michael Kolba2b2ba82010-08-04 17:54:03 -0700661 public void onUrlAndTitle(Tab tab, String url, String title) {
Michael Kolb94827b62011-01-08 14:23:45 -0800662 TabView tv = mTabMap.get(tab);
663 if (tv != null) {
664 if (title != null) {
665 tv.setDisplayTitle(title);
666 } else if (url != null) {
667 tv.setDisplayTitle(UrlUtils.stripUrl(url));
668 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700669 }
670 }
671
Michael Kolba2b2ba82010-08-04 17:54:03 -0700672 private boolean isLoading() {
Michael Kolb94827b62011-01-08 14:23:45 -0800673 TabView tv = mTabMap.get(mTabControl.getCurrentTab());
674 if (tv != null) {
675 return tv.mInLoad;
Michael Kolb8233fac2010-10-26 16:08:53 -0700676 } else {
677 return false;
678 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700679 }
680
Michael Kolba2b2ba82010-08-04 17:54:03 -0700681}