blob: 0a4dff1636abcba0b864177bc73f38b8d6538286 [file] [log] [blame]
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.documentsui;
18
Steve McKay98f8c5f2017-03-03 13:52:14 -080019import static com.android.documentsui.base.Shared.VERBOSE;
20
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070021import android.content.AsyncTaskLoader;
22import android.content.ContentProviderClient;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070023import android.content.ContentResolver;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070024import android.content.Context;
Steve McKay98f8c5f2017-03-03 13:52:14 -080025import android.content.res.Resources;
Ben Lin9fea3122016-10-10 18:32:26 -070026import android.database.ContentObserver;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070027import android.database.Cursor;
28import android.net.Uri;
Steve McKay50b9bae2017-01-17 11:12:08 -080029import android.os.Bundle;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070030import android.os.CancellationSignal;
Ben Lin9fea3122016-10-10 18:32:26 -070031import android.os.Handler;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070032import android.os.OperationCanceledException;
Makoto Onuki77778752015-07-01 14:55:14 -070033import android.os.RemoteException;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070034import android.provider.DocumentsContract.Document;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070035import android.util.Log;
36
Tomasz Mikolajewskib19061c2017-02-13 17:33:42 +090037import com.android.documentsui.archives.ArchivesProvider;
Steve McKay50b9bae2017-01-17 11:12:08 -080038import com.android.documentsui.base.DebugFlags;
Steve McKayd0805062016-09-15 14:30:38 -070039import com.android.documentsui.base.DocumentInfo;
Steve McKayd9caa6a2016-09-15 16:36:45 -070040import com.android.documentsui.base.FilteringCursorWrapper;
Steve McKayd0805062016-09-15 14:30:38 -070041import com.android.documentsui.base.RootInfo;
Steve McKayd9caa6a2016-09-15 16:36:45 -070042import com.android.documentsui.roots.RootCursorWrapper;
Garfield, Tan11d23482016-08-05 09:33:29 -070043import com.android.documentsui.sorting.SortModel;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070044
Steve McKayc88f83c2016-08-31 12:01:43 -070045import libcore.io.IoUtils;
46
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070047public class DirectoryLoader extends AsyncTaskLoader<DirectoryResult> {
Jeff Sharkey9dd02622013-09-27 16:44:11 -070048
Garfield, Tan11d23482016-08-05 09:33:29 -070049 private static final String TAG = "DirectoryLoader";
50
Jeff Sharkey9dd02622013-09-27 16:44:11 -070051 private static final String[] SEARCH_REJECT_MIMES = new String[] { Document.MIME_TYPE_DIR };
52
Ben Lin9fea3122016-10-10 18:32:26 -070053 private final LockingContentObserver mObserver;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070054 private final RootInfo mRoot;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070055 private final Uri mUri;
Garfield, Tan11d23482016-08-05 09:33:29 -070056 private final SortModel mModel;
Aga Wronskaaf5ace52016-02-17 13:50:42 -080057 private final boolean mSearchMode;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070058
Steve McKay7776aa52016-01-25 19:00:22 -080059 private DocumentInfo mDoc;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070060 private CancellationSignal mSignal;
61 private DirectoryResult mResult;
62
Steve McKayc88f83c2016-08-31 12:01:43 -070063 public DirectoryLoader(
64 Context context,
65 RootInfo root,
66 DocumentInfo doc,
67 Uri uri,
68 SortModel model,
Ben Lin9fea3122016-10-10 18:32:26 -070069 DirectoryReloadLock lock,
Steve McKayc88f83c2016-08-31 12:01:43 -070070 boolean inSearchMode) {
71
Jeff Sharkeyf63b7772013-10-01 17:57:41 -070072 super(context, ProviderExecutor.forAuthority(root.authority));
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070073 mRoot = root;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070074 mUri = uri;
Garfield, Tan11d23482016-08-05 09:33:29 -070075 mModel = model;
Steve McKay7776aa52016-01-25 19:00:22 -080076 mDoc = doc;
Aga Wronskaaf5ace52016-02-17 13:50:42 -080077 mSearchMode = inSearchMode;
Ben Lin9fea3122016-10-10 18:32:26 -070078 mObserver = new LockingContentObserver(lock, this::onContentChanged);
Jeff Sharkeya5defe32013-08-05 17:56:48 -070079 }
80
81 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070082 public final DirectoryResult loadInBackground() {
83 synchronized (this) {
84 if (isLoadInBackgroundCanceled()) {
85 throw new OperationCanceledException();
86 }
87 mSignal = new CancellationSignal();
88 }
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070089
90 final ContentResolver resolver = getContext().getContentResolver();
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -070091 final String authority = mUri.getAuthority();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070092
93 final DirectoryResult result = new DirectoryResult();
Tomasz Mikolajewskie29e3412016-02-24 12:53:44 +090094 result.doc = mDoc;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070095
Jeff Sharkeyf63b7772013-10-01 17:57:41 -070096 ContentProviderClient client = null;
Garfield, Tan11d23482016-08-05 09:33:29 -070097 Cursor cursor;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070098 try {
Jeff Sharkeyf63b7772013-10-01 17:57:41 -070099 client = DocumentsApplication.acquireUnstableProviderOrThrow(resolver, authority);
Tomasz Mikolajewskib19061c2017-02-13 17:33:42 +0900100 if (mDoc.isInArchive()) {
101 ArchivesProvider.acquireArchive(client, mUri);
102 }
103 result.client = client;
Steve McKay50b9bae2017-01-17 11:12:08 -0800104
Steve McKay98f8c5f2017-03-03 13:52:14 -0800105 Resources resources = getContext().getResources();
106 if (resources.getBoolean(R.bool.feature_content_paging)) {
Steve McKayf208d842017-02-27 12:57:58 -0800107 Bundle queryArgs = new Bundle();
108 mModel.addQuerySortArgs(queryArgs);
Steve McKay50b9bae2017-01-17 11:12:08 -0800109
Steve McKayf208d842017-02-27 12:57:58 -0800110 // TODO: At some point we don't want forced flags to override real paging...
111 // and that point is when we have real paging.
112 DebugFlags.addForcedPagingArgs(queryArgs);
113
114 cursor = client.query(mUri, null, queryArgs, mSignal);
115 } else {
116 cursor = client.query(
117 mUri, null, null, null, mModel.getDocumentSortQuery(), mSignal);
Makoto Onuki77778752015-07-01 14:55:14 -0700118 }
119
Steve McKayf208d842017-02-27 12:57:58 -0800120 if (cursor == null) {
121 throw new RemoteException("Provider returned null");
Steve McKay50b9bae2017-01-17 11:12:08 -0800122 }
123
Jeff Sharkey20b32272013-09-03 15:25:52 -0700124 cursor.registerContentObserver(mObserver);
125
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700126 cursor = new RootCursorWrapper(mUri.getAuthority(), mRoot.rootId, cursor, -1);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700127
Steve McKay98f8c5f2017-03-03 13:52:14 -0800128 if (mSearchMode && !resources.getBoolean(R.bool.feature_folders_in_search_results)) {
Garfield Tanb00bbc52016-11-01 14:23:35 -0700129 // There is no findDocumentPath API. Enable filtering on folders in search mode.
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700130 cursor = new FilteringCursorWrapper(cursor, null, SEARCH_REJECT_MIMES);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700131 }
132
Steve McKay98f8c5f2017-03-03 13:52:14 -0800133 // TODO: When API tweaks have landed, use ContentResolver.EXTRA_HONORED_ARGS
134 // instead of checking directly for ContentResolver.QUERY_ARG_SORT_COLUMNS (won't work)
135 if (resources.getBoolean(R.bool.feature_content_paging)
136 && cursor.getExtras().containsKey(ContentResolver.QUERY_ARG_SORT_COLUMNS)) {
137 if (VERBOSE) Log.d(TAG, "Skipping sort of pre-sorted cursor. Booya!");
138 } else {
139 cursor = mModel.sortCursor(cursor);
140 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700141 result.cursor = cursor;
Jeff Sharkey1d890e02013-08-15 11:24:03 -0700142 } catch (Exception e) {
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700143 Log.w(TAG, "Failed to query", e);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700144 result.exception = e;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700145 } finally {
146 synchronized (this) {
147 mSignal = null;
148 }
Tomasz Mikolajewskib19061c2017-02-13 17:33:42 +0900149 // TODO: Remove this call.
Ben Linf8f06e92017-01-27 17:15:48 -0800150 ContentProviderClient.releaseQuietly(client);
Jeff Sharkey1d890e02013-08-15 11:24:03 -0700151 }
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700152
Jeff Sharkey46899c82013-08-18 22:26:48 -0700153 return result;
Jeff Sharkey1d890e02013-08-15 11:24:03 -0700154 }
155
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700156 @Override
157 public void cancelLoadInBackground() {
158 super.cancelLoadInBackground();
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700159
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700160 synchronized (this) {
161 if (mSignal != null) {
162 mSignal.cancel();
Jeff Sharkey46899c82013-08-18 22:26:48 -0700163 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700164 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700165 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700166
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700167 @Override
168 public void deliverResult(DirectoryResult result) {
169 if (isReset()) {
170 IoUtils.closeQuietly(result);
171 return;
172 }
173 DirectoryResult oldResult = mResult;
174 mResult = result;
175
176 if (isStarted()) {
177 super.deliverResult(result);
178 }
179
180 if (oldResult != null && oldResult != result) {
181 IoUtils.closeQuietly(oldResult);
182 }
183 }
184
185 @Override
186 protected void onStartLoading() {
187 if (mResult != null) {
188 deliverResult(mResult);
189 }
190 if (takeContentChanged() || mResult == null) {
191 forceLoad();
192 }
193 }
194
195 @Override
196 protected void onStopLoading() {
197 cancelLoad();
198 }
199
200 @Override
201 public void onCanceled(DirectoryResult result) {
202 IoUtils.closeQuietly(result);
203 }
204
205 @Override
206 protected void onReset() {
207 super.onReset();
208
209 // Ensure the loader is stopped
210 onStopLoading();
211
212 IoUtils.closeQuietly(mResult);
213 mResult = null;
214
215 getContext().getContentResolver().unregisterContentObserver(mObserver);
216 }
Ben Lin9fea3122016-10-10 18:32:26 -0700217
218 private static final class LockingContentObserver extends ContentObserver {
219 private final DirectoryReloadLock mLock;
220 private final Runnable mContentChangedCallback;
221
222 public LockingContentObserver(DirectoryReloadLock lock, Runnable contentChangedCallback) {
223 super(new Handler());
224 mLock = lock;
225 mContentChangedCallback = contentChangedCallback;
226 }
227
228 @Override
229 public boolean deliverSelfNotifications() {
230 return true;
231 }
232
233 @Override
234 public void onChange(boolean selfChange) {
235 mLock.tryUpdate(mContentChangedCallback);
236 }
237 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700238}