1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
|
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.browser;
import com.android.browser.BrowserWebView.ScrollListener;
import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.PaintDrawable;
import android.view.ContextMenu;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* tabbed title bar for xlarge screen browser
*/
public class TabBar extends LinearLayout
implements ScrollListener, OnClickListener {
private static final int PROGRESS_MAX = 100;
private Activity mActivity;
private UiController mUiController;
private TabControl mTabControl;
private XLargeUi mUi;
private final int mTabWidthSelected;
private final int mTabWidthUnselected;
private TabScrollView mTabs;
private ImageButton mNewTab;
private int mButtonWidth;
private Map<Tab, TabView> mTabMap;
private Drawable mGenericFavicon;
private int mCurrentTextureWidth = 0;
private int mCurrentTextureHeight = 0;
private Drawable mActiveDrawable;
private Drawable mInactiveDrawable;
private final Paint mActiveShaderPaint = new Paint();
private final Paint mInactiveShaderPaint = new Paint();
private final Paint mFocusPaint = new Paint();
private final Matrix mActiveMatrix = new Matrix();
private final Matrix mInactiveMatrix = new Matrix();
private BitmapShader mActiveShader;
private BitmapShader mInactiveShader;
private int mTabOverlap;
private int mAddTabOverlap;
private int mTabSliceWidth;
private boolean mUseQuickControls;
public TabBar(Activity activity, UiController controller, XLargeUi ui) {
super(activity);
mActivity = activity;
mUiController = controller;
mTabControl = mUiController.getTabControl();
mUi = ui;
Resources res = activity.getResources();
mTabWidthSelected = (int) res.getDimension(R.dimen.tab_width_selected);
mTabWidthUnselected = (int) res.getDimension(R.dimen.tab_width_unselected);
mActiveDrawable = res.getDrawable(R.drawable.bg_urlbar);
mInactiveDrawable = res.getDrawable(R.drawable.browsertab_inactive);
mTabMap = new HashMap<Tab, TabView>();
Resources resources = activity.getResources();
LayoutInflater factory = LayoutInflater.from(activity);
factory.inflate(R.layout.tab_bar, this);
setPadding(0, (int) res.getDimension(R.dimen.tab_padding_top), 0, 0);
mTabs = (TabScrollView) findViewById(R.id.tabs);
mNewTab = (ImageButton) findViewById(R.id.newtab);
mNewTab.setOnClickListener(this);
mGenericFavicon = res.getDrawable(R.drawable.app_web_browser_sm);
updateTabs(mUiController.getTabs());
mButtonWidth = -1;
// tab dimensions
mTabOverlap = (int) res.getDimension(R.dimen.tab_overlap);
mAddTabOverlap = (int) res.getDimension(R.dimen.tab_addoverlap);
mTabSliceWidth = (int) res.getDimension(R.dimen.tab_slice);
mActiveShaderPaint.setStyle(Paint.Style.FILL);
mActiveShaderPaint.setAntiAlias(true);
mInactiveShaderPaint.setStyle(Paint.Style.FILL);
mInactiveShaderPaint.setAntiAlias(true);
mFocusPaint.setStyle(Paint.Style.STROKE);
mFocusPaint.setStrokeWidth(res.getDimension(R.dimen.tab_focus_stroke));
mFocusPaint.setAntiAlias(true);
mFocusPaint.setColor(res.getColor(R.color.tabFocusHighlight));
}
void setUseQuickControls(boolean useQuickControls) {
mUseQuickControls = useQuickControls;
mNewTab.setVisibility(mUseQuickControls ? View.GONE
: View.VISIBLE);
}
int getTabCount() {
return mTabMap.size();
}
void updateTabs(List<Tab> tabs) {
mTabs.clearTabs();
mTabMap.clear();
for (Tab tab : tabs) {
TabView tv = buildTabView(tab);
mTabs.addTab(tv);
}
mTabs.setSelectedTab(mTabControl.getCurrentPosition());
}
@Override
protected void onMeasure(int hspec, int vspec) {
super.onMeasure(hspec, vspec);
int w = getMeasuredWidth();
// adjust for new tab overlap
if (!mUseQuickControls) {
w -= mAddTabOverlap;
}
setMeasuredDimension(w, getMeasuredHeight());
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
// use paddingLeft and paddingTop
int pl = getPaddingLeft();
int pt = getPaddingTop();
int sw = mTabs.getMeasuredWidth();
int w = right - left - pl;
if (mUseQuickControls) {
mButtonWidth = 0;
} else {
mButtonWidth = mNewTab.getMeasuredWidth() - mAddTabOverlap;
if (w-sw < mButtonWidth) {
sw = w - mButtonWidth;
}
}
mTabs.layout(pl, pt, pl + sw, bottom - top);
// adjust for overlap
if (!mUseQuickControls) {
mNewTab.layout(pl + sw - mAddTabOverlap, pt,
pl + sw + mButtonWidth - mAddTabOverlap, bottom - top);
}
}
public void onClick(View view) {
if (mNewTab == view) {
mUiController.openTabToHomePage();
} else if (mTabs.getSelectedTab() == view) {
if (mUseQuickControls) {
if (mUi.isTitleBarShowing() && !isLoading()) {
mUi.stopEditingUrl();
mUi.hideTitleBar();
} else {
mUi.stopWebViewScrolling();
mUi.editUrl(false);
}
} else if (mUi.isTitleBarShowing() && !isLoading()) {
mUi.stopEditingUrl();
mUi.hideTitleBar();
} else {
showUrlBar();
}
} else if (view instanceof TabView) {
final Tab tab = ((TabView) view).mTab;
int ix = mTabs.getChildIndex(view);
if (ix >= 0) {
mTabs.setSelectedTab(ix);
mUiController.switchToTab(tab);
}
}
}
private void showUrlBar() {
mUi.stopWebViewScrolling();
mUi.showTitleBar();
}
void showTitleBarIndicator(boolean show) {
Tab tab = mTabControl.getCurrentTab();
if (tab != null) {
TabView tv = mTabMap.get(tab);
if (tv != null) {
tv.showIndicator(show);
}
}
}
boolean showsTitleBarIndicator() {
Tab tab = mTabControl.getCurrentTab();
if (tab != null) {
TabView tv = mTabMap.get(tab);
if (tv != null) {
return tv.showsIndicator();
}
}
return false;
}
// callback after fake titlebar is shown
void onShowTitleBar() {
showTitleBarIndicator(false);
}
// callback after fake titlebar is hidden
void onHideTitleBar() {
Tab tab = mTabControl.getCurrentTab();
WebView w = tab.getWebView();
if (w != null) {
showTitleBarIndicator(w.getVisibleTitleHeight() == 0);
}
}
// webview scroll listener
@Override
public void onScroll(int visibleTitleHeight, boolean userInitiated) {
if (mUseQuickControls) return;
// isLoading is using the current tab, which initially might not be set yet
if (mTabControl.getCurrentTab() != null
&& !isLoading()) {
if (visibleTitleHeight == 0) {
if (!showsTitleBarIndicator()
&& (!mUi.isEditingUrl() || userInitiated)) {
mUi.hideTitleBar();
showTitleBarIndicator(true);
}
} else {
if (showsTitleBarIndicator()) {
showTitleBarIndicator(false);
}
}
}
}
@Override
public void createContextMenu(ContextMenu menu) {
MenuInflater inflater = mActivity.getMenuInflater();
inflater.inflate(R.menu.title_context, menu);
mActivity.onCreateContextMenu(menu, this, null);
}
private TabView buildTabView(Tab tab) {
TabView tabview = new TabView(mActivity, tab);
mTabMap.put(tab, tabview);
tabview.setOnClickListener(this);
return tabview;
}
private static Bitmap getDrawableAsBitmap(Drawable drawable, int width, int height) {
Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
drawable.setBounds(0, 0, width, height);
drawable.draw(c);
return b;
}
/**
* View used in the tab bar
*/
class TabView extends LinearLayout implements OnClickListener {
Tab mTab;
View mTabContent;
TextView mTitle;
View mIndicator;
View mIncognito;
ImageView mIconView;
ImageView mLock;
ImageView mClose;
boolean mSelected;
boolean mInLoad;
Path mPath;
Path mFocusPath;
int[] mWindowPos;
/**
* @param context
*/
public TabView(Context context, Tab tab) {
super(context);
setWillNotDraw(false);
mPath = new Path();
mFocusPath = new Path();
mWindowPos = new int[2];
mTab = tab;
setGravity(Gravity.CENTER_VERTICAL);
setOrientation(LinearLayout.HORIZONTAL);
setPadding(mTabOverlap, 0, mTabSliceWidth, 0);
LayoutInflater inflater = LayoutInflater.from(getContext());
mTabContent = inflater.inflate(R.layout.tab_title, this, true);
mTitle = (TextView) mTabContent.findViewById(R.id.title);
mIconView = (ImageView) mTabContent.findViewById(R.id.favicon);
mLock = (ImageView) mTabContent.findViewById(R.id.lock);
mClose = (ImageView) mTabContent.findViewById(R.id.close);
mClose.setOnClickListener(this);
mIncognito = mTabContent.findViewById(R.id.incognito);
mIndicator = mTabContent.findViewById(R.id.chevron);
mSelected = false;
mInLoad = false;
// update the status
updateFromTab();
}
void showIndicator(boolean show) {
if (mSelected) {
mIndicator.setVisibility(show ? View.VISIBLE : View.GONE);
LayoutParams lp = (LinearLayout.LayoutParams) getLayoutParams();
if (show) {
lp.width = mTabWidthSelected + mIndicator.getWidth();
} else {
lp.width = mTabWidthSelected;
}
lp.height = LayoutParams.MATCH_PARENT;
setLayoutParams(lp);
} else {
mIndicator.setVisibility(View.GONE);
}
}
boolean showsIndicator() {
return (mIndicator.getVisibility() == View.VISIBLE);
}
@Override
public void onClick(View v) {
if (v == mClose) {
closeTab();
}
}
private void updateFromTab() {
String displayTitle = mTab.getTitle();
if (displayTitle == null) {
displayTitle = mTab.getUrl();
}
setDisplayTitle(displayTitle);
setProgress(mTab.getLoadProgress());
if (mTab.getFavicon() != null) {
setFavicon(renderFavicon(mTab.getFavicon()));
}
if (mTab != null) {
mIncognito.setVisibility(
mTab.isPrivateBrowsingEnabled() ?
View.VISIBLE : View.GONE);
}
}
@Override
public void setActivated(boolean selected) {
mSelected = selected;
mClose.setVisibility(mSelected ? View.VISIBLE : View.GONE);
mIndicator.setVisibility(View.GONE);
mTitle.setTextAppearance(mActivity, mSelected ?
R.style.TabTitleSelected : R.style.TabTitleUnselected);
setHorizontalFadingEdgeEnabled(!mSelected);
super.setActivated(selected);
LayoutParams lp = (LinearLayout.LayoutParams) getLayoutParams();
lp.width = selected ? mTabWidthSelected : mTabWidthUnselected;
lp.height = LayoutParams.MATCH_PARENT;
setLayoutParams(lp);
setFocusable(!selected);
postInvalidate();
}
void setDisplayTitle(String title) {
mTitle.setText(title);
}
void setFavicon(Drawable d) {
mIconView.setImageDrawable(d);
}
void setLock(Drawable d) {
if (null == d) {
mLock.setVisibility(View.GONE);
} else {
mLock.setImageDrawable(d);
mLock.setVisibility(View.VISIBLE);
}
}
void setProgress(int newProgress) {
if (newProgress >= PROGRESS_MAX) {
mInLoad = false;
} else {
if (!mInLoad && getWindowToken() != null) {
mInLoad = true;
}
}
}
private void closeTab() {
if (mTab == mTabControl.getCurrentTab()) {
mUiController.closeCurrentTab();
} else {
mUiController.closeTab(mTab);
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
setTabPath(mPath, 0, 0, r - l, b - t);
setFocusPath(mFocusPath, 0, 0, r - l, b - t);
}
@Override
protected void dispatchDraw(Canvas canvas) {
if (mCurrentTextureWidth != mUi.getContentWidth() ||
mCurrentTextureHeight != getHeight()) {
mCurrentTextureWidth = mUi.getContentWidth();
mCurrentTextureHeight = getHeight();
if (mCurrentTextureWidth > 0 && mCurrentTextureHeight > 0) {
Bitmap activeTexture = getDrawableAsBitmap(mActiveDrawable,
mCurrentTextureWidth, mCurrentTextureHeight);
Bitmap inactiveTexture = getDrawableAsBitmap(mInactiveDrawable,
mCurrentTextureWidth, mCurrentTextureHeight);
mActiveShader = new BitmapShader(activeTexture,
Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
mActiveShaderPaint.setShader(mActiveShader);
mInactiveShader = new BitmapShader(inactiveTexture,
Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
mInactiveShaderPaint.setShader(mInactiveShader);
}
}
// add some monkey protection
if ((mActiveShader != null) && (mInactiveShader != null)) {
int state = canvas.save();
getLocationInWindow(mWindowPos);
Paint paint = mSelected ? mActiveShaderPaint : mInactiveShaderPaint;
drawClipped(canvas, paint, mPath, mWindowPos[0]);
canvas.restoreToCount(state);
}
super.dispatchDraw(canvas);
}
private void drawClipped(Canvas canvas, Paint paint, Path clipPath, int left) {
// TODO: We should change the matrix/shader only when needed
final Matrix matrix = mSelected ? mActiveMatrix : mInactiveMatrix;
matrix.setTranslate(-left, 0.0f);
(mSelected ? mActiveShader : mInactiveShader).setLocalMatrix(matrix);
canvas.drawPath(clipPath, paint);
if (isFocused()) {
canvas.drawPath(mFocusPath, mFocusPaint);
}
}
private void setTabPath(Path path, int l, int t, int r, int b) {
path.reset();
path.moveTo(l, b);
path.lineTo(l, t);
path.lineTo(r - mTabSliceWidth, t);
path.lineTo(r, b);
path.close();
}
private void setFocusPath(Path path, int l, int t, int r, int b) {
path.reset();
path.moveTo(l, b);
path.lineTo(l, t);
path.lineTo(r - mTabSliceWidth, t);
path.lineTo(r, b);
}
}
static Drawable createFaviconBackground(Context context) {
PaintDrawable faviconBackground = new PaintDrawable();
Resources res = context.getResources();
faviconBackground.getPaint().setColor(context.getResources()
.getColor(R.color.tabFaviconBackground));
faviconBackground.setCornerRadius(
res.getDimension(R.dimen.tab_favicon_corner_radius));
return faviconBackground;
}
private Drawable renderFavicon(Bitmap icon) {
Drawable[] array = new Drawable[2];
array[0] = createFaviconBackground(getContext());
if (icon == null) {
array[1] = mGenericFavicon;
} else {
array[1] = new BitmapDrawable(icon);
}
LayerDrawable d = new LayerDrawable(array);
d.setLayerInset(1, 2, 2, 2, 2);
return d;
}
private void animateTabOut(final Tab tab, final TabView tv) {
ObjectAnimator scalex = ObjectAnimator.ofFloat(tv, "scaleX", 1.0f, 0.0f);
ObjectAnimator scaley = ObjectAnimator.ofFloat(tv, "scaleY", 1.0f, 0.0f);
ObjectAnimator alpha = ObjectAnimator.ofFloat(tv, "alpha", 1.0f, 0.0f);
AnimatorSet animator = new AnimatorSet();
animator.playTogether(scalex, scaley, alpha);
animator.setDuration(150);
animator.addListener(new AnimatorListener() {
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
mTabs.removeTab(tv);
mTabMap.remove(tab);
mUi.onRemoveTabCompleted(tab);
}
@Override
public void onAnimationRepeat(Animator animation) {
}
@Override
public void onAnimationStart(Animator animation) {
}
});
animator.start();
}
private void animateTabIn(final Tab tab, final TabView tv) {
ObjectAnimator scalex = ObjectAnimator.ofFloat(tv, "scaleX", 0.0f, 1.0f);
scalex.setDuration(150);
scalex.addListener(new AnimatorListener() {
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
mUi.onAddTabCompleted(tab);
}
@Override
public void onAnimationRepeat(Animator animation) {
}
@Override
public void onAnimationStart(Animator animation) {
mTabs.addTab(tv);
}
});
scalex.start();
}
// TabChangeListener implementation
public void onSetActiveTab(Tab tab) {
mTabs.setSelectedTab(mTabControl.getTabPosition(tab));
TabView tv = mTabMap.get(tab);
if (tv != null) {
tv.setProgress(tv.mTab.getLoadProgress());
// update the scroll state
WebView webview = tab.getWebView();
if (webview != null) {
int h = webview.getVisibleTitleHeight();
onScroll(h, true);
}
}
}
public void onFavicon(Tab tab, Bitmap favicon) {
TabView tv = mTabMap.get(tab);
if (tv != null) {
tv.setFavicon(renderFavicon(favicon));
}
}
public void onNewTab(Tab tab) {
TabView tv = buildTabView(tab);
animateTabIn(tab, tv);
}
public void onProgress(Tab tab, int progress) {
TabView tv = mTabMap.get(tab);
if (tv != null) {
tv.setProgress(progress);
}
}
public void onRemoveTab(Tab tab) {
TabView tv = mTabMap.get(tab);
if (tv != null) {
animateTabOut(tab, tv);
} else {
mTabMap.remove(tab);
}
}
public void onUrlAndTitle(Tab tab, String url, String title) {
TabView tv = mTabMap.get(tab);
if (tv != null) {
if (title != null) {
tv.setDisplayTitle(title);
} else if (url != null) {
tv.setDisplayTitle(UrlUtils.stripUrl(url));
}
}
}
private boolean isLoading() {
TabView tv = mTabMap.get(mTabControl.getCurrentTab());
if (tv != null) {
return tv.mInLoad;
} else {
return false;
}
}
}
|