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() { | |
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 // TODO(finnur): Remove the null checks here and below. | |
84 if (isGalleryTile()) { | |
85 mCategoryView.showGallery(); | |
86 return; | |
87 } else if (isCameraTile()) { | |
88 mCategoryView.showCamera(); | |
89 return; | |
90 } | |
91 | |
92 // The SelectableItemView expects long press to be the selection event, but this class wants | |
93 // that to happen on click instead. | |
94 super.onLongClick(this); | |
95 } | |
96 | |
97 @Override | |
98 protected boolean toggleSelectionForItem(PickerBitmap item) { | |
99 if (isGalleryTile() || isCameraTile()) return false; | |
100 return super.toggleSelectionForItem(item); | |
101 } | |
102 | |
103 @Override | |
104 public void setChecked(boolean checked) { | |
105 if (!isPictureTile()) { | |
106 return; | |
107 } | |
108 | |
109 super.setChecked(checked); | |
110 updateSelectionState(); | |
111 } | |
112 | |
113 @Override | |
114 public void onSelectionStateChange(List<PickerBitmap> selectedItems) { | |
115 updateSelectionState(); | |
116 } | |
117 | |
118 /** | |
119 * Sets the {@link PickerCategoryView} for this PickerBitmapView. | |
120 * @param categoryView The category view showing the images. Used to access | |
121 * common functionality and sizes and retrieve the {@link SelectionDeleg ate}. | |
122 */ | |
123 public void setCategoryView(PickerCategoryView categoryView) { | |
124 mCategoryView = categoryView; | |
125 mSelectionDelegate = mCategoryView.getSelectionDelegate(); | |
126 super.setSelectionDelegate(mSelectionDelegate); | |
127 } | |
128 | |
129 /** | |
130 * Completes the initialization of the PickerBitmapView. Must be called befo re the image can | |
131 * respond to click events. | |
132 * @param bitmapDetails The details about the bitmap represented by this Pic kerBitmapView. | |
133 * @param thumbnail The Bitmap to use for the thumbnail (or null). | |
134 * @param placeholder Whether the image given is a placeholder or the actual image. | |
135 */ | |
136 public void initialize( | |
137 PickerBitmap bitmapDetails, @Nullable Bitmap thumbnail, boolean plac eholder) { | |
138 resetTile(); | |
139 | |
140 mBitmapDetails = bitmapDetails; | |
141 setItem(bitmapDetails); | |
142 setThumbnailBitmap(thumbnail); | |
143 mImageLoaded = !placeholder; | |
144 | |
145 updateSelectionState(); | |
146 } | |
147 | |
148 /** | |
149 * Initialization for the special tiles (camera/gallery icon). | |
150 * @param bitmapDetails The request represented by this PickerBitmapView. | |
Theresa
2017/04/05 15:48:24
nit: Rework the @param description here too
Finnur
2017/04/06 13:22:04
Done.
| |
151 */ | |
152 public void initializeSpecialTile(PickerBitmap bitmapDetails) { | |
153 int labelStringId; | |
154 Drawable image; | |
155 Resources resources = mContext.getResources(); | |
156 | |
157 if (isCameraTile()) { | |
158 image = ApiCompatibilityUtils.getDrawable(resources, R.drawable.ic_p hoto_camera); | |
159 labelStringId = R.string.file_picker_camera; | |
160 } else { | |
161 image = VectorDrawableCompat.create( | |
162 resources, R.drawable.ic_collections_grey, mContext.getTheme ()); | |
163 labelStringId = R.string.file_picker_browse; | |
164 } | |
165 | |
166 ApiCompatibilityUtils.setCompoundDrawablesRelativeWithIntrinsicBounds( | |
167 mSpecialTile, null, image, null, null); | |
168 mSpecialTile.setText(labelStringId); | |
169 | |
170 initialize(bitmapDetails, null, false); | |
171 | |
172 // Reset visibility, since #initialize() sets mSpecialTile visibility to GONE. | |
173 mSpecialTile.setVisibility(View.VISIBLE); | |
174 } | |
175 | |
176 /** | |
177 * Sets a thumbnail bitmap for the current view. | |
178 * @param thumbnail The Bitmap to use for the icon ImageView. | |
179 * @return True if no image was loaded before (e.g. not even a low-res image ). | |
180 */ | |
181 public boolean setThumbnailBitmap(Bitmap thumbnail) { | |
182 mIconView.setImageBitmap(thumbnail); | |
183 | |
184 boolean noImageWasLoaded = !mImageLoaded; | |
185 mImageLoaded = true; | |
186 updateSelectionState(); | |
187 | |
188 return noImageWasLoaded; | |
189 } | |
190 | |
191 /** | |
192 * Resets the view to its starting state, which is necessary when the view i s about to be | |
193 * re-used. | |
194 */ | |
195 private void resetTile() { | |
196 mSpecialTile.setVisibility(View.GONE); | |
197 } | |
198 | |
199 /** | |
200 * Updates the selection controls for this view. | |
201 */ | |
202 private void updateSelectionState() { | |
203 boolean special = !isPictureTile(); | |
204 boolean checked = super.isChecked(); | |
205 boolean anySelection = | |
206 mSelectionDelegate != null && mSelectionDelegate.isSelectionEnab led(); | |
207 int bgColorId, fgColorId; | |
208 if (!special) { | |
209 bgColorId = R.color.file_picker_tile_bg_color; | |
210 fgColorId = R.color.file_picker_special_tile_color; | |
211 } else if (!anySelection) { | |
212 bgColorId = R.color.file_picker_special_tile_bg_color; | |
213 fgColorId = R.color.file_picker_special_tile_color; | |
214 } else { | |
215 bgColorId = R.color.file_picker_special_tile_disabled_bg_color; | |
216 fgColorId = R.color.file_picker_special_tile_disabled_color; | |
217 } | |
218 | |
219 Resources resources = mContext.getResources(); | |
220 mBorderView.setBackgroundColor(ApiCompatibilityUtils.getColor(resources, bgColorId)); | |
221 mSpecialTile.setTextColor(ApiCompatibilityUtils.getColor(resources, fgCo lorId)); | |
222 Drawable[] drawables = mSpecialTile.getCompoundDrawables(); | |
223 // The textview only has a top compound drawable (2nd element). | |
224 if (drawables[1] != null) { | |
225 int color = ApiCompatibilityUtils.getColor(resources, fgColorId); | |
226 drawables[1].setColorFilter(color, PorterDuff.Mode.SRC_IN); | |
227 } | |
228 } | |
229 | |
230 private boolean isGalleryTile() { | |
231 return mBitmapDetails != null && mBitmapDetails.type() == PickerBitmap.T ileTypes.GALLERY; | |
232 } | |
233 | |
234 private boolean isCameraTile() { | |
235 return mBitmapDetails != null && mBitmapDetails.type() == PickerBitmap.T ileTypes.CAMERA; | |
236 } | |
237 | |
238 private boolean isPictureTile() { | |
239 return mBitmapDetails != null && mBitmapDetails.type() == PickerBitmap.T ileTypes.PICTURE; | |
240 } | |
241 } | |
OLD | NEW |