OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
Theresa
2017/04/04 15:48:33
s/2016/2017
Finnur
2017/04/04 18:05:37
Done.
| |
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.BitmapFactory; | |
11 import android.graphics.Color; | |
12 import android.graphics.PorterDuff; | |
13 import android.graphics.drawable.BitmapDrawable; | |
14 import android.graphics.drawable.Drawable; | |
15 import android.support.annotation.Nullable; | |
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. | |
Theresa
2017/04/04 15:48:33
nit: s/photo picker/Photo Picker for consistency i
Finnur
2017/04/04 18:05:37
Done.
| |
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 mRequest; | |
Theresa
2017/04/04 15:48:33
nit: mBitmap? The PickerBitmap object isn't actual
Finnur
2017/04/04 18:05:37
I didn't like calling it mBitmap/mImage or somethi
Theresa
2017/04/04 18:40:46
Acknowledged.
| |
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 (mRequest != null && mRequest.type() == PickerBitmap.TileTypes.GALLER Y) { | |
87 mCategoryView.showGallery(); | |
88 return; | |
89 } else if (mRequest != null && mRequest.type() == PickerBitmap.TileTypes .CAMERA) { | |
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 (mRequest != null | |
102 && (mRequest.type() == PickerBitmap.TileTypes.GALLERY | |
103 || mRequest.type() == PickerBitmap.TileTypes.CAMERA)) { | |
Theresa
2017/04/04 15:48:33
nit: && mRequest.type() != PickerBitmap.TileTypes.
Finnur
2017/04/04 18:05:37
Did it slightly differently...
| |
104 return false; | |
105 } | |
106 return super.toggleSelectionForItem(item); | |
107 } | |
108 | |
109 @Override | |
110 public void setChecked(boolean checked) { | |
111 if (mRequest != null && mRequest.type() != PickerBitmap.TileTypes.PICTUR E) { | |
112 return; | |
113 } | |
114 | |
115 super.setChecked(checked); | |
116 updateSelectionState(); | |
117 } | |
118 | |
119 @Override | |
120 public void onSelectionStateChange(List<PickerBitmap> selectedItems) { | |
121 boolean selected = selectedItems.contains(mRequest); | |
Theresa
2017/04/04 15:48:33
It appears this variable isn't used anymore.
Finnur
2017/04/04 18:05:37
Done.
| |
122 | |
123 updateSelectionState(); | |
124 } | |
125 | |
126 /** | |
127 * Pre-initializes the PickerBitmapView. | |
128 * @param categoryView The category view showing the images. | |
129 */ | |
130 public void preInitialize(PickerCategoryView categoryView) { | |
Theresa
2017/04/04 15:48:33
nit: Maybe we can just call this setCategoryView(
Finnur
2017/04/04 18:05:37
Also very reasonable.
| |
131 mCategoryView = categoryView; | |
132 mSelectionDelegate = mCategoryView.getSelectionDelegate(); | |
133 super.setSelectionDelegate(mSelectionDelegate); | |
134 } | |
135 | |
136 /** | |
137 * Completes the initialization of the PickerBitmapView. Must be called befo re the image can | |
138 * respond to click events. | |
139 * @param request The request represented by this PickerBitmapView. | |
140 * @param thumbnail The Bitmap to use for the thumbnail (or null). | |
141 * @param placeholder Whether the image given is a placeholder or the actual image. | |
142 */ | |
143 public void initialize(PickerBitmap request, @Nullable Bitmap thumbnail, boo lean placeholder) { | |
144 resetTile(); | |
145 | |
146 mRequest = request; | |
147 setItem(request); | |
148 setThumbnailBitmap(thumbnail); | |
149 mImageLoaded = !placeholder; | |
150 | |
151 updateSelectionState(); | |
152 } | |
153 | |
154 /** | |
155 * Initialization for the special tiles (camera/gallery icon). | |
156 * @param request The request represented by this PickerBitmapView. | |
157 */ | |
158 public void initializeSpecialTile(PickerBitmap request) { | |
159 int size = mCategoryView.getImageSize(); | |
160 Bitmap tile = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); | |
161 tile.eraseColor(Color.argb(0, 0, 0, 0)); | |
162 | |
163 int iconBitmapId, labelStringId; | |
164 if (request != null && request.type() == PickerBitmap.TileTypes.CAMERA) { | |
165 iconBitmapId = R.drawable.ic_photo_camera; | |
166 labelStringId = R.string.file_picker_camera; | |
167 } else { | |
168 iconBitmapId = R.drawable.ic_collections_black_24dp; | |
169 labelStringId = R.string.file_picker_browse; | |
170 } | |
171 | |
172 Resources resources = mContext.getResources(); | |
173 mSpecialTile.setText(labelStringId); | |
174 Bitmap icon = BitmapFactory.decodeResource(resources, iconBitmapId); | |
175 float pixels = resources.getDimensionPixelOffset(R.dimen.file_picker_spe cial_icon_size); | |
176 BitmapDrawable drawable = new BitmapDrawable( | |
177 resources, Bitmap.createScaledBitmap(icon, (int) pixels, (int) p ixels, true)); | |
178 ApiCompatibilityUtils.setCompoundDrawablesRelativeWithIntrinsicBounds( | |
179 mSpecialTile, null, drawable, null, null); | |
180 | |
181 initialize(request, tile, false); | |
182 | |
183 mSpecialTile.setVisibility(View.VISIBLE); | |
184 } | |
185 | |
186 /** | |
187 * Sets a thumbnail bitmap for the current view. | |
188 * @param thumbnail The Bitmap to use for the icon ImageView. | |
189 * @return True if no image was loaded before (e.g. not even a low-res image ). | |
190 */ | |
191 public boolean setThumbnailBitmap(Bitmap thumbnail) { | |
192 mIconView.setImageBitmap(thumbnail); | |
193 | |
194 boolean noImageWasLoaded = !mImageLoaded; | |
195 mImageLoaded = true; | |
196 updateSelectionState(); | |
197 | |
198 return noImageWasLoaded; | |
199 } | |
200 | |
201 /** | |
202 * Resets the view to its starting state, which is necessary when the view i s about to be | |
203 * re-used. | |
204 */ | |
205 private void resetTile() { | |
206 mSpecialTile.setVisibility(View.GONE); | |
207 } | |
208 | |
209 /** | |
210 * Updates the selection controls for this view. | |
211 */ | |
212 private void updateSelectionState() { | |
213 boolean special = mRequest != null && mRequest.type() != PickerBitmap.Ti leTypes.PICTURE; | |
214 boolean checked = super.isChecked(); | |
215 boolean anySelection = | |
216 mSelectionDelegate != null && mSelectionDelegate.isSelectionEnab led(); | |
217 int bgColorId, fgColorId; | |
218 if (!special) { | |
219 bgColorId = R.color.file_picker_tile_bg_color; | |
220 fgColorId = R.color.file_picker_special_tile_color; | |
221 } else if (!anySelection) { | |
222 bgColorId = R.color.file_picker_special_tile_bg_color; | |
223 fgColorId = R.color.file_picker_special_tile_color; | |
224 } else { | |
225 bgColorId = R.color.file_picker_special_tile_disabled_bg_color; | |
226 fgColorId = R.color.file_picker_special_tile_disabled_color; | |
227 } | |
228 | |
229 Resources resources = mContext.getResources(); | |
230 mBorderView.setBackgroundColor(ApiCompatibilityUtils.getColor(resources, bgColorId)); | |
231 mSpecialTile.setTextColor(ApiCompatibilityUtils.getColor(resources, fgCo lorId)); | |
232 Drawable[] drawables = mSpecialTile.getCompoundDrawables(); | |
233 // The textview only has a top compound drawable (2nd element). | |
234 if (drawables[1] != null) { | |
235 int color = ApiCompatibilityUtils.getColor(resources, fgColorId); | |
236 drawables[1].setColorFilter(color, PorterDuff.Mode.SRC_IN); | |
237 } | |
238 } | |
239 } | |
OLD | NEW |