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.base.DocumentInfo.getCursorLong;
20 import static com.android.documentsui.base.DocumentInfo.getCursorString;
21 
22 import android.annotation.ColorInt;
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.View;
28 import android.view.ViewGroup;
29 import android.widget.ImageView;
30 import android.widget.TextView;
31 
32 import com.android.documentsui.R;
33 import com.android.documentsui.base.DebugFlags;
34 import com.android.documentsui.base.DocumentInfo;
35 import com.android.documentsui.base.Events.InputEvent;
36 import com.android.documentsui.base.Shared;
37 import com.android.documentsui.roots.RootCursorWrapper;
38 
39 final class GridDocumentHolder extends DocumentHolder {
40 
41     final TextView mTitle;
42     final TextView mDate;
43     final TextView mDetails;
44     final ImageView mIconMimeLg;
45     final ImageView mIconMimeSm;
46     final ImageView mIconThumb;
47     final ImageView mIconCheck;
48     final IconHelper mIconHelper;
49 
50     private final @ColorInt int mDisabledBgColor;
51     // This is used in as a convenience in our bind method.
52     private final DocumentInfo mDoc = new DocumentInfo();
53 
GridDocumentHolder(Context context, ViewGroup parent, IconHelper iconHelper)54     public GridDocumentHolder(Context context, ViewGroup parent, IconHelper iconHelper) {
55         super(context, parent, R.layout.item_doc_grid);
56 
57         mDisabledBgColor = context.getColor(R.color.item_doc_background_disabled);
58 
59         mTitle = (TextView) itemView.findViewById(android.R.id.title);
60         mDate = (TextView) itemView.findViewById(R.id.date);
61         mDetails = (TextView) itemView.findViewById(R.id.details);
62         mIconMimeLg = (ImageView) itemView.findViewById(R.id.icon_mime_lg);
63         mIconMimeSm = (ImageView) itemView.findViewById(R.id.icon_mime_sm);
64         mIconThumb = (ImageView) itemView.findViewById(R.id.icon_thumb);
65         mIconCheck = (ImageView) itemView.findViewById(R.id.icon_check);
66 
67         mIconHelper = iconHelper;
68     }
69 
70     @Override
setSelected(boolean selected, boolean animate)71     public void setSelected(boolean selected, boolean animate) {
72         // We always want to make sure our check box disappears if we're not selected,
73         // even if the item is disabled. This is because this object can be reused
74         // and this method will be called to setup initial state.
75         float checkAlpha = selected ? 1f : 0f;
76         if (animate) {
77             fade(mIconMimeSm, checkAlpha).start();
78             fade(mIconCheck, checkAlpha).start();
79         } else {
80             mIconCheck.setAlpha(checkAlpha);
81         }
82 
83         // But it should be an error to be set to selected && be disabled.
84         if (!itemView.isEnabled()) {
85             assert(!selected);
86             return;
87         }
88 
89         super.setSelected(selected, animate);
90 
91         if (animate) {
92             fade(mIconMimeSm, 1f - checkAlpha).start();
93         } else {
94             mIconMimeSm.setAlpha(1f - checkAlpha);
95         }
96     }
97 
98     @Override
setEnabled(boolean enabled)99     public void setEnabled(boolean enabled) {
100         super.setEnabled(enabled);
101 
102         // Text colors enabled/disabled is handle via a color set.
103         itemView.setBackgroundColor(enabled ? mDefaultBgColor : mDisabledBgColor);
104         float imgAlpha = enabled ? 1f : DISABLED_ALPHA;
105 
106         mIconMimeLg.setAlpha(imgAlpha);
107         mIconMimeSm.setAlpha(imgAlpha);
108         mIconThumb.setAlpha(imgAlpha);
109     }
110 
111     @Override
isInDragHotspot(InputEvent event)112     public boolean isInDragHotspot(InputEvent event) {
113      // Entire grid box should be draggable
114         return true;
115     }
116 
117     /**
118      * Bind this view to the given document for display.
119      * @param cursor Pointing to the item to be bound.
120      * @param modelId The model ID of the item.
121      * @param state Current display state.
122      */
123     @Override
bind(Cursor cursor, String modelId)124     public void bind(Cursor cursor, String modelId) {
125         assert(cursor != null);
126 
127         mModelId = modelId;
128 
129         mDoc.updateFromCursor(cursor, getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY));
130 
131         mIconHelper.stopLoading(mIconThumb);
132 
133         mIconMimeLg.animate().cancel();
134         mIconMimeLg.setAlpha(1f);
135         mIconThumb.animate().cancel();
136         mIconThumb.setAlpha(0f);
137 
138         mIconHelper.load(mDoc, mIconThumb, mIconMimeLg, mIconMimeSm);
139 
140         mTitle.setText(mDoc.displayName, TextView.BufferType.SPANNABLE);
141         mTitle.setVisibility(View.VISIBLE);
142 
143         // If file is partial, we want to show summary field as that's more relevant than fileSize
144         // and date
145         if (mDoc.isPartial()) {
146             final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY);
147             mDetails.setVisibility(View.VISIBLE);
148             mDate.setText(null);
149             mDetails.setText(docSummary);
150         } else {
151             if (mDoc.lastModified == -1) {
152                 mDate.setText(null);
153             } else {
154                 mDate.setText(Shared.formatTime(mContext, mDoc.lastModified));
155             }
156 
157             final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE);
158             if (mDoc.isDirectory() || docSize == -1) {
159                 mDetails.setVisibility(View.GONE);
160             } else {
161                 mDetails.setVisibility(View.VISIBLE);
162                 mDetails.setText(Formatter.formatFileSize(mContext, docSize));
163             }
164         }
165 
166         if (DebugFlags.getDocumentDetailsEnabled()) {
167             includeDebugInfo(mDoc);
168         }
169     }
170 }
171