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..e50520fb0cf7edf25611c77ff0c4bdb53c9f5862 |
--- /dev/null |
+++ b/chrome/android/java/src/org/chromium/chrome/browser/photo_picker/PickerBitmapView.java |
@@ -0,0 +1,241 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// 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.PorterDuff; |
+import android.graphics.drawable.Drawable; |
+import android.support.annotation.Nullable; |
+import android.support.graphics.drawable.VectorDrawableCompat; |
+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. |
+ */ |
+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 mBitmapDetails; |
+ |
+ // 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() { |
+ if (isGalleryTile()) { |
+ mCategoryView.showGallery(); |
+ return; |
+ } else if (isCameraTile()) { |
+ 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 (isGalleryTile() || isCameraTile()) return false; |
+ return super.toggleSelectionForItem(item); |
+ } |
+ |
+ @Override |
+ public void setChecked(boolean checked) { |
+ if (!isPictureTile()) { |
+ return; |
+ } |
+ |
+ super.setChecked(checked); |
+ updateSelectionState(); |
+ } |
+ |
+ @Override |
+ public void onSelectionStateChange(List<PickerBitmap> selectedItems) { |
+ updateSelectionState(); |
+ } |
+ |
+ /** |
+ * Sets the {@link PickerCategoryView} for this PickerBitmapView. |
+ * @param categoryView The category view showing the images. Used to access |
+ * common functionality and sizes and retrieve the {@link SelectionDelegate}. |
+ */ |
+ public void setCategoryView(PickerCategoryView categoryView) { |
+ 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 bitmapDetails The details about the bitmap 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 bitmapDetails, @Nullable Bitmap thumbnail, boolean placeholder) { |
+ resetTile(); |
+ |
+ mBitmapDetails = bitmapDetails; |
+ setItem(bitmapDetails); |
+ setThumbnailBitmap(thumbnail); |
+ mImageLoaded = !placeholder; |
+ |
+ updateSelectionState(); |
+ } |
+ |
+ /** |
+ * Initialization for the special tiles (camera/gallery icon). |
+ * @param bitmapDetails The details about the bitmap represented by this PickerBitmapView. |
+ */ |
+ public void initializeSpecialTile(PickerBitmap bitmapDetails) { |
+ int labelStringId; |
+ Drawable image; |
+ Resources resources = mContext.getResources(); |
+ |
+ if (isCameraTile()) { |
+ image = ApiCompatibilityUtils.getDrawable(resources, R.drawable.ic_photo_camera); |
+ labelStringId = R.string.photo_picker_camera; |
+ } else { |
+ image = VectorDrawableCompat.create( |
+ resources, R.drawable.ic_collections_grey, mContext.getTheme()); |
+ labelStringId = R.string.photo_picker_browse; |
+ } |
+ |
+ ApiCompatibilityUtils.setCompoundDrawablesRelativeWithIntrinsicBounds( |
+ mSpecialTile, null, image, null, null); |
+ mSpecialTile.setText(labelStringId); |
+ |
+ initialize(bitmapDetails, null, false); |
+ |
+ // Reset visibility, since #initialize() sets mSpecialTile visibility to GONE. |
+ 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 = !isPictureTile(); |
+ boolean checked = super.isChecked(); |
Theresa
2017/04/06 15:15:29
nit: findbugs reported a deadstore for this variab
Finnur
2017/04/06 15:46:47
Done.
|
+ boolean anySelection = |
+ mSelectionDelegate != null && mSelectionDelegate.isSelectionEnabled(); |
+ int bgColorId, fgColorId; |
+ if (!special) { |
+ bgColorId = R.color.photo_picker_tile_bg_color; |
+ fgColorId = R.color.photo_picker_special_tile_color; |
+ } else if (!anySelection) { |
+ bgColorId = R.color.photo_picker_special_tile_bg_color; |
+ fgColorId = R.color.photo_picker_special_tile_color; |
+ } else { |
+ bgColorId = R.color.photo_picker_special_tile_disabled_bg_color; |
+ fgColorId = R.color.photo_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); |
+ } |
+ } |
+ |
+ private boolean isGalleryTile() { |
+ // TODO(finnur): Remove the null checks here and below. |
+ return mBitmapDetails != null && mBitmapDetails.type() == PickerBitmap.TileTypes.GALLERY; |
+ } |
+ |
+ private boolean isCameraTile() { |
+ return mBitmapDetails != null && mBitmapDetails.type() == PickerBitmap.TileTypes.CAMERA; |
+ } |
+ |
+ private boolean isPictureTile() { |
+ return mBitmapDetails != null && mBitmapDetails.type() == PickerBitmap.TileTypes.PICTURE; |
+ } |
+} |