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