blob: e36c2048721bab0155f2664e74ad936ee54c0dd0 [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
73 private Map<Tab, TabViewData> mTabMap;
74
Michael Kolba2b2ba82010-08-04 17:54:03 -070075 private boolean mUserRequestedUrlbar;
Michael Kolba0e2a752010-12-12 15:27:56 -080076 private int mVisibleTitleHeight;
Michael Kolba2b2ba82010-08-04 17:54:03 -070077
John Reckaff60fb2010-10-25 13:47:58 -070078 private Drawable mGenericFavicon;
79
Romain Guyc8373212011-01-07 19:15:16 -080080 private int mCurrentTextureWidth = 0;
81 private int mCurrentTextureHeight = 0;
82
Michael Kolb2b5a13a2010-12-09 14:13:42 -080083 private Drawable mActiveDrawable;
84 private Drawable mInactiveDrawable;
85
Romain Guyc8373212011-01-07 19:15:16 -080086 private final Paint mActiveShaderPaint = new Paint();
87 private final Paint mInactiveShaderPaint = new Paint();
88 private final Matrix mActiveMatrix = new Matrix();
89 private final Matrix mInactiveMatrix = new Matrix();
90
91 private BitmapShader mActiveShader;
92 private BitmapShader mInactiveShader;
93
Michael Kolb2b5a13a2010-12-09 14:13:42 -080094 private int mTabOverlap;
95 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 Kolba2b2ba82010-08-04 17:54:03 -0700111 mTabMap = new HashMap<Tab, TabViewData>();
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 Kolba2b2ba82010-08-04 17:54:03 -0700126 mUserRequestedUrlbar = false;
Michael Kolba0e2a752010-12-12 15:27:56 -0800127 mVisibleTitleHeight = 1;
Michael Kolbebba8b42010-09-30 12:57:59 -0700128 mButtonWidth = -1;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800129 // tab dimensions
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800130 mTabOverlap = (int) res.getDimension(R.dimen.tab_overlap);
131 mTabSliceWidth = (int) res.getDimension(R.dimen.tab_slice);
132 mTabPadding = (int) res.getDimension(R.dimen.tab_padding);
Romain Guyc8373212011-01-07 19:15:16 -0800133
134 mActiveShaderPaint.setStyle(Paint.Style.FILL);
135 mActiveShaderPaint.setAntiAlias(true);
136
137 mInactiveShaderPaint.setStyle(Paint.Style.FILL);
138 mInactiveShaderPaint.setAntiAlias(true);
139
Michael Kolbebba8b42010-09-30 12:57:59 -0700140 }
141
Michael Kolb376b5412010-12-15 11:52:57 -0800142 void setUseQuickControls(boolean useQuickControls) {
143 mUseQuickControls = useQuickControls;
144 }
145
146 int getTabCount() {
147 return mTabMap.size();
148 }
149
Michael Kolb1bf23132010-11-19 12:55:12 -0800150 void updateTabs(List<Tab> tabs) {
151 mTabs.clearTabs();
152 mTabMap.clear();
153 for (Tab tab : tabs) {
154 TabViewData data = buildTab(tab);
155 TabView tv = buildView(data);
156 }
157 mTabs.setSelectedTab(mTabControl.getCurrentIndex());
158 }
159
Michael Kolbebba8b42010-09-30 12:57:59 -0700160 @Override
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800161 protected void onMeasure(int hspec, int vspec) {
162 super.onMeasure(hspec, vspec);
163 int w = getMeasuredWidth();
164 // adjust for new tab overlap
165 w -= mTabOverlap;
166 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 if (mButtonWidth == -1) {
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800175 mButtonWidth = mNewTab.getMeasuredWidth() - mTabOverlap;
Michael Kolbebba8b42010-09-30 12:57:59 -0700176 }
177 int sw = mTabs.getMeasuredWidth();
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800178 int w = right - left - pl;
Michael Kolbebba8b42010-09-30 12:57:59 -0700179 if (w-sw < mButtonWidth) {
180 sw = w - mButtonWidth;
181 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800182 mTabs.layout(pl, pt, pl + sw, bottom - top);
183 // adjust for overlap
184 mNewTab.layout(pl + sw - mTabOverlap, pt,
185 pl + sw + mButtonWidth - mTabOverlap, bottom - top);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700186 }
187
188 public void onClick(View view) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700189 mUi.hideComboView();
Michael Kolbebba8b42010-09-30 12:57:59 -0700190 if (mNewTab == view) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700191 mUiController.openTabToHomePage();
Michael Kolbebba8b42010-09-30 12:57:59 -0700192 } else if (mTabs.getSelectedTab() == view) {
Michael Kolb376b5412010-12-15 11:52:57 -0800193 if (mUseQuickControls) return;
Michael Kolb8233fac2010-10-26 16:08:53 -0700194 if (mUi.isFakeTitleBarShowing() && !isLoading()) {
195 mUi.hideFakeTitleBar();
Michael Kolbed217742010-08-10 17:52:34 -0700196 } else {
197 showUrlBar();
198 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700199 } else {
Michael Kolbebba8b42010-09-30 12:57:59 -0700200 int ix = mTabs.getChildIndex(view);
201 if (ix >= 0) {
202 mTabs.setSelectedTab(ix);
Michael Kolb8233fac2010-10-26 16:08:53 -0700203 mUiController.switchToTab(ix);
Michael Kolbebba8b42010-09-30 12:57:59 -0700204 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700205 }
206 }
207
Michael Kolbed217742010-08-10 17:52:34 -0700208 private void showUrlBar() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700209 mUi.stopWebViewScrolling();
210 mUi.showFakeTitleBar();
Michael Kolbed217742010-08-10 17:52:34 -0700211 mUserRequestedUrlbar = true;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700212 }
213
Michael Kolb376b5412010-12-15 11:52:57 -0800214 void showTitleBarIndicator(boolean show) {
Michael Kolba0e2a752010-12-12 15:27:56 -0800215 Tab tab = mTabControl.getCurrentTab();
216 if (tab != null) {
217 TabViewData tvd = mTabMap.get(tab);
218 if (tvd != null) {
219 tvd.mTabView.showIndicator(show);
220 }
221 }
Michael Kolbed217742010-08-10 17:52:34 -0700222 }
223
224 // callback after fake titlebar is shown
225 void onShowTitleBar() {
Michael Kolba0e2a752010-12-12 15:27:56 -0800226 showTitleBarIndicator(false);
Michael Kolbed217742010-08-10 17:52:34 -0700227 }
228
229 // callback after fake titlebar is hidden
Michael Kolba2b2ba82010-08-04 17:54:03 -0700230 void onHideTitleBar() {
Michael Kolba0e2a752010-12-12 15:27:56 -0800231 showTitleBarIndicator(mVisibleTitleHeight == 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700232 Tab tab = mTabControl.getCurrentTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700233 tab.getWebView().requestFocus();
234 mUserRequestedUrlbar = false;
235 }
236
Michael Kolbed217742010-08-10 17:52:34 -0700237 // webview scroll listener
238
239 @Override
Michael Kolba0e2a752010-12-12 15:27:56 -0800240 public void onScroll(int visibleTitleHeight) {
Michael Kolb376b5412010-12-15 11:52:57 -0800241 if (mUseQuickControls) return;
Michael Kolb8233fac2010-10-26 16:08:53 -0700242 // isLoading is using the current tab, which initially might not be set yet
John Reckdcf911c2010-12-22 16:32:31 -0800243 if (mTabControl.getCurrentTab() != null
244 && !isLoading()) {
245 if (visibleTitleHeight == 0) {
246 mUi.hideFakeTitleBar();
247 showTitleBarIndicator(true);
248 } else {
Michael Kolba0e2a752010-12-12 15:27:56 -0800249 showTitleBarIndicator(false);
Michael Kolbed217742010-08-10 17:52:34 -0700250 }
251 }
Michael Kolba0e2a752010-12-12 15:27:56 -0800252 mVisibleTitleHeight = visibleTitleHeight;
Michael Kolbed217742010-08-10 17:52:34 -0700253 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700254
255 @Override
256 public void createContextMenu(ContextMenu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700257 MenuInflater inflater = mActivity.getMenuInflater();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700258 inflater.inflate(R.menu.title_context, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -0700259 mActivity.onCreateContextMenu(menu, this, null);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700260 }
261
262 private TabViewData buildTab(Tab tab) {
263 TabViewData data = new TabViewData(tab);
264 mTabMap.put(tab, data);
265 return data;
266 }
267
268 private TabView buildView(final TabViewData data) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700269 TabView tv = new TabView(mActivity, data);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700270 tv.setTag(data);
271 tv.setOnClickListener(this);
272 mTabs.addTab(tv);
273 return tv;
274 }
275
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800276 @Override
277 protected int getChildDrawingOrder(int count, int i) {
278 // reverse
279 return count - 1 - i;
280 }
281
Romain Guyc8373212011-01-07 19:15:16 -0800282 private static Bitmap getDrawableAsBitmap(Drawable drawable, int width, int height) {
283 Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
284 Canvas c = new Canvas(b);
285 drawable.setBounds(0, 0, width, height);
286 drawable.draw(c);
287 return b;
288 }
289
Michael Kolba2b2ba82010-08-04 17:54:03 -0700290 /**
291 * View used in the tab bar
292 */
293 class TabView extends LinearLayout implements OnClickListener {
294
295 TabViewData mTabData;
296 View mTabContent;
297 TextView mTitle;
Michael Kolba0e2a752010-12-12 15:27:56 -0800298 View mIndicator;
Michael Kolbae62fd42010-08-18 16:33:28 -0700299 View mIncognito;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700300 ImageView mIconView;
301 ImageView mLock;
302 ImageView mClose;
303 boolean mSelected;
304 boolean mInLoad;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800305 Path mPath;
306 int[] mWindowPos;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700307
308 /**
309 * @param context
310 */
311 public TabView(Context context, TabViewData tab) {
312 super(context);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800313 setWillNotDraw(false);
314 mPath = new Path();
315 mWindowPos = new int[2];
Michael Kolba2b2ba82010-08-04 17:54:03 -0700316 mTabData = tab;
Michael Kolbed217742010-08-10 17:52:34 -0700317 setGravity(Gravity.CENTER_VERTICAL);
318 setOrientation(LinearLayout.HORIZONTAL);
Michael Kolba0e2a752010-12-12 15:27:56 -0800319 setPadding(mTabPadding, 0, 0, 0);
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100320 LayoutInflater inflater = LayoutInflater.from(getContext());
Michael Kolbed217742010-08-10 17:52:34 -0700321 mTabContent = inflater.inflate(R.layout.tab_title, this, true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700322 mTitle = (TextView) mTabContent.findViewById(R.id.title);
323 mIconView = (ImageView) mTabContent.findViewById(R.id.favicon);
324 mLock = (ImageView) mTabContent.findViewById(R.id.lock);
325 mClose = (ImageView) mTabContent.findViewById(R.id.close);
326 mClose.setOnClickListener(this);
Michael Kolbae62fd42010-08-18 16:33:28 -0700327 mIncognito = mTabContent.findViewById(R.id.incognito);
Michael Kolba0e2a752010-12-12 15:27:56 -0800328 mIndicator = mTabContent.findViewById(R.id.chevron);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700329 mSelected = false;
330 mInLoad = false;
331 // update the status
332 updateFromData();
333 }
334
Michael Kolba0e2a752010-12-12 15:27:56 -0800335 void showIndicator(boolean show) {
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800336 if (mSelected) {
337 mIndicator.setVisibility(show ? View.VISIBLE : View.GONE);
338 } else {
339 mIndicator.setVisibility(View.GONE);
340 }
Michael Kolba0e2a752010-12-12 15:27:56 -0800341 }
342
Michael Kolba2b2ba82010-08-04 17:54:03 -0700343 @Override
344 public void onClick(View v) {
345 if (v == mClose) {
346 closeTab();
347 }
348 }
349
350 private void updateFromData() {
351 mTabData.mTabView = this;
John Reck30c714c2010-12-16 17:30:34 -0800352 Tab tab = mTabData.mTab;
353 String displayTitle = tab.getTitle();
354 if (displayTitle == null) {
355 displayTitle = tab.getUrl();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700356 }
John Reck30c714c2010-12-16 17:30:34 -0800357 setDisplayTitle(displayTitle);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700358 setProgress(mTabData.mProgress);
359 if (mTabData.mIcon != null) {
360 setFavicon(mTabData.mIcon);
361 }
Michael Kolbae62fd42010-08-18 16:33:28 -0700362 if (mTabData.mTab != null) {
363 mIncognito.setVisibility(
Michael Kolb1bf23132010-11-19 12:55:12 -0800364 mTabData.mTab.isPrivateBrowsingEnabled() ?
Michael Kolbae62fd42010-08-18 16:33:28 -0700365 View.VISIBLE : View.GONE);
366 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700367 }
368
369 @Override
Michael Kolbb7b115e2010-09-25 16:59:37 -0700370 public void setActivated(boolean selected) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700371 mSelected = selected;
372 mClose.setVisibility(mSelected ? View.VISIBLE : View.GONE);
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800373 mIndicator.setVisibility(View.GONE);
Michael Kolb8233fac2010-10-26 16:08:53 -0700374 mTitle.setTextAppearance(mActivity, mSelected ?
Michael Kolbc7485ae2010-09-03 10:10:58 -0700375 R.style.TabTitleSelected : R.style.TabTitleUnselected);
376 setHorizontalFadingEdgeEnabled(!mSelected);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700377 super.setActivated(selected);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800378 LayoutParams lp = (LinearLayout.LayoutParams) getLayoutParams();
379 lp.width = selected ? mTabWidthSelected : mTabWidthUnselected;
380 lp.height = LayoutParams.MATCH_PARENT;
381 setLayoutParams(lp);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700382 }
383
384 void setDisplayTitle(String title) {
385 mTitle.setText(title);
386 }
387
388 void setFavicon(Drawable d) {
389 mIconView.setImageDrawable(d);
390 }
391
392 void setLock(Drawable d) {
393 if (null == d) {
394 mLock.setVisibility(View.GONE);
395 } else {
396 mLock.setImageDrawable(d);
397 mLock.setVisibility(View.VISIBLE);
398 }
399 }
400
Michael Kolba2b2ba82010-08-04 17:54:03 -0700401 void setProgress(int newProgress) {
402 if (newProgress >= PROGRESS_MAX) {
403 mInLoad = false;
404 } else {
405 if (!mInLoad && getWindowToken() != null) {
406 mInLoad = true;
407 }
408 }
409 }
410
411 private void closeTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700412 if (mTabData.mTab == mTabControl.getCurrentTab()) {
413 mUiController.closeCurrentTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700414 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -0700415 mUiController.closeTab(mTabData.mTab);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700416 }
417 }
418
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800419 @Override
420 protected void onLayout(boolean changed, int l, int t, int r, int b) {
421 super.onLayout(changed, l, t, r, b);
422 setTabPath(mPath, 0, 0, r - l, b - t);
423 }
Romain Guyc8373212011-01-07 19:15:16 -0800424
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800425 @Override
426 protected void dispatchDraw(Canvas canvas) {
Romain Guyc8373212011-01-07 19:15:16 -0800427 if (mCurrentTextureWidth != mUi.getContentWidth() ||
428 mCurrentTextureHeight != getHeight()) {
429 mCurrentTextureWidth = mUi.getContentWidth();
430 mCurrentTextureHeight = getHeight();
431
432 if (mCurrentTextureWidth > 0 && mCurrentTextureHeight > 0) {
433 Bitmap activeTexture = getDrawableAsBitmap(mActiveDrawable,
434 mCurrentTextureWidth, mCurrentTextureHeight);
435 Bitmap inactiveTexture = getDrawableAsBitmap(mInactiveDrawable,
436 mCurrentTextureWidth, mCurrentTextureHeight);
437
438 mActiveShader = new BitmapShader(activeTexture,
439 Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
440 mActiveShaderPaint.setShader(mActiveShader);
441
442 mInactiveShader = new BitmapShader(inactiveTexture,
443 Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
444 mInactiveShaderPaint.setShader(mInactiveShader);
445 }
446 }
447
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800448 int state = canvas.save();
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800449 getLocationInWindow(mWindowPos);
Romain Guyc8373212011-01-07 19:15:16 -0800450 Paint paint = mSelected ? mActiveShaderPaint : mInactiveShaderPaint;
451 drawClipped(canvas, paint, mPath, mWindowPos[0]);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800452 canvas.restoreToCount(state);
453 super.dispatchDraw(canvas);
454 }
455
Romain Guyc8373212011-01-07 19:15:16 -0800456 private void drawClipped(Canvas canvas, Paint paint, Path clipPath, int left) {
457 // TODO: We should change the matrix/shader only when needed
458 final Matrix matrix = mSelected ? mActiveMatrix : mInactiveMatrix;
459 matrix.setTranslate(-left, 0.0f);
460 (mSelected ? mActiveShader : mInactiveShader).setLocalMatrix(matrix);
461 canvas.drawPath(clipPath, paint);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800462 }
463
464 private void setTabPath(Path path, int l, int t, int r, int b) {
465 path.reset();
466 path.moveTo(l, b);
467 path.lineTo(l, t);
468 path.lineTo(r - mTabSliceWidth, t);
469 path.lineTo(r, b);
470 path.close();
471 }
472
Michael Kolba2b2ba82010-08-04 17:54:03 -0700473 }
474
475 /**
476 * Store tab state within the title bar
477 */
478 class TabViewData {
479
480 Tab mTab;
481 TabView mTabView;
482 int mProgress;
483 Drawable mIcon;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700484
485 TabViewData(Tab tab) {
486 mTab = tab;
John Reck30c714c2010-12-16 17:30:34 -0800487 setUrlAndTitle(mTab.getUrl(), mTab.getTitle());
Michael Kolba2b2ba82010-08-04 17:54:03 -0700488 }
489
490 void setUrlAndTitle(String url, String title) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700491 if (mTabView != null) {
492 if (title != null) {
493 mTabView.setDisplayTitle(title);
494 } else if (url != null) {
John Reckfb3017f2010-10-26 19:01:24 -0700495 mTabView.setDisplayTitle(UrlUtils.stripUrl(url));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700496 }
497 }
498 }
499
500 void setProgress(int newProgress) {
501 mProgress = newProgress;
502 if (mTabView != null) {
503 mTabView.setProgress(mProgress);
504 }
505 }
506
507 void setFavicon(Bitmap icon) {
508 Drawable[] array = new Drawable[3];
509 array[0] = new PaintDrawable(Color.BLACK);
510 array[1] = new PaintDrawable(Color.WHITE);
511 if (icon == null) {
John Reckaff60fb2010-10-25 13:47:58 -0700512 array[2] = mGenericFavicon;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700513 } else {
514 array[2] = new BitmapDrawable(icon);
515 }
516 LayerDrawable d = new LayerDrawable(array);
517 d.setLayerInset(1, 1, 1, 1, 1);
518 d.setLayerInset(2, 2, 2, 2, 2);
519 mIcon = d;
520 if (mTabView != null) {
521 mTabView.setFavicon(mIcon);
522 }
523 }
524
525 }
526
527 // TabChangeListener implementation
528
Michael Kolb8233fac2010-10-26 16:08:53 -0700529 public void onSetActiveTab(Tab tab) {
530 mTabs.setSelectedTab(mTabControl.getTabIndex(tab));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700531 TabViewData tvd = mTabMap.get(tab);
532 if (tvd != null) {
533 tvd.setProgress(tvd.mProgress);
534 // update the scroll state
535 WebView webview = tab.getWebView();
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800536 if (webview != null) {
537 int h = webview.getVisibleTitleHeight();
538 mVisibleTitleHeight = h -1;
539 onScroll(h);
540 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700541 }
542 }
543
Michael Kolba2b2ba82010-08-04 17:54:03 -0700544 public void onFavicon(Tab tab, Bitmap favicon) {
545 TabViewData tvd = mTabMap.get(tab);
546 if (tvd != null) {
547 tvd.setFavicon(favicon);
548 }
549 }
550
Michael Kolba2b2ba82010-08-04 17:54:03 -0700551 public void onNewTab(Tab tab) {
552 TabViewData tvd = buildTab(tab);
553 buildView(tvd);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700554 }
555
Michael Kolba2b2ba82010-08-04 17:54:03 -0700556 public void onProgress(Tab tab, int progress) {
557 TabViewData tvd = mTabMap.get(tab);
558 if (tvd != null) {
559 tvd.setProgress(progress);
560 }
561 }
562
Michael Kolba2b2ba82010-08-04 17:54:03 -0700563 public void onRemoveTab(Tab tab) {
564 TabViewData tvd = mTabMap.get(tab);
Michael Kolbe2752f72010-10-25 16:50:16 -0700565 if (tvd != null) {
566 TabView tv = tvd.mTabView;
567 if (tv != null) {
568 mTabs.removeTab(tv);
569 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700570 }
571 mTabMap.remove(tab);
572 }
573
Michael Kolba2b2ba82010-08-04 17:54:03 -0700574 public void onUrlAndTitle(Tab tab, String url, String title) {
575 TabViewData tvd = mTabMap.get(tab);
576 if (tvd != null) {
577 tvd.setUrlAndTitle(url, title);
578 }
579 }
580
Michael Kolba2b2ba82010-08-04 17:54:03 -0700581 private boolean isLoading() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700582 TabViewData tvd = mTabMap.get(mTabControl.getCurrentTab());
583 if ((tvd != null) && (tvd.mTabView != null)) {
584 return tvd.mTabView.mInLoad;
585 } else {
586 return false;
587 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700588 }
589
Michael Kolba2b2ba82010-08-04 17:54:03 -0700590}