1 /*
2  * Copyright (C) 2018 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.base.DocumentInfo.getCursorInt;
20 import static com.android.documentsui.base.DocumentInfo.getCursorLong;
21 import static com.android.documentsui.base.DocumentInfo.getCursorString;
22 
23 import android.content.Context;
24 import android.database.Cursor;
25 import android.provider.DocumentsContract.Document;
26 import android.text.format.Formatter;
27 import android.view.MotionEvent;
28 import android.view.View;
29 import android.view.ViewGroup;
30 import android.widget.ImageView;
31 
32 import com.android.documentsui.R;
33 import com.android.documentsui.base.DocumentInfo;
34 import com.android.documentsui.base.Shared;
35 import com.android.documentsui.base.UserId;
36 import com.android.documentsui.roots.RootCursorWrapper;
37 import com.android.documentsui.ui.Views;
38 
39 import java.util.function.Function;
40 
41 final class GridPhotoHolder extends DocumentHolder {
42 
43     private final ImageView mIconMimeLg;
44     private final ImageView mIconThumb;
45     private final ImageView mIconCheck;
46     private final IconHelper mIconHelper;
47     private final View mPreviewIcon;
48     private final View mIconBriefcase;
49 
50     // This is used in as a convenience in our bind method.
51     private final DocumentInfo mDoc = new DocumentInfo();
52 
GridPhotoHolder(Context context, ViewGroup parent, IconHelper iconHelper)53     public GridPhotoHolder(Context context, ViewGroup parent, IconHelper iconHelper) {
54         super(context, parent, R.layout.item_photo_grid);
55 
56         mIconMimeLg = (ImageView) itemView.findViewById(R.id.icon_mime_lg);
57         mIconThumb = (ImageView) itemView.findViewById(R.id.icon_thumb);
58         mIconCheck = (ImageView) itemView.findViewById(R.id.icon_check);
59         mIconBriefcase = itemView.findViewById(R.id.icon_briefcase);
60         mPreviewIcon = itemView.findViewById(R.id.preview_icon);
61 
62         mIconHelper = iconHelper;
63     }
64 
65     @Override
setSelected(boolean selected, boolean animate)66     public void setSelected(boolean selected, boolean animate) {
67         // We always want to make sure our check box disappears if we're not selected,
68         // even if the item is disabled. This is because this object can be reused
69         // and this method will be called to setup initial state.
70         float checkAlpha = selected ? 1f : 0f;
71         if (animate) {
72             fade(mIconCheck, checkAlpha).start();
73         } else {
74             mIconCheck.setAlpha(checkAlpha);
75         }
76 
77         // But it should be an error to be set to selected && be disabled.
78         if (!itemView.isEnabled()) {
79             assert (!selected);
80             return;
81         }
82 
83         super.setSelected(selected, animate);
84     }
85 
86     @Override
setEnabled(boolean enabled)87     public void setEnabled(boolean enabled) {
88         super.setEnabled(enabled);
89 
90         float imgAlpha = enabled ? 1f : DISABLED_ALPHA;
91 
92         mIconMimeLg.setAlpha(imgAlpha);
93         mIconThumb.setAlpha(imgAlpha);
94     }
95 
96     @Override
bindPreviewIcon(boolean show, Function<View, Boolean> clickCallback)97     public void bindPreviewIcon(boolean show, Function<View, Boolean> clickCallback) {
98         mPreviewIcon.setVisibility(show ? View.VISIBLE : View.GONE);
99         if (show) {
100             mPreviewIcon.setContentDescription(
101                     itemView.getResources().getString(
102                             mIconHelper.shouldShowBadge(mDoc.userId.getIdentifier())
103                                     ? R.string.preview_work_file
104                                     : R.string.preview_file, mDoc.displayName));
105             mPreviewIcon.setAccessibilityDelegate(new PreviewAccessibilityDelegate(clickCallback));
106         }
107     }
108 
109     @Override
bindBriefcaseIcon(boolean show)110     public void bindBriefcaseIcon(boolean show) {
111         mIconBriefcase.setVisibility(show ? View.VISIBLE : View.GONE);
112     }
113 
114     @Override
inDragRegion(MotionEvent event)115     public boolean inDragRegion(MotionEvent event) {
116         // Entire grid box should be draggable
117         return true;
118     }
119 
120     @Override
inSelectRegion(MotionEvent event)121     public boolean inSelectRegion(MotionEvent event) {
122         // Photo gird should not have any select region.
123         return false;
124     }
125 
126     @Override
inPreviewIconRegion(MotionEvent event)127     public boolean inPreviewIconRegion(MotionEvent event) {
128         return Views.isEventOver(event, mPreviewIcon);
129     }
130 
131     /**
132      * Bind this view to the given document for display.
133      * @param cursor Pointing to the item to be bound.
134      * @param modelId The model ID of the item.
135      */
136     @Override
bind(Cursor cursor, String modelId)137     public void bind(Cursor cursor, String modelId) {
138         assert (cursor != null);
139 
140         mModelId = modelId;
141 
142         mDoc.updateFromCursor(cursor,
143                 UserId.of(getCursorInt(cursor, RootCursorWrapper.COLUMN_USER_ID)),
144                 getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY));
145 
146         mIconHelper.stopLoading(mIconThumb);
147 
148         mIconMimeLg.animate().cancel();
149         mIconMimeLg.setAlpha(1f);
150         mIconThumb.animate().cancel();
151         mIconThumb.setAlpha(0f);
152 
153         mIconHelper.load(mDoc, mIconThumb, mIconMimeLg, null);
154 
155         final String docSize =
156                 Formatter.formatFileSize(mContext, getCursorLong(cursor, Document.COLUMN_SIZE));
157         final String docDate = Shared.formatTime(mContext, mDoc.lastModified);
158         if (mIconHelper.shouldShowBadge(mDoc.userId.getIdentifier())) {
159             itemView.setContentDescription((mContext.getText(R.string.a11y_work) + ", ")
160                     + mDoc.displayName + ", " + docSize + ", " + docDate);
161         } else {
162             itemView.setContentDescription(mDoc.displayName + ", " + docSize + ", " + docDate);
163         }
164     }
165 }
166