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.DevicePolicyResources.Drawables.Style.SOLID_COLORED; 20 import static com.android.documentsui.DevicePolicyResources.Drawables.WORK_PROFILE_ICON; 21 import static com.android.documentsui.base.DocumentInfo.getCursorInt; 22 import static com.android.documentsui.base.DocumentInfo.getCursorString; 23 24 import android.app.admin.DevicePolicyManager; 25 import android.content.Context; 26 import android.database.Cursor; 27 import android.graphics.Rect; 28 import android.graphics.drawable.Drawable; 29 import android.os.Build; 30 import android.text.TextUtils; 31 import android.text.format.Formatter; 32 import android.util.Log; 33 import android.view.MotionEvent; 34 import android.view.View; 35 import android.view.ViewGroup; 36 import android.widget.ImageView; 37 import android.widget.LinearLayout; 38 import android.widget.TextView; 39 40 import androidx.annotation.Nullable; 41 import androidx.annotation.RequiresApi; 42 43 import com.android.documentsui.ConfigStore; 44 import com.android.documentsui.DocumentsApplication; 45 import com.android.documentsui.R; 46 import com.android.documentsui.base.DocumentInfo; 47 import com.android.documentsui.base.Lookup; 48 import com.android.documentsui.base.Shared; 49 import com.android.documentsui.base.State; 50 import com.android.documentsui.base.UserId; 51 import com.android.documentsui.roots.RootCursorWrapper; 52 import com.android.documentsui.ui.Views; 53 import com.android.modules.utils.build.SdkLevel; 54 55 import java.util.ArrayList; 56 import java.util.Map; 57 import java.util.function.Function; 58 59 final class ListDocumentHolder extends DocumentHolder { 60 private static final String TAG = "ListDocumentHolder"; 61 62 private final TextView mTitle; 63 private final @Nullable TextView mDate; // Non-null for tablets/sw720dp, null for other devices. 64 private final @Nullable TextView mSize; // Non-null for tablets/sw720dp, null for other devices. 65 private final @Nullable TextView mType; // Non-null for tablets/sw720dp, null for other devices. 66 // Container for date + size + summary, null only for tablets/sw720dp 67 private final @Nullable LinearLayout mDetails; 68 // TextView for date + size + summary, null only for tablets/sw720dp 69 private final @Nullable TextView mMetadataView; 70 private final ImageView mIconMime; 71 private final ImageView mIconThumb; 72 private final ImageView mIconCheck; 73 private final ImageView mIconBadge; 74 private final View mIconLayout; 75 final View mPreviewIcon; 76 77 private final IconHelper mIconHelper; 78 private final Lookup<String, String> mFileTypeLookup; 79 // This is used in as a convenience in our bind method. 80 private final DocumentInfo mDoc; 81 ListDocumentHolder(Context context, ViewGroup parent, IconHelper iconHelper, Lookup<String, String> fileTypeLookup, ConfigStore configStore)82 public ListDocumentHolder(Context context, ViewGroup parent, IconHelper iconHelper, 83 Lookup<String, String> fileTypeLookup, ConfigStore configStore) { 84 super(context, parent, R.layout.item_doc_list, configStore); 85 86 mIconLayout = itemView.findViewById(R.id.icon); 87 mIconMime = (ImageView) itemView.findViewById(R.id.icon_mime); 88 mIconThumb = (ImageView) itemView.findViewById(R.id.icon_thumb); 89 mIconCheck = (ImageView) itemView.findViewById(R.id.icon_check); 90 mIconBadge = (ImageView) itemView.findViewById(R.id.icon_profile_badge); 91 mTitle = (TextView) itemView.findViewById(android.R.id.title); 92 mSize = (TextView) itemView.findViewById(R.id.size); 93 mDate = (TextView) itemView.findViewById(R.id.date); 94 mType = (TextView) itemView.findViewById(R.id.file_type); 95 mMetadataView = (TextView) itemView.findViewById(R.id.metadata); 96 // Warning: mDetails view doesn't exists in layout-sw720dp-land layout 97 mDetails = (LinearLayout) itemView.findViewById(R.id.line2); 98 mPreviewIcon = itemView.findViewById(R.id.preview_icon); 99 100 mIconHelper = iconHelper; 101 mFileTypeLookup = fileTypeLookup; 102 mDoc = new DocumentInfo(); 103 104 if (SdkLevel.isAtLeastT() && !mConfigStore.isPrivateSpaceInDocsUIEnabled()) { 105 setUpdatableWorkProfileIcon(context); 106 } 107 } 108 109 @RequiresApi(Build.VERSION_CODES.TIRAMISU) setUpdatableWorkProfileIcon(Context context)110 private void setUpdatableWorkProfileIcon(Context context) { 111 DevicePolicyManager dpm = context.getSystemService(DevicePolicyManager.class); 112 Drawable drawable = dpm.getResources().getDrawable(WORK_PROFILE_ICON, SOLID_COLORED, () -> 113 context.getDrawable(R.drawable.ic_briefcase)); 114 mIconBadge.setImageDrawable(drawable); 115 } 116 117 @Override setSelected(boolean selected, boolean animate)118 public void setSelected(boolean selected, boolean animate) { 119 // We always want to make sure our check box disappears if we're not selected, 120 // even if the item is disabled. But it should be an error (see assert below) 121 // to be set to selected && be disabled. 122 float checkAlpha = selected ? 1f : 0f; 123 if (animate) { 124 fade(mIconCheck, checkAlpha).start(); 125 } else { 126 mIconCheck.setAlpha(checkAlpha); 127 } 128 129 if (!itemView.isEnabled()) { 130 assert (!selected); 131 } 132 133 super.setSelected(selected, animate); 134 135 if (animate) { 136 fade(mIconMime, 1f - checkAlpha).start(); 137 fade(mIconThumb, 1f - checkAlpha).start(); 138 } else { 139 mIconMime.setAlpha(1f - checkAlpha); 140 mIconThumb.setAlpha(1f - checkAlpha); 141 } 142 } 143 144 @Override setEnabled(boolean enabled)145 public void setEnabled(boolean enabled) { 146 super.setEnabled(enabled); 147 148 // Text colors enabled/disabled is handle via a color set. 149 final float imgAlpha = enabled ? 1f : DISABLED_ALPHA; 150 mIconMime.setAlpha(imgAlpha); 151 mIconThumb.setAlpha(imgAlpha); 152 } 153 154 @Override bindPreviewIcon(boolean show, Function<View, Boolean> clickCallback)155 public void bindPreviewIcon(boolean show, Function<View, Boolean> clickCallback) { 156 if (mDoc.isDirectory()) { 157 mPreviewIcon.setVisibility(View.GONE); 158 } else { 159 mPreviewIcon.setVisibility(show ? View.VISIBLE : View.GONE); 160 if (show) { 161 mPreviewIcon.setContentDescription( 162 getPreviewIconContentDescription( 163 mIconHelper.shouldShowBadge(mDoc.userId.getIdentifier()), 164 mDoc.displayName, mDoc.userId)); 165 mPreviewIcon.setAccessibilityDelegate( 166 new PreviewAccessibilityDelegate(clickCallback)); 167 } 168 } 169 } 170 171 @Override bindBriefcaseIcon(boolean show)172 public void bindBriefcaseIcon(boolean show) { 173 mIconBadge.setVisibility(show ? View.VISIBLE : View.GONE); 174 } 175 176 @Override 177 @RequiresApi(Build.VERSION_CODES.S) bindProfileIcon(boolean show, int userIdIdentifier)178 public void bindProfileIcon(boolean show, int userIdIdentifier) { 179 Map<UserId, Drawable> userIdToBadgeMap = DocumentsApplication.getUserManagerState( 180 mContext).getUserIdToBadgeMap(); 181 Drawable drawable = userIdToBadgeMap.get(UserId.of(userIdIdentifier)); 182 mIconBadge.setImageDrawable(drawable); 183 mIconBadge.setVisibility(show ? View.VISIBLE : View.GONE); 184 mIconBadge.setContentDescription(mIconHelper.getProfileLabel(userIdIdentifier)); 185 } 186 187 @Override inDragRegion(MotionEvent event)188 public boolean inDragRegion(MotionEvent event) { 189 // If itemView is activated = selected, then whole region is interactive 190 if (itemView.isActivated()) { 191 return true; 192 } 193 194 // Do everything in global coordinates - it makes things simpler. 195 int[] coords = new int[2]; 196 mIconLayout.getLocationOnScreen(coords); 197 198 Rect textBounds = new Rect(); 199 mTitle.getPaint().getTextBounds( 200 mTitle.getText().toString(), 0, mTitle.getText().length(), textBounds); 201 202 Rect rect = new Rect( 203 coords[0], 204 coords[1], 205 coords[0] + mIconLayout.getWidth() + textBounds.width(), 206 coords[1] + Math.max(mIconLayout.getHeight(), textBounds.height())); 207 208 // If the tap occurred inside icon or the text, these are interactive spots. 209 return rect.contains((int) event.getRawX(), (int) event.getRawY()); 210 } 211 212 @Override inSelectRegion(MotionEvent event)213 public boolean inSelectRegion(MotionEvent event) { 214 return (mDoc.isDirectory() && !(mAction == State.ACTION_BROWSE)) ? 215 false : Views.isEventOver(event, itemView.getParent(), mIconLayout); 216 } 217 218 @Override inPreviewIconRegion(MotionEvent event)219 public boolean inPreviewIconRegion(MotionEvent event) { 220 return Views.isEventOver(event, itemView.getParent(), mPreviewIcon); 221 } 222 223 /** 224 * Bind this view to the given document for display. 225 * 226 * @param cursor Pointing to the item to be bound. 227 * @param modelId The model ID of the item. 228 */ 229 @Override bind(Cursor cursor, String modelId)230 public void bind(Cursor cursor, String modelId) { 231 assert (cursor != null); 232 233 mModelId = modelId; 234 235 mDoc.updateFromCursor(cursor, 236 UserId.of(getCursorInt(cursor, RootCursorWrapper.COLUMN_USER_ID)), 237 getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY)); 238 239 mIconHelper.stopLoading(mIconThumb); 240 241 mIconMime.animate().cancel(); 242 mIconMime.setAlpha(1f); 243 mIconThumb.animate().cancel(); 244 mIconThumb.setAlpha(0f); 245 246 mIconHelper.load(mDoc, mIconThumb, mIconMime, null); 247 248 mTitle.setText(mDoc.displayName, TextView.BufferType.SPANNABLE); 249 mTitle.setVisibility(View.VISIBLE); 250 251 if (mDoc.isDirectory()) { 252 // Note, we don't show any details for any directory...ever. 253 if (mDetails != null) { 254 // Non-tablets 255 mDetails.setVisibility(View.GONE); 256 } 257 } else { 258 // For tablets metadata is provided in columns mDate, mSize, mType. 259 // For other devices mMetadataView consolidates the metadata info. 260 if (mMetadataView != null) { 261 // Non-tablets 262 boolean hasDetails = false; 263 ArrayList<String> metadataList = new ArrayList<>(); 264 if (mDoc.lastModified > 0) { 265 hasDetails = true; 266 metadataList.add(Shared.formatTime(mContext, mDoc.lastModified)); 267 } 268 if (mDoc.size > -1) { 269 hasDetails = true; 270 metadataList.add(Formatter.formatFileSize(mContext, mDoc.size)); 271 } 272 metadataList.add(mFileTypeLookup.lookup(mDoc.mimeType)); 273 mMetadataView.setText(TextUtils.join(", ", metadataList)); 274 if (mDetails != null) { 275 mDetails.setVisibility(hasDetails ? View.VISIBLE : View.GONE); 276 } else { 277 Log.w(TAG, "mDetails is unexpectedly null for non-tablet devices!"); 278 } 279 } else { 280 // Tablets 281 if (mDoc.lastModified > 0) { 282 mDate.setVisibility(View.VISIBLE); 283 mDate.setText(Shared.formatTime(mContext, mDoc.lastModified)); 284 } else { 285 mDate.setVisibility(View.INVISIBLE); 286 } 287 if (mDoc.size > -1) { 288 mSize.setVisibility(View.VISIBLE); 289 mSize.setText(Formatter.formatFileSize(mContext, mDoc.size)); 290 } else { 291 mSize.setVisibility(View.INVISIBLE); 292 } 293 mType.setText(mFileTypeLookup.lookup(mDoc.mimeType)); 294 } 295 } 296 297 // TODO: Add document debug info 298 // Call includeDebugInfo 299 } 300 } 301