blob: a4e039dcef2d812e6396a83151516696cf5c45a2 [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
Bijan Amirzada41242f22014-03-21 12:12:18 -070017package com.android.browser;
Michael Kolba2b2ba82010-08-04 17:54:03 -070018
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.Drawable;
Michael Kolbed217742010-08-10 17:52:34 -070035import android.view.Gravity;
Michael Kolba2b2ba82010-08-04 17:54:03 -070036import android.view.LayoutInflater;
Michael Kolba2b2ba82010-08-04 17:54:03 -070037import android.view.View;
38import android.view.View.OnClickListener;
Michael Kolbed217742010-08-10 17:52:34 -070039import android.widget.ImageButton;
Michael Kolba2b2ba82010-08-04 17:54:03 -070040import android.widget.ImageView;
41import android.widget.LinearLayout;
42import android.widget.TextView;
43
Michael Kolba2b2ba82010-08-04 17:54:03 -070044import java.util.HashMap;
Michael Kolb1bf23132010-11-19 12:55:12 -080045import java.util.List;
Michael Kolba2b2ba82010-08-04 17:54:03 -070046import java.util.Map;
47
Bijan Amirzada41242f22014-03-21 12:12:18 -070048import com.android.browser.R;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080049
Michael Kolba2b2ba82010-08-04 17:54:03 -070050/**
51 * tabbed title bar for xlarge screen browser
52 */
Michael Kolbb14ff2f2011-07-01 15:33:56 -070053public class TabBar extends LinearLayout implements OnClickListener {
Michael Kolba2b2ba82010-08-04 17:54:03 -070054
55 private static final int PROGRESS_MAX = 100;
56
Michael Kolb8233fac2010-10-26 16:08:53 -070057 private Activity mActivity;
58 private UiController mUiController;
59 private TabControl mTabControl;
Michael Kolb66706532010-12-12 19:50:22 -080060 private XLargeUi mUi;
Michael Kolba2b2ba82010-08-04 17:54:03 -070061
John Reck034637c2011-08-11 11:34:44 -070062 private int mTabWidth;
Michael Kolbed217742010-08-10 17:52:34 -070063
Michael Kolba2b2ba82010-08-04 17:54:03 -070064 private TabScrollView mTabs;
Michael Kolb8233fac2010-10-26 16:08:53 -070065
Michael Kolbebba8b42010-09-30 12:57:59 -070066 private ImageButton mNewTab;
67 private int mButtonWidth;
Michael Kolba2b2ba82010-08-04 17:54:03 -070068
Michael Kolb94827b62011-01-08 14:23:45 -080069 private Map<Tab, TabView> mTabMap;
Michael Kolba2b2ba82010-08-04 17:54:03 -070070
Romain Guyc8373212011-01-07 19:15:16 -080071 private int mCurrentTextureWidth = 0;
72 private int mCurrentTextureHeight = 0;
73
Michael Kolb2b5a13a2010-12-09 14:13:42 -080074 private Drawable mActiveDrawable;
75 private Drawable mInactiveDrawable;
76
Romain Guyc8373212011-01-07 19:15:16 -080077 private final Paint mActiveShaderPaint = new Paint();
78 private final Paint mInactiveShaderPaint = new Paint();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080079 private final Paint mFocusPaint = new Paint();
Romain Guyc8373212011-01-07 19:15:16 -080080 private final Matrix mActiveMatrix = new Matrix();
81 private final Matrix mInactiveMatrix = new Matrix();
82
83 private BitmapShader mActiveShader;
84 private BitmapShader mInactiveShader;
85
Michael Kolb2b5a13a2010-12-09 14:13:42 -080086 private int mTabOverlap;
Michael Kolb5a72f182011-01-13 20:35:06 -080087 private int mAddTabOverlap;
Michael Kolb2b5a13a2010-12-09 14:13:42 -080088 private int mTabSliceWidth;
Michael Kolb2b5a13a2010-12-09 14:13:42 -080089
Michael Kolb66706532010-12-12 19:50:22 -080090 public TabBar(Activity activity, UiController controller, XLargeUi ui) {
Michael Kolb8233fac2010-10-26 16:08:53 -070091 super(activity);
92 mActivity = activity;
93 mUiController = controller;
94 mTabControl = mUiController.getTabControl();
95 mUi = ui;
96 Resources res = activity.getResources();
John Reck034637c2011-08-11 11:34:44 -070097 mTabWidth = (int) res.getDimension(R.dimen.tab_width);
Michael Kolb2b5a13a2010-12-09 14:13:42 -080098 mActiveDrawable = res.getDrawable(R.drawable.bg_urlbar);
99 mInactiveDrawable = res.getDrawable(R.drawable.browsertab_inactive);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700100
Michael Kolb94827b62011-01-08 14:23:45 -0800101 mTabMap = new HashMap<Tab, TabView>();
Michael Kolb8233fac2010-10-26 16:08:53 -0700102 LayoutInflater factory = LayoutInflater.from(activity);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700103 factory.inflate(R.layout.tab_bar, this);
Michael Kolbf558f0d2011-01-13 19:24:18 -0800104 setPadding(0, (int) res.getDimension(R.dimen.tab_padding_top), 0, 0);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700105 mTabs = (TabScrollView) findViewById(R.id.tabs);
Michael Kolbebba8b42010-09-30 12:57:59 -0700106 mNewTab = (ImageButton) findViewById(R.id.newtab);
107 mNewTab.setOnClickListener(this);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700108
Michael Kolb1bf23132010-11-19 12:55:12 -0800109 updateTabs(mUiController.getTabs());
Michael Kolbebba8b42010-09-30 12:57:59 -0700110 mButtonWidth = -1;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800111 // tab dimensions
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800112 mTabOverlap = (int) res.getDimension(R.dimen.tab_overlap);
Michael Kolb5a72f182011-01-13 20:35:06 -0800113 mAddTabOverlap = (int) res.getDimension(R.dimen.tab_addoverlap);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800114 mTabSliceWidth = (int) res.getDimension(R.dimen.tab_slice);
Romain Guyc8373212011-01-07 19:15:16 -0800115
116 mActiveShaderPaint.setStyle(Paint.Style.FILL);
117 mActiveShaderPaint.setAntiAlias(true);
118
119 mInactiveShaderPaint.setStyle(Paint.Style.FILL);
120 mInactiveShaderPaint.setAntiAlias(true);
Michael Kolbb4cafc52011-01-12 16:55:04 -0800121
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800122 mFocusPaint.setStyle(Paint.Style.STROKE);
Michael Kolbeb7001c2011-02-16 16:07:33 -0800123 mFocusPaint.setStrokeWidth(res.getDimension(R.dimen.tab_focus_stroke));
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800124 mFocusPaint.setAntiAlias(true);
125 mFocusPaint.setColor(res.getColor(R.color.tabFocusHighlight));
Michael Kolbebba8b42010-09-30 12:57:59 -0700126 }
127
Michael Kolb678afc82011-05-17 14:52:41 -0700128 @Override
129 public void onConfigurationChanged(Configuration config) {
130 super.onConfigurationChanged(config);
131 Resources res = mActivity.getResources();
John Reck034637c2011-08-11 11:34:44 -0700132 mTabWidth = (int) res.getDimension(R.dimen.tab_width);
Michael Kolb678afc82011-05-17 14:52:41 -0700133 // force update of tab bar
134 mTabs.updateLayout();
135 }
136
Michael Kolb376b5412010-12-15 11:52:57 -0800137 int getTabCount() {
138 return mTabMap.size();
139 }
140
Michael Kolb1bf23132010-11-19 12:55:12 -0800141 void updateTabs(List<Tab> tabs) {
142 mTabs.clearTabs();
143 mTabMap.clear();
144 for (Tab tab : tabs) {
Michael Kolb94827b62011-01-08 14:23:45 -0800145 TabView tv = buildTabView(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800146 mTabs.addTab(tv);
Michael Kolb1bf23132010-11-19 12:55:12 -0800147 }
Michael Kolbc831b632011-05-11 09:30:34 -0700148 mTabs.setSelectedTab(mTabControl.getCurrentPosition());
Michael Kolb1bf23132010-11-19 12:55:12 -0800149 }
150
Michael Kolbebba8b42010-09-30 12:57:59 -0700151 @Override
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800152 protected void onMeasure(int hspec, int vspec) {
153 super.onMeasure(hspec, vspec);
154 int w = getMeasuredWidth();
155 // adjust for new tab overlap
Vivek Sekhar3bec6a32014-10-22 17:03:42 -0700156 w -= mAddTabOverlap;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800157 setMeasuredDimension(w, getMeasuredHeight());
158 }
159
160 @Override
Michael Kolbebba8b42010-09-30 12:57:59 -0700161 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800162 // use paddingLeft and paddingTop
163 int pl = getPaddingLeft();
164 int pt = getPaddingTop();
Michael Kolbebba8b42010-09-30 12:57:59 -0700165 int sw = mTabs.getMeasuredWidth();
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800166 int w = right - left - pl;
Vivek Sekhar3bec6a32014-10-22 17:03:42 -0700167 mButtonWidth = mNewTab.getMeasuredWidth() - mAddTabOverlap;
168 if (w-sw < mButtonWidth) {
169 sw = w - mButtonWidth;
Michael Kolbebba8b42010-09-30 12:57:59 -0700170 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800171 mTabs.layout(pl, pt, pl + sw, bottom - top);
172 // adjust for overlap
Vivek Sekhar3bec6a32014-10-22 17:03:42 -0700173 mNewTab.layout(pl + sw - mAddTabOverlap, pt,
174 pl + sw + mButtonWidth - mAddTabOverlap, bottom - top);
175
Michael Kolba2b2ba82010-08-04 17:54:03 -0700176 }
177
178 public void onClick(View view) {
Michael Kolbebba8b42010-09-30 12:57:59 -0700179 if (mNewTab == view) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700180 mUiController.openTabToHomePage();
Michael Kolbebba8b42010-09-30 12:57:59 -0700181 } else if (mTabs.getSelectedTab() == view) {
Vivek Sekhar3bec6a32014-10-22 17:03:42 -0700182 if (mUi.isTitleBarShowing() && !isLoading()) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800183 mUi.stopEditingUrl();
184 mUi.hideTitleBar();
Michael Kolbed217742010-08-10 17:52:34 -0700185 } else {
186 showUrlBar();
187 }
Michael Kolbc831b632011-05-11 09:30:34 -0700188 } else if (view instanceof TabView) {
189 final Tab tab = ((TabView) view).mTab;
Michael Kolbebba8b42010-09-30 12:57:59 -0700190 int ix = mTabs.getChildIndex(view);
191 if (ix >= 0) {
192 mTabs.setSelectedTab(ix);
Michael Kolbc831b632011-05-11 09:30:34 -0700193 mUiController.switchToTab(tab);
Michael Kolbebba8b42010-09-30 12:57:59 -0700194 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700195 }
196 }
197
Michael Kolbed217742010-08-10 17:52:34 -0700198 private void showUrlBar() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700199 mUi.stopWebViewScrolling();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800200 mUi.showTitleBar();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700201 }
202
Michael Kolb94827b62011-01-08 14:23:45 -0800203 private TabView buildTabView(Tab tab) {
204 TabView tabview = new TabView(mActivity, tab);
205 mTabMap.put(tab, tabview);
206 tabview.setOnClickListener(this);
Michael Kolb94827b62011-01-08 14:23:45 -0800207 return tabview;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700208 }
209
Romain Guyc8373212011-01-07 19:15:16 -0800210 private static Bitmap getDrawableAsBitmap(Drawable drawable, int width, int height) {
211 Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
212 Canvas c = new Canvas(b);
213 drawable.setBounds(0, 0, width, height);
214 drawable.draw(c);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -0700215 c.setBitmap(null);
Romain Guyc8373212011-01-07 19:15:16 -0800216 return b;
Michael Kolbb4cafc52011-01-12 16:55:04 -0800217 }
218
Michael Kolba2b2ba82010-08-04 17:54:03 -0700219 /**
220 * View used in the tab bar
221 */
222 class TabView extends LinearLayout implements OnClickListener {
223
Michael Kolb94827b62011-01-08 14:23:45 -0800224 Tab mTab;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700225 View mTabContent;
226 TextView mTitle;
Michael Kolbae62fd42010-08-18 16:33:28 -0700227 View mIncognito;
John Reck541f55a2011-06-07 16:34:43 -0700228 View mSnapshot;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700229 ImageView mIconView;
230 ImageView mLock;
231 ImageView mClose;
232 boolean mSelected;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800233 Path mPath;
Michael Kolbeb7001c2011-02-16 16:07:33 -0800234 Path mFocusPath;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800235 int[] mWindowPos;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700236
237 /**
238 * @param context
239 */
Michael Kolb94827b62011-01-08 14:23:45 -0800240 public TabView(Context context, Tab tab) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700241 super(context);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800242 setWillNotDraw(false);
243 mPath = new Path();
Michael Kolbeb7001c2011-02-16 16:07:33 -0800244 mFocusPath = new Path();
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800245 mWindowPos = new int[2];
Michael Kolb94827b62011-01-08 14:23:45 -0800246 mTab = tab;
Michael Kolbed217742010-08-10 17:52:34 -0700247 setGravity(Gravity.CENTER_VERTICAL);
248 setOrientation(LinearLayout.HORIZONTAL);
Michael Kolb49f15832011-01-25 15:02:27 -0800249 setPadding(mTabOverlap, 0, mTabSliceWidth, 0);
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100250 LayoutInflater inflater = LayoutInflater.from(getContext());
Michael Kolbed217742010-08-10 17:52:34 -0700251 mTabContent = inflater.inflate(R.layout.tab_title, this, true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700252 mTitle = (TextView) mTabContent.findViewById(R.id.title);
253 mIconView = (ImageView) mTabContent.findViewById(R.id.favicon);
254 mLock = (ImageView) mTabContent.findViewById(R.id.lock);
255 mClose = (ImageView) mTabContent.findViewById(R.id.close);
256 mClose.setOnClickListener(this);
Michael Kolbae62fd42010-08-18 16:33:28 -0700257 mIncognito = mTabContent.findViewById(R.id.incognito);
John Reck541f55a2011-06-07 16:34:43 -0700258 mSnapshot = mTabContent.findViewById(R.id.snapshot);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700259 mSelected = false;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700260 // update the status
Michael Kolb94827b62011-01-08 14:23:45 -0800261 updateFromTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700262 }
263
264 @Override
265 public void onClick(View v) {
266 if (v == mClose) {
267 closeTab();
268 }
269 }
270
Michael Kolb94827b62011-01-08 14:23:45 -0800271 private void updateFromTab() {
272 String displayTitle = mTab.getTitle();
John Reck30c714c2010-12-16 17:30:34 -0800273 if (displayTitle == null) {
Michael Kolb94827b62011-01-08 14:23:45 -0800274 displayTitle = mTab.getUrl();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700275 }
John Reck30c714c2010-12-16 17:30:34 -0800276 setDisplayTitle(displayTitle);
Michael Kolb94827b62011-01-08 14:23:45 -0800277 if (mTab.getFavicon() != null) {
John Reck034637c2011-08-11 11:34:44 -0700278 setFavicon(mUi.getFaviconDrawable(mTab.getFavicon()));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700279 }
John Reck541f55a2011-06-07 16:34:43 -0700280 updateTabIcons();
281 }
282
283 private void updateTabIcons() {
284 mIncognito.setVisibility(
285 mTab.isPrivateBrowsingEnabled() ?
286 View.VISIBLE : View.GONE);
287 mSnapshot.setVisibility(mTab.isSnapshot()
288 ? View.VISIBLE : View.GONE);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700289 }
290
291 @Override
Michael Kolbb7b115e2010-09-25 16:59:37 -0700292 public void setActivated(boolean selected) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700293 mSelected = selected;
294 mClose.setVisibility(mSelected ? View.VISIBLE : View.GONE);
John Reck034637c2011-08-11 11:34:44 -0700295 mIconView.setVisibility(mSelected ? View.GONE : View.VISIBLE);
Michael Kolb8233fac2010-10-26 16:08:53 -0700296 mTitle.setTextAppearance(mActivity, mSelected ?
Michael Kolbc7485ae2010-09-03 10:10:58 -0700297 R.style.TabTitleSelected : R.style.TabTitleUnselected);
298 setHorizontalFadingEdgeEnabled(!mSelected);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700299 super.setActivated(selected);
Michael Kolb678afc82011-05-17 14:52:41 -0700300 updateLayoutParams();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800301 setFocusable(!selected);
302 postInvalidate();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700303 }
304
Michael Kolb678afc82011-05-17 14:52:41 -0700305 public void updateLayoutParams() {
306 LayoutParams lp = (LinearLayout.LayoutParams) getLayoutParams();
John Reck034637c2011-08-11 11:34:44 -0700307 lp.width = mTabWidth;
Michael Kolb678afc82011-05-17 14:52:41 -0700308 lp.height = LayoutParams.MATCH_PARENT;
309 setLayoutParams(lp);
310 }
311
Michael Kolba2b2ba82010-08-04 17:54:03 -0700312 void setDisplayTitle(String title) {
313 mTitle.setText(title);
314 }
315
316 void setFavicon(Drawable d) {
317 mIconView.setImageDrawable(d);
318 }
319
320 void setLock(Drawable d) {
321 if (null == d) {
322 mLock.setVisibility(View.GONE);
323 } else {
324 mLock.setImageDrawable(d);
325 mLock.setVisibility(View.VISIBLE);
326 }
327 }
328
Michael Kolba2b2ba82010-08-04 17:54:03 -0700329 private void closeTab() {
Michael Kolb94827b62011-01-08 14:23:45 -0800330 if (mTab == mTabControl.getCurrentTab()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700331 mUiController.closeCurrentTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700332 } else {
Michael Kolb94827b62011-01-08 14:23:45 -0800333 mUiController.closeTab(mTab);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700334 }
335 }
336
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800337 @Override
338 protected void onLayout(boolean changed, int l, int t, int r, int b) {
339 super.onLayout(changed, l, t, r, b);
340 setTabPath(mPath, 0, 0, r - l, b - t);
Michael Kolbeb7001c2011-02-16 16:07:33 -0800341 setFocusPath(mFocusPath, 0, 0, r - l, b - t);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800342 }
Michael Kolbb4cafc52011-01-12 16:55:04 -0800343
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800344 @Override
345 protected void dispatchDraw(Canvas canvas) {
Romain Guyc8373212011-01-07 19:15:16 -0800346 if (mCurrentTextureWidth != mUi.getContentWidth() ||
347 mCurrentTextureHeight != getHeight()) {
348 mCurrentTextureWidth = mUi.getContentWidth();
349 mCurrentTextureHeight = getHeight();
350
351 if (mCurrentTextureWidth > 0 && mCurrentTextureHeight > 0) {
352 Bitmap activeTexture = getDrawableAsBitmap(mActiveDrawable,
353 mCurrentTextureWidth, mCurrentTextureHeight);
354 Bitmap inactiveTexture = getDrawableAsBitmap(mInactiveDrawable,
355 mCurrentTextureWidth, mCurrentTextureHeight);
356
357 mActiveShader = new BitmapShader(activeTexture,
358 Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
359 mActiveShaderPaint.setShader(mActiveShader);
Michael Kolbb4cafc52011-01-12 16:55:04 -0800360
Romain Guyc8373212011-01-07 19:15:16 -0800361 mInactiveShader = new BitmapShader(inactiveTexture,
362 Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
363 mInactiveShaderPaint.setShader(mInactiveShader);
364 }
365 }
Michael Kolb05902aa2011-02-22 17:43:38 -0800366 // add some monkey protection
367 if ((mActiveShader != null) && (mInactiveShader != null)) {
368 int state = canvas.save();
369 getLocationInWindow(mWindowPos);
370 Paint paint = mSelected ? mActiveShaderPaint : mInactiveShaderPaint;
371 drawClipped(canvas, paint, mPath, mWindowPos[0]);
372 canvas.restoreToCount(state);
373 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800374 super.dispatchDraw(canvas);
375 }
376
Romain Guyc8373212011-01-07 19:15:16 -0800377 private void drawClipped(Canvas canvas, Paint paint, Path clipPath, int left) {
378 // TODO: We should change the matrix/shader only when needed
379 final Matrix matrix = mSelected ? mActiveMatrix : mInactiveMatrix;
380 matrix.setTranslate(-left, 0.0f);
381 (mSelected ? mActiveShader : mInactiveShader).setLocalMatrix(matrix);
382 canvas.drawPath(clipPath, paint);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800383 if (isFocused()) {
Michael Kolbeb7001c2011-02-16 16:07:33 -0800384 canvas.drawPath(mFocusPath, mFocusPaint);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800385 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800386 }
387
388 private void setTabPath(Path path, int l, int t, int r, int b) {
389 path.reset();
390 path.moveTo(l, b);
391 path.lineTo(l, t);
392 path.lineTo(r - mTabSliceWidth, t);
393 path.lineTo(r, b);
394 path.close();
395 }
396
Michael Kolbeb7001c2011-02-16 16:07:33 -0800397 private void setFocusPath(Path path, int l, int t, int r, int b) {
398 path.reset();
399 path.moveTo(l, b);
400 path.lineTo(l, t);
401 path.lineTo(r - mTabSliceWidth, t);
402 path.lineTo(r, b);
403 }
404
Michael Kolba2b2ba82010-08-04 17:54:03 -0700405 }
406
Michael Kolb2d59c322011-01-25 13:18:55 -0800407 private void animateTabOut(final Tab tab, final TabView tv) {
408 ObjectAnimator scalex = ObjectAnimator.ofFloat(tv, "scaleX", 1.0f, 0.0f);
409 ObjectAnimator scaley = ObjectAnimator.ofFloat(tv, "scaleY", 1.0f, 0.0f);
Michael Kolb49f15832011-01-25 15:02:27 -0800410 ObjectAnimator alpha = ObjectAnimator.ofFloat(tv, "alpha", 1.0f, 0.0f);
Michael Kolb2d59c322011-01-25 13:18:55 -0800411 AnimatorSet animator = new AnimatorSet();
Michael Kolb49f15832011-01-25 15:02:27 -0800412 animator.playTogether(scalex, scaley, alpha);
Michael Kolb2d59c322011-01-25 13:18:55 -0800413 animator.setDuration(150);
414 animator.addListener(new AnimatorListener() {
415
416 @Override
417 public void onAnimationCancel(Animator animation) {
418 }
419
420 @Override
421 public void onAnimationEnd(Animator animation) {
422 mTabs.removeTab(tv);
423 mTabMap.remove(tab);
Michael Kolb8814d732011-01-26 11:22:30 -0800424 mUi.onRemoveTabCompleted(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800425 }
426
427 @Override
428 public void onAnimationRepeat(Animator animation) {
429 }
430
431 @Override
432 public void onAnimationStart(Animator animation) {
433 }
434
435 });
436 animator.start();
437 }
438
439 private void animateTabIn(final Tab tab, final TabView tv) {
Michael Kolb49f15832011-01-25 15:02:27 -0800440 ObjectAnimator scalex = ObjectAnimator.ofFloat(tv, "scaleX", 0.0f, 1.0f);
Michael Kolb2d59c322011-01-25 13:18:55 -0800441 scalex.setDuration(150);
442 scalex.addListener(new AnimatorListener() {
443
444 @Override
445 public void onAnimationCancel(Animator animation) {
446 }
447
448 @Override
449 public void onAnimationEnd(Animator animation) {
Michael Kolb8814d732011-01-26 11:22:30 -0800450 mUi.onAddTabCompleted(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800451 }
452
453 @Override
454 public void onAnimationRepeat(Animator animation) {
455 }
456
457 @Override
458 public void onAnimationStart(Animator animation) {
459 mTabs.addTab(tv);
460 }
461
462 });
463 scalex.start();
464 }
465
Michael Kolba2b2ba82010-08-04 17:54:03 -0700466 // TabChangeListener implementation
467
Michael Kolb8233fac2010-10-26 16:08:53 -0700468 public void onSetActiveTab(Tab tab) {
Michael Kolbc831b632011-05-11 09:30:34 -0700469 mTabs.setSelectedTab(mTabControl.getTabPosition(tab));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700470 }
471
Michael Kolba2b2ba82010-08-04 17:54:03 -0700472 public void onFavicon(Tab tab, Bitmap favicon) {
Michael Kolb94827b62011-01-08 14:23:45 -0800473 TabView tv = mTabMap.get(tab);
474 if (tv != null) {
John Reck034637c2011-08-11 11:34:44 -0700475 tv.setFavicon(mUi.getFaviconDrawable(favicon));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700476 }
477 }
478
Michael Kolba2b2ba82010-08-04 17:54:03 -0700479 public void onNewTab(Tab tab) {
Michael Kolb94827b62011-01-08 14:23:45 -0800480 TabView tv = buildTabView(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800481 animateTabIn(tab, tv);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700482 }
483
Michael Kolba2b2ba82010-08-04 17:54:03 -0700484 public void onRemoveTab(Tab tab) {
Michael Kolb94827b62011-01-08 14:23:45 -0800485 TabView tv = mTabMap.get(tab);
486 if (tv != null) {
Michael Kolb2d59c322011-01-25 13:18:55 -0800487 animateTabOut(tab, tv);
488 } else {
489 mTabMap.remove(tab);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700490 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700491 }
492
Michael Kolba2b2ba82010-08-04 17:54:03 -0700493 public void onUrlAndTitle(Tab tab, String url, String title) {
Michael Kolb94827b62011-01-08 14:23:45 -0800494 TabView tv = mTabMap.get(tab);
495 if (tv != null) {
496 if (title != null) {
497 tv.setDisplayTitle(title);
498 } else if (url != null) {
499 tv.setDisplayTitle(UrlUtils.stripUrl(url));
500 }
John Reck541f55a2011-06-07 16:34:43 -0700501 tv.updateTabIcons();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700502 }
503 }
504
Michael Kolba2b2ba82010-08-04 17:54:03 -0700505 private boolean isLoading() {
Michael Kolb1c3a6d22011-12-15 15:28:49 -0800506 Tab tab = mTabControl.getCurrentTab();
507 if (tab != null) {
508 return tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -0700509 } else {
510 return false;
511 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700512 }
513
Michael Kolba2b2ba82010-08-04 17:54:03 -0700514}