From 41585d5e062a9db9c567b7e93bd5d1e14453b340 Mon Sep 17 00:00:00 2001 From: Steve McKay Date: Thu, 21 Jan 2016 15:10:39 -0800 Subject: [PATCH] Make Drag and Drop mouse specific behavior for now. We'll implement "photos style selection" for touch behaviors. Autosubmit? Yes, please? Bug: 20556237 Change-Id: I60dfc2504f2f9fb1dec6e8e311122dd0e2f484fe --- .../documentsui/dirlist/DirectoryFragment.java | 101 +++++++++++++-------- 1 file changed, 61 insertions(+), 40 deletions(-) diff --git a/packages/DocumentsUI/src/com/android/documentsui/dirlist/DirectoryFragment.java b/packages/DocumentsUI/src/com/android/documentsui/dirlist/DirectoryFragment.java index d220f40cc751..ba96b0e457b1 100644 --- a/packages/DocumentsUI/src/com/android/documentsui/dirlist/DirectoryFragment.java +++ b/packages/DocumentsUI/src/com/android/documentsui/dirlist/DirectoryFragment.java @@ -147,6 +147,7 @@ public class DirectoryFragment extends Fragment implements DocumentsAdapter.Envi private View mEmptyView; private RecyclerView mRecView; + private ListeningGestureDetector mGestureDetector; private int mType = TYPE_NORMAL; private String mStateKey; @@ -300,24 +301,9 @@ public class DirectoryFragment extends Fragment implements DocumentsAdapter.Envi mRecView.setAdapter(mAdapter); - GestureListener listener = new GestureListener(); - final GestureDetector detector = new GestureDetector(this.getContext(), listener); - detector.setOnDoubleTapListener(listener); + mGestureDetector = new ListeningGestureDetector(this.getContext(), new GestureListener()); - mRecView.addOnItemTouchListener( - new OnItemTouchListener() { - @Override - public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) { - detector.onTouchEvent(e); - return false; - } - - @Override - public void onTouchEvent(RecyclerView rv, MotionEvent e) {} - - @Override - public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {} - }); + mRecView.addOnItemTouchListener(mGestureDetector); // TODO: instead of inserting the view into the constructor, extract listener-creation code // and set the listener on the view after the fact. Then the view doesn't need to be passed @@ -1129,11 +1115,7 @@ public class DirectoryFragment extends Fragment implements DocumentsAdapter.Envi view.setOnDragListener(mOnDragListener); } - // Temporary: attaching the listener to the title only. - // Attaching to the entire item conflicts with the item long click handler responsible - // for item selection. - final View title = view.findViewById(android.R.id.title); - title.setOnLongClickListener(mLongClickListener); + view.setOnLongClickListener(mLongClickListener); } private View.OnDragListener mOnDragListener = new View.OnDragListener() { @@ -1208,24 +1190,6 @@ public class DirectoryFragment extends Fragment implements DocumentsAdapter.Envi } } - private View.OnLongClickListener mLongClickListener = new View.OnLongClickListener() { - @Override - public boolean onLongClick(View v) { - final List docs = getDraggableDocuments(v); - if (docs.isEmpty()) { - return false; - } - v.startDrag( - getClipDataFromDocuments(docs), - new DrawableShadowBuilder(getDragShadowIcon(docs)), - null, - View.DRAG_FLAG_GLOBAL | View.DRAG_FLAG_GLOBAL_URI_READ | - View.DRAG_FLAG_GLOBAL_URI_WRITE - ); - return true; - } - }; - private List getDraggableDocuments(View currentItemView) { String modelId = getModelId(currentItemView); if (modelId == null) { @@ -1349,6 +1313,63 @@ public class DirectoryFragment extends Fragment implements DocumentsAdapter.Envi } } + private View.OnLongClickListener mLongClickListener = new View.OnLongClickListener() { + @Override + public boolean onLongClick(View v) { + if (mGestureDetector.mouseSpawnedLastEvent()) { + List docs = getDraggableDocuments(v); + if (docs.isEmpty()) { + return false; + } + v.startDrag( + getClipDataFromDocuments(docs), + new DrawableShadowBuilder(getDragShadowIcon(docs)), + null, + View.DRAG_FLAG_GLOBAL | View.DRAG_FLAG_GLOBAL_URI_READ | + View.DRAG_FLAG_GLOBAL_URI_WRITE + ); + return true; + } + + return false; + } + }; + + // Previously we listened to events with one class, only to bounce them forward + // to GestureDetector. We're still doing that here, but with a single class + // that reduces overall complexity in our glue code. + private static final class ListeningGestureDetector extends GestureDetector + implements OnItemTouchListener { + + private int mLastTool = -1; + + public ListeningGestureDetector(Context context, GestureListener listener) { + super(context, listener); + setOnDoubleTapListener(listener); + } + + boolean mouseSpawnedLastEvent() { + return Events.isMouseType(mLastTool); + } + + boolean touchSpawnedLastEvent() { + return Events.isTouchType(mLastTool); + } + + @Override + public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) { + mLastTool = e.getToolType(0); + onTouchEvent(e); // bounce this forward to our detecty heart + return false; + } + + @Override + public void onTouchEvent(RecyclerView rv, MotionEvent e) {} + + @Override + public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {} + } + /** * The gesture listener for items in the list/grid view. Interprets gestures and sends the * events to the target DocumentHolder, whence they are routed to the appropriate listener. -- 2.11.0