blob: 5c93f761ed6e800353d9db62e8570fd5b424e994 [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
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070019import android.content.AsyncTaskLoader;
20import android.content.ContentProviderClient;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070021import android.content.ContentResolver;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070022import android.content.Context;
Ben Lin9fea3122016-10-10 18:32:26 -070023import android.database.ContentObserver;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070024import android.database.Cursor;
25import android.net.Uri;
Steve McKay50b9bae2017-01-17 11:12:08 -080026import android.os.Bundle;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070027import android.os.CancellationSignal;
Ben Lin9fea3122016-10-10 18:32:26 -070028import android.os.Handler;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070029import android.os.OperationCanceledException;
Makoto Onuki77778752015-07-01 14:55:14 -070030import android.os.RemoteException;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070031import android.provider.DocumentsContract.Document;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070032import android.util.Log;
33
Tomasz Mikolajewskib19061c2017-02-13 17:33:42 +090034import com.android.documentsui.archives.ArchivesProvider;
Steve McKay50b9bae2017-01-17 11:12:08 -080035import com.android.documentsui.base.DebugFlags;
Steve McKayd0805062016-09-15 14:30:38 -070036import com.android.documentsui.base.DocumentInfo;
Steve McKayd9caa6a2016-09-15 16:36:45 -070037import com.android.documentsui.base.FilteringCursorWrapper;
Steve McKayd0805062016-09-15 14:30:38 -070038import com.android.documentsui.base.RootInfo;
Garfield Tan16868832016-09-26 10:01:45 -070039import com.android.documentsui.base.Shared;
Steve McKayd9caa6a2016-09-15 16:36:45 -070040import com.android.documentsui.roots.RootCursorWrapper;
Garfield, Tan11d23482016-08-05 09:33:29 -070041import com.android.documentsui.sorting.SortModel;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070042
Steve McKayc88f83c2016-08-31 12:01:43 -070043import libcore.io.IoUtils;
44
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070045public class DirectoryLoader extends AsyncTaskLoader<DirectoryResult> {
Jeff Sharkey9dd02622013-09-27 16:44:11 -070046
Garfield, Tan11d23482016-08-05 09:33:29 -070047 private static final String TAG = "DirectoryLoader";
48
Jeff Sharkey9dd02622013-09-27 16:44:11 -070049 private static final String[] SEARCH_REJECT_MIMES = new String[] { Document.MIME_TYPE_DIR };
50
Ben Lin9fea3122016-10-10 18:32:26 -070051 private final LockingContentObserver mObserver;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070052 private final RootInfo mRoot;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070053 private final Uri mUri;
Garfield, Tan11d23482016-08-05 09:33:29 -070054 private final SortModel mModel;
Aga Wronskaaf5ace52016-02-17 13:50:42 -080055 private final boolean mSearchMode;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070056
Steve McKay7776aa52016-01-25 19:00:22 -080057 private DocumentInfo mDoc;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070058 private CancellationSignal mSignal;
59 private DirectoryResult mResult;
60
Steve McKayc88f83c2016-08-31 12:01:43 -070061 public DirectoryLoader(
62 Context context,
63 RootInfo root,
64 DocumentInfo doc,
65 Uri uri,
66 SortModel model,
Ben Lin9fea3122016-10-10 18:32:26 -070067 DirectoryReloadLock lock,
Steve McKayc88f83c2016-08-31 12:01:43 -070068 boolean inSearchMode) {
69
Jeff Sharkeyf63b7772013-10-01 17:57:41 -070070 super(context, ProviderExecutor.forAuthority(root.authority));
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070071 mRoot = root;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070072 mUri = uri;
Garfield, Tan11d23482016-08-05 09:33:29 -070073 mModel = model;
Steve McKay7776aa52016-01-25 19:00:22 -080074 mDoc = doc;
Aga Wronskaaf5ace52016-02-17 13:50:42 -080075 mSearchMode = inSearchMode;
Ben Lin9fea3122016-10-10 18:32:26 -070076 mObserver = new LockingContentObserver(lock, this::onContentChanged);
Jeff Sharkeya5defe32013-08-05 17:56:48 -070077 }
78
79 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070080 public final DirectoryResult loadInBackground() {
81 synchronized (this) {
82 if (isLoadInBackgroundCanceled()) {
83 throw new OperationCanceledException();
84 }
85 mSignal = new CancellationSignal();
86 }
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070087
88 final ContentResolver resolver = getContext().getContentResolver();
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -070089 final String authority = mUri.getAuthority();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070090
91 final DirectoryResult result = new DirectoryResult();
Tomasz Mikolajewskie29e3412016-02-24 12:53:44 +090092 result.doc = mDoc;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070093
Jeff Sharkeyf63b7772013-10-01 17:57:41 -070094 ContentProviderClient client = null;
Garfield, Tan11d23482016-08-05 09:33:29 -070095 Cursor cursor;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070096 try {
Jeff Sharkeyf63b7772013-10-01 17:57:41 -070097 client = DocumentsApplication.acquireUnstableProviderOrThrow(resolver, authority);
Tomasz Mikolajewskib19061c2017-02-13 17:33:42 +090098 if (mDoc.isInArchive()) {
99 ArchivesProvider.acquireArchive(client, mUri);
100 }
101 result.client = client;
Steve McKay50b9bae2017-01-17 11:12:08 -0800102
Steve McKayf208d842017-02-27 12:57:58 -0800103 if (Shared.ENABLE_OMC_API_FEATURES) {
104 Bundle queryArgs = new Bundle();
105 mModel.addQuerySortArgs(queryArgs);
Steve McKay50b9bae2017-01-17 11:12:08 -0800106
Steve McKayf208d842017-02-27 12:57:58 -0800107 // TODO: At some point we don't want forced flags to override real paging...
108 // and that point is when we have real paging.
109 DebugFlags.addForcedPagingArgs(queryArgs);
110
111 cursor = client.query(mUri, null, queryArgs, mSignal);
112 } else {
113 cursor = client.query(
114 mUri, null, null, null, mModel.getDocumentSortQuery(), mSignal);
Makoto Onuki77778752015-07-01 14:55:14 -0700115 }
116
Steve McKayf208d842017-02-27 12:57:58 -0800117 if (cursor == null) {
118 throw new RemoteException("Provider returned null");
Steve McKay50b9bae2017-01-17 11:12:08 -0800119 }
120
Jeff Sharkey20b32272013-09-03 15:25:52 -0700121 cursor.registerContentObserver(mObserver);
122
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700123 cursor = new RootCursorWrapper(mUri.getAuthority(), mRoot.rootId, cursor, -1);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700124
Garfield Tan16868832016-09-26 10:01:45 -0700125 if (mSearchMode && !Shared.ENABLE_OMC_API_FEATURES) {
Garfield Tanb00bbc52016-11-01 14:23:35 -0700126 // There is no findDocumentPath API. Enable filtering on folders in search mode.
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700127 cursor = new FilteringCursorWrapper(cursor, null, SEARCH_REJECT_MIMES);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700128 }
129
Garfield Tan2010ff72016-09-30 14:55:32 -0700130 cursor = mModel.sortCursor(cursor);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700131 result.cursor = cursor;
Jeff Sharkey1d890e02013-08-15 11:24:03 -0700132 } catch (Exception e) {
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700133 Log.w(TAG, "Failed to query", e);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700134 result.exception = e;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700135 } finally {
136 synchronized (this) {
137 mSignal = null;
138 }
Tomasz Mikolajewskib19061c2017-02-13 17:33:42 +0900139 // TODO: Remove this call.
Ben Linf8f06e92017-01-27 17:15:48 -0800140 ContentProviderClient.releaseQuietly(client);
Jeff Sharkey1d890e02013-08-15 11:24:03 -0700141 }
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700142
Jeff Sharkey46899c82013-08-18 22:26:48 -0700143 return result;
Jeff Sharkey1d890e02013-08-15 11:24:03 -0700144 }
145
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700146 @Override
147 public void cancelLoadInBackground() {
148 super.cancelLoadInBackground();
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700149
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700150 synchronized (this) {
151 if (mSignal != null) {
152 mSignal.cancel();
Jeff Sharkey46899c82013-08-18 22:26:48 -0700153 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700154 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700155 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700156
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700157 @Override
158 public void deliverResult(DirectoryResult result) {
159 if (isReset()) {
160 IoUtils.closeQuietly(result);
161 return;
162 }
163 DirectoryResult oldResult = mResult;
164 mResult = result;
165
166 if (isStarted()) {
167 super.deliverResult(result);
168 }
169
170 if (oldResult != null && oldResult != result) {
171 IoUtils.closeQuietly(oldResult);
172 }
173 }
174
175 @Override
176 protected void onStartLoading() {
177 if (mResult != null) {
178 deliverResult(mResult);
179 }
180 if (takeContentChanged() || mResult == null) {
181 forceLoad();
182 }
183 }
184
185 @Override
186 protected void onStopLoading() {
187 cancelLoad();
188 }
189
190 @Override
191 public void onCanceled(DirectoryResult result) {
192 IoUtils.closeQuietly(result);
193 }
194
195 @Override
196 protected void onReset() {
197 super.onReset();
198
199 // Ensure the loader is stopped
200 onStopLoading();
201
202 IoUtils.closeQuietly(mResult);
203 mResult = null;
204
205 getContext().getContentResolver().unregisterContentObserver(mObserver);
206 }
Ben Lin9fea3122016-10-10 18:32:26 -0700207
208 private static final class LockingContentObserver extends ContentObserver {
209 private final DirectoryReloadLock mLock;
210 private final Runnable mContentChangedCallback;
211
212 public LockingContentObserver(DirectoryReloadLock lock, Runnable contentChangedCallback) {
213 super(new Handler());
214 mLock = lock;
215 mContentChangedCallback = contentChangedCallback;
216 }
217
218 @Override
219 public boolean deliverSelfNotifications() {
220 return true;
221 }
222
223 @Override
224 public void onChange(boolean selfChange) {
225 mLock.tryUpdate(mContentChangedCallback);
226 }
227 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700228}