forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
TabsTray.java
641 lines (507 loc) · 22 KB
/
TabsTray.java
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
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.gecko;
import org.mozilla.gecko.PropertyAnimator.Property;
import org.mozilla.gecko.widget.TwoWayView;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class TabsTray extends TwoWayView
implements TabsPanel.PanelView {
private static final String LOGTAG = "GeckoTabsTray";
private Context mContext;
private TabsPanel mTabsPanel;
private TabsAdapter mTabsAdapter;
private List<View> mPendingClosedTabs;
private int mCloseAnimationCount;
private TabSwipeGestureListener mSwipeListener;
// Time to animate non-flinged tabs of screen, in milliseconds
private static final int ANIMATION_DURATION = 250;
private static final String ABOUT_HOME = "about:home";
public TabsTray(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
mCloseAnimationCount = 0;
mPendingClosedTabs = new ArrayList<View>();
//setItemsCanFocus(true);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TabsTray);
boolean isPrivate = (a.getInt(R.styleable.TabsTray_tabs, 0x0) == 1);
a.recycle();
mTabsAdapter = new TabsAdapter(mContext, isPrivate);
setAdapter(mTabsAdapter);
mSwipeListener = new TabSwipeGestureListener();
setOnTouchListener(mSwipeListener);
setOnScrollListener(mSwipeListener.makeScrollListener());
setRecyclerListener(new RecyclerListener() {
@Override
public void onMovedToScrapHeap(View view) {
TabRow row = (TabRow) view.getTag();
row.thumbnail.setImageDrawable(null);
row.close.setVisibility(View.VISIBLE);
}
});
}
@Override
public ViewGroup getLayout() {
return this;
}
@Override
public void setTabsPanel(TabsPanel panel) {
mTabsPanel = panel;
}
@Override
public void show() {
setVisibility(View.VISIBLE);
Tabs.getInstance().refreshThumbnails();
Tabs.registerOnTabsChangedListener(mTabsAdapter);
mTabsAdapter.refreshTabsData();
}
@Override
public void hide() {
setVisibility(View.GONE);
Tabs.unregisterOnTabsChangedListener(mTabsAdapter);
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Tab:Screenshot:Cancel",""));
mTabsAdapter.clear();
}
@Override
public boolean shouldExpand() {
return isVertical();
}
private void autoHidePanel() {
mTabsPanel.autoHidePanel();
}
// ViewHolder for a row in the list
private class TabRow {
int id;
TextView title;
ImageView thumbnail;
ImageButton close;
ViewGroup info;
public TabRow(View view) {
info = (ViewGroup) view;
title = (TextView) view.findViewById(R.id.title);
thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
close = (ImageButton) view.findViewById(R.id.close);
}
}
// Adapter to bind tabs into a list
private class TabsAdapter extends BaseAdapter implements Tabs.OnTabsChangedListener {
private Context mContext;
private boolean mIsPrivate;
private ArrayList<Tab> mTabs;
private LayoutInflater mInflater;
private Button.OnClickListener mOnCloseClickListener;
public TabsAdapter(Context context, boolean isPrivate) {
mContext = context;
mInflater = LayoutInflater.from(mContext);
mIsPrivate = isPrivate;
mOnCloseClickListener = new Button.OnClickListener() {
@Override
public void onClick(View v) {
TabRow tab = (TabRow) v.getTag();
final int pos = (isVertical() ? tab.info.getWidth() : tab.info.getHeight());
animateClose(tab.info, pos);
}
};
}
@Override
public void onTabChanged(Tab tab, Tabs.TabEvents msg, Object data) {
switch (msg) {
case ADDED:
// Refresh the list to make sure the new tab is added in the right position.
refreshTabsData();
break;
case CLOSED:
removeTab(tab);
break;
case SELECTED:
// Update the selected position, then fall through...
updateSelectedPosition();
case UNSELECTED:
// We just need to update the style for the unselected tab...
case THUMBNAIL:
case TITLE:
View view = TabsTray.this.getChildAt(getPositionForTab(tab) - TabsTray.this.getFirstVisiblePosition());
if (view == null)
return;
TabRow row = (TabRow) view.getTag();
assignValues(row, tab);
break;
}
}
private void refreshTabsData() {
// Store a different copy of the tabs, so that we don't have to worry about
// accidentally updating it on the wrong thread.
mTabs = new ArrayList<Tab>();
Iterable<Tab> tabs = Tabs.getInstance().getTabsInOrder();
for (Tab tab : tabs) {
if (tab.isPrivate() == mIsPrivate)
mTabs.add(tab);
}
notifyDataSetChanged(); // Be sure to call this whenever mTabs changes.
updateSelectedPosition();
}
// Updates the selected position in the list so that it will be scrolled to the right place.
private void updateSelectedPosition() {
int selected = getPositionForTab(Tabs.getInstance().getSelectedTab());
for (int i=0; i < getCount(); i++)
TabsTray.this.setItemChecked(i, (i == selected));
if (selected != -1)
TabsTray.this.setSelection(selected);
}
public void clear() {
mTabs = null;
notifyDataSetChanged(); // Be sure to call this whenever mTabs changes.
}
@Override
public int getCount() {
return (mTabs == null ? 0 : mTabs.size());
}
@Override
public Tab getItem(int position) {
return mTabs.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
private int getPositionForTab(Tab tab) {
if (mTabs == null || tab == null)
return -1;
return mTabs.indexOf(tab);
}
private void removeTab(Tab tab) {
if (tab.isPrivate() == mIsPrivate && mTabs != null) {
mTabs.remove(tab);
notifyDataSetChanged(); // Be sure to call this whenever mTabs changes.
updateSelectedPosition();
}
}
private void assignValues(TabRow row, Tab tab) {
if (row == null || tab == null)
return;
row.id = tab.getId();
Drawable thumbnailImage = tab.getThumbnail();
if (thumbnailImage != null)
row.thumbnail.setImageDrawable(thumbnailImage);
else if (TextUtils.equals(tab.getURL(), ABOUT_HOME))
row.thumbnail.setImageResource(R.drawable.abouthome_thumbnail);
else
row.thumbnail.setImageResource(R.drawable.tab_thumbnail_default);
row.title.setText(tab.getDisplayTitle());
row.close.setTag(row);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TabRow row;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.tabs_row, null);
row = new TabRow(convertView);
row.close.setOnClickListener(mOnCloseClickListener);
convertView.setTag(row);
} else {
row = (TabRow) convertView.getTag();
}
Tab tab = mTabs.get(position);
assignValues(row, tab);
return convertView;
}
}
private boolean isVertical() {
return (getOrientation().compareTo(TwoWayView.Orientation.VERTICAL) == 0);
}
private void animateClose(final View view, int pos) {
PropertyAnimator animator = new PropertyAnimator(ANIMATION_DURATION);
animator.attach(view, Property.ALPHA, 0);
if (isVertical())
animator.attach(view, Property.TRANSLATION_X, pos);
else
animator.attach(view, Property.TRANSLATION_Y, pos);
mCloseAnimationCount++;
mPendingClosedTabs.add(view);
animator.setPropertyAnimationListener(new PropertyAnimator.PropertyAnimationListener() {
@Override
public void onPropertyAnimationStart() { }
@Override
public void onPropertyAnimationEnd() {
mCloseAnimationCount--;
if (mCloseAnimationCount > 0)
return;
for (View pendingView : mPendingClosedTabs) {
animateFinishClose(pendingView);
}
mPendingClosedTabs.clear();
}
});
if (mTabsAdapter.getCount() == 1)
autoHidePanel();
animator.start();
}
private void animateFinishClose(final View view) {
PropertyAnimator animator = new PropertyAnimator(ANIMATION_DURATION);
final boolean isVertical = isVertical();
if (isVertical)
animator.attach(view, Property.HEIGHT, 1);
else
animator.attach(view, Property.WIDTH, 1);
TabRow tab = (TabRow)view.getTag();
final int tabId = tab.id;
final int originalSize = (isVertical ? view.getHeight() : view.getWidth());
animator.setPropertyAnimationListener(new PropertyAnimator.PropertyAnimationListener() {
@Override
public void onPropertyAnimationStart() { }
@Override
public void onPropertyAnimationEnd() {
// Reset view presentation as it will be recycled in the
// list view by the adapter.
AnimatorProxy proxy = AnimatorProxy.create(view);
proxy.setAlpha(1);
if (isVertical) {
proxy.setHeight(originalSize);
proxy.setTranslationX(0);
} else {
proxy.setWidth(originalSize);
proxy.setTranslationY(0);
}
Tabs tabs = Tabs.getInstance();
Tab tab = tabs.getTab(tabId);
tabs.closeTab(tab);
}
});
animator.start();
}
private void animateCancel(final View view) {
PropertyAnimator animator = new PropertyAnimator(ANIMATION_DURATION);
animator.attach(view, Property.ALPHA, 1);
if (isVertical())
animator.attach(view, Property.TRANSLATION_X, 0);
else
animator.attach(view, Property.TRANSLATION_Y, 0);
animator.setPropertyAnimationListener(new PropertyAnimator.PropertyAnimationListener() {
@Override
public void onPropertyAnimationStart() { }
@Override
public void onPropertyAnimationEnd() {
TabRow tab = (TabRow) view.getTag();
tab.close.setVisibility(View.VISIBLE);
}
});
animator.start();
}
private class TabSwipeGestureListener implements View.OnTouchListener {
// same value the stock browser uses for after drag animation velocity in pixels/sec
// http://androidxref.com/4.0.4/xref/packages/apps/Browser/src/com/android/browser/NavTabScroller.java#61
private static final float MIN_VELOCITY = 750;
private int mSwipeThreshold;
private int mMinFlingVelocity;
private int mMaxFlingVelocity;
private VelocityTracker mVelocityTracker;
private int mListWidth = 1;
private int mListHeight = 1;
private View mSwipeView;
private AnimatorProxy mSwipeProxy;
private int mSwipeViewPosition;
private Runnable mPendingCheckForTap;
private float mSwipeStartX;
private float mSwipeStartY;
private boolean mSwiping;
private boolean mEnabled;
public TabSwipeGestureListener() {
mSwipeView = null;
mSwipeProxy = null;
mSwipeViewPosition = TwoWayView.INVALID_POSITION;
mSwiping = false;
mEnabled = true;
ViewConfiguration vc = ViewConfiguration.get(TabsTray.this.getContext());
mSwipeThreshold = vc.getScaledTouchSlop();
mMinFlingVelocity = (int) (getContext().getResources().getDisplayMetrics().density * MIN_VELOCITY);
mMaxFlingVelocity = vc.getScaledMaximumFlingVelocity();
}
public void setEnabled(boolean enabled) {
mEnabled = enabled;
}
public TwoWayView.OnScrollListener makeScrollListener() {
return new TwoWayView.OnScrollListener() {
@Override
public void onScrollStateChanged(TwoWayView twoWayView, int scrollState) {
setEnabled(scrollState != TwoWayView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL);
}
@Override
public void onScroll(TwoWayView twoWayView, int i, int i1, int i2) {
}
};
}
@Override
public boolean onTouch(View view, MotionEvent e) {
if (!mEnabled)
return false;
if (mListWidth < 2 || mListHeight < 2) {
mListWidth = TabsTray.this.getWidth();
mListHeight = TabsTray.this.getHeight();
}
switch (e.getActionMasked()) {
case MotionEvent.ACTION_DOWN: {
// Check if we should set pressed state on the
// touched view after a standard delay.
triggerCheckForTap();
final float x = e.getRawX();
final float y = e.getRawY();
// Find out which view is being touched
mSwipeView = findViewAt(x, y);
if (mSwipeView != null) {
mSwipeStartX = e.getRawX();
mSwipeStartY = e.getRawY();
mSwipeViewPosition = TabsTray.this.getPositionForView(mSwipeView);
mVelocityTracker = VelocityTracker.obtain();
mVelocityTracker.addMovement(e);
}
view.onTouchEvent(e);
return true;
}
case MotionEvent.ACTION_UP: {
if (mSwipeView == null)
break;
cancelCheckForTap();
mSwipeView.setPressed(false);
if (!mSwiping) {
TabRow tab = (TabRow) mSwipeView.getTag();
Tabs.getInstance().selectTab(tab.id);
autoHidePanel();
break;
}
mVelocityTracker.addMovement(e);
mVelocityTracker.computeCurrentVelocity(1000, mMaxFlingVelocity);
float velocityX = Math.abs(mVelocityTracker.getXVelocity());
float velocityY = Math.abs(mVelocityTracker.getYVelocity());
boolean dismiss = false;
boolean dismissDirection = false;
int dismissTranslation = 0;
if (isVertical()) {
float deltaX = mSwipeProxy.getTranslationX();
if (Math.abs(deltaX) > mListWidth / 2) {
dismiss = true;
dismissDirection = (deltaX > 0);
} else if (mMinFlingVelocity <= velocityX && velocityX <= mMaxFlingVelocity
&& velocityY < velocityX) {
dismiss = mSwiping && (deltaX * mVelocityTracker.getXVelocity() > 0);
dismissDirection = (mVelocityTracker.getXVelocity() > 0);
}
dismissTranslation = (dismissDirection ? mListWidth : -mListWidth);
} else {
float deltaY = mSwipeProxy.getTranslationY();
if (Math.abs(deltaY) > mListHeight / 2) {
dismiss = true;
dismissDirection = (deltaY > 0);
} else if (mMinFlingVelocity <= velocityY && velocityY <= mMaxFlingVelocity
&& velocityX < velocityY) {
dismiss = mSwiping && (deltaY * mVelocityTracker.getYVelocity() > 0);
dismissDirection = (mVelocityTracker.getYVelocity() > 0);
}
dismissTranslation = (dismissDirection ? mListHeight : -mListHeight);
}
if (dismiss)
animateClose(mSwipeView, dismissTranslation);
else
animateCancel(mSwipeView);
mVelocityTracker = null;
mSwipeView = null;
mSwipeViewPosition = TwoWayView.INVALID_POSITION;
mSwipeProxy = null;
mSwipeStartX = 0;
mSwipeStartY = 0;
mSwiping = false;
break;
}
case MotionEvent.ACTION_MOVE: {
if (mSwipeView == null)
break;
mVelocityTracker.addMovement(e);
final boolean isVertical = isVertical();
float deltaX = e.getRawX() - mSwipeStartX;
float deltaY = e.getRawY() - mSwipeStartY;
float delta = (isVertical ? deltaX : deltaY);
boolean isScrollingX = Math.abs(deltaX) > mSwipeThreshold;
boolean isScrollingY = Math.abs(deltaY) > mSwipeThreshold;
boolean isSwipingToClose = (isVertical ? isScrollingX : isScrollingY);
// If we're actually swiping, make sure we don't
// set pressed state on the swiped view.
if (isScrollingX || isScrollingY)
cancelCheckForTap();
if (isSwipingToClose) {
mSwiping = true;
TabsTray.this.requestDisallowInterceptTouchEvent(true);
TabRow tab = (TabRow) mSwipeView.getTag();
tab.close.setVisibility(View.INVISIBLE);
// Stops listview from highlighting the touched item
// in the list when swiping.
MotionEvent cancelEvent = MotionEvent.obtain(e);
cancelEvent.setAction(MotionEvent.ACTION_CANCEL |
(e.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT));
TabsTray.this.onTouchEvent(cancelEvent);
mSwipeProxy = AnimatorProxy.create(mSwipeView);
}
if (mSwiping) {
if (isVertical)
mSwipeProxy.setTranslationX(delta);
else
mSwipeProxy.setTranslationY(delta);
mSwipeProxy.setAlpha(Math.max(0.1f, Math.min(1f,
1f - 2f * Math.abs(delta) / (isVertical ? mListWidth : mListHeight))));
return true;
}
break;
}
}
return false;
}
private View findViewAt(float rawX, float rawY) {
Rect rect = new Rect();
int[] listViewCoords = new int[2];
TabsTray.this.getLocationOnScreen(listViewCoords);
int x = (int) rawX - listViewCoords[0];
int y = (int) rawY - listViewCoords[1];
for (int i = 0; i < TabsTray.this.getChildCount(); i++) {
View child = TabsTray.this.getChildAt(i);
child.getHitRect(rect);
if (rect.contains(x, y))
return child;
}
return null;
}
private void triggerCheckForTap() {
if (mPendingCheckForTap == null)
mPendingCheckForTap = new CheckForTap();
TabsTray.this.postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
}
private void cancelCheckForTap() {
if (mPendingCheckForTap == null)
return;
TabsTray.this.removeCallbacks(mPendingCheckForTap);
}
private class CheckForTap implements Runnable {
@Override
public void run() {
if (!mSwiping && mSwipeView != null && mEnabled)
mSwipeView.setPressed(true);
}
}
}
}