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