blob: ba123d074cb5d4f990cade2fa754ddef870f9b3b [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 Kolb467af0a2011-01-11 13:09:49 -080028import android.graphics.Matrix;
Michael Kolb2b5a13a2010-12-09 14:13:42 -080029import 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;
Michael Kolb467af0a2011-01-11 13:09:49 -080036import android.util.Log;
Michael Kolba2b2ba82010-08-04 17:54:03 -070037import android.view.ContextMenu;
Michael Kolbed217742010-08-10 17:52:34 -070038import android.view.Gravity;
Michael Kolba2b2ba82010-08-04 17:54:03 -070039import android.view.LayoutInflater;
40import android.view.MenuInflater;
41import android.view.View;
42import android.view.View.OnClickListener;
43import android.webkit.WebView;
Michael Kolbed217742010-08-10 17:52:34 -070044import android.widget.ImageButton;
Michael Kolba2b2ba82010-08-04 17:54:03 -070045import android.widget.ImageView;
46import android.widget.LinearLayout;
47import android.widget.TextView;
48
Michael Kolba2b2ba82010-08-04 17:54:03 -070049import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -080050import java.util.List;
Michael Kolba2b2ba82010-08-04 17:54:03 -070051import java.util.Map;
52
53/**
54 * tabbed title bar for xlarge screen browser
55 */
56public class TabBar extends LinearLayout
Michael Kolb8233fac2010-10-26 16:08:53 -070057 implements ScrollListener, OnClickListener {
Michael Kolba2b2ba82010-08-04 17:54:03 -070058
59 private static final int PROGRESS_MAX = 100;
60
Michael Kolb8233fac2010-10-26 16:08:53 -070061 private Activity mActivity;
62 private UiController mUiController;
63 private TabControl mTabControl;
Michael Kolb66706532010-12-12 19:50:22 -080064 private XLargeUi mUi;
Michael Kolba2b2ba82010-08-04 17:54:03 -070065
Michael Kolbed217742010-08-10 17:52:34 -070066 private final int mTabWidthSelected;
67 private final int mTabWidthUnselected;
68
Michael Kolba2b2ba82010-08-04 17:54:03 -070069 private TabScrollView mTabs;
Michael Kolb8233fac2010-10-26 16:08:53 -070070
Michael Kolbebba8b42010-09-30 12:57:59 -070071 private ImageButton mNewTab;
72 private int mButtonWidth;
Michael Kolba2b2ba82010-08-04 17:54:03 -070073
Michael Kolb94827b62011-01-08 14:23:45 -080074 private Map<Tab, TabView> mTabMap;
Michael Kolba2b2ba82010-08-04 17:54:03 -070075
Michael Kolba0e2a752010-12-12 15:27:56 -080076 private int mVisibleTitleHeight;
Michael Kolba2b2ba82010-08-04 17:54:03 -070077
John Reckaff60fb2010-10-25 13:47:58 -070078 private Drawable mGenericFavicon;
79
Romain Guyc8373212011-01-07 19:15:16 -080080 private int mCurrentTextureWidth = 0;
81 private int mCurrentTextureHeight = 0;
82
Michael Kolb2b5a13a2010-12-09 14:13:42 -080083 private Drawable mActiveDrawable;
84 private Drawable mInactiveDrawable;
85
Romain Guyc8373212011-01-07 19:15:16 -080086 private final Paint mActiveShaderPaint = new Paint();
87 private final Paint mInactiveShaderPaint = new Paint();
88 private final Matrix mActiveMatrix = new Matrix();
89 private final Matrix mInactiveMatrix = new Matrix();
90
91 private BitmapShader mActiveShader;
92 private BitmapShader mInactiveShader;
93
Michael Kolb2b5a13a2010-12-09 14:13:42 -080094 private int mTabOverlap;
95 private int mTabSliceWidth;
96 private int mTabPadding;
Michael Kolb376b5412010-12-15 11:52:57 -080097 private boolean mUseQuickControls;
Michael Kolb2b5a13a2010-12-09 14:13:42 -080098
Michael Kolb66706532010-12-12 19:50:22 -080099 public TabBar(Activity activity, UiController controller, XLargeUi ui) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700100 super(activity);
101 mActivity = activity;
102 mUiController = controller;
103 mTabControl = mUiController.getTabControl();
104 mUi = ui;
105 Resources res = activity.getResources();
Michael Kolbed217742010-08-10 17:52:34 -0700106 mTabWidthSelected = (int) res.getDimension(R.dimen.tab_width_selected);
107 mTabWidthUnselected = (int) res.getDimension(R.dimen.tab_width_unselected);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800108 mActiveDrawable = res.getDrawable(R.drawable.bg_urlbar);
109 mInactiveDrawable = res.getDrawable(R.drawable.browsertab_inactive);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700110
Michael Kolb94827b62011-01-08 14:23:45 -0800111 mTabMap = new HashMap<Tab, TabView>();
112 Resources resources = activity.getResources();
Michael Kolb8233fac2010-10-26 16:08:53 -0700113 LayoutInflater factory = LayoutInflater.from(activity);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700114 factory.inflate(R.layout.tab_bar, this);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800115 setPadding(12, 12, 0, 0);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700116 mTabs = (TabScrollView) findViewById(R.id.tabs);
Michael Kolbebba8b42010-09-30 12:57:59 -0700117 mNewTab = (ImageButton) findViewById(R.id.newtab);
118 mNewTab.setOnClickListener(this);
John Reckaff60fb2010-10-25 13:47:58 -0700119 mGenericFavicon = res.getDrawable(R.drawable.app_web_browser_sm);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800120 setChildrenDrawingOrderEnabled(true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700121
122 // TODO: Change enabled states based on whether you can go
123 // back/forward. Probably should be done inside onPageStarted.
124
Michael Kolb1bf23132010-11-19 12:55:12 -0800125 updateTabs(mUiController.getTabs());
Michael Kolba2b2ba82010-08-04 17:54:03 -0700126
Michael Kolba0e2a752010-12-12 15:27:56 -0800127 mVisibleTitleHeight = 1;
Michael Kolbebba8b42010-09-30 12:57:59 -0700128 mButtonWidth = -1;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800129 // tab dimensions
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800130 mTabOverlap = (int) res.getDimension(R.dimen.tab_overlap);
131 mTabSliceWidth = (int) res.getDimension(R.dimen.tab_slice);
132 mTabPadding = (int) res.getDimension(R.dimen.tab_padding);
Romain Guyc8373212011-01-07 19:15:16 -0800133
134 mActiveShaderPaint.setStyle(Paint.Style.FILL);
135 mActiveShaderPaint.setAntiAlias(true);
136
137 mInactiveShaderPaint.setStyle(Paint.Style.FILL);
138 mInactiveShaderPaint.setAntiAlias(true);
139
Michael Kolbebba8b42010-09-30 12:57:59 -0700140 }
141
Michael Kolb376b5412010-12-15 11:52:57 -0800142 void setUseQuickControls(boolean useQuickControls) {
143 mUseQuickControls = useQuickControls;
Michael Kolb467af0a2011-01-11 13:09:49 -0800144 mNewTab.setVisibility(mUseQuickControls ? View.GONE
145 : View.VISIBLE);
Michael Kolb376b5412010-12-15 11:52:57 -0800146 }
147
148 int getTabCount() {
149 return mTabMap.size();
150 }
151
Michael Kolb1bf23132010-11-19 12:55:12 -0800152 void updateTabs(List<Tab> tabs) {
153 mTabs.clearTabs();
154 mTabMap.clear();
155 for (Tab tab : tabs) {
Michael Kolb94827b62011-01-08 14:23:45 -0800156 TabView tv = buildTabView(tab);
Michael Kolb1bf23132010-11-19 12:55:12 -0800157 }
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
Michael Kolb467af0a2011-01-11 13:09:49 -0800166 if (!mUseQuickControls) {
167 w -= mTabOverlap;
168 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800169 setMeasuredDimension(w, getMeasuredHeight());
170 }
171
172 @Override
Michael Kolbebba8b42010-09-30 12:57:59 -0700173 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800174 // use paddingLeft and paddingTop
175 int pl = getPaddingLeft();
176 int pt = getPaddingTop();
Michael Kolbebba8b42010-09-30 12:57:59 -0700177 if (mButtonWidth == -1) {
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800178 mButtonWidth = mNewTab.getMeasuredWidth() - mTabOverlap;
Michael Kolbebba8b42010-09-30 12:57:59 -0700179 }
180 int sw = mTabs.getMeasuredWidth();
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800181 int w = right - left - pl;
Michael Kolbebba8b42010-09-30 12:57:59 -0700182 if (w-sw < mButtonWidth) {
183 sw = w - mButtonWidth;
184 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800185 mTabs.layout(pl, pt, pl + sw, bottom - top);
186 // adjust for overlap
187 mNewTab.layout(pl + sw - mTabOverlap, pt,
188 pl + sw + mButtonWidth - mTabOverlap, bottom - top);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700189 }
190
191 public void onClick(View view) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700192 mUi.hideComboView();
Michael Kolbebba8b42010-09-30 12:57:59 -0700193 if (mNewTab == view) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700194 mUiController.openTabToHomePage();
Michael Kolbebba8b42010-09-30 12:57:59 -0700195 } else if (mTabs.getSelectedTab() == view) {
Michael Kolb467af0a2011-01-11 13:09:49 -0800196 if (mUseQuickControls) {
197 if (mUi.isFakeTitleBarShowing() && !isLoading()) {
198 mUi.hideFakeTitleBar();
199 } else {
200 mUi.stopWebViewScrolling();
201 mUi.showFakeTitleBarAndEdit();
202 }
203 } else if (mUi.isFakeTitleBarShowing() && !isLoading()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700204 mUi.hideFakeTitleBar();
Michael Kolbed217742010-08-10 17:52:34 -0700205 } else {
206 showUrlBar();
207 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700208 } else {
Michael Kolbebba8b42010-09-30 12:57:59 -0700209 int ix = mTabs.getChildIndex(view);
210 if (ix >= 0) {
211 mTabs.setSelectedTab(ix);
Michael Kolb8233fac2010-10-26 16:08:53 -0700212 mUiController.switchToTab(ix);
Michael Kolbebba8b42010-09-30 12:57:59 -0700213 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700214 }
215 }
216
Michael Kolbed217742010-08-10 17:52:34 -0700217 private void showUrlBar() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700218 mUi.stopWebViewScrolling();
219 mUi.showFakeTitleBar();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700220 }
221
Michael Kolb376b5412010-12-15 11:52:57 -0800222 void showTitleBarIndicator(boolean show) {
Michael Kolba0e2a752010-12-12 15:27:56 -0800223 Tab tab = mTabControl.getCurrentTab();
224 if (tab != null) {
Michael Kolb94827b62011-01-08 14:23:45 -0800225 TabView tv = mTabMap.get(tab);
226 if (tv != null) {
227 tv.showIndicator(show);
Michael Kolba0e2a752010-12-12 15:27:56 -0800228 }
229 }
Michael Kolbed217742010-08-10 17:52:34 -0700230 }
231
232 // callback after fake titlebar is shown
233 void onShowTitleBar() {
Michael Kolba0e2a752010-12-12 15:27:56 -0800234 showTitleBarIndicator(false);
Michael Kolbed217742010-08-10 17:52:34 -0700235 }
236
237 // callback after fake titlebar is hidden
Michael Kolba2b2ba82010-08-04 17:54:03 -0700238 void onHideTitleBar() {
Michael Kolba0e2a752010-12-12 15:27:56 -0800239 showTitleBarIndicator(mVisibleTitleHeight == 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700240 Tab tab = mTabControl.getCurrentTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700241 tab.getWebView().requestFocus();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700242 }
243
Michael Kolbed217742010-08-10 17:52:34 -0700244 // webview scroll listener
245
246 @Override
Michael Kolba0e2a752010-12-12 15:27:56 -0800247 public void onScroll(int visibleTitleHeight) {
Michael Kolb376b5412010-12-15 11:52:57 -0800248 if (mUseQuickControls) return;
Michael Kolb8233fac2010-10-26 16:08:53 -0700249 // isLoading is using the current tab, which initially might not be set yet
John Reckdcf911c2010-12-22 16:32:31 -0800250 if (mTabControl.getCurrentTab() != null
251 && !isLoading()) {
252 if (visibleTitleHeight == 0) {
253 mUi.hideFakeTitleBar();
254 showTitleBarIndicator(true);
255 } else {
Michael Kolba0e2a752010-12-12 15:27:56 -0800256 showTitleBarIndicator(false);
Michael Kolbed217742010-08-10 17:52:34 -0700257 }
258 }
Michael Kolba0e2a752010-12-12 15:27:56 -0800259 mVisibleTitleHeight = visibleTitleHeight;
Michael Kolbed217742010-08-10 17:52:34 -0700260 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700261
262 @Override
263 public void createContextMenu(ContextMenu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700264 MenuInflater inflater = mActivity.getMenuInflater();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700265 inflater.inflate(R.menu.title_context, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -0700266 mActivity.onCreateContextMenu(menu, this, null);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700267 }
268
Michael Kolb94827b62011-01-08 14:23:45 -0800269 private TabView buildTabView(Tab tab) {
270 TabView tabview = new TabView(mActivity, tab);
271 mTabMap.put(tab, tabview);
272 tabview.setOnClickListener(this);
273 mTabs.addTab(tabview);
274 return tabview;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700275 }
276
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800277 @Override
278 protected int getChildDrawingOrder(int count, int i) {
279 // reverse
280 return count - 1 - i;
281 }
282
Romain Guyc8373212011-01-07 19:15:16 -0800283 private static Bitmap getDrawableAsBitmap(Drawable drawable, int width, int height) {
284 Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
285 Canvas c = new Canvas(b);
286 drawable.setBounds(0, 0, width, height);
287 drawable.draw(c);
288 return b;
289 }
290
Michael Kolba2b2ba82010-08-04 17:54:03 -0700291 /**
292 * View used in the tab bar
293 */
294 class TabView extends LinearLayout implements OnClickListener {
295
Michael Kolb94827b62011-01-08 14:23:45 -0800296 Tab mTab;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700297 View mTabContent;
298 TextView mTitle;
Michael Kolba0e2a752010-12-12 15:27:56 -0800299 View mIndicator;
Michael Kolbae62fd42010-08-18 16:33:28 -0700300 View mIncognito;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700301 ImageView mIconView;
302 ImageView mLock;
303 ImageView mClose;
304 boolean mSelected;
305 boolean mInLoad;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800306 Path mPath;
307 int[] mWindowPos;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700308
309 /**
310 * @param context
311 */
Michael Kolb94827b62011-01-08 14:23:45 -0800312 public TabView(Context context, Tab tab) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700313 super(context);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800314 setWillNotDraw(false);
315 mPath = new Path();
316 mWindowPos = new int[2];
Michael Kolb94827b62011-01-08 14:23:45 -0800317 mTab = tab;
Michael Kolbed217742010-08-10 17:52:34 -0700318 setGravity(Gravity.CENTER_VERTICAL);
319 setOrientation(LinearLayout.HORIZONTAL);
Michael Kolba0e2a752010-12-12 15:27:56 -0800320 setPadding(mTabPadding, 0, 0, 0);
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100321 LayoutInflater inflater = LayoutInflater.from(getContext());
Michael Kolbed217742010-08-10 17:52:34 -0700322 mTabContent = inflater.inflate(R.layout.tab_title, this, true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700323 mTitle = (TextView) mTabContent.findViewById(R.id.title);
324 mIconView = (ImageView) mTabContent.findViewById(R.id.favicon);
325 mLock = (ImageView) mTabContent.findViewById(R.id.lock);
326 mClose = (ImageView) mTabContent.findViewById(R.id.close);
327 mClose.setOnClickListener(this);
Michael Kolbae62fd42010-08-18 16:33:28 -0700328 mIncognito = mTabContent.findViewById(R.id.incognito);
Michael Kolba0e2a752010-12-12 15:27:56 -0800329 mIndicator = mTabContent.findViewById(R.id.chevron);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700330 mSelected = false;
331 mInLoad = false;
332 // update the status
Michael Kolb94827b62011-01-08 14:23:45 -0800333 updateFromTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700334 }
335
Michael Kolba0e2a752010-12-12 15:27:56 -0800336 void showIndicator(boolean show) {
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800337 if (mSelected) {
338 mIndicator.setVisibility(show ? View.VISIBLE : View.GONE);
339 } else {
340 mIndicator.setVisibility(View.GONE);
341 }
Michael Kolba0e2a752010-12-12 15:27:56 -0800342 }
343
Michael Kolba2b2ba82010-08-04 17:54:03 -0700344 @Override
345 public void onClick(View v) {
346 if (v == mClose) {
347 closeTab();
348 }
349 }
350
Michael Kolb94827b62011-01-08 14:23:45 -0800351 private void updateFromTab() {
352 String displayTitle = mTab.getTitle();
John Reck30c714c2010-12-16 17:30:34 -0800353 if (displayTitle == null) {
Michael Kolb94827b62011-01-08 14:23:45 -0800354 displayTitle = mTab.getUrl();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700355 }
John Reck30c714c2010-12-16 17:30:34 -0800356 setDisplayTitle(displayTitle);
Michael Kolb94827b62011-01-08 14:23:45 -0800357 setProgress(mTab.getLoadProgress());
358 if (mTab.getFavicon() != null) {
359 setFavicon(renderFavicon(mTab.getFavicon()));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700360 }
Michael Kolb94827b62011-01-08 14:23:45 -0800361 if (mTab != null) {
Michael Kolbae62fd42010-08-18 16:33:28 -0700362 mIncognito.setVisibility(
Michael Kolb94827b62011-01-08 14:23:45 -0800363 mTab.isPrivateBrowsingEnabled() ?
Michael Kolbae62fd42010-08-18 16:33:28 -0700364 View.VISIBLE : View.GONE);
365 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700366 }
367
368 @Override
Michael Kolbb7b115e2010-09-25 16:59:37 -0700369 public void setActivated(boolean selected) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700370 mSelected = selected;
371 mClose.setVisibility(mSelected ? View.VISIBLE : View.GONE);
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800372 mIndicator.setVisibility(View.GONE);
Michael Kolb8233fac2010-10-26 16:08:53 -0700373 mTitle.setTextAppearance(mActivity, mSelected ?
Michael Kolbc7485ae2010-09-03 10:10:58 -0700374 R.style.TabTitleSelected : R.style.TabTitleUnselected);
375 setHorizontalFadingEdgeEnabled(!mSelected);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700376 super.setActivated(selected);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800377 LayoutParams lp = (LinearLayout.LayoutParams) getLayoutParams();
378 lp.width = selected ? mTabWidthSelected : mTabWidthUnselected;
379 lp.height = LayoutParams.MATCH_PARENT;
380 setLayoutParams(lp);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700381 }
382
383 void setDisplayTitle(String title) {
384 mTitle.setText(title);
385 }
386
387 void setFavicon(Drawable d) {
388 mIconView.setImageDrawable(d);
389 }
390
391 void setLock(Drawable d) {
392 if (null == d) {
393 mLock.setVisibility(View.GONE);
394 } else {
395 mLock.setImageDrawable(d);
396 mLock.setVisibility(View.VISIBLE);
397 }
398 }
399
Michael Kolba2b2ba82010-08-04 17:54:03 -0700400 void setProgress(int newProgress) {
401 if (newProgress >= PROGRESS_MAX) {
402 mInLoad = false;
403 } else {
404 if (!mInLoad && getWindowToken() != null) {
405 mInLoad = true;
406 }
407 }
408 }
409
410 private void closeTab() {
Michael Kolb94827b62011-01-08 14:23:45 -0800411 if (mTab == mTabControl.getCurrentTab()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700412 mUiController.closeCurrentTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700413 } else {
Michael Kolb94827b62011-01-08 14:23:45 -0800414 mUiController.closeTab(mTab);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700415 }
416 }
417
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800418 @Override
419 protected void onLayout(boolean changed, int l, int t, int r, int b) {
420 super.onLayout(changed, l, t, r, b);
421 setTabPath(mPath, 0, 0, r - l, b - t);
422 }
Romain Guyc8373212011-01-07 19:15:16 -0800423
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800424 @Override
425 protected void dispatchDraw(Canvas canvas) {
Romain Guyc8373212011-01-07 19:15:16 -0800426 if (mCurrentTextureWidth != mUi.getContentWidth() ||
427 mCurrentTextureHeight != getHeight()) {
428 mCurrentTextureWidth = mUi.getContentWidth();
429 mCurrentTextureHeight = getHeight();
430
431 if (mCurrentTextureWidth > 0 && mCurrentTextureHeight > 0) {
432 Bitmap activeTexture = getDrawableAsBitmap(mActiveDrawable,
433 mCurrentTextureWidth, mCurrentTextureHeight);
434 Bitmap inactiveTexture = getDrawableAsBitmap(mInactiveDrawable,
435 mCurrentTextureWidth, mCurrentTextureHeight);
436
437 mActiveShader = new BitmapShader(activeTexture,
438 Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
439 mActiveShaderPaint.setShader(mActiveShader);
440
441 mInactiveShader = new BitmapShader(inactiveTexture,
442 Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
443 mInactiveShaderPaint.setShader(mInactiveShader);
444 }
445 }
446
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800447 int state = canvas.save();
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800448 getLocationInWindow(mWindowPos);
Romain Guyc8373212011-01-07 19:15:16 -0800449 Paint paint = mSelected ? mActiveShaderPaint : mInactiveShaderPaint;
450 drawClipped(canvas, paint, mPath, mWindowPos[0]);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800451 canvas.restoreToCount(state);
452 super.dispatchDraw(canvas);
453 }
454
Romain Guyc8373212011-01-07 19:15:16 -0800455 private void drawClipped(Canvas canvas, Paint paint, Path clipPath, int left) {
456 // TODO: We should change the matrix/shader only when needed
457 final Matrix matrix = mSelected ? mActiveMatrix : mInactiveMatrix;
458 matrix.setTranslate(-left, 0.0f);
459 (mSelected ? mActiveShader : mInactiveShader).setLocalMatrix(matrix);
460 canvas.drawPath(clipPath, paint);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800461 }
462
463 private void setTabPath(Path path, int l, int t, int r, int b) {
464 path.reset();
465 path.moveTo(l, b);
466 path.lineTo(l, t);
467 path.lineTo(r - mTabSliceWidth, t);
468 path.lineTo(r, b);
469 path.close();
470 }
471
Michael Kolba2b2ba82010-08-04 17:54:03 -0700472 }
473
Michael Kolb94827b62011-01-08 14:23:45 -0800474 private Drawable renderFavicon(Bitmap icon) {
475 Drawable[] array = new Drawable[3];
476 array[0] = new PaintDrawable(Color.BLACK);
477 array[1] = new PaintDrawable(Color.WHITE);
478 if (icon == null) {
479 array[2] = mGenericFavicon;
480 } else {
481 array[2] = new BitmapDrawable(icon);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700482 }
Michael Kolb94827b62011-01-08 14:23:45 -0800483 LayerDrawable d = new LayerDrawable(array);
484 d.setLayerInset(1, 1, 1, 1, 1);
485 d.setLayerInset(2, 2, 2, 2, 2);
486 return d;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700487 }
488
489 // TabChangeListener implementation
490
Michael Kolb8233fac2010-10-26 16:08:53 -0700491 public void onSetActiveTab(Tab tab) {
492 mTabs.setSelectedTab(mTabControl.getTabIndex(tab));
Michael Kolb94827b62011-01-08 14:23:45 -0800493 TabView tv = mTabMap.get(tab);
494 if (tv != null) {
495 tv.setProgress(tv.mTab.getLoadProgress());
Michael Kolba2b2ba82010-08-04 17:54:03 -0700496 // update the scroll state
497 WebView webview = tab.getWebView();
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800498 if (webview != null) {
499 int h = webview.getVisibleTitleHeight();
500 mVisibleTitleHeight = h -1;
501 onScroll(h);
502 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700503 }
504 }
505
Michael Kolba2b2ba82010-08-04 17:54:03 -0700506 public void onFavicon(Tab tab, Bitmap favicon) {
Michael Kolb94827b62011-01-08 14:23:45 -0800507 TabView tv = mTabMap.get(tab);
508 if (tv != null) {
509 tv.setFavicon(renderFavicon(favicon));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700510 }
511 }
512
Michael Kolba2b2ba82010-08-04 17:54:03 -0700513 public void onNewTab(Tab tab) {
Michael Kolb94827b62011-01-08 14:23:45 -0800514 TabView tv = buildTabView(tab);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700515 }
516
Michael Kolba2b2ba82010-08-04 17:54:03 -0700517 public void onProgress(Tab tab, int progress) {
Michael Kolb94827b62011-01-08 14:23:45 -0800518 TabView tv = mTabMap.get(tab);
519 if (tv != null) {
520 tv.setProgress(progress);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700521 }
522 }
523
Michael Kolba2b2ba82010-08-04 17:54:03 -0700524 public void onRemoveTab(Tab tab) {
Michael Kolb94827b62011-01-08 14:23:45 -0800525 TabView tv = mTabMap.get(tab);
526 if (tv != null) {
527 mTabs.removeTab(tv);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700528 }
529 mTabMap.remove(tab);
530 }
531
Michael Kolba2b2ba82010-08-04 17:54:03 -0700532 public void onUrlAndTitle(Tab tab, String url, String title) {
Michael Kolb94827b62011-01-08 14:23:45 -0800533 TabView tv = mTabMap.get(tab);
534 if (tv != null) {
535 if (title != null) {
536 tv.setDisplayTitle(title);
537 } else if (url != null) {
538 tv.setDisplayTitle(UrlUtils.stripUrl(url));
539 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700540 }
541 }
542
Michael Kolba2b2ba82010-08-04 17:54:03 -0700543 private boolean isLoading() {
Michael Kolb94827b62011-01-08 14:23:45 -0800544 TabView tv = mTabMap.get(mTabControl.getCurrentTab());
545 if (tv != null) {
546 return tv.mInLoad;
Michael Kolb8233fac2010-10-26 16:08:53 -0700547 } else {
548 return false;
549 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700550 }
551
Michael Kolba2b2ba82010-08-04 17:54:03 -0700552}