Ben Kwa | 8e3fd76 | 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 | |
Steve McKay | d080506 | 2016-09-15 14:30:38 -0700 | [diff] [blame] | 19 | import static com.android.documentsui.base.DocumentInfo.getCursorLong; |
| 20 | import static com.android.documentsui.base.DocumentInfo.getCursorString; |
Ben Kwa | 8e3fd76 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 21 | |
Steve McKay | 2e74440 | 2016-03-08 11:37:22 -0800 | [diff] [blame] | 22 | import android.annotation.ColorInt; |
Ben Kwa | 8e3fd76 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 23 | import android.content.Context; |
| 24 | import android.database.Cursor; |
Ben Kwa | 8e3fd76 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 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; |
Steve McKay | 358c3ec | 2016-10-21 09:16:57 -0700 | [diff] [blame] | 33 | import com.android.documentsui.base.DebugFlags; |
| 34 | import com.android.documentsui.base.DocumentInfo; |
Steve McKay | d9caa6a | 2016-09-15 16:36:45 -0700 | [diff] [blame] | 35 | import com.android.documentsui.base.Events.InputEvent; |
Steve McKay | 358c3ec | 2016-10-21 09:16:57 -0700 | [diff] [blame] | 36 | import com.android.documentsui.base.Shared; |
Steve McKay | d9caa6a | 2016-09-15 16:36:45 -0700 | [diff] [blame] | 37 | import com.android.documentsui.roots.RootCursorWrapper; |
Ben Kwa | 8e3fd76 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 38 | |
| 39 | final class GridDocumentHolder extends DocumentHolder { |
Steve McKay | 2e74440 | 2016-03-08 11:37:22 -0800 | [diff] [blame] | 40 | |
Ben Kwa | c3c20f2 | 2015-12-22 14:03:30 -0800 | [diff] [blame] | 41 | final TextView mTitle; |
| 42 | final TextView mDate; |
Ben Lin | 5eed8fa | 2016-10-13 11:31:38 -0700 | [diff] [blame] | 43 | final TextView mDetails; |
Ben Kwa | a4f47ce | 2016-01-06 12:10:18 -0800 | [diff] [blame] | 44 | final ImageView mIconMimeLg; |
| 45 | final ImageView mIconMimeSm; |
Ben Kwa | c3c20f2 | 2015-12-22 14:03:30 -0800 | [diff] [blame] | 46 | final ImageView mIconThumb; |
Ben Kwa | e59f1db | 2016-01-07 18:50:34 -0800 | [diff] [blame] | 47 | final ImageView mIconCheck; |
Ben Kwa | c3c20f2 | 2015-12-22 14:03:30 -0800 | [diff] [blame] | 48 | final IconHelper mIconHelper; |
| 49 | |
Steve McKay | 2e74440 | 2016-03-08 11:37:22 -0800 | [diff] [blame] | 50 | private final @ColorInt int mDisabledBgColor; |
Steve McKay | 358c3ec | 2016-10-21 09:16:57 -0700 | [diff] [blame] | 51 | // This is used in as a convenience in our bind method. |
| 52 | private final DocumentInfo mDoc = new DocumentInfo(); |
Steve McKay | 2e74440 | 2016-03-08 11:37:22 -0800 | [diff] [blame] | 53 | |
Ben Kwa | c3c20f2 | 2015-12-22 14:03:30 -0800 | [diff] [blame] | 54 | public GridDocumentHolder(Context context, ViewGroup parent, IconHelper iconHelper) { |
| 55 | super(context, parent, R.layout.item_doc_grid); |
| 56 | |
Steve McKay | 2e74440 | 2016-03-08 11:37:22 -0800 | [diff] [blame] | 57 | mDisabledBgColor = context.getColor(R.color.item_doc_background_disabled); |
| 58 | |
Ben Kwa | c3c20f2 | 2015-12-22 14:03:30 -0800 | [diff] [blame] | 59 | mTitle = (TextView) itemView.findViewById(android.R.id.title); |
| 60 | mDate = (TextView) itemView.findViewById(R.id.date); |
Ben Lin | 5eed8fa | 2016-10-13 11:31:38 -0700 | [diff] [blame] | 61 | mDetails = (TextView) itemView.findViewById(R.id.details); |
Ben Kwa | e59f1db | 2016-01-07 18:50:34 -0800 | [diff] [blame] | 62 | mIconMimeLg = (ImageView) itemView.findViewById(R.id.icon_mime_lg); |
| 63 | mIconMimeSm = (ImageView) itemView.findViewById(R.id.icon_mime_sm); |
Ben Kwa | c3c20f2 | 2015-12-22 14:03:30 -0800 | [diff] [blame] | 64 | mIconThumb = (ImageView) itemView.findViewById(R.id.icon_thumb); |
Ben Kwa | e59f1db | 2016-01-07 18:50:34 -0800 | [diff] [blame] | 65 | mIconCheck = (ImageView) itemView.findViewById(R.id.icon_check); |
| 66 | |
Ben Kwa | c3c20f2 | 2015-12-22 14:03:30 -0800 | [diff] [blame] | 67 | mIconHelper = iconHelper; |
Ben Kwa | 8e3fd76 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Ben Kwa | e59f1db | 2016-01-07 18:50:34 -0800 | [diff] [blame] | 70 | @Override |
Ben Kwa | 4c2f1a6 | 2016-03-16 12:13:21 -0700 | [diff] [blame] | 71 | public void setSelected(boolean selected, boolean animate) { |
Steve McKay | 2e74440 | 2016-03-08 11:37:22 -0800 | [diff] [blame] | 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. |
Ben Kwa | e59f1db | 2016-01-07 18:50:34 -0800 | [diff] [blame] | 75 | float checkAlpha = selected ? 1f : 0f; |
Ben Kwa | 4c2f1a6 | 2016-03-16 12:13:21 -0700 | [diff] [blame] | 76 | if (animate) { |
Steve McKay | 83ac678 | 2016-08-24 14:27:14 -0700 | [diff] [blame] | 77 | fade(mIconMimeSm, checkAlpha).start(); |
| 78 | fade(mIconCheck, checkAlpha).start(); |
Ben Kwa | 4c2f1a6 | 2016-03-16 12:13:21 -0700 | [diff] [blame] | 79 | } else { |
| 80 | mIconCheck.setAlpha(checkAlpha); |
| 81 | } |
Steve McKay | 2e74440 | 2016-03-08 11:37:22 -0800 | [diff] [blame] | 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 | |
Ben Kwa | 4c2f1a6 | 2016-03-16 12:13:21 -0700 | [diff] [blame] | 89 | super.setSelected(selected, animate); |
Steve McKay | 2e74440 | 2016-03-08 11:37:22 -0800 | [diff] [blame] | 90 | |
Ben Kwa | 4c2f1a6 | 2016-03-16 12:13:21 -0700 | [diff] [blame] | 91 | if (animate) { |
Steve McKay | 83ac678 | 2016-08-24 14:27:14 -0700 | [diff] [blame] | 92 | fade(mIconMimeSm, 1f - checkAlpha).start(); |
Ben Kwa | 4c2f1a6 | 2016-03-16 12:13:21 -0700 | [diff] [blame] | 93 | } else { |
| 94 | mIconMimeSm.setAlpha(1f - checkAlpha); |
| 95 | } |
Ben Kwa | e59f1db | 2016-01-07 18:50:34 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Steve McKay | fcf4516 | 2016-08-19 17:36:04 -0700 | [diff] [blame] | 98 | @Override |
Steve McKay | 2e74440 | 2016-03-08 11:37:22 -0800 | [diff] [blame] | 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 | |
Ben Lin | 19edf34 | 2016-09-09 16:18:18 -0700 | [diff] [blame] | 111 | @Override |
| 112 | public boolean isInDragHotspot(InputEvent event) { |
| 113 | // Entire grid box should be draggable |
| 114 | return true; |
| 115 | } |
| 116 | |
Ben Kwa | 8e3fd76 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 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 | */ |
Steve McKay | fcf4516 | 2016-08-19 17:36:04 -0700 | [diff] [blame] | 123 | @Override |
Steve McKay | 358c3ec | 2016-10-21 09:16:57 -0700 | [diff] [blame] | 124 | public void bind(Cursor cursor, String modelId) { |
Steve McKay | 0af8afd | 2016-02-25 13:34:03 -0800 | [diff] [blame] | 125 | assert(cursor != null); |
Ben Kwa | 8e3fd76 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 126 | |
Steve McKay | 358c3ec | 2016-10-21 09:16:57 -0700 | [diff] [blame] | 127 | mModelId = modelId; |
Ben Kwa | 8e3fd76 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 128 | |
Steve McKay | 358c3ec | 2016-10-21 09:16:57 -0700 | [diff] [blame] | 129 | mDoc.updateFromCursor(cursor, getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY)); |
Ben Kwa | 8e3fd76 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 130 | |
Ben Kwa | c3c20f2 | 2015-12-22 14:03:30 -0800 | [diff] [blame] | 131 | mIconHelper.stopLoading(mIconThumb); |
Ben Kwa | 8e3fd76 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 132 | |
Ben Kwa | a4f47ce | 2016-01-06 12:10:18 -0800 | [diff] [blame] | 133 | mIconMimeLg.animate().cancel(); |
| 134 | mIconMimeLg.setAlpha(1f); |
Ben Kwa | c3c20f2 | 2015-12-22 14:03:30 -0800 | [diff] [blame] | 135 | mIconThumb.animate().cancel(); |
| 136 | mIconThumb.setAlpha(0f); |
Ben Kwa | 8e3fd76 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 137 | |
Steve McKay | 358c3ec | 2016-10-21 09:16:57 -0700 | [diff] [blame] | 138 | mIconHelper.load(mDoc, mIconThumb, mIconMimeLg, mIconMimeSm); |
Ben Kwa | 8e3fd76 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 139 | |
Steve McKay | 358c3ec | 2016-10-21 09:16:57 -0700 | [diff] [blame] | 140 | mTitle.setText(mDoc.displayName, TextView.BufferType.SPANNABLE); |
| 141 | mTitle.setVisibility(View.VISIBLE); |
Ben Kwa | 8e3fd76 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 142 | |
Ben Lin | 5eed8fa | 2016-10-13 11:31:38 -0700 | [diff] [blame] | 143 | // If file is partial, we want to show summary field as that's more relevant than fileSize |
| 144 | // and date |
Steve McKay | 358c3ec | 2016-10-21 09:16:57 -0700 | [diff] [blame] | 145 | if (mDoc.isPartial()) { |
Ben Lin | 5eed8fa | 2016-10-13 11:31:38 -0700 | [diff] [blame] | 146 | final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY); |
| 147 | mDetails.setVisibility(View.VISIBLE); |
Ben Kwa | c3c20f2 | 2015-12-22 14:03:30 -0800 | [diff] [blame] | 148 | mDate.setText(null); |
Ben Lin | 5eed8fa | 2016-10-13 11:31:38 -0700 | [diff] [blame] | 149 | mDetails.setText(docSummary); |
Ben Kwa | 8e3fd76 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 150 | } else { |
Steve McKay | 358c3ec | 2016-10-21 09:16:57 -0700 | [diff] [blame] | 151 | if (mDoc.lastModified == -1) { |
Ben Lin | 5eed8fa | 2016-10-13 11:31:38 -0700 | [diff] [blame] | 152 | mDate.setText(null); |
| 153 | } else { |
Steve McKay | 358c3ec | 2016-10-21 09:16:57 -0700 | [diff] [blame] | 154 | mDate.setText(Shared.formatTime(mContext, mDoc.lastModified)); |
Ben Lin | 5eed8fa | 2016-10-13 11:31:38 -0700 | [diff] [blame] | 155 | } |
Ben Kwa | 8e3fd76 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 156 | |
Ben Lin | 5eed8fa | 2016-10-13 11:31:38 -0700 | [diff] [blame] | 157 | final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE); |
Steve McKay | 358c3ec | 2016-10-21 09:16:57 -0700 | [diff] [blame] | 158 | if (mDoc.isDirectory() || docSize == -1) { |
Ben Lin | 5eed8fa | 2016-10-13 11:31:38 -0700 | [diff] [blame] | 159 | mDetails.setVisibility(View.GONE); |
| 160 | } else { |
| 161 | mDetails.setVisibility(View.VISIBLE); |
| 162 | mDetails.setText(Formatter.formatFileSize(mContext, docSize)); |
| 163 | } |
Ben Kwa | 8e3fd76 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 164 | } |
Steve McKay | 358c3ec | 2016-10-21 09:16:57 -0700 | [diff] [blame] | 165 | |
| 166 | if (DebugFlags.getDocumentDetailsEnabled()) { |
| 167 | includeDebugInfo(mDoc); |
| 168 | } |
Ben Kwa | 8e3fd76 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 169 | } |
Ben Kwa | 8e3fd76 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 170 | } |