Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(287)

Side by Side Diff: chrome/android/java/src/org/chromium/chrome/browser/photo_picker/FileEnumWorkerTask.java

Issue 2810773002: Photo Picker Dialog: Recursively traverse the photo directories. (Closed)
Patch Set: Add todo Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | chrome/android/java/src/org/chromium/chrome/browser/photo_picker/MimeTypeFileFilter.java » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 MimeTypeFileFilter 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, MimeTypeFileFilt er 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) 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;
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 // TODO(finnur): Figure out which directories to scan and stop hard codi ng "Camera" above.
97 File[] sourceDirs = new File[] {
98 getCameraDirectory(),
99 Environment.getExternalStoragePublicDirectory(Environment.DIRECT ORY_PICTURES),
100 Environment.getExternalStoragePublicDirectory(Environment.DIRECT ORY_DOWNLOADS),
101 };
102
103 for (File directory : sourceDirs) {
104 if (!traverseDir(directory, pickerBitmaps)) return null;
105 }
106
107 Collections.sort(pickerBitmaps);
108
109 pickerBitmaps.add(0, new PickerBitmap("", 0, PickerBitmap.GALLERY));
110 pickerBitmaps.add(0, new PickerBitmap("", 0, PickerBitmap.CAMERA));
111
112 return pickerBitmaps;
113 }
114
115 /**
116 * Communicates the results back to the client. Called on the UI thread.
117 * @param files The resulting list of files on disk.
118 */
119 @Override
120 protected void onPostExecute(List<PickerBitmap> files) {
121 if (isCancelled()) {
122 return;
123 }
124
125 mCallback.filesEnumeratedCallback(files);
126 }
127 }
OLDNEW
« no previous file with comments | « no previous file | chrome/android/java/src/org/chromium/chrome/browser/photo_picker/MimeTypeFileFilter.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698