2011-11-18 10:28:17 -08:00
|
|
|
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
|
2012-03-13 08:15:43 -07:00
|
|
|
/* 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/. */
|
2011-11-18 10:28:17 -08:00
|
|
|
|
|
|
|
package org.mozilla.gecko;
|
|
|
|
|
2012-07-27 17:53:54 -07:00
|
|
|
import org.mozilla.gecko.PropertyAnimator.Property;
|
2011-11-18 10:28:17 -08:00
|
|
|
|
|
|
|
import android.content.Context;
|
2012-12-18 16:44:37 -08:00
|
|
|
import android.content.res.TypedArray;
|
2012-10-12 05:22:03 -07:00
|
|
|
import android.graphics.Rect;
|
2012-07-27 17:53:54 -07:00
|
|
|
import android.graphics.drawable.Drawable;
|
2012-03-19 12:36:18 -07:00
|
|
|
import android.text.TextUtils;
|
2012-01-27 12:18:02 -08:00
|
|
|
import android.util.AttributeSet;
|
2011-11-18 10:28:17 -08:00
|
|
|
import android.view.LayoutInflater;
|
2012-06-05 13:29:37 -07:00
|
|
|
import android.view.MotionEvent;
|
2012-10-12 05:22:03 -07:00
|
|
|
import android.view.VelocityTracker;
|
2011-11-18 10:28:17 -08:00
|
|
|
import android.view.View;
|
2012-10-12 05:22:03 -07:00
|
|
|
import android.view.ViewConfiguration;
|
2011-11-18 10:28:17 -08:00
|
|
|
import android.view.ViewGroup;
|
2012-10-12 05:22:03 -07:00
|
|
|
import android.widget.AbsListView;
|
2012-04-30 14:09:41 -07:00
|
|
|
import android.widget.AbsListView.RecyclerListener;
|
2011-11-18 10:28:17 -08:00
|
|
|
import android.widget.BaseAdapter;
|
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.ImageButton;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.LinearLayout;
|
|
|
|
import android.widget.ListView;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
2012-07-27 17:53:54 -07:00
|
|
|
import java.util.ArrayList;
|
2012-10-31 08:28:29 -07:00
|
|
|
import java.util.List;
|
2012-06-05 13:29:37 -07:00
|
|
|
|
2012-12-20 14:35:03 -08:00
|
|
|
public class TabsTray extends ListView
|
2012-07-09 16:19:18 -07:00
|
|
|
implements TabsPanel.PanelView {
|
2012-06-07 21:47:22 -07:00
|
|
|
private static final String LOGTAG = "GeckoTabsTray";
|
2012-03-13 08:15:43 -07:00
|
|
|
|
2012-06-07 21:47:22 -07:00
|
|
|
private Context mContext;
|
2012-07-27 23:31:40 -07:00
|
|
|
private TabsPanel mTabsPanel;
|
2011-11-18 10:28:17 -08:00
|
|
|
|
|
|
|
private TabsAdapter mTabsAdapter;
|
2012-10-31 08:28:29 -07:00
|
|
|
|
|
|
|
private List<View> mPendingClosedTabs;
|
|
|
|
private int mCloseAnimationCount;
|
2011-11-18 10:28:17 -08:00
|
|
|
|
2012-10-12 05:22:03 -07:00
|
|
|
private TabSwipeGestureListener mSwipeListener;
|
|
|
|
|
|
|
|
// Time to animate non-flinged tabs of screen, in milliseconds
|
|
|
|
private static final int ANIMATION_DURATION = 250;
|
2012-06-05 13:29:37 -07:00
|
|
|
|
2012-03-19 12:36:18 -07:00
|
|
|
private static final String ABOUT_HOME = "about:home";
|
|
|
|
|
2012-06-07 21:47:22 -07:00
|
|
|
public TabsTray(Context context, AttributeSet attrs) {
|
|
|
|
super(context, attrs);
|
|
|
|
mContext = context;
|
2012-03-23 12:00:17 -07:00
|
|
|
|
2012-10-31 08:28:29 -07:00
|
|
|
mCloseAnimationCount = 0;
|
|
|
|
mPendingClosedTabs = new ArrayList<View>();
|
|
|
|
|
2012-12-20 14:35:03 -08:00
|
|
|
setItemsCanFocus(true);
|
2012-07-09 16:19:18 -07:00
|
|
|
|
2012-12-18 16:44:37 -08:00
|
|
|
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);
|
2012-12-20 14:35:03 -08:00
|
|
|
setAdapter(mTabsAdapter);
|
2012-07-09 16:19:18 -07:00
|
|
|
|
2012-12-20 14:35:03 -08:00
|
|
|
mSwipeListener = new TabSwipeGestureListener();
|
|
|
|
setOnTouchListener(mSwipeListener);
|
|
|
|
setOnScrollListener(mSwipeListener.makeScrollListener());
|
2012-04-25 17:09:12 -07:00
|
|
|
|
2012-12-20 14:35:03 -08:00
|
|
|
setRecyclerListener(new RecyclerListener() {
|
2012-04-30 14:09:41 -07:00
|
|
|
@Override
|
|
|
|
public void onMovedToScrapHeap(View view) {
|
2012-05-01 10:50:27 -07:00
|
|
|
TabRow row = (TabRow) view.getTag();
|
|
|
|
row.thumbnail.setImageDrawable(null);
|
2012-04-30 14:09:41 -07:00
|
|
|
}
|
|
|
|
});
|
2012-06-07 21:47:22 -07:00
|
|
|
}
|
2011-11-18 10:28:17 -08:00
|
|
|
|
2012-06-07 21:47:22 -07:00
|
|
|
@Override
|
|
|
|
public ViewGroup getLayout() {
|
|
|
|
return this;
|
|
|
|
}
|
2012-03-09 10:42:43 -08:00
|
|
|
|
2012-07-27 23:31:40 -07:00
|
|
|
@Override
|
|
|
|
public void setTabsPanel(TabsPanel panel) {
|
|
|
|
mTabsPanel = panel;
|
|
|
|
}
|
|
|
|
|
2012-06-07 21:47:22 -07:00
|
|
|
@Override
|
|
|
|
public void show() {
|
2012-03-15 12:01:52 -07:00
|
|
|
Tabs.getInstance().refreshThumbnails();
|
2012-07-09 16:19:18 -07:00
|
|
|
Tabs.registerOnTabsChangedListener(mTabsAdapter);
|
|
|
|
mTabsAdapter.refreshTabsData();
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-06-07 21:47:22 -07:00
|
|
|
public void hide() {
|
2012-07-09 16:19:18 -07:00
|
|
|
Tabs.unregisterOnTabsChangedListener(mTabsAdapter);
|
2012-06-07 21:47:22 -07:00
|
|
|
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Tab:Screenshot:Cancel",""));
|
|
|
|
mTabsAdapter.clear();
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
|
2012-11-28 16:29:08 -08:00
|
|
|
private void autoHidePanel() {
|
2012-07-27 23:31:40 -07:00
|
|
|
mTabsPanel.autoHidePanel();
|
2012-03-09 10:42:43 -08:00
|
|
|
}
|
|
|
|
|
2012-05-01 10:50:27 -07:00
|
|
|
// ViewHolder for a row in the list
|
|
|
|
private class TabRow {
|
|
|
|
int id;
|
|
|
|
TextView title;
|
|
|
|
ImageView thumbnail;
|
|
|
|
ImageButton close;
|
2012-06-07 21:47:22 -07:00
|
|
|
LinearLayout info;
|
2012-05-01 10:50:27 -07:00
|
|
|
|
|
|
|
public TabRow(View view) {
|
2012-06-07 21:47:22 -07:00
|
|
|
info = (LinearLayout) view;
|
2012-05-01 10:50:27 -07:00
|
|
|
title = (TextView) view.findViewById(R.id.title);
|
|
|
|
thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
|
|
|
|
close = (ImageButton) view.findViewById(R.id.close);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-09 10:42:43 -08:00
|
|
|
// Adapter to bind tabs into a list
|
2012-07-09 16:19:18 -07:00
|
|
|
private class TabsAdapter extends BaseAdapter implements Tabs.OnTabsChangedListener {
|
2012-05-01 10:50:27 -07:00
|
|
|
private Context mContext;
|
2012-12-18 16:44:37 -08:00
|
|
|
private boolean mIsPrivate;
|
2012-05-01 10:50:27 -07:00
|
|
|
private ArrayList<Tab> mTabs;
|
|
|
|
private LayoutInflater mInflater;
|
|
|
|
private Button.OnClickListener mOnCloseClickListener;
|
|
|
|
|
2012-12-18 16:44:37 -08:00
|
|
|
public TabsAdapter(Context context, boolean isPrivate) {
|
2011-11-18 10:28:17 -08:00
|
|
|
mContext = context;
|
2011-11-21 12:21:10 -08:00
|
|
|
mInflater = LayoutInflater.from(mContext);
|
2012-12-18 16:44:37 -08:00
|
|
|
mIsPrivate = isPrivate;
|
2012-03-09 10:42:43 -08:00
|
|
|
|
2011-11-22 12:32:45 -08:00
|
|
|
mOnCloseClickListener = new Button.OnClickListener() {
|
|
|
|
public void onClick(View v) {
|
2012-06-05 13:29:37 -07:00
|
|
|
TabRow tab = (TabRow) v.getTag();
|
2012-10-12 05:22:03 -07:00
|
|
|
animateClose(tab.info, tab.info.getWidth());
|
2011-11-22 12:32:45 -08:00
|
|
|
}
|
|
|
|
};
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
|
2012-07-09 16:19:18 -07:00
|
|
|
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:
|
2012-12-20 14:35:03 -08:00
|
|
|
View view = TabsTray.this.getChildAt(getPositionForTab(tab) - TabsTray.this.getFirstVisiblePosition());
|
2012-07-09 16:19:18 -07:00
|
|
|
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>();
|
2012-07-12 11:09:47 -07:00
|
|
|
|
2012-08-09 13:52:58 -07:00
|
|
|
Iterable<Tab> tabs = Tabs.getInstance().getTabsInOrder();
|
|
|
|
for (Tab tab : tabs) {
|
2012-12-18 16:44:37 -08:00
|
|
|
if (tab.isPrivate() == mIsPrivate)
|
|
|
|
mTabs.add(tab);
|
2012-07-09 16:19:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
|
|
|
if (selected == -1)
|
|
|
|
return;
|
|
|
|
|
2012-12-20 14:35:03 -08:00
|
|
|
TabsTray.this.setSelection(selected);
|
2012-07-09 16:19:18 -07:00
|
|
|
}
|
|
|
|
|
2012-06-07 21:47:22 -07:00
|
|
|
public void clear() {
|
|
|
|
mTabs = null;
|
2012-07-09 16:19:18 -07:00
|
|
|
notifyDataSetChanged(); // Be sure to call this whenever mTabs changes.
|
2012-06-07 21:47:22 -07:00
|
|
|
}
|
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
public int getCount() {
|
2012-06-07 21:47:22 -07:00
|
|
|
return (mTabs == null ? 0 : mTabs.size());
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
2012-03-09 10:42:43 -08:00
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
public Tab getItem(int position) {
|
|
|
|
return mTabs.get(position);
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getItemId(int position) {
|
|
|
|
return position;
|
|
|
|
}
|
|
|
|
|
2012-07-09 16:19:18 -07:00
|
|
|
private int getPositionForTab(Tab tab) {
|
2011-11-18 10:28:17 -08:00
|
|
|
if (mTabs == null || tab == null)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return mTabs.indexOf(tab);
|
|
|
|
}
|
|
|
|
|
2012-07-09 16:19:18 -07:00
|
|
|
private void removeTab(Tab tab) {
|
2012-12-26 12:56:34 -08:00
|
|
|
if (tab.isPrivate() == mIsPrivate && mTabs != null) {
|
2012-12-18 16:44:37 -08:00
|
|
|
mTabs.remove(tab);
|
|
|
|
notifyDataSetChanged(); // Be sure to call this whenever mTabs changes.
|
|
|
|
}
|
2012-02-13 16:50:09 -08:00
|
|
|
}
|
|
|
|
|
2012-07-09 16:19:18 -07:00
|
|
|
private void assignValues(TabRow row, Tab tab) {
|
2012-05-01 10:50:27 -07:00
|
|
|
if (row == null || tab == null)
|
2011-11-18 10:28:17 -08:00
|
|
|
return;
|
|
|
|
|
2012-05-01 10:50:27 -07:00
|
|
|
row.id = tab.getId();
|
2011-11-18 10:28:17 -08:00
|
|
|
|
2011-12-14 14:31:39 -08:00
|
|
|
Drawable thumbnailImage = tab.getThumbnail();
|
|
|
|
if (thumbnailImage != null)
|
2012-05-01 10:50:27 -07:00
|
|
|
row.thumbnail.setImageDrawable(thumbnailImage);
|
2012-03-19 12:36:18 -07:00
|
|
|
else if (TextUtils.equals(tab.getURL(), ABOUT_HOME))
|
2012-05-01 10:50:27 -07:00
|
|
|
row.thumbnail.setImageResource(R.drawable.abouthome_thumbnail);
|
2011-11-18 10:28:17 -08:00
|
|
|
else
|
2012-05-01 10:50:27 -07:00
|
|
|
row.thumbnail.setImageResource(R.drawable.tab_thumbnail_default);
|
2011-12-14 14:31:39 -08:00
|
|
|
|
|
|
|
if (Tabs.getInstance().isSelectedTab(tab))
|
2012-06-07 21:47:22 -07:00
|
|
|
row.info.setBackgroundResource(R.drawable.tabs_tray_active_selector);
|
2012-05-01 10:50:27 -07:00
|
|
|
else
|
2012-06-07 21:47:22 -07:00
|
|
|
row.info.setBackgroundResource(R.drawable.tabs_tray_default_selector);
|
2011-11-18 10:28:17 -08:00
|
|
|
|
2012-05-01 10:50:27 -07:00
|
|
|
row.title.setText(tab.getDisplayTitle());
|
|
|
|
|
2012-06-05 13:29:37 -07:00
|
|
|
row.close.setTag(row);
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public View getView(int position, View convertView, ViewGroup parent) {
|
2012-05-01 10:50:27 -07:00
|
|
|
TabRow row;
|
2011-11-18 10:28:17 -08:00
|
|
|
|
2012-05-01 10:50:27 -07:00
|
|
|
if (convertView == null) {
|
|
|
|
convertView = mInflater.inflate(R.layout.tabs_row, null);
|
2011-11-18 10:28:17 -08:00
|
|
|
|
2012-05-01 10:50:27 -07:00
|
|
|
row = new TabRow(convertView);
|
|
|
|
row.close.setOnClickListener(mOnCloseClickListener);
|
2012-03-09 10:42:43 -08:00
|
|
|
|
2012-05-01 10:50:27 -07:00
|
|
|
convertView.setTag(row);
|
2011-11-18 10:28:17 -08:00
|
|
|
} else {
|
2012-05-01 10:50:27 -07:00
|
|
|
row = (TabRow) convertView.getTag();
|
2012-12-18 23:03:25 -08:00
|
|
|
row.close.setVisibility(View.VISIBLE);
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|
|
|
|
|
2012-05-01 10:50:27 -07:00
|
|
|
Tab tab = mTabs.get(position);
|
|
|
|
assignValues(row, tab);
|
|
|
|
|
2011-11-18 10:28:17 -08:00
|
|
|
return convertView;
|
|
|
|
}
|
|
|
|
}
|
2012-06-05 13:29:37 -07:00
|
|
|
|
2012-10-12 05:22:03 -07:00
|
|
|
private void animateClose(final View view, int x) {
|
|
|
|
PropertyAnimator animator = new PropertyAnimator(ANIMATION_DURATION);
|
|
|
|
animator.attach(view, Property.ALPHA, 0);
|
|
|
|
animator.attach(view, Property.TRANSLATION_X, x);
|
|
|
|
|
2012-10-31 08:28:29 -07:00
|
|
|
mCloseAnimationCount++;
|
|
|
|
mPendingClosedTabs.add(view);
|
2012-10-12 05:22:03 -07:00
|
|
|
|
2012-10-12 05:22:03 -07:00
|
|
|
animator.setPropertyAnimationListener(new PropertyAnimator.PropertyAnimationListener() {
|
|
|
|
public void onPropertyAnimationStart() { }
|
|
|
|
public void onPropertyAnimationEnd() {
|
2012-10-31 08:28:29 -07:00
|
|
|
mCloseAnimationCount--;
|
|
|
|
if (mCloseAnimationCount > 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (View pendingView : mPendingClosedTabs) {
|
|
|
|
animateFinishClose(pendingView);
|
|
|
|
}
|
|
|
|
|
|
|
|
mPendingClosedTabs.clear();
|
2012-10-12 05:22:03 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-11-28 16:29:08 -08:00
|
|
|
if (mTabsAdapter.getCount() == 1)
|
|
|
|
autoHidePanel();
|
|
|
|
|
2012-10-12 05:22:03 -07:00
|
|
|
animator.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void animateFinishClose(final View view) {
|
|
|
|
PropertyAnimator animator = new PropertyAnimator(ANIMATION_DURATION);
|
|
|
|
animator.attach(view, Property.HEIGHT, 1);
|
|
|
|
|
2012-10-12 05:22:03 -07:00
|
|
|
TabRow tab = (TabRow)view.getTag();
|
|
|
|
final int tabId = tab.id;
|
2012-10-12 05:22:03 -07:00
|
|
|
final int originalHeight = view.getHeight();
|
2012-10-12 05:22:03 -07:00
|
|
|
|
|
|
|
animator.setPropertyAnimationListener(new PropertyAnimator.PropertyAnimationListener() {
|
|
|
|
public void onPropertyAnimationStart() { }
|
|
|
|
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);
|
|
|
|
proxy.setTranslationX(0);
|
2012-10-12 05:22:03 -07:00
|
|
|
proxy.setHeight(originalHeight);
|
2012-10-12 05:22:03 -07:00
|
|
|
|
|
|
|
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);
|
|
|
|
animator.attach(view, Property.TRANSLATION_X, 0);
|
2012-11-06 11:24:14 -08:00
|
|
|
|
|
|
|
animator.setPropertyAnimationListener(new PropertyAnimator.PropertyAnimationListener() {
|
|
|
|
public void onPropertyAnimationStart() { }
|
|
|
|
public void onPropertyAnimationEnd() {
|
|
|
|
TabRow tab = (TabRow) view.getTag();
|
|
|
|
tab.close.setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-10-12 05:22:03 -07:00
|
|
|
animator.start();
|
2012-06-05 13:29:37 -07:00
|
|
|
}
|
|
|
|
|
2012-10-12 05:22:03 -07:00
|
|
|
private class TabSwipeGestureListener implements View.OnTouchListener {
|
2012-12-19 10:06:13 -08:00
|
|
|
// 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;
|
|
|
|
|
2012-10-12 05:22:03 -07:00
|
|
|
private int mSwipeThreshold;
|
|
|
|
private int mMinFlingVelocity;
|
2012-12-19 10:06:13 -08:00
|
|
|
|
2012-10-12 05:22:03 -07:00
|
|
|
private int mMaxFlingVelocity;
|
|
|
|
private VelocityTracker mVelocityTracker;
|
|
|
|
|
|
|
|
private int mListWidth = 1;
|
|
|
|
|
|
|
|
private View mSwipeView;
|
|
|
|
private AnimatorProxy mSwipeProxy;
|
|
|
|
private int mSwipeViewPosition;
|
|
|
|
private Runnable mPendingCheckForTap;
|
|
|
|
|
|
|
|
private float mSwipeStart;
|
|
|
|
private boolean mSwiping;
|
|
|
|
private boolean mEnabled;
|
|
|
|
|
2012-12-20 14:35:03 -08:00
|
|
|
public TabSwipeGestureListener() {
|
2012-10-12 05:22:03 -07:00
|
|
|
mSwipeView = null;
|
|
|
|
mSwipeProxy = null;
|
|
|
|
mSwipeViewPosition = ListView.INVALID_POSITION;
|
|
|
|
mSwiping = false;
|
|
|
|
mEnabled = true;
|
|
|
|
|
2012-12-20 14:35:03 -08:00
|
|
|
ViewConfiguration vc = ViewConfiguration.get(TabsTray.this.getContext());
|
2012-10-12 05:22:03 -07:00
|
|
|
mSwipeThreshold = vc.getScaledTouchSlop();
|
2012-12-19 10:06:13 -08:00
|
|
|
mMinFlingVelocity = (int) (getContext().getResources().getDisplayMetrics().density * MIN_VELOCITY);
|
2012-10-12 05:22:03 -07:00
|
|
|
mMaxFlingVelocity = vc.getScaledMaximumFlingVelocity();
|
|
|
|
}
|
2012-06-05 13:29:37 -07:00
|
|
|
|
2012-10-12 05:22:03 -07:00
|
|
|
public void setEnabled(boolean enabled) {
|
|
|
|
mEnabled = enabled;
|
2012-06-05 13:29:37 -07:00
|
|
|
}
|
|
|
|
|
2012-10-12 05:22:03 -07:00
|
|
|
public AbsListView.OnScrollListener makeScrollListener() {
|
|
|
|
return new AbsListView.OnScrollListener() {
|
|
|
|
@Override
|
|
|
|
public void onScrollStateChanged(AbsListView absListView, int scrollState) {
|
|
|
|
setEnabled(scrollState != AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onScroll(AbsListView absListView, int i, int i1, int i2) {
|
|
|
|
}
|
|
|
|
};
|
2012-06-21 09:53:54 -07:00
|
|
|
}
|
|
|
|
|
2012-06-05 13:29:37 -07:00
|
|
|
@Override
|
2012-10-12 05:22:03 -07:00
|
|
|
public boolean onTouch(View view, MotionEvent e) {
|
|
|
|
if (!mEnabled)
|
2012-06-05 13:29:37 -07:00
|
|
|
return false;
|
|
|
|
|
2012-10-12 05:22:03 -07:00
|
|
|
if (mListWidth < 2)
|
2012-12-20 14:35:03 -08:00
|
|
|
mListWidth = TabsTray.this.getWidth();
|
2012-10-12 05:22:03 -07:00
|
|
|
|
|
|
|
switch (e.getActionMasked()) {
|
|
|
|
case MotionEvent.ACTION_DOWN: {
|
|
|
|
// Check if we should set pressed state on the
|
|
|
|
// touched view after a standard delay.
|
|
|
|
triggerCheckForTap();
|
2012-06-05 13:29:37 -07:00
|
|
|
|
2012-10-12 05:22:03 -07:00
|
|
|
// Find out which view is being touched
|
|
|
|
mSwipeView = findViewAt(e.getRawX(), e.getRawY());
|
|
|
|
|
|
|
|
if (mSwipeView != null) {
|
|
|
|
mSwipeStart = e.getRawX();
|
2012-12-20 14:35:03 -08:00
|
|
|
mSwipeViewPosition = TabsTray.this.getPositionForView(mSwipeView);
|
2012-10-12 05:22:03 -07:00
|
|
|
|
|
|
|
mVelocityTracker = VelocityTracker.obtain();
|
|
|
|
mVelocityTracker.addMovement(e);
|
2012-06-05 13:29:37 -07:00
|
|
|
}
|
2012-10-12 05:22:03 -07:00
|
|
|
|
|
|
|
view.onTouchEvent(e);
|
|
|
|
return true;
|
2012-06-05 13:29:37 -07:00
|
|
|
}
|
|
|
|
|
2012-10-12 05:22:03 -07:00
|
|
|
case MotionEvent.ACTION_UP: {
|
|
|
|
if (mSwipeView == null)
|
|
|
|
break;
|
2012-06-05 13:29:37 -07:00
|
|
|
|
2012-10-18 06:00:15 -07:00
|
|
|
cancelCheckForTap();
|
2012-10-12 05:22:03 -07:00
|
|
|
mSwipeView.setPressed(false);
|
2012-06-05 13:29:37 -07:00
|
|
|
|
2012-10-12 05:22:03 -07:00
|
|
|
if (!mSwiping) {
|
|
|
|
TabRow tab = (TabRow) mSwipeView.getTag();
|
|
|
|
Tabs.getInstance().selectTab(tab.id);
|
|
|
|
autoHidePanel();
|
|
|
|
break;
|
|
|
|
}
|
2012-06-05 13:29:37 -07:00
|
|
|
|
2012-10-12 05:22:03 -07:00
|
|
|
float deltaX = mSwipeProxy.getTranslationX();
|
|
|
|
|
|
|
|
mVelocityTracker.addMovement(e);
|
|
|
|
mVelocityTracker.computeCurrentVelocity(1000, mMaxFlingVelocity);
|
|
|
|
|
|
|
|
float velocityX = Math.abs(mVelocityTracker.getXVelocity());
|
|
|
|
float velocityY = Math.abs(mVelocityTracker.getYVelocity());
|
|
|
|
|
|
|
|
boolean dismiss = false;
|
|
|
|
boolean dismissRight = false;
|
|
|
|
|
|
|
|
if (Math.abs(deltaX) > mListWidth / 2) {
|
|
|
|
dismiss = true;
|
|
|
|
dismissRight = (deltaX > 0);
|
|
|
|
} else if (mMinFlingVelocity <= velocityX && velocityX <= mMaxFlingVelocity
|
|
|
|
&& velocityY < velocityX) {
|
2012-11-06 11:10:10 -08:00
|
|
|
dismiss = mSwiping && (deltaX * mVelocityTracker.getXVelocity() > 0);
|
2012-10-12 05:22:03 -07:00
|
|
|
dismissRight = (mVelocityTracker.getXVelocity() > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dismiss)
|
|
|
|
animateClose(mSwipeView, (dismissRight ? mListWidth : -mListWidth));
|
|
|
|
else
|
|
|
|
animateCancel(mSwipeView);
|
|
|
|
|
|
|
|
mVelocityTracker = null;
|
|
|
|
mSwipeView = null;
|
|
|
|
mSwipeViewPosition = ListView.INVALID_POSITION;
|
|
|
|
mSwipeProxy = null;
|
|
|
|
|
|
|
|
mSwipeStart = 0;
|
|
|
|
mSwiping = false;
|
|
|
|
|
|
|
|
break;
|
2012-06-05 13:29:37 -07:00
|
|
|
}
|
|
|
|
|
2012-10-12 05:22:03 -07:00
|
|
|
case MotionEvent.ACTION_MOVE: {
|
|
|
|
if (mSwipeView == null)
|
|
|
|
break;
|
2012-06-05 13:29:37 -07:00
|
|
|
|
2012-10-12 05:22:03 -07:00
|
|
|
mVelocityTracker.addMovement(e);
|
2012-06-05 13:29:37 -07:00
|
|
|
|
2012-10-12 05:22:03 -07:00
|
|
|
float deltaX = e.getRawX() - mSwipeStart;
|
|
|
|
if (Math.abs(deltaX) > mSwipeThreshold) {
|
|
|
|
// If we're actually swiping, make sure we don't
|
|
|
|
// set pressed state on the swiped view.
|
|
|
|
cancelCheckForTap();
|
|
|
|
|
|
|
|
mSwiping = true;
|
2012-12-20 14:35:03 -08:00
|
|
|
TabsTray.this.requestDisallowInterceptTouchEvent(true);
|
2012-10-12 05:22:03 -07:00
|
|
|
|
|
|
|
TabRow tab = (TabRow) mSwipeView.getTag();
|
2012-11-06 11:24:14 -08:00
|
|
|
tab.close.setVisibility(View.INVISIBLE);
|
2012-06-05 13:29:37 -07:00
|
|
|
|
2012-10-12 05:22:03 -07:00
|
|
|
// 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));
|
2012-12-20 14:35:03 -08:00
|
|
|
TabsTray.this.onTouchEvent(cancelEvent);
|
2012-10-12 05:22:03 -07:00
|
|
|
|
|
|
|
mSwipeProxy = AnimatorProxy.create(mSwipeView);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mSwiping) {
|
2012-11-06 11:10:10 -08:00
|
|
|
mSwipeProxy.setTranslationX(deltaX);
|
|
|
|
mSwipeProxy.setAlpha(Math.max(0.1f, Math.min(1f,
|
|
|
|
1f - 2f * Math.abs(deltaX) / mListWidth)));
|
2012-10-12 05:22:03 -07:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2012-06-05 13:29:37 -07:00
|
|
|
}
|
|
|
|
|
2012-10-12 05:22:03 -07:00
|
|
|
return false;
|
2012-06-05 13:29:37 -07:00
|
|
|
}
|
|
|
|
|
2012-10-12 05:22:03 -07:00
|
|
|
private View findViewAt(float rawX, float rawY) {
|
|
|
|
Rect rect = new Rect();
|
|
|
|
|
|
|
|
int[] listViewCoords = new int[2];
|
2012-12-20 14:35:03 -08:00
|
|
|
TabsTray.this.getLocationOnScreen(listViewCoords);
|
2012-10-12 05:22:03 -07:00
|
|
|
|
|
|
|
int x = (int) rawX - listViewCoords[0];
|
|
|
|
int y = (int) rawY - listViewCoords[1];
|
|
|
|
|
2012-12-20 14:35:03 -08:00
|
|
|
for (int i = 0; i < TabsTray.this.getChildCount(); i++) {
|
|
|
|
View child = TabsTray.this.getChildAt(i);
|
2012-10-12 05:22:03 -07:00
|
|
|
child.getHitRect(rect);
|
|
|
|
|
|
|
|
if (rect.contains(x, y))
|
|
|
|
return child;
|
2012-06-05 13:29:37 -07:00
|
|
|
}
|
2012-10-12 05:22:03 -07:00
|
|
|
|
2012-06-05 13:29:37 -07:00
|
|
|
return null;
|
|
|
|
}
|
2012-10-12 05:22:03 -07:00
|
|
|
|
|
|
|
private void triggerCheckForTap() {
|
|
|
|
if (mPendingCheckForTap == null)
|
|
|
|
mPendingCheckForTap = new CheckForTap();
|
|
|
|
|
2012-12-20 14:35:03 -08:00
|
|
|
TabsTray.this.postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
|
2012-10-12 05:22:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private void cancelCheckForTap() {
|
|
|
|
if (mPendingCheckForTap == null)
|
|
|
|
return;
|
|
|
|
|
2012-12-20 14:35:03 -08:00
|
|
|
TabsTray.this.removeCallbacks(mPendingCheckForTap);
|
2012-10-12 05:22:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private class CheckForTap implements Runnable {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (!mSwiping && mSwipeView != null && mEnabled)
|
|
|
|
mSwipeView.setPressed(true);
|
|
|
|
}
|
|
|
|
}
|
2012-06-05 13:29:37 -07:00
|
|
|
}
|
2011-11-18 10:28:17 -08:00
|
|
|
}
|