blob: 1ab02eddda4229be86bba00888c23fbf0ebeddbd [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 Kolb8233fac2010-10-26 16:08:53 -070021import android.app.Activity;
Michael Kolba2b2ba82010-08-04 17:54:03 -070022import android.content.Context;
23import android.content.res.Resources;
24import android.graphics.Bitmap;
Michael Kolb2b5a13a2010-12-09 14:13:42 -080025import android.graphics.BitmapShader;
26import android.graphics.Canvas;
Michael Kolba2b2ba82010-08-04 17:54:03 -070027import android.graphics.Color;
Michael Kolb467af0a2011-01-11 13:09:49 -080028import android.graphics.Matrix;
Michael Kolb2b5a13a2010-12-09 14:13:42 -080029import android.graphics.Paint;
30import android.graphics.Path;
31import android.graphics.Shader;
Michael Kolba2b2ba82010-08-04 17:54:03 -070032import android.graphics.drawable.BitmapDrawable;
33import android.graphics.drawable.Drawable;
34import android.graphics.drawable.LayerDrawable;
35import android.graphics.drawable.PaintDrawable;
36import android.view.ContextMenu;
Michael Kolbed217742010-08-10 17:52:34 -070037import android.view.Gravity;
Michael Kolba2b2ba82010-08-04 17:54:03 -070038import android.view.LayoutInflater;
39import android.view.MenuInflater;
40import android.view.View;
41import android.view.View.OnClickListener;
42import android.webkit.WebView;
Michael Kolbed217742010-08-10 17:52:34 -070043import android.widget.ImageButton;
Michael Kolba2b2ba82010-08-04 17:54:03 -070044import android.widget.ImageView;
45import android.widget.LinearLayout;
46import android.widget.TextView;
47
Michael Kolba2b2ba82010-08-04 17:54:03 -070048import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -080049import java.util.List;
Michael Kolba2b2ba82010-08-04 17:54:03 -070050import java.util.Map;
51
52/**
53 * tabbed title bar for xlarge screen browser
54 */
55public class TabBar extends LinearLayout
Michael Kolb8233fac2010-10-26 16:08:53 -070056 implements ScrollListener, OnClickListener {
Michael Kolba2b2ba82010-08-04 17:54:03 -070057
58 private static final int PROGRESS_MAX = 100;
59
Michael Kolb8233fac2010-10-26 16:08:53 -070060 private Activity mActivity;
61 private UiController mUiController;
62 private TabControl mTabControl;
Michael Kolb66706532010-12-12 19:50:22 -080063 private XLargeUi mUi;
Michael Kolba2b2ba82010-08-04 17:54:03 -070064
Michael Kolbed217742010-08-10 17:52:34 -070065 private final int mTabWidthSelected;
66 private final int mTabWidthUnselected;
67
Michael Kolba2b2ba82010-08-04 17:54:03 -070068 private TabScrollView mTabs;
Michael Kolb8233fac2010-10-26 16:08:53 -070069
Michael Kolbebba8b42010-09-30 12:57:59 -070070 private ImageButton mNewTab;
71 private int mButtonWidth;
Michael Kolba2b2ba82010-08-04 17:54:03 -070072
Michael Kolb94827b62011-01-08 14:23:45 -080073 private Map<Tab, TabView> mTabMap;
Michael Kolba2b2ba82010-08-04 17:54:03 -070074
Michael Kolba0e2a752010-12-12 15:27:56 -080075 private int mVisibleTitleHeight;
Michael Kolba2b2ba82010-08-04 17:54:03 -070076
John Reckaff60fb2010-10-25 13:47:58 -070077 private Drawable mGenericFavicon;
78
Romain Guyc8373212011-01-07 19:15:16 -080079 private int mCurrentTextureWidth = 0;
80 private int mCurrentTextureHeight = 0;
81
Michael Kolb2b5a13a2010-12-09 14:13:42 -080082 private Drawable mActiveDrawable;
83 private Drawable mInactiveDrawable;
84
Romain Guyc8373212011-01-07 19:15:16 -080085 private final Paint mActiveShaderPaint = new Paint();
86 private final Paint mInactiveShaderPaint = new Paint();
87 private final Matrix mActiveMatrix = new Matrix();
88 private final Matrix mInactiveMatrix = new Matrix();
89
90 private BitmapShader mActiveShader;
91 private BitmapShader mInactiveShader;
92
Michael Kolb2b5a13a2010-12-09 14:13:42 -080093 private int mTabOverlap;
Michael Kolb5a72f182011-01-13 20:35:06 -080094 private int mAddTabOverlap;
Michael Kolb2b5a13a2010-12-09 14:13:42 -080095 private int mTabSliceWidth;
96 private int mTabPadding;
Michael Kolb376b5412010-12-15 11:52:57 -080097 private boolean mUseQuickControls;
Michael Kolb2b5a13a2010-12-09 14:13:42 -080098
Michael Kolb66706532010-12-12 19:50:22 -080099 public TabBar(Activity activity, UiController controller, XLargeUi ui) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700100 super(activity);
101 mActivity = activity;
102 mUiController = controller;
103 mTabControl = mUiController.getTabControl();
104 mUi = ui;
105 Resources res = activity.getResources();
Michael Kolbed217742010-08-10 17:52:34 -0700106 mTabWidthSelected = (int) res.getDimension(R.dimen.tab_width_selected);
107 mTabWidthUnselected = (int) res.getDimension(R.dimen.tab_width_unselected);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800108 mActiveDrawable = res.getDrawable(R.drawable.bg_urlbar);
109 mInactiveDrawable = res.getDrawable(R.drawable.browsertab_inactive);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700110
Michael Kolb94827b62011-01-08 14:23:45 -0800111 mTabMap = new HashMap<Tab, TabView>();
112 Resources resources = activity.getResources();
Michael Kolb8233fac2010-10-26 16:08:53 -0700113 LayoutInflater factory = LayoutInflater.from(activity);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700114 factory.inflate(R.layout.tab_bar, this);
Michael Kolbf558f0d2011-01-13 19:24:18 -0800115 setPadding(0, (int) res.getDimension(R.dimen.tab_padding_top), 0, 0);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700116 mTabs = (TabScrollView) findViewById(R.id.tabs);
Michael Kolbebba8b42010-09-30 12:57:59 -0700117 mNewTab = (ImageButton) findViewById(R.id.newtab);
118 mNewTab.setOnClickListener(this);
John Reckaff60fb2010-10-25 13:47:58 -0700119 mGenericFavicon = res.getDrawable(R.drawable.app_web_browser_sm);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700120
Michael Kolb1bf23132010-11-19 12:55:12 -0800121 updateTabs(mUiController.getTabs());
Michael Kolba2b2ba82010-08-04 17:54:03 -0700122
Michael Kolba0e2a752010-12-12 15:27:56 -0800123 mVisibleTitleHeight = 1;
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);
129 mTabPadding = (int) res.getDimension(R.dimen.tab_padding);
Romain Guyc8373212011-01-07 19:15:16 -0800130
131 mActiveShaderPaint.setStyle(Paint.Style.FILL);
132 mActiveShaderPaint.setAntiAlias(true);
133
134 mInactiveShaderPaint.setStyle(Paint.Style.FILL);
135 mInactiveShaderPaint.setAntiAlias(true);
Michael Kolbb4cafc52011-01-12 16:55:04 -0800136
Michael Kolbebba8b42010-09-30 12:57:59 -0700137 }
138
Michael Kolb376b5412010-12-15 11:52:57 -0800139 void setUseQuickControls(boolean useQuickControls) {
140 mUseQuickControls = useQuickControls;
Michael Kolb467af0a2011-01-11 13:09:49 -0800141 mNewTab.setVisibility(mUseQuickControls ? View.GONE
142 : View.VISIBLE);
Michael Kolb376b5412010-12-15 11:52:57 -0800143 }
144
145 int getTabCount() {
146 return mTabMap.size();
147 }
148
Michael Kolb1bf23132010-11-19 12:55:12 -0800149 void updateTabs(List<Tab> tabs) {
150 mTabs.clearTabs();
151 mTabMap.clear();
152 for (Tab tab : tabs) {
Michael Kolb94827b62011-01-08 14:23:45 -0800153 TabView tv = buildTabView(tab);
Michael Kolb1bf23132010-11-19 12:55:12 -0800154 }
155 mTabs.setSelectedTab(mTabControl.getCurrentIndex());
156 }
157
Michael Kolbebba8b42010-09-30 12:57:59 -0700158 @Override
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800159 protected void onMeasure(int hspec, int vspec) {
160 super.onMeasure(hspec, vspec);
161 int w = getMeasuredWidth();
162 // adjust for new tab overlap
Michael Kolb467af0a2011-01-11 13:09:49 -0800163 if (!mUseQuickControls) {
Michael Kolb5a72f182011-01-13 20:35:06 -0800164 w -= mAddTabOverlap;
Michael Kolb467af0a2011-01-11 13:09:49 -0800165 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800166 setMeasuredDimension(w, getMeasuredHeight());
167 }
168
169 @Override
Michael Kolbebba8b42010-09-30 12:57:59 -0700170 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800171 // use paddingLeft and paddingTop
172 int pl = getPaddingLeft();
173 int pt = getPaddingTop();
Michael Kolbebba8b42010-09-30 12:57:59 -0700174 int sw = mTabs.getMeasuredWidth();
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800175 int w = right - left - pl;
Michael Kolbb4cafc52011-01-12 16:55:04 -0800176 if (mUseQuickControls) {
177 mButtonWidth = 0;
178 } else {
Michael Kolb5a72f182011-01-13 20:35:06 -0800179 mButtonWidth = mNewTab.getMeasuredWidth() - mAddTabOverlap;
Michael Kolbb4cafc52011-01-12 16:55:04 -0800180 if (w-sw < mButtonWidth) {
181 sw = w - mButtonWidth;
182 }
Michael Kolbebba8b42010-09-30 12:57:59 -0700183 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800184 mTabs.layout(pl, pt, pl + sw, bottom - top);
185 // adjust for overlap
Michael Kolbb4cafc52011-01-12 16:55:04 -0800186 if (!mUseQuickControls) {
Michael Kolb5a72f182011-01-13 20:35:06 -0800187 mNewTab.layout(pl + sw - mAddTabOverlap, pt,
188 pl + sw + mButtonWidth - mAddTabOverlap, bottom - top);
Michael Kolbb4cafc52011-01-12 16:55:04 -0800189 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700190 }
191
192 public void onClick(View view) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700193 mUi.hideComboView();
Michael Kolbebba8b42010-09-30 12:57:59 -0700194 if (mNewTab == view) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700195 mUiController.openTabToHomePage();
Michael Kolbebba8b42010-09-30 12:57:59 -0700196 } else if (mTabs.getSelectedTab() == view) {
Michael Kolb467af0a2011-01-11 13:09:49 -0800197 if (mUseQuickControls) {
198 if (mUi.isFakeTitleBarShowing() && !isLoading()) {
199 mUi.hideFakeTitleBar();
200 } else {
201 mUi.stopWebViewScrolling();
202 mUi.showFakeTitleBarAndEdit();
203 }
204 } else if (mUi.isFakeTitleBarShowing() && !isLoading()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700205 mUi.hideFakeTitleBar();
Michael Kolbed217742010-08-10 17:52:34 -0700206 } else {
207 showUrlBar();
208 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700209 } else {
Michael Kolbebba8b42010-09-30 12:57:59 -0700210 int ix = mTabs.getChildIndex(view);
211 if (ix >= 0) {
212 mTabs.setSelectedTab(ix);
Michael Kolb8233fac2010-10-26 16:08:53 -0700213 mUiController.switchToTab(ix);
Michael Kolbebba8b42010-09-30 12:57:59 -0700214 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700215 }
216 }
217
Michael Kolbed217742010-08-10 17:52:34 -0700218 private void showUrlBar() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700219 mUi.stopWebViewScrolling();
220 mUi.showFakeTitleBar();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700221 }
222
Michael Kolb376b5412010-12-15 11:52:57 -0800223 void showTitleBarIndicator(boolean show) {
Michael Kolba0e2a752010-12-12 15:27:56 -0800224 Tab tab = mTabControl.getCurrentTab();
225 if (tab != null) {
Michael Kolb94827b62011-01-08 14:23:45 -0800226 TabView tv = mTabMap.get(tab);
227 if (tv != null) {
228 tv.showIndicator(show);
Michael Kolba0e2a752010-12-12 15:27:56 -0800229 }
230 }
Michael Kolbed217742010-08-10 17:52:34 -0700231 }
232
Michael Kolb7dad8f02011-01-21 11:06:18 -0800233 boolean showsTitleBarIndicator() {
234 Tab tab = mTabControl.getCurrentTab();
235 if (tab != null) {
236 TabView tv = mTabMap.get(tab);
237 if (tv != null) {
238 return tv.showsIndicator();
239 }
240 }
241 return false;
242 }
243
Michael Kolbed217742010-08-10 17:52:34 -0700244 // callback after fake titlebar is shown
245 void onShowTitleBar() {
Michael Kolba0e2a752010-12-12 15:27:56 -0800246 showTitleBarIndicator(false);
Michael Kolbed217742010-08-10 17:52:34 -0700247 }
248
249 // callback after fake titlebar is hidden
Michael Kolba2b2ba82010-08-04 17:54:03 -0700250 void onHideTitleBar() {
Michael Kolba0e2a752010-12-12 15:27:56 -0800251 showTitleBarIndicator(mVisibleTitleHeight == 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700252 Tab tab = mTabControl.getCurrentTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700253 tab.getWebView().requestFocus();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700254 }
255
Michael Kolbed217742010-08-10 17:52:34 -0700256 // webview scroll listener
257
258 @Override
Michael Kolba0e2a752010-12-12 15:27:56 -0800259 public void onScroll(int visibleTitleHeight) {
Michael Kolb376b5412010-12-15 11:52:57 -0800260 if (mUseQuickControls) return;
Michael Kolb8233fac2010-10-26 16:08:53 -0700261 // isLoading is using the current tab, which initially might not be set yet
John Reckdcf911c2010-12-22 16:32:31 -0800262 if (mTabControl.getCurrentTab() != null
263 && !isLoading()) {
264 if (visibleTitleHeight == 0) {
Michael Kolb7dad8f02011-01-21 11:06:18 -0800265 if (!showsTitleBarIndicator()) {
266 mUi.hideFakeTitleBar();
267 showTitleBarIndicator(true);
268 }
John Reckdcf911c2010-12-22 16:32:31 -0800269 } else {
Michael Kolb7dad8f02011-01-21 11:06:18 -0800270 if (showsTitleBarIndicator()) {
271 showTitleBarIndicator(false);
272 }
Michael Kolbed217742010-08-10 17:52:34 -0700273 }
274 }
Michael Kolba0e2a752010-12-12 15:27:56 -0800275 mVisibleTitleHeight = visibleTitleHeight;
Michael Kolbed217742010-08-10 17:52:34 -0700276 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700277
278 @Override
279 public void createContextMenu(ContextMenu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700280 MenuInflater inflater = mActivity.getMenuInflater();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700281 inflater.inflate(R.menu.title_context, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -0700282 mActivity.onCreateContextMenu(menu, this, null);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700283 }
284
Michael Kolb94827b62011-01-08 14:23:45 -0800285 private TabView buildTabView(Tab tab) {
286 TabView tabview = new TabView(mActivity, tab);
287 mTabMap.put(tab, tabview);
288 tabview.setOnClickListener(this);
289 mTabs.addTab(tabview);
290 return tabview;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700291 }
292
Romain Guyc8373212011-01-07 19:15:16 -0800293 private static Bitmap getDrawableAsBitmap(Drawable drawable, int width, int height) {
294 Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
295 Canvas c = new Canvas(b);
296 drawable.setBounds(0, 0, width, height);
297 drawable.draw(c);
298 return b;
Michael Kolbb4cafc52011-01-12 16:55:04 -0800299 }
300
Michael Kolba2b2ba82010-08-04 17:54:03 -0700301 /**
302 * View used in the tab bar
303 */
304 class TabView extends LinearLayout implements OnClickListener {
305
Michael Kolb94827b62011-01-08 14:23:45 -0800306 Tab mTab;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700307 View mTabContent;
308 TextView mTitle;
Michael Kolba0e2a752010-12-12 15:27:56 -0800309 View mIndicator;
Michael Kolbae62fd42010-08-18 16:33:28 -0700310 View mIncognito;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700311 ImageView mIconView;
312 ImageView mLock;
313 ImageView mClose;
314 boolean mSelected;
315 boolean mInLoad;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800316 Path mPath;
317 int[] mWindowPos;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700318
319 /**
320 * @param context
321 */
Michael Kolb94827b62011-01-08 14:23:45 -0800322 public TabView(Context context, Tab tab) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700323 super(context);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800324 setWillNotDraw(false);
325 mPath = new Path();
326 mWindowPos = new int[2];
Michael Kolb94827b62011-01-08 14:23:45 -0800327 mTab = tab;
Michael Kolbed217742010-08-10 17:52:34 -0700328 setGravity(Gravity.CENTER_VERTICAL);
329 setOrientation(LinearLayout.HORIZONTAL);
Michael Kolba0e2a752010-12-12 15:27:56 -0800330 setPadding(mTabPadding, 0, 0, 0);
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100331 LayoutInflater inflater = LayoutInflater.from(getContext());
Michael Kolbed217742010-08-10 17:52:34 -0700332 mTabContent = inflater.inflate(R.layout.tab_title, this, true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700333 mTitle = (TextView) mTabContent.findViewById(R.id.title);
334 mIconView = (ImageView) mTabContent.findViewById(R.id.favicon);
335 mLock = (ImageView) mTabContent.findViewById(R.id.lock);
336 mClose = (ImageView) mTabContent.findViewById(R.id.close);
337 mClose.setOnClickListener(this);
Michael Kolbae62fd42010-08-18 16:33:28 -0700338 mIncognito = mTabContent.findViewById(R.id.incognito);
Michael Kolba0e2a752010-12-12 15:27:56 -0800339 mIndicator = mTabContent.findViewById(R.id.chevron);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700340 mSelected = false;
341 mInLoad = false;
342 // update the status
Michael Kolb94827b62011-01-08 14:23:45 -0800343 updateFromTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700344 }
345
Michael Kolba0e2a752010-12-12 15:27:56 -0800346 void showIndicator(boolean show) {
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800347 if (mSelected) {
348 mIndicator.setVisibility(show ? View.VISIBLE : View.GONE);
349 } else {
350 mIndicator.setVisibility(View.GONE);
351 }
Michael Kolba0e2a752010-12-12 15:27:56 -0800352 }
353
Michael Kolb7dad8f02011-01-21 11:06:18 -0800354 boolean showsIndicator() {
355 return (mIndicator.getVisibility() == View.VISIBLE);
356 }
357
Michael Kolba2b2ba82010-08-04 17:54:03 -0700358 @Override
359 public void onClick(View v) {
360 if (v == mClose) {
361 closeTab();
362 }
363 }
364
Michael Kolb94827b62011-01-08 14:23:45 -0800365 private void updateFromTab() {
366 String displayTitle = mTab.getTitle();
John Reck30c714c2010-12-16 17:30:34 -0800367 if (displayTitle == null) {
Michael Kolb94827b62011-01-08 14:23:45 -0800368 displayTitle = mTab.getUrl();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700369 }
John Reck30c714c2010-12-16 17:30:34 -0800370 setDisplayTitle(displayTitle);
Michael Kolb94827b62011-01-08 14:23:45 -0800371 setProgress(mTab.getLoadProgress());
372 if (mTab.getFavicon() != null) {
373 setFavicon(renderFavicon(mTab.getFavicon()));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700374 }
Michael Kolb94827b62011-01-08 14:23:45 -0800375 if (mTab != null) {
Michael Kolbae62fd42010-08-18 16:33:28 -0700376 mIncognito.setVisibility(
Michael Kolb94827b62011-01-08 14:23:45 -0800377 mTab.isPrivateBrowsingEnabled() ?
Michael Kolbae62fd42010-08-18 16:33:28 -0700378 View.VISIBLE : View.GONE);
379 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700380 }
381
382 @Override
Michael Kolbb7b115e2010-09-25 16:59:37 -0700383 public void setActivated(boolean selected) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700384 mSelected = selected;
385 mClose.setVisibility(mSelected ? View.VISIBLE : View.GONE);
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800386 mIndicator.setVisibility(View.GONE);
Michael Kolb8233fac2010-10-26 16:08:53 -0700387 mTitle.setTextAppearance(mActivity, mSelected ?
Michael Kolbc7485ae2010-09-03 10:10:58 -0700388 R.style.TabTitleSelected : R.style.TabTitleUnselected);
389 setHorizontalFadingEdgeEnabled(!mSelected);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700390 super.setActivated(selected);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800391 LayoutParams lp = (LinearLayout.LayoutParams) getLayoutParams();
392 lp.width = selected ? mTabWidthSelected : mTabWidthUnselected;
393 lp.height = LayoutParams.MATCH_PARENT;
394 setLayoutParams(lp);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700395 }
396
397 void setDisplayTitle(String title) {
398 mTitle.setText(title);
399 }
400
401 void setFavicon(Drawable d) {
402 mIconView.setImageDrawable(d);
403 }
404
405 void setLock(Drawable d) {
406 if (null == d) {
407 mLock.setVisibility(View.GONE);
408 } else {
409 mLock.setImageDrawable(d);
410 mLock.setVisibility(View.VISIBLE);
411 }
412 }
413
Michael Kolba2b2ba82010-08-04 17:54:03 -0700414 void setProgress(int newProgress) {
415 if (newProgress >= PROGRESS_MAX) {
416 mInLoad = false;
417 } else {
418 if (!mInLoad && getWindowToken() != null) {
419 mInLoad = true;
420 }
421 }
422 }
423
424 private void closeTab() {
Michael Kolb94827b62011-01-08 14:23:45 -0800425 if (mTab == mTabControl.getCurrentTab()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700426 mUiController.closeCurrentTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700427 } else {
Michael Kolb94827b62011-01-08 14:23:45 -0800428 mUiController.closeTab(mTab);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700429 }
430 }
431
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800432 @Override
433 protected void onLayout(boolean changed, int l, int t, int r, int b) {
434 super.onLayout(changed, l, t, r, b);
435 setTabPath(mPath, 0, 0, r - l, b - t);
436 }
Michael Kolbb4cafc52011-01-12 16:55:04 -0800437
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800438 @Override
439 protected void dispatchDraw(Canvas canvas) {
Romain Guyc8373212011-01-07 19:15:16 -0800440 if (mCurrentTextureWidth != mUi.getContentWidth() ||
441 mCurrentTextureHeight != getHeight()) {
442 mCurrentTextureWidth = mUi.getContentWidth();
443 mCurrentTextureHeight = getHeight();
444
445 if (mCurrentTextureWidth > 0 && mCurrentTextureHeight > 0) {
446 Bitmap activeTexture = getDrawableAsBitmap(mActiveDrawable,
447 mCurrentTextureWidth, mCurrentTextureHeight);
448 Bitmap inactiveTexture = getDrawableAsBitmap(mInactiveDrawable,
449 mCurrentTextureWidth, mCurrentTextureHeight);
450
451 mActiveShader = new BitmapShader(activeTexture,
452 Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
453 mActiveShaderPaint.setShader(mActiveShader);
Michael Kolbb4cafc52011-01-12 16:55:04 -0800454
Romain Guyc8373212011-01-07 19:15:16 -0800455 mInactiveShader = new BitmapShader(inactiveTexture,
456 Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
457 mInactiveShaderPaint.setShader(mInactiveShader);
458 }
459 }
Michael Kolbb4cafc52011-01-12 16:55:04 -0800460
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800461 int state = canvas.save();
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800462 getLocationInWindow(mWindowPos);
Romain Guyc8373212011-01-07 19:15:16 -0800463 Paint paint = mSelected ? mActiveShaderPaint : mInactiveShaderPaint;
464 drawClipped(canvas, paint, mPath, mWindowPos[0]);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800465 canvas.restoreToCount(state);
466 super.dispatchDraw(canvas);
467 }
468
Romain Guyc8373212011-01-07 19:15:16 -0800469 private void drawClipped(Canvas canvas, Paint paint, Path clipPath, int left) {
470 // TODO: We should change the matrix/shader only when needed
471 final Matrix matrix = mSelected ? mActiveMatrix : mInactiveMatrix;
472 matrix.setTranslate(-left, 0.0f);
473 (mSelected ? mActiveShader : mInactiveShader).setLocalMatrix(matrix);
474 canvas.drawPath(clipPath, paint);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800475 }
476
477 private void setTabPath(Path path, int l, int t, int r, int b) {
478 path.reset();
479 path.moveTo(l, b);
480 path.lineTo(l, t);
481 path.lineTo(r - mTabSliceWidth, t);
482 path.lineTo(r, b);
483 path.close();
484 }
485
Michael Kolba2b2ba82010-08-04 17:54:03 -0700486 }
487
Michael Kolb94827b62011-01-08 14:23:45 -0800488 private Drawable renderFavicon(Bitmap icon) {
489 Drawable[] array = new Drawable[3];
490 array[0] = new PaintDrawable(Color.BLACK);
491 array[1] = new PaintDrawable(Color.WHITE);
492 if (icon == null) {
493 array[2] = mGenericFavicon;
494 } else {
495 array[2] = new BitmapDrawable(icon);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700496 }
Michael Kolb94827b62011-01-08 14:23:45 -0800497 LayerDrawable d = new LayerDrawable(array);
498 d.setLayerInset(1, 1, 1, 1, 1);
499 d.setLayerInset(2, 2, 2, 2, 2);
500 return d;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700501 }
502
503 // TabChangeListener implementation
504
Michael Kolb8233fac2010-10-26 16:08:53 -0700505 public void onSetActiveTab(Tab tab) {
506 mTabs.setSelectedTab(mTabControl.getTabIndex(tab));
Michael Kolb94827b62011-01-08 14:23:45 -0800507 TabView tv = mTabMap.get(tab);
508 if (tv != null) {
509 tv.setProgress(tv.mTab.getLoadProgress());
Michael Kolba2b2ba82010-08-04 17:54:03 -0700510 // update the scroll state
511 WebView webview = tab.getWebView();
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800512 if (webview != null) {
513 int h = webview.getVisibleTitleHeight();
514 mVisibleTitleHeight = h -1;
515 onScroll(h);
516 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700517 }
518 }
519
Michael Kolba2b2ba82010-08-04 17:54:03 -0700520 public void onFavicon(Tab tab, Bitmap favicon) {
Michael Kolb94827b62011-01-08 14:23:45 -0800521 TabView tv = mTabMap.get(tab);
522 if (tv != null) {
523 tv.setFavicon(renderFavicon(favicon));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700524 }
525 }
526
Michael Kolba2b2ba82010-08-04 17:54:03 -0700527 public void onNewTab(Tab tab) {
Michael Kolb94827b62011-01-08 14:23:45 -0800528 TabView tv = buildTabView(tab);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700529 }
530
Michael Kolba2b2ba82010-08-04 17:54:03 -0700531 public void onProgress(Tab tab, int progress) {
Michael Kolb94827b62011-01-08 14:23:45 -0800532 TabView tv = mTabMap.get(tab);
533 if (tv != null) {
534 tv.setProgress(progress);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700535 }
536 }
537
Michael Kolba2b2ba82010-08-04 17:54:03 -0700538 public void onRemoveTab(Tab tab) {
Michael Kolb94827b62011-01-08 14:23:45 -0800539 TabView tv = mTabMap.get(tab);
540 if (tv != null) {
541 mTabs.removeTab(tv);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700542 }
543 mTabMap.remove(tab);
544 }
545
Michael Kolba2b2ba82010-08-04 17:54:03 -0700546 public void onUrlAndTitle(Tab tab, String url, String title) {
Michael Kolb94827b62011-01-08 14:23:45 -0800547 TabView tv = mTabMap.get(tab);
548 if (tv != null) {
549 if (title != null) {
550 tv.setDisplayTitle(title);
551 } else if (url != null) {
552 tv.setDisplayTitle(UrlUtils.stripUrl(url));
553 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700554 }
555 }
556
Michael Kolba2b2ba82010-08-04 17:54:03 -0700557 private boolean isLoading() {
Michael Kolb94827b62011-01-08 14:23:45 -0800558 TabView tv = mTabMap.get(mTabControl.getCurrentTab());
559 if (tv != null) {
560 return tv.mInLoad;
Michael Kolb8233fac2010-10-26 16:08:53 -0700561 } else {
562 return false;
563 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700564 }
565
Michael Kolba2b2ba82010-08-04 17:54:03 -0700566}