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 AttrAcceptFileFilter 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, AttrAcceptFileFi lter 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); | |
| 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) { | |
| 68 return true; | |
|
Theresa
2017/04/10 18:01:14
nit: inline this
if (files == null) return true;
Finnur
2017/04/11 11:30:57
Done.
| |
| 69 } | |
| 70 | |
| 71 for (File file : files) { | |
| 72 if (isCancelled()) { | |
| 73 return false; | |
| 74 } | |
| 75 | |
| 76 if (file.isDirectory()) { | |
| 77 return traverseDir(file, pickerBitmaps); | |
| 78 } | |
| 79 | |
| 80 pickerBitmaps.add( | |
| 81 new PickerBitmap(file.getPath(), file.lastModified(), Picker Bitmap.PICTURE)); | |
| 82 } | |
| 83 | |
| 84 return true; | |
| 85 } | |
| 86 | |
| 87 /** | |
| 88 * Enumerates (in the background) the image files on disk. Called on a non-U I thread | |
| 89 * @param params Ignored, do not use. | |
| 90 * @return A sorted list of images (by last-modified first). | |
| 91 */ | |
| 92 @Override | |
| 93 protected List<PickerBitmap> doInBackground(Void... params) { | |
| 94 assert !ThreadUtils.runningOnUiThread(); | |
| 95 | |
| 96 if (isCancelled()) return null; | |
| 97 | |
| 98 List<PickerBitmap> pickerBitmaps = new ArrayList<>(); | |
| 99 | |
| 100 File[] sourceDirs = new File[] { | |
| 101 getCameraDirectory(), | |
| 102 Environment.getExternalStoragePublicDirectory(Environment.DIRECT ORY_PICTURES), | |
| 103 Environment.getExternalStoragePublicDirectory(Environment.DIRECT ORY_DOWNLOADS), | |
|
Theresa
2017/04/10 18:01:14
If videos/* is an accepted type, should we also be
Finnur
2017/04/11 11:30:57
videos/* I'll remove, because we don't have good t
Theresa
2017/04/11 15:44:48
I think screenshots are really important e.g. scre
| |
| 104 }; | |
| 105 | |
| 106 for (File directory : sourceDirs) { | |
| 107 if (!traverseDir(directory, pickerBitmaps)) return null; | |
| 108 } | |
| 109 | |
| 110 Collections.sort(pickerBitmaps); | |
| 111 | |
| 112 pickerBitmaps.add(0, new PickerBitmap("", 0, PickerBitmap.GALLERY)); | |
| 113 pickerBitmaps.add(0, new PickerBitmap("", 0, PickerBitmap.CAMERA)); | |
| 114 | |
| 115 return pickerBitmaps; | |
| 116 } | |
| 117 | |
| 118 /** | |
| 119 * Communicates the results back to the client. Called on the UI thread. | |
| 120 * @param files The resulting list of files on disk. | |
| 121 */ | |
| 122 @Override | |
| 123 protected void onPostExecute(List<PickerBitmap> files) { | |
| 124 if (isCancelled()) { | |
| 125 return; | |
| 126 } | |
| 127 | |
| 128 mCallback.filesEnumeratedCallback(files); | |
| 129 } | |
| 130 } | |
| OLD | NEW |