Home | History | Annotate | Download | only in dirlist
      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 android.support.annotation.Nullable;
     19 import android.support.v7.widget.RecyclerView;
     20 import android.support.v7.widget.RecyclerView.ViewHolder;
     21 import android.view.MotionEvent;
     22 import android.view.View;
     23 
     24 import com.android.documentsui.selection.ItemDetailsLookup;
     25 
     26 /**
     27  * Access to details of an item associated with a {@link MotionEvent} instance.
     28  */
     29 final class DocsItemDetailsLookup extends ItemDetailsLookup {
     30 
     31     private final RecyclerView mRecView;
     32 
     33     public DocsItemDetailsLookup(RecyclerView view) {
     34         mRecView = view;
     35     }
     36 
     37     @Override
     38     public boolean overItem(MotionEvent e) {
     39         return getItemPosition(e) != RecyclerView.NO_POSITION;
     40     }
     41 
     42     @Override
     43     public boolean overStableItem(MotionEvent e) {
     44     if (!overItem(e)) {
     45         return false;
     46     }
     47     ItemDetails details = getItemDetails(e);
     48     return details != null && details.hasStableId();
     49     }
     50 
     51     @Override
     52     public boolean inItemDragRegion(MotionEvent e) {
     53     if (!overItem(e)) {
     54         return false;
     55     }
     56     ItemDetails details = getItemDetails(e);
     57     return details != null && details.inDragRegion(e);
     58     }
     59 
     60     @Override
     61     public boolean inItemSelectRegion(MotionEvent e) {
     62     if (!overItem(e)) {
     63         return false;
     64     }
     65     ItemDetails details = getItemDetails(e);
     66     return details != null && details.inSelectionHotspot(e);
     67     }
     68 
     69     @Override
     70     public int getItemPosition(MotionEvent e) {
     71         View child = mRecView.findChildViewUnder(e.getX(), e.getY());
     72         return (child != null)
     73                 ? mRecView.getChildAdapterPosition(child)
     74                 : RecyclerView.NO_POSITION;
     75     }
     76 
     77     @Override
     78     public ItemDetails getItemDetails(MotionEvent e) {
     79         @Nullable DocumentHolder holder = getDocumentHolder(e);
     80         return holder == null ? null : holder.getItemDetails();
     81     }
     82 
     83     private @Nullable DocumentHolder getDocumentHolder(MotionEvent e) {
     84         View childView = mRecView.findChildViewUnder(e.getX(), e.getY());
     85         if (childView != null) {
     86             ViewHolder holder = mRecView.getChildViewHolder(childView);
     87             if (holder instanceof DocumentHolder) {
     88                 return (DocumentHolder) holder;
     89             }
     90         }
     91         return null;
     92     }
     93 }
     94