Ben Kwa | d839149 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 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.model.DocumentInfo.getCursorInt; |
| 20 | import static com.android.documentsui.model.DocumentInfo.getCursorLong; |
| 21 | import static com.android.documentsui.model.DocumentInfo.getCursorString; |
| 22 | import static com.android.internal.util.Preconditions.checkNotNull; |
| 23 | |
| 24 | import android.content.Context; |
| 25 | import android.database.Cursor; |
| 26 | import android.net.Uri; |
| 27 | import android.provider.DocumentsContract; |
| 28 | import android.provider.DocumentsContract.Document; |
| 29 | import android.text.format.Formatter; |
| 30 | import android.view.View; |
| 31 | import android.view.ViewGroup; |
| 32 | import android.widget.ImageView; |
| 33 | import android.widget.TextView; |
| 34 | |
| 35 | import com.android.documentsui.R; |
| 36 | import com.android.documentsui.RootCursorWrapper; |
| 37 | import com.android.documentsui.Shared; |
| 38 | import com.android.documentsui.State; |
| 39 | |
| 40 | final class GridDocumentHolder extends DocumentHolder { |
| 41 | private static boolean mHideTitles; |
| 42 | |
Ben Kwa | f8a51f6 | 2015-12-22 14:03:30 -0800 | [diff] [blame] | 43 | final TextView mTitle; |
| 44 | final TextView mDate; |
| 45 | final TextView mSize; |
Ben Kwa | 91430f1 | 2016-01-06 12:10:18 -0800 | [diff] [blame] | 46 | final ImageView mIconMimeLg; |
| 47 | final ImageView mIconMimeSm; |
Ben Kwa | f8a51f6 | 2015-12-22 14:03:30 -0800 | [diff] [blame] | 48 | final ImageView mIconThumb; |
Ben Kwa | ddc3d21 | 2016-01-07 18:50:34 -0800 | [diff] [blame] | 49 | final ImageView mIconCheck; |
Ben Kwa | f8a51f6 | 2015-12-22 14:03:30 -0800 | [diff] [blame] | 50 | 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 Kwa | ddc3d21 | 2016-01-07 18:50:34 -0800 | [diff] [blame] | 58 | mIconMimeLg = (ImageView) itemView.findViewById(R.id.icon_mime_lg); |
| 59 | mIconMimeSm = (ImageView) itemView.findViewById(R.id.icon_mime_sm); |
Ben Kwa | f8a51f6 | 2015-12-22 14:03:30 -0800 | [diff] [blame] | 60 | mIconThumb = (ImageView) itemView.findViewById(R.id.icon_thumb); |
Ben Kwa | ddc3d21 | 2016-01-07 18:50:34 -0800 | [diff] [blame] | 61 | mIconCheck = (ImageView) itemView.findViewById(R.id.icon_check); |
| 62 | |
Ben Kwa | f8a51f6 | 2015-12-22 14:03:30 -0800 | [diff] [blame] | 63 | mIconHelper = iconHelper; |
Ben Kwa | d839149 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Ben Kwa | ddc3d21 | 2016-01-07 18:50:34 -0800 | [diff] [blame] | 66 | @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 Kwa | d839149 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 75 | /** |
| 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 Kwa | f8a51f6 | 2015-12-22 14:03:30 -0800 | [diff] [blame] | 95 | mIconHelper.stopLoading(mIconThumb); |
Ben Kwa | d839149 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 96 | |
Ben Kwa | 91430f1 | 2016-01-06 12:10:18 -0800 | [diff] [blame] | 97 | mIconMimeLg.animate().cancel(); |
| 98 | mIconMimeLg.setAlpha(1f); |
Ben Kwa | f8a51f6 | 2015-12-22 14:03:30 -0800 | [diff] [blame] | 99 | mIconThumb.animate().cancel(); |
| 100 | mIconThumb.setAlpha(0f); |
Ben Kwa | d839149 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 101 | |
| 102 | final Uri uri = DocumentsContract.buildDocumentUri(docAuthority, docId); |
Tomasz Mikolajewski | 4e708cb | 2016-02-16 12:28:43 +0900 | [diff] [blame] | 103 | mIconHelper.loadThumbnail(uri, docMimeType, docFlags, docIcon, mIconThumb, mIconMimeLg, |
| 104 | mIconMimeSm); |
Ben Kwa | d839149 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 105 | |
| 106 | if (mHideTitles) { |
Ben Kwa | f8a51f6 | 2015-12-22 14:03:30 -0800 | [diff] [blame] | 107 | mTitle.setVisibility(View.GONE); |
Ben Kwa | d839149 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 108 | } else { |
Ben Kwa | a4acc90 | 2016-02-10 15:48:25 -0800 | [diff] [blame] | 109 | mTitle.setText(docDisplayName, TextView.BufferType.SPANNABLE); |
Ben Kwa | f8a51f6 | 2015-12-22 14:03:30 -0800 | [diff] [blame] | 110 | mTitle.setVisibility(View.VISIBLE); |
Ben Kwa | d839149 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | if (docLastModified == -1) { |
Ben Kwa | f8a51f6 | 2015-12-22 14:03:30 -0800 | [diff] [blame] | 114 | mDate.setText(null); |
Ben Kwa | d839149 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 115 | } else { |
Ben Kwa | f8a51f6 | 2015-12-22 14:03:30 -0800 | [diff] [blame] | 116 | mDate.setText(Shared.formatTime(mContext, docLastModified)); |
Ben Kwa | d839149 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | if (!state.showSize || Document.MIME_TYPE_DIR.equals(docMimeType) || docSize == -1) { |
Ben Kwa | f8a51f6 | 2015-12-22 14:03:30 -0800 | [diff] [blame] | 120 | mSize.setVisibility(View.GONE); |
Ben Kwa | d839149 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 121 | } else { |
Ben Kwa | f8a51f6 | 2015-12-22 14:03:30 -0800 | [diff] [blame] | 122 | mSize.setVisibility(View.VISIBLE); |
| 123 | mSize.setText(Formatter.formatFileSize(mContext, docSize)); |
Ben Kwa | d839149 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 124 | } |
| 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 | } |