Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2619)

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/photo_picker/PickerBitmapView.java

Issue 2758313002: Implement the new Photo picker, part two. (Closed)
Patch Set: Address Ted's comments (and sync to latest) Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..088b6331c7d2b5653b6c2ab6f95feb3f2f0da197
--- /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 onFinishInflate() {
+ super.onFinishInflate();
+ mIconView = (ImageView) findViewById(R.id.bitmap_view);
+ mBorderView = findViewById(R.id.border);
+ mSpecialTile = (TextView) findViewById(R.id.special_tile);
+ }
+
+ @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
+ 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.
+ 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();
+ 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 anySelection =
+ mSelectionDelegate != null && mSelectionDelegate.isSelectionEnabled();
+ int bgColorId;
+ int 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.GALLERY;
+ }
+
+ private boolean isCameraTile() {
+ return mBitmapDetails != null && mBitmapDetails.type() == PickerBitmap.CAMERA;
+ }
+
+ private boolean isPictureTile() {
+ return mBitmapDetails != null && mBitmapDetails.type() == PickerBitmap.PICTURE;
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698