Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(87)

Side by Side Diff: chrome/android/java/src/org/chromium/chrome/browser/photo_picker/PickerCategoryView.java

Issue 2845773003: Photo Picker Dialog: Add caching for the decoded images. (Closed)
Patch Set: Remove dead var Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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.
136 mLowResBitmaps = new LruCache<String, Bitmap>(cacheSizeSmall);
137 mHighResBitmaps = new LruCache<String, Bitmap>(cacheSizeLarge);
122 } 138 }
123 139
124 /** 140 /**
125 * Severs the connection to the decoding utility process and cancels any out standing requests. 141 * Severs the connection to the decoding utility process and cancels any out standing requests.
126 */ 142 */
127 public void onDialogDismissed() { 143 public void onDialogDismissed() {
128 if (mWorkerTask != null) { 144 if (mWorkerTask != null) {
129 mWorkerTask.cancel(true); 145 mWorkerTask.cancel(true);
130 mWorkerTask = null; 146 mWorkerTask = null;
131 } 147 }
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 } 231 }
216 232
217 public List<PickerBitmap> getPickerBitmaps() { 233 public List<PickerBitmap> getPickerBitmaps() {
218 return mPickerBitmaps; 234 return mPickerBitmaps;
219 } 235 }
220 236
221 public DecoderServiceHost getDecoderServiceHost() { 237 public DecoderServiceHost getDecoderServiceHost() {
222 return mDecoderServiceHost; 238 return mDecoderServiceHost;
223 } 239 }
224 240
241 public LruCache<String, Bitmap> getLowResBitmaps() {
242 return mLowResBitmaps;
243 }
244
245 public LruCache<String, Bitmap> getHighResBitmaps() {
246 return mHighResBitmaps;
247 }
248
225 public boolean isMultiSelectAllowed() { 249 public boolean isMultiSelectAllowed() {
226 return mMultiSelectionAllowed; 250 return mMultiSelectionAllowed;
227 } 251 }
228 252
229 /** 253 /**
230 * Notifies the listener that the user selected to launch the gallery. 254 * Notifies the listener that the user selected to launch the gallery.
231 */ 255 */
232 public void showGallery() { 256 public void showGallery() {
233 mListener.onPickerUserAction(PhotoPickerListener.Action.LAUNCH_GALLERY, null); 257 mListener.onPickerUserAction(PhotoPickerListener.Action.LAUNCH_GALLERY, null);
234 } 258 }
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
314 if (position < mSpanCount) { 338 if (position < mSpanCount) {
315 top = mSpacing; 339 top = mSpacing;
316 } 340 }
317 bottom = mSpacing; 341 bottom = mSpacing;
318 } 342 }
319 343
320 outRect.set(left, top, right, bottom); 344 outRect.set(left, top, right, bottom);
321 } 345 }
322 } 346 }
323 } 347 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698