OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser.photo_picker; |
| 6 |
| 7 import android.graphics.Bitmap; |
| 8 import android.graphics.Canvas; |
| 9 import android.graphics.Color; |
| 10 import android.graphics.Paint; |
| 11 import android.support.v7.widget.RecyclerView.ViewHolder; |
| 12 import android.text.TextUtils; |
| 13 |
| 14 import java.util.List; |
| 15 |
| 16 /** |
| 17 * Holds on to a {@link PickerBitmapView} that displays information about a pick
er bitmap. |
| 18 */ |
| 19 public class PickerBitmapViewHolder extends ViewHolder { |
| 20 // Our parent category. |
| 21 private PickerCategoryView mCategoryView; |
| 22 |
| 23 // The bitmap view we are holding on to. |
| 24 private final PickerBitmapView mItemView; |
| 25 |
| 26 // The request we are showing the bitmap for. |
| 27 private PickerBitmap mBitmapDetails; |
| 28 |
| 29 /** |
| 30 * The PickerBitmapViewHolder. |
| 31 * @param itemView The {@link PickerBitmapView} view for showing the image. |
| 32 */ |
| 33 public PickerBitmapViewHolder(PickerBitmapView itemView) { |
| 34 super(itemView); |
| 35 mItemView = itemView; |
| 36 } |
| 37 |
| 38 /** |
| 39 * The notification handler for when an image has been decoded. |
| 40 * @param filePath The file path for the newly decoded image. |
| 41 * @param bitmap The results of the decoding (or placeholder image, if faile
d). |
| 42 */ |
| 43 public void imageDecodedCallback(String filePath, Bitmap bitmap) { |
| 44 if (bitmap == null || bitmap.getWidth() == 0 || bitmap.getHeight() == 0)
{ |
| 45 return; |
| 46 } |
| 47 |
| 48 if (!TextUtils.equals(mBitmapDetails.getFilePath(), filePath)) { |
| 49 return; |
| 50 } |
| 51 |
| 52 mItemView.setThumbnailBitmap(bitmap); |
| 53 } |
| 54 |
| 55 /** |
| 56 * Display a single item from |position| in the PickerCategoryView. |
| 57 * @param categoryView The PickerCategoryView to use to fetch the image. |
| 58 * @param position The position of the item to fetch. |
| 59 */ |
| 60 public void displayItem(PickerCategoryView categoryView, int position) { |
| 61 mCategoryView = categoryView; |
| 62 |
| 63 List<PickerBitmap> pickerBitmaps = mCategoryView.getPickerBitmaps(); |
| 64 mBitmapDetails = pickerBitmaps.get(position); |
| 65 |
| 66 if (mBitmapDetails.type() == PickerBitmap.CAMERA |
| 67 || mBitmapDetails.type() == PickerBitmap.GALLERY) { |
| 68 mItemView.initializeSpecialTile(mBitmapDetails); |
| 69 return; |
| 70 } |
| 71 |
| 72 // TODO(finnur): Use cached image, if available. |
| 73 |
| 74 // TODO(finnur): Use decoder instead. |
| 75 int size = mCategoryView.getImageSize(); |
| 76 imageDecodedCallback(mBitmapDetails.getFilePath(), createPlaceholderBitm
ap(size, size)); |
| 77 } |
| 78 |
| 79 /** |
| 80 * Creates a placeholder bitmap. |
| 81 * @param width The requested width of the resulting bitmap. |
| 82 * @param height The requested height of the resulting bitmap. |
| 83 * @return Placeholder bitmap. |
| 84 */ |
| 85 // TODO(finnur): Remove once the decoder is in place. |
| 86 private Bitmap createPlaceholderBitmap(int width, int height) { |
| 87 Bitmap placeholder = Bitmap.createBitmap(width, height, Bitmap.Config.AR
GB_8888); |
| 88 Canvas canvas = new Canvas(placeholder); |
| 89 Paint paint = new Paint(); |
| 90 paint.setColor(Color.GRAY); |
| 91 canvas.drawRect(0, 0, (float) width, (float) height, paint); |
| 92 return placeholder; |
| 93 } |
| 94 |
| 95 /** |
| 96 * Returns the file path of the current request. |
| 97 */ |
| 98 public String getFilePath() { |
| 99 return mBitmapDetails == null ? null : mBitmapDetails.getFilePath(); |
| 100 } |
| 101 } |
OLD | NEW |