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..cfb02a76e163b0836857c7bd9adff5bc9319f94b |
--- /dev/null |
+++ b/chrome/android/java/src/org/chromium/chrome/browser/photo_picker/PickerBitmapView.java |
@@ -0,0 +1,252 @@ |
+// 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.Canvas; |
+import android.graphics.Color; |
+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() { |
+ // TODO(finnur): Remove the null checks here and below. |
+ 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 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(); |
+ |
+ mBitmapDetails = request; |
Theresa
2017/04/04 18:40:46
nit: should the @param be renamed to match mBitmap
Finnur
2017/04/05 15:14:39
Done.
|
+ 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) { |
+ Bitmap icon; |
+ int labelStringId; |
+ Resources resources = mContext.getResources(); |
+ |
+ if (isCameraTile()) { |
+ int iconBitmapId = R.drawable.ic_photo_camera; |
+ Drawable image = ApiCompatibilityUtils.getDrawable(resources, iconBitmapId); |
+ ApiCompatibilityUtils.setCompoundDrawablesRelativeWithIntrinsicBounds( |
+ mSpecialTile, null, image, null, null); |
+ labelStringId = R.string.file_picker_camera; |
+ } else { |
+ Drawable collections = VectorDrawableCompat.create( |
+ resources, R.drawable.ic_collections_grey, mContext.getTheme()); |
+ icon = Bitmap.createBitmap(collections.getIntrinsicWidth(), |
+ collections.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); |
+ Canvas canvas = new Canvas(icon); |
+ collections.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); |
+ collections.draw(canvas); |
Theresa
2017/04/04 18:40:46
Can collections be passed directly to ApiCompatibi
Finnur
2017/04/05 15:14:39
Done. Except the last part (constructs for null bi
Theresa
2017/04/05 15:48:23
If Android is happy with a null passed in to #setI
|
+ ApiCompatibilityUtils.setCompoundDrawablesRelativeWithIntrinsicBounds( |
+ mSpecialTile, null, collections, null, null); |
+ labelStringId = R.string.file_picker_browse; |
+ } |
+ |
+ mSpecialTile.setText(labelStringId); |
+ |
+ int size = mCategoryView.getImageSize(); |
+ Bitmap tile = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); |
+ tile.eraseColor(Color.argb(0, 0, 0, 0)); |
+ 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 = !isPictureTile(); |
+ 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); |
+ } |
+ } |
+ |
+ private boolean isGalleryTile() { |
+ 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; |
+ } |
+} |