Chromium Code Reviews| 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.download.DownloadUtils; | |
| 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. | |
| 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 = |
| 158 (int) (Runtime.getRuntime().maxMemory() / DownloadUtils.BYTES_PE R_KILOBYTE); | |
| 149 final int cacheSizeLarge = maxMemory / 2; // 1/2 of the available memory . | 159 final int cacheSizeLarge = maxMemory / 2; // 1/2 of the available memory . |
| 150 final int cacheSizeSmall = maxMemory / 8; // 1/8th of the available memo ry. | 160 final int cacheSizeSmall = maxMemory / 8; // 1/8th of the available memo ry. |
| 151 mLowResBitmaps = new LruCache<String, Bitmap>(cacheSizeSmall); | 161 mLowResBitmaps = new LruCache<String, Bitmap>(cacheSizeSmall); |
| 152 mHighResBitmaps = new LruCache<String, Bitmap>(cacheSizeLarge); | 162 mHighResBitmaps = new LruCache<String, Bitmap>(cacheSizeLarge); |
| 153 } | 163 } |
| 154 | 164 |
| 155 @Override | 165 @Override |
| 156 public void onConfigurationChanged(Configuration newConfig) { | 166 public void onConfigurationChanged(Configuration newConfig) { |
| 157 super.onConfigurationChanged(newConfig); | 167 super.onConfigurationChanged(newConfig); |
| 158 | 168 |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 188 PhotoPickerDialog dialog, PhotoPickerListener listener, boolean mult iSelectionAllowed) { | 198 PhotoPickerDialog dialog, PhotoPickerListener listener, boolean mult iSelectionAllowed) { |
| 189 if (!multiSelectionAllowed) mSelectionDelegate.setSingleSelectionMode(); | 199 if (!multiSelectionAllowed) mSelectionDelegate.setSingleSelectionMode(); |
| 190 | 200 |
| 191 mDialog = dialog; | 201 mDialog = dialog; |
| 192 mMultiSelectionAllowed = multiSelectionAllowed; | 202 mMultiSelectionAllowed = multiSelectionAllowed; |
| 193 mListener = listener; | 203 mListener = listener; |
| 194 | 204 |
| 195 mDialog.setOnCancelListener(new DialogInterface.OnCancelListener() { | 205 mDialog.setOnCancelListener(new DialogInterface.OnCancelListener() { |
| 196 @Override | 206 @Override |
| 197 public void onCancel(DialogInterface dialog) { | 207 public void onCancel(DialogInterface dialog) { |
| 208 recordFinalUmaStats(ACTION_CANCEL); | |
| 198 mListener.onPickerUserAction(PhotoPickerListener.Action.CANCEL, null); | 209 mListener.onPickerUserAction(PhotoPickerListener.Action.CANCEL, null); |
| 199 } | 210 } |
| 200 }); | 211 }); |
| 201 } | 212 } |
| 202 | 213 |
| 203 // FileEnumWorkerTask.FilesEnumeratedCallback: | 214 // FileEnumWorkerTask.FilesEnumeratedCallback: |
| 204 | 215 |
| 205 @Override | 216 @Override |
| 206 public void filesEnumeratedCallback(List<PickerBitmap> files) { | 217 public void filesEnumeratedCallback(List<PickerBitmap> files) { |
| 207 mPickerBitmaps = files; | 218 mPickerBitmaps = files; |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 225 if (filePath != null) { | 236 if (filePath != null) { |
| 226 getDecoderServiceHost().cancelDecodeImage(filePath); | 237 getDecoderServiceHost().cancelDecodeImage(filePath); |
| 227 } | 238 } |
| 228 } | 239 } |
| 229 | 240 |
| 230 // OnClickListener: | 241 // OnClickListener: |
| 231 | 242 |
| 232 @Override | 243 @Override |
| 233 public void onClick(View view) { | 244 public void onClick(View view) { |
| 234 if (view.getId() == R.id.done) { | 245 if (view.getId() == R.id.done) { |
| 246 recordFinalUmaStats(ACTION_PHOTO_PICKED); | |
| 235 notifyPhotosSelected(); | 247 notifyPhotosSelected(); |
| 236 } else { | 248 } else { |
| 249 recordFinalUmaStats(ACTION_CANCEL); | |
| 237 mListener.onPickerUserAction(PhotoPickerListener.Action.CANCEL, null ); | 250 mListener.onPickerUserAction(PhotoPickerListener.Action.CANCEL, null ); |
| 238 } | 251 } |
| 239 | 252 |
| 240 mDialog.dismiss(); | 253 mDialog.dismiss(); |
| 241 } | 254 } |
| 242 | 255 |
| 243 /** | 256 /** |
| 244 * Start loading of bitmaps, once files have been enumerated and service is | 257 * Start loading of bitmaps, once files have been enumerated and service is |
| 245 * ready to decode. | 258 * ready to decode. |
| 246 */ | 259 */ |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 277 } | 290 } |
| 278 | 291 |
| 279 public boolean isMultiSelectAllowed() { | 292 public boolean isMultiSelectAllowed() { |
| 280 return mMultiSelectionAllowed; | 293 return mMultiSelectionAllowed; |
| 281 } | 294 } |
| 282 | 295 |
| 283 /** | 296 /** |
| 284 * Notifies the listener that the user selected to launch the gallery. | 297 * Notifies the listener that the user selected to launch the gallery. |
| 285 */ | 298 */ |
| 286 public void showGallery() { | 299 public void showGallery() { |
| 300 recordFinalUmaStats(ACTION_BROWSE); | |
| 287 mListener.onPickerUserAction(PhotoPickerListener.Action.LAUNCH_GALLERY, null); | 301 mListener.onPickerUserAction(PhotoPickerListener.Action.LAUNCH_GALLERY, null); |
| 288 } | 302 } |
| 289 | 303 |
| 290 /** | 304 /** |
| 291 * Notifies the listener that the user selected to launch the camera intent. | 305 * Notifies the listener that the user selected to launch the camera intent. |
| 292 */ | 306 */ |
| 293 public void showCamera() { | 307 public void showCamera() { |
| 308 recordFinalUmaStats(ACTION_NEW_PHOTO); | |
| 294 mListener.onPickerUserAction(PhotoPickerListener.Action.LAUNCH_CAMERA, n ull); | 309 mListener.onPickerUserAction(PhotoPickerListener.Action.LAUNCH_CAMERA, n ull); |
| 295 } | 310 } |
| 296 | 311 |
| 297 /** | 312 /** |
| 298 * Calculates image size and how many columns can fit on-screen. | 313 * Calculates image size and how many columns can fit on-screen. |
| 299 */ | 314 */ |
| 300 private void calculateGridMetrics() { | 315 private void calculateGridMetrics() { |
| 301 Rect appRect = new Rect(); | 316 Rect appRect = new Rect(); |
| 302 ((Activity) mContext).getWindow().getDecorView().getWindowVisibleDisplay Frame(appRect); | 317 ((Activity) mContext).getWindow().getDecorView().getWindowVisibleDisplay Frame(appRect); |
| 303 | 318 |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 376 if (position < mSpanCount) { | 391 if (position < mSpanCount) { |
| 377 top = mSpacing; | 392 top = mSpacing; |
| 378 } | 393 } |
| 379 bottom = mSpacing; | 394 bottom = mSpacing; |
| 380 } | 395 } |
| 381 | 396 |
| 382 outRect.set(left, top, right, bottom); | 397 outRect.set(left, top, right, bottom); |
| 383 } | 398 } |
| 384 } | 399 } |
| 385 | 400 |
| 401 /** | |
| 402 * Record UMA statistics (what action was taken in the dialog and other perf ormance stats. | |
|
Theresa
2017/06/01 15:58:04
nit: the ( parenthesis is missing a closing parent
| |
| 403 * @param action The action the user took in the dialog. | |
| 404 */ | |
| 405 private void recordFinalUmaStats(int action) { | |
| 406 RecordHistogram.recordEnumeratedHistogram( | |
| 407 "Android.PhotoPicker.DialogAction", action, ACTION_BOUNDARY); | |
| 408 RecordHistogram.recordCountHistogram( | |
| 409 "Android.PhotoPicker.DecodeRequests", mPickerAdapter.getDecodeRe questCount()); | |
| 410 RecordHistogram.recordCountHistogram( | |
| 411 "Android.PhotoPicker.CacheHits", mPickerAdapter.getCacheHitCount ()); | |
| 412 } | |
| 413 | |
| 386 /** Sets a list of files to use as data for the dialog. For testing use only . */ | 414 /** Sets a list of files to use as data for the dialog. For testing use only . */ |
| 387 @VisibleForTesting | 415 @VisibleForTesting |
| 388 public static void setTestFiles(List<PickerBitmap> testFiles) { | 416 public static void setTestFiles(List<PickerBitmap> testFiles) { |
| 389 sTestFiles = new ArrayList<>(testFiles); | 417 sTestFiles = new ArrayList<>(testFiles); |
| 390 } | 418 } |
| 391 | 419 |
| 392 @VisibleForTesting | 420 @VisibleForTesting |
| 393 public SelectionDelegate<PickerBitmap> getSelectionDelegateForTesting() { | 421 public SelectionDelegate<PickerBitmap> getSelectionDelegateForTesting() { |
| 394 return mSelectionDelegate; | 422 return mSelectionDelegate; |
| 395 } | 423 } |
| 396 } | 424 } |
| OLD | NEW |