1 /* 2 * Copyright (C) 2023 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.intentresolver.contentpreview; 18 19 import android.content.res.Resources; 20 import android.util.Log; 21 import android.util.PluralsMessageFormatter; 22 import android.view.LayoutInflater; 23 import android.view.View; 24 import android.view.ViewGroup; 25 import android.widget.ImageView; 26 import android.widget.TextView; 27 28 import androidx.annotation.Nullable; 29 30 import com.android.intentresolver.R; 31 import com.android.intentresolver.widget.ActionRow; 32 33 import java.util.HashMap; 34 import java.util.List; 35 import java.util.Map; 36 37 class FileContentPreviewUi extends ContentPreviewUi { 38 private static final String PLURALS_COUNT = "count"; 39 40 @Nullable 41 private String mFirstFileName = null; 42 private final int mFileCount; 43 private final ChooserContentPreviewUi.ActionFactory mActionFactory; 44 private final HeadlineGenerator mHeadlineGenerator; 45 @Nullable 46 private final CharSequence mMetadata; 47 @Nullable 48 private ViewGroup mContentPreview = null; 49 FileContentPreviewUi( int fileCount, ChooserContentPreviewUi.ActionFactory actionFactory, HeadlineGenerator headlineGenerator, @Nullable CharSequence metadata )50 FileContentPreviewUi( 51 int fileCount, 52 ChooserContentPreviewUi.ActionFactory actionFactory, 53 HeadlineGenerator headlineGenerator, 54 @Nullable CharSequence metadata 55 ) { 56 mFileCount = fileCount; 57 mActionFactory = actionFactory; 58 mHeadlineGenerator = headlineGenerator; 59 mMetadata = metadata; 60 } 61 62 @Override getType()63 public int getType() { 64 return ContentPreviewType.CONTENT_PREVIEW_FILE; 65 } 66 setFirstFileName(String fileName)67 public void setFirstFileName(String fileName) { 68 mFirstFileName = fileName; 69 if (mContentPreview != null) { 70 showFileName(mContentPreview, fileName); 71 } 72 } 73 74 @Override display( Resources resources, LayoutInflater layoutInflater, ViewGroup parent, View headlineViewParent)75 public ViewGroup display( 76 Resources resources, 77 LayoutInflater layoutInflater, 78 ViewGroup parent, 79 View headlineViewParent) { 80 return displayInternal(resources, layoutInflater, parent, headlineViewParent); 81 } 82 displayInternal( Resources resources, LayoutInflater layoutInflater, ViewGroup parent, View headlineViewParent)83 private ViewGroup displayInternal( 84 Resources resources, 85 LayoutInflater layoutInflater, 86 ViewGroup parent, 87 View headlineViewParent) { 88 mContentPreview = (ViewGroup) layoutInflater.inflate( 89 R.layout.chooser_grid_preview_file, parent, false); 90 inflateHeadline(headlineViewParent); 91 92 displayHeadline(headlineViewParent, mHeadlineGenerator.getFilesHeadline(mFileCount)); 93 displayMetadata(headlineViewParent, mMetadata); 94 95 if (mFileCount == 0) { 96 mContentPreview.setVisibility(View.GONE); 97 Log.i(TAG, "Appears to be no uris available in EXTRA_STREAM," 98 + " removing preview area"); 99 return mContentPreview; 100 } 101 102 if (mFirstFileName != null) { 103 showFileName(mContentPreview, mFirstFileName); 104 } 105 106 TextView secondLine = mContentPreview.findViewById( 107 R.id.content_preview_more_files); 108 if (mFileCount > 1) { 109 int remUriCount = mFileCount - 1; 110 Map<String, Object> arguments = new HashMap<>(); 111 arguments.put(PLURALS_COUNT, remUriCount); 112 secondLine.setText( 113 PluralsMessageFormatter.format(resources, arguments, R.string.more_files)); 114 } else { 115 ImageView icon = mContentPreview.findViewById(R.id.content_preview_file_icon); 116 icon.setImageResource(R.drawable.single_file); 117 secondLine.setVisibility(View.GONE); 118 } 119 120 final ActionRow actionRow = 121 mContentPreview.findViewById(com.android.internal.R.id.chooser_action_row); 122 List<ActionRow.Action> actions = mActionFactory.createCustomActions(); 123 actionRow.setActions(actions); 124 125 return mContentPreview; 126 } 127 showFileName(ViewGroup contentPreview, String name)128 private void showFileName(ViewGroup contentPreview, String name) { 129 TextView fileNameView = contentPreview.requireViewById(R.id.content_preview_filename); 130 fileNameView.setText(name); 131 } 132 } 133