Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
|
Theresa
2017/03/28 20:40:28
s/2016/2017
Finnur
2017/03/31 14:26:49
Done.
| |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.chrome.browser.photo_picker; | |
| 6 | |
| 7 import android.os.AsyncTask; | |
| 8 import android.os.Environment; | |
| 9 | |
| 10 import java.io.File; | |
| 11 import java.util.ArrayList; | |
| 12 import java.util.Collections; | |
| 13 import java.util.List; | |
| 14 import java.util.Locale; | |
| 15 | |
| 16 /** | |
| 17 * A worker task to enumerate image files on disk. | |
| 18 */ | |
| 19 class FileEnumWorkerTask extends AsyncTask<Void, Void, List<PickerBitmap>> { | |
| 20 /** | |
| 21 * An interface to use to communicate back the results to the client. | |
| 22 */ | |
| 23 public interface FilesEnumeratedCallback { | |
| 24 /** | |
| 25 * A callback to define to receive the list of all images on disk. | |
| 26 * @param files The list of images. | |
| 27 */ | |
| 28 void filesEnumeratedCallback(List<PickerBitmap> files); | |
| 29 } | |
| 30 | |
| 31 // The callback to use to communicate the results. | |
| 32 private FilesEnumeratedCallback mCallback; | |
| 33 | |
| 34 // The filter to apply to the list. | |
| 35 private AttrAcceptFileFilter mFilter; | |
| 36 | |
| 37 /** | |
| 38 * A FileEnumWorkerTask constructor. | |
| 39 * @param callback The callback to use to communicate back the results. | |
| 40 * @param filter The file filter to apply to the list. | |
| 41 */ | |
| 42 public FileEnumWorkerTask(FilesEnumeratedCallback callback, AttrAcceptFileFi lter filter) { | |
| 43 mCallback = callback; | |
| 44 mFilter = filter; | |
| 45 } | |
| 46 | |
| 47 /** | |
| 48 * Enumerates (in the background) the image files on disk. Called on a non-U I thread | |
|
Theresa
2017/03/28 20:40:28
nit: Please add an assert to the method to check t
Finnur
2017/03/31 14:26:49
Done.
| |
| 49 * @param params Ignored, do not use. | |
| 50 * @return A sorted list of images (by last-modified first). | |
| 51 */ | |
| 52 @Override | |
| 53 protected List<PickerBitmap> doInBackground(Void... params) { | |
| 54 if (isCancelled()) { | |
| 55 return null; | |
|
Theresa
2017/03/28 20:40:28
nit: inline above
if (isCancelled()) return null;
Finnur
2017/03/31 14:26:49
Done.
| |
| 56 } | |
| 57 | |
| 58 List<PickerBitmap> pickerBitmaps = new ArrayList<>(); | |
| 59 | |
| 60 String paths[] = new String[3]; | |
| 61 paths[0] = "/DCIM/Camera"; | |
|
Theresa
2017/03/28 20:40:28
How confident are we that the directories will be
Finnur
2017/03/31 14:26:49
Acknowledged. Will change it in a future CL, since
| |
| 62 paths[1] = "/Pictures/Screenshots"; | |
| 63 paths[2] = "/Download"; | |
| 64 | |
| 65 for (int path = 0; path < paths.length; ++path) { | |
| 66 String filePath = Environment.getExternalStorageDirectory().toString () + paths[path]; | |
| 67 File directory = new File(filePath); | |
| 68 File[] files = directory.listFiles(mFilter); | |
| 69 if (files == null) { | |
| 70 continue; | |
| 71 } | |
| 72 | |
| 73 for (File file : files) { | |
| 74 if (isCancelled()) { | |
| 75 return null; | |
| 76 } | |
| 77 | |
| 78 if (isImageExtension(file.getName())) { | |
| 79 pickerBitmaps.add(new PickerBitmap(filePath + "/" + file.get Name(), | |
| 80 file.lastModified(), PickerBitmap.TileTypes.PICTURE) ); | |
| 81 } | |
| 82 } | |
| 83 } | |
| 84 | |
| 85 Collections.sort(pickerBitmaps); | |
| 86 | |
| 87 pickerBitmaps.add(0, new PickerBitmap("", 0, PickerBitmap.TileTypes.GALL ERY)); | |
| 88 pickerBitmaps.add(0, new PickerBitmap("", 0, PickerBitmap.TileTypes.CAME RA)); | |
| 89 | |
| 90 return pickerBitmaps; | |
| 91 } | |
| 92 | |
| 93 /** | |
| 94 * @param filePath The file path to consider. | |
| 95 * @return true if the |filePath| ends in an image extension. | |
| 96 */ | |
| 97 private boolean isImageExtension(String filePath) { | |
| 98 String file = filePath.toLowerCase(Locale.US); | |
| 99 return file.endsWith(".jpg") || file.endsWith(".gif") || file.endsWith(" .png"); | |
|
Theresa
2017/03/28 20:40:28
This list should probably include ".webp" and ".bm
Finnur
2017/03/31 14:26:49
Acknowledged. Will change it in a future CL, since
| |
| 100 } | |
| 101 | |
| 102 /** | |
| 103 * Communicates the results back to the client. Called on the UI thread. | |
| 104 * @param files The resulting list of files on disk. | |
| 105 */ | |
| 106 @Override | |
| 107 protected void onPostExecute(List<PickerBitmap> files) { | |
| 108 if (isCancelled()) { | |
| 109 return; | |
| 110 } | |
| 111 | |
| 112 mCallback.filesEnumeratedCallback(files); | |
| 113 } | |
| 114 } | |
| OLD | NEW |