| 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.graphics.Bitmap; | 7 import android.graphics.Bitmap; |
| 8 import android.os.AsyncTask; | 8 import android.os.AsyncTask; |
| 9 import android.os.SystemClock; |
| 9 import android.util.LruCache; | 10 import android.util.LruCache; |
| 10 | 11 |
| 11 import org.chromium.base.ThreadUtils; | 12 import org.chromium.base.ThreadUtils; |
| 13 import org.chromium.base.metrics.RecordHistogram; |
| 14 |
| 15 import java.util.concurrent.TimeUnit; |
| 12 | 16 |
| 13 /** | 17 /** |
| 14 * A worker task to scale bitmaps in the background. | 18 * A worker task to scale bitmaps in the background. |
| 15 */ | 19 */ |
| 16 class BitmapScalerTask extends AsyncTask<Bitmap, Void, Bitmap> { | 20 class BitmapScalerTask extends AsyncTask<Bitmap, Void, Bitmap> { |
| 17 private final LruCache<String, Bitmap> mCache; | 21 private final LruCache<String, Bitmap> mCache; |
| 18 private final String mFilePath; | 22 private final String mFilePath; |
| 19 private final int mSize; | 23 private final int mSize; |
| 20 | 24 |
| 21 /** | 25 /** |
| 22 * A BitmapScalerTask constructor. | 26 * A BitmapScalerTask constructor. |
| 23 */ | 27 */ |
| 24 public BitmapScalerTask(LruCache<String, Bitmap> cache, String filePath, int
size) { | 28 public BitmapScalerTask(LruCache<String, Bitmap> cache, String filePath, int
size) { |
| 25 mCache = cache; | 29 mCache = cache; |
| 26 mFilePath = filePath; | 30 mFilePath = filePath; |
| 27 mSize = size; | 31 mSize = size; |
| 28 } | 32 } |
| 29 | 33 |
| 30 /** | 34 /** |
| 31 * Scales the image provided. Called on a non-UI thread. | 35 * Scales the image provided. Called on a non-UI thread. |
| 32 * @param params Ignored, do not use. | 36 * @param params Ignored, do not use. |
| 33 * @return A sorted list of images (by last-modified first). | 37 * @return A sorted list of images (by last-modified first). |
| 34 */ | 38 */ |
| 35 @Override | 39 @Override |
| 36 protected Bitmap doInBackground(Bitmap... bitmaps) { | 40 protected Bitmap doInBackground(Bitmap... bitmaps) { |
| 37 assert !ThreadUtils.runningOnUiThread(); | 41 assert !ThreadUtils.runningOnUiThread(); |
| 38 | 42 |
| 39 if (isCancelled()) return null; | 43 if (isCancelled()) return null; |
| 40 | 44 |
| 41 return BitmapUtils.scale(bitmaps[0], mSize, false); | 45 long begin = SystemClock.elapsedRealtime(); |
| 46 Bitmap bitmap = BitmapUtils.scale(bitmaps[0], mSize, false); |
| 47 long scaleTime = SystemClock.elapsedRealtime() - begin; |
| 48 RecordHistogram.recordTimesHistogram( |
| 49 "Android.PhotoPicker.BitmapScalerTask", scaleTime, TimeUnit.MILL
ISECONDS); |
| 50 return bitmap; |
| 42 } | 51 } |
| 43 | 52 |
| 44 /** | 53 /** |
| 45 * Communicates the results back to the client. Called on the UI thread. | 54 * Communicates the results back to the client. Called on the UI thread. |
| 46 * @param result The resulting scaled bitmap. | 55 * @param result The resulting scaled bitmap. |
| 47 */ | 56 */ |
| 48 @Override | 57 @Override |
| 49 protected void onPostExecute(Bitmap result) { | 58 protected void onPostExecute(Bitmap result) { |
| 50 if (isCancelled()) { | 59 if (isCancelled()) { |
| 51 return; | 60 return; |
| 52 } | 61 } |
| 53 | 62 |
| 54 mCache.put(mFilePath, result); | 63 mCache.put(mFilePath, result); |
| 55 } | 64 } |
| 56 } | 65 } |
| OLD | NEW |