Chromium Code Reviews| OLD | NEW |
|---|---|
| 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.support.v7.widget.RecyclerView.ViewHolder; | 8 import android.support.v7.widget.RecyclerView.ViewHolder; |
| 9 import android.text.TextUtils; | 9 import android.text.TextUtils; |
| 10 | 10 |
| 11 import java.util.List; | 11 import java.util.List; |
| 12 | 12 |
| 13 /** | 13 /** |
| 14 * Holds on to a {@link PickerBitmapView} that displays information about a pick er bitmap. | 14 * Holds on to a {@link PickerBitmapView} that displays information about a pick er bitmap. |
| 15 */ | 15 */ |
| 16 public class PickerBitmapViewHolder | 16 public class PickerBitmapViewHolder |
| 17 extends ViewHolder implements DecoderServiceHost.ImageDecodedCallback { | 17 extends ViewHolder implements DecoderServiceHost.ImageDecodedCallback { |
| 18 // The size (in pixels) of the low-res thumbnails. | |
| 19 private static final int GRAINY_THUMBNAIL_SIZE_PX = 40; | |
|
Theresa
2017/05/04 16:53:13
40px on an xxxhdpi is going to be a very different
Finnur
2017/05/05 20:41:45
Good idea. Done.
| |
| 20 | |
| 18 // Our parent category. | 21 // Our parent category. |
| 19 private PickerCategoryView mCategoryView; | 22 private PickerCategoryView mCategoryView; |
| 20 | 23 |
| 21 // The bitmap view we are holding on to. | 24 // The bitmap view we are holding on to. |
| 22 private final PickerBitmapView mItemView; | 25 private final PickerBitmapView mItemView; |
| 23 | 26 |
| 24 // The request we are showing the bitmap for. | 27 // The request we are showing the bitmap for. |
| 25 private PickerBitmap mBitmapDetails; | 28 private PickerBitmap mBitmapDetails; |
| 26 | 29 |
| 27 /** | 30 /** |
| 28 * The PickerBitmapViewHolder. | 31 * The PickerBitmapViewHolder. |
| 29 * @param itemView The {@link PickerBitmapView} view for showing the image. | 32 * @param itemView The {@link PickerBitmapView} view for showing the image. |
| 30 */ | 33 */ |
| 31 public PickerBitmapViewHolder(PickerBitmapView itemView) { | 34 public PickerBitmapViewHolder(PickerBitmapView itemView) { |
| 32 super(itemView); | 35 super(itemView); |
| 33 mItemView = itemView; | 36 mItemView = itemView; |
| 34 } | 37 } |
| 35 | 38 |
| 36 // DecoderServiceHost.ImageDecodedCallback | 39 // DecoderServiceHost.ImageDecodedCallback |
| 37 | 40 |
| 38 @Override | 41 @Override |
| 39 public void imageDecodedCallback(String filePath, Bitmap bitmap) { | 42 public void imageDecodedCallback(String filePath, Bitmap bitmap) { |
| 40 if (bitmap == null || bitmap.getWidth() == 0 || bitmap.getHeight() == 0) { | 43 if (bitmap == null || bitmap.getWidth() == 0 || bitmap.getHeight() == 0) { |
| 41 return; | 44 return; |
| 42 } | 45 } |
| 43 | 46 |
| 47 if (mCategoryView.getHighResBitmaps().get(filePath) == null) { | |
| 48 mCategoryView.getHighResBitmaps().put(filePath, bitmap); | |
| 49 } | |
| 50 | |
| 51 if (mCategoryView.getLowResBitmaps().get(filePath) == null) { | |
| 52 // Scaling the image down takes between 0-1 ms on average (Nexus 6 p hone debug build). | |
|
Theresa
2017/05/04 16:53:13
The Nexus 6 is a relatively good device. Did we ta
Finnur
2017/05/05 20:41:45
0-2 ms on my Evercross Svelte device.
| |
| 53 Bitmap lowres = BitmapUtils.scale(bitmap, GRAINY_THUMBNAIL_SIZE_PX, false); | |
|
Theresa
2017/05/04 16:53:13
Can this be done on an async background thread rat
Finnur
2017/05/05 20:41:45
Done.
| |
| 54 mCategoryView.getLowResBitmaps().put(filePath, lowres); | |
| 55 } | |
| 56 | |
| 44 if (!TextUtils.equals(mBitmapDetails.getFilePath(), filePath)) { | 57 if (!TextUtils.equals(mBitmapDetails.getFilePath(), filePath)) { |
| 45 return; | 58 return; |
| 46 } | 59 } |
| 47 | 60 |
| 48 mItemView.setThumbnailBitmap(bitmap); | 61 mItemView.setThumbnailBitmap(bitmap); |
| 49 } | 62 } |
| 50 | 63 |
| 51 /** | 64 /** |
| 52 * Display a single item from |position| in the PickerCategoryView. | 65 * Display a single item from |position| in the PickerCategoryView. |
| 53 * @param categoryView The PickerCategoryView to use to fetch the image. | 66 * @param categoryView The PickerCategoryView to use to fetch the image. |
| 54 * @param position The position of the item to fetch. | 67 * @param position The position of the item to fetch. |
| 55 */ | 68 */ |
| 56 public void displayItem(PickerCategoryView categoryView, int position) { | 69 public void displayItem(PickerCategoryView categoryView, int position) { |
| 57 mCategoryView = categoryView; | 70 mCategoryView = categoryView; |
| 58 | 71 |
| 59 List<PickerBitmap> pickerBitmaps = mCategoryView.getPickerBitmaps(); | 72 List<PickerBitmap> pickerBitmaps = mCategoryView.getPickerBitmaps(); |
| 60 mBitmapDetails = pickerBitmaps.get(position); | 73 mBitmapDetails = pickerBitmaps.get(position); |
| 61 | 74 |
| 62 if (mBitmapDetails.type() == PickerBitmap.CAMERA | 75 if (mBitmapDetails.type() == PickerBitmap.CAMERA |
| 63 || mBitmapDetails.type() == PickerBitmap.GALLERY) { | 76 || mBitmapDetails.type() == PickerBitmap.GALLERY) { |
| 64 mItemView.initialize(mBitmapDetails, null, false); | 77 mItemView.initialize(mBitmapDetails, null, false); |
| 65 return; | 78 return; |
| 66 } | 79 } |
| 67 | 80 |
| 68 // TODO(finnur): Use cached image, if available. | 81 String filePath = mBitmapDetails.getFilePath(); |
| 69 | 82 Bitmap original = mCategoryView.getHighResBitmaps().get(filePath); |
| 70 mItemView.initialize(mBitmapDetails, null, true); | 83 if (original != null) { |
| 84 mItemView.initialize(mBitmapDetails, original, false); | |
| 85 return; | |
| 86 } | |
| 71 | 87 |
| 72 int size = mCategoryView.getImageSize(); | 88 int size = mCategoryView.getImageSize(); |
| 89 Bitmap placeholder = mCategoryView.getLowResBitmaps().get(filePath); | |
| 90 if (placeholder != null) { | |
| 91 // Scaling the image up takes between 3-4 ms on average (Nexus 6 pho ne debug build). | |
| 92 placeholder = BitmapUtils.scale(placeholder, size, false); | |
| 93 mItemView.initialize(mBitmapDetails, placeholder, true); | |
| 94 } else { | |
| 95 mItemView.initialize(mBitmapDetails, null, true); | |
| 96 } | |
| 97 | |
| 73 mCategoryView.getDecoderServiceHost().decodeImage(mBitmapDetails.getFile Path(), size, this); | 98 mCategoryView.getDecoderServiceHost().decodeImage(mBitmapDetails.getFile Path(), size, this); |
| 74 } | 99 } |
| 75 | 100 |
| 76 /** | 101 /** |
| 77 * Returns the file path of the current request. | 102 * Returns the file path of the current request. |
| 78 */ | 103 */ |
| 79 public String getFilePath() { | 104 public String getFilePath() { |
| 80 return mBitmapDetails == null ? null : mBitmapDetails.getFilePath(); | 105 return mBitmapDetails == null ? null : mBitmapDetails.getFilePath(); |
| 81 } | 106 } |
| 82 } | 107 } |
| OLD | NEW |