blob: 6a139f34adde48077b1392db86863bbeb1d23167 [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;
John Reckfb3017f2010-10-26 19:01:24 -070076 private boolean mHasReceivedTitle;
Michael Kolba2b2ba82010-08-04 17:54:03 -070077
John Reckaff60fb2010-10-25 13:47:58 -070078 private Drawable mGenericFavicon;
John Reckfb3017f2010-10-26 19:01:24 -070079 private String mLoadingText;
John Reckaff60fb2010-10-25 13:47:58 -070080
Michael Kolb2b5a13a2010-12-09 14:13:42 -080081 private Drawable mActiveDrawable;
82 private Drawable mInactiveDrawable;
83
84 private Bitmap mShaderBuffer;
85 private Canvas mShaderCanvas;
86 private Paint mShaderPaint;
87 private int mTabHeight;
88 private int mTabOverlap;
89 private int mTabSliceWidth;
90 private int mTabPadding;
91
Michael Kolb66706532010-12-12 19:50:22 -080092 public TabBar(Activity activity, UiController controller, XLargeUi ui) {
Michael Kolb8233fac2010-10-26 16:08:53 -070093 super(activity);
94 mActivity = activity;
95 mUiController = controller;
96 mTabControl = mUiController.getTabControl();
97 mUi = ui;
98 Resources res = activity.getResources();
Michael Kolbed217742010-08-10 17:52:34 -070099 mTabWidthSelected = (int) res.getDimension(R.dimen.tab_width_selected);
100 mTabWidthUnselected = (int) res.getDimension(R.dimen.tab_width_unselected);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800101 mActiveDrawable = res.getDrawable(R.drawable.bg_urlbar);
102 mInactiveDrawable = res.getDrawable(R.drawable.browsertab_inactive);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700103
Michael Kolba2b2ba82010-08-04 17:54:03 -0700104 mTabMap = new HashMap<Tab, TabViewData>();
Michael Kolb8233fac2010-10-26 16:08:53 -0700105 Resources resources = activity.getResources();
106 LayoutInflater factory = LayoutInflater.from(activity);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700107 factory.inflate(R.layout.tab_bar, this);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800108 setPadding(12, 12, 0, 0);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700109 mTabs = (TabScrollView) findViewById(R.id.tabs);
Michael Kolbebba8b42010-09-30 12:57:59 -0700110 mNewTab = (ImageButton) findViewById(R.id.newtab);
111 mNewTab.setOnClickListener(this);
John Reckaff60fb2010-10-25 13:47:58 -0700112 mGenericFavicon = res.getDrawable(R.drawable.app_web_browser_sm);
John Reckfb3017f2010-10-26 19:01:24 -0700113 mLoadingText = res.getString(R.string.title_bar_loading);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800114 setChildrenDrawingOrderEnabled(true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700115
116 // TODO: Change enabled states based on whether you can go
117 // back/forward. Probably should be done inside onPageStarted.
118
Michael Kolb1bf23132010-11-19 12:55:12 -0800119 updateTabs(mUiController.getTabs());
Michael Kolba2b2ba82010-08-04 17:54:03 -0700120
Michael Kolba2b2ba82010-08-04 17:54:03 -0700121 mUserRequestedUrlbar = false;
Michael Kolba0e2a752010-12-12 15:27:56 -0800122 mVisibleTitleHeight = 1;
Michael Kolbebba8b42010-09-30 12:57:59 -0700123 mButtonWidth = -1;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800124 // tab dimensions
125 mTabHeight = (int) res.getDimension(R.dimen.tab_height);
126 mTabOverlap = (int) res.getDimension(R.dimen.tab_overlap);
127 mTabSliceWidth = (int) res.getDimension(R.dimen.tab_slice);
128 mTabPadding = (int) res.getDimension(R.dimen.tab_padding);
129 int maxTabWidth = (int) res.getDimension(R.dimen.max_tab_width);
130 // shader initialization
131 mShaderBuffer = Bitmap.createBitmap(maxTabWidth, mTabHeight,
132 Bitmap.Config.ARGB_8888);
133 mShaderCanvas = new Canvas(mShaderBuffer);
134 mShaderPaint = new Paint();
135 BitmapShader shader = new BitmapShader(mShaderBuffer,
136 Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
137 mShaderPaint.setShader(shader);
138 mShaderPaint.setStyle(Paint.Style.FILL);
139 mShaderPaint.setAntiAlias(true);
Michael Kolbebba8b42010-09-30 12:57:59 -0700140 }
141
Michael Kolb1bf23132010-11-19 12:55:12 -0800142 void updateTabs(List<Tab> tabs) {
143 mTabs.clearTabs();
144 mTabMap.clear();
145 for (Tab tab : tabs) {
146 TabViewData data = buildTab(tab);
147 TabView tv = buildView(data);
148 }
149 mTabs.setSelectedTab(mTabControl.getCurrentIndex());
150 }
151
Michael Kolbebba8b42010-09-30 12:57:59 -0700152 @Override
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800153 protected void onMeasure(int hspec, int vspec) {
154 super.onMeasure(hspec, vspec);
155 int w = getMeasuredWidth();
156 // adjust for new tab overlap
157 w -= mTabOverlap;
158 setMeasuredDimension(w, getMeasuredHeight());
159 }
160
161 @Override
Michael Kolbebba8b42010-09-30 12:57:59 -0700162 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800163 // use paddingLeft and paddingTop
164 int pl = getPaddingLeft();
165 int pt = getPaddingTop();
Michael Kolbebba8b42010-09-30 12:57:59 -0700166 if (mButtonWidth == -1) {
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800167 mButtonWidth = mNewTab.getMeasuredWidth() - mTabOverlap;
Michael Kolbebba8b42010-09-30 12:57:59 -0700168 }
169 int sw = mTabs.getMeasuredWidth();
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800170 int w = right - left - pl;
Michael Kolbebba8b42010-09-30 12:57:59 -0700171 if (w-sw < mButtonWidth) {
172 sw = w - mButtonWidth;
173 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800174 mTabs.layout(pl, pt, pl + sw, bottom - top);
175 // adjust for overlap
176 mNewTab.layout(pl + sw - mTabOverlap, pt,
177 pl + sw + mButtonWidth - mTabOverlap, bottom - top);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700178 }
179
180 public void onClick(View view) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700181 mUi.hideComboView();
Michael Kolbebba8b42010-09-30 12:57:59 -0700182 if (mNewTab == view) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700183 mUiController.openTabToHomePage();
Michael Kolbebba8b42010-09-30 12:57:59 -0700184 } else if (mTabs.getSelectedTab() == view) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700185 if (mUi.isFakeTitleBarShowing() && !isLoading()) {
186 mUi.hideFakeTitleBar();
Michael Kolbed217742010-08-10 17:52:34 -0700187 } else {
188 showUrlBar();
189 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700190 } else {
Michael Kolbebba8b42010-09-30 12:57:59 -0700191 int ix = mTabs.getChildIndex(view);
192 if (ix >= 0) {
193 mTabs.setSelectedTab(ix);
Michael Kolb8233fac2010-10-26 16:08:53 -0700194 mUiController.switchToTab(ix);
Michael Kolbebba8b42010-09-30 12:57:59 -0700195 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700196 }
197 }
198
Michael Kolbed217742010-08-10 17:52:34 -0700199 private void showUrlBar() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700200 mUi.stopWebViewScrolling();
201 mUi.showFakeTitleBar();
Michael Kolbed217742010-08-10 17:52:34 -0700202 mUserRequestedUrlbar = true;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700203 }
204
Michael Kolba0e2a752010-12-12 15:27:56 -0800205 private void showTitleBarIndicator(boolean show) {
206 Tab tab = mTabControl.getCurrentTab();
207 if (tab != null) {
208 TabViewData tvd = mTabMap.get(tab);
209 if (tvd != null) {
210 tvd.mTabView.showIndicator(show);
211 }
212 }
Michael Kolbed217742010-08-10 17:52:34 -0700213 }
214
215 // callback after fake titlebar is shown
216 void onShowTitleBar() {
Michael Kolba0e2a752010-12-12 15:27:56 -0800217 showTitleBarIndicator(false);
Michael Kolbed217742010-08-10 17:52:34 -0700218 }
219
220 // callback after fake titlebar is hidden
Michael Kolba2b2ba82010-08-04 17:54:03 -0700221 void onHideTitleBar() {
Michael Kolba0e2a752010-12-12 15:27:56 -0800222 showTitleBarIndicator(mVisibleTitleHeight == 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700223 Tab tab = mTabControl.getCurrentTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700224 tab.getWebView().requestFocus();
225 mUserRequestedUrlbar = false;
226 }
227
Michael Kolbed217742010-08-10 17:52:34 -0700228 // webview scroll listener
229
230 @Override
Michael Kolba0e2a752010-12-12 15:27:56 -0800231 public void onScroll(int visibleTitleHeight) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700232 // isLoading is using the current tab, which initially might not be set yet
233 if (mTabControl.getCurrentTab() != null) {
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800234 if ((mVisibleTitleHeight != 0) && (visibleTitleHeight == 0)
Michael Kolba0e2a752010-12-12 15:27:56 -0800235 && !isLoading()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700236 if (mUserRequestedUrlbar) {
237 mUi.hideFakeTitleBar();
238 } else {
Michael Kolba0e2a752010-12-12 15:27:56 -0800239 showTitleBarIndicator(true);
Michael Kolb8233fac2010-10-26 16:08:53 -0700240 }
Michael Kolba0e2a752010-12-12 15:27:56 -0800241 } else if ((mVisibleTitleHeight == 0) && (visibleTitleHeight != 0)
242 && !isLoading()) {
243 showTitleBarIndicator(false);
Michael Kolbed217742010-08-10 17:52:34 -0700244 }
245 }
Michael Kolba0e2a752010-12-12 15:27:56 -0800246 mVisibleTitleHeight = visibleTitleHeight;
Michael Kolbed217742010-08-10 17:52:34 -0700247 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700248
249 @Override
250 public void createContextMenu(ContextMenu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700251 MenuInflater inflater = mActivity.getMenuInflater();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700252 inflater.inflate(R.menu.title_context, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -0700253 mActivity.onCreateContextMenu(menu, this, null);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700254 }
255
256 private TabViewData buildTab(Tab tab) {
257 TabViewData data = new TabViewData(tab);
258 mTabMap.put(tab, data);
259 return data;
260 }
261
262 private TabView buildView(final TabViewData data) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700263 TabView tv = new TabView(mActivity, data);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700264 tv.setTag(data);
265 tv.setOnClickListener(this);
266 mTabs.addTab(tv);
267 return tv;
268 }
269
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800270 @Override
271 protected int getChildDrawingOrder(int count, int i) {
272 // reverse
273 return count - 1 - i;
274 }
275
Michael Kolba2b2ba82010-08-04 17:54:03 -0700276 /**
277 * View used in the tab bar
278 */
279 class TabView extends LinearLayout implements OnClickListener {
280
281 TabViewData mTabData;
282 View mTabContent;
283 TextView mTitle;
Michael Kolba0e2a752010-12-12 15:27:56 -0800284 View mIndicator;
Michael Kolbae62fd42010-08-18 16:33:28 -0700285 View mIncognito;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700286 ImageView mIconView;
287 ImageView mLock;
288 ImageView mClose;
289 boolean mSelected;
290 boolean mInLoad;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800291 Path mPath;
292 int[] mWindowPos;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700293
294 /**
295 * @param context
296 */
297 public TabView(Context context, TabViewData tab) {
298 super(context);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800299 setWillNotDraw(false);
300 mPath = new Path();
301 mWindowPos = new int[2];
Michael Kolba2b2ba82010-08-04 17:54:03 -0700302 mTabData = tab;
Michael Kolbed217742010-08-10 17:52:34 -0700303 setGravity(Gravity.CENTER_VERTICAL);
304 setOrientation(LinearLayout.HORIZONTAL);
Michael Kolba0e2a752010-12-12 15:27:56 -0800305 setPadding(mTabPadding, 0, 0, 0);
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100306 LayoutInflater inflater = LayoutInflater.from(getContext());
Michael Kolbed217742010-08-10 17:52:34 -0700307 mTabContent = inflater.inflate(R.layout.tab_title, this, true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700308 mTitle = (TextView) mTabContent.findViewById(R.id.title);
309 mIconView = (ImageView) mTabContent.findViewById(R.id.favicon);
310 mLock = (ImageView) mTabContent.findViewById(R.id.lock);
311 mClose = (ImageView) mTabContent.findViewById(R.id.close);
312 mClose.setOnClickListener(this);
Michael Kolbae62fd42010-08-18 16:33:28 -0700313 mIncognito = mTabContent.findViewById(R.id.incognito);
Michael Kolba0e2a752010-12-12 15:27:56 -0800314 mIndicator = mTabContent.findViewById(R.id.chevron);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700315 mSelected = false;
316 mInLoad = false;
317 // update the status
318 updateFromData();
319 }
320
Michael Kolba0e2a752010-12-12 15:27:56 -0800321 void showIndicator(boolean show) {
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800322 if (mSelected) {
323 mIndicator.setVisibility(show ? View.VISIBLE : View.GONE);
324 } else {
325 mIndicator.setVisibility(View.GONE);
326 }
Michael Kolba0e2a752010-12-12 15:27:56 -0800327 }
328
Michael Kolba2b2ba82010-08-04 17:54:03 -0700329 @Override
330 public void onClick(View v) {
331 if (v == mClose) {
332 closeTab();
333 }
334 }
335
336 private void updateFromData() {
337 mTabData.mTabView = this;
338 if (mTabData.mUrl != null) {
339 setDisplayTitle(mTabData.mUrl);
340 }
341 if (mTabData.mTitle != null) {
342 setDisplayTitle(mTabData.mTitle);
343 }
344 setProgress(mTabData.mProgress);
345 if (mTabData.mIcon != null) {
346 setFavicon(mTabData.mIcon);
347 }
348 if (mTabData.mLock != null) {
349 setLock(mTabData.mLock);
350 }
Michael Kolbae62fd42010-08-18 16:33:28 -0700351 if (mTabData.mTab != null) {
352 mIncognito.setVisibility(
Michael Kolb1bf23132010-11-19 12:55:12 -0800353 mTabData.mTab.isPrivateBrowsingEnabled() ?
Michael Kolbae62fd42010-08-18 16:33:28 -0700354 View.VISIBLE : View.GONE);
355 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700356 }
357
358 @Override
Michael Kolbb7b115e2010-09-25 16:59:37 -0700359 public void setActivated(boolean selected) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700360 mSelected = selected;
361 mClose.setVisibility(mSelected ? View.VISIBLE : View.GONE);
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800362 mIndicator.setVisibility(View.GONE);
Michael Kolb8233fac2010-10-26 16:08:53 -0700363 mTitle.setTextAppearance(mActivity, mSelected ?
Michael Kolbc7485ae2010-09-03 10:10:58 -0700364 R.style.TabTitleSelected : R.style.TabTitleUnselected);
365 setHorizontalFadingEdgeEnabled(!mSelected);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700366 super.setActivated(selected);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800367 LayoutParams lp = (LinearLayout.LayoutParams) getLayoutParams();
368 lp.width = selected ? mTabWidthSelected : mTabWidthUnselected;
369 lp.height = LayoutParams.MATCH_PARENT;
370 setLayoutParams(lp);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700371 }
372
373 void setDisplayTitle(String title) {
374 mTitle.setText(title);
375 }
376
377 void setFavicon(Drawable d) {
378 mIconView.setImageDrawable(d);
379 }
380
381 void setLock(Drawable d) {
382 if (null == d) {
383 mLock.setVisibility(View.GONE);
384 } else {
385 mLock.setImageDrawable(d);
386 mLock.setVisibility(View.VISIBLE);
387 }
388 }
389
Michael Kolba2b2ba82010-08-04 17:54:03 -0700390 void setProgress(int newProgress) {
391 if (newProgress >= PROGRESS_MAX) {
392 mInLoad = false;
393 } else {
394 if (!mInLoad && getWindowToken() != null) {
395 mInLoad = true;
396 }
397 }
398 }
399
400 private void closeTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700401 if (mTabData.mTab == mTabControl.getCurrentTab()) {
402 mUiController.closeCurrentTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700403 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -0700404 mUiController.closeTab(mTabData.mTab);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700405 }
406 }
407
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800408 @Override
409 protected void onLayout(boolean changed, int l, int t, int r, int b) {
410 super.onLayout(changed, l, t, r, b);
411 setTabPath(mPath, 0, 0, r - l, b - t);
412 }
413
414 @Override
415 protected void dispatchDraw(Canvas canvas) {
416 int state = canvas.save();
417 int[] pos = new int[2];
418 getLocationInWindow(mWindowPos);
419 Drawable drawable = mSelected ? mActiveDrawable : mInactiveDrawable;
420 drawable.setBounds(0, 0, mUi.getTitleBarWidth(), getHeight());
421 drawClipped(canvas, drawable, mPath, mWindowPos[0]);
422 canvas.restoreToCount(state);
423 super.dispatchDraw(canvas);
424 }
425
426 private void drawClipped(Canvas canvas, Drawable drawable,
427 Path clipPath, int left) {
428 mShaderCanvas.drawColor(Color.TRANSPARENT);
429 mShaderCanvas.translate(-left, 0);
430 drawable.draw(mShaderCanvas);
431 canvas.drawPath(clipPath, mShaderPaint);
432 mShaderCanvas.translate(left, 0);
433 }
434
435 private void setTabPath(Path path, int l, int t, int r, int b) {
436 path.reset();
437 path.moveTo(l, b);
438 path.lineTo(l, t);
439 path.lineTo(r - mTabSliceWidth, t);
440 path.lineTo(r, b);
441 path.close();
442 }
443
Michael Kolba2b2ba82010-08-04 17:54:03 -0700444 }
445
446 /**
447 * Store tab state within the title bar
448 */
449 class TabViewData {
450
451 Tab mTab;
452 TabView mTabView;
453 int mProgress;
454 Drawable mIcon;
455 Drawable mLock;
456 String mTitle;
457 String mUrl;
458
459 TabViewData(Tab tab) {
460 mTab = tab;
John Reckd8657622010-10-25 16:20:40 -0700461 WebView web = tab.getWebView();
462 if (web != null) {
463 setUrlAndTitle(web.getUrl(), web.getTitle());
464 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700465 }
466
467 void setUrlAndTitle(String url, String title) {
468 mUrl = url;
469 mTitle = title;
470 if (mTabView != null) {
471 if (title != null) {
472 mTabView.setDisplayTitle(title);
473 } else if (url != null) {
John Reckfb3017f2010-10-26 19:01:24 -0700474 mTabView.setDisplayTitle(UrlUtils.stripUrl(url));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700475 }
476 }
477 }
478
479 void setProgress(int newProgress) {
480 mProgress = newProgress;
481 if (mTabView != null) {
482 mTabView.setProgress(mProgress);
483 }
484 }
485
486 void setFavicon(Bitmap icon) {
487 Drawable[] array = new Drawable[3];
488 array[0] = new PaintDrawable(Color.BLACK);
489 array[1] = new PaintDrawable(Color.WHITE);
490 if (icon == null) {
John Reckaff60fb2010-10-25 13:47:58 -0700491 array[2] = mGenericFavicon;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700492 } else {
493 array[2] = new BitmapDrawable(icon);
494 }
495 LayerDrawable d = new LayerDrawable(array);
496 d.setLayerInset(1, 1, 1, 1, 1);
497 d.setLayerInset(2, 2, 2, 2, 2);
498 mIcon = d;
499 if (mTabView != null) {
500 mTabView.setFavicon(mIcon);
501 }
502 }
503
504 }
505
506 // TabChangeListener implementation
507
Michael Kolb8233fac2010-10-26 16:08:53 -0700508 public void onSetActiveTab(Tab tab) {
509 mTabs.setSelectedTab(mTabControl.getTabIndex(tab));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700510 TabViewData tvd = mTabMap.get(tab);
511 if (tvd != null) {
512 tvd.setProgress(tvd.mProgress);
513 // update the scroll state
514 WebView webview = tab.getWebView();
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800515 if (webview != null) {
516 int h = webview.getVisibleTitleHeight();
517 mVisibleTitleHeight = h -1;
518 onScroll(h);
519 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700520 }
521 }
522
Michael Kolba2b2ba82010-08-04 17:54:03 -0700523 public void onFavicon(Tab tab, Bitmap favicon) {
524 TabViewData tvd = mTabMap.get(tab);
525 if (tvd != null) {
526 tvd.setFavicon(favicon);
527 }
528 }
529
Michael Kolba2b2ba82010-08-04 17:54:03 -0700530 public void onNewTab(Tab tab) {
531 TabViewData tvd = buildTab(tab);
532 buildView(tvd);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700533 }
534
Michael Kolba2b2ba82010-08-04 17:54:03 -0700535 public void onProgress(Tab tab, int progress) {
536 TabViewData tvd = mTabMap.get(tab);
537 if (tvd != null) {
538 tvd.setProgress(progress);
539 }
540 }
541
Michael Kolba2b2ba82010-08-04 17:54:03 -0700542 public void onRemoveTab(Tab tab) {
543 TabViewData tvd = mTabMap.get(tab);
Michael Kolbe2752f72010-10-25 16:50:16 -0700544 if (tvd != null) {
545 TabView tv = tvd.mTabView;
546 if (tv != null) {
547 mTabs.removeTab(tv);
548 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700549 }
550 mTabMap.remove(tab);
551 }
552
Michael Kolba2b2ba82010-08-04 17:54:03 -0700553 public void onUrlAndTitle(Tab tab, String url, String title) {
John Reckfb3017f2010-10-26 19:01:24 -0700554 mHasReceivedTitle = true;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700555 TabViewData tvd = mTabMap.get(tab);
556 if (tvd != null) {
557 tvd.setUrlAndTitle(url, title);
558 }
559 }
560
Michael Kolba2b2ba82010-08-04 17:54:03 -0700561 public void onPageFinished(Tab tab) {
John Reckfb3017f2010-10-26 19:01:24 -0700562 if (!mHasReceivedTitle) {
563 TabViewData tvd = mTabMap.get(tab);
564 if (tvd != null) {
565 tvd.setUrlAndTitle(tvd.mUrl, null);
566 }
567 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700568 }
569
John Reckfb3017f2010-10-26 19:01:24 -0700570 public void onPageStarted(Tab tab, String url, Bitmap favicon) {
571 mHasReceivedTitle = false;
John Reckaff60fb2010-10-25 13:47:58 -0700572 TabViewData tvd = mTabMap.get(tab);
573 if (tvd != null) {
574 tvd.setFavicon(favicon);
John Reckfb3017f2010-10-26 19:01:24 -0700575 tvd.setUrlAndTitle(url, mLoadingText);
John Reckaff60fb2010-10-25 13:47:58 -0700576 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700577 }
578
Michael Kolba2b2ba82010-08-04 17:54:03 -0700579 private boolean isLoading() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700580 TabViewData tvd = mTabMap.get(mTabControl.getCurrentTab());
581 if ((tvd != null) && (tvd.mTabView != null)) {
582 return tvd.mTabView.mInLoad;
583 } else {
584 return false;
585 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700586 }
587
Michael Kolba2b2ba82010-08-04 17:54:03 -0700588}