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

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: Polish 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.graphics.Bitmap; 7 import android.graphics.Bitmap;
8 import android.os.AsyncTask;
8 import android.support.v7.widget.RecyclerView.ViewHolder; 9 import android.support.v7.widget.RecyclerView.ViewHolder;
9 import android.text.TextUtils; 10 import android.text.TextUtils;
10 11
11 import java.util.List; 12 import java.util.List;
12 13
13 /** 14 /**
14 * Holds on to a {@link PickerBitmapView} that displays information about a pick er bitmap. 15 * Holds on to a {@link PickerBitmapView} that displays information about a pick er bitmap.
15 */ 16 */
16 public class PickerBitmapViewHolder 17 public class PickerBitmapViewHolder
17 extends ViewHolder implements DecoderServiceHost.ImageDecodedCallback { 18 extends ViewHolder implements DecoderServiceHost.ImageDecodedCallback {
19 // The size (in dp) of the low-res thumbnails.
20 private static final int GRAINY_THUMBNAIL_SIZE_DP = 12;
Theresa 2017/05/08 19:34:42 Nit: Put this in dimens.xml and use Resources.getD
Finnur 2017/05/09 11:53:56 Done.
21
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;
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 new BitmapScalerTask(mCategoryView.getLowResBitmaps(), filePath,
58 (int) (GRAINY_THUMBNAIL_SIZE_DP * mDensity))
59 .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, bitmap);
60 }
61
44 if (!TextUtils.equals(mBitmapDetails.getFilePath(), filePath)) { 62 if (!TextUtils.equals(mBitmapDetails.getFilePath(), filePath)) {
45 return; 63 return;
46 } 64 }
47 65
48 mItemView.setThumbnailBitmap(bitmap); 66 mItemView.setThumbnailBitmap(bitmap);
49 } 67 }
50 68
51 /** 69 /**
52 * Display a single item from |position| in the PickerCategoryView. 70 * Display a single item from |position| in the PickerCategoryView.
53 * @param categoryView The PickerCategoryView to use to fetch the image. 71 * @param categoryView The PickerCategoryView to use to fetch the image.
54 * @param position The position of the item to fetch. 72 * @param position The position of the item to fetch.
55 */ 73 */
56 public void displayItem(PickerCategoryView categoryView, int position) { 74 public void displayItem(PickerCategoryView categoryView, int position) {
57 mCategoryView = categoryView; 75 mCategoryView = categoryView;
58 76
59 List<PickerBitmap> pickerBitmaps = mCategoryView.getPickerBitmaps(); 77 List<PickerBitmap> pickerBitmaps = mCategoryView.getPickerBitmaps();
60 mBitmapDetails = pickerBitmaps.get(position); 78 mBitmapDetails = pickerBitmaps.get(position);
61 79
62 if (mBitmapDetails.type() == PickerBitmap.CAMERA 80 if (mBitmapDetails.type() == PickerBitmap.CAMERA
63 || mBitmapDetails.type() == PickerBitmap.GALLERY) { 81 || mBitmapDetails.type() == PickerBitmap.GALLERY) {
64 mItemView.initialize(mBitmapDetails, null, false); 82 mItemView.initialize(mBitmapDetails, null, false);
65 return; 83 return;
66 } 84 }
67 85
68 // TODO(finnur): Use cached image, if available. 86 String filePath = mBitmapDetails.getFilePath();
69 87 Bitmap original = mCategoryView.getHighResBitmaps().get(filePath);
70 mItemView.initialize(mBitmapDetails, null, true); 88 if (original != null) {
89 mItemView.initialize(mBitmapDetails, original, false);
90 return;
91 }
71 92
72 int size = mCategoryView.getImageSize(); 93 int size = mCategoryView.getImageSize();
73 mCategoryView.getDecoderServiceHost().decodeImage(mBitmapDetails.getFile Path(), size, this); 94 Bitmap placeholder = mCategoryView.getLowResBitmaps().get(filePath);
95 if (placeholder != null) {
96 // Scaling the image up takes between 3-4 ms on average (Nexus 6 pho ne debug build).
Theresa 2017/05/08 19:34:42 nit: This type of information is likely to get out
Finnur 2017/05/09 11:53:56 Done.
97 placeholder = BitmapUtils.scale(placeholder, size, false);
98 mItemView.initialize(mBitmapDetails, placeholder, true);
99 } else {
100 mItemView.initialize(mBitmapDetails, null, true);
101 }
102
103 mCategoryView.getDecoderServiceHost().decodeImage(filePath, size, this);
74 } 104 }
75 105
76 /** 106 /**
77 * Returns the file path of the current request. 107 * Returns the file path of the current request.
78 */ 108 */
79 public String getFilePath() { 109 public String getFilePath() {
80 return mBitmapDetails == null ? null : mBitmapDetails.getFilePath(); 110 return mBitmapDetails == null ? null : mBitmapDetails.getFilePath();
81 } 111 }
82 } 112 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698