blob: 8eaed17e86768e8b77d856e053920001bbe2b888 [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;
22import static com.android.internal.util.Preconditions.checkNotNull;
23
24import android.content.Context;
25import android.database.Cursor;
26import android.net.Uri;
27import android.provider.DocumentsContract;
28import android.provider.DocumentsContract.Document;
29import android.text.format.Formatter;
30import android.view.View;
31import android.view.ViewGroup;
32import android.widget.ImageView;
33import android.widget.TextView;
34
35import com.android.documentsui.R;
36import com.android.documentsui.RootCursorWrapper;
37import com.android.documentsui.Shared;
38import com.android.documentsui.State;
39
40final class GridDocumentHolder extends DocumentHolder {
41 private static boolean mHideTitles;
42
Ben Kwaf8a51f62015-12-22 14:03:30 -080043 final TextView mTitle;
44 final TextView mDate;
45 final TextView mSize;
Ben Kwa91430f12016-01-06 12:10:18 -080046 final ImageView mIconMimeLg;
47 final ImageView mIconMimeSm;
Ben Kwaf8a51f62015-12-22 14:03:30 -080048 final ImageView mIconThumb;
Ben Kwaddc3d212016-01-07 18:50:34 -080049 final ImageView mIconCheck;
Ben Kwaf8a51f62015-12-22 14:03:30 -080050 final IconHelper mIconHelper;
51
52 public GridDocumentHolder(Context context, ViewGroup parent, IconHelper iconHelper) {
53 super(context, parent, R.layout.item_doc_grid);
54
55 mTitle = (TextView) itemView.findViewById(android.R.id.title);
56 mDate = (TextView) itemView.findViewById(R.id.date);
57 mSize = (TextView) itemView.findViewById(R.id.size);
Ben Kwaddc3d212016-01-07 18:50:34 -080058 mIconMimeLg = (ImageView) itemView.findViewById(R.id.icon_mime_lg);
59 mIconMimeSm = (ImageView) itemView.findViewById(R.id.icon_mime_sm);
Ben Kwaf8a51f62015-12-22 14:03:30 -080060 mIconThumb = (ImageView) itemView.findViewById(R.id.icon_thumb);
Ben Kwaddc3d212016-01-07 18:50:34 -080061 mIconCheck = (ImageView) itemView.findViewById(R.id.icon_check);
62
Ben Kwaf8a51f62015-12-22 14:03:30 -080063 mIconHelper = iconHelper;
Ben Kwad8391492015-12-17 10:37:00 -080064 }
65
Ben Kwaddc3d212016-01-07 18:50:34 -080066 @Override
67 public void setSelected(boolean selected) {
68 super.setSelected(selected);
69 float checkAlpha = selected ? 1f : 0f;
70
71 mIconCheck.animate().alpha(checkAlpha).start();
72 mIconMimeSm.animate().alpha(1f - checkAlpha).start();
73 }
74
Ben Kwad8391492015-12-17 10:37:00 -080075 /**
76 * Bind this view to the given document for display.
77 * @param cursor Pointing to the item to be bound.
78 * @param modelId The model ID of the item.
79 * @param state Current display state.
80 */
81 public void bind(Cursor cursor, String modelId, State state) {
82 this.modelId = modelId;
83
84 checkNotNull(cursor, "Cursor cannot be null.");
85
86 final String docAuthority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY);
87 final String docId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID);
88 final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
89 final String docDisplayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME);
90 final long docLastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
91 final int docIcon = getCursorInt(cursor, Document.COLUMN_ICON);
92 final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
93 final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE);
94
Ben Kwaf8a51f62015-12-22 14:03:30 -080095 mIconHelper.stopLoading(mIconThumb);
Ben Kwad8391492015-12-17 10:37:00 -080096
Ben Kwa91430f12016-01-06 12:10:18 -080097 mIconMimeLg.animate().cancel();
98 mIconMimeLg.setAlpha(1f);
Ben Kwaf8a51f62015-12-22 14:03:30 -080099 mIconThumb.animate().cancel();
100 mIconThumb.setAlpha(0f);
Ben Kwad8391492015-12-17 10:37:00 -0800101
102 final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId);
Tomasz Mikolajewski4e708cb2016-02-16 12:28:43 +0900103 mIconHelper.loadThumbnail(uri, docMimeType, docFlags, docIcon, mIconThumb, mIconMimeLg,
104 mIconMimeSm);
Ben Kwad8391492015-12-17 10:37:00 -0800105
106 if (mHideTitles) {
Ben Kwaf8a51f62015-12-22 14:03:30 -0800107 mTitle.setVisibility(View.GONE);
Ben Kwad8391492015-12-17 10:37:00 -0800108 } else {
Ben Kwaa4acc902016-02-10 15:48:25 -0800109 mTitle.setText(docDisplayName, TextView.BufferType.SPANNABLE);
Ben Kwaf8a51f62015-12-22 14:03:30 -0800110 mTitle.setVisibility(View.VISIBLE);
Ben Kwad8391492015-12-17 10:37:00 -0800111 }
112
113 if (docLastModified == -1) {
Ben Kwaf8a51f62015-12-22 14:03:30 -0800114 mDate.setText(null);
Ben Kwad8391492015-12-17 10:37:00 -0800115 } else {
Ben Kwaf8a51f62015-12-22 14:03:30 -0800116 mDate.setText(Shared.formatTime(mContext, docLastModified));
Ben Kwad8391492015-12-17 10:37:00 -0800117 }
118
119 if (!state.showSize || Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) {
Ben Kwaf8a51f62015-12-22 14:03:30 -0800120 mSize.setVisibility(View.GONE);
Ben Kwad8391492015-12-17 10:37:00 -0800121 } else {
Ben Kwaf8a51f62015-12-22 14:03:30 -0800122 mSize.setVisibility(View.VISIBLE);
123 mSize.setText(Formatter.formatFileSize(mContext, docSize));
Ben Kwad8391492015-12-17 10:37:00 -0800124 }
125 }
126
127 /**
128 * Sets whether to hide titles on subsequently created GridDocumentHolder items.
129 * @param hideTitles
130 */
131 public static void setHideTitles(boolean hideTitles) {
132 mHideTitles = hideTitles;
133 }
134}