blob: b872cad08217b6b063b1fd90358b7bce75ecc98d [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;
94 private int mTabSliceWidth;
95 private int mTabPadding;
Michael Kolb376b5412010-12-15 11:52:57 -080096 private boolean mUseQuickControls;
Michael Kolb2b5a13a2010-12-09 14:13:42 -080097
Michael Kolb66706532010-12-12 19:50:22 -080098 public TabBar(Activity activity, UiController controller, XLargeUi ui) {
Michael Kolb8233fac2010-10-26 16:08:53 -070099 super(activity);
100 mActivity = activity;
101 mUiController = controller;
102 mTabControl = mUiController.getTabControl();
103 mUi = ui;
104 Resources res = activity.getResources();
Michael Kolbed217742010-08-10 17:52:34 -0700105 mTabWidthSelected = (int) res.getDimension(R.dimen.tab_width_selected);
106 mTabWidthUnselected = (int) res.getDimension(R.dimen.tab_width_unselected);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800107 mActiveDrawable = res.getDrawable(R.drawable.bg_urlbar);
108 mInactiveDrawable = res.getDrawable(R.drawable.browsertab_inactive);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700109
Michael Kolb94827b62011-01-08 14:23:45 -0800110 mTabMap = new HashMap<Tab, TabView>();
111 Resources resources = activity.getResources();
Michael Kolb8233fac2010-10-26 16:08:53 -0700112 LayoutInflater factory = LayoutInflater.from(activity);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700113 factory.inflate(R.layout.tab_bar, this);
Michael Kolbf558f0d2011-01-13 19:24:18 -0800114 setPadding(0, (int) res.getDimension(R.dimen.tab_padding_top), 0, 0);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700115 mTabs = (TabScrollView) findViewById(R.id.tabs);
Michael Kolbebba8b42010-09-30 12:57:59 -0700116 mNewTab = (ImageButton) findViewById(R.id.newtab);
117 mNewTab.setOnClickListener(this);
John Reckaff60fb2010-10-25 13:47:58 -0700118 mGenericFavicon = res.getDrawable(R.drawable.app_web_browser_sm);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700119
Michael Kolb1bf23132010-11-19 12:55:12 -0800120 updateTabs(mUiController.getTabs());
Michael Kolba2b2ba82010-08-04 17:54:03 -0700121
Michael Kolba0e2a752010-12-12 15:27:56 -0800122 mVisibleTitleHeight = 1;
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);
126 mTabSliceWidth = (int) res.getDimension(R.dimen.tab_slice);
127 mTabPadding = (int) res.getDimension(R.dimen.tab_padding);
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 Kolbebba8b42010-09-30 12:57:59 -0700135 }
136
Michael Kolb376b5412010-12-15 11:52:57 -0800137 void setUseQuickControls(boolean useQuickControls) {
138 mUseQuickControls = useQuickControls;
Michael Kolb467af0a2011-01-11 13:09:49 -0800139 mNewTab.setVisibility(mUseQuickControls ? View.GONE
140 : View.VISIBLE);
Michael Kolb376b5412010-12-15 11:52:57 -0800141 }
142
143 int getTabCount() {
144 return mTabMap.size();
145 }
146
Michael Kolb1bf23132010-11-19 12:55:12 -0800147 void updateTabs(List<Tab> tabs) {
148 mTabs.clearTabs();
149 mTabMap.clear();
150 for (Tab tab : tabs) {
Michael Kolb94827b62011-01-08 14:23:45 -0800151 TabView tv = buildTabView(tab);
Michael Kolb1bf23132010-11-19 12:55:12 -0800152 }
153 mTabs.setSelectedTab(mTabControl.getCurrentIndex());
154 }
155
Michael Kolbebba8b42010-09-30 12:57:59 -0700156 @Override
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800157 protected void onMeasure(int hspec, int vspec) {
158 super.onMeasure(hspec, vspec);
159 int w = getMeasuredWidth();
160 // adjust for new tab overlap
Michael Kolb467af0a2011-01-11 13:09:49 -0800161 if (!mUseQuickControls) {
162 w -= mTabOverlap;
163 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800164 setMeasuredDimension(w, getMeasuredHeight());
165 }
166
167 @Override
Michael Kolbebba8b42010-09-30 12:57:59 -0700168 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800169 // use paddingLeft and paddingTop
170 int pl = getPaddingLeft();
171 int pt = getPaddingTop();
Michael Kolbebba8b42010-09-30 12:57:59 -0700172 int sw = mTabs.getMeasuredWidth();
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800173 int w = right - left - pl;
Michael Kolbb4cafc52011-01-12 16:55:04 -0800174 if (mUseQuickControls) {
175 mButtonWidth = 0;
176 } else {
177 mButtonWidth = mNewTab.getMeasuredWidth() - mTabOverlap;
178 if (w-sw < mButtonWidth) {
179 sw = w - mButtonWidth;
180 }
Michael Kolbebba8b42010-09-30 12:57:59 -0700181 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800182 mTabs.layout(pl, pt, pl + sw, bottom - top);
183 // adjust for overlap
Michael Kolbb4cafc52011-01-12 16:55:04 -0800184 if (!mUseQuickControls) {
185 mNewTab.layout(pl + sw - mTabOverlap, pt,
186 pl + sw + mButtonWidth - mTabOverlap, bottom - top);
187 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700188 }
189
190 public void onClick(View view) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700191 mUi.hideComboView();
Michael Kolbebba8b42010-09-30 12:57:59 -0700192 if (mNewTab == view) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700193 mUiController.openTabToHomePage();
Michael Kolbebba8b42010-09-30 12:57:59 -0700194 } else if (mTabs.getSelectedTab() == view) {
Michael Kolb467af0a2011-01-11 13:09:49 -0800195 if (mUseQuickControls) {
196 if (mUi.isFakeTitleBarShowing() && !isLoading()) {
197 mUi.hideFakeTitleBar();
198 } else {
199 mUi.stopWebViewScrolling();
200 mUi.showFakeTitleBarAndEdit();
201 }
202 } else if (mUi.isFakeTitleBarShowing() && !isLoading()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700203 mUi.hideFakeTitleBar();
Michael Kolbed217742010-08-10 17:52:34 -0700204 } else {
205 showUrlBar();
206 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700207 } else {
Michael Kolbebba8b42010-09-30 12:57:59 -0700208 int ix = mTabs.getChildIndex(view);
209 if (ix >= 0) {
210 mTabs.setSelectedTab(ix);
Michael Kolb8233fac2010-10-26 16:08:53 -0700211 mUiController.switchToTab(ix);
Michael Kolbebba8b42010-09-30 12:57:59 -0700212 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700213 }
214 }
215
Michael Kolbed217742010-08-10 17:52:34 -0700216 private void showUrlBar() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700217 mUi.stopWebViewScrolling();
218 mUi.showFakeTitleBar();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700219 }
220
Michael Kolb376b5412010-12-15 11:52:57 -0800221 void showTitleBarIndicator(boolean show) {
Michael Kolba0e2a752010-12-12 15:27:56 -0800222 Tab tab = mTabControl.getCurrentTab();
223 if (tab != null) {
Michael Kolb94827b62011-01-08 14:23:45 -0800224 TabView tv = mTabMap.get(tab);
225 if (tv != null) {
226 tv.showIndicator(show);
Michael Kolba0e2a752010-12-12 15:27:56 -0800227 }
228 }
Michael Kolbed217742010-08-10 17:52:34 -0700229 }
230
231 // callback after fake titlebar is shown
232 void onShowTitleBar() {
Michael Kolba0e2a752010-12-12 15:27:56 -0800233 showTitleBarIndicator(false);
Michael Kolbed217742010-08-10 17:52:34 -0700234 }
235
236 // callback after fake titlebar is hidden
Michael Kolba2b2ba82010-08-04 17:54:03 -0700237 void onHideTitleBar() {
Michael Kolba0e2a752010-12-12 15:27:56 -0800238 showTitleBarIndicator(mVisibleTitleHeight == 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700239 Tab tab = mTabControl.getCurrentTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700240 tab.getWebView().requestFocus();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700241 }
242
Michael Kolbed217742010-08-10 17:52:34 -0700243 // webview scroll listener
244
245 @Override
Michael Kolba0e2a752010-12-12 15:27:56 -0800246 public void onScroll(int visibleTitleHeight) {
Michael Kolb376b5412010-12-15 11:52:57 -0800247 if (mUseQuickControls) return;
Michael Kolb8233fac2010-10-26 16:08:53 -0700248 // isLoading is using the current tab, which initially might not be set yet
John Reckdcf911c2010-12-22 16:32:31 -0800249 if (mTabControl.getCurrentTab() != null
250 && !isLoading()) {
251 if (visibleTitleHeight == 0) {
252 mUi.hideFakeTitleBar();
253 showTitleBarIndicator(true);
254 } else {
Michael Kolba0e2a752010-12-12 15:27:56 -0800255 showTitleBarIndicator(false);
Michael Kolbed217742010-08-10 17:52:34 -0700256 }
257 }
Michael Kolba0e2a752010-12-12 15:27:56 -0800258 mVisibleTitleHeight = visibleTitleHeight;
Michael Kolbed217742010-08-10 17:52:34 -0700259 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700260
261 @Override
262 public void createContextMenu(ContextMenu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700263 MenuInflater inflater = mActivity.getMenuInflater();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700264 inflater.inflate(R.menu.title_context, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -0700265 mActivity.onCreateContextMenu(menu, this, null);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700266 }
267
Michael Kolb94827b62011-01-08 14:23:45 -0800268 private TabView buildTabView(Tab tab) {
269 TabView tabview = new TabView(mActivity, tab);
270 mTabMap.put(tab, tabview);
271 tabview.setOnClickListener(this);
272 mTabs.addTab(tabview);
273 return tabview;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700274 }
275
Romain Guyc8373212011-01-07 19:15:16 -0800276 private static Bitmap getDrawableAsBitmap(Drawable drawable, int width, int height) {
277 Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
278 Canvas c = new Canvas(b);
279 drawable.setBounds(0, 0, width, height);
280 drawable.draw(c);
281 return b;
Michael Kolbb4cafc52011-01-12 16:55:04 -0800282 }
283
Michael Kolba2b2ba82010-08-04 17:54:03 -0700284 /**
285 * View used in the tab bar
286 */
287 class TabView extends LinearLayout implements OnClickListener {
288
Michael Kolb94827b62011-01-08 14:23:45 -0800289 Tab mTab;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700290 View mTabContent;
291 TextView mTitle;
Michael Kolba0e2a752010-12-12 15:27:56 -0800292 View mIndicator;
Michael Kolbae62fd42010-08-18 16:33:28 -0700293 View mIncognito;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700294 ImageView mIconView;
295 ImageView mLock;
296 ImageView mClose;
297 boolean mSelected;
298 boolean mInLoad;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800299 Path mPath;
300 int[] mWindowPos;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700301
302 /**
303 * @param context
304 */
Michael Kolb94827b62011-01-08 14:23:45 -0800305 public TabView(Context context, Tab tab) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700306 super(context);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800307 setWillNotDraw(false);
308 mPath = new Path();
309 mWindowPos = new int[2];
Michael Kolb94827b62011-01-08 14:23:45 -0800310 mTab = tab;
Michael Kolbed217742010-08-10 17:52:34 -0700311 setGravity(Gravity.CENTER_VERTICAL);
312 setOrientation(LinearLayout.HORIZONTAL);
Michael Kolba0e2a752010-12-12 15:27:56 -0800313 setPadding(mTabPadding, 0, 0, 0);
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100314 LayoutInflater inflater = LayoutInflater.from(getContext());
Michael Kolbed217742010-08-10 17:52:34 -0700315 mTabContent = inflater.inflate(R.layout.tab_title, this, true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700316 mTitle = (TextView) mTabContent.findViewById(R.id.title);
317 mIconView = (ImageView) mTabContent.findViewById(R.id.favicon);
318 mLock = (ImageView) mTabContent.findViewById(R.id.lock);
319 mClose = (ImageView) mTabContent.findViewById(R.id.close);
320 mClose.setOnClickListener(this);
Michael Kolbae62fd42010-08-18 16:33:28 -0700321 mIncognito = mTabContent.findViewById(R.id.incognito);
Michael Kolba0e2a752010-12-12 15:27:56 -0800322 mIndicator = mTabContent.findViewById(R.id.chevron);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700323 mSelected = false;
324 mInLoad = false;
325 // update the status
Michael Kolb94827b62011-01-08 14:23:45 -0800326 updateFromTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700327 }
328
Michael Kolba0e2a752010-12-12 15:27:56 -0800329 void showIndicator(boolean show) {
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800330 if (mSelected) {
331 mIndicator.setVisibility(show ? View.VISIBLE : View.GONE);
332 } else {
333 mIndicator.setVisibility(View.GONE);
334 }
Michael Kolba0e2a752010-12-12 15:27:56 -0800335 }
336
Michael Kolba2b2ba82010-08-04 17:54:03 -0700337 @Override
338 public void onClick(View v) {
339 if (v == mClose) {
340 closeTab();
341 }
342 }
343
Michael Kolb94827b62011-01-08 14:23:45 -0800344 private void updateFromTab() {
345 String displayTitle = mTab.getTitle();
John Reck30c714c2010-12-16 17:30:34 -0800346 if (displayTitle == null) {
Michael Kolb94827b62011-01-08 14:23:45 -0800347 displayTitle = mTab.getUrl();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700348 }
John Reck30c714c2010-12-16 17:30:34 -0800349 setDisplayTitle(displayTitle);
Michael Kolb94827b62011-01-08 14:23:45 -0800350 setProgress(mTab.getLoadProgress());
351 if (mTab.getFavicon() != null) {
352 setFavicon(renderFavicon(mTab.getFavicon()));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700353 }
Michael Kolb94827b62011-01-08 14:23:45 -0800354 if (mTab != null) {
Michael Kolbae62fd42010-08-18 16:33:28 -0700355 mIncognito.setVisibility(
Michael Kolb94827b62011-01-08 14:23:45 -0800356 mTab.isPrivateBrowsingEnabled() ?
Michael Kolbae62fd42010-08-18 16:33:28 -0700357 View.VISIBLE : View.GONE);
358 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700359 }
360
361 @Override
Michael Kolbb7b115e2010-09-25 16:59:37 -0700362 public void setActivated(boolean selected) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700363 mSelected = selected;
364 mClose.setVisibility(mSelected ? View.VISIBLE : View.GONE);
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800365 mIndicator.setVisibility(View.GONE);
Michael Kolb8233fac2010-10-26 16:08:53 -0700366 mTitle.setTextAppearance(mActivity, mSelected ?
Michael Kolbc7485ae2010-09-03 10:10:58 -0700367 R.style.TabTitleSelected : R.style.TabTitleUnselected);
368 setHorizontalFadingEdgeEnabled(!mSelected);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700369 super.setActivated(selected);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800370 LayoutParams lp = (LinearLayout.LayoutParams) getLayoutParams();
371 lp.width = selected ? mTabWidthSelected : mTabWidthUnselected;
372 lp.height = LayoutParams.MATCH_PARENT;
373 setLayoutParams(lp);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700374 }
375
376 void setDisplayTitle(String title) {
377 mTitle.setText(title);
378 }
379
380 void setFavicon(Drawable d) {
381 mIconView.setImageDrawable(d);
382 }
383
384 void setLock(Drawable d) {
385 if (null == d) {
386 mLock.setVisibility(View.GONE);
387 } else {
388 mLock.setImageDrawable(d);
389 mLock.setVisibility(View.VISIBLE);
390 }
391 }
392
Michael Kolba2b2ba82010-08-04 17:54:03 -0700393 void setProgress(int newProgress) {
394 if (newProgress >= PROGRESS_MAX) {
395 mInLoad = false;
396 } else {
397 if (!mInLoad && getWindowToken() != null) {
398 mInLoad = true;
399 }
400 }
401 }
402
403 private void closeTab() {
Michael Kolb94827b62011-01-08 14:23:45 -0800404 if (mTab == mTabControl.getCurrentTab()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700405 mUiController.closeCurrentTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700406 } else {
Michael Kolb94827b62011-01-08 14:23:45 -0800407 mUiController.closeTab(mTab);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700408 }
409 }
410
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800411 @Override
412 protected void onLayout(boolean changed, int l, int t, int r, int b) {
413 super.onLayout(changed, l, t, r, b);
414 setTabPath(mPath, 0, 0, r - l, b - t);
415 }
Michael Kolbb4cafc52011-01-12 16:55:04 -0800416
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800417 @Override
418 protected void dispatchDraw(Canvas canvas) {
Romain Guyc8373212011-01-07 19:15:16 -0800419 if (mCurrentTextureWidth != mUi.getContentWidth() ||
420 mCurrentTextureHeight != getHeight()) {
421 mCurrentTextureWidth = mUi.getContentWidth();
422 mCurrentTextureHeight = getHeight();
423
424 if (mCurrentTextureWidth > 0 && mCurrentTextureHeight > 0) {
425 Bitmap activeTexture = getDrawableAsBitmap(mActiveDrawable,
426 mCurrentTextureWidth, mCurrentTextureHeight);
427 Bitmap inactiveTexture = getDrawableAsBitmap(mInactiveDrawable,
428 mCurrentTextureWidth, mCurrentTextureHeight);
429
430 mActiveShader = new BitmapShader(activeTexture,
431 Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
432 mActiveShaderPaint.setShader(mActiveShader);
Michael Kolbb4cafc52011-01-12 16:55:04 -0800433
Romain Guyc8373212011-01-07 19:15:16 -0800434 mInactiveShader = new BitmapShader(inactiveTexture,
435 Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
436 mInactiveShaderPaint.setShader(mInactiveShader);
437 }
438 }
Michael Kolbb4cafc52011-01-12 16:55:04 -0800439
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800440 int state = canvas.save();
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800441 getLocationInWindow(mWindowPos);
Romain Guyc8373212011-01-07 19:15:16 -0800442 Paint paint = mSelected ? mActiveShaderPaint : mInactiveShaderPaint;
443 drawClipped(canvas, paint, mPath, mWindowPos[0]);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800444 canvas.restoreToCount(state);
445 super.dispatchDraw(canvas);
446 }
447
Romain Guyc8373212011-01-07 19:15:16 -0800448 private void drawClipped(Canvas canvas, Paint paint, Path clipPath, int left) {
449 // TODO: We should change the matrix/shader only when needed
450 final Matrix matrix = mSelected ? mActiveMatrix : mInactiveMatrix;
451 matrix.setTranslate(-left, 0.0f);
452 (mSelected ? mActiveShader : mInactiveShader).setLocalMatrix(matrix);
453 canvas.drawPath(clipPath, paint);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800454 }
455
456 private void setTabPath(Path path, int l, int t, int r, int b) {
457 path.reset();
458 path.moveTo(l, b);
459 path.lineTo(l, t);
460 path.lineTo(r - mTabSliceWidth, t);
461 path.lineTo(r, b);
462 path.close();
463 }
464
Michael Kolba2b2ba82010-08-04 17:54:03 -0700465 }
466
Michael Kolb94827b62011-01-08 14:23:45 -0800467 private Drawable renderFavicon(Bitmap icon) {
468 Drawable[] array = new Drawable[3];
469 array[0] = new PaintDrawable(Color.BLACK);
470 array[1] = new PaintDrawable(Color.WHITE);
471 if (icon == null) {
472 array[2] = mGenericFavicon;
473 } else {
474 array[2] = new BitmapDrawable(icon);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700475 }
Michael Kolb94827b62011-01-08 14:23:45 -0800476 LayerDrawable d = new LayerDrawable(array);
477 d.setLayerInset(1, 1, 1, 1, 1);
478 d.setLayerInset(2, 2, 2, 2, 2);
479 return d;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700480 }
481
482 // TabChangeListener implementation
483
Michael Kolb8233fac2010-10-26 16:08:53 -0700484 public void onSetActiveTab(Tab tab) {
485 mTabs.setSelectedTab(mTabControl.getTabIndex(tab));
Michael Kolb94827b62011-01-08 14:23:45 -0800486 TabView tv = mTabMap.get(tab);
487 if (tv != null) {
488 tv.setProgress(tv.mTab.getLoadProgress());
Michael Kolba2b2ba82010-08-04 17:54:03 -0700489 // update the scroll state
490 WebView webview = tab.getWebView();
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800491 if (webview != null) {
492 int h = webview.getVisibleTitleHeight();
493 mVisibleTitleHeight = h -1;
494 onScroll(h);
495 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700496 }
497 }
498
Michael Kolba2b2ba82010-08-04 17:54:03 -0700499 public void onFavicon(Tab tab, Bitmap favicon) {
Michael Kolb94827b62011-01-08 14:23:45 -0800500 TabView tv = mTabMap.get(tab);
501 if (tv != null) {
502 tv.setFavicon(renderFavicon(favicon));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700503 }
504 }
505
Michael Kolba2b2ba82010-08-04 17:54:03 -0700506 public void onNewTab(Tab tab) {
Michael Kolb94827b62011-01-08 14:23:45 -0800507 TabView tv = buildTabView(tab);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700508 }
509
Michael Kolba2b2ba82010-08-04 17:54:03 -0700510 public void onProgress(Tab tab, int progress) {
Michael Kolb94827b62011-01-08 14:23:45 -0800511 TabView tv = mTabMap.get(tab);
512 if (tv != null) {
513 tv.setProgress(progress);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700514 }
515 }
516
Michael Kolba2b2ba82010-08-04 17:54:03 -0700517 public void onRemoveTab(Tab tab) {
Michael Kolb94827b62011-01-08 14:23:45 -0800518 TabView tv = mTabMap.get(tab);
519 if (tv != null) {
520 mTabs.removeTab(tv);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700521 }
522 mTabMap.remove(tab);
523 }
524
Michael Kolba2b2ba82010-08-04 17:54:03 -0700525 public void onUrlAndTitle(Tab tab, String url, String title) {
Michael Kolb94827b62011-01-08 14:23:45 -0800526 TabView tv = mTabMap.get(tab);
527 if (tv != null) {
528 if (title != null) {
529 tv.setDisplayTitle(title);
530 } else if (url != null) {
531 tv.setDisplayTitle(UrlUtils.stripUrl(url));
532 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700533 }
534 }
535
Michael Kolba2b2ba82010-08-04 17:54:03 -0700536 private boolean isLoading() {
Michael Kolb94827b62011-01-08 14:23:45 -0800537 TabView tv = mTabMap.get(mTabControl.getCurrentTab());
538 if (tv != null) {
539 return tv.mInLoad;
Michael Kolb8233fac2010-10-26 16:08:53 -0700540 } else {
541 return false;
542 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700543 }
544
Michael Kolba2b2ba82010-08-04 17:54:03 -0700545}