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