1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
|
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.documentsui;
import static com.android.documentsui.base.SharedMinimal.DEBUG;
import android.app.ActivityManager;
import android.content.ContentProviderClient;
import android.content.Context;
import android.database.Cursor;
import android.database.CursorWrapper;
import android.database.MatrixCursor;
import android.database.MergeCursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.FileUtils;
import android.provider.DocumentsContract;
import android.provider.DocumentsContract.Document;
import android.util.Log;
import androidx.annotation.GuardedBy;
import androidx.annotation.NonNull;
import androidx.loader.content.AsyncTaskLoader;
import com.android.documentsui.base.DocumentInfo;
import com.android.documentsui.base.FilteringCursorWrapper;
import com.android.documentsui.base.Lookup;
import com.android.documentsui.base.RootInfo;
import com.android.documentsui.base.State;
import com.android.documentsui.roots.ProvidersAccess;
import com.android.documentsui.roots.RootCursorWrapper;
import com.google.common.util.concurrent.AbstractFuture;
import java.io.Closeable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
/**
* The abstract class to query multiple roots from {@link android.provider.DocumentsProvider}
* and return the combined result.
*/
public abstract class MultiRootDocumentsLoader extends AsyncTaskLoader<DirectoryResult> {
private static final String TAG = "MultiRootDocsLoader";
// TODO: clean up cursor ownership so background thread doesn't traverse
// previously returned cursors for filtering/sorting; this currently races
// with the UI thread.
public static final int MAX_OUTSTANDING_TASK = 4;
private static final int MAX_OUTSTANDING_TASK_SVELTE = 2;
/**
* Time to wait for first pass to complete before returning partial results.
*/
private static final int MAX_FIRST_PASS_WAIT_MILLIS = 500;
protected final State mState;
private final Semaphore mQueryPermits;
private final ProvidersAccess mProviders;
private final Lookup<String, Executor> mExecutors;
private final Lookup<String, String> mFileTypeMap;
private LockingContentObserver mObserver;
@GuardedBy("mTasks")
/* A authority -> QueryTask map */
private final Map<String, QueryTask> mTasks = new HashMap<>();
private CountDownLatch mFirstPassLatch;
private volatile boolean mFirstPassDone;
private DirectoryResult mResult;
/**
* Create the loader to query roots from {@link android.provider.DocumentsProvider}.
*
* @param context the context
* @param providers the providers
* @param state current state
* @param executors the executors of authorities
* @param fileTypeMap the map of mime types and file types.
*/
public MultiRootDocumentsLoader(Context context, ProvidersAccess providers, State state,
Lookup<String, Executor> executors, Lookup<String, String> fileTypeMap) {
super(context);
mProviders = providers;
mState = state;
mExecutors = executors;
mFileTypeMap = fileTypeMap;
// Keep clients around on high-RAM devices, since we'd be spinning them
// up moments later to fetch thumbnails anyway.
final ActivityManager am = (ActivityManager) getContext().getSystemService(
Context.ACTIVITY_SERVICE);
mQueryPermits = new Semaphore(
am.isLowRamDevice() ? MAX_OUTSTANDING_TASK_SVELTE : MAX_OUTSTANDING_TASK);
}
@Override
public DirectoryResult loadInBackground() {
try {
synchronized (mTasks) {
return loadInBackgroundLocked();
}
} catch (InterruptedException e) {
Log.w(TAG, "loadInBackground is interrupted: ", e);
return null;
}
}
public void setObserver(LockingContentObserver observer) {
mObserver = observer;
}
private DirectoryResult loadInBackgroundLocked() throws InterruptedException {
if (mFirstPassLatch == null) {
// First time through we kick off all the recent tasks, and wait
// around to see if everyone finishes quickly.
Map<String, List<RootInfo>> rootsIndex = indexRoots();
for (Map.Entry<String, List<RootInfo>> rootEntry : rootsIndex.entrySet()) {
mTasks.put(rootEntry.getKey(),
getQueryTask(rootEntry.getKey(), rootEntry.getValue()));
}
if (isLoadInBackgroundCanceled()) {
// Loader is cancelled (e.g. about to be reset), preempt loading.
throw new InterruptedException("Loading is cancelled!");
}
mFirstPassLatch = new CountDownLatch(mTasks.size());
for (QueryTask task : mTasks.values()) {
mExecutors.lookup(task.authority).execute(task);
}
try {
mFirstPassLatch.await(MAX_FIRST_PASS_WAIT_MILLIS, TimeUnit.MILLISECONDS);
mFirstPassDone = true;
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
final long rejectBefore = getRejectBeforeTime();
// Collect all finished tasks
boolean allDone = true;
int totalQuerySize = 0;
List<Cursor> cursors = new ArrayList<>(mTasks.size());
for (QueryTask task : mTasks.values()) {
if (isLoadInBackgroundCanceled()) {
// Loader is cancelled (e.g. about to be reset), preempt loading.
throw new InterruptedException("Loading is cancelled!");
}
if (task.isDone()) {
try {
final Cursor[] taskCursors = task.get();
if (taskCursors == null || taskCursors.length == 0) {
continue;
}
totalQuerySize += taskCursors.length;
for (Cursor cursor : taskCursors) {
if (cursor == null) {
// It's possible given an authority, some roots fail to return a cursor
// after a query.
continue;
}
final FilteringCursorWrapper filteredCursor =
new FilteringCursorWrapper(cursor) {
@Override
public void close() {
// Ignored, since we manage cursor lifecycle internally
}
};
filteredCursor.filterHiddenFiles(mState.showHiddenFiles);
filteredCursor.filterMimes(mState.acceptMimes, getRejectMimes());
filteredCursor.filterLastModified(rejectBefore);
cursors.add(filteredCursor);
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (ExecutionException e) {
// We already logged on other side
} catch (Exception e) {
// Catch exceptions thrown when we read the cursor.
Log.e(TAG, "Failed to query documents for authority: " + task.authority
+ ". Skip this authority.", e);
}
} else {
allDone = false;
}
}
if (DEBUG) {
Log.d(TAG,
"Found " + cursors.size() + " of " + totalQuerySize + " queries done");
}
final DirectoryResult result = new DirectoryResult();
result.doc = new DocumentInfo();
final Cursor merged;
if (cursors.size() > 0) {
merged = new MergeCursor(cursors.toArray(new Cursor[cursors.size()]));
} else {
// Return something when nobody is ready
merged = new MatrixCursor(new String[0]);
}
final Cursor sorted;
if (isDocumentsMovable()) {
sorted = mState.sortModel.sortCursor(merged, mFileTypeMap);
} else {
final Cursor notMovableMasked = new NotMovableMaskCursor(merged);
sorted = mState.sortModel.sortCursor(notMovableMasked, mFileTypeMap);
}
// Tell the UI if this is an in-progress result. When loading is complete, another update is
// sent with EXTRA_LOADING set to false.
Bundle extras = new Bundle();
extras.putBoolean(DocumentsContract.EXTRA_LOADING, !allDone);
sorted.setExtras(extras);
result.setCursor(sorted);
return result;
}
/**
* Returns a map of Authority -> rootInfos.
*/
private Map<String, List<RootInfo>> indexRoots() {
final Collection<RootInfo> roots = mProviders.getMatchingRootsBlocking(mState);
HashMap<String, List<RootInfo>> rootsIndex = new HashMap<>();
for (RootInfo root : roots) {
// ignore the root with authority is null. e.g. Recent
if (root.authority == null || shouldIgnoreRoot(root)
|| !mState.canInteractWith(root.userId)) {
continue;
}
if (!rootsIndex.containsKey(root.authority)) {
rootsIndex.put(root.authority, new ArrayList<>());
}
rootsIndex.get(root.authority).add(root);
}
return rootsIndex;
}
protected long getRejectBeforeTime() {
return -1;
}
protected String[] getRejectMimes() {
return null;
}
protected boolean shouldIgnoreRoot(RootInfo root) {
return false;
}
protected boolean isDocumentsMovable() {
return false;
}
protected abstract QueryTask getQueryTask(String authority, List<RootInfo> rootInfos);
@Override
public void deliverResult(DirectoryResult result) {
if (isReset()) {
FileUtils.closeQuietly(result);
return;
}
DirectoryResult oldResult = mResult;
mResult = result;
if (isStarted() && !isAbandoned() && !isLoadInBackgroundCanceled()) {
super.deliverResult(result);
}
if (oldResult != null && oldResult != result) {
FileUtils.closeQuietly(oldResult);
}
}
@Override
protected void onStartLoading() {
boolean isCursorStale = checkIfCursorStale(mResult);
if (mResult != null && !isCursorStale) {
deliverResult(mResult);
}
if (takeContentChanged() || mResult == null || isCursorStale) {
forceLoad();
}
}
@Override
protected void onStopLoading() {
cancelLoad();
}
@Override
public void onCanceled(DirectoryResult result) {
FileUtils.closeQuietly(result);
}
@Override
protected void onReset() {
super.onReset();
synchronized (mTasks) {
for (QueryTask task : mTasks.values()) {
mExecutors.lookup(task.authority).execute(() -> FileUtils.closeQuietly(task));
}
}
FileUtils.closeQuietly(mResult);
mResult = null;
}
// TODO: create better transfer of ownership around cursor to ensure its
// closed in all edge cases.
private static class NotMovableMaskCursor extends CursorWrapper {
private static final int NOT_MOVABLE_MASK =
~(Document.FLAG_SUPPORTS_DELETE
| Document.FLAG_SUPPORTS_REMOVE
| Document.FLAG_SUPPORTS_MOVE);
private NotMovableMaskCursor(Cursor cursor) {
super(cursor);
}
@Override
public int getInt(int index) {
final int flagIndex = getWrappedCursor().getColumnIndex(Document.COLUMN_FLAGS);
final int value = super.getInt(index);
return (index == flagIndex) ? (value & NOT_MOVABLE_MASK) : value;
}
}
protected abstract class QueryTask extends AbstractFuture<Cursor[]> implements Runnable,
Closeable {
public final String authority;
public final List<RootInfo> rootInfos;
private Cursor[] mCursors;
private boolean mIsClosed = false;
public QueryTask(String authority, List<RootInfo> rootInfos) {
this.authority = authority;
this.rootInfos = rootInfos;
}
@Override
public void run() {
if (isCancelled()) {
return;
}
try {
mQueryPermits.acquire();
} catch (InterruptedException e) {
return;
}
try {
runInternal();
} finally {
mQueryPermits.release();
}
}
protected abstract Uri getQueryUri(RootInfo rootInfo);
protected abstract RootCursorWrapper generateResultCursor(RootInfo rootInfo,
Cursor oriCursor);
protected void addQueryArgs(@NonNull Bundle queryArgs) {
}
private synchronized void runInternal() {
if (mIsClosed) {
return;
}
final int rootInfoCount = rootInfos.size();
final Cursor[] res = new Cursor[rootInfoCount];
mCursors = new Cursor[rootInfoCount];
for (int i = 0; i < rootInfoCount; i++) {
final RootInfo rootInfo = rootInfos.get(i);
try (ContentProviderClient client =
DocumentsApplication.acquireUnstableProviderOrThrow(
rootInfo.userId.getContentResolver(getContext()),
authority)) {
final Uri uri = getQueryUri(rootInfo);
try {
final Bundle queryArgs = new Bundle();
mState.sortModel.addQuerySortArgs(queryArgs);
addQueryArgs(queryArgs);
res[i] = client.query(uri, null, queryArgs, null);
if (mObserver != null) {
res[i].registerContentObserver(mObserver);
}
mCursors[i] = generateResultCursor(rootInfo, res[i]);
} catch (Exception e) {
Log.w(TAG, "Failed to load " + authority + ", " + rootInfo.rootId, e);
}
} catch (Exception e) {
Log.w(TAG, "Failed to acquire content resolver for authority: " + authority);
}
}
set(mCursors);
mFirstPassLatch.countDown();
if (mFirstPassDone) {
onContentChanged();
}
}
@Override
public synchronized void close() throws IOException {
if (mCursors == null) {
return;
}
for (Cursor cursor : mCursors) {
if (mObserver != null && cursor != null) {
cursor.unregisterContentObserver(mObserver);
}
FileUtils.closeQuietly(cursor);
}
mIsClosed = true;
}
}
private boolean checkIfCursorStale(DirectoryResult result) {
if (result == null || result.getCursor() == null || result.getCursor().isClosed()) {
return true;
}
Cursor cursor = result.getCursor();
try {
cursor.moveToPosition(-1);
for (int pos = 0; pos < cursor.getCount(); ++pos) {
if (!cursor.moveToNext()) {
return true;
}
}
} catch (Exception e) {
return true;
}
return false;
}
}
|