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