1 /* 2 * Copyright 2019 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.car.apps.common.imaging; 18 19 import android.content.Context; 20 import android.graphics.drawable.BitmapDrawable; 21 import android.graphics.drawable.Drawable; 22 import android.util.Size; 23 import android.widget.ImageView; 24 25 import androidx.annotation.Nullable; 26 27 import com.android.car.apps.common.CommonFlags; 28 import com.android.car.apps.common.R; 29 30 /** 31 * Binds images to an image view.<p/> 32 * While making a new image request (including passing a null {@link ImageBinder.ImageRef} in 33 * {@link #setImage}) will cancel the current image request (if any), RecyclerView doesn't 34 * always reuse all its views, causing multiple requests to not be canceled. On a slow network, 35 * those requests then take time to execute and can make it look like the application has 36 * stopped loading images if the user keeps browsing. To prevent that, override: 37 * {@link RecyclerView.Adapter#onViewDetachedFromWindow} and call {@link #maybeCancelLoading} 38 * {@link RecyclerView.Adapter#onViewAttachedToWindow} and call {@link #maybeRestartLoading}. 39 * 40 * @param <T> see {@link ImageRef}. 41 */ 42 public class ImageViewBinder<T extends ImageBinder.ImageRef> extends ImageBinder<T> { 43 44 @Nullable 45 private final ImageView mImageView; 46 private final boolean mFlagBitmaps; 47 48 private T mSavedRef; 49 private boolean mCancelled; 50 51 /** See {@link ImageViewBinder} and {@link ImageBinder}. */ ImageViewBinder(Size maxImageSize, @Nullable ImageView imageView)52 public ImageViewBinder(Size maxImageSize, @Nullable ImageView imageView) { 53 this(PlaceholderType.FOREGROUND, maxImageSize, imageView, false); 54 } 55 56 /** 57 * See {@link ImageViewBinder} and {@link ImageBinder}. 58 * @param flagBitmaps whether this binder should flag bitmap drawables if flagging is enabled. 59 */ ImageViewBinder(PlaceholderType type, Size maxImageSize, @Nullable ImageView imageView, boolean flagBitmaps)60 public ImageViewBinder(PlaceholderType type, Size maxImageSize, 61 @Nullable ImageView imageView, boolean flagBitmaps) { 62 super(type, maxImageSize); 63 mImageView = imageView; 64 mFlagBitmaps = flagBitmaps; 65 } 66 67 @Override setDrawable(@ullable Drawable drawable)68 protected void setDrawable(@Nullable Drawable drawable) { 69 if (mImageView != null) { 70 mImageView.setImageDrawable(drawable); 71 if (mFlagBitmaps) { 72 CommonFlags flags = CommonFlags.getInstance(mImageView.getContext()); 73 if (flags.shouldFlagImproperImageRefs()) { 74 if (drawable instanceof BitmapDrawable) { 75 int tint = mImageView.getContext().getColor( 76 R.color.improper_image_refs_tint_color); 77 mImageView.setColorFilter(tint); 78 } else { 79 mImageView.clearColorFilter(); 80 } 81 } 82 } 83 } 84 } 85 86 /** 87 * Loads a new {@link ImageRef}. The previous request (if any) will be canceled. 88 */ 89 @Override setImage(Context context, @Nullable T newRef)90 public void setImage(Context context, @Nullable T newRef) { 91 mSavedRef = newRef; 92 mCancelled = false; 93 if (mImageView != null) { 94 super.setImage(context, newRef); 95 } 96 } 97 98 /** 99 * Restarts the image loading request if {@link #setImage} was called with a valid reference 100 * that could not be loaded before {@link #maybeCancelLoading} was called. 101 */ maybeRestartLoading(Context context)102 public void maybeRestartLoading(Context context) { 103 if (mCancelled) { 104 setImage(context, mSavedRef); 105 } 106 } 107 108 /** 109 * Cancels the current loading request (if any) so it doesn't take cycles when the imageView 110 * doesn't need the image (like when the view was moved off screen). 111 */ maybeCancelLoading(Context context)112 public void maybeCancelLoading(Context context) { 113 mCancelled = true; 114 if (mImageView != null) { 115 super.setImage(context, null); // Call super to keep mSavedRef. 116 } 117 } 118 119 @Override prepareForNewBinding(Context context)120 protected void prepareForNewBinding(Context context) { 121 mImageView.setImageBitmap(null); 122 mImageView.setImageDrawable(null); 123 mImageView.clearColorFilter(); 124 // Call super last to setup the default loading drawable. 125 super.prepareForNewBinding(context); 126 } 127 128 } 129