Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 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 org.chromium.base.ThreadUtils; | |
| 11 | |
| 12 import java.io.File; | |
| 13 import java.util.ArrayList; | |
| 14 import java.util.Collections; | |
| 15 import java.util.List; | |
| 16 | |
| 17 /** | |
| 18 * A worker task to enumerate image files on disk. | |
| 19 */ | |
| 20 class FileEnumWorkerTask extends AsyncTask<Void, Void, List<PickerBitmap>> { | |
| 21 /** | |
| 22 * An interface to use to communicate back the results to the client. | |
| 23 */ | |
| 24 public interface FilesEnumeratedCallback { | |
| 25 /** | |
| 26 * A callback to define to receive the list of all images on disk. | |
| 27 * @param files The list of images. | |
| 28 */ | |
| 29 void filesEnumeratedCallback(List<PickerBitmap> files); | |
| 30 } | |
| 31 | |
| 32 // The callback to use to communicate the results. | |
| 33 private FilesEnumeratedCallback mCallback; | |
| 34 | |
| 35 // The filter to apply to the list. | |
| 36 private ImageFileFilter mFilter; | |
| 37 | |
| 38 // The camera directory undir DCIM. | |
| 39 private static final String SAMPLE_DCIM_SOURCE_SUB_DIRECTORY = "Camera"; | |
| 40 | |
| 41 /** | |
| 42 * A FileEnumWorkerTask constructor. | |
| 43 * @param callback The callback to use to communicate back the results. | |
| 44 * @param filter The file filter to apply to the list. | |
| 45 */ | |
| 46 public FileEnumWorkerTask(FilesEnumeratedCallback callback, ImageFileFilter filter) { | |
| 47 mCallback = callback; | |
| 48 mFilter = filter; | |
| 49 } | |
| 50 | |
| 51 /** | |
| 52 * Retrieves the DCIM/camera directory. | |
| 53 */ | |
| 54 private File getCameraDirectory() { | |
| 55 return new File(Environment.getExternalStoragePublicDirectory(Environmen t.DIRECTORY_DCIM), | |
| 56 SAMPLE_DCIM_SOURCE_SUB_DIRECTORY); | |
|
Theresa
2017/04/11 15:44:49
Instead of explicitly finding "Camera", can we sea
Finnur
2017/04/11 16:25:23
I actually modeled this after what Google Photos d
Theresa
2017/04/11 17:01:28
Acknowledged.
| |
| 57 } | |
| 58 | |
| 59 /** | |
| 60 * Recursively enumerate files in a directory (and subdirectories) and add t hem to a list. | |
| 61 * @param directory The parent directory to recursively traverse. | |
| 62 * @param pickerBitmaps The list to add the results to. | |
| 63 * @return True if traversing can continue, false if traversing was aborted and should stop. | |
| 64 */ | |
| 65 private boolean traverseDir(File directory, List<PickerBitmap> pickerBitmaps ) { | |
| 66 File[] files = directory.listFiles(mFilter); | |
| 67 if (files == null) return true; | |
| 68 | |
| 69 for (File file : files) { | |
| 70 if (isCancelled()) return false; | |
| 71 | |
| 72 if (file.isDirectory()) { | |
| 73 if (!traverseDir(file, pickerBitmaps)) return false; | |
|
Theresa
2017/04/11 15:44:49
optional nit: I liked the old
return traverseDir(
Finnur
2017/04/11 16:25:23
I see you fell into the same mind trap I did. The
Theresa
2017/04/11 17:01:28
Ah, I see. Thank you for the explanation.
| |
| 74 } else { | |
| 75 pickerBitmaps.add(new PickerBitmap( | |
| 76 file.getPath(), file.lastModified(), PickerBitmap.PICTUR E)); | |
| 77 } | |
| 78 } | |
| 79 | |
| 80 return true; | |
| 81 } | |
| 82 | |
| 83 /** | |
| 84 * Enumerates (in the background) the image files on disk. Called on a non-U I thread | |
| 85 * @param params Ignored, do not use. | |
| 86 * @return A sorted list of images (by last-modified first). | |
| 87 */ | |
| 88 @Override | |
| 89 protected List<PickerBitmap> doInBackground(Void... params) { | |
| 90 assert !ThreadUtils.runningOnUiThread(); | |
| 91 | |
| 92 if (isCancelled()) return null; | |
| 93 | |
| 94 List<PickerBitmap> pickerBitmaps = new ArrayList<>(); | |
| 95 | |
| 96 File[] sourceDirs = new File[] { | |
| 97 getCameraDirectory(), | |
| 98 Environment.getExternalStoragePublicDirectory(Environment.DIRECT ORY_PICTURES), | |
| 99 Environment.getExternalStoragePublicDirectory(Environment.DIRECT ORY_DOWNLOADS), | |
| 100 }; | |
| 101 | |
| 102 for (File directory : sourceDirs) { | |
| 103 if (!traverseDir(directory, pickerBitmaps)) return null; | |
| 104 } | |
| 105 | |
| 106 Collections.sort(pickerBitmaps); | |
| 107 | |
| 108 pickerBitmaps.add(0, new PickerBitmap("", 0, PickerBitmap.GALLERY)); | |
| 109 pickerBitmaps.add(0, new PickerBitmap("", 0, PickerBitmap.CAMERA)); | |
| 110 | |
| 111 return pickerBitmaps; | |
| 112 } | |
| 113 | |
| 114 /** | |
| 115 * Communicates the results back to the client. Called on the UI thread. | |
| 116 * @param files The resulting list of files on disk. | |
| 117 */ | |
| 118 @Override | |
| 119 protected void onPostExecute(List<PickerBitmap> files) { | |
| 120 if (isCancelled()) { | |
| 121 return; | |
| 122 } | |
| 123 | |
| 124 mCallback.filesEnumeratedCallback(files); | |
| 125 } | |
| 126 } | |
| OLD | NEW |