blob: 4078ba42f3c62a49b7ecfa575c3ff173ba8a80e9 [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 Kolb376b5412010-12-15 11:52:57 -080089 private boolean mUseQuickControls;
Michael Kolb2b5a13a2010-12-09 14:13:42 -080090
Michael Kolb66706532010-12-12 19:50:22 -080091 public TabBar(Activity activity, UiController controller, XLargeUi ui) {
Michael Kolb8233fac2010-10-26 16:08:53 -070092 super(activity);
93 mActivity = activity;
94 mUiController = controller;
95 mTabControl = mUiController.getTabControl();
96 mUi = ui;
97 Resources res = activity.getResources();
John Reck034637c2011-08-11 11:34:44 -070098 mTabWidth = (int) res.getDimension(R.dimen.tab_width);
Michael Kolb2b5a13a2010-12-09 14:13:42 -080099 mActiveDrawable = res.getDrawable(R.drawable.bg_urlbar);
100 mInactiveDrawable = res.getDrawable(R.drawable.browsertab_inactive);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700101
Michael Kolb94827b62011-01-08 14:23:45 -0800102 mTabMap = new HashMap<Tab, TabView>();
Michael Kolb8233fac2010-10-26 16:08:53 -0700103 LayoutInflater factory = LayoutInflater.from(activity);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700104 factory.inflate(R.layout.tab_bar, this);
Michael Kolbf558f0d2011-01-13 19:24:18 -0800105 setPadding(0, (int) res.getDimension(R.dimen.tab_padding_top), 0, 0);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700106 mTabs = (TabScrollView) findViewById(R.id.tabs);
Michael Kolbebba8b42010-09-30 12:57:59 -0700107 mNewTab = (ImageButton) findViewById(R.id.newtab);
108 mNewTab.setOnClickListener(this);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700109
Michael Kolb1bf23132010-11-19 12:55:12 -0800110 updateTabs(mUiController.getTabs());
Michael Kolbebba8b42010-09-30 12:57:59 -0700111 mButtonWidth = -1;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800112 // tab dimensions
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800113 mTabOverlap = (int) res.getDimension(R.dimen.tab_overlap);
Michael Kolb5a72f182011-01-13 20:35:06 -0800114 mAddTabOverlap = (int) res.getDimension(R.dimen.tab_addoverlap);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800115 mTabSliceWidth = (int) res.getDimension(R.dimen.tab_slice);
Romain Guyc8373212011-01-07 19:15:16 -0800116
117 mActiveShaderPaint.setStyle(Paint.Style.FILL);
118 mActiveShaderPaint.setAntiAlias(true);
119
120 mInactiveShaderPaint.setStyle(Paint.Style.FILL);
121 mInactiveShaderPaint.setAntiAlias(true);
Michael Kolbb4cafc52011-01-12 16:55:04 -0800122
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800123 mFocusPaint.setStyle(Paint.Style.STROKE);
Michael Kolbeb7001c2011-02-16 16:07:33 -0800124 mFocusPaint.setStrokeWidth(res.getDimension(R.dimen.tab_focus_stroke));
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800125 mFocusPaint.setAntiAlias(true);
126 mFocusPaint.setColor(res.getColor(R.color.tabFocusHighlight));
Michael Kolbebba8b42010-09-30 12:57:59 -0700127 }
128
Michael Kolb678afc82011-05-17 14:52:41 -0700129 @Override
130 public void onConfigurationChanged(Configuration config) {
131 super.onConfigurationChanged(config);
132 Resources res = mActivity.getResources();
John Reck034637c2011-08-11 11:34:44 -0700133 mTabWidth = (int) res.getDimension(R.dimen.tab_width);
Michael Kolb678afc82011-05-17 14:52:41 -0700134 // force update of tab bar
135 mTabs.updateLayout();
136 }
137
Michael Kolb376b5412010-12-15 11:52:57 -0800138 void setUseQuickControls(boolean useQuickControls) {
139 mUseQuickControls = useQuickControls;
Michael Kolb467af0a2011-01-11 13:09:49 -0800140 mNewTab.setVisibility(mUseQuickControls ? View.GONE
141 : View.VISIBLE);
Michael Kolb376b5412010-12-15 11:52:57 -0800142 }
143
144 int getTabCount() {
145 return mTabMap.size();
146 }
147
Michael Kolb1bf23132010-11-19 12:55:12 -0800148 void updateTabs(List<Tab> tabs) {
149 mTabs.clearTabs();
150 mTabMap.clear();
151 for (Tab tab : tabs) {
Michael Kolb94827b62011-01-08 14:23:45 -0800152 TabView tv = buildTabView(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800153 mTabs.addTab(tv);
Michael Kolb1bf23132010-11-19 12:55:12 -0800154 }
Michael Kolbc831b632011-05-11 09:30:34 -0700155 mTabs.setSelectedTab(mTabControl.getCurrentPosition());
Michael Kolb1bf23132010-11-19 12:55:12 -0800156 }
157
Michael Kolbebba8b42010-09-30 12:57:59 -0700158 @Override
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800159 protected void onMeasure(int hspec, int vspec) {
160 super.onMeasure(hspec, vspec);
161 int w = getMeasuredWidth();
162 // adjust for new tab overlap
Michael Kolb467af0a2011-01-11 13:09:49 -0800163 if (!mUseQuickControls) {
Michael Kolb5a72f182011-01-13 20:35:06 -0800164 w -= mAddTabOverlap;
Michael Kolb467af0a2011-01-11 13:09:49 -0800165 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800166 setMeasuredDimension(w, getMeasuredHeight());
167 }
168
169 @Override
Michael Kolbebba8b42010-09-30 12:57:59 -0700170 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800171 // use paddingLeft and paddingTop
172 int pl = getPaddingLeft();
173 int pt = getPaddingTop();
Michael Kolbebba8b42010-09-30 12:57:59 -0700174 int sw = mTabs.getMeasuredWidth();
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800175 int w = right - left - pl;
Michael Kolbb4cafc52011-01-12 16:55:04 -0800176 if (mUseQuickControls) {
177 mButtonWidth = 0;
178 } else {
Michael Kolb5a72f182011-01-13 20:35:06 -0800179 mButtonWidth = mNewTab.getMeasuredWidth() - mAddTabOverlap;
Michael Kolbb4cafc52011-01-12 16:55:04 -0800180 if (w-sw < mButtonWidth) {
181 sw = w - mButtonWidth;
182 }
Michael Kolbebba8b42010-09-30 12:57:59 -0700183 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800184 mTabs.layout(pl, pt, pl + sw, bottom - top);
185 // adjust for overlap
Michael Kolbb4cafc52011-01-12 16:55:04 -0800186 if (!mUseQuickControls) {
Michael Kolb5a72f182011-01-13 20:35:06 -0800187 mNewTab.layout(pl + sw - mAddTabOverlap, pt,
188 pl + sw + mButtonWidth - mAddTabOverlap, bottom - top);
Michael Kolbb4cafc52011-01-12 16:55:04 -0800189 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700190 }
191
192 public void onClick(View view) {
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) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800197 if (mUi.isTitleBarShowing() && !isLoading()) {
198 mUi.stopEditingUrl();
199 mUi.hideTitleBar();
Michael Kolb467af0a2011-01-11 13:09:49 -0800200 } else {
201 mUi.stopWebViewScrolling();
Michael Kolb1f9b3562012-04-24 14:38:34 -0700202 mUi.editUrl(false, false);
Michael Kolb467af0a2011-01-11 13:09:49 -0800203 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800204 } else if (mUi.isTitleBarShowing() && !isLoading()) {
205 mUi.stopEditingUrl();
206 mUi.hideTitleBar();
Michael Kolbed217742010-08-10 17:52:34 -0700207 } else {
208 showUrlBar();
209 }
Michael Kolbc831b632011-05-11 09:30:34 -0700210 } else if (view instanceof TabView) {
211 final Tab tab = ((TabView) view).mTab;
Michael Kolbebba8b42010-09-30 12:57:59 -0700212 int ix = mTabs.getChildIndex(view);
213 if (ix >= 0) {
214 mTabs.setSelectedTab(ix);
Michael Kolbc831b632011-05-11 09:30:34 -0700215 mUiController.switchToTab(tab);
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();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800222 mUi.showTitleBar();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700223 }
224
Michael Kolb94827b62011-01-08 14:23:45 -0800225 private TabView buildTabView(Tab tab) {
226 TabView tabview = new TabView(mActivity, tab);
227 mTabMap.put(tab, tabview);
228 tabview.setOnClickListener(this);
Michael Kolb94827b62011-01-08 14:23:45 -0800229 return tabview;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700230 }
231
Romain Guyc8373212011-01-07 19:15:16 -0800232 private static Bitmap getDrawableAsBitmap(Drawable drawable, int width, int height) {
233 Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
234 Canvas c = new Canvas(b);
235 drawable.setBounds(0, 0, width, height);
236 drawable.draw(c);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -0700237 c.setBitmap(null);
Romain Guyc8373212011-01-07 19:15:16 -0800238 return b;
Michael Kolbb4cafc52011-01-12 16:55:04 -0800239 }
240
Michael Kolba2b2ba82010-08-04 17:54:03 -0700241 /**
242 * View used in the tab bar
243 */
244 class TabView extends LinearLayout implements OnClickListener {
245
Michael Kolb94827b62011-01-08 14:23:45 -0800246 Tab mTab;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700247 View mTabContent;
248 TextView mTitle;
Michael Kolbae62fd42010-08-18 16:33:28 -0700249 View mIncognito;
John Reck541f55a2011-06-07 16:34:43 -0700250 View mSnapshot;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700251 ImageView mIconView;
252 ImageView mLock;
253 ImageView mClose;
254 boolean mSelected;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800255 Path mPath;
Michael Kolbeb7001c2011-02-16 16:07:33 -0800256 Path mFocusPath;
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800257 int[] mWindowPos;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700258
259 /**
260 * @param context
261 */
Michael Kolb94827b62011-01-08 14:23:45 -0800262 public TabView(Context context, Tab tab) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700263 super(context);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800264 setWillNotDraw(false);
265 mPath = new Path();
Michael Kolbeb7001c2011-02-16 16:07:33 -0800266 mFocusPath = new Path();
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800267 mWindowPos = new int[2];
Michael Kolb94827b62011-01-08 14:23:45 -0800268 mTab = tab;
Michael Kolbed217742010-08-10 17:52:34 -0700269 setGravity(Gravity.CENTER_VERTICAL);
270 setOrientation(LinearLayout.HORIZONTAL);
Michael Kolb49f15832011-01-25 15:02:27 -0800271 setPadding(mTabOverlap, 0, mTabSliceWidth, 0);
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100272 LayoutInflater inflater = LayoutInflater.from(getContext());
Michael Kolbed217742010-08-10 17:52:34 -0700273 mTabContent = inflater.inflate(R.layout.tab_title, this, true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700274 mTitle = (TextView) mTabContent.findViewById(R.id.title);
275 mIconView = (ImageView) mTabContent.findViewById(R.id.favicon);
276 mLock = (ImageView) mTabContent.findViewById(R.id.lock);
277 mClose = (ImageView) mTabContent.findViewById(R.id.close);
278 mClose.setOnClickListener(this);
Michael Kolbae62fd42010-08-18 16:33:28 -0700279 mIncognito = mTabContent.findViewById(R.id.incognito);
John Reck541f55a2011-06-07 16:34:43 -0700280 mSnapshot = mTabContent.findViewById(R.id.snapshot);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700281 mSelected = false;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700282 // update the status
Michael Kolb94827b62011-01-08 14:23:45 -0800283 updateFromTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700284 }
285
286 @Override
287 public void onClick(View v) {
288 if (v == mClose) {
289 closeTab();
290 }
291 }
292
Michael Kolb94827b62011-01-08 14:23:45 -0800293 private void updateFromTab() {
294 String displayTitle = mTab.getTitle();
John Reck30c714c2010-12-16 17:30:34 -0800295 if (displayTitle == null) {
Michael Kolb94827b62011-01-08 14:23:45 -0800296 displayTitle = mTab.getUrl();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700297 }
John Reck30c714c2010-12-16 17:30:34 -0800298 setDisplayTitle(displayTitle);
Michael Kolb94827b62011-01-08 14:23:45 -0800299 if (mTab.getFavicon() != null) {
John Reck034637c2011-08-11 11:34:44 -0700300 setFavicon(mUi.getFaviconDrawable(mTab.getFavicon()));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700301 }
John Reck541f55a2011-06-07 16:34:43 -0700302 updateTabIcons();
303 }
304
305 private void updateTabIcons() {
306 mIncognito.setVisibility(
307 mTab.isPrivateBrowsingEnabled() ?
308 View.VISIBLE : View.GONE);
309 mSnapshot.setVisibility(mTab.isSnapshot()
310 ? View.VISIBLE : View.GONE);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700311 }
312
313 @Override
Michael Kolbb7b115e2010-09-25 16:59:37 -0700314 public void setActivated(boolean selected) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700315 mSelected = selected;
316 mClose.setVisibility(mSelected ? View.VISIBLE : View.GONE);
John Reck034637c2011-08-11 11:34:44 -0700317 mIconView.setVisibility(mSelected ? View.GONE : View.VISIBLE);
Michael Kolb8233fac2010-10-26 16:08:53 -0700318 mTitle.setTextAppearance(mActivity, mSelected ?
Michael Kolbc7485ae2010-09-03 10:10:58 -0700319 R.style.TabTitleSelected : R.style.TabTitleUnselected);
320 setHorizontalFadingEdgeEnabled(!mSelected);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700321 super.setActivated(selected);
Michael Kolb678afc82011-05-17 14:52:41 -0700322 updateLayoutParams();
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800323 setFocusable(!selected);
324 postInvalidate();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700325 }
326
Michael Kolb678afc82011-05-17 14:52:41 -0700327 public void updateLayoutParams() {
328 LayoutParams lp = (LinearLayout.LayoutParams) getLayoutParams();
John Reck034637c2011-08-11 11:34:44 -0700329 lp.width = mTabWidth;
Michael Kolb678afc82011-05-17 14:52:41 -0700330 lp.height = LayoutParams.MATCH_PARENT;
331 setLayoutParams(lp);
332 }
333
Michael Kolba2b2ba82010-08-04 17:54:03 -0700334 void setDisplayTitle(String title) {
335 mTitle.setText(title);
336 }
337
338 void setFavicon(Drawable d) {
339 mIconView.setImageDrawable(d);
340 }
341
342 void setLock(Drawable d) {
343 if (null == d) {
344 mLock.setVisibility(View.GONE);
345 } else {
346 mLock.setImageDrawable(d);
347 mLock.setVisibility(View.VISIBLE);
348 }
349 }
350
Michael Kolba2b2ba82010-08-04 17:54:03 -0700351 private void closeTab() {
Michael Kolb94827b62011-01-08 14:23:45 -0800352 if (mTab == mTabControl.getCurrentTab()) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700353 mUiController.closeCurrentTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700354 } else {
Michael Kolb94827b62011-01-08 14:23:45 -0800355 mUiController.closeTab(mTab);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700356 }
357 }
358
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800359 @Override
360 protected void onLayout(boolean changed, int l, int t, int r, int b) {
361 super.onLayout(changed, l, t, r, b);
362 setTabPath(mPath, 0, 0, r - l, b - t);
Michael Kolbeb7001c2011-02-16 16:07:33 -0800363 setFocusPath(mFocusPath, 0, 0, r - l, b - t);
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800364 }
Michael Kolbb4cafc52011-01-12 16:55:04 -0800365
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800366 @Override
367 protected void dispatchDraw(Canvas canvas) {
Romain Guyc8373212011-01-07 19:15:16 -0800368 if (mCurrentTextureWidth != mUi.getContentWidth() ||
369 mCurrentTextureHeight != getHeight()) {
370 mCurrentTextureWidth = mUi.getContentWidth();
371 mCurrentTextureHeight = getHeight();
372
373 if (mCurrentTextureWidth > 0 && mCurrentTextureHeight > 0) {
374 Bitmap activeTexture = getDrawableAsBitmap(mActiveDrawable,
375 mCurrentTextureWidth, mCurrentTextureHeight);
376 Bitmap inactiveTexture = getDrawableAsBitmap(mInactiveDrawable,
377 mCurrentTextureWidth, mCurrentTextureHeight);
378
379 mActiveShader = new BitmapShader(activeTexture,
380 Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
381 mActiveShaderPaint.setShader(mActiveShader);
Michael Kolbb4cafc52011-01-12 16:55:04 -0800382
Romain Guyc8373212011-01-07 19:15:16 -0800383 mInactiveShader = new BitmapShader(inactiveTexture,
384 Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
385 mInactiveShaderPaint.setShader(mInactiveShader);
386 }
387 }
Michael Kolb05902aa2011-02-22 17:43:38 -0800388 // add some monkey protection
389 if ((mActiveShader != null) && (mInactiveShader != null)) {
390 int state = canvas.save();
391 getLocationInWindow(mWindowPos);
392 Paint paint = mSelected ? mActiveShaderPaint : mInactiveShaderPaint;
393 drawClipped(canvas, paint, mPath, mWindowPos[0]);
394 canvas.restoreToCount(state);
395 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800396 super.dispatchDraw(canvas);
397 }
398
Romain Guyc8373212011-01-07 19:15:16 -0800399 private void drawClipped(Canvas canvas, Paint paint, Path clipPath, int left) {
400 // TODO: We should change the matrix/shader only when needed
401 final Matrix matrix = mSelected ? mActiveMatrix : mInactiveMatrix;
402 matrix.setTranslate(-left, 0.0f);
403 (mSelected ? mActiveShader : mInactiveShader).setLocalMatrix(matrix);
404 canvas.drawPath(clipPath, paint);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800405 if (isFocused()) {
Michael Kolbeb7001c2011-02-16 16:07:33 -0800406 canvas.drawPath(mFocusPath, mFocusPaint);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800407 }
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800408 }
409
410 private void setTabPath(Path path, int l, int t, int r, int b) {
411 path.reset();
412 path.moveTo(l, b);
413 path.lineTo(l, t);
414 path.lineTo(r - mTabSliceWidth, t);
415 path.lineTo(r, b);
416 path.close();
417 }
418
Michael Kolbeb7001c2011-02-16 16:07:33 -0800419 private void setFocusPath(Path path, int l, int t, int r, int b) {
420 path.reset();
421 path.moveTo(l, b);
422 path.lineTo(l, t);
423 path.lineTo(r - mTabSliceWidth, t);
424 path.lineTo(r, b);
425 }
426
Michael Kolba2b2ba82010-08-04 17:54:03 -0700427 }
428
Michael Kolb2d59c322011-01-25 13:18:55 -0800429 private void animateTabOut(final Tab tab, final TabView tv) {
430 ObjectAnimator scalex = ObjectAnimator.ofFloat(tv, "scaleX", 1.0f, 0.0f);
431 ObjectAnimator scaley = ObjectAnimator.ofFloat(tv, "scaleY", 1.0f, 0.0f);
Michael Kolb49f15832011-01-25 15:02:27 -0800432 ObjectAnimator alpha = ObjectAnimator.ofFloat(tv, "alpha", 1.0f, 0.0f);
Michael Kolb2d59c322011-01-25 13:18:55 -0800433 AnimatorSet animator = new AnimatorSet();
Michael Kolb49f15832011-01-25 15:02:27 -0800434 animator.playTogether(scalex, scaley, alpha);
Michael Kolb2d59c322011-01-25 13:18:55 -0800435 animator.setDuration(150);
436 animator.addListener(new AnimatorListener() {
437
438 @Override
439 public void onAnimationCancel(Animator animation) {
440 }
441
442 @Override
443 public void onAnimationEnd(Animator animation) {
444 mTabs.removeTab(tv);
445 mTabMap.remove(tab);
Michael Kolb8814d732011-01-26 11:22:30 -0800446 mUi.onRemoveTabCompleted(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800447 }
448
449 @Override
450 public void onAnimationRepeat(Animator animation) {
451 }
452
453 @Override
454 public void onAnimationStart(Animator animation) {
455 }
456
457 });
458 animator.start();
459 }
460
461 private void animateTabIn(final Tab tab, final TabView tv) {
Michael Kolb49f15832011-01-25 15:02:27 -0800462 ObjectAnimator scalex = ObjectAnimator.ofFloat(tv, "scaleX", 0.0f, 1.0f);
Michael Kolb2d59c322011-01-25 13:18:55 -0800463 scalex.setDuration(150);
464 scalex.addListener(new AnimatorListener() {
465
466 @Override
467 public void onAnimationCancel(Animator animation) {
468 }
469
470 @Override
471 public void onAnimationEnd(Animator animation) {
Michael Kolb8814d732011-01-26 11:22:30 -0800472 mUi.onAddTabCompleted(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800473 }
474
475 @Override
476 public void onAnimationRepeat(Animator animation) {
477 }
478
479 @Override
480 public void onAnimationStart(Animator animation) {
481 mTabs.addTab(tv);
482 }
483
484 });
485 scalex.start();
486 }
487
Michael Kolba2b2ba82010-08-04 17:54:03 -0700488 // TabChangeListener implementation
489
Michael Kolb8233fac2010-10-26 16:08:53 -0700490 public void onSetActiveTab(Tab tab) {
Michael Kolbc831b632011-05-11 09:30:34 -0700491 mTabs.setSelectedTab(mTabControl.getTabPosition(tab));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700492 }
493
Michael Kolba2b2ba82010-08-04 17:54:03 -0700494 public void onFavicon(Tab tab, Bitmap favicon) {
Michael Kolb94827b62011-01-08 14:23:45 -0800495 TabView tv = mTabMap.get(tab);
496 if (tv != null) {
John Reck034637c2011-08-11 11:34:44 -0700497 tv.setFavicon(mUi.getFaviconDrawable(favicon));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700498 }
499 }
500
Michael Kolba2b2ba82010-08-04 17:54:03 -0700501 public void onNewTab(Tab tab) {
Michael Kolb94827b62011-01-08 14:23:45 -0800502 TabView tv = buildTabView(tab);
Michael Kolb2d59c322011-01-25 13:18:55 -0800503 animateTabIn(tab, tv);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700504 }
505
Michael Kolba2b2ba82010-08-04 17:54:03 -0700506 public void onRemoveTab(Tab tab) {
Michael Kolb94827b62011-01-08 14:23:45 -0800507 TabView tv = mTabMap.get(tab);
508 if (tv != null) {
Michael Kolb2d59c322011-01-25 13:18:55 -0800509 animateTabOut(tab, tv);
510 } else {
511 mTabMap.remove(tab);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700512 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700513 }
514
Michael Kolba2b2ba82010-08-04 17:54:03 -0700515 public void onUrlAndTitle(Tab tab, String url, String title) {
Michael Kolb94827b62011-01-08 14:23:45 -0800516 TabView tv = mTabMap.get(tab);
517 if (tv != null) {
518 if (title != null) {
519 tv.setDisplayTitle(title);
520 } else if (url != null) {
521 tv.setDisplayTitle(UrlUtils.stripUrl(url));
522 }
John Reck541f55a2011-06-07 16:34:43 -0700523 tv.updateTabIcons();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700524 }
525 }
526
Michael Kolba2b2ba82010-08-04 17:54:03 -0700527 private boolean isLoading() {
Michael Kolb1c3a6d22011-12-15 15:28:49 -0800528 Tab tab = mTabControl.getCurrentTab();
529 if (tab != null) {
530 return tab.inPageLoad();
Michael Kolb8233fac2010-10-26 16:08:53 -0700531 } else {
532 return false;
533 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700534 }
535
Michael Kolba2b2ba82010-08-04 17:54:03 -0700536}