blob: ea734a6d0620ff10847d9403f8c98fe2df3d69c0 [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;
Michael Kolb376b5412010-12-15 11:52:57 -080091 private boolean mUseQuickControls;
Michael Kolb2b5a13a2010-12-09 14:13:42 -080092
Michael Kolb66706532010-12-12 19:50:22 -080093 public TabBar(Activity activity, UiController controller, XLargeUi ui) {
Michael Kolb8233fac2010-10-26 16:08:53 -070094 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 Kolb376b5412010-12-15 11:52:57 -0800143 void setUseQuickControls(boolean useQuickControls) {
144 mUseQuickControls = useQuickControls;
145 }
146
147 int getTabCount() {
148 return mTabMap.size();
149 }
150
Michael Kolb1bf23132010-11-19 12:55:12 -0800151 void updateTabs(List<Tab> tabs) {
152 mTabs.clearTabs();
153 mTabMap.clear();
154 for (Tab tab : tabs) {
155 TabViewData data = buildTab(tab);
156 TabView tv = buildView(data);
157 }
158 mTabs.setSelectedTab(mTabControl.getCurrentIndex());
159 }
160
Michael Kolbebba8b42010-09-30 12:57:59 -0700161 @Override
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800162 protected void onMeasure(int hspec, int vspec) {
163 super.onMeasure(hspec, vspec);
164 int w = getMeasuredWidth();
165 // adjust for new tab overlap
166 w -= mTabOverlap;
167 setMeasuredDimension(w, getMeasuredHeight());
168 }
169
170 @Override
Michael Kolbebba8b42010-09-30 12:57:59 -0700171 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800172 // use paddingLeft and paddingTop
173 int pl = getPaddingLeft();
174 int pt = getPaddingTop();
Michael Kolbebba8b42010-09-30 12:57:59 -0700175 if (mButtonWidth == -1) {
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800176 mButtonWidth = mNewTab.getMeasuredWidth() - mTabOverlap;
Michael Kolbebba8b42010-09-30 12:57:59 -0700177 }
178 int sw = mTabs.getMeasuredWidth();
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800179 int w = right - left - pl;
Michael Kolbebba8b42010-09-30 12:57:59 -0700180 if (w-sw < mButtonWidth) {
181 sw = w - mButtonWidth;
182 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800183 mTabs.layout(pl, pt, pl + sw, bottom - top);
184 // adjust for overlap
185 mNewTab.layout(pl + sw - mTabOverlap, pt,
186 pl + sw + mButtonWidth - mTabOverlap, bottom - top);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700187 }
188
189 public void onClick(View view) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700190 mUi.hideComboView();
Michael Kolbebba8b42010-09-30 12:57:59 -0700191 if (mNewTab == view) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700192 mUiController.openTabToHomePage();
Michael Kolbebba8b42010-09-30 12:57:59 -0700193 } else if (mTabs.getSelectedTab() == view) {
Michael Kolb376b5412010-12-15 11:52:57 -0800194 if (mUseQuickControls) return;
Michael Kolb8233fac2010-10-26 16:08:53 -0700195 if (mUi.isFakeTitleBarShowing() && !isLoading()) {
196 mUi.hideFakeTitleBar();
Michael Kolbed217742010-08-10 17:52:34 -0700197 } else {
198 showUrlBar();
199 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700200 } else {
Michael Kolbebba8b42010-09-30 12:57:59 -0700201 int ix = mTabs.getChildIndex(view);
202 if (ix >= 0) {
203 mTabs.setSelectedTab(ix);
Michael Kolb8233fac2010-10-26 16:08:53 -0700204 mUiController.switchToTab(ix);
Michael Kolbebba8b42010-09-30 12:57:59 -0700205 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700206 }
207 }
208
Michael Kolbed217742010-08-10 17:52:34 -0700209 private void showUrlBar() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700210 mUi.stopWebViewScrolling();
211 mUi.showFakeTitleBar();
Michael Kolbed217742010-08-10 17:52:34 -0700212 mUserRequestedUrlbar = true;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700213 }
214
Michael Kolb376b5412010-12-15 11:52:57 -0800215 void showTitleBarIndicator(boolean show) {
Michael Kolba0e2a752010-12-12 15:27:56 -0800216 Tab tab = mTabControl.getCurrentTab();
217 if (tab != null) {
218 TabViewData tvd = mTabMap.get(tab);
219 if (tvd != null) {
220 tvd.mTabView.showIndicator(show);
221 }
222 }
Michael Kolbed217742010-08-10 17:52:34 -0700223 }
224
225 // callback after fake titlebar is shown
226 void onShowTitleBar() {
Michael Kolba0e2a752010-12-12 15:27:56 -0800227 showTitleBarIndicator(false);
Michael Kolbed217742010-08-10 17:52:34 -0700228 }
229
230 // callback after fake titlebar is hidden
Michael Kolba2b2ba82010-08-04 17:54:03 -0700231 void onHideTitleBar() {
Michael Kolba0e2a752010-12-12 15:27:56 -0800232 showTitleBarIndicator(mVisibleTitleHeight == 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700233 Tab tab = mTabControl.getCurrentTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700234 tab.getWebView().requestFocus();
235 mUserRequestedUrlbar = false;
236 }
237
Michael Kolbed217742010-08-10 17:52:34 -0700238 // webview scroll listener
239
240 @Override
Michael Kolba0e2a752010-12-12 15:27:56 -0800241 public void onScroll(int visibleTitleHeight) {
Michael Kolb376b5412010-12-15 11:52:57 -0800242 if (mUseQuickControls) return;
Michael Kolb8233fac2010-10-26 16:08:53 -0700243 // isLoading is using the current tab, which initially might not be set yet
244 if (mTabControl.getCurrentTab() != null) {
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800245 if ((mVisibleTitleHeight != 0) && (visibleTitleHeight == 0)
Michael Kolba0e2a752010-12-12 15:27:56 -0800246 && !isLoading()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700247 if (mUserRequestedUrlbar) {
248 mUi.hideFakeTitleBar();
249 } else {
Michael Kolba0e2a752010-12-12 15:27:56 -0800250 showTitleBarIndicator(true);
Michael Kolb8233fac2010-10-26 16:08:53 -0700251 }
Michael Kolba0e2a752010-12-12 15:27:56 -0800252 } else if ((mVisibleTitleHeight == 0) && (visibleTitleHeight != 0)
253 && !isLoading()) {
254 showTitleBarIndicator(false);
Michael Kolbed217742010-08-10 17:52:34 -0700255 }
256 }
Michael Kolba0e2a752010-12-12 15:27:56 -0800257 mVisibleTitleHeight = visibleTitleHeight;
Michael Kolbed217742010-08-10 17:52:34 -0700258 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700259
260 @Override
261 public void createContextMenu(ContextMenu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700262 MenuInflater inflater = mActivity.getMenuInflater();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700263 inflater.inflate(R.menu.title_context, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -0700264 mActivity.onCreateContextMenu(menu, this, null);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700265 }
266
267 private TabViewData buildTab(Tab tab) {
268 TabViewData data = new TabViewData(tab);
269 mTabMap.put(tab, data);
270 return data;
271 }
272
273 private TabView buildView(final TabViewData data) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700274 TabView tv = new TabView(mActivity, data);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700275 tv.setTag(data);
276 tv.setOnClickListener(this);
277 mTabs.addTab(tv);
278 return tv;
279 }
280
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800281 @Override
282 protected int getChildDrawingOrder(int count, int i) {
283 // reverse
284 return count - 1 - i;
285 }
286
Michael Kolba2b2ba82010-08-04 17:54:03 -0700287 /**
288 * View used in the tab bar
289 */
290 class TabView extends LinearLayout implements OnClickListener {
291
292 TabViewData mTabData;
293 View mTabContent;
294 TextView mTitle;
Michael Kolba0e2a752010-12-12 15:27:56 -0800295 View mIndicator;
Michael Kolbae62fd42010-08-18 16:33:28 -0700296 View mIncognito;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700297 ImageView mIconView;
298 ImageView mLock;
299 ImageView mClose;
300 boolean mSelected;
301 boolean mInLoad;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800302 Path mPath;
303 int[] mWindowPos;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700304
305 /**
306 * @param context
307 */
308 public TabView(Context context, TabViewData tab) {
309 super(context);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800310 setWillNotDraw(false);
311 mPath = new Path();
312 mWindowPos = new int[2];
Michael Kolba2b2ba82010-08-04 17:54:03 -0700313 mTabData = tab;
Michael Kolbed217742010-08-10 17:52:34 -0700314 setGravity(Gravity.CENTER_VERTICAL);
315 setOrientation(LinearLayout.HORIZONTAL);
Michael Kolba0e2a752010-12-12 15:27:56 -0800316 setPadding(mTabPadding, 0, 0, 0);
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100317 LayoutInflater inflater = LayoutInflater.from(getContext());
Michael Kolbed217742010-08-10 17:52:34 -0700318 mTabContent = inflater.inflate(R.layout.tab_title, this, true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700319 mTitle = (TextView) mTabContent.findViewById(R.id.title);
320 mIconView = (ImageView) mTabContent.findViewById(R.id.favicon);
321 mLock = (ImageView) mTabContent.findViewById(R.id.lock);
322 mClose = (ImageView) mTabContent.findViewById(R.id.close);
323 mClose.setOnClickListener(this);
Michael Kolbae62fd42010-08-18 16:33:28 -0700324 mIncognito = mTabContent.findViewById(R.id.incognito);
Michael Kolba0e2a752010-12-12 15:27:56 -0800325 mIndicator = mTabContent.findViewById(R.id.chevron);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700326 mSelected = false;
327 mInLoad = false;
328 // update the status
329 updateFromData();
330 }
331
Michael Kolba0e2a752010-12-12 15:27:56 -0800332 void showIndicator(boolean show) {
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800333 if (mSelected) {
334 mIndicator.setVisibility(show ? View.VISIBLE : View.GONE);
335 } else {
336 mIndicator.setVisibility(View.GONE);
337 }
Michael Kolba0e2a752010-12-12 15:27:56 -0800338 }
339
Michael Kolba2b2ba82010-08-04 17:54:03 -0700340 @Override
341 public void onClick(View v) {
342 if (v == mClose) {
343 closeTab();
344 }
345 }
346
347 private void updateFromData() {
348 mTabData.mTabView = this;
349 if (mTabData.mUrl != null) {
350 setDisplayTitle(mTabData.mUrl);
351 }
352 if (mTabData.mTitle != null) {
353 setDisplayTitle(mTabData.mTitle);
354 }
355 setProgress(mTabData.mProgress);
356 if (mTabData.mIcon != null) {
357 setFavicon(mTabData.mIcon);
358 }
359 if (mTabData.mLock != null) {
360 setLock(mTabData.mLock);
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 }
424
425 @Override
426 protected void dispatchDraw(Canvas canvas) {
427 int state = canvas.save();
428 int[] pos = new int[2];
429 getLocationInWindow(mWindowPos);
430 Drawable drawable = mSelected ? mActiveDrawable : mInactiveDrawable;
Michael Kolb376b5412010-12-15 11:52:57 -0800431 drawable.setBounds(0, 0, mUi.getContentWidth(), getHeight());
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800432 drawClipped(canvas, drawable, mPath, mWindowPos[0]);
433 canvas.restoreToCount(state);
434 super.dispatchDraw(canvas);
435 }
436
437 private void drawClipped(Canvas canvas, Drawable drawable,
438 Path clipPath, int left) {
439 mShaderCanvas.drawColor(Color.TRANSPARENT);
440 mShaderCanvas.translate(-left, 0);
441 drawable.draw(mShaderCanvas);
442 canvas.drawPath(clipPath, mShaderPaint);
443 mShaderCanvas.translate(left, 0);
444 }
445
446 private void setTabPath(Path path, int l, int t, int r, int b) {
447 path.reset();
448 path.moveTo(l, b);
449 path.lineTo(l, t);
450 path.lineTo(r - mTabSliceWidth, t);
451 path.lineTo(r, b);
452 path.close();
453 }
454
Michael Kolba2b2ba82010-08-04 17:54:03 -0700455 }
456
457 /**
458 * Store tab state within the title bar
459 */
460 class TabViewData {
461
462 Tab mTab;
463 TabView mTabView;
464 int mProgress;
465 Drawable mIcon;
466 Drawable mLock;
467 String mTitle;
468 String mUrl;
469
470 TabViewData(Tab tab) {
471 mTab = tab;
John Reckd8657622010-10-25 16:20:40 -0700472 WebView web = tab.getWebView();
473 if (web != null) {
474 setUrlAndTitle(web.getUrl(), web.getTitle());
475 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700476 }
477
478 void setUrlAndTitle(String url, String title) {
479 mUrl = url;
480 mTitle = title;
481 if (mTabView != null) {
482 if (title != null) {
483 mTabView.setDisplayTitle(title);
484 } else if (url != null) {
John Reckfb3017f2010-10-26 19:01:24 -0700485 mTabView.setDisplayTitle(UrlUtils.stripUrl(url));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700486 }
487 }
488 }
489
490 void setProgress(int newProgress) {
491 mProgress = newProgress;
492 if (mTabView != null) {
493 mTabView.setProgress(mProgress);
494 }
495 }
496
497 void setFavicon(Bitmap icon) {
498 Drawable[] array = new Drawable[3];
499 array[0] = new PaintDrawable(Color.BLACK);
500 array[1] = new PaintDrawable(Color.WHITE);
501 if (icon == null) {
John Reckaff60fb2010-10-25 13:47:58 -0700502 array[2] = mGenericFavicon;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700503 } else {
504 array[2] = new BitmapDrawable(icon);
505 }
506 LayerDrawable d = new LayerDrawable(array);
507 d.setLayerInset(1, 1, 1, 1, 1);
508 d.setLayerInset(2, 2, 2, 2, 2);
509 mIcon = d;
510 if (mTabView != null) {
511 mTabView.setFavicon(mIcon);
512 }
513 }
514
515 }
516
517 // TabChangeListener implementation
518
Michael Kolb8233fac2010-10-26 16:08:53 -0700519 public void onSetActiveTab(Tab tab) {
520 mTabs.setSelectedTab(mTabControl.getTabIndex(tab));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700521 TabViewData tvd = mTabMap.get(tab);
522 if (tvd != null) {
523 tvd.setProgress(tvd.mProgress);
524 // update the scroll state
525 WebView webview = tab.getWebView();
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800526 if (webview != null) {
527 int h = webview.getVisibleTitleHeight();
528 mVisibleTitleHeight = h -1;
529 onScroll(h);
530 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700531 }
532 }
533
Michael Kolba2b2ba82010-08-04 17:54:03 -0700534 public void onFavicon(Tab tab, Bitmap favicon) {
535 TabViewData tvd = mTabMap.get(tab);
536 if (tvd != null) {
537 tvd.setFavicon(favicon);
538 }
539 }
540
Michael Kolba2b2ba82010-08-04 17:54:03 -0700541 public void onNewTab(Tab tab) {
542 TabViewData tvd = buildTab(tab);
543 buildView(tvd);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700544 }
545
Michael Kolba2b2ba82010-08-04 17:54:03 -0700546 public void onProgress(Tab tab, int progress) {
547 TabViewData tvd = mTabMap.get(tab);
548 if (tvd != null) {
549 tvd.setProgress(progress);
550 }
551 }
552
Michael Kolba2b2ba82010-08-04 17:54:03 -0700553 public void onRemoveTab(Tab tab) {
554 TabViewData tvd = mTabMap.get(tab);
Michael Kolbe2752f72010-10-25 16:50:16 -0700555 if (tvd != null) {
556 TabView tv = tvd.mTabView;
557 if (tv != null) {
558 mTabs.removeTab(tv);
559 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700560 }
561 mTabMap.remove(tab);
562 }
563
Michael Kolba2b2ba82010-08-04 17:54:03 -0700564 public void onUrlAndTitle(Tab tab, String url, String title) {
John Reckfb3017f2010-10-26 19:01:24 -0700565 mHasReceivedTitle = true;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700566 TabViewData tvd = mTabMap.get(tab);
567 if (tvd != null) {
568 tvd.setUrlAndTitle(url, title);
569 }
570 }
571
Michael Kolba2b2ba82010-08-04 17:54:03 -0700572 public void onPageFinished(Tab tab) {
John Reckfb3017f2010-10-26 19:01:24 -0700573 if (!mHasReceivedTitle) {
574 TabViewData tvd = mTabMap.get(tab);
575 if (tvd != null) {
576 tvd.setUrlAndTitle(tvd.mUrl, null);
577 }
578 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700579 }
580
John Reckfb3017f2010-10-26 19:01:24 -0700581 public void onPageStarted(Tab tab, String url, Bitmap favicon) {
582 mHasReceivedTitle = false;
John Reckaff60fb2010-10-25 13:47:58 -0700583 TabViewData tvd = mTabMap.get(tab);
584 if (tvd != null) {
585 tvd.setFavicon(favicon);
John Reckfb3017f2010-10-26 19:01:24 -0700586 tvd.setUrlAndTitle(url, mLoadingText);
John Reckaff60fb2010-10-25 13:47:58 -0700587 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700588 }
589
Michael Kolba2b2ba82010-08-04 17:54:03 -0700590 private boolean isLoading() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700591 TabViewData tvd = mTabMap.get(mTabControl.getCurrentTab());
592 if ((tvd != null) && (tvd.mTabView != null)) {
593 return tvd.mTabView.mInLoad;
594 } else {
595 return false;
596 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700597 }
598
Michael Kolba2b2ba82010-08-04 17:54:03 -0700599}