1 /* 2 * Copyright (C) 2017 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 package com.android.documentsui.dirlist; 17 18 import static android.support.v4.util.Preconditions.checkArgument; 19 20 import android.support.annotation.Nullable; 21 import android.support.v7.widget.RecyclerView; 22 import android.support.v7.widget.RecyclerView.OnItemTouchListener; 23 import android.view.MotionEvent; 24 25 import com.android.documentsui.base.EventHandler; 26 import com.android.documentsui.base.Events; 27 import com.android.documentsui.selection.BandSelectionHelper; 28 import com.android.documentsui.selection.ItemDetailsLookup; 29 import com.android.documentsui.selection.TouchEventRouter; 30 31 /** 32 * OnItemTouchListener that helps enable support for drag/drop functionality 33 * in DirectoryFragment. 34 * 35 * <p>This class takes an OnItemTouchListener that is presumably the 36 * one provided by {@link BandSelectionHelper#getListener()}, and 37 * is used in conjunction with the {@link TouchEventRouter}. 38 * 39 * <p>See DirectoryFragment for more details on how this is glued 40 * into DocumetnsUI event handling to make the magic happen. 41 */ 42 class MouseDragEventInterceptor implements OnItemTouchListener { 43 44 private final ItemDetailsLookup mEventDetailsLookup; 45 private final EventHandler<MotionEvent> mMouseDragListener; 46 private final @Nullable OnItemTouchListener mDelegate; 47 48 public MouseDragEventInterceptor( 49 ItemDetailsLookup eventDetailsLookup, 50 EventHandler<MotionEvent> mouseDragListener, 51 @Nullable OnItemTouchListener delegate) { 52 53 checkArgument(eventDetailsLookup != null); 54 checkArgument(mouseDragListener != null); 55 56 mEventDetailsLookup = eventDetailsLookup; 57 mMouseDragListener = mouseDragListener; 58 mDelegate = delegate; 59 } 60 61 @Override 62 public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) { 63 if (Events.isMouseDragEvent(e) && mEventDetailsLookup.inItemDragRegion(e)) { 64 return mMouseDragListener.accept(e); 65 } else if (mDelegate != null) { 66 return mDelegate.onInterceptTouchEvent(rv, e); 67 } 68 return false; 69 } 70 71 @Override 72 public void onTouchEvent(RecyclerView rv, MotionEvent e) { 73 if (mDelegate != null) { 74 mDelegate.onTouchEvent(rv, e); 75 } 76 } 77 78 @Override 79 public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {} 80 } 81