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.getCursorString; |
Ben Kwa | 8e3fd76 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 20 | |
| 21 | import android.content.Context; |
| 22 | import android.database.Cursor; |
Ben Lin | 19edf34 | 2016-09-09 16:18:18 -0700 | [diff] [blame] | 23 | import android.graphics.Rect; |
Ben Kwa | 8e3fd76 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 24 | import android.text.format.Formatter; |
| 25 | import android.view.View; |
| 26 | import android.view.ViewGroup; |
| 27 | import android.widget.ImageView; |
Steve McKay | 33e29c8 | 2016-03-01 11:26:00 -0800 | [diff] [blame] | 28 | import android.widget.LinearLayout; |
Ben Kwa | 8e3fd76 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 29 | import android.widget.TextView; |
| 30 | |
| 31 | import com.android.documentsui.R; |
Steve McKay | 358c3ec | 2016-10-21 09:16:57 -0700 | [diff] [blame] | 32 | import com.android.documentsui.base.DocumentInfo; |
Steve McKay | d9caa6a | 2016-09-15 16:36:45 -0700 | [diff] [blame] | 33 | import com.android.documentsui.base.Events.InputEvent; |
Steve McKay | 358c3ec | 2016-10-21 09:16:57 -0700 | [diff] [blame] | 34 | import com.android.documentsui.base.Shared; |
Steve McKay | d9caa6a | 2016-09-15 16:36:45 -0700 | [diff] [blame] | 35 | import com.android.documentsui.roots.RootCursorWrapper; |
Ben Kwa | 8e3fd76 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 36 | |
| 37 | final class ListDocumentHolder extends DocumentHolder { |
Steve McKay | 358c3ec | 2016-10-21 09:16:57 -0700 | [diff] [blame] | 38 | |
| 39 | private final TextView mTitle; |
| 40 | private final LinearLayout mDetails; // Container of date/size/summary |
| 41 | private final TextView mDate; |
| 42 | private final TextView mSize; |
| 43 | private final TextView mSummary; |
| 44 | private final ImageView mIconMime; |
| 45 | private final ImageView mIconThumb; |
| 46 | private final ImageView mIconCheck; |
| 47 | private final IconHelper mIconHelper; |
| 48 | private final View mIconLayout; |
| 49 | // This is used in as a convenience in our bind method. |
| 50 | private final DocumentInfo mDoc; |
Ben Kwa | 8e3fd76 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 51 | |
Ben Kwa | c3c20f2 | 2015-12-22 14:03:30 -0800 | [diff] [blame] | 52 | public ListDocumentHolder(Context context, ViewGroup parent, IconHelper iconHelper) { |
| 53 | super(context, parent, R.layout.item_doc_list); |
Ben Kwa | 8e3fd76 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 54 | |
Ben Lin | 19edf34 | 2016-09-09 16:18:18 -0700 | [diff] [blame] | 55 | mIconLayout = itemView.findViewById(android.R.id.icon); |
Ben Kwa | 8e3fd76 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 56 | mIconMime = (ImageView) itemView.findViewById(R.id.icon_mime); |
| 57 | mIconThumb = (ImageView) itemView.findViewById(R.id.icon_thumb); |
Ben Kwa | e59f1db | 2016-01-07 18:50:34 -0800 | [diff] [blame] | 58 | mIconCheck = (ImageView) itemView.findViewById(R.id.icon_check); |
Ben Lin | 19edf34 | 2016-09-09 16:18:18 -0700 | [diff] [blame] | 59 | mTitle = (TextView) itemView.findViewById(android.R.id.title); |
| 60 | mSummary = (TextView) itemView.findViewById(android.R.id.summary); |
| 61 | mSize = (TextView) itemView.findViewById(R.id.size); |
| 62 | mDate = (TextView) itemView.findViewById(R.id.date); |
Aga Wronska | 77298e5 | 2016-03-04 10:53:50 -0800 | [diff] [blame] | 63 | // Warning: mDetails view doesn't exists in layout-sw720dp-land layout |
| 64 | mDetails = (LinearLayout) itemView.findViewById(R.id.line2); |
Ben Kwa | c3c20f2 | 2015-12-22 14:03:30 -0800 | [diff] [blame] | 65 | |
| 66 | mIconHelper = iconHelper; |
Steve McKay | 358c3ec | 2016-10-21 09:16:57 -0700 | [diff] [blame] | 67 | mDoc = new DocumentInfo(); |
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. But it should be an error (see assert below) |
| 74 | // to be set to selected && be disabled. |
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(mIconCheck, checkAlpha).start(); |
Ben Kwa | 4c2f1a6 | 2016-03-16 12:13:21 -0700 | [diff] [blame] | 78 | } else { |
| 79 | mIconCheck.setAlpha(checkAlpha); |
| 80 | } |
Steve McKay | 2e74440 | 2016-03-08 11:37:22 -0800 | [diff] [blame] | 81 | |
| 82 | if (!itemView.isEnabled()) { |
| 83 | assert(!selected); |
| 84 | return; |
| 85 | } |
| 86 | |
Ben Kwa | 4c2f1a6 | 2016-03-16 12:13:21 -0700 | [diff] [blame] | 87 | super.setSelected(selected, animate); |
Steve McKay | 2e74440 | 2016-03-08 11:37:22 -0800 | [diff] [blame] | 88 | |
Ben Kwa | 4c2f1a6 | 2016-03-16 12:13:21 -0700 | [diff] [blame] | 89 | if (animate) { |
Steve McKay | 83ac678 | 2016-08-24 14:27:14 -0700 | [diff] [blame] | 90 | fade(mIconMime, 1f - checkAlpha).start(); |
| 91 | fade(mIconThumb, 1f - checkAlpha).start(); |
Ben Kwa | 4c2f1a6 | 2016-03-16 12:13:21 -0700 | [diff] [blame] | 92 | } else { |
| 93 | mIconMime.setAlpha(1f - checkAlpha); |
| 94 | mIconThumb.setAlpha(1f - checkAlpha); |
| 95 | } |
Ben Kwa | e59f1db | 2016-01-07 18:50:34 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Steve McKay | 2e74440 | 2016-03-08 11:37:22 -0800 | [diff] [blame] | 98 | @Override |
| 99 | public void setEnabled(boolean enabled) { |
| 100 | super.setEnabled(enabled); |
| 101 | |
| 102 | // Text colors enabled/disabled is handle via a color set. |
| 103 | final float imgAlpha = enabled ? 1f : DISABLED_ALPHA; |
| 104 | mIconMime.setAlpha(imgAlpha); |
| 105 | mIconThumb.setAlpha(imgAlpha); |
| 106 | } |
| 107 | |
Ben Lin | 19edf34 | 2016-09-09 16:18:18 -0700 | [diff] [blame] | 108 | @Override |
| 109 | public boolean isInDragHotspot(InputEvent event) { |
| 110 | // If itemView is activated = selected, then whole region is interactive |
| 111 | if (itemView.isActivated()) { |
| 112 | return true; |
| 113 | } |
| 114 | |
| 115 | // Do everything in global coordinates - it makes things simpler. |
| 116 | int[] coords = new int[2]; |
| 117 | mIconLayout.getLocationOnScreen(coords); |
| 118 | |
| 119 | Rect textBounds = new Rect(); |
| 120 | mTitle.getPaint().getTextBounds( |
| 121 | mTitle.getText().toString(), 0, mTitle.getText().length(), textBounds); |
| 122 | |
| 123 | Rect rect = new Rect( |
| 124 | coords[0], |
| 125 | coords[1], |
| 126 | coords[0] + mIconLayout.getWidth() + textBounds.width(), |
| 127 | coords[1] + Math.max(mIconLayout.getHeight(), textBounds.height())); |
| 128 | |
| 129 | // If the tap occurred inside icon or the text, these are interactive spots. |
| 130 | return rect.contains((int) event.getRawX(), (int) event.getRawY()); |
| 131 | } |
| 132 | |
Ben Kwa | 8e3fd76 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 133 | /** |
| 134 | * Bind this view to the given document for display. |
| 135 | * @param cursor Pointing to the item to be bound. |
| 136 | * @param modelId The model ID of the item. |
| 137 | * @param state Current display state. |
| 138 | */ |
| 139 | @Override |
Steve McKay | 358c3ec | 2016-10-21 09:16:57 -0700 | [diff] [blame] | 140 | public void bind(Cursor cursor, String modelId) { |
Steve McKay | 0af8afd | 2016-02-25 13:34:03 -0800 | [diff] [blame] | 141 | assert(cursor != null); |
Ben Kwa | 8e3fd76 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 142 | |
Steve McKay | 358c3ec | 2016-10-21 09:16:57 -0700 | [diff] [blame] | 143 | mModelId = modelId; |
Ben Kwa | 8e3fd76 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 144 | |
Steve McKay | 358c3ec | 2016-10-21 09:16:57 -0700 | [diff] [blame] | 145 | mDoc.updateFromCursor(cursor, getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY)); |
Ben Kwa | 8e3fd76 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 146 | |
Ben Kwa | 8e3fd76 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 147 | mIconHelper.stopLoading(mIconThumb); |
| 148 | |
| 149 | mIconMime.animate().cancel(); |
Ben Kwa | e59f1db | 2016-01-07 18:50:34 -0800 | [diff] [blame] | 150 | mIconMime.setAlpha(1f); |
Ben Kwa | 8e3fd76 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 151 | mIconThumb.animate().cancel(); |
Ben Kwa | e59f1db | 2016-01-07 18:50:34 -0800 | [diff] [blame] | 152 | mIconThumb.setAlpha(0f); |
Ben Kwa | 8e3fd76 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 153 | |
Steve McKay | 358c3ec | 2016-10-21 09:16:57 -0700 | [diff] [blame] | 154 | mIconHelper.load(mDoc, mIconThumb, mIconMime, null); |
Ben Kwa | 8e3fd76 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 155 | |
Steve McKay | 358c3ec | 2016-10-21 09:16:57 -0700 | [diff] [blame] | 156 | mTitle.setText(mDoc.displayName, TextView.BufferType.SPANNABLE); |
Ben Kwa | c3c20f2 | 2015-12-22 14:03:30 -0800 | [diff] [blame] | 157 | mTitle.setVisibility(View.VISIBLE); |
Ben Kwa | 8e3fd76 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 158 | |
Ben Kwa | 8e3fd76 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 159 | |
Aga Wronska | 77298e5 | 2016-03-04 10:53:50 -0800 | [diff] [blame] | 160 | boolean hasDetails = false; |
Steve McKay | 358c3ec | 2016-10-21 09:16:57 -0700 | [diff] [blame] | 161 | if (mDoc.isDirectory()) { |
Aga Wronska | 77298e5 | 2016-03-04 10:53:50 -0800 | [diff] [blame] | 162 | // Note, we don't show any details for any directory...ever. |
| 163 | hasDetails = false; |
Ben Kwa | 8e3fd76 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 164 | } else { |
Steve McKay | 358c3ec | 2016-10-21 09:16:57 -0700 | [diff] [blame] | 165 | if (mDoc.summary != null) { |
Steve McKay | 33e29c8 | 2016-03-01 11:26:00 -0800 | [diff] [blame] | 166 | hasDetails = true; |
Steve McKay | 358c3ec | 2016-10-21 09:16:57 -0700 | [diff] [blame] | 167 | mSummary.setText(mDoc.summary); |
Steve McKay | 33e29c8 | 2016-03-01 11:26:00 -0800 | [diff] [blame] | 168 | mSummary.setVisibility(View.VISIBLE); |
| 169 | } else { |
| 170 | mSummary.setVisibility(View.INVISIBLE); |
| 171 | } |
Ben Kwa | 8e3fd76 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 172 | |
Steve McKay | 358c3ec | 2016-10-21 09:16:57 -0700 | [diff] [blame] | 173 | if (mDoc.lastModified > 0) { |
Steve McKay | 33e29c8 | 2016-03-01 11:26:00 -0800 | [diff] [blame] | 174 | hasDetails = true; |
Steve McKay | 358c3ec | 2016-10-21 09:16:57 -0700 | [diff] [blame] | 175 | mDate.setText(Shared.formatTime(mContext, mDoc.lastModified)); |
Steve McKay | 73fae99 | 2016-03-02 14:04:54 -0800 | [diff] [blame] | 176 | } else { |
| 177 | mDate.setText(null); |
Steve McKay | 33e29c8 | 2016-03-01 11:26:00 -0800 | [diff] [blame] | 178 | } |
| 179 | |
Steve McKay | 358c3ec | 2016-10-21 09:16:57 -0700 | [diff] [blame] | 180 | if (mDoc.size > -1) { |
Steve McKay | 33e29c8 | 2016-03-01 11:26:00 -0800 | [diff] [blame] | 181 | hasDetails = true; |
Steve McKay | 33e29c8 | 2016-03-01 11:26:00 -0800 | [diff] [blame] | 182 | mSize.setVisibility(View.VISIBLE); |
Steve McKay | 358c3ec | 2016-10-21 09:16:57 -0700 | [diff] [blame] | 183 | mSize.setText(Formatter.formatFileSize(mContext, mDoc.size)); |
Steve McKay | 73fae99 | 2016-03-02 14:04:54 -0800 | [diff] [blame] | 184 | } else { |
| 185 | mSize.setVisibility(View.GONE); |
Steve McKay | 33e29c8 | 2016-03-01 11:26:00 -0800 | [diff] [blame] | 186 | } |
Aga Wronska | 77298e5 | 2016-03-04 10:53:50 -0800 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | // mDetails view doesn't exists in layout-sw720dp-land layout |
| 190 | if (mDetails != null) { |
Steve McKay | 33e29c8 | 2016-03-01 11:26:00 -0800 | [diff] [blame] | 191 | mDetails.setVisibility(hasDetails ? View.VISIBLE : View.GONE); |
Ben Kwa | 8e3fd76 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 192 | } |
Steve McKay | 358c3ec | 2016-10-21 09:16:57 -0700 | [diff] [blame] | 193 | |
| 194 | // TODO: Add document debug info |
| 195 | // Call includeDebugInfo |
Ben Kwa | 8e3fd76 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 196 | } |
Ben Kwa | 8e3fd76 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 197 | } |