blob: 88209fe283f07863c046bdff8c53844d3ddd9ed3 [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
241 if (mTabControl.getCurrentTab() != null) {
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800242 if ((mVisibleTitleHeight != 0) && (visibleTitleHeight == 0)
Michael Kolba0e2a752010-12-12 15:27:56 -0800243 && !isLoading()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700244 if (mUserRequestedUrlbar) {
245 mUi.hideFakeTitleBar();
246 } else {
Michael Kolba0e2a752010-12-12 15:27:56 -0800247 showTitleBarIndicator(true);
Michael Kolb8233fac2010-10-26 16:08:53 -0700248 }
Michael Kolba0e2a752010-12-12 15:27:56 -0800249 } else if ((mVisibleTitleHeight == 0) && (visibleTitleHeight != 0)
250 && !isLoading()) {
251 showTitleBarIndicator(false);
Michael Kolbed217742010-08-10 17:52:34 -0700252 }
253 }
Michael Kolba0e2a752010-12-12 15:27:56 -0800254 mVisibleTitleHeight = visibleTitleHeight;
Michael Kolbed217742010-08-10 17:52:34 -0700255 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700256
257 @Override
258 public void createContextMenu(ContextMenu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700259 MenuInflater inflater = mActivity.getMenuInflater();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700260 inflater.inflate(R.menu.title_context, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -0700261 mActivity.onCreateContextMenu(menu, this, null);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700262 }
263
264 private TabViewData buildTab(Tab tab) {
265 TabViewData data = new TabViewData(tab);
266 mTabMap.put(tab, data);
267 return data;
268 }
269
270 private TabView buildView(final TabViewData data) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700271 TabView tv = new TabView(mActivity, data);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700272 tv.setTag(data);
273 tv.setOnClickListener(this);
274 mTabs.addTab(tv);
275 return tv;
276 }
277
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800278 @Override
279 protected int getChildDrawingOrder(int count, int i) {
280 // reverse
281 return count - 1 - i;
282 }
283
Michael Kolba2b2ba82010-08-04 17:54:03 -0700284 /**
285 * View used in the tab bar
286 */
287 class TabView extends LinearLayout implements OnClickListener {
288
289 TabViewData mTabData;
290 View mTabContent;
291 TextView mTitle;
Michael Kolba0e2a752010-12-12 15:27:56 -0800292 View mIndicator;
Michael Kolbae62fd42010-08-18 16:33:28 -0700293 View mIncognito;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700294 ImageView mIconView;
295 ImageView mLock;
296 ImageView mClose;
297 boolean mSelected;
298 boolean mInLoad;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800299 Path mPath;
300 int[] mWindowPos;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700301
302 /**
303 * @param context
304 */
305 public TabView(Context context, TabViewData tab) {
306 super(context);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800307 setWillNotDraw(false);
308 mPath = new Path();
309 mWindowPos = new int[2];
Michael Kolba2b2ba82010-08-04 17:54:03 -0700310 mTabData = tab;
Michael Kolbed217742010-08-10 17:52:34 -0700311 setGravity(Gravity.CENTER_VERTICAL);
312 setOrientation(LinearLayout.HORIZONTAL);
Michael Kolba0e2a752010-12-12 15:27:56 -0800313 setPadding(mTabPadding, 0, 0, 0);
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100314 LayoutInflater inflater = LayoutInflater.from(getContext());
Michael Kolbed217742010-08-10 17:52:34 -0700315 mTabContent = inflater.inflate(R.layout.tab_title, this, true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700316 mTitle = (TextView) mTabContent.findViewById(R.id.title);
317 mIconView = (ImageView) mTabContent.findViewById(R.id.favicon);
318 mLock = (ImageView) mTabContent.findViewById(R.id.lock);
319 mClose = (ImageView) mTabContent.findViewById(R.id.close);
320 mClose.setOnClickListener(this);
Michael Kolbae62fd42010-08-18 16:33:28 -0700321 mIncognito = mTabContent.findViewById(R.id.incognito);
Michael Kolba0e2a752010-12-12 15:27:56 -0800322 mIndicator = mTabContent.findViewById(R.id.chevron);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700323 mSelected = false;
324 mInLoad = false;
325 // update the status
326 updateFromData();
327 }
328
Michael Kolba0e2a752010-12-12 15:27:56 -0800329 void showIndicator(boolean show) {
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800330 if (mSelected) {
331 mIndicator.setVisibility(show ? View.VISIBLE : View.GONE);
332 } else {
333 mIndicator.setVisibility(View.GONE);
334 }
Michael Kolba0e2a752010-12-12 15:27:56 -0800335 }
336
Michael Kolba2b2ba82010-08-04 17:54:03 -0700337 @Override
338 public void onClick(View v) {
339 if (v == mClose) {
340 closeTab();
341 }
342 }
343
344 private void updateFromData() {
345 mTabData.mTabView = this;
John Reck30c714c2010-12-16 17:30:34 -0800346 Tab tab = mTabData.mTab;
347 String displayTitle = tab.getTitle();
348 if (displayTitle == null) {
349 displayTitle = tab.getUrl();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700350 }
John Reck30c714c2010-12-16 17:30:34 -0800351 setDisplayTitle(displayTitle);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700352 setProgress(mTabData.mProgress);
353 if (mTabData.mIcon != null) {
354 setFavicon(mTabData.mIcon);
355 }
Michael Kolbae62fd42010-08-18 16:33:28 -0700356 if (mTabData.mTab != null) {
357 mIncognito.setVisibility(
Michael Kolb1bf23132010-11-19 12:55:12 -0800358 mTabData.mTab.isPrivateBrowsingEnabled() ?
Michael Kolbae62fd42010-08-18 16:33:28 -0700359 View.VISIBLE : View.GONE);
360 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700361 }
362
363 @Override
Michael Kolbb7b115e2010-09-25 16:59:37 -0700364 public void setActivated(boolean selected) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700365 mSelected = selected;
366 mClose.setVisibility(mSelected ? View.VISIBLE : View.GONE);
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800367 mIndicator.setVisibility(View.GONE);
Michael Kolb8233fac2010-10-26 16:08:53 -0700368 mTitle.setTextAppearance(mActivity, mSelected ?
Michael Kolbc7485ae2010-09-03 10:10:58 -0700369 R.style.TabTitleSelected : R.style.TabTitleUnselected);
370 setHorizontalFadingEdgeEnabled(!mSelected);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700371 super.setActivated(selected);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800372 LayoutParams lp = (LinearLayout.LayoutParams) getLayoutParams();
373 lp.width = selected ? mTabWidthSelected : mTabWidthUnselected;
374 lp.height = LayoutParams.MATCH_PARENT;
375 setLayoutParams(lp);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700376 }
377
378 void setDisplayTitle(String title) {
379 mTitle.setText(title);
380 }
381
382 void setFavicon(Drawable d) {
383 mIconView.setImageDrawable(d);
384 }
385
386 void setLock(Drawable d) {
387 if (null == d) {
388 mLock.setVisibility(View.GONE);
389 } else {
390 mLock.setImageDrawable(d);
391 mLock.setVisibility(View.VISIBLE);
392 }
393 }
394
Michael Kolba2b2ba82010-08-04 17:54:03 -0700395 void setProgress(int newProgress) {
396 if (newProgress >= PROGRESS_MAX) {
397 mInLoad = false;
398 } else {
399 if (!mInLoad && getWindowToken() != null) {
400 mInLoad = true;
401 }
402 }
403 }
404
405 private void closeTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700406 if (mTabData.mTab == mTabControl.getCurrentTab()) {
407 mUiController.closeCurrentTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700408 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -0700409 mUiController.closeTab(mTabData.mTab);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700410 }
411 }
412
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800413 @Override
414 protected void onLayout(boolean changed, int l, int t, int r, int b) {
415 super.onLayout(changed, l, t, r, b);
416 setTabPath(mPath, 0, 0, r - l, b - t);
417 }
418
419 @Override
420 protected void dispatchDraw(Canvas canvas) {
421 int state = canvas.save();
422 int[] pos = new int[2];
423 getLocationInWindow(mWindowPos);
424 Drawable drawable = mSelected ? mActiveDrawable : mInactiveDrawable;
Michael Kolb376b5412010-12-15 11:52:57 -0800425 drawable.setBounds(0, 0, mUi.getContentWidth(), getHeight());
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800426 drawClipped(canvas, drawable, mPath, mWindowPos[0]);
427 canvas.restoreToCount(state);
428 super.dispatchDraw(canvas);
429 }
430
431 private void drawClipped(Canvas canvas, Drawable drawable,
432 Path clipPath, int left) {
433 mShaderCanvas.drawColor(Color.TRANSPARENT);
434 mShaderCanvas.translate(-left, 0);
435 drawable.draw(mShaderCanvas);
436 canvas.drawPath(clipPath, mShaderPaint);
437 mShaderCanvas.translate(left, 0);
438 }
439
440 private void setTabPath(Path path, int l, int t, int r, int b) {
441 path.reset();
442 path.moveTo(l, b);
443 path.lineTo(l, t);
444 path.lineTo(r - mTabSliceWidth, t);
445 path.lineTo(r, b);
446 path.close();
447 }
448
Michael Kolba2b2ba82010-08-04 17:54:03 -0700449 }
450
451 /**
452 * Store tab state within the title bar
453 */
454 class TabViewData {
455
456 Tab mTab;
457 TabView mTabView;
458 int mProgress;
459 Drawable mIcon;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700460
461 TabViewData(Tab tab) {
462 mTab = tab;
John Reck30c714c2010-12-16 17:30:34 -0800463 setUrlAndTitle(mTab.getUrl(), mTab.getTitle());
Michael Kolba2b2ba82010-08-04 17:54:03 -0700464 }
465
466 void setUrlAndTitle(String url, String title) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700467 if (mTabView != null) {
468 if (title != null) {
469 mTabView.setDisplayTitle(title);
470 } else if (url != null) {
John Reckfb3017f2010-10-26 19:01:24 -0700471 mTabView.setDisplayTitle(UrlUtils.stripUrl(url));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700472 }
473 }
474 }
475
476 void setProgress(int newProgress) {
477 mProgress = newProgress;
478 if (mTabView != null) {
479 mTabView.setProgress(mProgress);
480 }
481 }
482
483 void setFavicon(Bitmap icon) {
484 Drawable[] array = new Drawable[3];
485 array[0] = new PaintDrawable(Color.BLACK);
486 array[1] = new PaintDrawable(Color.WHITE);
487 if (icon == null) {
John Reckaff60fb2010-10-25 13:47:58 -0700488 array[2] = mGenericFavicon;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700489 } else {
490 array[2] = new BitmapDrawable(icon);
491 }
492 LayerDrawable d = new LayerDrawable(array);
493 d.setLayerInset(1, 1, 1, 1, 1);
494 d.setLayerInset(2, 2, 2, 2, 2);
495 mIcon = d;
496 if (mTabView != null) {
497 mTabView.setFavicon(mIcon);
498 }
499 }
500
501 }
502
503 // TabChangeListener implementation
504
Michael Kolb8233fac2010-10-26 16:08:53 -0700505 public void onSetActiveTab(Tab tab) {
506 mTabs.setSelectedTab(mTabControl.getTabIndex(tab));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700507 TabViewData tvd = mTabMap.get(tab);
508 if (tvd != null) {
509 tvd.setProgress(tvd.mProgress);
510 // update the scroll state
511 WebView webview = tab.getWebView();
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800512 if (webview != null) {
513 int h = webview.getVisibleTitleHeight();
514 mVisibleTitleHeight = h -1;
515 onScroll(h);
516 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700517 }
518 }
519
Michael Kolba2b2ba82010-08-04 17:54:03 -0700520 public void onFavicon(Tab tab, Bitmap favicon) {
521 TabViewData tvd = mTabMap.get(tab);
522 if (tvd != null) {
523 tvd.setFavicon(favicon);
524 }
525 }
526
Michael Kolba2b2ba82010-08-04 17:54:03 -0700527 public void onNewTab(Tab tab) {
528 TabViewData tvd = buildTab(tab);
529 buildView(tvd);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700530 }
531
Michael Kolba2b2ba82010-08-04 17:54:03 -0700532 public void onProgress(Tab tab, int progress) {
533 TabViewData tvd = mTabMap.get(tab);
534 if (tvd != null) {
535 tvd.setProgress(progress);
536 }
537 }
538
Michael Kolba2b2ba82010-08-04 17:54:03 -0700539 public void onRemoveTab(Tab tab) {
540 TabViewData tvd = mTabMap.get(tab);
Michael Kolbe2752f72010-10-25 16:50:16 -0700541 if (tvd != null) {
542 TabView tv = tvd.mTabView;
543 if (tv != null) {
544 mTabs.removeTab(tv);
545 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700546 }
547 mTabMap.remove(tab);
548 }
549
Michael Kolba2b2ba82010-08-04 17:54:03 -0700550 public void onUrlAndTitle(Tab tab, String url, String title) {
551 TabViewData tvd = mTabMap.get(tab);
552 if (tvd != null) {
553 tvd.setUrlAndTitle(url, title);
554 }
555 }
556
Michael Kolba2b2ba82010-08-04 17:54:03 -0700557 private boolean isLoading() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700558 TabViewData tvd = mTabMap.get(mTabControl.getCurrentTab());
559 if ((tvd != null) && (tvd.mTabView != null)) {
560 return tvd.mTabView.mInLoad;
561 } else {
562 return false;
563 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700564 }
565
Michael Kolba2b2ba82010-08-04 17:54:03 -0700566}