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.graphics.Bitmap; | |
| 9 import android.graphics.Rect; | 10 import android.graphics.Rect; |
| 10 import android.support.v7.widget.GridLayoutManager; | 11 import android.support.v7.widget.GridLayoutManager; |
| 11 import android.support.v7.widget.RecyclerView; | 12 import android.support.v7.widget.RecyclerView; |
| 12 import android.support.v7.widget.Toolbar.OnMenuItemClickListener; | 13 import android.support.v7.widget.Toolbar.OnMenuItemClickListener; |
| 14 import android.util.LruCache; | |
| 13 import android.view.LayoutInflater; | 15 import android.view.LayoutInflater; |
| 14 import android.view.MenuItem; | 16 import android.view.MenuItem; |
| 15 import android.view.View; | 17 import android.view.View; |
| 16 import android.widget.RelativeLayout; | 18 import android.widget.RelativeLayout; |
| 17 | 19 |
| 18 import org.chromium.chrome.R; | 20 import org.chromium.chrome.R; |
| 19 import org.chromium.chrome.browser.widget.selection.SelectableListLayout; | 21 import org.chromium.chrome.browser.widget.selection.SelectableListLayout; |
| 20 import org.chromium.chrome.browser.widget.selection.SelectionDelegate; | 22 import org.chromium.chrome.browser.widget.selection.SelectionDelegate; |
| 21 import org.chromium.ui.PhotoPickerListener; | 23 import org.chromium.ui.PhotoPickerListener; |
| 22 | 24 |
| 23 import java.util.Arrays; | 25 import java.util.Arrays; |
| 24 import java.util.List; | 26 import java.util.List; |
| 25 | 27 |
| 26 /** | 28 /** |
| 27 * A class for keeping track of common data associated with showing photos in | 29 * A class for keeping track of common data associated with showing photos in |
| 28 * the photo picker, for example the RecyclerView and the bitmap caches. | 30 * the photo picker, for example the RecyclerView and the bitmap caches. |
| 29 */ | 31 */ |
| 30 public class PickerCategoryView extends RelativeLayout | 32 public class PickerCategoryView extends RelativeLayout |
| 31 implements FileEnumWorkerTask.FilesEnumeratedCallback, RecyclerView.Recy clerListener, | 33 implements FileEnumWorkerTask.FilesEnumeratedCallback, RecyclerView.Recy clerListener, |
| 32 DecoderServiceHost.ServiceReadyCallback, OnMenuItemClickListe ner { | 34 DecoderServiceHost.ServiceReadyCallback, OnMenuItemClickListe ner { |
| 35 private static final int KILOBYTE = 1024; | |
| 36 | |
| 33 // The dialog that owns us. | 37 // The dialog that owns us. |
| 34 private PhotoPickerDialog mDialog; | 38 private PhotoPickerDialog mDialog; |
| 35 | 39 |
| 36 // The view containing the RecyclerView and the toolbar, etc. | 40 // The view containing the RecyclerView and the toolbar, etc. |
| 37 private SelectableListLayout<PickerBitmap> mSelectableListLayout; | 41 private SelectableListLayout<PickerBitmap> mSelectableListLayout; |
| 38 | 42 |
| 39 // Our context. | 43 // Our context. |
| 40 private Context mContext; | 44 private Context mContext; |
| 41 | 45 |
| 42 // The list of images on disk, sorted by last-modified first. | 46 // The list of images on disk, sorted by last-modified first. |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 53 | 57 |
| 54 // The RecyclerView showing the images. | 58 // The RecyclerView showing the images. |
| 55 private RecyclerView mRecyclerView; | 59 private RecyclerView mRecyclerView; |
| 56 | 60 |
| 57 // The {@link PickerAdapter} for the RecyclerView. | 61 // The {@link PickerAdapter} for the RecyclerView. |
| 58 private PickerAdapter mPickerAdapter; | 62 private PickerAdapter mPickerAdapter; |
| 59 | 63 |
| 60 // The {@link SelectionDelegate} keeping track of which images are selected. | 64 // The {@link SelectionDelegate} keeping track of which images are selected. |
| 61 private SelectionDelegate<PickerBitmap> mSelectionDelegate; | 65 private SelectionDelegate<PickerBitmap> mSelectionDelegate; |
| 62 | 66 |
| 67 // A low-resolution cache for images. Helpful for cache misses from the high -resolution cache | |
| 68 // to avoid showing gray squares (we show pixelated versions instead until i mage can be loaded | |
| 69 // off disk, which is much less jarring). | |
| 70 private LruCache<String, Bitmap> mLowResBitmaps; | |
| 71 | |
| 72 // A high-resolution cache for images. | |
| 73 private LruCache<String, Bitmap> mHighResBitmaps; | |
| 74 | |
| 63 /** | 75 /** |
| 64 * The number of columns to show. Note: mColumns and mPadding (see below) sh ould both be even | 76 * The number of columns to show. Note: mColumns and mPadding (see below) sh ould both be even |
| 65 * numbers or both odd, not a mix (the column padding will not be of uniform thickness if they | 77 * numbers or both odd, not a mix (the column padding will not be of uniform thickness if they |
| 66 * are a mix). | 78 * are a mix). |
| 67 */ | 79 */ |
| 68 private int mColumns; | 80 private int mColumns; |
| 69 | 81 |
| 70 // The padding between columns. See also comment for mColumns. | 82 // The padding between columns. See also comment for mColumns. |
| 71 private int mPadding; | 83 private int mPadding; |
| 72 | 84 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 111 | 123 |
| 112 Rect appRect = new Rect(); | 124 Rect appRect = new Rect(); |
| 113 ((Activity) context).getWindow().getDecorView().getWindowVisibleDisplayF rame(appRect); | 125 ((Activity) context).getWindow().getDecorView().getWindowVisibleDisplayF rame(appRect); |
| 114 calculateGridMetrics(appRect.width()); | 126 calculateGridMetrics(appRect.width()); |
| 115 | 127 |
| 116 RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(mConte xt, mColumns); | 128 RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(mConte xt, mColumns); |
| 117 mRecyclerView.setHasFixedSize(true); | 129 mRecyclerView.setHasFixedSize(true); |
| 118 mRecyclerView.setLayoutManager(mLayoutManager); | 130 mRecyclerView.setLayoutManager(mLayoutManager); |
| 119 mRecyclerView.addItemDecoration(new GridSpacingItemDecoration(mColumns, mPadding)); | 131 mRecyclerView.addItemDecoration(new GridSpacingItemDecoration(mColumns, mPadding)); |
| 120 | 132 |
| 121 // TODO(finnur): Implement caching. | 133 final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / KILOBYTE ); |
| 134 final int cacheSizeLarge = maxMemory / 2; // 1/2 of the available memory . | |
| 135 final int cacheSizeSmall = maxMemory / 8; // 1/8th of the available memo ry. | |
|
Theresa
2017/05/04 16:53:13
How many bitmaps can the small cache hold vs the l
Finnur
2017/05/05 20:41:45
On a Nexus 6 run on my test phone, this resulted i
Theresa
2017/05/08 19:34:42
Thank you for the detailed information!
| |
| 136 mLowResBitmaps = new LruCache<String, Bitmap>(cacheSizeSmall) { | |
| 137 @Override | |
| 138 protected int sizeOf(String key, Bitmap bitmap) { | |
|
Theresa
2017/05/04 16:53:13
What does this override do?
Finnur
2017/05/05 20:41:45
It presents the size of the cache in KB, since tha
Theresa
2017/05/08 19:34:42
I phrased my question poorly -- what is using this
Finnur
2017/05/09 11:53:56
It is for debugging. Removed.
| |
| 139 return bitmap.getByteCount() / KILOBYTE; | |
| 140 } | |
| 141 }; | |
| 142 mHighResBitmaps = new LruCache<String, Bitmap>(cacheSizeLarge) { | |
| 143 @Override | |
| 144 protected int sizeOf(String key, Bitmap bitmap) { | |
| 145 return bitmap.getByteCount() / KILOBYTE; | |
| 146 } | |
| 147 }; | |
| 122 } | 148 } |
| 123 | 149 |
| 124 /** | 150 /** |
| 125 * Severs the connection to the decoding utility process and cancels any out standing requests. | 151 * Severs the connection to the decoding utility process and cancels any out standing requests. |
| 126 */ | 152 */ |
| 127 public void onDialogDismissed() { | 153 public void onDialogDismissed() { |
| 128 if (mWorkerTask != null) { | 154 if (mWorkerTask != null) { |
| 129 mWorkerTask.cancel(true); | 155 mWorkerTask.cancel(true); |
| 130 mWorkerTask = null; | 156 mWorkerTask = null; |
| 131 } | 157 } |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 215 } | 241 } |
| 216 | 242 |
| 217 public List<PickerBitmap> getPickerBitmaps() { | 243 public List<PickerBitmap> getPickerBitmaps() { |
| 218 return mPickerBitmaps; | 244 return mPickerBitmaps; |
| 219 } | 245 } |
| 220 | 246 |
| 221 public DecoderServiceHost getDecoderServiceHost() { | 247 public DecoderServiceHost getDecoderServiceHost() { |
| 222 return mDecoderServiceHost; | 248 return mDecoderServiceHost; |
| 223 } | 249 } |
| 224 | 250 |
| 251 public LruCache<String, Bitmap> getLowResBitmaps() { | |
| 252 return mLowResBitmaps; | |
| 253 } | |
| 254 | |
| 255 public LruCache<String, Bitmap> getHighResBitmaps() { | |
| 256 return mHighResBitmaps; | |
| 257 } | |
| 258 | |
| 225 public boolean isMultiSelectAllowed() { | 259 public boolean isMultiSelectAllowed() { |
| 226 return mMultiSelectionAllowed; | 260 return mMultiSelectionAllowed; |
| 227 } | 261 } |
| 228 | 262 |
| 229 /** | 263 /** |
| 230 * Notifies the listener that the user selected to launch the gallery. | 264 * Notifies the listener that the user selected to launch the gallery. |
| 231 */ | 265 */ |
| 232 public void showGallery() { | 266 public void showGallery() { |
| 233 mListener.onPickerUserAction(PhotoPickerListener.Action.LAUNCH_GALLERY, null); | 267 mListener.onPickerUserAction(PhotoPickerListener.Action.LAUNCH_GALLERY, null); |
| 234 } | 268 } |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 314 if (position < mSpanCount) { | 348 if (position < mSpanCount) { |
| 315 top = mSpacing; | 349 top = mSpacing; |
| 316 } | 350 } |
| 317 bottom = mSpacing; | 351 bottom = mSpacing; |
| 318 } | 352 } |
| 319 | 353 |
| 320 outRect.set(left, top, right, bottom); | 354 outRect.set(left, top, right, bottom); |
| 321 } | 355 } |
| 322 } | 356 } |
| 323 } | 357 } |
| OLD | NEW |