blob: 790d1629176ad6840acc5df63a3c6076e09dec55 [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 Kolb8233fac2010-10-26 16:08:53 -0700197 mUi.hideComboView();
Michael Kolbebba8b42010-09-30 12:57:59 -0700198 if (mNewTab == view) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700199 mUiController.openTabToHomePage();
Michael Kolbebba8b42010-09-30 12:57:59 -0700200 } else if (mTabs.getSelectedTab() == view) {
Michael Kolb467af0a2011-01-11 13:09:49 -0800201 if (mUseQuickControls) {
202 if (mUi.isFakeTitleBarShowing() && !isLoading()) {
203 mUi.hideFakeTitleBar();
204 } else {
205 mUi.stopWebViewScrolling();
206 mUi.showFakeTitleBarAndEdit();
207 }
208 } else if (mUi.isFakeTitleBarShowing() && !isLoading()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700209 mUi.hideFakeTitleBar();
Michael Kolbed217742010-08-10 17:52:34 -0700210 } else {
211 showUrlBar();
212 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700213 } else {
Michael Kolbebba8b42010-09-30 12:57:59 -0700214 int ix = mTabs.getChildIndex(view);
215 if (ix >= 0) {
216 mTabs.setSelectedTab(ix);
Michael Kolb8233fac2010-10-26 16:08:53 -0700217 mUiController.switchToTab(ix);
Michael Kolbebba8b42010-09-30 12:57:59 -0700218 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700219 }
220 }
221
Michael Kolbed217742010-08-10 17:52:34 -0700222 private void showUrlBar() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700223 mUi.stopWebViewScrolling();
224 mUi.showFakeTitleBar();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700225 }
226
Michael Kolb376b5412010-12-15 11:52:57 -0800227 void showTitleBarIndicator(boolean show) {
Michael Kolba0e2a752010-12-12 15:27:56 -0800228 Tab tab = mTabControl.getCurrentTab();
229 if (tab != null) {
Michael Kolb94827b62011-01-08 14:23:45 -0800230 TabView tv = mTabMap.get(tab);
231 if (tv != null) {
232 tv.showIndicator(show);
Michael Kolba0e2a752010-12-12 15:27:56 -0800233 }
234 }
Michael Kolbed217742010-08-10 17:52:34 -0700235 }
236
Michael Kolb7dad8f02011-01-21 11:06:18 -0800237 boolean showsTitleBarIndicator() {
238 Tab tab = mTabControl.getCurrentTab();
239 if (tab != null) {
240 TabView tv = mTabMap.get(tab);
241 if (tv != null) {
242 return tv.showsIndicator();
243 }
244 }
245 return false;
246 }
247
Michael Kolbed217742010-08-10 17:52:34 -0700248 // callback after fake titlebar is shown
249 void onShowTitleBar() {
Michael Kolba0e2a752010-12-12 15:27:56 -0800250 showTitleBarIndicator(false);
Michael Kolbed217742010-08-10 17:52:34 -0700251 }
252
253 // callback after fake titlebar is hidden
Michael Kolba2b2ba82010-08-04 17:54:03 -0700254 void onHideTitleBar() {
Michael Kolba0e2a752010-12-12 15:27:56 -0800255 showTitleBarIndicator(mVisibleTitleHeight == 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700256 Tab tab = mTabControl.getCurrentTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700257 tab.getWebView().requestFocus();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700258 }
259
Michael Kolbed217742010-08-10 17:52:34 -0700260 // webview scroll listener
261
262 @Override
Michael Kolba0e2a752010-12-12 15:27:56 -0800263 public void onScroll(int visibleTitleHeight) {
Michael Kolb376b5412010-12-15 11:52:57 -0800264 if (mUseQuickControls) return;
Michael Kolb8233fac2010-10-26 16:08:53 -0700265 // isLoading is using the current tab, which initially might not be set yet
John Reckdcf911c2010-12-22 16:32:31 -0800266 if (mTabControl.getCurrentTab() != null
267 && !isLoading()) {
268 if (visibleTitleHeight == 0) {
Michael Kolb7dad8f02011-01-21 11:06:18 -0800269 if (!showsTitleBarIndicator()) {
270 mUi.hideFakeTitleBar();
271 showTitleBarIndicator(true);
272 }
John Reckdcf911c2010-12-22 16:32:31 -0800273 } else {
Michael Kolb7dad8f02011-01-21 11:06:18 -0800274 if (showsTitleBarIndicator()) {
275 showTitleBarIndicator(false);
276 }
Michael Kolbed217742010-08-10 17:52:34 -0700277 }
278 }
Michael Kolba0e2a752010-12-12 15:27:56 -0800279 mVisibleTitleHeight = visibleTitleHeight;
Michael Kolbed217742010-08-10 17:52:34 -0700280 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700281
282 @Override
283 public void createContextMenu(ContextMenu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700284 MenuInflater inflater = mActivity.getMenuInflater();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700285 inflater.inflate(R.menu.title_context, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -0700286 mActivity.onCreateContextMenu(menu, this, null);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700287 }
288
Michael Kolb94827b62011-01-08 14:23:45 -0800289 private TabView buildTabView(Tab tab) {
290 TabView tabview = new TabView(mActivity, tab);
291 mTabMap.put(tab, tabview);
292 tabview.setOnClickListener(this);
Michael Kolb94827b62011-01-08 14:23:45 -0800293 return tabview;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700294 }
295
Romain Guyc8373212011-01-07 19:15:16 -0800296 private static Bitmap getDrawableAsBitmap(Drawable drawable, int width, int height) {
297 Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
298 Canvas c = new Canvas(b);
299 drawable.setBounds(0, 0, width, height);
300 drawable.draw(c);
301 return b;
Michael Kolbb4cafc52011-01-12 16:55:04 -0800302 }
303
Michael Kolba2b2ba82010-08-04 17:54:03 -0700304 /**
305 * View used in the tab bar
306 */
307 class TabView extends LinearLayout implements OnClickListener {
308
Michael Kolb94827b62011-01-08 14:23:45 -0800309 Tab mTab;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700310 View mTabContent;
311 TextView mTitle;
Michael Kolba0e2a752010-12-12 15:27:56 -0800312 View mIndicator;
Michael Kolbae62fd42010-08-18 16:33:28 -0700313 View mIncognito;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700314 ImageView mIconView;
315 ImageView mLock;
316 ImageView mClose;
317 boolean mSelected;
318 boolean mInLoad;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800319 Path mPath;
320 int[] mWindowPos;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700321
322 /**
323 * @param context
324 */
Michael Kolb94827b62011-01-08 14:23:45 -0800325 public TabView(Context context, Tab tab) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700326 super(context);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800327 setWillNotDraw(false);
328 mPath = new Path();
329 mWindowPos = new int[2];
Michael Kolb94827b62011-01-08 14:23:45 -0800330 mTab = tab;
Michael Kolbed217742010-08-10 17:52:34 -0700331 setGravity(Gravity.CENTER_VERTICAL);
332 setOrientation(LinearLayout.HORIZONTAL);
Michael Kolb49f15832011-01-25 15:02:27 -0800333 setPadding(mTabOverlap, 0, mTabSliceWidth, 0);
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100334 LayoutInflater inflater = LayoutInflater.from(getContext());
Michael Kolbed217742010-08-10 17:52:34 -0700335 mTabContent = inflater.inflate(R.layout.tab_title, this, true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700336 mTitle = (TextView) mTabContent.findViewById(R.id.title);
337 mIconView = (ImageView) mTabContent.findViewById(R.id.favicon);
338 mLock = (ImageView) mTabContent.findViewById(R.id.lock);
339 mClose = (ImageView) mTabContent.findViewById(R.id.close);
340 mClose.setOnClickListener(this);
Michael Kolbae62fd42010-08-18 16:33:28 -0700341 mIncognito = mTabContent.findViewById(R.id.incognito);
Michael Kolba0e2a752010-12-12 15:27:56 -0800342 mIndicator = mTabContent.findViewById(R.id.chevron);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700343 mSelected = false;
344 mInLoad = false;
345 // update the status
Michael Kolb94827b62011-01-08 14:23:45 -0800346 updateFromTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700347 }
348
Michael Kolba0e2a752010-12-12 15:27:56 -0800349 void showIndicator(boolean show) {
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800350 if (mSelected) {
351 mIndicator.setVisibility(show ? View.VISIBLE : View.GONE);
Michael Kolb49f15832011-01-25 15:02:27 -0800352 LayoutParams lp = (LinearLayout.LayoutParams) getLayoutParams();
353 if (show) {
354 lp.width = mTabWidthSelected + mIndicator.getWidth();
355 } else {
356 lp.width = mTabWidthSelected;
357 }
358 lp.height = LayoutParams.MATCH_PARENT;
359 setLayoutParams(lp);
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800360 } else {
361 mIndicator.setVisibility(View.GONE);
362 }
Michael Kolba0e2a752010-12-12 15:27:56 -0800363 }
364
Michael Kolb7dad8f02011-01-21 11:06:18 -0800365 boolean showsIndicator() {
366 return (mIndicator.getVisibility() == View.VISIBLE);
367 }
368
Michael Kolba2b2ba82010-08-04 17:54:03 -0700369 @Override
370 public void onClick(View v) {
371 if (v == mClose) {
372 closeTab();
373 }
374 }
375
Michael Kolb94827b62011-01-08 14:23:45 -0800376 private void updateFromTab() {
377 String displayTitle = mTab.getTitle();
John Reck30c714c2010-12-16 17:30:34 -0800378 if (displayTitle == null) {
Michael Kolb94827b62011-01-08 14:23:45 -0800379 displayTitle = mTab.getUrl();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700380 }
John Reck30c714c2010-12-16 17:30:34 -0800381 setDisplayTitle(displayTitle);
Michael Kolb94827b62011-01-08 14:23:45 -0800382 setProgress(mTab.getLoadProgress());
383 if (mTab.getFavicon() != null) {
384 setFavicon(renderFavicon(mTab.getFavicon()));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700385 }
Michael Kolb94827b62011-01-08 14:23:45 -0800386 if (mTab != null) {
Michael Kolbae62fd42010-08-18 16:33:28 -0700387 mIncognito.setVisibility(
Michael Kolb94827b62011-01-08 14:23:45 -0800388 mTab.isPrivateBrowsingEnabled() ?
Michael Kolbae62fd42010-08-18 16:33:28 -0700389 View.VISIBLE : View.GONE);
390 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700391 }
392
393 @Override
Michael Kolbb7b115e2010-09-25 16:59:37 -0700394 public void setActivated(boolean selected) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700395 mSelected = selected;
396 mClose.setVisibility(mSelected ? View.VISIBLE : View.GONE);
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800397 mIndicator.setVisibility(View.GONE);
Michael Kolb8233fac2010-10-26 16:08:53 -0700398 mTitle.setTextAppearance(mActivity, mSelected ?
Michael Kolbc7485ae2010-09-03 10:10:58 -0700399 R.style.TabTitleSelected : R.style.TabTitleUnselected);
400 setHorizontalFadingEdgeEnabled(!mSelected);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700401 super.setActivated(selected);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800402 LayoutParams lp = (LinearLayout.LayoutParams) getLayoutParams();
403 lp.width = selected ? mTabWidthSelected : mTabWidthUnselected;
404 lp.height = LayoutParams.MATCH_PARENT;
405 setLayoutParams(lp);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700406 }
407
408 void setDisplayTitle(String title) {
409 mTitle.setText(title);
410 }
411
412 void setFavicon(Drawable d) {
413 mIconView.setImageDrawable(d);
414 }
415
416 void setLock(Drawable d) {
417 if (null == d) {
418 mLock.setVisibility(View.GONE);
419 } else {
420 mLock.setImageDrawable(d);
421 mLock.setVisibility(View.VISIBLE);
422 }
423 }
424
Michael Kolba2b2ba82010-08-04 17:54:03 -0700425 void setProgress(int newProgress) {
426 if (newProgress >= PROGRESS_MAX) {
427 mInLoad = false;
428 } else {
429 if (!mInLoad && getWindowToken() != null) {
430 mInLoad = true;
431 }
432 }
433 }
434
435 private void closeTab() {
Michael Kolb94827b62011-01-08 14:23:45 -0800436 if (mTab == mTabControl.getCurrentTab()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700437 mUiController.closeCurrentTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700438 } else {
Michael Kolb94827b62011-01-08 14:23:45 -0800439 mUiController.closeTab(mTab);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700440 }
441 }
442
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800443 @Override
444 protected void onLayout(boolean changed, int l, int t, int r, int b) {
445 super.onLayout(changed, l, t, r, b);
446 setTabPath(mPath, 0, 0, r - l, b - t);
447 }
Michael Kolbb4cafc52011-01-12 16:55:04 -0800448
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800449 @Override
450 protected void dispatchDraw(Canvas canvas) {
Romain Guyc8373212011-01-07 19:15:16 -0800451 if (mCurrentTextureWidth != mUi.getContentWidth() ||
452 mCurrentTextureHeight != getHeight()) {
453 mCurrentTextureWidth = mUi.getContentWidth();
454 mCurrentTextureHeight = getHeight();
455
456 if (mCurrentTextureWidth > 0 && mCurrentTextureHeight > 0) {
457 Bitmap activeTexture = getDrawableAsBitmap(mActiveDrawable,
458 mCurrentTextureWidth, mCurrentTextureHeight);
459 Bitmap inactiveTexture = getDrawableAsBitmap(mInactiveDrawable,
460 mCurrentTextureWidth, mCurrentTextureHeight);
461
462 mActiveShader = new BitmapShader(activeTexture,
463 Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
464 mActiveShaderPaint.setShader(mActiveShader);
Michael Kolbb4cafc52011-01-12 16:55:04 -0800465
Romain Guyc8373212011-01-07 19:15:16 -0800466 mInactiveShader = new BitmapShader(inactiveTexture,
467 Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
468 mInactiveShaderPaint.setShader(mInactiveShader);
469 }
470 }
Michael Kolbb4cafc52011-01-12 16:55:04 -0800471
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800472 int state = canvas.save();
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800473 getLocationInWindow(mWindowPos);
Romain Guyc8373212011-01-07 19:15:16 -0800474 Paint paint = mSelected ? mActiveShaderPaint : mInactiveShaderPaint;
475 drawClipped(canvas, paint, mPath, mWindowPos[0]);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800476 canvas.restoreToCount(state);
477 super.dispatchDraw(canvas);
478 }
479
Romain Guyc8373212011-01-07 19:15:16 -0800480 private void drawClipped(Canvas canvas, Paint paint, Path clipPath, int left) {
481 // TODO: We should change the matrix/shader only when needed
482 final Matrix matrix = mSelected ? mActiveMatrix : mInactiveMatrix;
483 matrix.setTranslate(-left, 0.0f);
484 (mSelected ? mActiveShader : mInactiveShader).setLocalMatrix(matrix);
485 canvas.drawPath(clipPath, paint);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800486 }
487
488 private void setTabPath(Path path, int l, int t, int r, int b) {
489 path.reset();
490 path.moveTo(l, b);
491 path.lineTo(l, t);
492 path.lineTo(r - mTabSliceWidth, t);
493 path.lineTo(r, b);
494 path.close();
495 }
496
Michael Kolba2b2ba82010-08-04 17:54:03 -0700497 }
498
Michael Kolb49f15832011-01-25 15:02:27 -0800499 static Drawable createFaviconBackground(Context context) {
500 PaintDrawable faviconBackground = new PaintDrawable();
501 Resources res = context.getResources();
502 faviconBackground.getPaint().setColor(context.getResources()
503 .getColor(R.color.tabFaviconBackground));
504 faviconBackground.setCornerRadius(
505 res.getDimension(R.dimen.tab_favicon_corner_radius));
506 return faviconBackground;
507 }
508
Michael Kolb94827b62011-01-08 14:23:45 -0800509 private Drawable renderFavicon(Bitmap icon) {
Michael Kolb49f15832011-01-25 15:02:27 -0800510 Drawable[] array = new Drawable[2];
511 array[0] = createFaviconBackground(getContext());
Michael Kolb94827b62011-01-08 14:23:45 -0800512 if (icon == null) {
Michael Kolb49f15832011-01-25 15:02:27 -0800513 array[1] = mGenericFavicon;
Michael Kolb94827b62011-01-08 14:23:45 -0800514 } else {
Michael Kolb49f15832011-01-25 15:02:27 -0800515 array[1] = new BitmapDrawable(icon);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700516 }
Michael Kolb94827b62011-01-08 14:23:45 -0800517 LayerDrawable d = new LayerDrawable(array);
Michael Kolb49f15832011-01-25 15:02:27 -0800518 d.setLayerInset(1, 2, 2, 2, 2);
Michael Kolb94827b62011-01-08 14:23:45 -0800519 return d;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700520 }
521
Michael Kolb2d59c322011-01-25 13:18:55 -0800522 private void animateTabOut(final Tab tab, final TabView tv) {
523 ObjectAnimator scalex = ObjectAnimator.ofFloat(tv, "scaleX", 1.0f, 0.0f);
524 ObjectAnimator scaley = ObjectAnimator.ofFloat(tv, "scaleY", 1.0f, 0.0f);
Michael Kolb49f15832011-01-25 15:02:27 -0800525 ObjectAnimator alpha = ObjectAnimator.ofFloat(tv, "alpha", 1.0f, 0.0f);
Michael Kolb2d59c322011-01-25 13:18:55 -0800526 AnimatorSet animator = new AnimatorSet();
Michael Kolb49f15832011-01-25 15:02:27 -0800527 animator.playTogether(scalex, scaley, alpha);
Michael Kolb2d59c322011-01-25 13:18:55 -0800528 animator.setDuration(150);
529 animator.addListener(new AnimatorListener() {
530
531 @Override
532 public void onAnimationCancel(Animator animation) {
533 }
534
535 @Override
536 public void onAnimationEnd(Animator animation) {
537 mTabs.removeTab(tv);
538 mTabMap.remove(tab);
539 }
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) {
564 }
565
566 @Override
567 public void onAnimationRepeat(Animator animation) {
568 }
569
570 @Override
571 public void onAnimationStart(Animator animation) {
572 mTabs.addTab(tv);
573 }
574
575 });
576 scalex.start();
577 }
578
Michael Kolba2b2ba82010-08-04 17:54:03 -0700579 // TabChangeListener implementation
580
Michael Kolb8233fac2010-10-26 16:08:53 -0700581 public void onSetActiveTab(Tab tab) {
582 mTabs.setSelectedTab(mTabControl.getTabIndex(tab));
Michael Kolb94827b62011-01-08 14:23:45 -0800583 TabView tv = mTabMap.get(tab);
584 if (tv != null) {
585 tv.setProgress(tv.mTab.getLoadProgress());
Michael Kolba2b2ba82010-08-04 17:54:03 -0700586 // update the scroll state
587 WebView webview = tab.getWebView();
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800588 if (webview != null) {
589 int h = webview.getVisibleTitleHeight();
590 mVisibleTitleHeight = h -1;
591 onScroll(h);
592 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700593 }
594 }
595
Michael Kolba2b2ba82010-08-04 17:54:03 -0700596 public void onFavicon(Tab tab, Bitmap favicon) {
Michael Kolb94827b62011-01-08 14:23:45 -0800597 TabView tv = mTabMap.get(tab);
598 if (tv != null) {
599 tv.setFavicon(renderFavicon(favicon));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700600 }
601 }
602
Michael Kolba2b2ba82010-08-04 17:54:03 -0700603 public void onNewTab(Tab tab) {
Michael Kolb94827b62011-01-08 14:23:45 -0800604 TabView tv = buildTabView(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800605 animateTabIn(tab, tv);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700606 }
607
Michael Kolba2b2ba82010-08-04 17:54:03 -0700608 public void onProgress(Tab tab, int progress) {
Michael Kolb94827b62011-01-08 14:23:45 -0800609 TabView tv = mTabMap.get(tab);
610 if (tv != null) {
611 tv.setProgress(progress);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700612 }
613 }
614
Michael Kolba2b2ba82010-08-04 17:54:03 -0700615 public void onRemoveTab(Tab tab) {
Michael Kolb94827b62011-01-08 14:23:45 -0800616 TabView tv = mTabMap.get(tab);
617 if (tv != null) {
Michael Kolb2d59c322011-01-25 13:18:55 -0800618 animateTabOut(tab, tv);
619 } else {
620 mTabMap.remove(tab);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700621 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700622 }
623
Michael Kolba2b2ba82010-08-04 17:54:03 -0700624 public void onUrlAndTitle(Tab tab, String url, String title) {
Michael Kolb94827b62011-01-08 14:23:45 -0800625 TabView tv = mTabMap.get(tab);
626 if (tv != null) {
627 if (title != null) {
628 tv.setDisplayTitle(title);
629 } else if (url != null) {
630 tv.setDisplayTitle(UrlUtils.stripUrl(url));
631 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700632 }
633 }
634
Michael Kolba2b2ba82010-08-04 17:54:03 -0700635 private boolean isLoading() {
Michael Kolb94827b62011-01-08 14:23:45 -0800636 TabView tv = mTabMap.get(mTabControl.getCurrentTab());
637 if (tv != null) {
638 return tv.mInLoad;
Michael Kolb8233fac2010-10-26 16:08:53 -0700639 } else {
640 return false;
641 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700642 }
643
Michael Kolba2b2ba82010-08-04 17:54:03 -0700644}