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.Rect; | 9 import android.graphics.Rect; |
10 import android.support.v7.widget.GridLayoutManager; | 10 import android.support.v7.widget.GridLayoutManager; |
11 import android.support.v7.widget.RecyclerView; | 11 import android.support.v7.widget.RecyclerView; |
12 import android.support.v7.widget.Toolbar.OnMenuItemClickListener; | 12 import android.support.v7.widget.Toolbar.OnMenuItemClickListener; |
13 import android.util.AttributeSet; | |
14 import android.view.LayoutInflater; | 13 import android.view.LayoutInflater; |
15 import android.view.MenuItem; | 14 import android.view.MenuItem; |
16 import android.view.View; | 15 import android.view.View; |
17 import android.widget.RelativeLayout; | 16 import android.widget.RelativeLayout; |
18 | 17 |
19 import org.chromium.chrome.R; | 18 import org.chromium.chrome.R; |
20 import org.chromium.chrome.browser.widget.selection.SelectableListLayout; | 19 import org.chromium.chrome.browser.widget.selection.SelectableListLayout; |
21 import org.chromium.chrome.browser.widget.selection.SelectionDelegate; | 20 import org.chromium.chrome.browser.widget.selection.SelectionDelegate; |
22 import org.chromium.ui.PhotoPickerListener; | 21 import org.chromium.ui.PhotoPickerListener; |
23 | 22 |
24 import java.util.ArrayList; | |
25 import java.util.List; | 23 import java.util.List; |
26 | 24 |
27 /** | 25 /** |
28 * A class for keeping track of common data associated with showing photos in | 26 * A class for keeping track of common data associated with showing photos in |
29 * the photo picker, for example the RecyclerView and the bitmap caches. | 27 * the photo picker, for example the RecyclerView and the bitmap caches. |
30 */ | 28 */ |
31 public class PickerCategoryView extends RelativeLayout implements OnMenuItemClic
kListener { | 29 public class PickerCategoryView extends RelativeLayout |
| 30 implements FileEnumWorkerTask.FilesEnumeratedCallback, OnMenuItemClickLi
stener { |
32 // The dialog that owns us. | 31 // The dialog that owns us. |
33 private PhotoPickerDialog mDialog; | 32 private PhotoPickerDialog mDialog; |
34 | 33 |
35 // The view containing the RecyclerView and the toolbar, etc. | 34 // The view containing the RecyclerView and the toolbar, etc. |
36 private SelectableListLayout<PickerBitmap> mSelectableListLayout; | 35 private SelectableListLayout<PickerBitmap> mSelectableListLayout; |
37 | 36 |
38 // Our context. | 37 // Our context. |
39 private Context mContext; | 38 private Context mContext; |
40 | 39 |
41 // The list of images on disk, sorted by last-modified first. | 40 // The list of images on disk, sorted by last-modified first. |
(...skipping 20 matching lines...) Expand all Loading... |
62 * are a mix). | 61 * are a mix). |
63 */ | 62 */ |
64 private int mColumns; | 63 private int mColumns; |
65 | 64 |
66 // The padding between columns. See also comment for mColumns. | 65 // The padding between columns. See also comment for mColumns. |
67 private int mPadding; | 66 private int mPadding; |
68 | 67 |
69 // The size of the bitmaps (equal length for width and height). | 68 // The size of the bitmaps (equal length for width and height). |
70 private int mImageSize; | 69 private int mImageSize; |
71 | 70 |
| 71 // A worker task for asynchronously enumerating files off the main thread. |
| 72 private FileEnumWorkerTask mWorkerTask; |
| 73 |
72 public PickerCategoryView(Context context) { | 74 public PickerCategoryView(Context context) { |
73 super(context); | 75 super(context); |
74 postConstruction(context); | 76 postConstruction(context); |
75 } | 77 } |
76 | 78 |
77 public PickerCategoryView(Context context, AttributeSet attrs) { | |
78 super(context, attrs); | |
79 postConstruction(context); | |
80 } | |
81 | |
82 public PickerCategoryView(Context context, AttributeSet attrs, int defStyle)
{ | |
83 super(context, attrs, defStyle); | |
84 postConstruction(context); | |
85 } | |
86 | |
87 /** | 79 /** |
88 * A helper function for initializing the PickerCategoryView. | 80 * A helper function for initializing the PickerCategoryView. |
89 * @param context The context to use. | 81 * @param context The context to use. |
90 */ | 82 */ |
91 @SuppressWarnings("unchecked") // mSelectableListLayout | 83 @SuppressWarnings("unchecked") // mSelectableListLayout |
92 private void postConstruction(Context context) { | 84 private void postConstruction(Context context) { |
93 mContext = context; | 85 mContext = context; |
94 | 86 |
95 mSelectionDelegate = new SelectionDelegate<PickerBitmap>(); | 87 mSelectionDelegate = new SelectionDelegate<PickerBitmap>(); |
96 | 88 |
(...skipping 16 matching lines...) Expand all Loading... |
113 mRecyclerView.setHasFixedSize(true); | 105 mRecyclerView.setHasFixedSize(true); |
114 mRecyclerView.setLayoutManager(mLayoutManager); | 106 mRecyclerView.setLayoutManager(mLayoutManager); |
115 mRecyclerView.addItemDecoration(new GridSpacingItemDecoration(mColumns,
mPadding)); | 107 mRecyclerView.addItemDecoration(new GridSpacingItemDecoration(mColumns,
mPadding)); |
116 | 108 |
117 // TODO(finnur): Implement caching. | 109 // TODO(finnur): Implement caching. |
118 // TODO(finnur): Remove this once the decoder service is in place. | 110 // TODO(finnur): Remove this once the decoder service is in place. |
119 prepareBitmaps(); | 111 prepareBitmaps(); |
120 } | 112 } |
121 | 113 |
122 /** | 114 /** |
| 115 * Cancels any outstanding requests. |
| 116 */ |
| 117 public void onDialogDismissed() { |
| 118 if (mWorkerTask != null) { |
| 119 mWorkerTask.cancel(true); |
| 120 mWorkerTask = null; |
| 121 } |
| 122 } |
| 123 |
| 124 /** |
123 * Initializes the PickerCategoryView object. | 125 * Initializes the PickerCategoryView object. |
124 * @param dialog The dialog showing us. | 126 * @param dialog The dialog showing us. |
125 * @param listener The listener who should be notified of actions. | 127 * @param listener The listener who should be notified of actions. |
126 * @param multiSelectionAllowed Whether to allow the user to select more tha
n one image. | 128 * @param multiSelectionAllowed Whether to allow the user to select more tha
n one image. |
127 */ | 129 */ |
128 public void initialize( | 130 public void initialize( |
129 PhotoPickerDialog dialog, PhotoPickerListener listener, boolean mult
iSelectionAllowed) { | 131 PhotoPickerDialog dialog, PhotoPickerListener listener, boolean mult
iSelectionAllowed) { |
130 // TODO(finnur): Change selection delegate to support single-selection. | 132 // TODO(finnur): Change selection delegate to support single-selection. |
131 | 133 |
132 mDialog = dialog; | 134 mDialog = dialog; |
133 mMultiSelectionAllowed = multiSelectionAllowed; | 135 mMultiSelectionAllowed = multiSelectionAllowed; |
134 mListener = listener; | 136 mListener = listener; |
135 } | 137 } |
136 | 138 |
| 139 // FileEnumWorkerTask.FilesEnumeratedCallback: |
| 140 |
| 141 @Override |
| 142 public void filesEnumeratedCallback(List<PickerBitmap> files) { |
| 143 mPickerBitmaps = files; |
| 144 if (files != null && files.size() > 0) { |
| 145 mPickerAdapter.notifyDataSetChanged(); |
| 146 } |
| 147 } |
| 148 |
137 // OnMenuItemClickListener: | 149 // OnMenuItemClickListener: |
138 | 150 |
139 @Override | 151 @Override |
140 public boolean onMenuItemClick(MenuItem item) { | 152 public boolean onMenuItemClick(MenuItem item) { |
141 if (item.getItemId() == R.id.close_menu_id) { | 153 if (item.getItemId() == R.id.close_menu_id) { |
142 mListener.onPickerUserAction(PhotoPickerListener.Action.CANCEL, null
); | 154 mListener.onPickerUserAction(PhotoPickerListener.Action.CANCEL, null
); |
143 mDialog.dismiss(); | 155 mDialog.dismiss(); |
144 return true; | 156 return true; |
145 } else if (item.getItemId() == R.id.selection_mode_done_menu_id) { | 157 } else if (item.getItemId() == R.id.selection_mode_done_menu_id) { |
146 notifyPhotosSelected(); | 158 notifyPhotosSelected(); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 // Make sure columns and padding are either both even or both odd. | 208 // Make sure columns and padding are either both even or both odd. |
197 if (((mColumns % 2) == 0) != ((mPadding % 2) == 0)) { | 209 if (((mColumns % 2) == 0) != ((mPadding % 2) == 0)) { |
198 mPadding++; | 210 mPadding++; |
199 } | 211 } |
200 } | 212 } |
201 | 213 |
202 /** | 214 /** |
203 * Prepares bitmaps for loading. | 215 * Prepares bitmaps for loading. |
204 */ | 216 */ |
205 private void prepareBitmaps() { | 217 private void prepareBitmaps() { |
206 // TODO(finnur): Use worker thread to fetch bitmaps instead of hard-codi
ng. | 218 if (mWorkerTask != null) { |
207 mPickerBitmaps = new ArrayList<>(); | 219 mWorkerTask.cancel(true); |
208 mPickerBitmaps.add(0, new PickerBitmap("", 0, PickerBitmap.GALLERY)); | 220 } |
209 mPickerBitmaps.add(0, new PickerBitmap("", 0, PickerBitmap.CAMERA)); | 221 |
210 mPickerBitmaps.add(new PickerBitmap("foo/bar1.jpg", 1, PickerBitmap.PICT
URE)); | 222 mWorkerTask = new FileEnumWorkerTask(this, new MimeTypeFileFilter("image
/*")); |
211 mPickerBitmaps.add(new PickerBitmap("foo/bar2.jpg", 2, PickerBitmap.PICT
URE)); | 223 mWorkerTask.execute(); |
212 mPickerBitmaps.add(new PickerBitmap("foo/bar3.jpg", 3, PickerBitmap.PICT
URE)); | |
213 mPickerBitmaps.add(new PickerBitmap("foo/bar4.jpg", 4, PickerBitmap.PICT
URE)); | |
214 mPickerAdapter.notifyDataSetChanged(); | |
215 } | 224 } |
216 | 225 |
217 /** | 226 /** |
218 * Notifies any listeners that one or more photos have been selected. | 227 * Notifies any listeners that one or more photos have been selected. |
219 */ | 228 */ |
220 private void notifyPhotosSelected() { | 229 private void notifyPhotosSelected() { |
221 List<PickerBitmap> selectedFiles = mSelectionDelegate.getSelectedItems()
; | 230 List<PickerBitmap> selectedFiles = mSelectionDelegate.getSelectedItems()
; |
222 String[] photos = new String[selectedFiles.size()]; | 231 String[] photos = new String[selectedFiles.size()]; |
223 int i = 0; | 232 int i = 0; |
224 for (PickerBitmap bitmap : selectedFiles) { | 233 for (PickerBitmap bitmap : selectedFiles) { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 if (position < mSpanCount) { | 267 if (position < mSpanCount) { |
259 top = mSpacing; | 268 top = mSpacing; |
260 } | 269 } |
261 bottom = mSpacing; | 270 bottom = mSpacing; |
262 } | 271 } |
263 | 272 |
264 outRect.set(left, top, right, bottom); | 273 outRect.set(left, top, right, bottom); |
265 } | 274 } |
266 } | 275 } |
267 } | 276 } |
OLD | NEW |