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.demo; 17 18 import android.graphics.Rect; 19 import android.support.v7.widget.RecyclerView; 20 import android.view.MotionEvent; 21 import android.widget.LinearLayout; 22 import android.widget.TextView; 23 24 import com.android.documentsui.R; 25 import com.android.documentsui.selection.ItemDetailsLookup.ItemDetails; 26 27 public final class DemoHolder extends RecyclerView.ViewHolder { 28 29 private final LinearLayout mContainer; 30 public final TextView mSelector; 31 public final TextView mLabel; 32 private final Details mDetails; 33 private DemoItem mItem; 34 DemoHolder(LinearLayout layout)35 DemoHolder(LinearLayout layout) { 36 super(layout); 37 mContainer = layout.findViewById(R.id.container); 38 mSelector = layout.findViewById(R.id.selector); 39 mLabel = layout.findViewById(R.id.label); 40 mDetails = new Details(this); 41 } 42 update(DemoItem demoItem)43 public void update(DemoItem demoItem) { 44 mItem = demoItem; 45 mLabel.setText(mItem.getName()); 46 } 47 setSelected(boolean selected)48 void setSelected(boolean selected) { 49 mContainer.setActivated(selected); 50 mSelector.setActivated(selected); 51 } 52 getStableId()53 public String getStableId() { 54 return mItem != null ? mItem.getId() : null; 55 } 56 inDragRegion(MotionEvent e)57 public boolean inDragRegion(MotionEvent e) { 58 // If itemView is activated = selected, then whole region is interactive 59 if (mContainer.isActivated()) { 60 return true; 61 } 62 63 if (inSelectRegion(e)) { 64 return true; 65 } 66 67 Rect rect = new Rect(); 68 mLabel.getPaint().getTextBounds( 69 mLabel.getText().toString(), 0, mLabel.getText().length(), rect); 70 71 // If the tap occurred inside the text, these are interactive spots. 72 return rect.contains((int) e.getRawX(), (int) e.getRawY()); 73 } 74 inSelectRegion(MotionEvent e)75 public boolean inSelectRegion(MotionEvent e) { 76 Rect iconRect = new Rect(); 77 mSelector.getGlobalVisibleRect(iconRect); 78 return iconRect.contains((int) e.getRawX(), (int) e.getRawY()); 79 } 80 getItemDetails()81 Details getItemDetails() { 82 return mDetails; 83 } 84 85 private static final class Details extends ItemDetails { 86 87 private final DemoHolder mHolder; 88 Details(DemoHolder holder)89 Details(DemoHolder holder) { 90 mHolder = holder; 91 } 92 93 @Override getPosition()94 public int getPosition() { 95 return mHolder.getAdapterPosition(); 96 } 97 98 @Override getStableId()99 public String getStableId() { 100 return mHolder.getStableId(); 101 } 102 103 @Override getItemViewType()104 public int getItemViewType() { 105 return mHolder.getItemViewType(); 106 } 107 108 @Override inDragRegion(MotionEvent e)109 public boolean inDragRegion(MotionEvent e) { 110 return mHolder.inDragRegion(e); 111 } 112 113 @Override inSelectionHotspot(MotionEvent e)114 public boolean inSelectionHotspot(MotionEvent e) { 115 return mHolder.inSelectRegion(e); 116 } 117 } 118 }