Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/photo_picker/PickerBitmapView.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/photo_picker/PickerBitmapView.java b/chrome/android/java/src/org/chromium/chrome/browser/photo_picker/PickerBitmapView.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..1a205f1cb3c36ff4c2c06bb52af9ef427f090ba7 |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/photo_picker/PickerBitmapView.java |
| @@ -0,0 +1,239 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
|
Theresa
2017/04/04 15:48:33
s/2016/2017
Finnur
2017/04/04 18:05:37
Done.
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.chrome.browser.photo_picker; |
| + |
| +import android.content.Context; |
| +import android.content.res.Resources; |
| +import android.graphics.Bitmap; |
| +import android.graphics.BitmapFactory; |
| +import android.graphics.Color; |
| +import android.graphics.PorterDuff; |
| +import android.graphics.drawable.BitmapDrawable; |
| +import android.graphics.drawable.Drawable; |
| +import android.support.annotation.Nullable; |
| +import android.util.AttributeSet; |
| +import android.view.View; |
| +import android.widget.ImageView; |
| +import android.widget.TextView; |
| + |
| +import org.chromium.base.ApiCompatibilityUtils; |
| +import org.chromium.chrome.R; |
| +import org.chromium.chrome.browser.widget.selection.SelectableItemView; |
| +import org.chromium.chrome.browser.widget.selection.SelectionDelegate; |
| + |
| +import java.util.List; |
| + |
| +/** |
| + * A container class for a view showing a photo in the photo picker. |
|
Theresa
2017/04/04 15:48:33
nit: s/photo picker/Photo Picker for consistency i
Finnur
2017/04/04 18:05:37
Done.
|
| + */ |
| +public class PickerBitmapView extends SelectableItemView<PickerBitmap> { |
| + // Our context. |
| + private Context mContext; |
| + |
| + // Our parent category. |
| + private PickerCategoryView mCategoryView; |
| + |
| + // Our selection delegate. |
| + private SelectionDelegate<PickerBitmap> mSelectionDelegate; |
| + |
| + // The request details (meta-data) for the bitmap shown. |
| + private PickerBitmap mRequest; |
|
Theresa
2017/04/04 15:48:33
nit: mBitmap? The PickerBitmap object isn't actual
Finnur
2017/04/04 18:05:37
I didn't like calling it mBitmap/mImage or somethi
Theresa
2017/04/04 18:40:46
Acknowledged.
|
| + |
| + // The image view containing the bitmap. |
| + private ImageView mIconView; |
| + |
| + // The view behind the image, representing the selection border. |
| + private View mBorderView; |
| + |
| + // The camera/gallery special tile (with icon as drawable). |
| + private TextView mSpecialTile; |
| + |
| + // Whether the image has been loaded already. |
| + private boolean mImageLoaded; |
| + |
| + /** |
| + * Constructor for inflating from XML. |
| + */ |
| + public PickerBitmapView(Context context, AttributeSet attrs) { |
| + super(context, attrs); |
| + mContext = context; |
| + } |
| + |
| + @Override |
| + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| + super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| + |
| + if (mCategoryView == null) return; |
| + |
| + int width = mCategoryView.getImageSize(); |
| + int height = mCategoryView.getImageSize(); |
| + setMeasuredDimension(width, height); |
| + } |
| + |
| + @Override |
| + protected void onFinishInflate() { |
| + super.onFinishInflate(); |
| + mIconView = (ImageView) findViewById(R.id.bitmap_view); |
| + mBorderView = findViewById(R.id.border); |
| + mSpecialTile = (TextView) findViewById(R.id.special_tile); |
| + } |
| + |
| + @Override |
| + public void onClick() { |
| + // TODO(finnur): Remove the null checks here and below. |
| + if (mRequest != null && mRequest.type() == PickerBitmap.TileTypes.GALLERY) { |
| + mCategoryView.showGallery(); |
| + return; |
| + } else if (mRequest != null && mRequest.type() == PickerBitmap.TileTypes.CAMERA) { |
| + mCategoryView.showCamera(); |
| + return; |
| + } |
| + |
| + // The SelectableItemView expects long press to be the selection event, but this class wants |
| + // that to happen on click instead. |
| + super.onLongClick(this); |
| + } |
| + |
| + @Override |
| + protected boolean toggleSelectionForItem(PickerBitmap item) { |
| + if (mRequest != null |
| + && (mRequest.type() == PickerBitmap.TileTypes.GALLERY |
| + || mRequest.type() == PickerBitmap.TileTypes.CAMERA)) { |
|
Theresa
2017/04/04 15:48:33
nit: && mRequest.type() != PickerBitmap.TileTypes.
Finnur
2017/04/04 18:05:37
Did it slightly differently...
|
| + return false; |
| + } |
| + return super.toggleSelectionForItem(item); |
| + } |
| + |
| + @Override |
| + public void setChecked(boolean checked) { |
| + if (mRequest != null && mRequest.type() != PickerBitmap.TileTypes.PICTURE) { |
| + return; |
| + } |
| + |
| + super.setChecked(checked); |
| + updateSelectionState(); |
| + } |
| + |
| + @Override |
| + public void onSelectionStateChange(List<PickerBitmap> selectedItems) { |
| + boolean selected = selectedItems.contains(mRequest); |
|
Theresa
2017/04/04 15:48:33
It appears this variable isn't used anymore.
Finnur
2017/04/04 18:05:37
Done.
|
| + |
| + updateSelectionState(); |
| + } |
| + |
| + /** |
| + * Pre-initializes the PickerBitmapView. |
| + * @param categoryView The category view showing the images. |
| + */ |
| + public void preInitialize(PickerCategoryView categoryView) { |
|
Theresa
2017/04/04 15:48:33
nit: Maybe we can just call this setCategoryView(
Finnur
2017/04/04 18:05:37
Also very reasonable.
|
| + mCategoryView = categoryView; |
| + mSelectionDelegate = mCategoryView.getSelectionDelegate(); |
| + super.setSelectionDelegate(mSelectionDelegate); |
| + } |
| + |
| + /** |
| + * Completes the initialization of the PickerBitmapView. Must be called before the image can |
| + * respond to click events. |
| + * @param request The request represented by this PickerBitmapView. |
| + * @param thumbnail The Bitmap to use for the thumbnail (or null). |
| + * @param placeholder Whether the image given is a placeholder or the actual image. |
| + */ |
| + public void initialize(PickerBitmap request, @Nullable Bitmap thumbnail, boolean placeholder) { |
| + resetTile(); |
| + |
| + mRequest = request; |
| + setItem(request); |
| + setThumbnailBitmap(thumbnail); |
| + mImageLoaded = !placeholder; |
| + |
| + updateSelectionState(); |
| + } |
| + |
| + /** |
| + * Initialization for the special tiles (camera/gallery icon). |
| + * @param request The request represented by this PickerBitmapView. |
| + */ |
| + public void initializeSpecialTile(PickerBitmap request) { |
| + int size = mCategoryView.getImageSize(); |
| + Bitmap tile = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); |
| + tile.eraseColor(Color.argb(0, 0, 0, 0)); |
| + |
| + int iconBitmapId, labelStringId; |
| + if (request != null && request.type() == PickerBitmap.TileTypes.CAMERA) { |
| + iconBitmapId = R.drawable.ic_photo_camera; |
| + labelStringId = R.string.file_picker_camera; |
| + } else { |
| + iconBitmapId = R.drawable.ic_collections_black_24dp; |
| + labelStringId = R.string.file_picker_browse; |
| + } |
| + |
| + Resources resources = mContext.getResources(); |
| + mSpecialTile.setText(labelStringId); |
| + Bitmap icon = BitmapFactory.decodeResource(resources, iconBitmapId); |
| + float pixels = resources.getDimensionPixelOffset(R.dimen.file_picker_special_icon_size); |
| + BitmapDrawable drawable = new BitmapDrawable( |
| + resources, Bitmap.createScaledBitmap(icon, (int) pixels, (int) pixels, true)); |
| + ApiCompatibilityUtils.setCompoundDrawablesRelativeWithIntrinsicBounds( |
| + mSpecialTile, null, drawable, null, null); |
| + |
| + initialize(request, tile, false); |
| + |
| + mSpecialTile.setVisibility(View.VISIBLE); |
| + } |
| + |
| + /** |
| + * Sets a thumbnail bitmap for the current view. |
| + * @param thumbnail The Bitmap to use for the icon ImageView. |
| + * @return True if no image was loaded before (e.g. not even a low-res image). |
| + */ |
| + public boolean setThumbnailBitmap(Bitmap thumbnail) { |
| + mIconView.setImageBitmap(thumbnail); |
| + |
| + boolean noImageWasLoaded = !mImageLoaded; |
| + mImageLoaded = true; |
| + updateSelectionState(); |
| + |
| + return noImageWasLoaded; |
| + } |
| + |
| + /** |
| + * Resets the view to its starting state, which is necessary when the view is about to be |
| + * re-used. |
| + */ |
| + private void resetTile() { |
| + mSpecialTile.setVisibility(View.GONE); |
| + } |
| + |
| + /** |
| + * Updates the selection controls for this view. |
| + */ |
| + private void updateSelectionState() { |
| + boolean special = mRequest != null && mRequest.type() != PickerBitmap.TileTypes.PICTURE; |
| + boolean checked = super.isChecked(); |
| + boolean anySelection = |
| + mSelectionDelegate != null && mSelectionDelegate.isSelectionEnabled(); |
| + int bgColorId, fgColorId; |
| + if (!special) { |
| + bgColorId = R.color.file_picker_tile_bg_color; |
| + fgColorId = R.color.file_picker_special_tile_color; |
| + } else if (!anySelection) { |
| + bgColorId = R.color.file_picker_special_tile_bg_color; |
| + fgColorId = R.color.file_picker_special_tile_color; |
| + } else { |
| + bgColorId = R.color.file_picker_special_tile_disabled_bg_color; |
| + fgColorId = R.color.file_picker_special_tile_disabled_color; |
| + } |
| + |
| + Resources resources = mContext.getResources(); |
| + mBorderView.setBackgroundColor(ApiCompatibilityUtils.getColor(resources, bgColorId)); |
| + mSpecialTile.setTextColor(ApiCompatibilityUtils.getColor(resources, fgColorId)); |
| + Drawable[] drawables = mSpecialTile.getCompoundDrawables(); |
| + // The textview only has a top compound drawable (2nd element). |
| + if (drawables[1] != null) { |
| + int color = ApiCompatibilityUtils.getColor(resources, fgColorId); |
| + drawables[1].setColorFilter(color, PorterDuff.Mode.SRC_IN); |
| + } |
| + } |
| +} |