blob: 32e19a1dd13c407b5e4424415639e764b826634b [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 Kolb2b5a13a2010-12-09 14:13:42 -080028import android.graphics.Paint;
29import android.graphics.Path;
30import android.graphics.Shader;
Michael Kolba2b2ba82010-08-04 17:54:03 -070031import android.graphics.drawable.BitmapDrawable;
32import android.graphics.drawable.Drawable;
33import android.graphics.drawable.LayerDrawable;
34import android.graphics.drawable.PaintDrawable;
35import android.view.ContextMenu;
Michael Kolbed217742010-08-10 17:52:34 -070036import android.view.Gravity;
Michael Kolba2b2ba82010-08-04 17:54:03 -070037import android.view.LayoutInflater;
38import android.view.MenuInflater;
39import android.view.View;
40import android.view.View.OnClickListener;
41import android.webkit.WebView;
Michael Kolbed217742010-08-10 17:52:34 -070042import android.widget.ImageButton;
Michael Kolba2b2ba82010-08-04 17:54:03 -070043import android.widget.ImageView;
44import android.widget.LinearLayout;
45import android.widget.TextView;
46
Michael Kolba2b2ba82010-08-04 17:54:03 -070047import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -080048import java.util.List;
Michael Kolba2b2ba82010-08-04 17:54:03 -070049import java.util.Map;
50
51/**
52 * tabbed title bar for xlarge screen browser
53 */
54public class TabBar extends LinearLayout
Michael Kolb8233fac2010-10-26 16:08:53 -070055 implements ScrollListener, OnClickListener {
Michael Kolba2b2ba82010-08-04 17:54:03 -070056
57 private static final int PROGRESS_MAX = 100;
58
Michael Kolb8233fac2010-10-26 16:08:53 -070059 private Activity mActivity;
60 private UiController mUiController;
61 private TabControl mTabControl;
Michael Kolb66706532010-12-12 19:50:22 -080062 private XLargeUi mUi;
Michael Kolba2b2ba82010-08-04 17:54:03 -070063
Michael Kolbed217742010-08-10 17:52:34 -070064 private final int mTabWidthSelected;
65 private final int mTabWidthUnselected;
66
Michael Kolba2b2ba82010-08-04 17:54:03 -070067 private TabScrollView mTabs;
Michael Kolb8233fac2010-10-26 16:08:53 -070068
Michael Kolbebba8b42010-09-30 12:57:59 -070069 private ImageButton mNewTab;
70 private int mButtonWidth;
Michael Kolba2b2ba82010-08-04 17:54:03 -070071
72 private Map<Tab, TabViewData> mTabMap;
73
Michael Kolba2b2ba82010-08-04 17:54:03 -070074 private boolean mUserRequestedUrlbar;
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
Michael Kolb2b5a13a2010-12-09 14:13:42 -080079 private Drawable mActiveDrawable;
80 private Drawable mInactiveDrawable;
81
82 private Bitmap mShaderBuffer;
83 private Canvas mShaderCanvas;
84 private Paint mShaderPaint;
85 private int mTabHeight;
86 private int mTabOverlap;
87 private int mTabSliceWidth;
88 private int mTabPadding;
Michael Kolb376b5412010-12-15 11:52:57 -080089 private boolean mUseQuickControls;
Michael Kolb2b5a13a2010-12-09 14:13:42 -080090
Michael Kolb66706532010-12-12 19:50:22 -080091 public TabBar(Activity activity, UiController controller, XLargeUi ui) {
Michael Kolb8233fac2010-10-26 16:08:53 -070092 super(activity);
93 mActivity = activity;
94 mUiController = controller;
95 mTabControl = mUiController.getTabControl();
96 mUi = ui;
97 Resources res = activity.getResources();
Michael Kolbed217742010-08-10 17:52:34 -070098 mTabWidthSelected = (int) res.getDimension(R.dimen.tab_width_selected);
99 mTabWidthUnselected = (int) res.getDimension(R.dimen.tab_width_unselected);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800100 mActiveDrawable = res.getDrawable(R.drawable.bg_urlbar);
101 mInactiveDrawable = res.getDrawable(R.drawable.browsertab_inactive);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700102
Michael Kolba2b2ba82010-08-04 17:54:03 -0700103 mTabMap = new HashMap<Tab, TabViewData>();
Michael Kolb8233fac2010-10-26 16:08:53 -0700104 Resources resources = activity.getResources();
105 LayoutInflater factory = LayoutInflater.from(activity);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700106 factory.inflate(R.layout.tab_bar, this);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800107 setPadding(12, 12, 0, 0);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700108 mTabs = (TabScrollView) findViewById(R.id.tabs);
Michael Kolbebba8b42010-09-30 12:57:59 -0700109 mNewTab = (ImageButton) findViewById(R.id.newtab);
110 mNewTab.setOnClickListener(this);
John Reckaff60fb2010-10-25 13:47:58 -0700111 mGenericFavicon = res.getDrawable(R.drawable.app_web_browser_sm);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800112 setChildrenDrawingOrderEnabled(true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700113
114 // TODO: Change enabled states based on whether you can go
115 // back/forward. Probably should be done inside onPageStarted.
116
Michael Kolb1bf23132010-11-19 12:55:12 -0800117 updateTabs(mUiController.getTabs());
Michael Kolba2b2ba82010-08-04 17:54:03 -0700118
Michael Kolba2b2ba82010-08-04 17:54:03 -0700119 mUserRequestedUrlbar = false;
Michael Kolba0e2a752010-12-12 15:27:56 -0800120 mVisibleTitleHeight = 1;
Michael Kolbebba8b42010-09-30 12:57:59 -0700121 mButtonWidth = -1;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800122 // tab dimensions
123 mTabHeight = (int) res.getDimension(R.dimen.tab_height);
124 mTabOverlap = (int) res.getDimension(R.dimen.tab_overlap);
125 mTabSliceWidth = (int) res.getDimension(R.dimen.tab_slice);
126 mTabPadding = (int) res.getDimension(R.dimen.tab_padding);
127 int maxTabWidth = (int) res.getDimension(R.dimen.max_tab_width);
128 // shader initialization
129 mShaderBuffer = Bitmap.createBitmap(maxTabWidth, mTabHeight,
130 Bitmap.Config.ARGB_8888);
131 mShaderCanvas = new Canvas(mShaderBuffer);
132 mShaderPaint = new Paint();
133 BitmapShader shader = new BitmapShader(mShaderBuffer,
134 Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
135 mShaderPaint.setShader(shader);
136 mShaderPaint.setStyle(Paint.Style.FILL);
137 mShaderPaint.setAntiAlias(true);
Michael Kolbebba8b42010-09-30 12:57:59 -0700138 }
139
Michael Kolb376b5412010-12-15 11:52:57 -0800140 void setUseQuickControls(boolean useQuickControls) {
141 mUseQuickControls = useQuickControls;
142 }
143
144 int getTabCount() {
145 return mTabMap.size();
146 }
147
Michael Kolb1bf23132010-11-19 12:55:12 -0800148 void updateTabs(List<Tab> tabs) {
149 mTabs.clearTabs();
150 mTabMap.clear();
151 for (Tab tab : tabs) {
152 TabViewData data = buildTab(tab);
153 TabView tv = buildView(data);
154 }
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 Kolbed217742010-08-10 17:52:34 -0700209 mUserRequestedUrlbar = true;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700210 }
211
Michael Kolb376b5412010-12-15 11:52:57 -0800212 void showTitleBarIndicator(boolean show) {
Michael Kolba0e2a752010-12-12 15:27:56 -0800213 Tab tab = mTabControl.getCurrentTab();
214 if (tab != null) {
215 TabViewData tvd = mTabMap.get(tab);
216 if (tvd != null) {
217 tvd.mTabView.showIndicator(show);
218 }
219 }
Michael Kolbed217742010-08-10 17:52:34 -0700220 }
221
222 // callback after fake titlebar is shown
223 void onShowTitleBar() {
Michael Kolba0e2a752010-12-12 15:27:56 -0800224 showTitleBarIndicator(false);
Michael Kolbed217742010-08-10 17:52:34 -0700225 }
226
227 // callback after fake titlebar is hidden
Michael Kolba2b2ba82010-08-04 17:54:03 -0700228 void onHideTitleBar() {
Michael Kolba0e2a752010-12-12 15:27:56 -0800229 showTitleBarIndicator(mVisibleTitleHeight == 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700230 Tab tab = mTabControl.getCurrentTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700231 tab.getWebView().requestFocus();
232 mUserRequestedUrlbar = false;
233 }
234
Michael Kolbed217742010-08-10 17:52:34 -0700235 // webview scroll listener
236
237 @Override
Michael Kolba0e2a752010-12-12 15:27:56 -0800238 public void onScroll(int visibleTitleHeight) {
Michael Kolb376b5412010-12-15 11:52:57 -0800239 if (mUseQuickControls) return;
Michael Kolb8233fac2010-10-26 16:08:53 -0700240 // isLoading is using the current tab, which initially might not be set yet
John Reckdcf911c2010-12-22 16:32:31 -0800241 if (mTabControl.getCurrentTab() != null
242 && !isLoading()) {
243 if (visibleTitleHeight == 0) {
244 mUi.hideFakeTitleBar();
245 showTitleBarIndicator(true);
246 } else {
Michael Kolba0e2a752010-12-12 15:27:56 -0800247 showTitleBarIndicator(false);
Michael Kolbed217742010-08-10 17:52:34 -0700248 }
249 }
Michael Kolba0e2a752010-12-12 15:27:56 -0800250 mVisibleTitleHeight = visibleTitleHeight;
Michael Kolbed217742010-08-10 17:52:34 -0700251 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700252
253 @Override
254 public void createContextMenu(ContextMenu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700255 MenuInflater inflater = mActivity.getMenuInflater();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700256 inflater.inflate(R.menu.title_context, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -0700257 mActivity.onCreateContextMenu(menu, this, null);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700258 }
259
260 private TabViewData buildTab(Tab tab) {
261 TabViewData data = new TabViewData(tab);
262 mTabMap.put(tab, data);
263 return data;
264 }
265
266 private TabView buildView(final TabViewData data) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700267 TabView tv = new TabView(mActivity, data);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700268 tv.setTag(data);
269 tv.setOnClickListener(this);
270 mTabs.addTab(tv);
271 return tv;
272 }
273
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800274 @Override
275 protected int getChildDrawingOrder(int count, int i) {
276 // reverse
277 return count - 1 - i;
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
285 TabViewData mTabData;
286 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 */
301 public TabView(Context context, TabViewData tab) {
302 super(context);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800303 setWillNotDraw(false);
304 mPath = new Path();
305 mWindowPos = new int[2];
Michael Kolba2b2ba82010-08-04 17:54:03 -0700306 mTabData = 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
322 updateFromData();
323 }
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
340 private void updateFromData() {
341 mTabData.mTabView = this;
John Reck30c714c2010-12-16 17:30:34 -0800342 Tab tab = mTabData.mTab;
343 String displayTitle = tab.getTitle();
344 if (displayTitle == null) {
345 displayTitle = tab.getUrl();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700346 }
John Reck30c714c2010-12-16 17:30:34 -0800347 setDisplayTitle(displayTitle);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700348 setProgress(mTabData.mProgress);
349 if (mTabData.mIcon != null) {
350 setFavicon(mTabData.mIcon);
351 }
Michael Kolbae62fd42010-08-18 16:33:28 -0700352 if (mTabData.mTab != null) {
353 mIncognito.setVisibility(
Michael Kolb1bf23132010-11-19 12:55:12 -0800354 mTabData.mTab.isPrivateBrowsingEnabled() ?
Michael Kolbae62fd42010-08-18 16:33:28 -0700355 View.VISIBLE : View.GONE);
356 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700357 }
358
359 @Override
Michael Kolbb7b115e2010-09-25 16:59:37 -0700360 public void setActivated(boolean selected) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700361 mSelected = selected;
362 mClose.setVisibility(mSelected ? View.VISIBLE : View.GONE);
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800363 mIndicator.setVisibility(View.GONE);
Michael Kolb8233fac2010-10-26 16:08:53 -0700364 mTitle.setTextAppearance(mActivity, mSelected ?
Michael Kolbc7485ae2010-09-03 10:10:58 -0700365 R.style.TabTitleSelected : R.style.TabTitleUnselected);
366 setHorizontalFadingEdgeEnabled(!mSelected);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700367 super.setActivated(selected);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800368 LayoutParams lp = (LinearLayout.LayoutParams) getLayoutParams();
369 lp.width = selected ? mTabWidthSelected : mTabWidthUnselected;
370 lp.height = LayoutParams.MATCH_PARENT;
371 setLayoutParams(lp);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700372 }
373
374 void setDisplayTitle(String title) {
375 mTitle.setText(title);
376 }
377
378 void setFavicon(Drawable d) {
379 mIconView.setImageDrawable(d);
380 }
381
382 void setLock(Drawable d) {
383 if (null == d) {
384 mLock.setVisibility(View.GONE);
385 } else {
386 mLock.setImageDrawable(d);
387 mLock.setVisibility(View.VISIBLE);
388 }
389 }
390
Michael Kolba2b2ba82010-08-04 17:54:03 -0700391 void setProgress(int newProgress) {
392 if (newProgress >= PROGRESS_MAX) {
393 mInLoad = false;
394 } else {
395 if (!mInLoad && getWindowToken() != null) {
396 mInLoad = true;
397 }
398 }
399 }
400
401 private void closeTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700402 if (mTabData.mTab == mTabControl.getCurrentTab()) {
403 mUiController.closeCurrentTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700404 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -0700405 mUiController.closeTab(mTabData.mTab);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700406 }
407 }
408
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800409 @Override
410 protected void onLayout(boolean changed, int l, int t, int r, int b) {
411 super.onLayout(changed, l, t, r, b);
412 setTabPath(mPath, 0, 0, r - l, b - t);
413 }
414
415 @Override
416 protected void dispatchDraw(Canvas canvas) {
417 int state = canvas.save();
418 int[] pos = new int[2];
419 getLocationInWindow(mWindowPos);
420 Drawable drawable = mSelected ? mActiveDrawable : mInactiveDrawable;
Michael Kolb376b5412010-12-15 11:52:57 -0800421 drawable.setBounds(0, 0, mUi.getContentWidth(), getHeight());
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800422 drawClipped(canvas, drawable, mPath, mWindowPos[0]);
423 canvas.restoreToCount(state);
424 super.dispatchDraw(canvas);
425 }
426
427 private void drawClipped(Canvas canvas, Drawable drawable,
428 Path clipPath, int left) {
429 mShaderCanvas.drawColor(Color.TRANSPARENT);
430 mShaderCanvas.translate(-left, 0);
431 drawable.draw(mShaderCanvas);
432 canvas.drawPath(clipPath, mShaderPaint);
433 mShaderCanvas.translate(left, 0);
434 }
435
436 private void setTabPath(Path path, int l, int t, int r, int b) {
437 path.reset();
438 path.moveTo(l, b);
439 path.lineTo(l, t);
440 path.lineTo(r - mTabSliceWidth, t);
441 path.lineTo(r, b);
442 path.close();
443 }
444
Michael Kolba2b2ba82010-08-04 17:54:03 -0700445 }
446
447 /**
448 * Store tab state within the title bar
449 */
450 class TabViewData {
451
452 Tab mTab;
453 TabView mTabView;
454 int mProgress;
455 Drawable mIcon;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700456
457 TabViewData(Tab tab) {
458 mTab = tab;
John Reck30c714c2010-12-16 17:30:34 -0800459 setUrlAndTitle(mTab.getUrl(), mTab.getTitle());
Michael Kolba2b2ba82010-08-04 17:54:03 -0700460 }
461
462 void setUrlAndTitle(String url, String title) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700463 if (mTabView != null) {
464 if (title != null) {
465 mTabView.setDisplayTitle(title);
466 } else if (url != null) {
John Reckfb3017f2010-10-26 19:01:24 -0700467 mTabView.setDisplayTitle(UrlUtils.stripUrl(url));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700468 }
469 }
470 }
471
472 void setProgress(int newProgress) {
473 mProgress = newProgress;
474 if (mTabView != null) {
475 mTabView.setProgress(mProgress);
476 }
477 }
478
479 void setFavicon(Bitmap icon) {
480 Drawable[] array = new Drawable[3];
481 array[0] = new PaintDrawable(Color.BLACK);
482 array[1] = new PaintDrawable(Color.WHITE);
483 if (icon == null) {
John Reckaff60fb2010-10-25 13:47:58 -0700484 array[2] = mGenericFavicon;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700485 } else {
486 array[2] = new BitmapDrawable(icon);
487 }
488 LayerDrawable d = new LayerDrawable(array);
489 d.setLayerInset(1, 1, 1, 1, 1);
490 d.setLayerInset(2, 2, 2, 2, 2);
491 mIcon = d;
492 if (mTabView != null) {
493 mTabView.setFavicon(mIcon);
494 }
495 }
496
497 }
498
499 // TabChangeListener implementation
500
Michael Kolb8233fac2010-10-26 16:08:53 -0700501 public void onSetActiveTab(Tab tab) {
502 mTabs.setSelectedTab(mTabControl.getTabIndex(tab));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700503 TabViewData tvd = mTabMap.get(tab);
504 if (tvd != null) {
505 tvd.setProgress(tvd.mProgress);
506 // update the scroll state
507 WebView webview = tab.getWebView();
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800508 if (webview != null) {
509 int h = webview.getVisibleTitleHeight();
510 mVisibleTitleHeight = h -1;
511 onScroll(h);
512 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700513 }
514 }
515
Michael Kolba2b2ba82010-08-04 17:54:03 -0700516 public void onFavicon(Tab tab, Bitmap favicon) {
517 TabViewData tvd = mTabMap.get(tab);
518 if (tvd != null) {
519 tvd.setFavicon(favicon);
520 }
521 }
522
Michael Kolba2b2ba82010-08-04 17:54:03 -0700523 public void onNewTab(Tab tab) {
524 TabViewData tvd = buildTab(tab);
525 buildView(tvd);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700526 }
527
Michael Kolba2b2ba82010-08-04 17:54:03 -0700528 public void onProgress(Tab tab, int progress) {
529 TabViewData tvd = mTabMap.get(tab);
530 if (tvd != null) {
531 tvd.setProgress(progress);
532 }
533 }
534
Michael Kolba2b2ba82010-08-04 17:54:03 -0700535 public void onRemoveTab(Tab tab) {
536 TabViewData tvd = mTabMap.get(tab);
Michael Kolbe2752f72010-10-25 16:50:16 -0700537 if (tvd != null) {
538 TabView tv = tvd.mTabView;
539 if (tv != null) {
540 mTabs.removeTab(tv);
541 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700542 }
543 mTabMap.remove(tab);
544 }
545
Michael Kolba2b2ba82010-08-04 17:54:03 -0700546 public void onUrlAndTitle(Tab tab, String url, String title) {
547 TabViewData tvd = mTabMap.get(tab);
548 if (tvd != null) {
549 tvd.setUrlAndTitle(url, title);
550 }
551 }
552
Michael Kolba2b2ba82010-08-04 17:54:03 -0700553 private boolean isLoading() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700554 TabViewData tvd = mTabMap.get(mTabControl.getCurrentTab());
555 if ((tvd != null) && (tvd.mTabView != null)) {
556 return tvd.mTabView.mInLoad;
557 } else {
558 return false;
559 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700560 }
561
Michael Kolba2b2ba82010-08-04 17:54:03 -0700562}