blob: f1cdf2f94d633eab04dd55d1cd78d63e3f9917ba [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;
36import android.view.ContextMenu;
Michael Kolbed217742010-08-10 17:52:34 -070037import android.view.Gravity;
Michael Kolba2b2ba82010-08-04 17:54:03 -070038import android.view.LayoutInflater;
39import android.view.MenuInflater;
40import android.view.View;
41import android.view.View.OnClickListener;
42import android.webkit.WebView;
Michael Kolbed217742010-08-10 17:52:34 -070043import android.widget.ImageButton;
Michael Kolba2b2ba82010-08-04 17:54:03 -070044import android.widget.ImageView;
45import android.widget.LinearLayout;
46import android.widget.TextView;
47
Michael Kolba2b2ba82010-08-04 17:54:03 -070048import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -080049import java.util.List;
Michael Kolba2b2ba82010-08-04 17:54:03 -070050import java.util.Map;
51
52/**
53 * tabbed title bar for xlarge screen browser
54 */
55public class TabBar extends LinearLayout
Michael Kolb8233fac2010-10-26 16:08:53 -070056 implements ScrollListener, OnClickListener {
Michael Kolba2b2ba82010-08-04 17:54:03 -070057
58 private static final int PROGRESS_MAX = 100;
59
Michael Kolb8233fac2010-10-26 16:08:53 -070060 private Activity mActivity;
61 private UiController mUiController;
62 private TabControl mTabControl;
Michael Kolb66706532010-12-12 19:50:22 -080063 private XLargeUi mUi;
Michael Kolba2b2ba82010-08-04 17:54:03 -070064
Michael Kolbed217742010-08-10 17:52:34 -070065 private final int mTabWidthSelected;
66 private final int mTabWidthUnselected;
67
Michael Kolba2b2ba82010-08-04 17:54:03 -070068 private TabScrollView mTabs;
Michael Kolb8233fac2010-10-26 16:08:53 -070069
Michael Kolbebba8b42010-09-30 12:57:59 -070070 private ImageButton mNewTab;
71 private int mButtonWidth;
Michael Kolba2b2ba82010-08-04 17:54:03 -070072
Michael Kolb94827b62011-01-08 14:23:45 -080073 private Map<Tab, TabView> mTabMap;
Michael Kolba2b2ba82010-08-04 17:54:03 -070074
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
Romain Guyc8373212011-01-07 19:15:16 -080079 private int mCurrentTextureWidth = 0;
80 private int mCurrentTextureHeight = 0;
81
Michael Kolb2b5a13a2010-12-09 14:13:42 -080082 private Drawable mActiveDrawable;
83 private Drawable mInactiveDrawable;
84
Romain Guyc8373212011-01-07 19:15:16 -080085 private final Paint mActiveShaderPaint = new Paint();
86 private final Paint mInactiveShaderPaint = new Paint();
87 private final Matrix mActiveMatrix = new Matrix();
88 private final Matrix mInactiveMatrix = new Matrix();
89
90 private BitmapShader mActiveShader;
91 private BitmapShader mInactiveShader;
92
Michael Kolb2b5a13a2010-12-09 14:13:42 -080093 private int mTabOverlap;
94 private int mTabSliceWidth;
95 private int mTabPadding;
Michael Kolb376b5412010-12-15 11:52:57 -080096 private boolean mUseQuickControls;
Michael Kolb2b5a13a2010-12-09 14:13:42 -080097
Michael Kolb66706532010-12-12 19:50:22 -080098 public TabBar(Activity activity, UiController controller, XLargeUi ui) {
Michael Kolb8233fac2010-10-26 16:08:53 -070099 super(activity);
100 mActivity = activity;
101 mUiController = controller;
102 mTabControl = mUiController.getTabControl();
103 mUi = ui;
104 Resources res = activity.getResources();
Michael Kolbed217742010-08-10 17:52:34 -0700105 mTabWidthSelected = (int) res.getDimension(R.dimen.tab_width_selected);
106 mTabWidthUnselected = (int) res.getDimension(R.dimen.tab_width_unselected);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800107 mActiveDrawable = res.getDrawable(R.drawable.bg_urlbar);
108 mInactiveDrawable = res.getDrawable(R.drawable.browsertab_inactive);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700109
Michael Kolb94827b62011-01-08 14:23:45 -0800110 mTabMap = new HashMap<Tab, TabView>();
111 Resources resources = activity.getResources();
Michael Kolb8233fac2010-10-26 16:08:53 -0700112 LayoutInflater factory = LayoutInflater.from(activity);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700113 factory.inflate(R.layout.tab_bar, this);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800114 setPadding(12, 12, 0, 0);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700115 mTabs = (TabScrollView) findViewById(R.id.tabs);
Michael Kolbebba8b42010-09-30 12:57:59 -0700116 mNewTab = (ImageButton) findViewById(R.id.newtab);
117 mNewTab.setOnClickListener(this);
John Reckaff60fb2010-10-25 13:47:58 -0700118 mGenericFavicon = res.getDrawable(R.drawable.app_web_browser_sm);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800119 setChildrenDrawingOrderEnabled(true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700120
121 // TODO: Change enabled states based on whether you can go
122 // back/forward. Probably should be done inside onPageStarted.
123
Michael Kolb1bf23132010-11-19 12:55:12 -0800124 updateTabs(mUiController.getTabs());
Michael Kolba2b2ba82010-08-04 17:54:03 -0700125
Michael Kolba0e2a752010-12-12 15:27:56 -0800126 mVisibleTitleHeight = 1;
Michael Kolbebba8b42010-09-30 12:57:59 -0700127 mButtonWidth = -1;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800128 // tab dimensions
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800129 mTabOverlap = (int) res.getDimension(R.dimen.tab_overlap);
130 mTabSliceWidth = (int) res.getDimension(R.dimen.tab_slice);
131 mTabPadding = (int) res.getDimension(R.dimen.tab_padding);
Romain Guyc8373212011-01-07 19:15:16 -0800132
133 mActiveShaderPaint.setStyle(Paint.Style.FILL);
134 mActiveShaderPaint.setAntiAlias(true);
135
136 mInactiveShaderPaint.setStyle(Paint.Style.FILL);
137 mInactiveShaderPaint.setAntiAlias(true);
Michael Kolbb4cafc52011-01-12 16:55:04 -0800138
Michael Kolbebba8b42010-09-30 12:57:59 -0700139 }
140
Michael Kolb376b5412010-12-15 11:52:57 -0800141 void setUseQuickControls(boolean useQuickControls) {
142 mUseQuickControls = useQuickControls;
Michael Kolb467af0a2011-01-11 13:09:49 -0800143 mNewTab.setVisibility(mUseQuickControls ? View.GONE
144 : View.VISIBLE);
Michael Kolb376b5412010-12-15 11:52:57 -0800145 }
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) {
Michael Kolb94827b62011-01-08 14:23:45 -0800155 TabView tv = buildTabView(tab);
Michael Kolb1bf23132010-11-19 12:55:12 -0800156 }
157 mTabs.setSelectedTab(mTabControl.getCurrentIndex());
158 }
159
Michael Kolbebba8b42010-09-30 12:57:59 -0700160 @Override
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800161 protected void onMeasure(int hspec, int vspec) {
162 super.onMeasure(hspec, vspec);
163 int w = getMeasuredWidth();
164 // adjust for new tab overlap
Michael Kolb467af0a2011-01-11 13:09:49 -0800165 if (!mUseQuickControls) {
166 w -= mTabOverlap;
167 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800168 setMeasuredDimension(w, getMeasuredHeight());
169 }
170
171 @Override
Michael Kolbebba8b42010-09-30 12:57:59 -0700172 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800173 // use paddingLeft and paddingTop
174 int pl = getPaddingLeft();
175 int pt = getPaddingTop();
Michael Kolbebba8b42010-09-30 12:57:59 -0700176 int sw = mTabs.getMeasuredWidth();
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800177 int w = right - left - pl;
Michael Kolbb4cafc52011-01-12 16:55:04 -0800178 if (mUseQuickControls) {
179 mButtonWidth = 0;
180 } else {
181 mButtonWidth = mNewTab.getMeasuredWidth() - mTabOverlap;
182 if (w-sw < mButtonWidth) {
183 sw = w - mButtonWidth;
184 }
Michael Kolbebba8b42010-09-30 12:57:59 -0700185 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800186 mTabs.layout(pl, pt, pl + sw, bottom - top);
187 // adjust for overlap
Michael Kolbb4cafc52011-01-12 16:55:04 -0800188 if (!mUseQuickControls) {
189 mNewTab.layout(pl + sw - mTabOverlap, pt,
190 pl + sw + mButtonWidth - mTabOverlap, bottom - top);
191 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700192 }
193
194 public void onClick(View view) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700195 mUi.hideComboView();
Michael Kolbebba8b42010-09-30 12:57:59 -0700196 if (mNewTab == view) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700197 mUiController.openTabToHomePage();
Michael Kolbebba8b42010-09-30 12:57:59 -0700198 } else if (mTabs.getSelectedTab() == view) {
Michael Kolb467af0a2011-01-11 13:09:49 -0800199 if (mUseQuickControls) {
200 if (mUi.isFakeTitleBarShowing() && !isLoading()) {
201 mUi.hideFakeTitleBar();
202 } else {
203 mUi.stopWebViewScrolling();
204 mUi.showFakeTitleBarAndEdit();
205 }
206 } else if (mUi.isFakeTitleBarShowing() && !isLoading()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700207 mUi.hideFakeTitleBar();
Michael Kolbed217742010-08-10 17:52:34 -0700208 } else {
209 showUrlBar();
210 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700211 } else {
Michael Kolbebba8b42010-09-30 12:57:59 -0700212 int ix = mTabs.getChildIndex(view);
213 if (ix >= 0) {
214 mTabs.setSelectedTab(ix);
Michael Kolb8233fac2010-10-26 16:08:53 -0700215 mUiController.switchToTab(ix);
Michael Kolbebba8b42010-09-30 12:57:59 -0700216 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700217 }
218 }
219
Michael Kolbed217742010-08-10 17:52:34 -0700220 private void showUrlBar() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700221 mUi.stopWebViewScrolling();
222 mUi.showFakeTitleBar();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700223 }
224
Michael Kolb376b5412010-12-15 11:52:57 -0800225 void showTitleBarIndicator(boolean show) {
Michael Kolba0e2a752010-12-12 15:27:56 -0800226 Tab tab = mTabControl.getCurrentTab();
227 if (tab != null) {
Michael Kolb94827b62011-01-08 14:23:45 -0800228 TabView tv = mTabMap.get(tab);
229 if (tv != null) {
230 tv.showIndicator(show);
Michael Kolba0e2a752010-12-12 15:27:56 -0800231 }
232 }
Michael Kolbed217742010-08-10 17:52:34 -0700233 }
234
235 // callback after fake titlebar is shown
236 void onShowTitleBar() {
Michael Kolba0e2a752010-12-12 15:27:56 -0800237 showTitleBarIndicator(false);
Michael Kolbed217742010-08-10 17:52:34 -0700238 }
239
240 // callback after fake titlebar is hidden
Michael Kolba2b2ba82010-08-04 17:54:03 -0700241 void onHideTitleBar() {
Michael Kolba0e2a752010-12-12 15:27:56 -0800242 showTitleBarIndicator(mVisibleTitleHeight == 0);
Michael Kolb8233fac2010-10-26 16:08:53 -0700243 Tab tab = mTabControl.getCurrentTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700244 tab.getWebView().requestFocus();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700245 }
246
Michael Kolbed217742010-08-10 17:52:34 -0700247 // webview scroll listener
248
249 @Override
Michael Kolba0e2a752010-12-12 15:27:56 -0800250 public void onScroll(int visibleTitleHeight) {
Michael Kolb376b5412010-12-15 11:52:57 -0800251 if (mUseQuickControls) return;
Michael Kolb8233fac2010-10-26 16:08:53 -0700252 // isLoading is using the current tab, which initially might not be set yet
John Reckdcf911c2010-12-22 16:32:31 -0800253 if (mTabControl.getCurrentTab() != null
254 && !isLoading()) {
255 if (visibleTitleHeight == 0) {
256 mUi.hideFakeTitleBar();
257 showTitleBarIndicator(true);
258 } else {
Michael Kolba0e2a752010-12-12 15:27:56 -0800259 showTitleBarIndicator(false);
Michael Kolbed217742010-08-10 17:52:34 -0700260 }
261 }
Michael Kolba0e2a752010-12-12 15:27:56 -0800262 mVisibleTitleHeight = visibleTitleHeight;
Michael Kolbed217742010-08-10 17:52:34 -0700263 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700264
265 @Override
266 public void createContextMenu(ContextMenu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700267 MenuInflater inflater = mActivity.getMenuInflater();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700268 inflater.inflate(R.menu.title_context, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -0700269 mActivity.onCreateContextMenu(menu, this, null);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700270 }
271
Michael Kolb94827b62011-01-08 14:23:45 -0800272 private TabView buildTabView(Tab tab) {
273 TabView tabview = new TabView(mActivity, tab);
274 mTabMap.put(tab, tabview);
275 tabview.setOnClickListener(this);
276 mTabs.addTab(tabview);
277 return tabview;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700278 }
279
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800280 @Override
281 protected int getChildDrawingOrder(int count, int i) {
282 // reverse
283 return count - 1 - i;
284 }
285
Romain Guyc8373212011-01-07 19:15:16 -0800286 private static Bitmap getDrawableAsBitmap(Drawable drawable, int width, int height) {
287 Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
288 Canvas c = new Canvas(b);
289 drawable.setBounds(0, 0, width, height);
290 drawable.draw(c);
291 return b;
Michael Kolbb4cafc52011-01-12 16:55:04 -0800292 }
293
Michael Kolba2b2ba82010-08-04 17:54:03 -0700294 /**
295 * View used in the tab bar
296 */
297 class TabView extends LinearLayout implements OnClickListener {
298
Michael Kolb94827b62011-01-08 14:23:45 -0800299 Tab mTab;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700300 View mTabContent;
301 TextView mTitle;
Michael Kolba0e2a752010-12-12 15:27:56 -0800302 View mIndicator;
Michael Kolbae62fd42010-08-18 16:33:28 -0700303 View mIncognito;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700304 ImageView mIconView;
305 ImageView mLock;
306 ImageView mClose;
307 boolean mSelected;
308 boolean mInLoad;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800309 Path mPath;
310 int[] mWindowPos;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700311
312 /**
313 * @param context
314 */
Michael Kolb94827b62011-01-08 14:23:45 -0800315 public TabView(Context context, Tab tab) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700316 super(context);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800317 setWillNotDraw(false);
318 mPath = new Path();
319 mWindowPos = new int[2];
Michael Kolb94827b62011-01-08 14:23:45 -0800320 mTab = tab;
Michael Kolbed217742010-08-10 17:52:34 -0700321 setGravity(Gravity.CENTER_VERTICAL);
322 setOrientation(LinearLayout.HORIZONTAL);
Michael Kolba0e2a752010-12-12 15:27:56 -0800323 setPadding(mTabPadding, 0, 0, 0);
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100324 LayoutInflater inflater = LayoutInflater.from(getContext());
Michael Kolbed217742010-08-10 17:52:34 -0700325 mTabContent = inflater.inflate(R.layout.tab_title, this, true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700326 mTitle = (TextView) mTabContent.findViewById(R.id.title);
327 mIconView = (ImageView) mTabContent.findViewById(R.id.favicon);
328 mLock = (ImageView) mTabContent.findViewById(R.id.lock);
329 mClose = (ImageView) mTabContent.findViewById(R.id.close);
330 mClose.setOnClickListener(this);
Michael Kolbae62fd42010-08-18 16:33:28 -0700331 mIncognito = mTabContent.findViewById(R.id.incognito);
Michael Kolba0e2a752010-12-12 15:27:56 -0800332 mIndicator = mTabContent.findViewById(R.id.chevron);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700333 mSelected = false;
334 mInLoad = false;
335 // update the status
Michael Kolb94827b62011-01-08 14:23:45 -0800336 updateFromTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700337 }
338
Michael Kolba0e2a752010-12-12 15:27:56 -0800339 void showIndicator(boolean show) {
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800340 if (mSelected) {
341 mIndicator.setVisibility(show ? View.VISIBLE : View.GONE);
342 } else {
343 mIndicator.setVisibility(View.GONE);
344 }
Michael Kolba0e2a752010-12-12 15:27:56 -0800345 }
346
Michael Kolba2b2ba82010-08-04 17:54:03 -0700347 @Override
348 public void onClick(View v) {
349 if (v == mClose) {
350 closeTab();
351 }
352 }
353
Michael Kolb94827b62011-01-08 14:23:45 -0800354 private void updateFromTab() {
355 String displayTitle = mTab.getTitle();
John Reck30c714c2010-12-16 17:30:34 -0800356 if (displayTitle == null) {
Michael Kolb94827b62011-01-08 14:23:45 -0800357 displayTitle = mTab.getUrl();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700358 }
John Reck30c714c2010-12-16 17:30:34 -0800359 setDisplayTitle(displayTitle);
Michael Kolb94827b62011-01-08 14:23:45 -0800360 setProgress(mTab.getLoadProgress());
361 if (mTab.getFavicon() != null) {
362 setFavicon(renderFavicon(mTab.getFavicon()));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700363 }
Michael Kolb94827b62011-01-08 14:23:45 -0800364 if (mTab != null) {
Michael Kolbae62fd42010-08-18 16:33:28 -0700365 mIncognito.setVisibility(
Michael Kolb94827b62011-01-08 14:23:45 -0800366 mTab.isPrivateBrowsingEnabled() ?
Michael Kolbae62fd42010-08-18 16:33:28 -0700367 View.VISIBLE : View.GONE);
368 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700369 }
370
371 @Override
Michael Kolbb7b115e2010-09-25 16:59:37 -0700372 public void setActivated(boolean selected) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700373 mSelected = selected;
374 mClose.setVisibility(mSelected ? View.VISIBLE : View.GONE);
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800375 mIndicator.setVisibility(View.GONE);
Michael Kolb8233fac2010-10-26 16:08:53 -0700376 mTitle.setTextAppearance(mActivity, mSelected ?
Michael Kolbc7485ae2010-09-03 10:10:58 -0700377 R.style.TabTitleSelected : R.style.TabTitleUnselected);
378 setHorizontalFadingEdgeEnabled(!mSelected);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700379 super.setActivated(selected);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800380 LayoutParams lp = (LinearLayout.LayoutParams) getLayoutParams();
381 lp.width = selected ? mTabWidthSelected : mTabWidthUnselected;
382 lp.height = LayoutParams.MATCH_PARENT;
383 setLayoutParams(lp);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700384 }
385
386 void setDisplayTitle(String title) {
387 mTitle.setText(title);
388 }
389
390 void setFavicon(Drawable d) {
391 mIconView.setImageDrawable(d);
392 }
393
394 void setLock(Drawable d) {
395 if (null == d) {
396 mLock.setVisibility(View.GONE);
397 } else {
398 mLock.setImageDrawable(d);
399 mLock.setVisibility(View.VISIBLE);
400 }
401 }
402
Michael Kolba2b2ba82010-08-04 17:54:03 -0700403 void setProgress(int newProgress) {
404 if (newProgress >= PROGRESS_MAX) {
405 mInLoad = false;
406 } else {
407 if (!mInLoad && getWindowToken() != null) {
408 mInLoad = true;
409 }
410 }
411 }
412
413 private void closeTab() {
Michael Kolb94827b62011-01-08 14:23:45 -0800414 if (mTab == mTabControl.getCurrentTab()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700415 mUiController.closeCurrentTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700416 } else {
Michael Kolb94827b62011-01-08 14:23:45 -0800417 mUiController.closeTab(mTab);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700418 }
419 }
420
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800421 @Override
422 protected void onLayout(boolean changed, int l, int t, int r, int b) {
423 super.onLayout(changed, l, t, r, b);
424 setTabPath(mPath, 0, 0, r - l, b - t);
425 }
Michael Kolbb4cafc52011-01-12 16:55:04 -0800426
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800427 @Override
428 protected void dispatchDraw(Canvas canvas) {
Romain Guyc8373212011-01-07 19:15:16 -0800429 if (mCurrentTextureWidth != mUi.getContentWidth() ||
430 mCurrentTextureHeight != getHeight()) {
431 mCurrentTextureWidth = mUi.getContentWidth();
432 mCurrentTextureHeight = getHeight();
433
434 if (mCurrentTextureWidth > 0 && mCurrentTextureHeight > 0) {
435 Bitmap activeTexture = getDrawableAsBitmap(mActiveDrawable,
436 mCurrentTextureWidth, mCurrentTextureHeight);
437 Bitmap inactiveTexture = getDrawableAsBitmap(mInactiveDrawable,
438 mCurrentTextureWidth, mCurrentTextureHeight);
439
440 mActiveShader = new BitmapShader(activeTexture,
441 Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
442 mActiveShaderPaint.setShader(mActiveShader);
Michael Kolbb4cafc52011-01-12 16:55:04 -0800443
Romain Guyc8373212011-01-07 19:15:16 -0800444 mInactiveShader = new BitmapShader(inactiveTexture,
445 Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
446 mInactiveShaderPaint.setShader(mInactiveShader);
447 }
448 }
Michael Kolbb4cafc52011-01-12 16:55:04 -0800449
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800450 int state = canvas.save();
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800451 getLocationInWindow(mWindowPos);
Romain Guyc8373212011-01-07 19:15:16 -0800452 Paint paint = mSelected ? mActiveShaderPaint : mInactiveShaderPaint;
453 drawClipped(canvas, paint, mPath, mWindowPos[0]);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800454 canvas.restoreToCount(state);
455 super.dispatchDraw(canvas);
456 }
457
Romain Guyc8373212011-01-07 19:15:16 -0800458 private void drawClipped(Canvas canvas, Paint paint, Path clipPath, int left) {
459 // TODO: We should change the matrix/shader only when needed
460 final Matrix matrix = mSelected ? mActiveMatrix : mInactiveMatrix;
461 matrix.setTranslate(-left, 0.0f);
462 (mSelected ? mActiveShader : mInactiveShader).setLocalMatrix(matrix);
463 canvas.drawPath(clipPath, paint);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800464 }
465
466 private void setTabPath(Path path, int l, int t, int r, int b) {
467 path.reset();
468 path.moveTo(l, b);
469 path.lineTo(l, t);
470 path.lineTo(r - mTabSliceWidth, t);
471 path.lineTo(r, b);
472 path.close();
473 }
474
Michael Kolba2b2ba82010-08-04 17:54:03 -0700475 }
476
Michael Kolb94827b62011-01-08 14:23:45 -0800477 private Drawable renderFavicon(Bitmap icon) {
478 Drawable[] array = new Drawable[3];
479 array[0] = new PaintDrawable(Color.BLACK);
480 array[1] = new PaintDrawable(Color.WHITE);
481 if (icon == null) {
482 array[2] = mGenericFavicon;
483 } else {
484 array[2] = new BitmapDrawable(icon);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700485 }
Michael Kolb94827b62011-01-08 14:23:45 -0800486 LayerDrawable d = new LayerDrawable(array);
487 d.setLayerInset(1, 1, 1, 1, 1);
488 d.setLayerInset(2, 2, 2, 2, 2);
489 return d;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700490 }
491
492 // TabChangeListener implementation
493
Michael Kolb8233fac2010-10-26 16:08:53 -0700494 public void onSetActiveTab(Tab tab) {
495 mTabs.setSelectedTab(mTabControl.getTabIndex(tab));
Michael Kolb94827b62011-01-08 14:23:45 -0800496 TabView tv = mTabMap.get(tab);
497 if (tv != null) {
498 tv.setProgress(tv.mTab.getLoadProgress());
Michael Kolba2b2ba82010-08-04 17:54:03 -0700499 // update the scroll state
500 WebView webview = tab.getWebView();
Michael Kolb6a85e2a2010-12-13 18:12:46 -0800501 if (webview != null) {
502 int h = webview.getVisibleTitleHeight();
503 mVisibleTitleHeight = h -1;
504 onScroll(h);
505 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700506 }
507 }
508
Michael Kolba2b2ba82010-08-04 17:54:03 -0700509 public void onFavicon(Tab tab, Bitmap favicon) {
Michael Kolb94827b62011-01-08 14:23:45 -0800510 TabView tv = mTabMap.get(tab);
511 if (tv != null) {
512 tv.setFavicon(renderFavicon(favicon));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700513 }
514 }
515
Michael Kolba2b2ba82010-08-04 17:54:03 -0700516 public void onNewTab(Tab tab) {
Michael Kolb94827b62011-01-08 14:23:45 -0800517 TabView tv = buildTabView(tab);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700518 }
519
Michael Kolba2b2ba82010-08-04 17:54:03 -0700520 public void onProgress(Tab tab, int progress) {
Michael Kolb94827b62011-01-08 14:23:45 -0800521 TabView tv = mTabMap.get(tab);
522 if (tv != null) {
523 tv.setProgress(progress);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700524 }
525 }
526
Michael Kolba2b2ba82010-08-04 17:54:03 -0700527 public void onRemoveTab(Tab tab) {
Michael Kolb94827b62011-01-08 14:23:45 -0800528 TabView tv = mTabMap.get(tab);
529 if (tv != null) {
530 mTabs.removeTab(tv);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700531 }
532 mTabMap.remove(tab);
533 }
534
Michael Kolba2b2ba82010-08-04 17:54:03 -0700535 public void onUrlAndTitle(Tab tab, String url, String title) {
Michael Kolb94827b62011-01-08 14:23:45 -0800536 TabView tv = mTabMap.get(tab);
537 if (tv != null) {
538 if (title != null) {
539 tv.setDisplayTitle(title);
540 } else if (url != null) {
541 tv.setDisplayTitle(UrlUtils.stripUrl(url));
542 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700543 }
544 }
545
Michael Kolba2b2ba82010-08-04 17:54:03 -0700546 private boolean isLoading() {
Michael Kolb94827b62011-01-08 14:23:45 -0800547 TabView tv = mTabMap.get(mTabControl.getCurrentTab());
548 if (tv != null) {
549 return tv.mInLoad;
Michael Kolb8233fac2010-10-26 16:08:53 -0700550 } else {
551 return false;
552 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700553 }
554
Michael Kolba2b2ba82010-08-04 17:54:03 -0700555}