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.selection; 17 18 import android.support.v7.widget.RecyclerView; 19 import android.view.MotionEvent; 20 21 import javax.annotation.Nullable; 22 23 /** 24 * Provides event handlers w/ access to details about documents details 25 * view items Documents in the UI (RecyclerView). 26 */ 27 public abstract class ItemDetailsLookup { 28 29 /** @return true if there is an item under the finger/cursor. */ overItem(MotionEvent e)30 public abstract boolean overItem(MotionEvent e); 31 32 /** @return true if there is an item w/ a stable ID under the finger/cursor. */ overStableItem(MotionEvent e)33 public abstract boolean overStableItem(MotionEvent e); 34 35 /** 36 * @return true if the event is over an area that can be dragged via touch 37 * or via mouse. List items have a white area that is not draggable. 38 */ inItemDragRegion(MotionEvent e)39 public abstract boolean inItemDragRegion(MotionEvent e); 40 41 /** 42 * @return true if the event is in the "selection hot spot" region. 43 * The hot spot region instantly selects in touch mode, vs launches. 44 */ inItemSelectRegion(MotionEvent e)45 public abstract boolean inItemSelectRegion(MotionEvent e); 46 47 /** 48 * @return the adapter position of the item under the finger/cursor. 49 */ getItemPosition(MotionEvent e)50 public abstract int getItemPosition(MotionEvent e); 51 52 /** 53 * @return the DocumentDetails for the item under the event, or null. 54 */ getItemDetails(MotionEvent e)55 public abstract @Nullable ItemDetails getItemDetails(MotionEvent e); 56 57 /** 58 * Abstract class providing helper classes with access to information about 59 * RecyclerView item associated with a MotionEvent. 60 */ 61 public static abstract class ItemDetails { 62 hasPosition()63 public boolean hasPosition() { 64 return getPosition() > RecyclerView.NO_POSITION; 65 } 66 getPosition()67 public abstract int getPosition(); 68 hasStableId()69 public boolean hasStableId() { 70 return getStableId() != null; 71 } 72 getStableId()73 public abstract @Nullable String getStableId(); 74 75 /** 76 * @return The view type of this ViewHolder. 77 */ getItemViewType()78 public abstract int getItemViewType(); 79 80 /** 81 * @return true if the event is in an area of the item that should be 82 * directly interpreted as a user wishing to select the item. This 83 * is useful for checkboxes and other UI affordances focused on enabling 84 * selection. 85 */ inSelectionHotspot(MotionEvent e)86 public boolean inSelectionHotspot(MotionEvent e) { 87 return false; 88 } 89 90 /** 91 * Events in the drag region will not be processed as selection events. This 92 * allows the client to implement custom handling for events related to drag 93 * and drop. 94 */ inDragRegion(MotionEvent e)95 public boolean inDragRegion(MotionEvent e) { 96 return false; 97 } 98 99 @Override equals(Object obj)100 public boolean equals(Object obj) { 101 if (obj instanceof ItemDetails) { 102 return equals((ItemDetails) obj); 103 } 104 return false; 105 } 106 equals(ItemDetails other)107 private boolean equals(ItemDetails other) { 108 return this.getPosition() == other.getPosition() 109 && this.getStableId() == other.getStableId(); 110 } 111 112 @Override hashCode()113 public int hashCode() { 114 return getPosition() >>> 8; 115 } 116 } 117 } 118