• 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 static com.android.documentsui.DevicePolicyResources.Strings.PREVIEW_WORK_FILE_ACCESSIBILITY;
20 import static com.android.documentsui.DevicePolicyResources.Strings.UNDEFINED;
21 
22 import android.app.admin.DevicePolicyManager;
23 import android.content.Context;
24 import android.database.Cursor;
25 import android.os.Build;
26 import android.os.Bundle;
27 import android.view.KeyEvent;
28 import android.view.LayoutInflater;
29 import android.view.MotionEvent;
30 import android.view.View;
31 import android.view.ViewGroup;
32 import android.view.ViewPropertyAnimator;
33 import android.widget.ImageView;
34 
35 import androidx.annotation.RequiresApi;
36 import androidx.core.view.accessibility.AccessibilityNodeInfoCompat;
37 import androidx.recyclerview.widget.RecyclerView;
38 
39 import com.android.documentsui.R;
40 import com.android.documentsui.base.Shared;
41 import com.android.documentsui.base.State;
42 import com.android.modules.utils.build.SdkLevel;
43 
44 import java.util.function.Function;
45 
46 import javax.annotation.Nullable;
47 
48 /**
49  * ViewHolder of a document item within a RecyclerView.
50  */
51 public abstract class DocumentHolder
52         extends RecyclerView.ViewHolder implements View.OnKeyListener {
53 
54     static final float DISABLED_ALPHA = 0.3f;
55 
56     protected final Context mContext;
57 
58     protected @Nullable String mModelId;
59 
60     protected @State.ActionType int mAction;
61 
62     // See #addKeyEventListener for details on the need for this field.
63     private KeyboardEventListener<DocumentItemDetails> mKeyEventListener;
64 
65     private final DocumentItemDetails mDetails;
66 
DocumentHolder(Context context, ViewGroup parent, int layout)67     public DocumentHolder(Context context, ViewGroup parent, int layout) {
68         this(context, inflateLayout(context, parent, layout));
69     }
70 
DocumentHolder(Context context, View item)71     public DocumentHolder(Context context, View item) {
72         super(item);
73 
74         itemView.setOnKeyListener(this);
75 
76         mContext = context;
77         mDetails = new DocumentItemDetails(this);
78     }
79 
80     /**
81      * Binds the view to the given item data.
82      * @param cursor
83      * @param modelId
84      * @param state
85      */
bind(Cursor cursor, String modelId)86     public abstract void bind(Cursor cursor, String modelId);
87 
getModelId()88     public String getModelId() {
89         return mModelId;
90     }
91 
92     /**
93      * Makes the associated item view appear selected. Note that this merely affects the appearance
94      * of the view, it doesn't actually select the item.
95      * TODO: Use the DirectoryItemAnimator instead of manually controlling animation using a boolean
96      * flag.
97      *
98      * @param selected
99      * @param animate Whether or not to animate the change. Only selection changes initiated by the
100      *            selection manager should be animated. See
101      *            {@link ModelBackedDocumentsAdapter#onBindViewHolder(DocumentHolder, int, java.util.List)}
102      */
setSelected(boolean selected, boolean animate)103     public void setSelected(boolean selected, boolean animate) {
104         itemView.setActivated(selected);
105         itemView.setSelected(selected);
106     }
107 
setEnabled(boolean enabled)108     public void setEnabled(boolean enabled) {
109         setEnabledRecursive(itemView, enabled);
110     }
111 
setAction(@tate.ActionType int action)112     public void setAction(@State.ActionType int action) {
113         mAction = action;
114     }
115 
bindPreviewIcon(boolean show, Function<View, Boolean> clickCallback)116     public void bindPreviewIcon(boolean show, Function<View, Boolean> clickCallback) {}
117 
bindBriefcaseIcon(boolean show)118     public void bindBriefcaseIcon(boolean show) {}
119 
120     @Override
onKey(View v, int keyCode, KeyEvent event)121     public boolean onKey(View v, int keyCode, KeyEvent event) {
122         assert(mKeyEventListener != null);
123         DocumentItemDetails details = getItemDetails();
124         return (details == null)
125                 ? false
126                 : mKeyEventListener.onKey(details, keyCode, event);
127     }
128 
129     /**
130      * Installs a delegate to receive keyboard input events. This arrangement is necessitated
131      * by the fact that a single listener cannot listen to all keyboard events
132      * on RecyclerView (our parent view). Not sure why this is, but have been
133      * assured it is the case.
134      *
135      * <p>Ideally we'd not involve DocumentHolder in propagation of events like this.
136      */
addKeyEventListener(KeyboardEventListener<DocumentItemDetails> listener)137     public void addKeyEventListener(KeyboardEventListener<DocumentItemDetails> listener) {
138         assert(mKeyEventListener == null);
139         mKeyEventListener = listener;
140     }
141 
inDragRegion(MotionEvent event)142     public boolean inDragRegion(MotionEvent event) {
143         return false;
144     }
145 
inSelectRegion(MotionEvent event)146     public boolean inSelectRegion(MotionEvent event) {
147         return false;
148     }
149 
inPreviewIconRegion(MotionEvent event)150     public boolean inPreviewIconRegion(MotionEvent event) {
151         return false;
152     }
153 
getItemDetails()154     public DocumentItemDetails getItemDetails() {
155         return mDetails;
156     }
157 
setEnabledRecursive(View itemView, boolean enabled)158     static void setEnabledRecursive(View itemView, boolean enabled) {
159         if (itemView == null || itemView.isEnabled() == enabled) {
160             return;
161         }
162         itemView.setEnabled(enabled);
163 
164         if (itemView instanceof ViewGroup) {
165             final ViewGroup vg = (ViewGroup) itemView;
166             for (int i = vg.getChildCount() - 1; i >= 0; i--) {
167                 setEnabledRecursive(vg.getChildAt(i), enabled);
168             }
169         }
170     }
171 
172     @SuppressWarnings("TypeParameterUnusedInFormals")
inflateLayout(Context context, ViewGroup parent, int layout)173     private static <V extends View> V inflateLayout(Context context, ViewGroup parent, int layout) {
174         final LayoutInflater inflater = LayoutInflater.from(context);
175         return (V) inflater.inflate(layout, parent, false);
176     }
177 
fade(ImageView view, float alpha)178     static ViewPropertyAnimator fade(ImageView view, float alpha) {
179         return view.animate().setDuration(Shared.CHECK_ANIMATION_DURATION).alpha(alpha);
180     }
181 
getPreviewIconContentDescription(boolean isWorkProfile, String fileName)182     protected String getPreviewIconContentDescription(boolean isWorkProfile, String fileName) {
183         if (SdkLevel.isAtLeastT()) {
184             return getUpdatablePreviewIconContentDescription(isWorkProfile, fileName);
185         } else {
186             return itemView.getResources().getString(
187                     isWorkProfile ? R.string.preview_work_file : R.string.preview_file, fileName);
188         }
189     }
190 
191     @RequiresApi(Build.VERSION_CODES.TIRAMISU)
getUpdatablePreviewIconContentDescription( boolean isWorkProfile, String fileName)192     private String getUpdatablePreviewIconContentDescription(
193             boolean isWorkProfile, String fileName) {
194         DevicePolicyManager dpm = itemView.getContext().getSystemService(
195                 DevicePolicyManager.class);
196         String updatableStringId = isWorkProfile ? PREVIEW_WORK_FILE_ACCESSIBILITY : UNDEFINED;
197         int defaultStringId =
198                 isWorkProfile ? R.string.preview_work_file : R.string.preview_file;
199         return dpm.getResources().getString(
200                 updatableStringId,
201                 () -> itemView.getResources().getString(defaultStringId, fileName),
202                 /* formatArgs= */ fileName);
203     }
204 
205     protected static class PreviewAccessibilityDelegate extends View.AccessibilityDelegate {
206         private Function<View, Boolean> mCallback;
207 
PreviewAccessibilityDelegate(Function<View, Boolean> clickCallback)208         public PreviewAccessibilityDelegate(Function<View, Boolean> clickCallback) {
209             super();
210             mCallback = clickCallback;
211         }
212 
213         @Override
performAccessibilityAction(View host, int action, Bundle args)214         public boolean performAccessibilityAction(View host, int action, Bundle args) {
215             if (action == AccessibilityNodeInfoCompat.ACTION_CLICK) {
216                 return mCallback.apply(host);
217             }
218             return super.performAccessibilityAction(host, action, args);
219         }
220     }
221 }
222