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.animation.ObjectAnimator; 20 import android.animation.ValueAnimator; 21 import android.content.res.Resources; 22 import android.graphics.Bitmap; 23 import android.text.TextUtils; 24 import android.view.LayoutInflater; 25 import android.view.View; 26 import android.view.ViewGroup; 27 import android.view.ViewStub; 28 import android.view.animation.DecelerateInterpolator; 29 import android.widget.ImageView; 30 import android.widget.TextView; 31 32 import androidx.annotation.Nullable; 33 import androidx.annotation.VisibleForTesting; 34 35 import com.android.intentresolver.R; 36 import com.android.intentresolver.widget.ActionRow; 37 import com.android.intentresolver.widget.ScrollableImagePreviewView; 38 39 @VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE) 40 public abstract class ContentPreviewUi { 41 private static final int IMAGE_FADE_IN_MILLIS = 150; 42 static final String TAG = "ChooserPreview"; 43 44 @ContentPreviewType getType()45 public abstract int getType(); 46 display( Resources resources, LayoutInflater layoutInflater, ViewGroup parent, View headlineViewParent)47 public abstract ViewGroup display( 48 Resources resources, 49 LayoutInflater layoutInflater, 50 ViewGroup parent, 51 View headlineViewParent); 52 updateViewWithImage(ImageView imageView, Bitmap image)53 protected static void updateViewWithImage(ImageView imageView, Bitmap image) { 54 if (image == null) { 55 imageView.setVisibility(View.GONE); 56 return; 57 } 58 imageView.setVisibility(View.VISIBLE); 59 imageView.setAlpha(0.0f); 60 imageView.setImageBitmap(image); 61 62 ValueAnimator fadeAnim = ObjectAnimator.ofFloat(imageView, "alpha", 0.0f, 1.0f); 63 fadeAnim.setInterpolator(new DecelerateInterpolator(1.0f)); 64 fadeAnim.setDuration(IMAGE_FADE_IN_MILLIS); 65 fadeAnim.start(); 66 } 67 inflateHeadline(View layout)68 protected static void inflateHeadline(View layout) { 69 ViewStub stub = layout.findViewById(R.id.chooser_headline_row_stub); 70 if (stub != null) { 71 stub.inflate(); 72 } 73 } 74 displayHeadline(View layout, String headline)75 protected static void displayHeadline(View layout, String headline) { 76 TextView titleView = layout == null ? null : layout.findViewById(R.id.headline); 77 if (titleView == null) { 78 return; 79 } 80 if (!TextUtils.isEmpty(headline)) { 81 titleView.setText(headline); 82 titleView.setVisibility(View.VISIBLE); 83 } else { 84 titleView.setVisibility(View.GONE); 85 } 86 } 87 displayMetadata(View layout, @Nullable CharSequence metadata)88 protected static void displayMetadata(View layout, @Nullable CharSequence metadata) { 89 TextView metadataView = layout == null ? null : layout.findViewById(R.id.metadata); 90 if (metadataView == null) { 91 return; 92 } 93 if (!TextUtils.isEmpty(metadata)) { 94 metadataView.setText(metadata); 95 metadataView.setVisibility(View.VISIBLE); 96 } else { 97 metadataView.setVisibility(View.GONE); 98 } 99 } 100 displayModifyShareAction( View layout, @Nullable ActionRow.Action modifyShareAction)101 static void displayModifyShareAction( 102 View layout, @Nullable ActionRow.Action modifyShareAction) { 103 TextView modifyShareView = 104 layout == null ? null : layout.findViewById(R.id.reselection_action); 105 if (modifyShareView == null) { 106 return; 107 } 108 if (modifyShareAction != null) { 109 modifyShareView.setText(modifyShareAction.getLabel()); 110 modifyShareView.setVisibility(View.VISIBLE); 111 modifyShareView.setOnClickListener(view -> modifyShareAction.getOnClicked().run()); 112 } else { 113 modifyShareView.setVisibility(View.GONE); 114 } 115 } 116 getPreviewType( MimeTypeClassifier typeClassifier, String mimeType)117 static ScrollableImagePreviewView.PreviewType getPreviewType( 118 MimeTypeClassifier typeClassifier, String mimeType) { 119 if (mimeType == null) { 120 return ScrollableImagePreviewView.PreviewType.File; 121 } 122 if (typeClassifier.isImageType(mimeType)) { 123 return ScrollableImagePreviewView.PreviewType.Image; 124 } 125 if (typeClassifier.isVideoType(mimeType)) { 126 return ScrollableImagePreviewView.PreviewType.Video; 127 } 128 return ScrollableImagePreviewView.PreviewType.File; 129 } 130 } 131