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.content.Context; |
| 8 import android.content.res.Resources; |
| 9 import android.graphics.Bitmap; |
| 10 import android.graphics.PorterDuff; |
| 11 import android.graphics.drawable.Drawable; |
| 12 import android.support.annotation.Nullable; |
| 13 import android.support.graphics.drawable.VectorDrawableCompat; |
| 14 import android.util.AttributeSet; |
| 15 import android.view.View; |
| 16 import android.widget.ImageView; |
| 17 import android.widget.TextView; |
| 18 |
| 19 import org.chromium.base.ApiCompatibilityUtils; |
| 20 import org.chromium.chrome.R; |
| 21 import org.chromium.chrome.browser.widget.selection.SelectableItemView; |
| 22 import org.chromium.chrome.browser.widget.selection.SelectionDelegate; |
| 23 |
| 24 import java.util.List; |
| 25 |
| 26 /** |
| 27 * A container class for a view showing a photo in the Photo Picker. |
| 28 */ |
| 29 public class PickerBitmapView extends SelectableItemView<PickerBitmap> { |
| 30 // Our context. |
| 31 private Context mContext; |
| 32 |
| 33 // Our parent category. |
| 34 private PickerCategoryView mCategoryView; |
| 35 |
| 36 // Our selection delegate. |
| 37 private SelectionDelegate<PickerBitmap> mSelectionDelegate; |
| 38 |
| 39 // The request details (meta-data) for the bitmap shown. |
| 40 private PickerBitmap mBitmapDetails; |
| 41 |
| 42 // The image view containing the bitmap. |
| 43 private ImageView mIconView; |
| 44 |
| 45 // The view behind the image, representing the selection border. |
| 46 private View mBorderView; |
| 47 |
| 48 // The camera/gallery special tile (with icon as drawable). |
| 49 private TextView mSpecialTile; |
| 50 |
| 51 // Whether the image has been loaded already. |
| 52 private boolean mImageLoaded; |
| 53 |
| 54 /** |
| 55 * Constructor for inflating from XML. |
| 56 */ |
| 57 public PickerBitmapView(Context context, AttributeSet attrs) { |
| 58 super(context, attrs); |
| 59 mContext = context; |
| 60 } |
| 61 |
| 62 @Override |
| 63 protected void onFinishInflate() { |
| 64 super.onFinishInflate(); |
| 65 mIconView = (ImageView) findViewById(R.id.bitmap_view); |
| 66 mBorderView = findViewById(R.id.border); |
| 67 mSpecialTile = (TextView) findViewById(R.id.special_tile); |
| 68 } |
| 69 |
| 70 @Override |
| 71 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 72 super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 73 |
| 74 if (mCategoryView == null) return; |
| 75 |
| 76 int width = mCategoryView.getImageSize(); |
| 77 int height = mCategoryView.getImageSize(); |
| 78 setMeasuredDimension(width, height); |
| 79 } |
| 80 |
| 81 @Override |
| 82 public void onClick() { |
| 83 if (isGalleryTile()) { |
| 84 mCategoryView.showGallery(); |
| 85 return; |
| 86 } else if (isCameraTile()) { |
| 87 mCategoryView.showCamera(); |
| 88 return; |
| 89 } |
| 90 |
| 91 // The SelectableItemView expects long press to be the selection event,
but this class wants |
| 92 // that to happen on click instead. |
| 93 onLongClick(this); |
| 94 } |
| 95 |
| 96 @Override |
| 97 protected boolean toggleSelectionForItem(PickerBitmap item) { |
| 98 if (isGalleryTile() || isCameraTile()) return false; |
| 99 return super.toggleSelectionForItem(item); |
| 100 } |
| 101 |
| 102 @Override |
| 103 public void setChecked(boolean checked) { |
| 104 if (!isPictureTile()) { |
| 105 return; |
| 106 } |
| 107 |
| 108 super.setChecked(checked); |
| 109 updateSelectionState(); |
| 110 } |
| 111 |
| 112 @Override |
| 113 public void onSelectionStateChange(List<PickerBitmap> selectedItems) { |
| 114 updateSelectionState(); |
| 115 } |
| 116 |
| 117 /** |
| 118 * Sets the {@link PickerCategoryView} for this PickerBitmapView. |
| 119 * @param categoryView The category view showing the images. Used to access |
| 120 * common functionality and sizes and retrieve the {@link SelectionDeleg
ate}. |
| 121 */ |
| 122 public void setCategoryView(PickerCategoryView categoryView) { |
| 123 mCategoryView = categoryView; |
| 124 mSelectionDelegate = mCategoryView.getSelectionDelegate(); |
| 125 setSelectionDelegate(mSelectionDelegate); |
| 126 } |
| 127 |
| 128 /** |
| 129 * Completes the initialization of the PickerBitmapView. Must be called befo
re the image can |
| 130 * respond to click events. |
| 131 * @param bitmapDetails The details about the bitmap represented by this Pic
kerBitmapView. |
| 132 * @param thumbnail The Bitmap to use for the thumbnail (or null). |
| 133 * @param placeholder Whether the image given is a placeholder or the actual
image. |
| 134 */ |
| 135 public void initialize( |
| 136 PickerBitmap bitmapDetails, @Nullable Bitmap thumbnail, boolean plac
eholder) { |
| 137 resetTile(); |
| 138 |
| 139 mBitmapDetails = bitmapDetails; |
| 140 setItem(bitmapDetails); |
| 141 setThumbnailBitmap(thumbnail); |
| 142 mImageLoaded = !placeholder; |
| 143 |
| 144 updateSelectionState(); |
| 145 } |
| 146 |
| 147 /** |
| 148 * Initialization for the special tiles (camera/gallery icon). |
| 149 * @param bitmapDetails The details about the bitmap represented by this Pic
kerBitmapView. |
| 150 */ |
| 151 public void initializeSpecialTile(PickerBitmap bitmapDetails) { |
| 152 int labelStringId; |
| 153 Drawable image; |
| 154 Resources resources = mContext.getResources(); |
| 155 |
| 156 if (isCameraTile()) { |
| 157 image = ApiCompatibilityUtils.getDrawable(resources, R.drawable.ic_p
hoto_camera); |
| 158 labelStringId = R.string.photo_picker_camera; |
| 159 } else { |
| 160 image = VectorDrawableCompat.create( |
| 161 resources, R.drawable.ic_collections_grey, mContext.getTheme
()); |
| 162 labelStringId = R.string.photo_picker_browse; |
| 163 } |
| 164 |
| 165 ApiCompatibilityUtils.setCompoundDrawablesRelativeWithIntrinsicBounds( |
| 166 mSpecialTile, null, image, null, null); |
| 167 mSpecialTile.setText(labelStringId); |
| 168 |
| 169 initialize(bitmapDetails, null, false); |
| 170 |
| 171 // Reset visibility, since #initialize() sets mSpecialTile visibility to
GONE. |
| 172 mSpecialTile.setVisibility(View.VISIBLE); |
| 173 } |
| 174 |
| 175 /** |
| 176 * Sets a thumbnail bitmap for the current view. |
| 177 * @param thumbnail The Bitmap to use for the icon ImageView. |
| 178 * @return True if no image was loaded before (e.g. not even a low-res image
). |
| 179 */ |
| 180 public boolean setThumbnailBitmap(Bitmap thumbnail) { |
| 181 mIconView.setImageBitmap(thumbnail); |
| 182 |
| 183 boolean noImageWasLoaded = !mImageLoaded; |
| 184 mImageLoaded = true; |
| 185 updateSelectionState(); |
| 186 |
| 187 return noImageWasLoaded; |
| 188 } |
| 189 |
| 190 /** |
| 191 * Resets the view to its starting state, which is necessary when the view i
s about to be |
| 192 * re-used. |
| 193 */ |
| 194 private void resetTile() { |
| 195 mSpecialTile.setVisibility(View.GONE); |
| 196 } |
| 197 |
| 198 /** |
| 199 * Updates the selection controls for this view. |
| 200 */ |
| 201 private void updateSelectionState() { |
| 202 boolean special = !isPictureTile(); |
| 203 boolean anySelection = |
| 204 mSelectionDelegate != null && mSelectionDelegate.isSelectionEnab
led(); |
| 205 int bgColorId; |
| 206 int fgColorId; |
| 207 if (!special) { |
| 208 bgColorId = R.color.photo_picker_tile_bg_color; |
| 209 fgColorId = R.color.photo_picker_special_tile_color; |
| 210 } else if (!anySelection) { |
| 211 bgColorId = R.color.photo_picker_special_tile_bg_color; |
| 212 fgColorId = R.color.photo_picker_special_tile_color; |
| 213 } else { |
| 214 bgColorId = R.color.photo_picker_special_tile_disabled_bg_color; |
| 215 fgColorId = R.color.photo_picker_special_tile_disabled_color; |
| 216 } |
| 217 |
| 218 Resources resources = mContext.getResources(); |
| 219 mBorderView.setBackgroundColor(ApiCompatibilityUtils.getColor(resources,
bgColorId)); |
| 220 mSpecialTile.setTextColor(ApiCompatibilityUtils.getColor(resources, fgCo
lorId)); |
| 221 Drawable[] drawables = mSpecialTile.getCompoundDrawables(); |
| 222 // The textview only has a top compound drawable (2nd element). |
| 223 if (drawables[1] != null) { |
| 224 int color = ApiCompatibilityUtils.getColor(resources, fgColorId); |
| 225 drawables[1].setColorFilter(color, PorterDuff.Mode.SRC_IN); |
| 226 } |
| 227 } |
| 228 |
| 229 private boolean isGalleryTile() { |
| 230 // TODO(finnur): Remove the null checks here and below. |
| 231 return mBitmapDetails != null && mBitmapDetails.type() == PickerBitmap.G
ALLERY; |
| 232 } |
| 233 |
| 234 private boolean isCameraTile() { |
| 235 return mBitmapDetails != null && mBitmapDetails.type() == PickerBitmap.C
AMERA; |
| 236 } |
| 237 |
| 238 private boolean isPictureTile() { |
| 239 return mBitmapDetails != null && mBitmapDetails.type() == PickerBitmap.P
ICTURE; |
| 240 } |
| 241 } |
OLD | NEW |