blob: a4bce1626fb416f27d35307c7573ba1e6b1c44a0 [file] [log] [blame]
Ben Kwad8391492015-12-17 10:37:00 -08001/*
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
17package com.android.documentsui.dirlist;
18
19import static com.android.documentsui.model.DocumentInfo.getCursorInt;
20import static com.android.documentsui.model.DocumentInfo.getCursorLong;
21import static com.android.documentsui.model.DocumentInfo.getCursorString;
Ben Kwad8391492015-12-17 10:37:00 -080022
23import android.content.Context;
24import android.database.Cursor;
25import android.net.Uri;
26import android.provider.DocumentsContract;
27import android.provider.DocumentsContract.Document;
28import android.text.format.Formatter;
29import android.view.View;
30import android.view.ViewGroup;
31import android.widget.ImageView;
32import android.widget.TextView;
33
34import com.android.documentsui.R;
35import com.android.documentsui.RootCursorWrapper;
36import com.android.documentsui.Shared;
37import com.android.documentsui.State;
38
39final class GridDocumentHolder extends DocumentHolder {
40 private static boolean mHideTitles;
41
Ben Kwaf8a51f62015-12-22 14:03:30 -080042 final TextView mTitle;
43 final TextView mDate;
44 final TextView mSize;
Ben Kwa91430f12016-01-06 12:10:18 -080045 final ImageView mIconMimeLg;
46 final ImageView mIconMimeSm;
Ben Kwaf8a51f62015-12-22 14:03:30 -080047 final ImageView mIconThumb;
Ben Kwaddc3d212016-01-07 18:50:34 -080048 final ImageView mIconCheck;
Ben Kwaf8a51f62015-12-22 14:03:30 -080049 final IconHelper mIconHelper;
50
51 public GridDocumentHolder(Context context, ViewGroup parent, IconHelper iconHelper) {
52 super(context, parent, R.layout.item_doc_grid);
53
54 mTitle = (TextView) itemView.findViewById(android.R.id.title);
55 mDate = (TextView) itemView.findViewById(R.id.date);
56 mSize = (TextView) itemView.findViewById(R.id.size);
Ben Kwaddc3d212016-01-07 18:50:34 -080057 mIconMimeLg = (ImageView) itemView.findViewById(R.id.icon_mime_lg);
58 mIconMimeSm = (ImageView) itemView.findViewById(R.id.icon_mime_sm);
Ben Kwaf8a51f62015-12-22 14:03:30 -080059 mIconThumb = (ImageView) itemView.findViewById(R.id.icon_thumb);
Ben Kwaddc3d212016-01-07 18:50:34 -080060 mIconCheck = (ImageView) itemView.findViewById(R.id.icon_check);
61
Ben Kwaf8a51f62015-12-22 14:03:30 -080062 mIconHelper = iconHelper;
Ben Kwad8391492015-12-17 10:37:00 -080063 }
64
Ben Kwaddc3d212016-01-07 18:50:34 -080065 @Override
66 public void setSelected(boolean selected) {
67 super.setSelected(selected);
68 float checkAlpha = selected ? 1f : 0f;
69
70 mIconCheck.animate().alpha(checkAlpha).start();
71 mIconMimeSm.animate().alpha(1f - checkAlpha).start();
72 }
73
Ben Kwad8391492015-12-17 10:37:00 -080074 /**
75 * Bind this view to the given document for display.
76 * @param cursor Pointing to the item to be bound.
77 * @param modelId The model ID of the item.
78 * @param state Current display state.
79 */
80 public void bind(Cursor cursor, String modelId, State state) {
Steve McKaya1f76802016-02-25 13:34:03 -080081 assert(cursor != null);
Ben Kwad8391492015-12-17 10:37:00 -080082
Steve McKaya1f76802016-02-25 13:34:03 -080083 this.modelId = modelId;
Ben Kwad8391492015-12-17 10:37:00 -080084
85 final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY);
86 final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID);
87 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
88 final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME);
89 final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
90 final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON);
91 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
92 final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE);
93
Ben Kwaf8a51f62015-12-22 14:03:30 -080094 mIconHelper.stopLoading(mIconThumb);
Ben Kwad8391492015-12-17 10:37:00 -080095
Ben Kwa91430f12016-01-06 12:10:18 -080096 mIconMimeLg.animate().cancel();
97 mIconMimeLg.setAlpha(1f);
Ben Kwaf8a51f62015-12-22 14:03:30 -080098 mIconThumb.animate().cancel();
99 mIconThumb.setAlpha(0f);
Ben Kwad8391492015-12-17 10:37:00 -0800100
101 final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId);
Tomasz Mikolajewski4e708cb2016-02-16 12:28:43 +0900102 mIconHelper.loadThumbnail(uri, docMimeType, docFlags, docIcon, mIconThumb, mIconMimeLg,
103 mIconMimeSm);
Ben Kwad8391492015-12-17 10:37:00 -0800104
105 if (mHideTitles) {
Ben Kwaf8a51f62015-12-22 14:03:30 -0800106 mTitle.setVisibility(View.GONE);
Ben Kwad8391492015-12-17 10:37:00 -0800107 } else {
Ben Kwaa4acc902016-02-10 15:48:25 -0800108 mTitle.setText(docDisplayName, TextView.BufferType.SPANNABLE);
Ben Kwaf8a51f62015-12-22 14:03:30 -0800109 mTitle.setVisibility(View.VISIBLE);
Ben Kwad8391492015-12-17 10:37:00 -0800110 }
111
112 if (docLastModified == -1) {
Ben Kwaf8a51f62015-12-22 14:03:30 -0800113 mDate.setText(null);
Ben Kwad8391492015-12-17 10:37:00 -0800114 } else {
Ben Kwaf8a51f62015-12-22 14:03:30 -0800115 mDate.setText(Shared.formatTime(mContext, docLastModified));
Ben Kwad8391492015-12-17 10:37:00 -0800116 }
117
118 if (!state.showSize || Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) {
Ben Kwaf8a51f62015-12-22 14:03:30 -0800119 mSize.setVisibility(View.GONE);
Ben Kwad8391492015-12-17 10:37:00 -0800120 } else {
Ben Kwaf8a51f62015-12-22 14:03:30 -0800121 mSize.setVisibility(View.VISIBLE);
122 mSize.setText(Formatter.formatFileSize(mContext, docSize));
Ben Kwad8391492015-12-17 10:37:00 -0800123 }
124 }
125
126 /**
127 * Sets whether to hide titles on subsequently created GridDocumentHolder items.
128 * @param hideTitles
129 */
130 public static void setHideTitles(boolean hideTitles) {
131 mHideTitles = hideTitles;
132 }
133}