OLD | NEW |
---|---|
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.photo_picker; | 5 package org.chromium.chrome.browser.photo_picker; |
6 | 6 |
7 import android.app.Activity; | 7 import android.app.Activity; |
8 import android.content.Context; | 8 import android.content.Context; |
9 import android.content.DialogInterface; | 9 import android.content.DialogInterface; |
10 import android.content.res.Configuration; | 10 import android.content.res.Configuration; |
11 import android.graphics.Bitmap; | 11 import android.graphics.Bitmap; |
12 import android.graphics.Rect; | 12 import android.graphics.Rect; |
13 import android.support.v7.widget.GridLayoutManager; | 13 import android.support.v7.widget.GridLayoutManager; |
14 import android.support.v7.widget.RecyclerView; | 14 import android.support.v7.widget.RecyclerView; |
15 import android.util.LruCache; | 15 import android.util.LruCache; |
16 import android.view.LayoutInflater; | 16 import android.view.LayoutInflater; |
17 import android.view.View; | 17 import android.view.View; |
18 import android.widget.Button; | 18 import android.widget.Button; |
19 import android.widget.RelativeLayout; | 19 import android.widget.RelativeLayout; |
20 | 20 |
21 import org.chromium.base.VisibleForTesting; | 21 import org.chromium.base.VisibleForTesting; |
22 import org.chromium.base.metrics.RecordHistogram; | |
22 import org.chromium.chrome.R; | 23 import org.chromium.chrome.R; |
24 import org.chromium.chrome.browser.ConversionUtils; | |
23 import org.chromium.chrome.browser.widget.selection.SelectableListLayout; | 25 import org.chromium.chrome.browser.widget.selection.SelectableListLayout; |
24 import org.chromium.chrome.browser.widget.selection.SelectionDelegate; | 26 import org.chromium.chrome.browser.widget.selection.SelectionDelegate; |
25 import org.chromium.ui.PhotoPickerListener; | 27 import org.chromium.ui.PhotoPickerListener; |
26 | 28 |
27 import java.util.ArrayList; | 29 import java.util.ArrayList; |
28 import java.util.Arrays; | 30 import java.util.Arrays; |
29 import java.util.List; | 31 import java.util.List; |
30 | 32 |
31 /** | 33 /** |
32 * A class for keeping track of common data associated with showing photos in | 34 * A class for keeping track of common data associated with showing photos in |
33 * the photo picker, for example the RecyclerView and the bitmap caches. | 35 * the photo picker, for example the RecyclerView and the bitmap caches. |
34 */ | 36 */ |
35 public class PickerCategoryView extends RelativeLayout | 37 public class PickerCategoryView extends RelativeLayout |
36 implements FileEnumWorkerTask.FilesEnumeratedCallback, RecyclerView.Recy clerListener, | 38 implements FileEnumWorkerTask.FilesEnumeratedCallback, RecyclerView.Recy clerListener, |
37 DecoderServiceHost.ServiceReadyCallback, View.OnClickListener { | 39 DecoderServiceHost.ServiceReadyCallback, View.OnClickListener { |
38 private static final int KILOBYTE = 1024; | 40 // Note: these values must match the PhotoPickerDialogAction enum values in histograms.xml. |
41 // Only add new values at the end, right before ACTIONS. We depend on these specific values in | |
42 // UMA histograms. | |
Mark P
2017/06/02 17:17:57
We use a comment more like the best practices one
Finnur
2017/06/06 14:23:16
Done.
| |
43 private static final int ACTION_CANCEL = 0; | |
44 private static final int ACTION_PHOTO_PICKED = 1; | |
45 private static final int ACTION_NEW_PHOTO = 2; | |
46 private static final int ACTION_BROWSE = 3; | |
47 private static final int ACTION_BOUNDARY = 4; | |
39 | 48 |
40 // The dialog that owns us. | 49 // The dialog that owns us. |
41 private PhotoPickerDialog mDialog; | 50 private PhotoPickerDialog mDialog; |
42 | 51 |
43 // The view containing the RecyclerView and the toolbar, etc. | 52 // The view containing the RecyclerView and the toolbar, etc. |
44 private SelectableListLayout<PickerBitmap> mSelectableListLayout; | 53 private SelectableListLayout<PickerBitmap> mSelectableListLayout; |
45 | 54 |
46 // Our context. | 55 // Our context. |
47 private Context mContext; | 56 private Context mContext; |
48 | 57 |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
138 doneButton.setOnClickListener(this); | 147 doneButton.setOnClickListener(this); |
139 | 148 |
140 calculateGridMetrics(); | 149 calculateGridMetrics(); |
141 | 150 |
142 mLayoutManager = new GridLayoutManager(mContext, mColumns); | 151 mLayoutManager = new GridLayoutManager(mContext, mColumns); |
143 mRecyclerView.setHasFixedSize(true); | 152 mRecyclerView.setHasFixedSize(true); |
144 mRecyclerView.setLayoutManager(mLayoutManager); | 153 mRecyclerView.setLayoutManager(mLayoutManager); |
145 mSpacingDecoration = new GridSpacingItemDecoration(mColumns, mPadding); | 154 mSpacingDecoration = new GridSpacingItemDecoration(mColumns, mPadding); |
146 mRecyclerView.addItemDecoration(mSpacingDecoration); | 155 mRecyclerView.addItemDecoration(mSpacingDecoration); |
147 | 156 |
148 final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / KILOBYTE ); | 157 final int maxMemory = ConversionUtils.bytesToKilobytes(Runtime.getRuntim e().maxMemory()); |
149 final int cacheSizeLarge = maxMemory / 2; // 1/2 of the available memory . | 158 final int cacheSizeLarge = maxMemory / 2; // 1/2 of the available memory . |
150 final int cacheSizeSmall = maxMemory / 8; // 1/8th of the available memo ry. | 159 final int cacheSizeSmall = maxMemory / 8; // 1/8th of the available memo ry. |
151 mLowResBitmaps = new LruCache<String, Bitmap>(cacheSizeSmall); | 160 mLowResBitmaps = new LruCache<String, Bitmap>(cacheSizeSmall); |
152 mHighResBitmaps = new LruCache<String, Bitmap>(cacheSizeLarge); | 161 mHighResBitmaps = new LruCache<String, Bitmap>(cacheSizeLarge); |
153 } | 162 } |
154 | 163 |
155 @Override | 164 @Override |
156 public void onConfigurationChanged(Configuration newConfig) { | 165 public void onConfigurationChanged(Configuration newConfig) { |
157 super.onConfigurationChanged(newConfig); | 166 super.onConfigurationChanged(newConfig); |
158 | 167 |
(...skipping 29 matching lines...) Expand all Loading... | |
188 PhotoPickerDialog dialog, PhotoPickerListener listener, boolean mult iSelectionAllowed) { | 197 PhotoPickerDialog dialog, PhotoPickerListener listener, boolean mult iSelectionAllowed) { |
189 if (!multiSelectionAllowed) mSelectionDelegate.setSingleSelectionMode(); | 198 if (!multiSelectionAllowed) mSelectionDelegate.setSingleSelectionMode(); |
190 | 199 |
191 mDialog = dialog; | 200 mDialog = dialog; |
192 mMultiSelectionAllowed = multiSelectionAllowed; | 201 mMultiSelectionAllowed = multiSelectionAllowed; |
193 mListener = listener; | 202 mListener = listener; |
194 | 203 |
195 mDialog.setOnCancelListener(new DialogInterface.OnCancelListener() { | 204 mDialog.setOnCancelListener(new DialogInterface.OnCancelListener() { |
196 @Override | 205 @Override |
197 public void onCancel(DialogInterface dialog) { | 206 public void onCancel(DialogInterface dialog) { |
207 recordFinalUmaStats(ACTION_CANCEL); | |
198 mListener.onPickerUserAction(PhotoPickerListener.Action.CANCEL, null); | 208 mListener.onPickerUserAction(PhotoPickerListener.Action.CANCEL, null); |
199 } | 209 } |
200 }); | 210 }); |
201 } | 211 } |
202 | 212 |
203 // FileEnumWorkerTask.FilesEnumeratedCallback: | 213 // FileEnumWorkerTask.FilesEnumeratedCallback: |
204 | 214 |
205 @Override | 215 @Override |
206 public void filesEnumeratedCallback(List<PickerBitmap> files) { | 216 public void filesEnumeratedCallback(List<PickerBitmap> files) { |
207 mPickerBitmaps = files; | 217 mPickerBitmaps = files; |
(...skipping 17 matching lines...) Expand all Loading... | |
225 if (filePath != null) { | 235 if (filePath != null) { |
226 getDecoderServiceHost().cancelDecodeImage(filePath); | 236 getDecoderServiceHost().cancelDecodeImage(filePath); |
227 } | 237 } |
228 } | 238 } |
229 | 239 |
230 // OnClickListener: | 240 // OnClickListener: |
231 | 241 |
232 @Override | 242 @Override |
233 public void onClick(View view) { | 243 public void onClick(View view) { |
234 if (view.getId() == R.id.done) { | 244 if (view.getId() == R.id.done) { |
245 recordFinalUmaStats(ACTION_PHOTO_PICKED); | |
235 notifyPhotosSelected(); | 246 notifyPhotosSelected(); |
236 } else { | 247 } else { |
248 recordFinalUmaStats(ACTION_CANCEL); | |
237 mListener.onPickerUserAction(PhotoPickerListener.Action.CANCEL, null ); | 249 mListener.onPickerUserAction(PhotoPickerListener.Action.CANCEL, null ); |
Mark P
2017/06/02 17:17:58
nit: if this triggers a cancel listener, does that
Finnur
2017/06/06 14:23:16
This is listening for the user clicking the X to c
Mark P
2017/06/07 17:19:30
Acknowledged, thanks for checking.
| |
238 } | 250 } |
239 | 251 |
240 mDialog.dismiss(); | 252 mDialog.dismiss(); |
241 } | 253 } |
242 | 254 |
243 /** | 255 /** |
244 * Start loading of bitmaps, once files have been enumerated and service is | 256 * Start loading of bitmaps, once files have been enumerated and service is |
245 * ready to decode. | 257 * ready to decode. |
246 */ | 258 */ |
247 private void processBitmaps() { | 259 private void processBitmaps() { |
(...skipping 29 matching lines...) Expand all Loading... | |
277 } | 289 } |
278 | 290 |
279 public boolean isMultiSelectAllowed() { | 291 public boolean isMultiSelectAllowed() { |
280 return mMultiSelectionAllowed; | 292 return mMultiSelectionAllowed; |
281 } | 293 } |
282 | 294 |
283 /** | 295 /** |
284 * Notifies the listener that the user selected to launch the gallery. | 296 * Notifies the listener that the user selected to launch the gallery. |
285 */ | 297 */ |
286 public void showGallery() { | 298 public void showGallery() { |
299 recordFinalUmaStats(ACTION_BROWSE); | |
287 mListener.onPickerUserAction(PhotoPickerListener.Action.LAUNCH_GALLERY, null); | 300 mListener.onPickerUserAction(PhotoPickerListener.Action.LAUNCH_GALLERY, null); |
288 } | 301 } |
289 | 302 |
290 /** | 303 /** |
291 * Notifies the listener that the user selected to launch the camera intent. | 304 * Notifies the listener that the user selected to launch the camera intent. |
292 */ | 305 */ |
293 public void showCamera() { | 306 public void showCamera() { |
307 recordFinalUmaStats(ACTION_NEW_PHOTO); | |
294 mListener.onPickerUserAction(PhotoPickerListener.Action.LAUNCH_CAMERA, n ull); | 308 mListener.onPickerUserAction(PhotoPickerListener.Action.LAUNCH_CAMERA, n ull); |
295 } | 309 } |
296 | 310 |
297 /** | 311 /** |
298 * Calculates image size and how many columns can fit on-screen. | 312 * Calculates image size and how many columns can fit on-screen. |
299 */ | 313 */ |
300 private void calculateGridMetrics() { | 314 private void calculateGridMetrics() { |
301 Rect appRect = new Rect(); | 315 Rect appRect = new Rect(); |
302 ((Activity) mContext).getWindow().getDecorView().getWindowVisibleDisplay Frame(appRect); | 316 ((Activity) mContext).getWindow().getDecorView().getWindowVisibleDisplay Frame(appRect); |
303 | 317 |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
376 if (position < mSpanCount) { | 390 if (position < mSpanCount) { |
377 top = mSpacing; | 391 top = mSpacing; |
378 } | 392 } |
379 bottom = mSpacing; | 393 bottom = mSpacing; |
380 } | 394 } |
381 | 395 |
382 outRect.set(left, top, right, bottom); | 396 outRect.set(left, top, right, bottom); |
383 } | 397 } |
384 } | 398 } |
385 | 399 |
400 /** | |
401 * Record UMA statistics (what action was taken in the dialog and other perf ormance stats). | |
402 * @param action The action the user took in the dialog. | |
403 */ | |
404 private void recordFinalUmaStats(int action) { | |
405 RecordHistogram.recordEnumeratedHistogram( | |
406 "Android.PhotoPicker.DialogAction", action, ACTION_BOUNDARY); | |
Mark P
2017/06/02 17:17:57
optional nit: Are you interested in what actions h
Finnur
2017/06/06 14:23:16
Thanks for the tip, but the order is not noteworth
Mark P
2017/06/07 17:19:30
Acknowledged, though I was more curious whether yo
Finnur
2017/06/08 10:39:31
There's little left to track; it is a very simple
| |
407 RecordHistogram.recordCountHistogram( | |
408 "Android.PhotoPicker.DecodeRequests", mPickerAdapter.getDecodeRe questCount()); | |
409 RecordHistogram.recordCountHistogram( | |
410 "Android.PhotoPicker.CacheHits", mPickerAdapter.getCacheHitCount ()); | |
411 } | |
412 | |
386 /** Sets a list of files to use as data for the dialog. For testing use only . */ | 413 /** Sets a list of files to use as data for the dialog. For testing use only . */ |
387 @VisibleForTesting | 414 @VisibleForTesting |
388 public static void setTestFiles(List<PickerBitmap> testFiles) { | 415 public static void setTestFiles(List<PickerBitmap> testFiles) { |
389 sTestFiles = new ArrayList<>(testFiles); | 416 sTestFiles = new ArrayList<>(testFiles); |
390 } | 417 } |
391 | 418 |
392 @VisibleForTesting | 419 @VisibleForTesting |
393 public SelectionDelegate<PickerBitmap> getSelectionDelegateForTesting() { | 420 public SelectionDelegate<PickerBitmap> getSelectionDelegateForTesting() { |
394 return mSelectionDelegate; | 421 return mSelectionDelegate; |
395 } | 422 } |
396 } | 423 } |
OLD | NEW |