blob: 2b4dd63c36303211be55252e044539392c48d5c8 [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 Kolb2d59c322011-01-25 13:18:55 -080019import android.animation.Animator;
20import android.animation.Animator.AnimatorListener;
21import android.animation.AnimatorSet;
22import android.animation.ObjectAnimator;
Michael Kolb8233fac2010-10-26 16:08:53 -070023import android.app.Activity;
Michael Kolba2b2ba82010-08-04 17:54:03 -070024import android.content.Context;
Michael Kolb678afc82011-05-17 14:52:41 -070025import android.content.res.Configuration;
Michael Kolba2b2ba82010-08-04 17:54:03 -070026import android.content.res.Resources;
27import android.graphics.Bitmap;
Michael Kolb2b5a13a2010-12-09 14:13:42 -080028import android.graphics.BitmapShader;
29import android.graphics.Canvas;
Michael Kolb467af0a2011-01-11 13:09:49 -080030import android.graphics.Matrix;
Michael Kolb2b5a13a2010-12-09 14:13:42 -080031import android.graphics.Paint;
32import android.graphics.Path;
33import android.graphics.Shader;
Michael Kolba2b2ba82010-08-04 17:54:03 -070034import android.graphics.drawable.BitmapDrawable;
35import android.graphics.drawable.Drawable;
36import android.graphics.drawable.LayerDrawable;
37import android.graphics.drawable.PaintDrawable;
38import android.view.ContextMenu;
Michael Kolbed217742010-08-10 17:52:34 -070039import android.view.Gravity;
Michael Kolba2b2ba82010-08-04 17:54:03 -070040import android.view.LayoutInflater;
41import android.view.MenuInflater;
42import android.view.View;
43import android.view.View.OnClickListener;
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 */
Michael Kolbb14ff2f2011-07-01 15:33:56 -070056public class TabBar extends LinearLayout implements 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 Kolb678afc82011-05-17 14:52:41 -070065 private int mTabWidthSelected;
66 private int mTabWidthUnselected;
Michael Kolbed217742010-08-10 17:52:34 -070067
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
John Reckaff60fb2010-10-25 13:47:58 -070075 private Drawable mGenericFavicon;
76
Romain Guyc8373212011-01-07 19:15:16 -080077 private int mCurrentTextureWidth = 0;
78 private int mCurrentTextureHeight = 0;
79
Michael Kolb2b5a13a2010-12-09 14:13:42 -080080 private Drawable mActiveDrawable;
81 private Drawable mInactiveDrawable;
82
Romain Guyc8373212011-01-07 19:15:16 -080083 private final Paint mActiveShaderPaint = new Paint();
84 private final Paint mInactiveShaderPaint = new Paint();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080085 private final Paint mFocusPaint = new Paint();
Romain Guyc8373212011-01-07 19:15:16 -080086 private final Matrix mActiveMatrix = new Matrix();
87 private final Matrix mInactiveMatrix = new Matrix();
88
89 private BitmapShader mActiveShader;
90 private BitmapShader mInactiveShader;
91
Michael Kolb2b5a13a2010-12-09 14:13:42 -080092 private int mTabOverlap;
Michael Kolb5a72f182011-01-13 20:35:06 -080093 private int mAddTabOverlap;
Michael Kolb2b5a13a2010-12-09 14:13:42 -080094 private int mTabSliceWidth;
Michael Kolb376b5412010-12-15 11:52:57 -080095 private boolean mUseQuickControls;
Michael Kolb2b5a13a2010-12-09 14:13:42 -080096
Michael Kolb66706532010-12-12 19:50:22 -080097 public TabBar(Activity activity, UiController controller, XLargeUi ui) {
Michael Kolb8233fac2010-10-26 16:08:53 -070098 super(activity);
99 mActivity = activity;
100 mUiController = controller;
101 mTabControl = mUiController.getTabControl();
102 mUi = ui;
103 Resources res = activity.getResources();
Michael Kolbed217742010-08-10 17:52:34 -0700104 mTabWidthSelected = (int) res.getDimension(R.dimen.tab_width_selected);
105 mTabWidthUnselected = (int) res.getDimension(R.dimen.tab_width_unselected);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800106 mActiveDrawable = res.getDrawable(R.drawable.bg_urlbar);
107 mInactiveDrawable = res.getDrawable(R.drawable.browsertab_inactive);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700108
Michael Kolb94827b62011-01-08 14:23:45 -0800109 mTabMap = new HashMap<Tab, TabView>();
Michael Kolb8233fac2010-10-26 16:08:53 -0700110 LayoutInflater factory = LayoutInflater.from(activity);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700111 factory.inflate(R.layout.tab_bar, this);
Michael Kolbf558f0d2011-01-13 19:24:18 -0800112 setPadding(0, (int) res.getDimension(R.dimen.tab_padding_top), 0, 0);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700113 mTabs = (TabScrollView) findViewById(R.id.tabs);
Michael Kolbebba8b42010-09-30 12:57:59 -0700114 mNewTab = (ImageButton) findViewById(R.id.newtab);
115 mNewTab.setOnClickListener(this);
John Reckaff60fb2010-10-25 13:47:58 -0700116 mGenericFavicon = res.getDrawable(R.drawable.app_web_browser_sm);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700117
Michael Kolb1bf23132010-11-19 12:55:12 -0800118 updateTabs(mUiController.getTabs());
Michael Kolbebba8b42010-09-30 12:57:59 -0700119 mButtonWidth = -1;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800120 // tab dimensions
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800121 mTabOverlap = (int) res.getDimension(R.dimen.tab_overlap);
Michael Kolb5a72f182011-01-13 20:35:06 -0800122 mAddTabOverlap = (int) res.getDimension(R.dimen.tab_addoverlap);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800123 mTabSliceWidth = (int) res.getDimension(R.dimen.tab_slice);
Romain Guyc8373212011-01-07 19:15:16 -0800124
125 mActiveShaderPaint.setStyle(Paint.Style.FILL);
126 mActiveShaderPaint.setAntiAlias(true);
127
128 mInactiveShaderPaint.setStyle(Paint.Style.FILL);
129 mInactiveShaderPaint.setAntiAlias(true);
Michael Kolbb4cafc52011-01-12 16:55:04 -0800130
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800131 mFocusPaint.setStyle(Paint.Style.STROKE);
Michael Kolbeb7001c2011-02-16 16:07:33 -0800132 mFocusPaint.setStrokeWidth(res.getDimension(R.dimen.tab_focus_stroke));
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800133 mFocusPaint.setAntiAlias(true);
134 mFocusPaint.setColor(res.getColor(R.color.tabFocusHighlight));
Michael Kolbebba8b42010-09-30 12:57:59 -0700135 }
136
Michael Kolb678afc82011-05-17 14:52:41 -0700137 @Override
138 public void onConfigurationChanged(Configuration config) {
139 super.onConfigurationChanged(config);
140 Resources res = mActivity.getResources();
141 mTabWidthSelected = (int) res.getDimension(R.dimen.tab_width_selected);
142 mTabWidthUnselected = (int) res.getDimension(R.dimen.tab_width_unselected);
143 // force update of tab bar
144 mTabs.updateLayout();
145 }
146
Michael Kolb376b5412010-12-15 11:52:57 -0800147 void setUseQuickControls(boolean useQuickControls) {
148 mUseQuickControls = useQuickControls;
Michael Kolb467af0a2011-01-11 13:09:49 -0800149 mNewTab.setVisibility(mUseQuickControls ? View.GONE
150 : View.VISIBLE);
Michael Kolb376b5412010-12-15 11:52:57 -0800151 }
152
153 int getTabCount() {
154 return mTabMap.size();
155 }
156
Michael Kolb1bf23132010-11-19 12:55:12 -0800157 void updateTabs(List<Tab> tabs) {
158 mTabs.clearTabs();
159 mTabMap.clear();
160 for (Tab tab : tabs) {
Michael Kolb94827b62011-01-08 14:23:45 -0800161 TabView tv = buildTabView(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800162 mTabs.addTab(tv);
Michael Kolb1bf23132010-11-19 12:55:12 -0800163 }
Michael Kolbc831b632011-05-11 09:30:34 -0700164 mTabs.setSelectedTab(mTabControl.getCurrentPosition());
Michael Kolb1bf23132010-11-19 12:55:12 -0800165 }
166
Michael Kolbebba8b42010-09-30 12:57:59 -0700167 @Override
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800168 protected void onMeasure(int hspec, int vspec) {
169 super.onMeasure(hspec, vspec);
170 int w = getMeasuredWidth();
171 // adjust for new tab overlap
Michael Kolb467af0a2011-01-11 13:09:49 -0800172 if (!mUseQuickControls) {
Michael Kolb5a72f182011-01-13 20:35:06 -0800173 w -= mAddTabOverlap;
Michael Kolb467af0a2011-01-11 13:09:49 -0800174 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800175 setMeasuredDimension(w, getMeasuredHeight());
176 }
177
178 @Override
Michael Kolbebba8b42010-09-30 12:57:59 -0700179 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800180 // use paddingLeft and paddingTop
181 int pl = getPaddingLeft();
182 int pt = getPaddingTop();
Michael Kolbebba8b42010-09-30 12:57:59 -0700183 int sw = mTabs.getMeasuredWidth();
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800184 int w = right - left - pl;
Michael Kolbb4cafc52011-01-12 16:55:04 -0800185 if (mUseQuickControls) {
186 mButtonWidth = 0;
187 } else {
Michael Kolb5a72f182011-01-13 20:35:06 -0800188 mButtonWidth = mNewTab.getMeasuredWidth() - mAddTabOverlap;
Michael Kolbb4cafc52011-01-12 16:55:04 -0800189 if (w-sw < mButtonWidth) {
190 sw = w - mButtonWidth;
191 }
Michael Kolbebba8b42010-09-30 12:57:59 -0700192 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800193 mTabs.layout(pl, pt, pl + sw, bottom - top);
194 // adjust for overlap
Michael Kolbb4cafc52011-01-12 16:55:04 -0800195 if (!mUseQuickControls) {
Michael Kolb5a72f182011-01-13 20:35:06 -0800196 mNewTab.layout(pl + sw - mAddTabOverlap, pt,
197 pl + sw + mButtonWidth - mAddTabOverlap, bottom - top);
Michael Kolbb4cafc52011-01-12 16:55:04 -0800198 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700199 }
200
201 public void onClick(View view) {
Michael Kolbebba8b42010-09-30 12:57:59 -0700202 if (mNewTab == view) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700203 mUiController.openTabToHomePage();
Michael Kolbebba8b42010-09-30 12:57:59 -0700204 } else if (mTabs.getSelectedTab() == view) {
Michael Kolb467af0a2011-01-11 13:09:49 -0800205 if (mUseQuickControls) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800206 if (mUi.isTitleBarShowing() && !isLoading()) {
207 mUi.stopEditingUrl();
208 mUi.hideTitleBar();
Michael Kolb467af0a2011-01-11 13:09:49 -0800209 } else {
210 mUi.stopWebViewScrolling();
Michael Kolb46f987e2011-04-05 10:39:10 -0700211 mUi.editUrl(false);
Michael Kolb467af0a2011-01-11 13:09:49 -0800212 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800213 } else if (mUi.isTitleBarShowing() && !isLoading()) {
214 mUi.stopEditingUrl();
215 mUi.hideTitleBar();
Michael Kolbed217742010-08-10 17:52:34 -0700216 } else {
217 showUrlBar();
218 }
Michael Kolbc831b632011-05-11 09:30:34 -0700219 } else if (view instanceof TabView) {
220 final Tab tab = ((TabView) view).mTab;
Michael Kolbebba8b42010-09-30 12:57:59 -0700221 int ix = mTabs.getChildIndex(view);
222 if (ix >= 0) {
223 mTabs.setSelectedTab(ix);
Michael Kolbc831b632011-05-11 09:30:34 -0700224 mUiController.switchToTab(tab);
Michael Kolbebba8b42010-09-30 12:57:59 -0700225 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700226 }
227 }
228
Michael Kolbed217742010-08-10 17:52:34 -0700229 private void showUrlBar() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700230 mUi.stopWebViewScrolling();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800231 mUi.showTitleBar();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700232 }
233
Michael Kolb94827b62011-01-08 14:23:45 -0800234 private TabView buildTabView(Tab tab) {
235 TabView tabview = new TabView(mActivity, tab);
236 mTabMap.put(tab, tabview);
237 tabview.setOnClickListener(this);
Michael Kolb94827b62011-01-08 14:23:45 -0800238 return tabview;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700239 }
240
Romain Guyc8373212011-01-07 19:15:16 -0800241 private static Bitmap getDrawableAsBitmap(Drawable drawable, int width, int height) {
242 Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
243 Canvas c = new Canvas(b);
244 drawable.setBounds(0, 0, width, height);
245 drawable.draw(c);
246 return b;
Michael Kolbb4cafc52011-01-12 16:55:04 -0800247 }
248
Michael Kolba2b2ba82010-08-04 17:54:03 -0700249 /**
250 * View used in the tab bar
251 */
252 class TabView extends LinearLayout implements OnClickListener {
253
Michael Kolb94827b62011-01-08 14:23:45 -0800254 Tab mTab;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700255 View mTabContent;
256 TextView mTitle;
Michael Kolbae62fd42010-08-18 16:33:28 -0700257 View mIncognito;
John Reck541f55a2011-06-07 16:34:43 -0700258 View mSnapshot;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700259 ImageView mIconView;
260 ImageView mLock;
261 ImageView mClose;
262 boolean mSelected;
263 boolean mInLoad;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800264 Path mPath;
Michael Kolbeb7001c2011-02-16 16:07:33 -0800265 Path mFocusPath;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800266 int[] mWindowPos;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700267
268 /**
269 * @param context
270 */
Michael Kolb94827b62011-01-08 14:23:45 -0800271 public TabView(Context context, Tab tab) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700272 super(context);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800273 setWillNotDraw(false);
274 mPath = new Path();
Michael Kolbeb7001c2011-02-16 16:07:33 -0800275 mFocusPath = new Path();
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800276 mWindowPos = new int[2];
Michael Kolb94827b62011-01-08 14:23:45 -0800277 mTab = tab;
Michael Kolbed217742010-08-10 17:52:34 -0700278 setGravity(Gravity.CENTER_VERTICAL);
279 setOrientation(LinearLayout.HORIZONTAL);
Michael Kolb49f15832011-01-25 15:02:27 -0800280 setPadding(mTabOverlap, 0, mTabSliceWidth, 0);
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100281 LayoutInflater inflater = LayoutInflater.from(getContext());
Michael Kolbed217742010-08-10 17:52:34 -0700282 mTabContent = inflater.inflate(R.layout.tab_title, this, true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700283 mTitle = (TextView) mTabContent.findViewById(R.id.title);
284 mIconView = (ImageView) mTabContent.findViewById(R.id.favicon);
285 mLock = (ImageView) mTabContent.findViewById(R.id.lock);
286 mClose = (ImageView) mTabContent.findViewById(R.id.close);
287 mClose.setOnClickListener(this);
Michael Kolbae62fd42010-08-18 16:33:28 -0700288 mIncognito = mTabContent.findViewById(R.id.incognito);
John Reck541f55a2011-06-07 16:34:43 -0700289 mSnapshot = mTabContent.findViewById(R.id.snapshot);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700290 mSelected = false;
291 mInLoad = false;
292 // update the status
Michael Kolb94827b62011-01-08 14:23:45 -0800293 updateFromTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700294 }
295
296 @Override
297 public void onClick(View v) {
298 if (v == mClose) {
299 closeTab();
300 }
301 }
302
Michael Kolb94827b62011-01-08 14:23:45 -0800303 private void updateFromTab() {
304 String displayTitle = mTab.getTitle();
John Reck30c714c2010-12-16 17:30:34 -0800305 if (displayTitle == null) {
Michael Kolb94827b62011-01-08 14:23:45 -0800306 displayTitle = mTab.getUrl();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700307 }
John Reck30c714c2010-12-16 17:30:34 -0800308 setDisplayTitle(displayTitle);
Michael Kolb94827b62011-01-08 14:23:45 -0800309 setProgress(mTab.getLoadProgress());
310 if (mTab.getFavicon() != null) {
311 setFavicon(renderFavicon(mTab.getFavicon()));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700312 }
John Reck541f55a2011-06-07 16:34:43 -0700313 updateTabIcons();
314 }
315
316 private void updateTabIcons() {
317 mIncognito.setVisibility(
318 mTab.isPrivateBrowsingEnabled() ?
319 View.VISIBLE : View.GONE);
320 mSnapshot.setVisibility(mTab.isSnapshot()
321 ? View.VISIBLE : View.GONE);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700322 }
323
324 @Override
Michael Kolbb7b115e2010-09-25 16:59:37 -0700325 public void setActivated(boolean selected) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700326 mSelected = selected;
327 mClose.setVisibility(mSelected ? View.VISIBLE : View.GONE);
Michael Kolb8233fac2010-10-26 16:08:53 -0700328 mTitle.setTextAppearance(mActivity, mSelected ?
Michael Kolbc7485ae2010-09-03 10:10:58 -0700329 R.style.TabTitleSelected : R.style.TabTitleUnselected);
330 setHorizontalFadingEdgeEnabled(!mSelected);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700331 super.setActivated(selected);
Michael Kolb678afc82011-05-17 14:52:41 -0700332 updateLayoutParams();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800333 setFocusable(!selected);
334 postInvalidate();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700335 }
336
Michael Kolb678afc82011-05-17 14:52:41 -0700337 public void updateLayoutParams() {
338 LayoutParams lp = (LinearLayout.LayoutParams) getLayoutParams();
339 lp.width = mSelected ? mTabWidthSelected : mTabWidthUnselected;
340 lp.height = LayoutParams.MATCH_PARENT;
341 setLayoutParams(lp);
342 }
343
Michael Kolba2b2ba82010-08-04 17:54:03 -0700344 void setDisplayTitle(String title) {
345 mTitle.setText(title);
346 }
347
348 void setFavicon(Drawable d) {
349 mIconView.setImageDrawable(d);
350 }
351
352 void setLock(Drawable d) {
353 if (null == d) {
354 mLock.setVisibility(View.GONE);
355 } else {
356 mLock.setImageDrawable(d);
357 mLock.setVisibility(View.VISIBLE);
358 }
359 }
360
Michael Kolba2b2ba82010-08-04 17:54:03 -0700361 void setProgress(int newProgress) {
362 if (newProgress >= PROGRESS_MAX) {
363 mInLoad = false;
364 } else {
365 if (!mInLoad && getWindowToken() != null) {
366 mInLoad = true;
367 }
368 }
369 }
370
371 private void closeTab() {
Michael Kolb94827b62011-01-08 14:23:45 -0800372 if (mTab == mTabControl.getCurrentTab()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700373 mUiController.closeCurrentTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700374 } else {
Michael Kolb94827b62011-01-08 14:23:45 -0800375 mUiController.closeTab(mTab);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700376 }
377 }
378
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800379 @Override
380 protected void onLayout(boolean changed, int l, int t, int r, int b) {
381 super.onLayout(changed, l, t, r, b);
382 setTabPath(mPath, 0, 0, r - l, b - t);
Michael Kolbeb7001c2011-02-16 16:07:33 -0800383 setFocusPath(mFocusPath, 0, 0, r - l, b - t);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800384 }
Michael Kolbb4cafc52011-01-12 16:55:04 -0800385
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800386 @Override
387 protected void dispatchDraw(Canvas canvas) {
Romain Guyc8373212011-01-07 19:15:16 -0800388 if (mCurrentTextureWidth != mUi.getContentWidth() ||
389 mCurrentTextureHeight != getHeight()) {
390 mCurrentTextureWidth = mUi.getContentWidth();
391 mCurrentTextureHeight = getHeight();
392
393 if (mCurrentTextureWidth > 0 && mCurrentTextureHeight > 0) {
394 Bitmap activeTexture = getDrawableAsBitmap(mActiveDrawable,
395 mCurrentTextureWidth, mCurrentTextureHeight);
396 Bitmap inactiveTexture = getDrawableAsBitmap(mInactiveDrawable,
397 mCurrentTextureWidth, mCurrentTextureHeight);
398
399 mActiveShader = new BitmapShader(activeTexture,
400 Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
401 mActiveShaderPaint.setShader(mActiveShader);
Michael Kolbb4cafc52011-01-12 16:55:04 -0800402
Romain Guyc8373212011-01-07 19:15:16 -0800403 mInactiveShader = new BitmapShader(inactiveTexture,
404 Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
405 mInactiveShaderPaint.setShader(mInactiveShader);
406 }
407 }
Michael Kolb05902aa2011-02-22 17:43:38 -0800408 // add some monkey protection
409 if ((mActiveShader != null) && (mInactiveShader != null)) {
410 int state = canvas.save();
411 getLocationInWindow(mWindowPos);
412 Paint paint = mSelected ? mActiveShaderPaint : mInactiveShaderPaint;
413 drawClipped(canvas, paint, mPath, mWindowPos[0]);
414 canvas.restoreToCount(state);
415 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800416 super.dispatchDraw(canvas);
417 }
418
Romain Guyc8373212011-01-07 19:15:16 -0800419 private void drawClipped(Canvas canvas, Paint paint, Path clipPath, int left) {
420 // TODO: We should change the matrix/shader only when needed
421 final Matrix matrix = mSelected ? mActiveMatrix : mInactiveMatrix;
422 matrix.setTranslate(-left, 0.0f);
423 (mSelected ? mActiveShader : mInactiveShader).setLocalMatrix(matrix);
424 canvas.drawPath(clipPath, paint);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800425 if (isFocused()) {
Michael Kolbeb7001c2011-02-16 16:07:33 -0800426 canvas.drawPath(mFocusPath, mFocusPaint);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800427 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800428 }
429
430 private void setTabPath(Path path, int l, int t, int r, int b) {
431 path.reset();
432 path.moveTo(l, b);
433 path.lineTo(l, t);
434 path.lineTo(r - mTabSliceWidth, t);
435 path.lineTo(r, b);
436 path.close();
437 }
438
Michael Kolbeb7001c2011-02-16 16:07:33 -0800439 private void setFocusPath(Path path, int l, int t, int r, int b) {
440 path.reset();
441 path.moveTo(l, b);
442 path.lineTo(l, t);
443 path.lineTo(r - mTabSliceWidth, t);
444 path.lineTo(r, b);
445 }
446
Michael Kolba2b2ba82010-08-04 17:54:03 -0700447 }
448
Michael Kolb49f15832011-01-25 15:02:27 -0800449 static Drawable createFaviconBackground(Context context) {
450 PaintDrawable faviconBackground = new PaintDrawable();
451 Resources res = context.getResources();
452 faviconBackground.getPaint().setColor(context.getResources()
453 .getColor(R.color.tabFaviconBackground));
454 faviconBackground.setCornerRadius(
455 res.getDimension(R.dimen.tab_favicon_corner_radius));
456 return faviconBackground;
457 }
458
Michael Kolb94827b62011-01-08 14:23:45 -0800459 private Drawable renderFavicon(Bitmap icon) {
Michael Kolb49f15832011-01-25 15:02:27 -0800460 Drawable[] array = new Drawable[2];
461 array[0] = createFaviconBackground(getContext());
Michael Kolb94827b62011-01-08 14:23:45 -0800462 if (icon == null) {
Michael Kolb49f15832011-01-25 15:02:27 -0800463 array[1] = mGenericFavicon;
Michael Kolb94827b62011-01-08 14:23:45 -0800464 } else {
Michael Kolb49f15832011-01-25 15:02:27 -0800465 array[1] = new BitmapDrawable(icon);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700466 }
Michael Kolb94827b62011-01-08 14:23:45 -0800467 LayerDrawable d = new LayerDrawable(array);
Michael Kolb49f15832011-01-25 15:02:27 -0800468 d.setLayerInset(1, 2, 2, 2, 2);
Michael Kolb94827b62011-01-08 14:23:45 -0800469 return d;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700470 }
471
Michael Kolb2d59c322011-01-25 13:18:55 -0800472 private void animateTabOut(final Tab tab, final TabView tv) {
473 ObjectAnimator scalex = ObjectAnimator.ofFloat(tv, "scaleX", 1.0f, 0.0f);
474 ObjectAnimator scaley = ObjectAnimator.ofFloat(tv, "scaleY", 1.0f, 0.0f);
Michael Kolb49f15832011-01-25 15:02:27 -0800475 ObjectAnimator alpha = ObjectAnimator.ofFloat(tv, "alpha", 1.0f, 0.0f);
Michael Kolb2d59c322011-01-25 13:18:55 -0800476 AnimatorSet animator = new AnimatorSet();
Michael Kolb49f15832011-01-25 15:02:27 -0800477 animator.playTogether(scalex, scaley, alpha);
Michael Kolb2d59c322011-01-25 13:18:55 -0800478 animator.setDuration(150);
479 animator.addListener(new AnimatorListener() {
480
481 @Override
482 public void onAnimationCancel(Animator animation) {
483 }
484
485 @Override
486 public void onAnimationEnd(Animator animation) {
487 mTabs.removeTab(tv);
488 mTabMap.remove(tab);
Michael Kolb8814d732011-01-26 11:22:30 -0800489 mUi.onRemoveTabCompleted(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800490 }
491
492 @Override
493 public void onAnimationRepeat(Animator animation) {
494 }
495
496 @Override
497 public void onAnimationStart(Animator animation) {
498 }
499
500 });
501 animator.start();
502 }
503
504 private void animateTabIn(final Tab tab, final TabView tv) {
Michael Kolb49f15832011-01-25 15:02:27 -0800505 ObjectAnimator scalex = ObjectAnimator.ofFloat(tv, "scaleX", 0.0f, 1.0f);
Michael Kolb2d59c322011-01-25 13:18:55 -0800506 scalex.setDuration(150);
507 scalex.addListener(new AnimatorListener() {
508
509 @Override
510 public void onAnimationCancel(Animator animation) {
511 }
512
513 @Override
514 public void onAnimationEnd(Animator animation) {
Michael Kolb8814d732011-01-26 11:22:30 -0800515 mUi.onAddTabCompleted(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800516 }
517
518 @Override
519 public void onAnimationRepeat(Animator animation) {
520 }
521
522 @Override
523 public void onAnimationStart(Animator animation) {
524 mTabs.addTab(tv);
525 }
526
527 });
528 scalex.start();
529 }
530
Michael Kolba2b2ba82010-08-04 17:54:03 -0700531 // TabChangeListener implementation
532
Michael Kolb8233fac2010-10-26 16:08:53 -0700533 public void onSetActiveTab(Tab tab) {
Michael Kolbc831b632011-05-11 09:30:34 -0700534 mTabs.setSelectedTab(mTabControl.getTabPosition(tab));
Michael Kolb94827b62011-01-08 14:23:45 -0800535 TabView tv = mTabMap.get(tab);
536 if (tv != null) {
537 tv.setProgress(tv.mTab.getLoadProgress());
Michael Kolba2b2ba82010-08-04 17:54:03 -0700538 }
539 }
540
Michael Kolba2b2ba82010-08-04 17:54:03 -0700541 public void onFavicon(Tab tab, Bitmap favicon) {
Michael Kolb94827b62011-01-08 14:23:45 -0800542 TabView tv = mTabMap.get(tab);
543 if (tv != null) {
544 tv.setFavicon(renderFavicon(favicon));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700545 }
546 }
547
Michael Kolba2b2ba82010-08-04 17:54:03 -0700548 public void onNewTab(Tab tab) {
Michael Kolb94827b62011-01-08 14:23:45 -0800549 TabView tv = buildTabView(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800550 animateTabIn(tab, tv);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700551 }
552
Michael Kolba2b2ba82010-08-04 17:54:03 -0700553 public void onProgress(Tab tab, int progress) {
Michael Kolb94827b62011-01-08 14:23:45 -0800554 TabView tv = mTabMap.get(tab);
555 if (tv != null) {
556 tv.setProgress(progress);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700557 }
558 }
559
Michael Kolba2b2ba82010-08-04 17:54:03 -0700560 public void onRemoveTab(Tab tab) {
Michael Kolb94827b62011-01-08 14:23:45 -0800561 TabView tv = mTabMap.get(tab);
562 if (tv != null) {
Michael Kolb2d59c322011-01-25 13:18:55 -0800563 animateTabOut(tab, tv);
564 } else {
565 mTabMap.remove(tab);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700566 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700567 }
568
Michael Kolba2b2ba82010-08-04 17:54:03 -0700569 public void onUrlAndTitle(Tab tab, String url, String title) {
Michael Kolb94827b62011-01-08 14:23:45 -0800570 TabView tv = mTabMap.get(tab);
571 if (tv != null) {
572 if (title != null) {
573 tv.setDisplayTitle(title);
574 } else if (url != null) {
575 tv.setDisplayTitle(UrlUtils.stripUrl(url));
576 }
John Reck541f55a2011-06-07 16:34:43 -0700577 tv.updateTabIcons();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700578 }
579 }
580
Michael Kolba2b2ba82010-08-04 17:54:03 -0700581 private boolean isLoading() {
Michael Kolb94827b62011-01-08 14:23:45 -0800582 TabView tv = mTabMap.get(mTabControl.getCurrentTab());
583 if (tv != null) {
584 return tv.mInLoad;
Michael Kolb8233fac2010-10-26 16:08:53 -0700585 } else {
586 return false;
587 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700588 }
589
Michael Kolba2b2ba82010-08-04 17:54:03 -0700590}