blob: 502512a082e4c083208948aa5255872efb39eabc [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
Romain Guyc8373212011-01-07 19:15:16 -080019import android.graphics.Matrix;
Michael Kolbebba8b42010-09-30 12:57:59 -070020import com.android.browser.ScrollWebView.ScrollListener;
Michael Kolbebba8b42010-09-30 12:57:59 -070021
Michael Kolb8233fac2010-10-26 16:08:53 -070022import android.app.Activity;
Michael Kolba2b2ba82010-08-04 17:54:03 -070023import android.content.Context;
24import android.content.res.Resources;
25import android.graphics.Bitmap;
Michael Kolb2b5a13a2010-12-09 14:13:42 -080026import android.graphics.BitmapShader;
27import android.graphics.Canvas;
Michael Kolba2b2ba82010-08-04 17:54:03 -070028import android.graphics.Color;
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 Kolb2b5a13a2010-12-09 14:13:42 -0800114 setPadding(12, 12, 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 Kolb2b5a13a2010-12-09 14:13:42 -0800119 setChildrenDrawingOrderEnabled(true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700120
121 // TODO: Change enabled states based on whether you can go
122 // back/forward. Probably should be done inside onPageStarted.
123
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);
130 mTabSliceWidth = (int) res.getDimension(R.dimen.tab_slice);
131 mTabPadding = (int) res.getDimension(R.dimen.tab_padding);
Romain Guyc8373212011-01-07 19:15:16 -0800132
133 mActiveShaderPaint.setStyle(Paint.Style.FILL);
134 mActiveShaderPaint.setAntiAlias(true);
135
136 mInactiveShaderPaint.setStyle(Paint.Style.FILL);
137 mInactiveShaderPaint.setAntiAlias(true);
138
Michael Kolbebba8b42010-09-30 12:57:59 -0700139 }
140
Michael Kolb376b5412010-12-15 11:52:57 -0800141 void setUseQuickControls(boolean useQuickControls) {
142 mUseQuickControls = useQuickControls;
143 }
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
163 w -= mTabOverlap;
164 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 if (mButtonWidth == -1) {
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800173 mButtonWidth = mNewTab.getMeasuredWidth() - mTabOverlap;
Michael Kolbebba8b42010-09-30 12:57:59 -0700174 }
175 int sw = mTabs.getMeasuredWidth();
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800176 int w = right - left - pl;
Michael Kolbebba8b42010-09-30 12:57:59 -0700177 if (w-sw < mButtonWidth) {
178 sw = w - mButtonWidth;
179 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800180 mTabs.layout(pl, pt, pl + sw, bottom - top);
181 // adjust for overlap
182 mNewTab.layout(pl + sw - mTabOverlap, pt,
183 pl + sw + mButtonWidth - mTabOverlap, bottom - top);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700184 }
185
186 public void onClick(View view) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700187 mUi.hideComboView();
Michael Kolbebba8b42010-09-30 12:57:59 -0700188 if (mNewTab == view) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700189 mUiController.openTabToHomePage();
Michael Kolbebba8b42010-09-30 12:57:59 -0700190 } else if (mTabs.getSelectedTab() == view) {
Michael Kolb376b5412010-12-15 11:52:57 -0800191 if (mUseQuickControls) return;
Michael Kolb8233fac2010-10-26 16:08:53 -0700192 if (mUi.isFakeTitleBarShowing() && !isLoading()) {
193 mUi.hideFakeTitleBar();
Michael Kolbed217742010-08-10 17:52:34 -0700194 } else {
195 showUrlBar();
196 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700197 } else {
Michael Kolbebba8b42010-09-30 12:57:59 -0700198 int ix = mTabs.getChildIndex(view);
199 if (ix >= 0) {
200 mTabs.setSelectedTab(ix);
Michael Kolb8233fac2010-10-26 16:08:53 -0700201 mUiController.switchToTab(ix);
Michael Kolbebba8b42010-09-30 12:57:59 -0700202 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700203 }
204 }
205
Michael Kolbed217742010-08-10 17:52:34 -0700206 private void showUrlBar() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700207 mUi.stopWebViewScrolling();
208 mUi.showFakeTitleBar();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700209 }
210
Michael Kolb376b5412010-12-15 11:52:57 -0800211 void showTitleBarIndicator(boolean show) {
Michael Kolba0e2a752010-12-12 15:27:56 -0800212 Tab tab = mTabControl.getCurrentTab();
213 if (tab != null) {
Michael Kolb94827b62011-01-08 14:23:45 -0800214 TabView tv = mTabMap.get(tab);
215 if (tv != null) {
216 tv.showIndicator(show);
Michael Kolba0e2a752010-12-12 15:27:56 -0800217 }
218 }
Michael Kolbed217742010-08-10 17:52:34 -0700219 }
220
221 // callback after fake titlebar is shown
222 void onShowTitleBar() {
Michael Kolba0e2a752010-12-12 15:27:56 -0800223 showTitleBarIndicator(false);
Michael Kolbed217742010-08-10 17:52:34 -0700224 }
225
226 // callback after fake titlebar is hidden
Michael Kolba2b2ba82010-08-04 17:54:03 -0700227 void onHideTitleBar() {
Michael Kolba0e2a752010-12-12 15:27:56 -0800228 showTitleBarIndicator(mVisibleTitleHeight == 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700229 Tab tab = mTabControl.getCurrentTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700230 tab.getWebView().requestFocus();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700231 }
232
Michael Kolbed217742010-08-10 17:52:34 -0700233 // webview scroll listener
234
235 @Override
Michael Kolba0e2a752010-12-12 15:27:56 -0800236 public void onScroll(int visibleTitleHeight) {
Michael Kolb376b5412010-12-15 11:52:57 -0800237 if (mUseQuickControls) return;
Michael Kolb8233fac2010-10-26 16:08:53 -0700238 // isLoading is using the current tab, which initially might not be set yet
John Reckdcf911c2010-12-22 16:32:31 -0800239 if (mTabControl.getCurrentTab() != null
240 && !isLoading()) {
241 if (visibleTitleHeight == 0) {
242 mUi.hideFakeTitleBar();
243 showTitleBarIndicator(true);
244 } else {
Michael Kolba0e2a752010-12-12 15:27:56 -0800245 showTitleBarIndicator(false);
Michael Kolbed217742010-08-10 17:52:34 -0700246 }
247 }
Michael Kolba0e2a752010-12-12 15:27:56 -0800248 mVisibleTitleHeight = visibleTitleHeight;
Michael Kolbed217742010-08-10 17:52:34 -0700249 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700250
251 @Override
252 public void createContextMenu(ContextMenu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700253 MenuInflater inflater = mActivity.getMenuInflater();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700254 inflater.inflate(R.menu.title_context, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -0700255 mActivity.onCreateContextMenu(menu, this, null);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700256 }
257
Michael Kolb94827b62011-01-08 14:23:45 -0800258 private TabView buildTabView(Tab tab) {
259 TabView tabview = new TabView(mActivity, tab);
260 mTabMap.put(tab, tabview);
261 tabview.setOnClickListener(this);
262 mTabs.addTab(tabview);
263 return tabview;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700264 }
265
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800266 @Override
267 protected int getChildDrawingOrder(int count, int i) {
268 // reverse
269 return count - 1 - i;
270 }
271
Romain Guyc8373212011-01-07 19:15:16 -0800272 private static Bitmap getDrawableAsBitmap(Drawable drawable, int width, int height) {
273 Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
274 Canvas c = new Canvas(b);
275 drawable.setBounds(0, 0, width, height);
276 drawable.draw(c);
277 return b;
278 }
279
Michael Kolba2b2ba82010-08-04 17:54:03 -0700280 /**
281 * View used in the tab bar
282 */
283 class TabView extends LinearLayout implements OnClickListener {
284
Michael Kolb94827b62011-01-08 14:23:45 -0800285 Tab mTab;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700286 View mTabContent;
287 TextView mTitle;
Michael Kolba0e2a752010-12-12 15:27:56 -0800288 View mIndicator;
Michael Kolbae62fd42010-08-18 16:33:28 -0700289 View mIncognito;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700290 ImageView mIconView;
291 ImageView mLock;
292 ImageView mClose;
293 boolean mSelected;
294 boolean mInLoad;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800295 Path mPath;
296 int[] mWindowPos;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700297
298 /**
299 * @param context
300 */
Michael Kolb94827b62011-01-08 14:23:45 -0800301 public TabView(Context context, Tab tab) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700302 super(context);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800303 setWillNotDraw(false);
304 mPath = new Path();
305 mWindowPos = new int[2];
Michael Kolb94827b62011-01-08 14:23:45 -0800306 mTab = tab;
Michael Kolbed217742010-08-10 17:52:34 -0700307 setGravity(Gravity.CENTER_VERTICAL);
308 setOrientation(LinearLayout.HORIZONTAL);
Michael Kolba0e2a752010-12-12 15:27:56 -0800309 setPadding(mTabPadding, 0, 0, 0);
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100310 LayoutInflater inflater = LayoutInflater.from(getContext());
Michael Kolbed217742010-08-10 17:52:34 -0700311 mTabContent = inflater.inflate(R.layout.tab_title, this, true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700312 mTitle = (TextView) mTabContent.findViewById(R.id.title);
313 mIconView = (ImageView) mTabContent.findViewById(R.id.favicon);
314 mLock = (ImageView) mTabContent.findViewById(R.id.lock);
315 mClose = (ImageView) mTabContent.findViewById(R.id.close);
316 mClose.setOnClickListener(this);
Michael Kolbae62fd42010-08-18 16:33:28 -0700317 mIncognito = mTabContent.findViewById(R.id.incognito);
Michael Kolba0e2a752010-12-12 15:27:56 -0800318 mIndicator = mTabContent.findViewById(R.id.chevron);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700319 mSelected = false;
320 mInLoad = false;
321 // update the status
Michael Kolb94827b62011-01-08 14:23:45 -0800322 updateFromTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700323 }
324
Michael Kolba0e2a752010-12-12 15:27:56 -0800325 void showIndicator(boolean show) {
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800326 if (mSelected) {
327 mIndicator.setVisibility(show ? View.VISIBLE : View.GONE);
328 } else {
329 mIndicator.setVisibility(View.GONE);
330 }
Michael Kolba0e2a752010-12-12 15:27:56 -0800331 }
332
Michael Kolba2b2ba82010-08-04 17:54:03 -0700333 @Override
334 public void onClick(View v) {
335 if (v == mClose) {
336 closeTab();
337 }
338 }
339
Michael Kolb94827b62011-01-08 14:23:45 -0800340 private void updateFromTab() {
341 String displayTitle = mTab.getTitle();
John Reck30c714c2010-12-16 17:30:34 -0800342 if (displayTitle == null) {
Michael Kolb94827b62011-01-08 14:23:45 -0800343 displayTitle = mTab.getUrl();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700344 }
John Reck30c714c2010-12-16 17:30:34 -0800345 setDisplayTitle(displayTitle);
Michael Kolb94827b62011-01-08 14:23:45 -0800346 setProgress(mTab.getLoadProgress());
347 if (mTab.getFavicon() != null) {
348 setFavicon(renderFavicon(mTab.getFavicon()));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700349 }
Michael Kolb94827b62011-01-08 14:23:45 -0800350 if (mTab != null) {
Michael Kolbae62fd42010-08-18 16:33:28 -0700351 mIncognito.setVisibility(
Michael Kolb94827b62011-01-08 14:23:45 -0800352 mTab.isPrivateBrowsingEnabled() ?
Michael Kolbae62fd42010-08-18 16:33:28 -0700353 View.VISIBLE : View.GONE);
354 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700355 }
356
357 @Override
Michael Kolbb7b115e2010-09-25 16:59:37 -0700358 public void setActivated(boolean selected) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700359 mSelected = selected;
360 mClose.setVisibility(mSelected ? View.VISIBLE : View.GONE);
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800361 mIndicator.setVisibility(View.GONE);
Michael Kolb8233fac2010-10-26 16:08:53 -0700362 mTitle.setTextAppearance(mActivity, mSelected ?
Michael Kolbc7485ae2010-09-03 10:10:58 -0700363 R.style.TabTitleSelected : R.style.TabTitleUnselected);
364 setHorizontalFadingEdgeEnabled(!mSelected);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700365 super.setActivated(selected);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800366 LayoutParams lp = (LinearLayout.LayoutParams) getLayoutParams();
367 lp.width = selected ? mTabWidthSelected : mTabWidthUnselected;
368 lp.height = LayoutParams.MATCH_PARENT;
369 setLayoutParams(lp);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700370 }
371
372 void setDisplayTitle(String title) {
373 mTitle.setText(title);
374 }
375
376 void setFavicon(Drawable d) {
377 mIconView.setImageDrawable(d);
378 }
379
380 void setLock(Drawable d) {
381 if (null == d) {
382 mLock.setVisibility(View.GONE);
383 } else {
384 mLock.setImageDrawable(d);
385 mLock.setVisibility(View.VISIBLE);
386 }
387 }
388
Michael Kolba2b2ba82010-08-04 17:54:03 -0700389 void setProgress(int newProgress) {
390 if (newProgress >= PROGRESS_MAX) {
391 mInLoad = false;
392 } else {
393 if (!mInLoad && getWindowToken() != null) {
394 mInLoad = true;
395 }
396 }
397 }
398
399 private void closeTab() {
Michael Kolb94827b62011-01-08 14:23:45 -0800400 if (mTab == mTabControl.getCurrentTab()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700401 mUiController.closeCurrentTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700402 } else {
Michael Kolb94827b62011-01-08 14:23:45 -0800403 mUiController.closeTab(mTab);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700404 }
405 }
406
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800407 @Override
408 protected void onLayout(boolean changed, int l, int t, int r, int b) {
409 super.onLayout(changed, l, t, r, b);
410 setTabPath(mPath, 0, 0, r - l, b - t);
411 }
Romain Guyc8373212011-01-07 19:15:16 -0800412
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800413 @Override
414 protected void dispatchDraw(Canvas canvas) {
Romain Guyc8373212011-01-07 19:15:16 -0800415 if (mCurrentTextureWidth != mUi.getContentWidth() ||
416 mCurrentTextureHeight != getHeight()) {
417 mCurrentTextureWidth = mUi.getContentWidth();
418 mCurrentTextureHeight = getHeight();
419
420 if (mCurrentTextureWidth > 0 && mCurrentTextureHeight > 0) {
421 Bitmap activeTexture = getDrawableAsBitmap(mActiveDrawable,
422 mCurrentTextureWidth, mCurrentTextureHeight);
423 Bitmap inactiveTexture = getDrawableAsBitmap(mInactiveDrawable,
424 mCurrentTextureWidth, mCurrentTextureHeight);
425
426 mActiveShader = new BitmapShader(activeTexture,
427 Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
428 mActiveShaderPaint.setShader(mActiveShader);
429
430 mInactiveShader = new BitmapShader(inactiveTexture,
431 Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
432 mInactiveShaderPaint.setShader(mInactiveShader);
433 }
434 }
435
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800436 int state = canvas.save();
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800437 getLocationInWindow(mWindowPos);
Romain Guyc8373212011-01-07 19:15:16 -0800438 Paint paint = mSelected ? mActiveShaderPaint : mInactiveShaderPaint;
439 drawClipped(canvas, paint, mPath, mWindowPos[0]);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800440 canvas.restoreToCount(state);
441 super.dispatchDraw(canvas);
442 }
443
Romain Guyc8373212011-01-07 19:15:16 -0800444 private void drawClipped(Canvas canvas, Paint paint, Path clipPath, int left) {
445 // TODO: We should change the matrix/shader only when needed
446 final Matrix matrix = mSelected ? mActiveMatrix : mInactiveMatrix;
447 matrix.setTranslate(-left, 0.0f);
448 (mSelected ? mActiveShader : mInactiveShader).setLocalMatrix(matrix);
449 canvas.drawPath(clipPath, paint);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800450 }
451
452 private void setTabPath(Path path, int l, int t, int r, int b) {
453 path.reset();
454 path.moveTo(l, b);
455 path.lineTo(l, t);
456 path.lineTo(r - mTabSliceWidth, t);
457 path.lineTo(r, b);
458 path.close();
459 }
460
Michael Kolba2b2ba82010-08-04 17:54:03 -0700461 }
462
Michael Kolb94827b62011-01-08 14:23:45 -0800463 private Drawable renderFavicon(Bitmap icon) {
464 Drawable[] array = new Drawable[3];
465 array[0] = new PaintDrawable(Color.BLACK);
466 array[1] = new PaintDrawable(Color.WHITE);
467 if (icon == null) {
468 array[2] = mGenericFavicon;
469 } else {
470 array[2] = new BitmapDrawable(icon);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700471 }
Michael Kolb94827b62011-01-08 14:23:45 -0800472 LayerDrawable d = new LayerDrawable(array);
473 d.setLayerInset(1, 1, 1, 1, 1);
474 d.setLayerInset(2, 2, 2, 2, 2);
475 return d;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700476 }
477
478 // TabChangeListener implementation
479
Michael Kolb8233fac2010-10-26 16:08:53 -0700480 public void onSetActiveTab(Tab tab) {
481 mTabs.setSelectedTab(mTabControl.getTabIndex(tab));
Michael Kolb94827b62011-01-08 14:23:45 -0800482 TabView tv = mTabMap.get(tab);
483 if (tv != null) {
484 tv.setProgress(tv.mTab.getLoadProgress());
Michael Kolba2b2ba82010-08-04 17:54:03 -0700485 // update the scroll state
486 WebView webview = tab.getWebView();
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800487 if (webview != null) {
488 int h = webview.getVisibleTitleHeight();
489 mVisibleTitleHeight = h -1;
490 onScroll(h);
491 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700492 }
493 }
494
Michael Kolba2b2ba82010-08-04 17:54:03 -0700495 public void onFavicon(Tab tab, Bitmap favicon) {
Michael Kolb94827b62011-01-08 14:23:45 -0800496 TabView tv = mTabMap.get(tab);
497 if (tv != null) {
498 tv.setFavicon(renderFavicon(favicon));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700499 }
500 }
501
Michael Kolba2b2ba82010-08-04 17:54:03 -0700502 public void onNewTab(Tab tab) {
Michael Kolb94827b62011-01-08 14:23:45 -0800503 TabView tv = buildTabView(tab);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700504 }
505
Michael Kolba2b2ba82010-08-04 17:54:03 -0700506 public void onProgress(Tab tab, int progress) {
Michael Kolb94827b62011-01-08 14:23:45 -0800507 TabView tv = mTabMap.get(tab);
508 if (tv != null) {
509 tv.setProgress(progress);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700510 }
511 }
512
Michael Kolba2b2ba82010-08-04 17:54:03 -0700513 public void onRemoveTab(Tab tab) {
Michael Kolb94827b62011-01-08 14:23:45 -0800514 TabView tv = mTabMap.get(tab);
515 if (tv != null) {
516 mTabs.removeTab(tv);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700517 }
518 mTabMap.remove(tab);
519 }
520
Michael Kolba2b2ba82010-08-04 17:54:03 -0700521 public void onUrlAndTitle(Tab tab, String url, String title) {
Michael Kolb94827b62011-01-08 14:23:45 -0800522 TabView tv = mTabMap.get(tab);
523 if (tv != null) {
524 if (title != null) {
525 tv.setDisplayTitle(title);
526 } else if (url != null) {
527 tv.setDisplayTitle(UrlUtils.stripUrl(url));
528 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700529 }
530 }
531
Michael Kolba2b2ba82010-08-04 17:54:03 -0700532 private boolean isLoading() {
Michael Kolb94827b62011-01-08 14:23:45 -0800533 TabView tv = mTabMap.get(mTabControl.getCurrentTab());
534 if (tv != null) {
535 return tv.mInLoad;
Michael Kolb8233fac2010-10-26 16:08:53 -0700536 } else {
537 return false;
538 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700539 }
540
Michael Kolba2b2ba82010-08-04 17:54:03 -0700541}