• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
51     protected @Nullable String mModelId;
52 
53     private final View mSelectionHotspot;
54     private @Nullable DocumentDebugInfo mDebugInfo;
55 
56     // See #addKeyEventListener for details on the need for this field.
57     private KeyboardEventListener mKeyEventListener;
58 
DocumentHolder(Context context, ViewGroup parent, int layout)59     public DocumentHolder(Context context, ViewGroup parent, int layout) {
60         this(context, inflateLayout(context, parent, layout));
61     }
62 
DocumentHolder(Context context, View item)63     public DocumentHolder(Context context, View item) {
64         super(item);
65 
66         itemView.setOnKeyListener(this);
67 
68         mContext = context;
69 
70         mSelectionHotspot = itemView.findViewById(R.id.icon_check);
71     }
72 
73     /**
74      * Binds the view to the given item data.
75      * @param cursor
76      * @param modelId
77      * @param state
78      */
bind(Cursor cursor, String modelId)79     public abstract void bind(Cursor cursor, String modelId);
80 
81     @Override
hasModelId()82     public boolean hasModelId() {
83         return !TextUtils.isEmpty(mModelId);
84     }
85 
86     @Override
getModelId()87     public String getModelId() {
88         return mModelId;
89     }
90 
91     /**
92      * Makes the associated item view appear selected. Note that this merely affects the appearance
93      * of the view, it doesn't actually select the item.
94      * TODO: Use the DirectoryItemAnimator instead of manually controlling animation using a boolean
95      * flag.
96      *
97      * @param selected
98      * @param animate Whether or not to animate the change. Only selection changes initiated by the
99      *            selection manager should be animated. See
100      *            {@link ModelBackedDocumentsAdapter#onBindViewHolder(DocumentHolder, int, java.util.List)}
101      */
setSelected(boolean selected, boolean animate)102     public void setSelected(boolean selected, boolean animate) {
103         itemView.setActivated(selected);
104         itemView.setSelected(selected);
105     }
106 
setEnabled(boolean enabled)107     public void setEnabled(boolean enabled) {
108         setEnabledRecursive(itemView, enabled);
109     }
110 
111     @Override
onKey(View v, int keyCode, KeyEvent event)112     public boolean onKey(View v, int keyCode, KeyEvent event) {
113         assert(mKeyEventListener != null);
114         return mKeyEventListener.onKey(this,  keyCode,  event);
115     }
116 
117     /**
118      * Installs a delegate to receive keyboard input events. This arrangement is necessitated
119      * by the fact that a single listener cannot listen to all keyboard events
120      * on RecyclerView (our parent view). Not sure why this is, but have been
121      * assured it is the case.
122      *
123      * <p>Ideally we'd not involve DocumentHolder in propagation of events like this.
124      */
addKeyEventListener(KeyboardEventListener listener)125     public void addKeyEventListener(KeyboardEventListener listener) {
126         assert(mKeyEventListener == null);
127         mKeyEventListener = listener;
128     }
129 
130     @Override
isInSelectionHotspot(InputEvent event)131     public boolean isInSelectionHotspot(InputEvent event) {
132         // Do everything in global coordinates - it makes things simpler.
133         int[] coords = new int[2];
134         mSelectionHotspot.getLocationOnScreen(coords);
135         Rect rect = new Rect(coords[0], coords[1], coords[0] + mSelectionHotspot.getWidth(),
136                 coords[1] + mSelectionHotspot.getHeight());
137 
138         // If the tap occurred within the icon rect, consider it a selection.
139         return rect.contains((int) event.getRawX(), (int) event.getRawY());
140     }
141 
142     @Override
isInDragHotspot(InputEvent event)143     public boolean isInDragHotspot(InputEvent event) {
144         return false;
145     }
146 
147     @Override
isOverDocIcon(InputEvent event)148     public boolean isOverDocIcon(InputEvent event) {
149         return false;
150     }
151 
setEnabledRecursive(View itemView, boolean enabled)152     static void setEnabledRecursive(View itemView, boolean enabled) {
153         if (itemView == null || itemView.isEnabled() == enabled) {
154             return;
155         }
156         itemView.setEnabled(enabled);
157 
158         if (itemView instanceof ViewGroup) {
159             final ViewGroup vg = (ViewGroup) itemView;
160             for (int i = vg.getChildCount() - 1; i >= 0; i--) {
161                 setEnabledRecursive(vg.getChildAt(i), enabled);
162             }
163         }
164     }
165 
inflateLayout(Context context, ViewGroup parent, int layout)166     private static <V extends View> V inflateLayout(Context context, ViewGroup parent, int layout) {
167         final LayoutInflater inflater = LayoutInflater.from(context);
168         return (V) inflater.inflate(layout, parent, false);
169     }
170 
fade(ImageView view, float alpha)171     static ViewPropertyAnimator fade(ImageView view, float alpha) {
172         return view.animate().setDuration(Shared.CHECK_ANIMATION_DURATION).alpha(alpha);
173     }
174 
175     /**
176      * Implement this in order to be able to respond to events coming from DocumentHolders.
177      * TODO: Make this bubble up logic events rather than having imperative commands.
178      */
179     interface KeyboardEventListener {
180 
181         /**
182          * Handles key events on the document holder.
183          *
184          * @param doc The target DocumentHolder.
185          * @param keyCode Key code for the event.
186          * @param event KeyEvent for the event.
187          * @return Whether the event was handled.
188          */
onKey(DocumentHolder doc, int keyCode, KeyEvent event)189         public boolean onKey(DocumentHolder doc, int keyCode, KeyEvent event);
190     }
191 }
192