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 */
16package com.android.documentsui.selection.demo;
17
18import android.support.annotation.Nullable;
19import android.support.v7.widget.RecyclerView;
20import android.support.v7.widget.RecyclerView.ViewHolder;
21import android.view.MotionEvent;
22import android.view.View;
23
24import com.android.documentsui.selection.ItemDetailsLookup;
25
26/**
27 * Access to details of an item associated with a {@link MotionEvent} instance.
28 */
29final class DemoDetailsLookup extends ItemDetailsLookup {
30
31    private final RecyclerView mRecView;
32
33    public DemoDetailsLookup(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        return overItem(e) && getItemDetails(e).hasStableId();
45    }
46
47    @Override
48    public boolean inItemDragRegion(MotionEvent e) {
49        return overItem(e) && getItemDetails(e).inDragRegion(e);
50    }
51
52    @Override
53    public boolean inItemSelectRegion(MotionEvent e) {
54        return overItem(e) && getItemDetails(e).inSelectionHotspot(e);
55    }
56
57    @Override
58    public int getItemPosition(MotionEvent e) {
59        View child = mRecView.findChildViewUnder(e.getX(), e.getY());
60        return (child != null)
61                ? mRecView.getChildAdapterPosition(child)
62                : RecyclerView.NO_POSITION;
63    }
64
65    @Override
66    public ItemDetails getItemDetails(MotionEvent e) {
67        @Nullable DemoHolder holder = getDemoHolder(e);
68        return holder == null ? null : holder.getItemDetails();
69    }
70
71    private @Nullable DemoHolder getDemoHolder(MotionEvent e) {
72        View childView = mRecView.findChildViewUnder(e.getX(), e.getY());
73        if (childView != null) {
74            ViewHolder holder = mRecView.getChildViewHolder(childView);
75            if (holder instanceof DemoHolder) {
76                return (DemoHolder) holder;
77            }
78        }
79        return null;
80    }
81}
82