Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(588)

Side by Side Diff: chrome/android/java/src/org/chromium/chrome/browser/photo_picker/PickerBitmapViewHolder.java

Issue 2845773003: Photo Picker Dialog: Add caching for the decoded images. (Closed)
Patch Set: Address feedback from Theresa Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.chrome.browser.photo_picker; 5 package org.chromium.chrome.browser.photo_picker;
6 6
7 import android.content.res.Resources;
7 import android.graphics.Bitmap; 8 import android.graphics.Bitmap;
9 import android.os.AsyncTask;
8 import android.support.v7.widget.RecyclerView.ViewHolder; 10 import android.support.v7.widget.RecyclerView.ViewHolder;
9 import android.text.TextUtils; 11 import android.text.TextUtils;
10 12
13 import org.chromium.chrome.R;
14
11 import java.util.List; 15 import java.util.List;
12 16
13 /** 17 /**
14 * Holds on to a {@link PickerBitmapView} that displays information about a pick er bitmap. 18 * Holds on to a {@link PickerBitmapView} that displays information about a pick er bitmap.
15 */ 19 */
16 public class PickerBitmapViewHolder 20 public class PickerBitmapViewHolder
17 extends ViewHolder implements DecoderServiceHost.ImageDecodedCallback { 21 extends ViewHolder implements DecoderServiceHost.ImageDecodedCallback {
18 // Our parent category. 22 // Our parent category.
19 private PickerCategoryView mCategoryView; 23 private PickerCategoryView mCategoryView;
20 24
21 // The bitmap view we are holding on to. 25 // The bitmap view we are holding on to.
22 private final PickerBitmapView mItemView; 26 private final PickerBitmapView mItemView;
23 27
28 // The screen density.
29 private final float mDensity;
Theresa 2017/05/09 14:59:27 This value isn't used anymore, right?
Finnur 2017/05/09 15:08:22 Correct. Done.
30
24 // The request we are showing the bitmap for. 31 // The request we are showing the bitmap for.
25 private PickerBitmap mBitmapDetails; 32 private PickerBitmap mBitmapDetails;
26 33
27 /** 34 /**
28 * The PickerBitmapViewHolder. 35 * The PickerBitmapViewHolder.
29 * @param itemView The {@link PickerBitmapView} view for showing the image. 36 * @param itemView The {@link PickerBitmapView} view for showing the image.
30 */ 37 */
31 public PickerBitmapViewHolder(PickerBitmapView itemView) { 38 public PickerBitmapViewHolder(PickerBitmapView itemView) {
32 super(itemView); 39 super(itemView);
33 mItemView = itemView; 40 mItemView = itemView;
41 mDensity = itemView.getContext().getResources().getDisplayMetrics().dens ity;
34 } 42 }
35 43
36 // DecoderServiceHost.ImageDecodedCallback 44 // DecoderServiceHost.ImageDecodedCallback
37 45
38 @Override 46 @Override
39 public void imageDecodedCallback(String filePath, Bitmap bitmap) { 47 public void imageDecodedCallback(String filePath, Bitmap bitmap) {
40 if (bitmap == null || bitmap.getWidth() == 0 || bitmap.getHeight() == 0) { 48 if (bitmap == null || bitmap.getWidth() == 0 || bitmap.getHeight() == 0) {
41 return; 49 return;
42 } 50 }
43 51
52 if (mCategoryView.getHighResBitmaps().get(filePath) == null) {
53 mCategoryView.getHighResBitmaps().put(filePath, bitmap);
54 }
55
56 if (mCategoryView.getLowResBitmaps().get(filePath) == null) {
57 Resources resources = mItemView.getContext().getResources();
58 new BitmapScalerTask(mCategoryView.getLowResBitmaps(), filePath,
59 resources.getDimensionPixelSize(R.dimen.photo_picker_grainy_ thumbnail_size))
60 .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, bitmap);
61 }
62
44 if (!TextUtils.equals(mBitmapDetails.getFilePath(), filePath)) { 63 if (!TextUtils.equals(mBitmapDetails.getFilePath(), filePath)) {
45 return; 64 return;
46 } 65 }
47 66
48 mItemView.setThumbnailBitmap(bitmap); 67 mItemView.setThumbnailBitmap(bitmap);
49 } 68 }
50 69
51 /** 70 /**
52 * Display a single item from |position| in the PickerCategoryView. 71 * Display a single item from |position| in the PickerCategoryView.
53 * @param categoryView The PickerCategoryView to use to fetch the image. 72 * @param categoryView The PickerCategoryView to use to fetch the image.
54 * @param position The position of the item to fetch. 73 * @param position The position of the item to fetch.
55 */ 74 */
56 public void displayItem(PickerCategoryView categoryView, int position) { 75 public void displayItem(PickerCategoryView categoryView, int position) {
57 mCategoryView = categoryView; 76 mCategoryView = categoryView;
58 77
59 List<PickerBitmap> pickerBitmaps = mCategoryView.getPickerBitmaps(); 78 List<PickerBitmap> pickerBitmaps = mCategoryView.getPickerBitmaps();
60 mBitmapDetails = pickerBitmaps.get(position); 79 mBitmapDetails = pickerBitmaps.get(position);
61 80
62 if (mBitmapDetails.type() == PickerBitmap.CAMERA 81 if (mBitmapDetails.type() == PickerBitmap.CAMERA
63 || mBitmapDetails.type() == PickerBitmap.GALLERY) { 82 || mBitmapDetails.type() == PickerBitmap.GALLERY) {
64 mItemView.initialize(mBitmapDetails, null, false); 83 mItemView.initialize(mBitmapDetails, null, false);
65 return; 84 return;
66 } 85 }
67 86
68 // TODO(finnur): Use cached image, if available. 87 String filePath = mBitmapDetails.getFilePath();
69 88 Bitmap original = mCategoryView.getHighResBitmaps().get(filePath);
70 mItemView.initialize(mBitmapDetails, null, true); 89 if (original != null) {
90 mItemView.initialize(mBitmapDetails, original, false);
91 return;
92 }
71 93
72 int size = mCategoryView.getImageSize(); 94 int size = mCategoryView.getImageSize();
73 mCategoryView.getDecoderServiceHost().decodeImage(mBitmapDetails.getFile Path(), size, this); 95 Bitmap placeholder = mCategoryView.getLowResBitmaps().get(filePath);
96 if (placeholder != null) {
97 // For performance stats see http://crbug.com/719919.
98 placeholder = BitmapUtils.scale(placeholder, size, false);
99 mItemView.initialize(mBitmapDetails, placeholder, true);
100 } else {
101 mItemView.initialize(mBitmapDetails, null, true);
102 }
103
104 mCategoryView.getDecoderServiceHost().decodeImage(filePath, size, this);
74 } 105 }
75 106
76 /** 107 /**
77 * Returns the file path of the current request. 108 * Returns the file path of the current request.
78 */ 109 */
79 public String getFilePath() { 110 public String getFilePath() {
80 return mBitmapDetails == null ? null : mBitmapDetails.getFilePath(); 111 return mBitmapDetails == null ? null : mBitmapDetails.getFilePath();
81 } 112 }
82 } 113 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698