blob: be6413bfbf6cac5383d0f8edf3970412d80df225 [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 ListDocumentHolder extends DocumentHolder {
Ben Kwaf8a51f62015-12-22 14:03:30 -080041 final TextView mTitle;
42 final TextView mSummary;
43 final TextView mDate;
44 final TextView mSize;
Ben Kwad8391492015-12-17 10:37:00 -080045 final ImageView mIconMime;
46 final ImageView mIconThumb;
Ben Kwaddc3d212016-01-07 18:50:34 -080047 final ImageView mIconCheck;
Ben Kwaf8a51f62015-12-22 14:03:30 -080048 final IconHelper mIconHelper;
Ben Kwad8391492015-12-17 10:37:00 -080049
Ben Kwaf8a51f62015-12-22 14:03:30 -080050 public ListDocumentHolder(Context context, ViewGroup parent, IconHelper iconHelper) {
51 super(context, parent, R.layout.item_doc_list);
Ben Kwad8391492015-12-17 10:37:00 -080052
Ben Kwaf8a51f62015-12-22 14:03:30 -080053 mTitle = (TextView) itemView.findViewById(android.R.id.title);
54 mSummary = (TextView) itemView.findViewById(android.R.id.summary);
55 mDate = (TextView) itemView.findViewById(R.id.date);
56 mSize = (TextView) itemView.findViewById(R.id.size);
Ben Kwad8391492015-12-17 10:37:00 -080057 mIconMime = (ImageView) itemView.findViewById(R.id.icon_mime);
58 mIconThumb = (ImageView) itemView.findViewById(R.id.icon_thumb);
Ben Kwaddc3d212016-01-07 18:50:34 -080059 mIconCheck = (ImageView) itemView.findViewById(R.id.icon_check);
Ben Kwaf8a51f62015-12-22 14:03:30 -080060
61 mIconHelper = iconHelper;
Ben Kwad8391492015-12-17 10:37:00 -080062 }
63
Ben Kwaddc3d212016-01-07 18:50:34 -080064 @Override
65 public void setSelected(boolean selected) {
66 super.setSelected(selected);
67 float checkAlpha = selected ? 1f : 0f;
68
69 mIconCheck.animate().alpha(checkAlpha).start();
70 mIconMime.animate().alpha(1f - checkAlpha).start();
71 mIconThumb.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 @Override
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 String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY);
94 final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE);
95
Ben Kwad8391492015-12-17 10:37:00 -080096 mIconHelper.stopLoading(mIconThumb);
97
98 mIconMime.animate().cancel();
Ben Kwaddc3d212016-01-07 18:50:34 -080099 mIconMime.setAlpha(1f);
Ben Kwad8391492015-12-17 10:37:00 -0800100 mIconThumb.animate().cancel();
Ben Kwaddc3d212016-01-07 18:50:34 -0800101 mIconThumb.setAlpha(0f);
Ben Kwad8391492015-12-17 10:37:00 -0800102
103 final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId);
Tomasz Mikolajewski4e708cb2016-02-16 12:28:43 +0900104 mIconHelper.loadThumbnail(uri, docMimeType, docFlags, docIcon, mIconThumb, mIconMime, null);
Ben Kwad8391492015-12-17 10:37:00 -0800105
Ben Kwaa4acc902016-02-10 15:48:25 -0800106 mTitle.setText(docDisplayName, TextView.BufferType.SPANNABLE);
Ben Kwaf8a51f62015-12-22 14:03:30 -0800107 mTitle.setVisibility(View.VISIBLE);
Ben Kwad8391492015-12-17 10:37:00 -0800108
109 if (docSummary != null) {
Ben Kwaf8a51f62015-12-22 14:03:30 -0800110 mSummary.setText(docSummary);
111 mSummary.setVisibility(View.VISIBLE);
Ben Kwad8391492015-12-17 10:37:00 -0800112 } else {
Ben Kwaf8a51f62015-12-22 14:03:30 -0800113 mSummary.setVisibility(View.INVISIBLE);
Ben Kwad8391492015-12-17 10:37:00 -0800114 }
115
116 if (docLastModified == -1) {
Ben Kwaf8a51f62015-12-22 14:03:30 -0800117 mDate.setText(null);
Ben Kwad8391492015-12-17 10:37:00 -0800118 } else {
Ben Kwaf8a51f62015-12-22 14:03:30 -0800119 mDate.setText(Shared.formatTime(mContext, docLastModified));
Ben Kwad8391492015-12-17 10:37:00 -0800120 }
121
122 if (!state.showSize || Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) {
Ben Kwaf8a51f62015-12-22 14:03:30 -0800123 mSize.setVisibility(View.GONE);
Ben Kwad8391492015-12-17 10:37:00 -0800124 } else {
Ben Kwaf8a51f62015-12-22 14:03:30 -0800125 mSize.setVisibility(View.VISIBLE);
126 mSize.setText(Formatter.formatFileSize(mContext, docSize));
Ben Kwad8391492015-12-17 10:37:00 -0800127 }
128 }
129
130 @Override
131 public void setEnabled(boolean enabled) {
132 super.setEnabled(enabled);
133 final float iconAlpha = enabled ? 1f : 0.5f;
134 mIconMime.setAlpha(iconAlpha);
135 mIconThumb.setAlpha(iconAlpha);
Ben Kwad8391492015-12-17 10:37:00 -0800136 }
137}