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 onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
64 super.onMeasure(widthMeasureSpec, heightMeasureSpec); | |
65 | |
66 if (mCategoryView == null) return; | |
67 | |
68 int width = mCategoryView.getImageSize(); | |
69 int height = mCategoryView.getImageSize(); | |
70 setMeasuredDimension(width, height); | |
71 } | |
72 | |
73 @Override | |
74 protected void onFinishInflate() { | |
Ted C
2017/04/06 23:52:15
nit, I'd put this right under the constructor as i
Finnur
2017/04/07 13:52:21
Done.
| |
75 super.onFinishInflate(); | |
76 mIconView = (ImageView) findViewById(R.id.bitmap_view); | |
77 mBorderView = findViewById(R.id.border); | |
78 mSpecialTile = (TextView) findViewById(R.id.special_tile); | |
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 super.onLongClick(this); | |
Ted C
2017/04/06 23:52:15
Do you specifically need super. here? That would
Finnur
2017/04/07 13:52:21
Done.
| |
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 super.setSelectionDelegate(mSelectionDelegate); | |
Ted C
2017/04/06 23:52:15
same comment where I don't think you need the supe
Finnur
2017/04/07 13:52:21
Done.
| |
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, fgColorId; | |
Ted C
2017/04/06 23:52:15
one init per line plz :-)
Finnur
2017/04/07 13:52:21
Done.
| |
206 if (!special) { | |
207 bgColorId = R.color.photo_picker_tile_bg_color; | |
208 fgColorId = R.color.photo_picker_special_tile_color; | |
209 } else if (!anySelection) { | |
210 bgColorId = R.color.photo_picker_special_tile_bg_color; | |
211 fgColorId = R.color.photo_picker_special_tile_color; | |
212 } else { | |
213 bgColorId = R.color.photo_picker_special_tile_disabled_bg_color; | |
214 fgColorId = R.color.photo_picker_special_tile_disabled_color; | |
215 } | |
216 | |
217 Resources resources = mContext.getResources(); | |
218 mBorderView.setBackgroundColor(ApiCompatibilityUtils.getColor(resources, bgColorId)); | |
219 mSpecialTile.setTextColor(ApiCompatibilityUtils.getColor(resources, fgCo lorId)); | |
220 Drawable[] drawables = mSpecialTile.getCompoundDrawables(); | |
221 // The textview only has a top compound drawable (2nd element). | |
222 if (drawables[1] != null) { | |
223 int color = ApiCompatibilityUtils.getColor(resources, fgColorId); | |
224 drawables[1].setColorFilter(color, PorterDuff.Mode.SRC_IN); | |
225 } | |
226 } | |
227 | |
228 private boolean isGalleryTile() { | |
229 // TODO(finnur): Remove the null checks here and below. | |
230 return mBitmapDetails != null && mBitmapDetails.type() == PickerBitmap.T ileTypes.GALLERY; | |
231 } | |
232 | |
233 private boolean isCameraTile() { | |
234 return mBitmapDetails != null && mBitmapDetails.type() == PickerBitmap.T ileTypes.CAMERA; | |
235 } | |
236 | |
237 private boolean isPictureTile() { | |
238 return mBitmapDetails != null && mBitmapDetails.type() == PickerBitmap.T ileTypes.PICTURE; | |
239 } | |
240 } | |
OLD | NEW |