blob: b0057ed09d7a333544d321cf9963e6025e1fb886 [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
233 // callback after fake titlebar is shown
234 void onShowTitleBar() {
Michael Kolba0e2a752010-12-12 15:27:56 -0800235 showTitleBarIndicator(false);
Michael Kolbed217742010-08-10 17:52:34 -0700236 }
237
238 // callback after fake titlebar is hidden
Michael Kolba2b2ba82010-08-04 17:54:03 -0700239 void onHideTitleBar() {
Michael Kolba0e2a752010-12-12 15:27:56 -0800240 showTitleBarIndicator(mVisibleTitleHeight == 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700241 Tab tab = mTabControl.getCurrentTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700242 tab.getWebView().requestFocus();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700243 }
244
Michael Kolbed217742010-08-10 17:52:34 -0700245 // webview scroll listener
246
247 @Override
Michael Kolba0e2a752010-12-12 15:27:56 -0800248 public void onScroll(int visibleTitleHeight) {
Michael Kolb376b5412010-12-15 11:52:57 -0800249 if (mUseQuickControls) return;
Michael Kolb8233fac2010-10-26 16:08:53 -0700250 // isLoading is using the current tab, which initially might not be set yet
John Reckdcf911c2010-12-22 16:32:31 -0800251 if (mTabControl.getCurrentTab() != null
252 && !isLoading()) {
253 if (visibleTitleHeight == 0) {
254 mUi.hideFakeTitleBar();
255 showTitleBarIndicator(true);
256 } else {
Michael Kolba0e2a752010-12-12 15:27:56 -0800257 showTitleBarIndicator(false);
Michael Kolbed217742010-08-10 17:52:34 -0700258 }
259 }
Michael Kolba0e2a752010-12-12 15:27:56 -0800260 mVisibleTitleHeight = visibleTitleHeight;
Michael Kolbed217742010-08-10 17:52:34 -0700261 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700262
263 @Override
264 public void createContextMenu(ContextMenu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700265 MenuInflater inflater = mActivity.getMenuInflater();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700266 inflater.inflate(R.menu.title_context, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -0700267 mActivity.onCreateContextMenu(menu, this, null);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700268 }
269
Michael Kolb94827b62011-01-08 14:23:45 -0800270 private TabView buildTabView(Tab tab) {
271 TabView tabview = new TabView(mActivity, tab);
272 mTabMap.put(tab, tabview);
273 tabview.setOnClickListener(this);
274 mTabs.addTab(tabview);
275 return tabview;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700276 }
277
Romain Guyc8373212011-01-07 19:15:16 -0800278 private static Bitmap getDrawableAsBitmap(Drawable drawable, int width, int height) {
279 Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
280 Canvas c = new Canvas(b);
281 drawable.setBounds(0, 0, width, height);
282 drawable.draw(c);
283 return b;
Michael Kolbb4cafc52011-01-12 16:55:04 -0800284 }
285
Michael Kolba2b2ba82010-08-04 17:54:03 -0700286 /**
287 * View used in the tab bar
288 */
289 class TabView extends LinearLayout implements OnClickListener {
290
Michael Kolb94827b62011-01-08 14:23:45 -0800291 Tab mTab;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700292 View mTabContent;
293 TextView mTitle;
Michael Kolba0e2a752010-12-12 15:27:56 -0800294 View mIndicator;
Michael Kolbae62fd42010-08-18 16:33:28 -0700295 View mIncognito;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700296 ImageView mIconView;
297 ImageView mLock;
298 ImageView mClose;
299 boolean mSelected;
300 boolean mInLoad;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800301 Path mPath;
302 int[] mWindowPos;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700303
304 /**
305 * @param context
306 */
Michael Kolb94827b62011-01-08 14:23:45 -0800307 public TabView(Context context, Tab tab) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700308 super(context);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800309 setWillNotDraw(false);
310 mPath = new Path();
311 mWindowPos = new int[2];
Michael Kolb94827b62011-01-08 14:23:45 -0800312 mTab = tab;
Michael Kolbed217742010-08-10 17:52:34 -0700313 setGravity(Gravity.CENTER_VERTICAL);
314 setOrientation(LinearLayout.HORIZONTAL);
Michael Kolba0e2a752010-12-12 15:27:56 -0800315 setPadding(mTabPadding, 0, 0, 0);
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100316 LayoutInflater inflater = LayoutInflater.from(getContext());
Michael Kolbed217742010-08-10 17:52:34 -0700317 mTabContent = inflater.inflate(R.layout.tab_title, this, true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700318 mTitle = (TextView) mTabContent.findViewById(R.id.title);
319 mIconView = (ImageView) mTabContent.findViewById(R.id.favicon);
320 mLock = (ImageView) mTabContent.findViewById(R.id.lock);
321 mClose = (ImageView) mTabContent.findViewById(R.id.close);
322 mClose.setOnClickListener(this);
Michael Kolbae62fd42010-08-18 16:33:28 -0700323 mIncognito = mTabContent.findViewById(R.id.incognito);
Michael Kolba0e2a752010-12-12 15:27:56 -0800324 mIndicator = mTabContent.findViewById(R.id.chevron);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700325 mSelected = false;
326 mInLoad = false;
327 // update the status
Michael Kolb94827b62011-01-08 14:23:45 -0800328 updateFromTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700329 }
330
Michael Kolba0e2a752010-12-12 15:27:56 -0800331 void showIndicator(boolean show) {
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800332 if (mSelected) {
333 mIndicator.setVisibility(show ? View.VISIBLE : View.GONE);
334 } else {
335 mIndicator.setVisibility(View.GONE);
336 }
Michael Kolba0e2a752010-12-12 15:27:56 -0800337 }
338
Michael Kolba2b2ba82010-08-04 17:54:03 -0700339 @Override
340 public void onClick(View v) {
341 if (v == mClose) {
342 closeTab();
343 }
344 }
345
Michael Kolb94827b62011-01-08 14:23:45 -0800346 private void updateFromTab() {
347 String displayTitle = mTab.getTitle();
John Reck30c714c2010-12-16 17:30:34 -0800348 if (displayTitle == null) {
Michael Kolb94827b62011-01-08 14:23:45 -0800349 displayTitle = mTab.getUrl();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700350 }
John Reck30c714c2010-12-16 17:30:34 -0800351 setDisplayTitle(displayTitle);
Michael Kolb94827b62011-01-08 14:23:45 -0800352 setProgress(mTab.getLoadProgress());
353 if (mTab.getFavicon() != null) {
354 setFavicon(renderFavicon(mTab.getFavicon()));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700355 }
Michael Kolb94827b62011-01-08 14:23:45 -0800356 if (mTab != null) {
Michael Kolbae62fd42010-08-18 16:33:28 -0700357 mIncognito.setVisibility(
Michael Kolb94827b62011-01-08 14:23:45 -0800358 mTab.isPrivateBrowsingEnabled() ?
Michael Kolbae62fd42010-08-18 16:33:28 -0700359 View.VISIBLE : View.GONE);
360 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700361 }
362
363 @Override
Michael Kolbb7b115e2010-09-25 16:59:37 -0700364 public void setActivated(boolean selected) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700365 mSelected = selected;
366 mClose.setVisibility(mSelected ? View.VISIBLE : View.GONE);
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800367 mIndicator.setVisibility(View.GONE);
Michael Kolb8233fac2010-10-26 16:08:53 -0700368 mTitle.setTextAppearance(mActivity, mSelected ?
Michael Kolbc7485ae2010-09-03 10:10:58 -0700369 R.style.TabTitleSelected : R.style.TabTitleUnselected);
370 setHorizontalFadingEdgeEnabled(!mSelected);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700371 super.setActivated(selected);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800372 LayoutParams lp = (LinearLayout.LayoutParams) getLayoutParams();
373 lp.width = selected ? mTabWidthSelected : mTabWidthUnselected;
374 lp.height = LayoutParams.MATCH_PARENT;
375 setLayoutParams(lp);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700376 }
377
378 void setDisplayTitle(String title) {
379 mTitle.setText(title);
380 }
381
382 void setFavicon(Drawable d) {
383 mIconView.setImageDrawable(d);
384 }
385
386 void setLock(Drawable d) {
387 if (null == d) {
388 mLock.setVisibility(View.GONE);
389 } else {
390 mLock.setImageDrawable(d);
391 mLock.setVisibility(View.VISIBLE);
392 }
393 }
394
Michael Kolba2b2ba82010-08-04 17:54:03 -0700395 void setProgress(int newProgress) {
396 if (newProgress >= PROGRESS_MAX) {
397 mInLoad = false;
398 } else {
399 if (!mInLoad && getWindowToken() != null) {
400 mInLoad = true;
401 }
402 }
403 }
404
405 private void closeTab() {
Michael Kolb94827b62011-01-08 14:23:45 -0800406 if (mTab == mTabControl.getCurrentTab()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700407 mUiController.closeCurrentTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700408 } else {
Michael Kolb94827b62011-01-08 14:23:45 -0800409 mUiController.closeTab(mTab);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700410 }
411 }
412
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800413 @Override
414 protected void onLayout(boolean changed, int l, int t, int r, int b) {
415 super.onLayout(changed, l, t, r, b);
416 setTabPath(mPath, 0, 0, r - l, b - t);
417 }
Michael Kolbb4cafc52011-01-12 16:55:04 -0800418
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800419 @Override
420 protected void dispatchDraw(Canvas canvas) {
Romain Guyc8373212011-01-07 19:15:16 -0800421 if (mCurrentTextureWidth != mUi.getContentWidth() ||
422 mCurrentTextureHeight != getHeight()) {
423 mCurrentTextureWidth = mUi.getContentWidth();
424 mCurrentTextureHeight = getHeight();
425
426 if (mCurrentTextureWidth > 0 && mCurrentTextureHeight > 0) {
427 Bitmap activeTexture = getDrawableAsBitmap(mActiveDrawable,
428 mCurrentTextureWidth, mCurrentTextureHeight);
429 Bitmap inactiveTexture = getDrawableAsBitmap(mInactiveDrawable,
430 mCurrentTextureWidth, mCurrentTextureHeight);
431
432 mActiveShader = new BitmapShader(activeTexture,
433 Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
434 mActiveShaderPaint.setShader(mActiveShader);
Michael Kolbb4cafc52011-01-12 16:55:04 -0800435
Romain Guyc8373212011-01-07 19:15:16 -0800436 mInactiveShader = new BitmapShader(inactiveTexture,
437 Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
438 mInactiveShaderPaint.setShader(mInactiveShader);
439 }
440 }
Michael Kolbb4cafc52011-01-12 16:55:04 -0800441
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800442 int state = canvas.save();
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800443 getLocationInWindow(mWindowPos);
Romain Guyc8373212011-01-07 19:15:16 -0800444 Paint paint = mSelected ? mActiveShaderPaint : mInactiveShaderPaint;
445 drawClipped(canvas, paint, mPath, mWindowPos[0]);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800446 canvas.restoreToCount(state);
447 super.dispatchDraw(canvas);
448 }
449
Romain Guyc8373212011-01-07 19:15:16 -0800450 private void drawClipped(Canvas canvas, Paint paint, Path clipPath, int left) {
451 // TODO: We should change the matrix/shader only when needed
452 final Matrix matrix = mSelected ? mActiveMatrix : mInactiveMatrix;
453 matrix.setTranslate(-left, 0.0f);
454 (mSelected ? mActiveShader : mInactiveShader).setLocalMatrix(matrix);
455 canvas.drawPath(clipPath, paint);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800456 }
457
458 private void setTabPath(Path path, int l, int t, int r, int b) {
459 path.reset();
460 path.moveTo(l, b);
461 path.lineTo(l, t);
462 path.lineTo(r - mTabSliceWidth, t);
463 path.lineTo(r, b);
464 path.close();
465 }
466
Michael Kolba2b2ba82010-08-04 17:54:03 -0700467 }
468
Michael Kolb94827b62011-01-08 14:23:45 -0800469 private Drawable renderFavicon(Bitmap icon) {
470 Drawable[] array = new Drawable[3];
471 array[0] = new PaintDrawable(Color.BLACK);
472 array[1] = new PaintDrawable(Color.WHITE);
473 if (icon == null) {
474 array[2] = mGenericFavicon;
475 } else {
476 array[2] = new BitmapDrawable(icon);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700477 }
Michael Kolb94827b62011-01-08 14:23:45 -0800478 LayerDrawable d = new LayerDrawable(array);
479 d.setLayerInset(1, 1, 1, 1, 1);
480 d.setLayerInset(2, 2, 2, 2, 2);
481 return d;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700482 }
483
484 // TabChangeListener implementation
485
Michael Kolb8233fac2010-10-26 16:08:53 -0700486 public void onSetActiveTab(Tab tab) {
487 mTabs.setSelectedTab(mTabControl.getTabIndex(tab));
Michael Kolb94827b62011-01-08 14:23:45 -0800488 TabView tv = mTabMap.get(tab);
489 if (tv != null) {
490 tv.setProgress(tv.mTab.getLoadProgress());
Michael Kolba2b2ba82010-08-04 17:54:03 -0700491 // update the scroll state
492 WebView webview = tab.getWebView();
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800493 if (webview != null) {
494 int h = webview.getVisibleTitleHeight();
495 mVisibleTitleHeight = h -1;
496 onScroll(h);
497 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700498 }
499 }
500
Michael Kolba2b2ba82010-08-04 17:54:03 -0700501 public void onFavicon(Tab tab, Bitmap favicon) {
Michael Kolb94827b62011-01-08 14:23:45 -0800502 TabView tv = mTabMap.get(tab);
503 if (tv != null) {
504 tv.setFavicon(renderFavicon(favicon));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700505 }
506 }
507
Michael Kolba2b2ba82010-08-04 17:54:03 -0700508 public void onNewTab(Tab tab) {
Michael Kolb94827b62011-01-08 14:23:45 -0800509 TabView tv = buildTabView(tab);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700510 }
511
Michael Kolba2b2ba82010-08-04 17:54:03 -0700512 public void onProgress(Tab tab, int progress) {
Michael Kolb94827b62011-01-08 14:23:45 -0800513 TabView tv = mTabMap.get(tab);
514 if (tv != null) {
515 tv.setProgress(progress);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700516 }
517 }
518
Michael Kolba2b2ba82010-08-04 17:54:03 -0700519 public void onRemoveTab(Tab tab) {
Michael Kolb94827b62011-01-08 14:23:45 -0800520 TabView tv = mTabMap.get(tab);
521 if (tv != null) {
522 mTabs.removeTab(tv);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700523 }
524 mTabMap.remove(tab);
525 }
526
Michael Kolba2b2ba82010-08-04 17:54:03 -0700527 public void onUrlAndTitle(Tab tab, String url, String title) {
Michael Kolb94827b62011-01-08 14:23:45 -0800528 TabView tv = mTabMap.get(tab);
529 if (tv != null) {
530 if (title != null) {
531 tv.setDisplayTitle(title);
532 } else if (url != null) {
533 tv.setDisplayTitle(UrlUtils.stripUrl(url));
534 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700535 }
536 }
537
Michael Kolba2b2ba82010-08-04 17:54:03 -0700538 private boolean isLoading() {
Michael Kolb94827b62011-01-08 14:23:45 -0800539 TabView tv = mTabMap.get(mTabControl.getCurrentTab());
540 if (tv != null) {
541 return tv.mInLoad;
Michael Kolb8233fac2010-10-26 16:08:53 -0700542 } else {
543 return false;
544 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700545 }
546
Michael Kolba2b2ba82010-08-04 17:54:03 -0700547}