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.base.DocumentInfo.getCursorLong; 20 import static com.android.documentsui.base.DocumentInfo.getCursorString; 21 22 import androidx.annotation.ColorInt; 23 import android.content.Context; 24 import android.content.res.TypedArray; 25 import android.database.Cursor; 26 import android.provider.DocumentsContract.Document; 27 import android.text.format.Formatter; 28 import android.view.MotionEvent; 29 import android.view.View; 30 import android.view.ViewGroup; 31 import android.widget.ImageView; 32 import android.widget.TextView; 33 34 import com.android.documentsui.R; 35 import com.android.documentsui.base.DocumentInfo; 36 import com.android.documentsui.base.Shared; 37 import com.android.documentsui.roots.RootCursorWrapper; 38 import com.android.documentsui.ui.Views; 39 40 import java.util.function.Function; 41 42 final class GridDocumentHolder extends DocumentHolder { 43 44 final TextView mTitle; 45 final TextView mDate; 46 final TextView mDetails; 47 final ImageView mIconMimeLg; 48 final ImageView mIconMimeSm; 49 final ImageView mIconThumb; 50 final ImageView mIconCheck; 51 final IconHelper mIconHelper; 52 final View mIconLayout; 53 final View mPreviewIcon; 54 55 // This is used in as a convenience in our bind method. 56 private final DocumentInfo mDoc = new DocumentInfo(); 57 GridDocumentHolder(Context context, ViewGroup parent, IconHelper iconHelper)58 public GridDocumentHolder(Context context, ViewGroup parent, IconHelper iconHelper) { 59 super(context, parent, R.layout.item_doc_grid); 60 61 mIconLayout = itemView.findViewById(R.id.icon); 62 mTitle = (TextView) itemView.findViewById(android.R.id.title); 63 mDate = (TextView) itemView.findViewById(R.id.date); 64 mDetails = (TextView) itemView.findViewById(R.id.details); 65 mIconMimeLg = (ImageView) itemView.findViewById(R.id.icon_mime_lg); 66 mIconMimeSm = (ImageView) itemView.findViewById(R.id.icon_mime_sm); 67 mIconThumb = (ImageView) itemView.findViewById(R.id.icon_thumb); 68 mIconCheck = (ImageView) itemView.findViewById(R.id.icon_check); 69 mPreviewIcon = itemView.findViewById(R.id.preview_icon); 70 71 mIconHelper = iconHelper; 72 } 73 74 @Override setSelected(boolean selected, boolean animate)75 public void setSelected(boolean selected, boolean animate) { 76 // We always want to make sure our check box disappears if we're not selected, 77 // even if the item is disabled. This is because this object can be reused 78 // and this method will be called to setup initial state. 79 float checkAlpha = selected ? 1f : 0f; 80 if (animate) { 81 fade(mIconMimeSm, checkAlpha).start(); 82 fade(mIconCheck, checkAlpha).start(); 83 } else { 84 mIconCheck.setAlpha(checkAlpha); 85 } 86 87 // But it should be an error to be set to selected && be disabled. 88 if (!itemView.isEnabled()) { 89 assert(!selected); 90 return; 91 } 92 93 super.setSelected(selected, animate); 94 95 if (animate) { 96 fade(mIconMimeSm, 1f - checkAlpha).start(); 97 } else { 98 mIconMimeSm.setAlpha(1f - checkAlpha); 99 } 100 } 101 102 @Override setEnabled(boolean enabled)103 public void setEnabled(boolean enabled) { 104 super.setEnabled(enabled); 105 106 float imgAlpha = enabled ? 1f : DISABLED_ALPHA; 107 108 mIconMimeLg.setAlpha(imgAlpha); 109 mIconMimeSm.setAlpha(imgAlpha); 110 mIconThumb.setAlpha(imgAlpha); 111 } 112 113 @Override bindPreviewIcon(boolean show, Function<View, Boolean> clickCallback)114 public void bindPreviewIcon(boolean show, Function<View, Boolean> clickCallback) { 115 mPreviewIcon.setVisibility(show ? View.VISIBLE : View.GONE); 116 if (show) { 117 mPreviewIcon.setContentDescription( 118 itemView.getResources().getString(R.string.preview_file, mDoc.displayName)); 119 mPreviewIcon.setAccessibilityDelegate(new PreviewAccessibilityDelegate(clickCallback)); 120 } 121 } 122 123 @Override inDragRegion(MotionEvent event)124 public boolean inDragRegion(MotionEvent event) { 125 // Entire grid box should be draggable 126 return true; 127 } 128 129 @Override inSelectRegion(MotionEvent event)130 public boolean inSelectRegion(MotionEvent event) { 131 return Views.isEventOver(event, mIconLayout); 132 } 133 134 @Override inPreviewIconRegion(MotionEvent event)135 public boolean inPreviewIconRegion(MotionEvent event) { 136 return Views.isEventOver(event, mPreviewIcon); 137 } 138 139 /** 140 * Bind this view to the given document for display. 141 * @param cursor Pointing to the item to be bound. 142 * @param modelId The model ID of the item. 143 */ 144 @Override bind(Cursor cursor, String modelId)145 public void bind(Cursor cursor, String modelId) { 146 assert(cursor != null); 147 148 mModelId = modelId; 149 150 mDoc.updateFromCursor(cursor, getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY)); 151 152 mIconHelper.stopLoading(mIconThumb); 153 154 mIconMimeLg.animate().cancel(); 155 mIconMimeLg.setAlpha(1f); 156 mIconThumb.animate().cancel(); 157 mIconThumb.setAlpha(0f); 158 159 mIconHelper.load(mDoc, mIconThumb, mIconMimeLg, mIconMimeSm); 160 161 mTitle.setText(mDoc.displayName, TextView.BufferType.SPANNABLE); 162 mTitle.setVisibility(View.VISIBLE); 163 164 // If file is partial, we want to show summary field as that's more relevant than fileSize 165 // and date 166 if (mDoc.isPartial()) { 167 final String docSummary = getCursorString(cursor, Document.COLUMN_SUMMARY); 168 mDetails.setVisibility(View.VISIBLE); 169 mDate.setText(null); 170 mDetails.setText(docSummary); 171 } else { 172 if (mDoc.lastModified == -1) { 173 mDate.setText(null); 174 } else { 175 mDate.setText(Shared.formatTime(mContext, mDoc.lastModified)); 176 } 177 178 final long docSize = getCursorLong(cursor, Document.COLUMN_SIZE); 179 if (mDoc.isDirectory() || docSize == -1) { 180 mDetails.setVisibility(View.GONE); 181 } else { 182 mDetails.setVisibility(View.VISIBLE); 183 mDetails.setText(Formatter.formatFileSize(mContext, docSize)); 184 } 185 } 186 } 187 } 188