blob: 4fbb3ffc545e741d0bfaa9a5388dbd87382aa603 [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;
Garfield Tane9670332017-03-06 18:33:23 -080032import android.os.Looper;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070033import android.os.OperationCanceledException;
Makoto Onuki77778752015-07-01 14:55:14 -070034import android.os.RemoteException;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070035import android.provider.DocumentsContract.Document;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070036import android.util.Log;
37
Tomasz Mikolajewskib19061c2017-02-13 17:33:42 +090038import com.android.documentsui.archives.ArchivesProvider;
Steve McKay50b9bae2017-01-17 11:12:08 -080039import com.android.documentsui.base.DebugFlags;
Steve McKayd0805062016-09-15 14:30:38 -070040import com.android.documentsui.base.DocumentInfo;
Steve McKayd9caa6a2016-09-15 16:36:45 -070041import com.android.documentsui.base.FilteringCursorWrapper;
Steve McKayd0805062016-09-15 14:30:38 -070042import com.android.documentsui.base.RootInfo;
Steve McKayd9caa6a2016-09-15 16:36:45 -070043import com.android.documentsui.roots.RootCursorWrapper;
Garfield, Tan11d23482016-08-05 09:33:29 -070044import com.android.documentsui.sorting.SortModel;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070045
Steve McKayc88f83c2016-08-31 12:01:43 -070046import libcore.io.IoUtils;
47
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070048public class DirectoryLoader extends AsyncTaskLoader<DirectoryResult> {
Jeff Sharkey9dd02622013-09-27 16:44:11 -070049
Garfield, Tan11d23482016-08-05 09:33:29 -070050 private static final String TAG = "DirectoryLoader";
51
Jeff Sharkey9dd02622013-09-27 16:44:11 -070052 private static final String[] SEARCH_REJECT_MIMES = new String[] { Document.MIME_TYPE_DIR };
53
Ben Lin9fea3122016-10-10 18:32:26 -070054 private final LockingContentObserver mObserver;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070055 private final RootInfo mRoot;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070056 private final Uri mUri;
Garfield, Tan11d23482016-08-05 09:33:29 -070057 private final SortModel mModel;
Aga Wronskaaf5ace52016-02-17 13:50:42 -080058 private final boolean mSearchMode;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070059
Steve McKay7776aa52016-01-25 19:00:22 -080060 private DocumentInfo mDoc;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070061 private CancellationSignal mSignal;
62 private DirectoryResult mResult;
63
Steve McKayc88f83c2016-08-31 12:01:43 -070064 public DirectoryLoader(
65 Context context,
66 RootInfo root,
67 DocumentInfo doc,
68 Uri uri,
69 SortModel model,
Ben Lin9fea3122016-10-10 18:32:26 -070070 DirectoryReloadLock lock,
Steve McKayc88f83c2016-08-31 12:01:43 -070071 boolean inSearchMode) {
72
Jeff Sharkeyf63b7772013-10-01 17:57:41 -070073 super(context, ProviderExecutor.forAuthority(root.authority));
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070074 mRoot = root;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070075 mUri = uri;
Garfield, Tan11d23482016-08-05 09:33:29 -070076 mModel = model;
Steve McKay7776aa52016-01-25 19:00:22 -080077 mDoc = doc;
Aga Wronskaaf5ace52016-02-17 13:50:42 -080078 mSearchMode = inSearchMode;
Ben Lin9fea3122016-10-10 18:32:26 -070079 mObserver = new LockingContentObserver(lock, this::onContentChanged);
Jeff Sharkeya5defe32013-08-05 17:56:48 -070080 }
81
82 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070083 public final DirectoryResult loadInBackground() {
84 synchronized (this) {
85 if (isLoadInBackgroundCanceled()) {
86 throw new OperationCanceledException();
87 }
88 mSignal = new CancellationSignal();
89 }
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070090
91 final ContentResolver resolver = getContext().getContentResolver();
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -070092 final String authority = mUri.getAuthority();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070093
94 final DirectoryResult result = new DirectoryResult();
Tomasz Mikolajewskie29e3412016-02-24 12:53:44 +090095 result.doc = mDoc;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070096
Jeff Sharkeyf63b7772013-10-01 17:57:41 -070097 ContentProviderClient client = null;
Garfield, Tan11d23482016-08-05 09:33:29 -070098 Cursor cursor;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070099 try {
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700100 client = DocumentsApplication.acquireUnstableProviderOrThrow(resolver, authority);
Tomasz Mikolajewskib19061c2017-02-13 17:33:42 +0900101 if (mDoc.isInArchive()) {
102 ArchivesProvider.acquireArchive(client, mUri);
103 }
104 result.client = client;
Steve McKay50b9bae2017-01-17 11:12:08 -0800105
Steve McKay98f8c5f2017-03-03 13:52:14 -0800106 Resources resources = getContext().getResources();
107 if (resources.getBoolean(R.bool.feature_content_paging)) {
Steve McKayf208d842017-02-27 12:57:58 -0800108 Bundle queryArgs = new Bundle();
109 mModel.addQuerySortArgs(queryArgs);
Steve McKay50b9bae2017-01-17 11:12:08 -0800110
Steve McKayf208d842017-02-27 12:57:58 -0800111 // TODO: At some point we don't want forced flags to override real paging...
112 // and that point is when we have real paging.
113 DebugFlags.addForcedPagingArgs(queryArgs);
114
115 cursor = client.query(mUri, null, queryArgs, mSignal);
116 } else {
117 cursor = client.query(
118 mUri, null, null, null, mModel.getDocumentSortQuery(), mSignal);
Makoto Onuki77778752015-07-01 14:55:14 -0700119 }
120
Steve McKayf208d842017-02-27 12:57:58 -0800121 if (cursor == null) {
122 throw new RemoteException("Provider returned null");
Steve McKay50b9bae2017-01-17 11:12:08 -0800123 }
124
Jeff Sharkey20b32272013-09-03 15:25:52 -0700125 cursor.registerContentObserver(mObserver);
126
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700127 cursor = new RootCursorWrapper(mUri.getAuthority(), mRoot.rootId, cursor, -1);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700128
Steve McKay98f8c5f2017-03-03 13:52:14 -0800129 if (mSearchMode && !resources.getBoolean(R.bool.feature_folders_in_search_results)) {
Garfield Tanb00bbc52016-11-01 14:23:35 -0700130 // There is no findDocumentPath API. Enable filtering on folders in search mode.
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700131 cursor = new FilteringCursorWrapper(cursor, null, SEARCH_REJECT_MIMES);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700132 }
133
Steve McKay98f8c5f2017-03-03 13:52:14 -0800134 // TODO: When API tweaks have landed, use ContentResolver.EXTRA_HONORED_ARGS
135 // instead of checking directly for ContentResolver.QUERY_ARG_SORT_COLUMNS (won't work)
136 if (resources.getBoolean(R.bool.feature_content_paging)
137 && cursor.getExtras().containsKey(ContentResolver.QUERY_ARG_SORT_COLUMNS)) {
138 if (VERBOSE) Log.d(TAG, "Skipping sort of pre-sorted cursor. Booya!");
139 } else {
140 cursor = mModel.sortCursor(cursor);
141 }
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700142 result.cursor = cursor;
Jeff Sharkey1d890e02013-08-15 11:24:03 -0700143 } catch (Exception e) {
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700144 Log.w(TAG, "Failed to query", e);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700145 result.exception = e;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700146 } finally {
147 synchronized (this) {
148 mSignal = null;
149 }
Tomasz Mikolajewskib19061c2017-02-13 17:33:42 +0900150 // TODO: Remove this call.
Ben Linf8f06e92017-01-27 17:15:48 -0800151 ContentProviderClient.releaseQuietly(client);
Jeff Sharkey1d890e02013-08-15 11:24:03 -0700152 }
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700153
Jeff Sharkey46899c82013-08-18 22:26:48 -0700154 return result;
Jeff Sharkey1d890e02013-08-15 11:24:03 -0700155 }
156
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700157 @Override
158 public void cancelLoadInBackground() {
159 super.cancelLoadInBackground();
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700160
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700161 synchronized (this) {
162 if (mSignal != null) {
163 mSignal.cancel();
Jeff Sharkey46899c82013-08-18 22:26:48 -0700164 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700165 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700166 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700167
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700168 @Override
169 public void deliverResult(DirectoryResult result) {
170 if (isReset()) {
171 IoUtils.closeQuietly(result);
172 return;
173 }
174 DirectoryResult oldResult = mResult;
175 mResult = result;
176
177 if (isStarted()) {
178 super.deliverResult(result);
179 }
180
181 if (oldResult != null && oldResult != result) {
182 IoUtils.closeQuietly(oldResult);
183 }
184 }
185
186 @Override
187 protected void onStartLoading() {
188 if (mResult != null) {
189 deliverResult(mResult);
190 }
191 if (takeContentChanged() || mResult == null) {
192 forceLoad();
193 }
194 }
195
196 @Override
197 protected void onStopLoading() {
198 cancelLoad();
199 }
200
201 @Override
202 public void onCanceled(DirectoryResult result) {
203 IoUtils.closeQuietly(result);
204 }
205
206 @Override
207 protected void onReset() {
208 super.onReset();
209
210 // Ensure the loader is stopped
211 onStopLoading();
212
213 IoUtils.closeQuietly(mResult);
214 mResult = null;
215
216 getContext().getContentResolver().unregisterContentObserver(mObserver);
217 }
Ben Lin9fea3122016-10-10 18:32:26 -0700218
219 private static final class LockingContentObserver extends ContentObserver {
220 private final DirectoryReloadLock mLock;
221 private final Runnable mContentChangedCallback;
222
223 public LockingContentObserver(DirectoryReloadLock lock, Runnable contentChangedCallback) {
Garfield Tane9670332017-03-06 18:33:23 -0800224 super(new Handler(Looper.getMainLooper()));
Ben Lin9fea3122016-10-10 18:32:26 -0700225 mLock = lock;
226 mContentChangedCallback = contentChangedCallback;
227 }
228
229 @Override
230 public boolean deliverSelfNotifications() {
231 return true;
232 }
233
234 @Override
235 public void onChange(boolean selfChange) {
236 mLock.tryUpdate(mContentChangedCallback);
237 }
238 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700239}