blob: 8584afa334b1c4c31c01ec8de190cf1e27442f22 [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>();
Michael Kolb8233fac2010-10-26 16:08:53 -0700114 LayoutInflater factory = LayoutInflater.from(activity);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700115 factory.inflate(R.layout.tab_bar, this);
Michael Kolbf558f0d2011-01-13 19:24:18 -0800116 setPadding(0, (int) res.getDimension(R.dimen.tab_padding_top), 0, 0);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700117 mTabs = (TabScrollView) findViewById(R.id.tabs);
Michael Kolbebba8b42010-09-30 12:57:59 -0700118 mNewTab = (ImageButton) findViewById(R.id.newtab);
119 mNewTab.setOnClickListener(this);
John Reckaff60fb2010-10-25 13:47:58 -0700120 mGenericFavicon = res.getDrawable(R.drawable.app_web_browser_sm);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700121
Michael Kolb1bf23132010-11-19 12:55:12 -0800122 updateTabs(mUiController.getTabs());
Michael Kolbebba8b42010-09-30 12:57:59 -0700123 mButtonWidth = -1;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800124 // tab dimensions
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800125 mTabOverlap = (int) res.getDimension(R.dimen.tab_overlap);
Michael Kolb5a72f182011-01-13 20:35:06 -0800126 mAddTabOverlap = (int) res.getDimension(R.dimen.tab_addoverlap);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800127 mTabSliceWidth = (int) res.getDimension(R.dimen.tab_slice);
Romain Guyc8373212011-01-07 19:15:16 -0800128
129 mActiveShaderPaint.setStyle(Paint.Style.FILL);
130 mActiveShaderPaint.setAntiAlias(true);
131
132 mInactiveShaderPaint.setStyle(Paint.Style.FILL);
133 mInactiveShaderPaint.setAntiAlias(true);
Michael Kolbb4cafc52011-01-12 16:55:04 -0800134
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800135 mFocusPaint.setStyle(Paint.Style.STROKE);
Michael Kolbeb7001c2011-02-16 16:07:33 -0800136 mFocusPaint.setStrokeWidth(res.getDimension(R.dimen.tab_focus_stroke));
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800137 mFocusPaint.setAntiAlias(true);
138 mFocusPaint.setColor(res.getColor(R.color.tabFocusHighlight));
Michael Kolbebba8b42010-09-30 12:57:59 -0700139 }
140
Michael Kolb678afc82011-05-17 14:52:41 -0700141 @Override
142 public void onConfigurationChanged(Configuration config) {
143 super.onConfigurationChanged(config);
144 Resources res = mActivity.getResources();
145 mTabWidthSelected = (int) res.getDimension(R.dimen.tab_width_selected);
146 mTabWidthUnselected = (int) res.getDimension(R.dimen.tab_width_unselected);
147 // force update of tab bar
148 mTabs.updateLayout();
149 }
150
Michael Kolb376b5412010-12-15 11:52:57 -0800151 void setUseQuickControls(boolean useQuickControls) {
152 mUseQuickControls = useQuickControls;
Michael Kolb467af0a2011-01-11 13:09:49 -0800153 mNewTab.setVisibility(mUseQuickControls ? View.GONE
154 : View.VISIBLE);
Michael Kolb376b5412010-12-15 11:52:57 -0800155 }
156
157 int getTabCount() {
158 return mTabMap.size();
159 }
160
Michael Kolb1bf23132010-11-19 12:55:12 -0800161 void updateTabs(List<Tab> tabs) {
162 mTabs.clearTabs();
163 mTabMap.clear();
164 for (Tab tab : tabs) {
Michael Kolb94827b62011-01-08 14:23:45 -0800165 TabView tv = buildTabView(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800166 mTabs.addTab(tv);
Michael Kolb1bf23132010-11-19 12:55:12 -0800167 }
Michael Kolbc831b632011-05-11 09:30:34 -0700168 mTabs.setSelectedTab(mTabControl.getCurrentPosition());
Michael Kolb1bf23132010-11-19 12:55:12 -0800169 }
170
Michael Kolbebba8b42010-09-30 12:57:59 -0700171 @Override
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800172 protected void onMeasure(int hspec, int vspec) {
173 super.onMeasure(hspec, vspec);
174 int w = getMeasuredWidth();
175 // adjust for new tab overlap
Michael Kolb467af0a2011-01-11 13:09:49 -0800176 if (!mUseQuickControls) {
Michael Kolb5a72f182011-01-13 20:35:06 -0800177 w -= mAddTabOverlap;
Michael Kolb467af0a2011-01-11 13:09:49 -0800178 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800179 setMeasuredDimension(w, getMeasuredHeight());
180 }
181
182 @Override
Michael Kolbebba8b42010-09-30 12:57:59 -0700183 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800184 // use paddingLeft and paddingTop
185 int pl = getPaddingLeft();
186 int pt = getPaddingTop();
Michael Kolbebba8b42010-09-30 12:57:59 -0700187 int sw = mTabs.getMeasuredWidth();
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800188 int w = right - left - pl;
Michael Kolbb4cafc52011-01-12 16:55:04 -0800189 if (mUseQuickControls) {
190 mButtonWidth = 0;
191 } else {
Michael Kolb5a72f182011-01-13 20:35:06 -0800192 mButtonWidth = mNewTab.getMeasuredWidth() - mAddTabOverlap;
Michael Kolbb4cafc52011-01-12 16:55:04 -0800193 if (w-sw < mButtonWidth) {
194 sw = w - mButtonWidth;
195 }
Michael Kolbebba8b42010-09-30 12:57:59 -0700196 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800197 mTabs.layout(pl, pt, pl + sw, bottom - top);
198 // adjust for overlap
Michael Kolbb4cafc52011-01-12 16:55:04 -0800199 if (!mUseQuickControls) {
Michael Kolb5a72f182011-01-13 20:35:06 -0800200 mNewTab.layout(pl + sw - mAddTabOverlap, pt,
201 pl + sw + mButtonWidth - mAddTabOverlap, bottom - top);
Michael Kolbb4cafc52011-01-12 16:55:04 -0800202 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700203 }
204
205 public void onClick(View view) {
Michael Kolbebba8b42010-09-30 12:57:59 -0700206 if (mNewTab == view) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700207 mUiController.openTabToHomePage();
Michael Kolbebba8b42010-09-30 12:57:59 -0700208 } else if (mTabs.getSelectedTab() == view) {
Michael Kolb467af0a2011-01-11 13:09:49 -0800209 if (mUseQuickControls) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800210 if (mUi.isTitleBarShowing() && !isLoading()) {
211 mUi.stopEditingUrl();
212 mUi.hideTitleBar();
Michael Kolb467af0a2011-01-11 13:09:49 -0800213 } else {
214 mUi.stopWebViewScrolling();
Michael Kolb46f987e2011-04-05 10:39:10 -0700215 mUi.editUrl(false);
Michael Kolb467af0a2011-01-11 13:09:49 -0800216 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800217 } else if (mUi.isTitleBarShowing() && !isLoading()) {
218 mUi.stopEditingUrl();
219 mUi.hideTitleBar();
Michael Kolbed217742010-08-10 17:52:34 -0700220 } else {
221 showUrlBar();
222 }
Michael Kolbc831b632011-05-11 09:30:34 -0700223 } else if (view instanceof TabView) {
224 final Tab tab = ((TabView) view).mTab;
Michael Kolbebba8b42010-09-30 12:57:59 -0700225 int ix = mTabs.getChildIndex(view);
226 if (ix >= 0) {
227 mTabs.setSelectedTab(ix);
Michael Kolbc831b632011-05-11 09:30:34 -0700228 mUiController.switchToTab(tab);
Michael Kolbebba8b42010-09-30 12:57:59 -0700229 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700230 }
231 }
232
Michael Kolbed217742010-08-10 17:52:34 -0700233 private void showUrlBar() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700234 mUi.stopWebViewScrolling();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800235 mUi.showTitleBar();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700236 }
237
Michael Kolb376b5412010-12-15 11:52:57 -0800238 void showTitleBarIndicator(boolean show) {
Michael Kolba0e2a752010-12-12 15:27:56 -0800239 Tab tab = mTabControl.getCurrentTab();
John Reck541f55a2011-06-07 16:34:43 -0700240 if (tab != null && !tab.isSnapshot()) {
Michael Kolb94827b62011-01-08 14:23:45 -0800241 TabView tv = mTabMap.get(tab);
242 if (tv != null) {
243 tv.showIndicator(show);
Michael Kolba0e2a752010-12-12 15:27:56 -0800244 }
245 }
Michael Kolbed217742010-08-10 17:52:34 -0700246 }
247
Michael Kolb7dad8f02011-01-21 11:06:18 -0800248 boolean showsTitleBarIndicator() {
249 Tab tab = mTabControl.getCurrentTab();
250 if (tab != null) {
251 TabView tv = mTabMap.get(tab);
252 if (tv != null) {
253 return tv.showsIndicator();
254 }
255 }
256 return false;
257 }
258
Michael Kolbed217742010-08-10 17:52:34 -0700259 // callback after fake titlebar is shown
260 void onShowTitleBar() {
Michael Kolba0e2a752010-12-12 15:27:56 -0800261 showTitleBarIndicator(false);
Michael Kolbed217742010-08-10 17:52:34 -0700262 }
263
264 // callback after fake titlebar is hidden
Michael Kolba2b2ba82010-08-04 17:54:03 -0700265 void onHideTitleBar() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700266 Tab tab = mTabControl.getCurrentTab();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800267 WebView w = tab.getWebView();
268 if (w != null) {
269 showTitleBarIndicator(w.getVisibleTitleHeight() == 0);
270 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700271 }
272
Michael Kolbed217742010-08-10 17:52:34 -0700273 // webview scroll listener
274
275 @Override
Michael Kolb5ee018e2011-02-18 15:47:21 -0800276 public void onScroll(int visibleTitleHeight, boolean userInitiated) {
Michael Kolb376b5412010-12-15 11:52:57 -0800277 if (mUseQuickControls) return;
Michael Kolb8233fac2010-10-26 16:08:53 -0700278 // isLoading is using the current tab, which initially might not be set yet
John Reck7e5b8b52011-06-22 13:46:25 -0700279 if (mTabControl.getCurrentTab() != null) {
280 if (visibleTitleHeight == 0 && !mUi.isTitleBarShowing()) {
281 if (!showsTitleBarIndicator()) {
Michael Kolb7dad8f02011-01-21 11:06:18 -0800282 showTitleBarIndicator(true);
283 }
John Reckdcf911c2010-12-22 16:32:31 -0800284 } else {
Michael Kolb7dad8f02011-01-21 11:06:18 -0800285 if (showsTitleBarIndicator()) {
286 showTitleBarIndicator(false);
287 }
Michael Kolbed217742010-08-10 17:52:34 -0700288 }
289 }
290 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700291
292 @Override
293 public void createContextMenu(ContextMenu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700294 MenuInflater inflater = mActivity.getMenuInflater();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700295 inflater.inflate(R.menu.title_context, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -0700296 mActivity.onCreateContextMenu(menu, this, null);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700297 }
298
Michael Kolb94827b62011-01-08 14:23:45 -0800299 private TabView buildTabView(Tab tab) {
300 TabView tabview = new TabView(mActivity, tab);
301 mTabMap.put(tab, tabview);
302 tabview.setOnClickListener(this);
Michael Kolb94827b62011-01-08 14:23:45 -0800303 return tabview;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700304 }
305
Romain Guyc8373212011-01-07 19:15:16 -0800306 private static Bitmap getDrawableAsBitmap(Drawable drawable, int width, int height) {
307 Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
308 Canvas c = new Canvas(b);
309 drawable.setBounds(0, 0, width, height);
310 drawable.draw(c);
311 return b;
Michael Kolbb4cafc52011-01-12 16:55:04 -0800312 }
313
Michael Kolba2b2ba82010-08-04 17:54:03 -0700314 /**
315 * View used in the tab bar
316 */
317 class TabView extends LinearLayout implements OnClickListener {
318
Michael Kolb94827b62011-01-08 14:23:45 -0800319 Tab mTab;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700320 View mTabContent;
321 TextView mTitle;
Michael Kolba0e2a752010-12-12 15:27:56 -0800322 View mIndicator;
Michael Kolbae62fd42010-08-18 16:33:28 -0700323 View mIncognito;
John Reck541f55a2011-06-07 16:34:43 -0700324 View mSnapshot;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700325 ImageView mIconView;
326 ImageView mLock;
327 ImageView mClose;
328 boolean mSelected;
329 boolean mInLoad;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800330 Path mPath;
Michael Kolbeb7001c2011-02-16 16:07:33 -0800331 Path mFocusPath;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800332 int[] mWindowPos;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700333
334 /**
335 * @param context
336 */
Michael Kolb94827b62011-01-08 14:23:45 -0800337 public TabView(Context context, Tab tab) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700338 super(context);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800339 setWillNotDraw(false);
340 mPath = new Path();
Michael Kolbeb7001c2011-02-16 16:07:33 -0800341 mFocusPath = new Path();
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800342 mWindowPos = new int[2];
Michael Kolb94827b62011-01-08 14:23:45 -0800343 mTab = tab;
Michael Kolbed217742010-08-10 17:52:34 -0700344 setGravity(Gravity.CENTER_VERTICAL);
345 setOrientation(LinearLayout.HORIZONTAL);
Michael Kolb49f15832011-01-25 15:02:27 -0800346 setPadding(mTabOverlap, 0, mTabSliceWidth, 0);
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100347 LayoutInflater inflater = LayoutInflater.from(getContext());
Michael Kolbed217742010-08-10 17:52:34 -0700348 mTabContent = inflater.inflate(R.layout.tab_title, this, true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700349 mTitle = (TextView) mTabContent.findViewById(R.id.title);
350 mIconView = (ImageView) mTabContent.findViewById(R.id.favicon);
351 mLock = (ImageView) mTabContent.findViewById(R.id.lock);
352 mClose = (ImageView) mTabContent.findViewById(R.id.close);
353 mClose.setOnClickListener(this);
Michael Kolbae62fd42010-08-18 16:33:28 -0700354 mIncognito = mTabContent.findViewById(R.id.incognito);
John Reck541f55a2011-06-07 16:34:43 -0700355 mSnapshot = mTabContent.findViewById(R.id.snapshot);
Michael Kolba0e2a752010-12-12 15:27:56 -0800356 mIndicator = mTabContent.findViewById(R.id.chevron);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700357 mSelected = false;
358 mInLoad = false;
359 // update the status
Michael Kolb94827b62011-01-08 14:23:45 -0800360 updateFromTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700361 }
362
Michael Kolba0e2a752010-12-12 15:27:56 -0800363 void showIndicator(boolean show) {
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800364 if (mSelected) {
365 mIndicator.setVisibility(show ? View.VISIBLE : View.GONE);
Michael Kolb49f15832011-01-25 15:02:27 -0800366 LayoutParams lp = (LinearLayout.LayoutParams) getLayoutParams();
367 if (show) {
368 lp.width = mTabWidthSelected + mIndicator.getWidth();
369 } else {
370 lp.width = mTabWidthSelected;
371 }
372 lp.height = LayoutParams.MATCH_PARENT;
373 setLayoutParams(lp);
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800374 } else {
375 mIndicator.setVisibility(View.GONE);
376 }
Michael Kolba0e2a752010-12-12 15:27:56 -0800377 }
378
Michael Kolb7dad8f02011-01-21 11:06:18 -0800379 boolean showsIndicator() {
380 return (mIndicator.getVisibility() == View.VISIBLE);
381 }
382
Michael Kolba2b2ba82010-08-04 17:54:03 -0700383 @Override
384 public void onClick(View v) {
385 if (v == mClose) {
386 closeTab();
387 }
388 }
389
Michael Kolb94827b62011-01-08 14:23:45 -0800390 private void updateFromTab() {
391 String displayTitle = mTab.getTitle();
John Reck30c714c2010-12-16 17:30:34 -0800392 if (displayTitle == null) {
Michael Kolb94827b62011-01-08 14:23:45 -0800393 displayTitle = mTab.getUrl();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700394 }
John Reck30c714c2010-12-16 17:30:34 -0800395 setDisplayTitle(displayTitle);
Michael Kolb94827b62011-01-08 14:23:45 -0800396 setProgress(mTab.getLoadProgress());
397 if (mTab.getFavicon() != null) {
398 setFavicon(renderFavicon(mTab.getFavicon()));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700399 }
John Reck541f55a2011-06-07 16:34:43 -0700400 updateTabIcons();
401 }
402
403 private void updateTabIcons() {
404 mIncognito.setVisibility(
405 mTab.isPrivateBrowsingEnabled() ?
406 View.VISIBLE : View.GONE);
407 mSnapshot.setVisibility(mTab.isSnapshot()
408 ? View.VISIBLE : View.GONE);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700409 }
410
411 @Override
Michael Kolbb7b115e2010-09-25 16:59:37 -0700412 public void setActivated(boolean selected) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700413 mSelected = selected;
414 mClose.setVisibility(mSelected ? View.VISIBLE : View.GONE);
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800415 mIndicator.setVisibility(View.GONE);
Michael Kolb8233fac2010-10-26 16:08:53 -0700416 mTitle.setTextAppearance(mActivity, mSelected ?
Michael Kolbc7485ae2010-09-03 10:10:58 -0700417 R.style.TabTitleSelected : R.style.TabTitleUnselected);
418 setHorizontalFadingEdgeEnabled(!mSelected);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700419 super.setActivated(selected);
Michael Kolb678afc82011-05-17 14:52:41 -0700420 updateLayoutParams();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800421 setFocusable(!selected);
422 postInvalidate();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700423 }
424
Michael Kolb678afc82011-05-17 14:52:41 -0700425 public void updateLayoutParams() {
426 LayoutParams lp = (LinearLayout.LayoutParams) getLayoutParams();
427 lp.width = mSelected ? mTabWidthSelected : mTabWidthUnselected;
428 lp.height = LayoutParams.MATCH_PARENT;
429 setLayoutParams(lp);
430 }
431
Michael Kolba2b2ba82010-08-04 17:54:03 -0700432 void setDisplayTitle(String title) {
433 mTitle.setText(title);
434 }
435
436 void setFavicon(Drawable d) {
437 mIconView.setImageDrawable(d);
438 }
439
440 void setLock(Drawable d) {
441 if (null == d) {
442 mLock.setVisibility(View.GONE);
443 } else {
444 mLock.setImageDrawable(d);
445 mLock.setVisibility(View.VISIBLE);
446 }
447 }
448
Michael Kolba2b2ba82010-08-04 17:54:03 -0700449 void setProgress(int newProgress) {
450 if (newProgress >= PROGRESS_MAX) {
451 mInLoad = false;
452 } else {
453 if (!mInLoad && getWindowToken() != null) {
454 mInLoad = true;
455 }
456 }
457 }
458
459 private void closeTab() {
Michael Kolb94827b62011-01-08 14:23:45 -0800460 if (mTab == mTabControl.getCurrentTab()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700461 mUiController.closeCurrentTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700462 } else {
Michael Kolb94827b62011-01-08 14:23:45 -0800463 mUiController.closeTab(mTab);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700464 }
465 }
466
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800467 @Override
468 protected void onLayout(boolean changed, int l, int t, int r, int b) {
469 super.onLayout(changed, l, t, r, b);
470 setTabPath(mPath, 0, 0, r - l, b - t);
Michael Kolbeb7001c2011-02-16 16:07:33 -0800471 setFocusPath(mFocusPath, 0, 0, r - l, b - t);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800472 }
Michael Kolbb4cafc52011-01-12 16:55:04 -0800473
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800474 @Override
475 protected void dispatchDraw(Canvas canvas) {
Romain Guyc8373212011-01-07 19:15:16 -0800476 if (mCurrentTextureWidth != mUi.getContentWidth() ||
477 mCurrentTextureHeight != getHeight()) {
478 mCurrentTextureWidth = mUi.getContentWidth();
479 mCurrentTextureHeight = getHeight();
480
481 if (mCurrentTextureWidth > 0 && mCurrentTextureHeight > 0) {
482 Bitmap activeTexture = getDrawableAsBitmap(mActiveDrawable,
483 mCurrentTextureWidth, mCurrentTextureHeight);
484 Bitmap inactiveTexture = getDrawableAsBitmap(mInactiveDrawable,
485 mCurrentTextureWidth, mCurrentTextureHeight);
486
487 mActiveShader = new BitmapShader(activeTexture,
488 Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
489 mActiveShaderPaint.setShader(mActiveShader);
Michael Kolbb4cafc52011-01-12 16:55:04 -0800490
Romain Guyc8373212011-01-07 19:15:16 -0800491 mInactiveShader = new BitmapShader(inactiveTexture,
492 Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
493 mInactiveShaderPaint.setShader(mInactiveShader);
494 }
495 }
Michael Kolb05902aa2011-02-22 17:43:38 -0800496 // add some monkey protection
497 if ((mActiveShader != null) && (mInactiveShader != null)) {
498 int state = canvas.save();
499 getLocationInWindow(mWindowPos);
500 Paint paint = mSelected ? mActiveShaderPaint : mInactiveShaderPaint;
501 drawClipped(canvas, paint, mPath, mWindowPos[0]);
502 canvas.restoreToCount(state);
503 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800504 super.dispatchDraw(canvas);
505 }
506
Romain Guyc8373212011-01-07 19:15:16 -0800507 private void drawClipped(Canvas canvas, Paint paint, Path clipPath, int left) {
508 // TODO: We should change the matrix/shader only when needed
509 final Matrix matrix = mSelected ? mActiveMatrix : mInactiveMatrix;
510 matrix.setTranslate(-left, 0.0f);
511 (mSelected ? mActiveShader : mInactiveShader).setLocalMatrix(matrix);
512 canvas.drawPath(clipPath, paint);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800513 if (isFocused()) {
Michael Kolbeb7001c2011-02-16 16:07:33 -0800514 canvas.drawPath(mFocusPath, mFocusPaint);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800515 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800516 }
517
518 private void setTabPath(Path path, int l, int t, int r, int b) {
519 path.reset();
520 path.moveTo(l, b);
521 path.lineTo(l, t);
522 path.lineTo(r - mTabSliceWidth, t);
523 path.lineTo(r, b);
524 path.close();
525 }
526
Michael Kolbeb7001c2011-02-16 16:07:33 -0800527 private void setFocusPath(Path path, int l, int t, int r, int b) {
528 path.reset();
529 path.moveTo(l, b);
530 path.lineTo(l, t);
531 path.lineTo(r - mTabSliceWidth, t);
532 path.lineTo(r, b);
533 }
534
Michael Kolba2b2ba82010-08-04 17:54:03 -0700535 }
536
Michael Kolb49f15832011-01-25 15:02:27 -0800537 static Drawable createFaviconBackground(Context context) {
538 PaintDrawable faviconBackground = new PaintDrawable();
539 Resources res = context.getResources();
540 faviconBackground.getPaint().setColor(context.getResources()
541 .getColor(R.color.tabFaviconBackground));
542 faviconBackground.setCornerRadius(
543 res.getDimension(R.dimen.tab_favicon_corner_radius));
544 return faviconBackground;
545 }
546
Michael Kolb94827b62011-01-08 14:23:45 -0800547 private Drawable renderFavicon(Bitmap icon) {
Michael Kolb49f15832011-01-25 15:02:27 -0800548 Drawable[] array = new Drawable[2];
549 array[0] = createFaviconBackground(getContext());
Michael Kolb94827b62011-01-08 14:23:45 -0800550 if (icon == null) {
Michael Kolb49f15832011-01-25 15:02:27 -0800551 array[1] = mGenericFavicon;
Michael Kolb94827b62011-01-08 14:23:45 -0800552 } else {
Michael Kolb49f15832011-01-25 15:02:27 -0800553 array[1] = new BitmapDrawable(icon);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700554 }
Michael Kolb94827b62011-01-08 14:23:45 -0800555 LayerDrawable d = new LayerDrawable(array);
Michael Kolb49f15832011-01-25 15:02:27 -0800556 d.setLayerInset(1, 2, 2, 2, 2);
Michael Kolb94827b62011-01-08 14:23:45 -0800557 return d;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700558 }
559
Michael Kolb2d59c322011-01-25 13:18:55 -0800560 private void animateTabOut(final Tab tab, final TabView tv) {
561 ObjectAnimator scalex = ObjectAnimator.ofFloat(tv, "scaleX", 1.0f, 0.0f);
562 ObjectAnimator scaley = ObjectAnimator.ofFloat(tv, "scaleY", 1.0f, 0.0f);
Michael Kolb49f15832011-01-25 15:02:27 -0800563 ObjectAnimator alpha = ObjectAnimator.ofFloat(tv, "alpha", 1.0f, 0.0f);
Michael Kolb2d59c322011-01-25 13:18:55 -0800564 AnimatorSet animator = new AnimatorSet();
Michael Kolb49f15832011-01-25 15:02:27 -0800565 animator.playTogether(scalex, scaley, alpha);
Michael Kolb2d59c322011-01-25 13:18:55 -0800566 animator.setDuration(150);
567 animator.addListener(new AnimatorListener() {
568
569 @Override
570 public void onAnimationCancel(Animator animation) {
571 }
572
573 @Override
574 public void onAnimationEnd(Animator animation) {
575 mTabs.removeTab(tv);
576 mTabMap.remove(tab);
Michael Kolb8814d732011-01-26 11:22:30 -0800577 mUi.onRemoveTabCompleted(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800578 }
579
580 @Override
581 public void onAnimationRepeat(Animator animation) {
582 }
583
584 @Override
585 public void onAnimationStart(Animator animation) {
586 }
587
588 });
589 animator.start();
590 }
591
592 private void animateTabIn(final Tab tab, final TabView tv) {
Michael Kolb49f15832011-01-25 15:02:27 -0800593 ObjectAnimator scalex = ObjectAnimator.ofFloat(tv, "scaleX", 0.0f, 1.0f);
Michael Kolb2d59c322011-01-25 13:18:55 -0800594 scalex.setDuration(150);
595 scalex.addListener(new AnimatorListener() {
596
597 @Override
598 public void onAnimationCancel(Animator animation) {
599 }
600
601 @Override
602 public void onAnimationEnd(Animator animation) {
Michael Kolb8814d732011-01-26 11:22:30 -0800603 mUi.onAddTabCompleted(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800604 }
605
606 @Override
607 public void onAnimationRepeat(Animator animation) {
608 }
609
610 @Override
611 public void onAnimationStart(Animator animation) {
612 mTabs.addTab(tv);
613 }
614
615 });
616 scalex.start();
617 }
618
Michael Kolba2b2ba82010-08-04 17:54:03 -0700619 // TabChangeListener implementation
620
Michael Kolb8233fac2010-10-26 16:08:53 -0700621 public void onSetActiveTab(Tab tab) {
Michael Kolbc831b632011-05-11 09:30:34 -0700622 mTabs.setSelectedTab(mTabControl.getTabPosition(tab));
Michael Kolb94827b62011-01-08 14:23:45 -0800623 TabView tv = mTabMap.get(tab);
624 if (tv != null) {
625 tv.setProgress(tv.mTab.getLoadProgress());
Michael Kolba2b2ba82010-08-04 17:54:03 -0700626 // update the scroll state
627 WebView webview = tab.getWebView();
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800628 if (webview != null) {
629 int h = webview.getVisibleTitleHeight();
Michael Kolb5ee018e2011-02-18 15:47:21 -0800630 onScroll(h, true);
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800631 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700632 }
633 }
634
Michael Kolba2b2ba82010-08-04 17:54:03 -0700635 public void onFavicon(Tab tab, Bitmap favicon) {
Michael Kolb94827b62011-01-08 14:23:45 -0800636 TabView tv = mTabMap.get(tab);
637 if (tv != null) {
638 tv.setFavicon(renderFavicon(favicon));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700639 }
640 }
641
Michael Kolba2b2ba82010-08-04 17:54:03 -0700642 public void onNewTab(Tab tab) {
Michael Kolb94827b62011-01-08 14:23:45 -0800643 TabView tv = buildTabView(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800644 animateTabIn(tab, tv);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700645 }
646
Michael Kolba2b2ba82010-08-04 17:54:03 -0700647 public void onProgress(Tab tab, int progress) {
Michael Kolb94827b62011-01-08 14:23:45 -0800648 TabView tv = mTabMap.get(tab);
649 if (tv != null) {
650 tv.setProgress(progress);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700651 }
652 }
653
Michael Kolba2b2ba82010-08-04 17:54:03 -0700654 public void onRemoveTab(Tab tab) {
Michael Kolb94827b62011-01-08 14:23:45 -0800655 TabView tv = mTabMap.get(tab);
656 if (tv != null) {
Michael Kolb2d59c322011-01-25 13:18:55 -0800657 animateTabOut(tab, tv);
658 } else {
659 mTabMap.remove(tab);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700660 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700661 }
662
Michael Kolba2b2ba82010-08-04 17:54:03 -0700663 public void onUrlAndTitle(Tab tab, String url, String title) {
Michael Kolb94827b62011-01-08 14:23:45 -0800664 TabView tv = mTabMap.get(tab);
665 if (tv != null) {
666 if (title != null) {
667 tv.setDisplayTitle(title);
668 } else if (url != null) {
669 tv.setDisplayTitle(UrlUtils.stripUrl(url));
670 }
John Reck541f55a2011-06-07 16:34:43 -0700671 tv.updateTabIcons();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700672 }
673 }
674
Michael Kolba2b2ba82010-08-04 17:54:03 -0700675 private boolean isLoading() {
Michael Kolb94827b62011-01-08 14:23:45 -0800676 TabView tv = mTabMap.get(mTabControl.getCurrentTab());
677 if (tv != null) {
678 return tv.mInLoad;
Michael Kolb8233fac2010-10-26 16:08:53 -0700679 } else {
680 return false;
681 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700682 }
683
Michael Kolba2b2ba82010-08-04 17:54:03 -0700684}