blob: 79699559eca718f38062beb73109ea43aa5b0b88 [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.LinearGradient;
29import 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;
63 private BaseUi 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;
John Reckfb3017f2010-10-26 19:01:24 -070077 private boolean mHasReceivedTitle;
Michael Kolba2b2ba82010-08-04 17:54:03 -070078
John Reckaff60fb2010-10-25 13:47:58 -070079 private Drawable mGenericFavicon;
John Reckfb3017f2010-10-26 19:01:24 -070080 private String mLoadingText;
John Reckaff60fb2010-10-25 13:47:58 -070081
Michael Kolb2b5a13a2010-12-09 14:13:42 -080082 private Drawable mActiveDrawable;
83 private Drawable mInactiveDrawable;
84
85 private Bitmap mShaderBuffer;
86 private Canvas mShaderCanvas;
87 private Paint mShaderPaint;
88 private int mTabHeight;
89 private int mTabOverlap;
90 private int mTabSliceWidth;
91 private int mTabPadding;
92
Michael Kolb8233fac2010-10-26 16:08:53 -070093 public TabBar(Activity activity, UiController controller, BaseUi ui) {
94 super(activity);
95 mActivity = activity;
96 mUiController = controller;
97 mTabControl = mUiController.getTabControl();
98 mUi = ui;
99 Resources res = activity.getResources();
Michael Kolbed217742010-08-10 17:52:34 -0700100 mTabWidthSelected = (int) res.getDimension(R.dimen.tab_width_selected);
101 mTabWidthUnselected = (int) res.getDimension(R.dimen.tab_width_unselected);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800102 mActiveDrawable = res.getDrawable(R.drawable.bg_urlbar);
103 mInactiveDrawable = res.getDrawable(R.drawable.browsertab_inactive);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700104
Michael Kolba2b2ba82010-08-04 17:54:03 -0700105 mTabMap = new HashMap<Tab, TabViewData>();
Michael Kolb8233fac2010-10-26 16:08:53 -0700106 Resources resources = activity.getResources();
107 LayoutInflater factory = LayoutInflater.from(activity);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700108 factory.inflate(R.layout.tab_bar, this);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800109 setPadding(12, 12, 0, 0);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700110 mTabs = (TabScrollView) findViewById(R.id.tabs);
Michael Kolbebba8b42010-09-30 12:57:59 -0700111 mNewTab = (ImageButton) findViewById(R.id.newtab);
112 mNewTab.setOnClickListener(this);
John Reckaff60fb2010-10-25 13:47:58 -0700113 mGenericFavicon = res.getDrawable(R.drawable.app_web_browser_sm);
John Reckfb3017f2010-10-26 19:01:24 -0700114 mLoadingText = res.getString(R.string.title_bar_loading);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800115 setChildrenDrawingOrderEnabled(true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700116
117 // TODO: Change enabled states based on whether you can go
118 // back/forward. Probably should be done inside onPageStarted.
119
Michael Kolb1bf23132010-11-19 12:55:12 -0800120 updateTabs(mUiController.getTabs());
Michael Kolba2b2ba82010-08-04 17:54:03 -0700121
Michael Kolba2b2ba82010-08-04 17:54:03 -0700122 mUserRequestedUrlbar = false;
Michael Kolba0e2a752010-12-12 15:27:56 -0800123 mVisibleTitleHeight = 1;
Michael Kolbebba8b42010-09-30 12:57:59 -0700124 mButtonWidth = -1;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800125 // tab dimensions
126 mTabHeight = (int) res.getDimension(R.dimen.tab_height);
127 mTabOverlap = (int) res.getDimension(R.dimen.tab_overlap);
128 mTabSliceWidth = (int) res.getDimension(R.dimen.tab_slice);
129 mTabPadding = (int) res.getDimension(R.dimen.tab_padding);
130 int maxTabWidth = (int) res.getDimension(R.dimen.max_tab_width);
131 // shader initialization
132 mShaderBuffer = Bitmap.createBitmap(maxTabWidth, mTabHeight,
133 Bitmap.Config.ARGB_8888);
134 mShaderCanvas = new Canvas(mShaderBuffer);
135 mShaderPaint = new Paint();
136 BitmapShader shader = new BitmapShader(mShaderBuffer,
137 Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
138 mShaderPaint.setShader(shader);
139 mShaderPaint.setStyle(Paint.Style.FILL);
140 mShaderPaint.setAntiAlias(true);
Michael Kolbebba8b42010-09-30 12:57:59 -0700141 }
142
Michael Kolb1bf23132010-11-19 12:55:12 -0800143 void updateTabs(List<Tab> tabs) {
144 mTabs.clearTabs();
145 mTabMap.clear();
146 for (Tab tab : tabs) {
147 TabViewData data = buildTab(tab);
148 TabView tv = buildView(data);
149 }
150 mTabs.setSelectedTab(mTabControl.getCurrentIndex());
151 }
152
Michael Kolbebba8b42010-09-30 12:57:59 -0700153 @Override
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800154 protected void onMeasure(int hspec, int vspec) {
155 super.onMeasure(hspec, vspec);
156 int w = getMeasuredWidth();
157 // adjust for new tab overlap
158 w -= mTabOverlap;
159 setMeasuredDimension(w, getMeasuredHeight());
160 }
161
162 @Override
Michael Kolbebba8b42010-09-30 12:57:59 -0700163 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800164 // use paddingLeft and paddingTop
165 int pl = getPaddingLeft();
166 int pt = getPaddingTop();
Michael Kolbebba8b42010-09-30 12:57:59 -0700167 if (mButtonWidth == -1) {
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800168 mButtonWidth = mNewTab.getMeasuredWidth() - mTabOverlap;
Michael Kolbebba8b42010-09-30 12:57:59 -0700169 }
170 int sw = mTabs.getMeasuredWidth();
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800171 int w = right - left - pl;
Michael Kolbebba8b42010-09-30 12:57:59 -0700172 if (w-sw < mButtonWidth) {
173 sw = w - mButtonWidth;
174 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800175 mTabs.layout(pl, pt, pl + sw, bottom - top);
176 // adjust for overlap
177 mNewTab.layout(pl + sw - mTabOverlap, pt,
178 pl + sw + mButtonWidth - mTabOverlap, bottom - top);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700179 }
180
181 public void onClick(View view) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700182 mUi.hideComboView();
Michael Kolbebba8b42010-09-30 12:57:59 -0700183 if (mNewTab == view) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700184 mUiController.openTabToHomePage();
Michael Kolbebba8b42010-09-30 12:57:59 -0700185 } else if (mTabs.getSelectedTab() == view) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700186 if (mUi.isFakeTitleBarShowing() && !isLoading()) {
187 mUi.hideFakeTitleBar();
Michael Kolbed217742010-08-10 17:52:34 -0700188 } else {
189 showUrlBar();
190 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700191 } else {
Michael Kolbebba8b42010-09-30 12:57:59 -0700192 int ix = mTabs.getChildIndex(view);
193 if (ix >= 0) {
194 mTabs.setSelectedTab(ix);
Michael Kolb8233fac2010-10-26 16:08:53 -0700195 mUiController.switchToTab(ix);
Michael Kolbebba8b42010-09-30 12:57:59 -0700196 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700197 }
198 }
199
Michael Kolbed217742010-08-10 17:52:34 -0700200 private void showUrlBar() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700201 mUi.stopWebViewScrolling();
202 mUi.showFakeTitleBar();
Michael Kolbed217742010-08-10 17:52:34 -0700203 mUserRequestedUrlbar = true;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700204 }
205
Michael Kolba0e2a752010-12-12 15:27:56 -0800206 private void showTitleBarIndicator(boolean show) {
207 Tab tab = mTabControl.getCurrentTab();
208 if (tab != null) {
209 TabViewData tvd = mTabMap.get(tab);
210 if (tvd != null) {
211 tvd.mTabView.showIndicator(show);
212 }
213 }
Michael Kolbed217742010-08-10 17:52:34 -0700214 }
215
216 // callback after fake titlebar is shown
217 void onShowTitleBar() {
Michael Kolba0e2a752010-12-12 15:27:56 -0800218 showTitleBarIndicator(false);
Michael Kolbed217742010-08-10 17:52:34 -0700219 }
220
221 // callback after fake titlebar is hidden
Michael Kolba2b2ba82010-08-04 17:54:03 -0700222 void onHideTitleBar() {
Michael Kolba0e2a752010-12-12 15:27:56 -0800223 showTitleBarIndicator(mVisibleTitleHeight == 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700224 Tab tab = mTabControl.getCurrentTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700225 tab.getWebView().requestFocus();
226 mUserRequestedUrlbar = false;
227 }
228
Michael Kolbed217742010-08-10 17:52:34 -0700229 // webview scroll listener
230
231 @Override
Michael Kolba0e2a752010-12-12 15:27:56 -0800232 public void onScroll(int visibleTitleHeight) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700233 // isLoading is using the current tab, which initially might not be set yet
234 if (mTabControl.getCurrentTab() != null) {
Michael Kolba0e2a752010-12-12 15:27:56 -0800235 if ((mVisibleTitleHeight > 0) && (visibleTitleHeight == 0)
236 && !isLoading()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700237 if (mUserRequestedUrlbar) {
238 mUi.hideFakeTitleBar();
239 } else {
Michael Kolba0e2a752010-12-12 15:27:56 -0800240 showTitleBarIndicator(true);
Michael Kolb8233fac2010-10-26 16:08:53 -0700241 }
Michael Kolba0e2a752010-12-12 15:27:56 -0800242 } else if ((mVisibleTitleHeight == 0) && (visibleTitleHeight != 0)
243 && !isLoading()) {
244 showTitleBarIndicator(false);
Michael Kolbed217742010-08-10 17:52:34 -0700245 }
246 }
Michael Kolba0e2a752010-12-12 15:27:56 -0800247 mVisibleTitleHeight = visibleTitleHeight;
Michael Kolbed217742010-08-10 17:52:34 -0700248 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700249
250 @Override
251 public void createContextMenu(ContextMenu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700252 MenuInflater inflater = mActivity.getMenuInflater();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700253 inflater.inflate(R.menu.title_context, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -0700254 mActivity.onCreateContextMenu(menu, this, null);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700255 }
256
257 private TabViewData buildTab(Tab tab) {
258 TabViewData data = new TabViewData(tab);
259 mTabMap.put(tab, data);
260 return data;
261 }
262
263 private TabView buildView(final TabViewData data) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700264 TabView tv = new TabView(mActivity, data);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700265 tv.setTag(data);
266 tv.setOnClickListener(this);
267 mTabs.addTab(tv);
268 return tv;
269 }
270
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800271 @Override
272 protected int getChildDrawingOrder(int count, int i) {
273 // reverse
274 return count - 1 - i;
275 }
276
Michael Kolba2b2ba82010-08-04 17:54:03 -0700277 /**
278 * View used in the tab bar
279 */
280 class TabView extends LinearLayout implements OnClickListener {
281
282 TabViewData mTabData;
283 View mTabContent;
284 TextView mTitle;
Michael Kolba0e2a752010-12-12 15:27:56 -0800285 View mIndicator;
Michael Kolbae62fd42010-08-18 16:33:28 -0700286 View mIncognito;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700287 ImageView mIconView;
288 ImageView mLock;
289 ImageView mClose;
290 boolean mSelected;
291 boolean mInLoad;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800292 Path mPath;
293 int[] mWindowPos;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700294
295 /**
296 * @param context
297 */
298 public TabView(Context context, TabViewData tab) {
299 super(context);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800300 setWillNotDraw(false);
301 mPath = new Path();
302 mWindowPos = new int[2];
Michael Kolba2b2ba82010-08-04 17:54:03 -0700303 mTabData = tab;
Michael Kolbed217742010-08-10 17:52:34 -0700304 setGravity(Gravity.CENTER_VERTICAL);
305 setOrientation(LinearLayout.HORIZONTAL);
Michael Kolba0e2a752010-12-12 15:27:56 -0800306 setPadding(mTabPadding, 0, 0, 0);
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100307 LayoutInflater inflater = LayoutInflater.from(getContext());
Michael Kolbed217742010-08-10 17:52:34 -0700308 mTabContent = inflater.inflate(R.layout.tab_title, this, true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700309 mTitle = (TextView) mTabContent.findViewById(R.id.title);
310 mIconView = (ImageView) mTabContent.findViewById(R.id.favicon);
311 mLock = (ImageView) mTabContent.findViewById(R.id.lock);
312 mClose = (ImageView) mTabContent.findViewById(R.id.close);
313 mClose.setOnClickListener(this);
Michael Kolbae62fd42010-08-18 16:33:28 -0700314 mIncognito = mTabContent.findViewById(R.id.incognito);
Michael Kolba0e2a752010-12-12 15:27:56 -0800315 mIndicator = mTabContent.findViewById(R.id.chevron);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700316 mSelected = false;
317 mInLoad = false;
318 // update the status
319 updateFromData();
320 }
321
Michael Kolba0e2a752010-12-12 15:27:56 -0800322 void showIndicator(boolean show) {
323 mIndicator.setVisibility(show ? View.VISIBLE : View.GONE);
324 }
325
Michael Kolba2b2ba82010-08-04 17:54:03 -0700326 @Override
327 public void onClick(View v) {
328 if (v == mClose) {
329 closeTab();
330 }
331 }
332
333 private void updateFromData() {
334 mTabData.mTabView = this;
335 if (mTabData.mUrl != null) {
336 setDisplayTitle(mTabData.mUrl);
337 }
338 if (mTabData.mTitle != null) {
339 setDisplayTitle(mTabData.mTitle);
340 }
341 setProgress(mTabData.mProgress);
342 if (mTabData.mIcon != null) {
343 setFavicon(mTabData.mIcon);
344 }
345 if (mTabData.mLock != null) {
346 setLock(mTabData.mLock);
347 }
Michael Kolbae62fd42010-08-18 16:33:28 -0700348 if (mTabData.mTab != null) {
349 mIncognito.setVisibility(
Michael Kolb1bf23132010-11-19 12:55:12 -0800350 mTabData.mTab.isPrivateBrowsingEnabled() ?
Michael Kolbae62fd42010-08-18 16:33:28 -0700351 View.VISIBLE : View.GONE);
352 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700353 }
354
355 @Override
Michael Kolbb7b115e2010-09-25 16:59:37 -0700356 public void setActivated(boolean selected) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700357 mSelected = selected;
358 mClose.setVisibility(mSelected ? View.VISIBLE : View.GONE);
Michael Kolb8233fac2010-10-26 16:08:53 -0700359 mTitle.setTextAppearance(mActivity, mSelected ?
Michael Kolbc7485ae2010-09-03 10:10:58 -0700360 R.style.TabTitleSelected : R.style.TabTitleUnselected);
361 setHorizontalFadingEdgeEnabled(!mSelected);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700362 super.setActivated(selected);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800363 LayoutParams lp = (LinearLayout.LayoutParams) getLayoutParams();
364 lp.width = selected ? mTabWidthSelected : mTabWidthUnselected;
365 lp.height = LayoutParams.MATCH_PARENT;
366 setLayoutParams(lp);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700367 }
368
369 void setDisplayTitle(String title) {
370 mTitle.setText(title);
371 }
372
373 void setFavicon(Drawable d) {
374 mIconView.setImageDrawable(d);
375 }
376
377 void setLock(Drawable d) {
378 if (null == d) {
379 mLock.setVisibility(View.GONE);
380 } else {
381 mLock.setImageDrawable(d);
382 mLock.setVisibility(View.VISIBLE);
383 }
384 }
385
Michael Kolba2b2ba82010-08-04 17:54:03 -0700386 void setProgress(int newProgress) {
387 if (newProgress >= PROGRESS_MAX) {
388 mInLoad = false;
389 } else {
390 if (!mInLoad && getWindowToken() != null) {
391 mInLoad = true;
392 }
393 }
394 }
395
396 private void closeTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700397 if (mTabData.mTab == mTabControl.getCurrentTab()) {
398 mUiController.closeCurrentTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700399 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -0700400 mUiController.closeTab(mTabData.mTab);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700401 }
402 }
403
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800404 @Override
405 protected void onLayout(boolean changed, int l, int t, int r, int b) {
406 super.onLayout(changed, l, t, r, b);
407 setTabPath(mPath, 0, 0, r - l, b - t);
408 }
409
410 @Override
411 protected void dispatchDraw(Canvas canvas) {
412 int state = canvas.save();
413 int[] pos = new int[2];
414 getLocationInWindow(mWindowPos);
415 Drawable drawable = mSelected ? mActiveDrawable : mInactiveDrawable;
416 drawable.setBounds(0, 0, mUi.getTitleBarWidth(), getHeight());
417 drawClipped(canvas, drawable, mPath, mWindowPos[0]);
418 canvas.restoreToCount(state);
419 super.dispatchDraw(canvas);
420 }
421
422 private void drawClipped(Canvas canvas, Drawable drawable,
423 Path clipPath, int left) {
424 mShaderCanvas.drawColor(Color.TRANSPARENT);
425 mShaderCanvas.translate(-left, 0);
426 drawable.draw(mShaderCanvas);
427 canvas.drawPath(clipPath, mShaderPaint);
428 mShaderCanvas.translate(left, 0);
429 }
430
431 private void setTabPath(Path path, int l, int t, int r, int b) {
432 path.reset();
433 path.moveTo(l, b);
434 path.lineTo(l, t);
435 path.lineTo(r - mTabSliceWidth, t);
436 path.lineTo(r, b);
437 path.close();
438 }
439
Michael Kolba2b2ba82010-08-04 17:54:03 -0700440 }
441
442 /**
443 * Store tab state within the title bar
444 */
445 class TabViewData {
446
447 Tab mTab;
448 TabView mTabView;
449 int mProgress;
450 Drawable mIcon;
451 Drawable mLock;
452 String mTitle;
453 String mUrl;
454
455 TabViewData(Tab tab) {
456 mTab = tab;
John Reckd8657622010-10-25 16:20:40 -0700457 WebView web = tab.getWebView();
458 if (web != null) {
459 setUrlAndTitle(web.getUrl(), web.getTitle());
460 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700461 }
462
463 void setUrlAndTitle(String url, String title) {
464 mUrl = url;
465 mTitle = title;
466 if (mTabView != null) {
467 if (title != null) {
468 mTabView.setDisplayTitle(title);
469 } else if (url != null) {
John Reckfb3017f2010-10-26 19:01:24 -0700470 mTabView.setDisplayTitle(UrlUtils.stripUrl(url));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700471 }
472 }
473 }
474
475 void setProgress(int newProgress) {
476 mProgress = newProgress;
477 if (mTabView != null) {
478 mTabView.setProgress(mProgress);
479 }
480 }
481
482 void setFavicon(Bitmap icon) {
483 Drawable[] array = new Drawable[3];
484 array[0] = new PaintDrawable(Color.BLACK);
485 array[1] = new PaintDrawable(Color.WHITE);
486 if (icon == null) {
John Reckaff60fb2010-10-25 13:47:58 -0700487 array[2] = mGenericFavicon;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700488 } else {
489 array[2] = new BitmapDrawable(icon);
490 }
491 LayerDrawable d = new LayerDrawable(array);
492 d.setLayerInset(1, 1, 1, 1, 1);
493 d.setLayerInset(2, 2, 2, 2, 2);
494 mIcon = d;
495 if (mTabView != null) {
496 mTabView.setFavicon(mIcon);
497 }
498 }
499
500 }
501
502 // TabChangeListener implementation
503
Michael Kolb8233fac2010-10-26 16:08:53 -0700504 public void onSetActiveTab(Tab tab) {
505 mTabs.setSelectedTab(mTabControl.getTabIndex(tab));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700506 TabViewData tvd = mTabMap.get(tab);
507 if (tvd != null) {
508 tvd.setProgress(tvd.mProgress);
509 // update the scroll state
510 WebView webview = tab.getWebView();
Michael Kolba0e2a752010-12-12 15:27:56 -0800511 onScroll(webview.getVisibleTitleHeight());
Michael Kolba2b2ba82010-08-04 17:54:03 -0700512 }
513 }
514
Michael Kolba2b2ba82010-08-04 17:54:03 -0700515 public void onFavicon(Tab tab, Bitmap favicon) {
516 TabViewData tvd = mTabMap.get(tab);
517 if (tvd != null) {
518 tvd.setFavicon(favicon);
519 }
520 }
521
Michael Kolba2b2ba82010-08-04 17:54:03 -0700522 public void onNewTab(Tab tab) {
523 TabViewData tvd = buildTab(tab);
524 buildView(tvd);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700525 }
526
Michael Kolba2b2ba82010-08-04 17:54:03 -0700527 public void onProgress(Tab tab, int progress) {
528 TabViewData tvd = mTabMap.get(tab);
529 if (tvd != null) {
530 tvd.setProgress(progress);
531 }
532 }
533
Michael Kolba2b2ba82010-08-04 17:54:03 -0700534 public void onRemoveTab(Tab tab) {
535 TabViewData tvd = mTabMap.get(tab);
Michael Kolbe2752f72010-10-25 16:50:16 -0700536 if (tvd != null) {
537 TabView tv = tvd.mTabView;
538 if (tv != null) {
539 mTabs.removeTab(tv);
540 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700541 }
542 mTabMap.remove(tab);
543 }
544
Michael Kolba2b2ba82010-08-04 17:54:03 -0700545 public void onUrlAndTitle(Tab tab, String url, String title) {
John Reckfb3017f2010-10-26 19:01:24 -0700546 mHasReceivedTitle = true;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700547 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 public void onPageFinished(Tab tab) {
John Reckfb3017f2010-10-26 19:01:24 -0700554 if (!mHasReceivedTitle) {
555 TabViewData tvd = mTabMap.get(tab);
556 if (tvd != null) {
557 tvd.setUrlAndTitle(tvd.mUrl, null);
558 }
559 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700560 }
561
John Reckfb3017f2010-10-26 19:01:24 -0700562 public void onPageStarted(Tab tab, String url, Bitmap favicon) {
563 mHasReceivedTitle = false;
John Reckaff60fb2010-10-25 13:47:58 -0700564 TabViewData tvd = mTabMap.get(tab);
565 if (tvd != null) {
566 tvd.setFavicon(favicon);
John Reckfb3017f2010-10-26 19:01:24 -0700567 tvd.setUrlAndTitle(url, mLoadingText);
John Reckaff60fb2010-10-25 13:47:58 -0700568 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700569 }
570
Michael Kolba2b2ba82010-08-04 17:54:03 -0700571 private boolean isLoading() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700572 TabViewData tvd = mTabMap.get(mTabControl.getCurrentTab());
573 if ((tvd != null) && (tvd.mTabView != null)) {
574 return tvd.mTabView.mInLoad;
575 } else {
576 return false;
577 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700578 }
579
Michael Kolba2b2ba82010-08-04 17:54:03 -0700580}