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.List; | 25 import java.util.List; |
24 | 26 |
25 /** | 27 /** |
26 * A class for keeping track of common data associated with showing photos in | 28 * A class for keeping track of common data associated with showing photos in |
27 * the photo picker, for example the RecyclerView and the bitmap caches. | 29 * the photo picker, for example the RecyclerView and the bitmap caches. |
28 */ | 30 */ |
29 public class PickerCategoryView extends RelativeLayout | 31 public class PickerCategoryView extends RelativeLayout |
30 implements FileEnumWorkerTask.FilesEnumeratedCallback, OnMenuItemClickLi stener { | 32 implements FileEnumWorkerTask.FilesEnumeratedCallback, OnMenuItemClickLi stener { |
33 private static final int KILOBYTE = 1024; | |
34 | |
31 // The dialog that owns us. | 35 // The dialog that owns us. |
32 private PhotoPickerDialog mDialog; | 36 private PhotoPickerDialog mDialog; |
33 | 37 |
34 // The view containing the RecyclerView and the toolbar, etc. | 38 // The view containing the RecyclerView and the toolbar, etc. |
35 private SelectableListLayout<PickerBitmap> mSelectableListLayout; | 39 private SelectableListLayout<PickerBitmap> mSelectableListLayout; |
36 | 40 |
37 // Our context. | 41 // Our context. |
38 private Context mContext; | 42 private Context mContext; |
39 | 43 |
40 // The list of images on disk, sorted by last-modified first. | 44 // The list of images on disk, sorted by last-modified first. |
41 private List<PickerBitmap> mPickerBitmaps; | 45 private List<PickerBitmap> mPickerBitmaps; |
42 | 46 |
43 // True if multi-selection is allowed in the picker. | 47 // True if multi-selection is allowed in the picker. |
44 private boolean mMultiSelectionAllowed; | 48 private boolean mMultiSelectionAllowed; |
45 | 49 |
46 // The callback to notify the listener of decisions reached in the picker. | 50 // The callback to notify the listener of decisions reached in the picker. |
47 private PhotoPickerListener mListener; | 51 private PhotoPickerListener mListener; |
48 | 52 |
49 // The RecyclerView showing the images. | 53 // The RecyclerView showing the images. |
50 private RecyclerView mRecyclerView; | 54 private RecyclerView mRecyclerView; |
51 | 55 |
52 // The {@link PickerAdapter} for the RecyclerView. | 56 // The {@link PickerAdapter} for the RecyclerView. |
53 private PickerAdapter mPickerAdapter; | 57 private PickerAdapter mPickerAdapter; |
54 | 58 |
55 // The {@link SelectionDelegate} keeping track of which images are selected. | 59 // The {@link SelectionDelegate} keeping track of which images are selected. |
56 private SelectionDelegate<PickerBitmap> mSelectionDelegate; | 60 private SelectionDelegate<PickerBitmap> mSelectionDelegate; |
57 | 61 |
62 // A low-resolution cache for images. Helpful for cache misses from the high -resolution cache | |
63 // to avoid showing gray squares (we show pixelated versions instead until i mage can be loaded | |
64 // off disk, which is much less jarring). | |
65 private LruCache<String, Bitmap> mLowResBitmaps; | |
66 | |
67 // A high-resolution cache for images. | |
68 private LruCache<String, Bitmap> mHighResBitmaps; | |
Michael van Ouwerkerk
2017/04/27 17:07:35
What guarantee do we have that these caches do not
Finnur
2017/05/04 16:05:15
This object is destroyed when the dialog goes away
| |
69 | |
58 /** | 70 /** |
59 * The number of columns to show. Note: mColumns and mPadding (see below) sh ould both be even | 71 * The number of columns to show. Note: mColumns and mPadding (see below) sh ould both be even |
60 * numbers or both odd, not a mix (the column padding will not be of uniform thickness if they | 72 * numbers or both odd, not a mix (the column padding will not be of uniform thickness if they |
61 * are a mix). | 73 * are a mix). |
62 */ | 74 */ |
63 private int mColumns; | 75 private int mColumns; |
64 | 76 |
65 // The padding between columns. See also comment for mColumns. | 77 // The padding between columns. See also comment for mColumns. |
66 private int mPadding; | 78 private int mPadding; |
67 | 79 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
99 | 111 |
100 Rect appRect = new Rect(); | 112 Rect appRect = new Rect(); |
101 ((Activity) context).getWindow().getDecorView().getWindowVisibleDisplayF rame(appRect); | 113 ((Activity) context).getWindow().getDecorView().getWindowVisibleDisplayF rame(appRect); |
102 calculateGridMetrics(appRect.width()); | 114 calculateGridMetrics(appRect.width()); |
103 | 115 |
104 RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(mConte xt, mColumns); | 116 RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(mConte xt, mColumns); |
105 mRecyclerView.setHasFixedSize(true); | 117 mRecyclerView.setHasFixedSize(true); |
106 mRecyclerView.setLayoutManager(mLayoutManager); | 118 mRecyclerView.setLayoutManager(mLayoutManager); |
107 mRecyclerView.addItemDecoration(new GridSpacingItemDecoration(mColumns, mPadding)); | 119 mRecyclerView.addItemDecoration(new GridSpacingItemDecoration(mColumns, mPadding)); |
108 | 120 |
109 // TODO(finnur): Implement caching. | 121 final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / KILOBYTE ); |
122 final int cacheSizeLarge = maxMemory / 2; // 1/2 of the available memory . | |
123 final int cacheSizeSmall = maxMemory / 8; // 1/8th of the available memo ry. | |
124 mLowResBitmaps = new LruCache<String, Bitmap>(cacheSizeSmall) { | |
125 @Override | |
126 protected int sizeOf(String key, Bitmap bitmap) { | |
127 return bitmap.getByteCount() / KILOBYTE; | |
128 } | |
129 }; | |
130 mHighResBitmaps = new LruCache<String, Bitmap>(cacheSizeLarge) { | |
Michael van Ouwerkerk
2017/04/27 17:07:35
Please ping memory-dev@chromium.org https://groups
Finnur
2017/05/04 16:05:15
Will do, thanks.
| |
131 @Override | |
132 protected int sizeOf(String key, Bitmap bitmap) { | |
133 return bitmap.getByteCount() / KILOBYTE; | |
134 } | |
135 }; | |
136 | |
110 // TODO(finnur): Remove this once the decoder service is in place. | 137 // TODO(finnur): Remove this once the decoder service is in place. |
111 prepareBitmaps(); | 138 prepareBitmaps(); |
112 } | 139 } |
113 | 140 |
114 /** | 141 /** |
115 * Cancels any outstanding requests. | 142 * Cancels any outstanding requests. |
116 */ | 143 */ |
117 public void onDialogDismissed() { | 144 public void onDialogDismissed() { |
118 if (mWorkerTask != null) { | 145 if (mWorkerTask != null) { |
119 mWorkerTask.cancel(true); | 146 mWorkerTask.cancel(true); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
169 } | 196 } |
170 | 197 |
171 public SelectionDelegate<PickerBitmap> getSelectionDelegate() { | 198 public SelectionDelegate<PickerBitmap> getSelectionDelegate() { |
172 return mSelectionDelegate; | 199 return mSelectionDelegate; |
173 } | 200 } |
174 | 201 |
175 public List<PickerBitmap> getPickerBitmaps() { | 202 public List<PickerBitmap> getPickerBitmaps() { |
176 return mPickerBitmaps; | 203 return mPickerBitmaps; |
177 } | 204 } |
178 | 205 |
206 public LruCache<String, Bitmap> getLowResBitmaps() { | |
207 return mLowResBitmaps; | |
208 } | |
209 | |
210 public LruCache<String, Bitmap> getHighResBitmaps() { | |
211 return mHighResBitmaps; | |
212 } | |
213 | |
179 public boolean isMultiSelectAllowed() { | 214 public boolean isMultiSelectAllowed() { |
180 return mMultiSelectionAllowed; | 215 return mMultiSelectionAllowed; |
181 } | 216 } |
182 | 217 |
183 /** | 218 /** |
184 * Notifies the listener that the user selected to launch the gallery. | 219 * Notifies the listener that the user selected to launch the gallery. |
185 */ | 220 */ |
186 public void showGallery() { | 221 public void showGallery() { |
187 mListener.onPickerUserAction(PhotoPickerListener.Action.LAUNCH_GALLERY, null); | 222 mListener.onPickerUserAction(PhotoPickerListener.Action.LAUNCH_GALLERY, null); |
188 } | 223 } |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
267 if (position < mSpanCount) { | 302 if (position < mSpanCount) { |
268 top = mSpacing; | 303 top = mSpacing; |
269 } | 304 } |
270 bottom = mSpacing; | 305 bottom = mSpacing; |
271 } | 306 } |
272 | 307 |
273 outRect.set(left, top, right, bottom); | 308 outRect.set(left, top, right, bottom); |
274 } | 309 } |
275 } | 310 } |
276 } | 311 } |
OLD | NEW |