1 /*
2  * Copyright (C) 2015 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 
17 package com.android.documentsui.dirlist;
18 
19 import android.annotation.ColorInt;
20 import android.content.Context;
21 import android.database.Cursor;
22 import android.graphics.Rect;
23 import android.os.Build;
24 import android.support.v7.widget.RecyclerView;
25 import android.text.TextUtils;
26 import android.view.KeyEvent;
27 import android.view.LayoutInflater;
28 import android.view.View;
29 import android.view.ViewGroup;
30 import android.view.ViewPropertyAnimator;
31 import android.widget.FrameLayout;
32 import android.widget.ImageView;
33 
34 import com.android.documentsui.R;
35 import com.android.documentsui.base.DebugFlags;
36 import com.android.documentsui.base.DocumentInfo;
37 import com.android.documentsui.base.Events.InputEvent;
38 import com.android.documentsui.base.Shared;
39 import com.android.documentsui.ui.DocumentDebugInfo;
40 
41 import javax.annotation.Nullable;
42 
43 public abstract class DocumentHolder
44         extends RecyclerView.ViewHolder
45         implements View.OnKeyListener, DocumentDetails {
46 
47     static final float DISABLED_ALPHA = 0.3f;
48 
49     protected final Context mContext;
50     protected final @ColorInt int mDefaultBgColor;
51     protected final @ColorInt int mSelectedBgColor;
52     protected final @ColorInt int mDroppableBgColor;
53     protected final @ColorInt int mNotDroppableBgColor;
54 
55     protected @Nullable String mModelId;
56 
57     private final View mSelectionHotspot;
58     private final @Nullable FrameLayout mDebugContainer;
59     private @Nullable DocumentDebugInfo mDebugInfo;
60 
61     // See #addKeyEventListener for details on the need for this field.
62     private KeyboardEventListener mKeyEventListener;
63 
DocumentHolder(Context context, ViewGroup parent, int layout)64     public DocumentHolder(Context context, ViewGroup parent, int layout) {
65         this(context, inflateLayout(context, parent, layout));
66     }
67 
DocumentHolder(Context context, View item)68     public DocumentHolder(Context context, View item) {
69         super(item);
70 
71         itemView.setOnKeyListener(this);
72 
73         mContext = context;
74 
75         mDefaultBgColor = context.getColor(R.color.item_doc_background);
76         mSelectedBgColor = context.getColor(R.color.item_doc_background_selected);
77         mDroppableBgColor = context.getColor(R.color.item_doc_droppable_background);
78         mNotDroppableBgColor = context.getColor(R.color.item_doc_not_droppable_background);
79 
80         mSelectionHotspot = itemView.findViewById(R.id.icon_check);
81 
82         mDebugContainer = (FrameLayout) itemView.findViewById(R.id.debug_info);
83     }
84 
85     /**
86      * Binds the view to the given item data.
87      * @param cursor
88      * @param modelId
89      * @param state
90      */
bind(Cursor cursor, String modelId)91     public abstract void bind(Cursor cursor, String modelId);
92 
93     @Override
hasModelId()94     public boolean hasModelId() {
95         return !TextUtils.isEmpty(mModelId);
96     }
97 
98     @Override
getModelId()99     public String getModelId() {
100         return mModelId;
101     }
102 
103     /**
104      * Makes the associated item view appear selected. Note that this merely affects the appearance
105      * of the view, it doesn't actually select the item.
106      * TODO: Use the DirectoryItemAnimator instead of manually controlling animation using a boolean
107      * flag.
108      *
109      * @param selected
110      * @param animate Whether or not to animate the change. Only selection changes initiated by the
111      *            selection manager should be animated. See
112      *            {@link ModelBackedDocumentsAdapter#onBindViewHolder(DocumentHolder, int, java.util.List)}
113      */
setSelected(boolean selected, boolean animate)114     public void setSelected(boolean selected, boolean animate) {
115         // Note: the animate param doesn't apply for this base implementation, because the
116         // DirectoryItemAnimator takes care of it. It's required by subclasses, which perform their
117         // own animation.
118         itemView.setActivated(selected);
119         itemView.setBackgroundColor(selected ? mSelectedBgColor : mDefaultBgColor);
120     }
121 
122     /**
123      * Highlights the associated item view to indicate it's droppable.
124      * @param highlighted
125      */
setDroppableHighlight(boolean droppable)126     public void setDroppableHighlight(boolean droppable) {
127         // If item is already selected, its highlight should not be changed.
128         if (itemView.isActivated()) {
129             return;
130         }
131 
132         itemView.setBackgroundColor(droppable ? mDroppableBgColor : mNotDroppableBgColor);
133     }
134 
135     /**
136      * Reset the associated item view's droppable background highlight.
137      */
resetDropHighlight()138     public void resetDropHighlight() {
139         if (itemView.isActivated()) {
140             return;
141         }
142 
143         itemView.setBackgroundColor(mDefaultBgColor);
144     }
145 
setEnabled(boolean enabled)146     public void setEnabled(boolean enabled) {
147         setEnabledRecursive(itemView, enabled);
148     }
149 
150     @Override
onKey(View v, int keyCode, KeyEvent event)151     public boolean onKey(View v, int keyCode, KeyEvent event) {
152         assert(mKeyEventListener != null);
153         return mKeyEventListener.onKey(this,  keyCode,  event);
154     }
155 
156     /**
157      * Installs a delegate to receive keyboard input events. This arrangement is necessitated
158      * by the fact that a single listener cannot listen to all keyboard events
159      * on RecyclerView (our parent view). Not sure why this is, but have been
160      * assured it is the case.
161      *
162      * <p>Ideally we'd not involve DocumentHolder in propagation of events like this.
163      */
addKeyEventListener(KeyboardEventListener listener)164     public void addKeyEventListener(KeyboardEventListener listener) {
165         assert(mKeyEventListener == null);
166         mKeyEventListener = listener;
167     }
168 
169     @Override
isInSelectionHotspot(InputEvent event)170     public boolean isInSelectionHotspot(InputEvent event) {
171         // Do everything in global coordinates - it makes things simpler.
172         int[] coords = new int[2];
173         mSelectionHotspot.getLocationOnScreen(coords);
174         Rect rect = new Rect(coords[0], coords[1], coords[0] + mSelectionHotspot.getWidth(),
175                 coords[1] + mSelectionHotspot.getHeight());
176 
177         // If the tap occurred within the icon rect, consider it a selection.
178         return rect.contains((int) event.getRawX(), (int) event.getRawY());
179     }
180 
181     @Override
isInDragHotspot(InputEvent event)182     public boolean isInDragHotspot(InputEvent event) {
183         return false;
184     }
185 
includeDebugInfo(DocumentInfo doc)186     protected void includeDebugInfo(DocumentInfo doc) {
187         if (mDebugContainer == null) {
188             return;
189         }
190         if (DebugFlags.getDocumentDetailsEnabled()) {
191             assert(Build.IS_DEBUGGABLE);
192             if (mDebugInfo == null) {
193                 assert(mDebugContainer.getChildAt(0) == null);
194                 mDebugInfo = inflateLayout(mContext, mDebugContainer, R.layout.document_debug_info);
195                 mDebugContainer.addView(mDebugInfo);
196             }
197             mDebugInfo.update(doc);
198             mDebugContainer.setVisibility(View.VISIBLE);
199         } else {
200             mDebugContainer.setVisibility(View.GONE);
201         }
202     }
203 
setEnabledRecursive(View itemView, boolean enabled)204     static void setEnabledRecursive(View itemView, boolean enabled) {
205         if (itemView == null || itemView.isEnabled() == enabled) {
206             return;
207         }
208         itemView.setEnabled(enabled);
209 
210         if (itemView instanceof ViewGroup) {
211             final ViewGroup vg = (ViewGroup) itemView;
212             for (int i = vg.getChildCount() - 1; i >= 0; i--) {
213                 setEnabledRecursive(vg.getChildAt(i), enabled);
214             }
215         }
216     }
217 
inflateLayout(Context context, ViewGroup parent, int layout)218     private static <V extends View> V inflateLayout(Context context, ViewGroup parent, int layout) {
219         final LayoutInflater inflater = LayoutInflater.from(context);
220         return (V) inflater.inflate(layout, parent, false);
221     }
222 
fade(ImageView view, float alpha)223     static ViewPropertyAnimator fade(ImageView view, float alpha) {
224         return view.animate().setDuration(Shared.CHECK_ANIMATION_DURATION).alpha(alpha);
225     }
226 
227     /**
228      * Implement this in order to be able to respond to events coming from DocumentHolders.
229      * TODO: Make this bubble up logic events rather than having imperative commands.
230      */
231     interface KeyboardEventListener {
232 
233         /**
234          * Handles key events on the document holder.
235          *
236          * @param doc The target DocumentHolder.
237          * @param keyCode Key code for the event.
238          * @param event KeyEvent for the event.
239          * @return Whether the event was handled.
240          */
onKey(DocumentHolder doc, int keyCode, KeyEvent event)241         public boolean onKey(DocumentHolder doc, int keyCode, KeyEvent event);
242     }
243 }
244