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.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; | 13 import android.util.AttributeSet; |
| 14 import android.view.LayoutInflater; | 14 import android.view.LayoutInflater; |
| 15 import android.view.MenuItem; | 15 import android.view.MenuItem; |
| 16 import android.view.View; | 16 import android.view.View; |
| 17 import android.widget.RelativeLayout; | 17 import android.widget.RelativeLayout; |
| 18 | 18 |
| 19 import org.chromium.chrome.R; | 19 import org.chromium.chrome.R; |
| 20 import org.chromium.chrome.browser.widget.selection.SelectableListLayout; | 20 import org.chromium.chrome.browser.widget.selection.SelectableListLayout; |
| 21 import org.chromium.chrome.browser.widget.selection.SelectionDelegate; | 21 import org.chromium.chrome.browser.widget.selection.SelectionDelegate; |
| 22 import org.chromium.ui.PhotoPickerListener; | 22 import org.chromium.ui.PhotoPickerListener; |
| 23 | 23 |
| 24 import java.util.ArrayList; | |
| 25 import java.util.List; | 24 import java.util.List; |
| 26 | 25 |
| 27 /** | 26 /** |
| 28 * A class for keeping track of common data associated with showing photos in | 27 * 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. | 28 * the photo picker, for example the RecyclerView and the bitmap caches. |
| 30 */ | 29 */ |
| 31 public class PickerCategoryView extends RelativeLayout implements OnMenuItemClic kListener { | 30 public class PickerCategoryView extends RelativeLayout |
| 31 implements FileEnumWorkerTask.FilesEnumeratedCallback, OnMenuItemClickLi stener { | |
| 32 // The dialog that owns us. | 32 // The dialog that owns us. |
| 33 private PhotoPickerDialog mDialog; | 33 private PhotoPickerDialog mDialog; |
| 34 | 34 |
| 35 // The view containing the RecyclerView and the toolbar, etc. | 35 // The view containing the RecyclerView and the toolbar, etc. |
| 36 private SelectableListLayout<PickerBitmap> mSelectableListLayout; | 36 private SelectableListLayout<PickerBitmap> mSelectableListLayout; |
| 37 | 37 |
| 38 // Our context. | 38 // Our context. |
| 39 private Context mContext; | 39 private Context mContext; |
| 40 | 40 |
| 41 // The list of images on disk, sorted by last-modified first. | 41 // The list of images on disk, sorted by last-modified first. |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 62 * are a mix). | 62 * are a mix). |
| 63 */ | 63 */ |
| 64 private int mColumns; | 64 private int mColumns; |
| 65 | 65 |
| 66 // The padding between columns. See also comment for mColumns. | 66 // The padding between columns. See also comment for mColumns. |
| 67 private int mPadding; | 67 private int mPadding; |
| 68 | 68 |
| 69 // The size of the bitmaps (equal length for width and height). | 69 // The size of the bitmaps (equal length for width and height). |
| 70 private int mImageSize; | 70 private int mImageSize; |
| 71 | 71 |
| 72 // A worker task for asynchronously enumerating files off the main thread. | |
| 73 private FileEnumWorkerTask mWorkerTask; | |
| 74 | |
| 72 public PickerCategoryView(Context context) { | 75 public PickerCategoryView(Context context) { |
| 73 super(context); | 76 super(context); |
| 74 postConstruction(context); | 77 postConstruction(context); |
| 75 } | 78 } |
| 76 | 79 |
| 77 public PickerCategoryView(Context context, AttributeSet attrs) { | 80 public PickerCategoryView(Context context, AttributeSet attrs) { |
| 78 super(context, attrs); | 81 super(context, attrs); |
| 79 postConstruction(context); | 82 postConstruction(context); |
| 80 } | 83 } |
| 81 | 84 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 126 */ | 129 */ |
| 127 public void initialize( | 130 public void initialize( |
| 128 PhotoPickerDialog dialog, PhotoPickerListener listener, boolean mult iSelectionAllowed) { | 131 PhotoPickerDialog dialog, PhotoPickerListener listener, boolean mult iSelectionAllowed) { |
| 129 // TODO(finnur): Change selection delegate to support single-selection. | 132 // TODO(finnur): Change selection delegate to support single-selection. |
| 130 | 133 |
| 131 mDialog = dialog; | 134 mDialog = dialog; |
| 132 mMultiSelectionAllowed = multiSelectionAllowed; | 135 mMultiSelectionAllowed = multiSelectionAllowed; |
| 133 mListener = listener; | 136 mListener = listener; |
| 134 } | 137 } |
| 135 | 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 | |
| 136 // OnMenuItemClickListener: | 149 // OnMenuItemClickListener: |
| 137 | 150 |
| 138 @Override | 151 @Override |
| 139 public boolean onMenuItemClick(MenuItem item) { | 152 public boolean onMenuItemClick(MenuItem item) { |
| 140 if (item.getItemId() == R.id.close_menu_id) { | 153 if (item.getItemId() == R.id.close_menu_id) { |
| 141 mListener.onPickerUserAction(PhotoPickerListener.Action.CANCEL, null ); | 154 mListener.onPickerUserAction(PhotoPickerListener.Action.CANCEL, null ); |
| 142 mDialog.dismiss(); | 155 mDialog.dismiss(); |
| 143 return true; | 156 return true; |
| 144 } else if (item.getItemId() == R.id.selection_mode_done_menu_id) { | 157 } else if (item.getItemId() == R.id.selection_mode_done_menu_id) { |
| 145 notifyPhotosSelected(); | 158 notifyPhotosSelected(); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 195 // 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. |
| 196 if (((mColumns % 2) == 0) != ((mPadding % 2) == 0)) { | 209 if (((mColumns % 2) == 0) != ((mPadding % 2) == 0)) { |
| 197 mPadding++; | 210 mPadding++; |
| 198 } | 211 } |
| 199 } | 212 } |
| 200 | 213 |
| 201 /** | 214 /** |
| 202 * Prepares bitmaps for loading. | 215 * Prepares bitmaps for loading. |
| 203 */ | 216 */ |
| 204 private void prepareBitmaps() { | 217 private void prepareBitmaps() { |
| 205 // TODO(finnur): Use worker thread to fetch bitmaps instead of hard-codi ng. | 218 if (mWorkerTask != null) { |
| 206 mPickerBitmaps = new ArrayList<>(); | 219 mWorkerTask.cancel(true); |
|
Theresa
2017/04/11 15:44:49
Should we also cancel the work task when the dialo
Finnur
2017/04/11 16:25:23
Not a bad idea. Done.
| |
| 207 mPickerBitmaps.add(0, new PickerBitmap("", 0, PickerBitmap.GALLERY)); | 220 } |
| 208 mPickerBitmaps.add(0, new PickerBitmap("", 0, PickerBitmap.CAMERA)); | 221 |
| 209 mPickerBitmaps.add(new PickerBitmap("foo/bar1.jpg", 1, PickerBitmap.PICT URE)); | 222 mWorkerTask = new FileEnumWorkerTask(this, new ImageFileFilter("image/*" )); |
|
Theresa
2017/04/11 15:44:49
I think the name ImageFileFilter indicates that on
Finnur
2017/04/11 16:25:23
How about MimeTypeFileFilter?
Theresa
2017/04/11 17:01:28
Sounds good to me.
| |
| 210 mPickerBitmaps.add(new PickerBitmap("foo/bar2.jpg", 2, PickerBitmap.PICT URE)); | 223 mWorkerTask.execute(); |
| 211 mPickerBitmaps.add(new PickerBitmap("foo/bar3.jpg", 3, PickerBitmap.PICT URE)); | |
| 212 mPickerBitmaps.add(new PickerBitmap("foo/bar4.jpg", 4, PickerBitmap.PICT URE)); | |
| 213 mPickerAdapter.notifyDataSetChanged(); | |
| 214 } | 224 } |
| 215 | 225 |
| 216 /** | 226 /** |
| 217 * Notifies any listeners that one or more photos have been selected. | 227 * Notifies any listeners that one or more photos have been selected. |
| 218 */ | 228 */ |
| 219 private void notifyPhotosSelected() { | 229 private void notifyPhotosSelected() { |
| 220 List<PickerBitmap> selectedFiles = mSelectionDelegate.getSelectedItems() ; | 230 List<PickerBitmap> selectedFiles = mSelectionDelegate.getSelectedItems() ; |
| 221 String[] photos = new String[selectedFiles.size()]; | 231 String[] photos = new String[selectedFiles.size()]; |
| 222 int i = 0; | 232 int i = 0; |
| 223 for (PickerBitmap bitmap : selectedFiles) { | 233 for (PickerBitmap bitmap : selectedFiles) { |
| 224 photos[i++] = bitmap.getFilePath(); | 234 photos[i++] = bitmap.getFilePath(); |
| 225 } | 235 } |
| 226 | 236 |
| 227 mListener.onPickerUserAction(PhotoPickerListener.Action.PHOTOS_SELECTED, photos); | 237 mListener.onPickerUserAction(PhotoPickerListener.Action.PHOTOS_SELECTED, photos); |
| 228 } | 238 } |
| 229 } | 239 } |
| OLD | NEW |