blob: 8720701115fe3de7da5ea8d8a126b058b032579e [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;
26import android.os.CancellationSignal;
Ben Lin9fea3122016-10-10 18:32:26 -070027import android.os.Handler;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070028import android.os.OperationCanceledException;
Makoto Onuki77778752015-07-01 14:55:14 -070029import android.os.RemoteException;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070030import android.provider.DocumentsContract.Document;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070031import android.util.Log;
32
Tomasz Mikolajewskib19061c2017-02-13 17:33:42 +090033import com.android.documentsui.archives.ArchivesProvider;
Steve McKayd0805062016-09-15 14:30:38 -070034import com.android.documentsui.base.DocumentInfo;
Steve McKayd9caa6a2016-09-15 16:36:45 -070035import com.android.documentsui.base.FilteringCursorWrapper;
Steve McKayd0805062016-09-15 14:30:38 -070036import com.android.documentsui.base.RootInfo;
Garfield Tan16868832016-09-26 10:01:45 -070037import com.android.documentsui.base.Shared;
Steve McKayd9caa6a2016-09-15 16:36:45 -070038import com.android.documentsui.roots.RootCursorWrapper;
Garfield, Tan11d23482016-08-05 09:33:29 -070039import com.android.documentsui.sorting.SortModel;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070040
Steve McKayc88f83c2016-08-31 12:01:43 -070041import libcore.io.IoUtils;
42
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070043public class DirectoryLoader extends AsyncTaskLoader<DirectoryResult> {
Jeff Sharkey9dd02622013-09-27 16:44:11 -070044
Garfield, Tan11d23482016-08-05 09:33:29 -070045 private static final String TAG = "DirectoryLoader";
46
Jeff Sharkey9dd02622013-09-27 16:44:11 -070047 private static final String[] SEARCH_REJECT_MIMES = new String[] { Document.MIME_TYPE_DIR };
48
Ben Lin9fea3122016-10-10 18:32:26 -070049 private final LockingContentObserver mObserver;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070050 private final RootInfo mRoot;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070051 private final Uri mUri;
Garfield, Tan11d23482016-08-05 09:33:29 -070052 private final SortModel mModel;
Aga Wronskaaf5ace52016-02-17 13:50:42 -080053 private final boolean mSearchMode;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070054
Steve McKay7776aa52016-01-25 19:00:22 -080055 private DocumentInfo mDoc;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070056 private CancellationSignal mSignal;
57 private DirectoryResult mResult;
58
Steve McKayc88f83c2016-08-31 12:01:43 -070059 public DirectoryLoader(
60 Context context,
61 RootInfo root,
62 DocumentInfo doc,
63 Uri uri,
64 SortModel model,
Ben Lin9fea3122016-10-10 18:32:26 -070065 DirectoryReloadLock lock,
Steve McKayc88f83c2016-08-31 12:01:43 -070066 boolean inSearchMode) {
67
Jeff Sharkeyf63b7772013-10-01 17:57:41 -070068 super(context, ProviderExecutor.forAuthority(root.authority));
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070069 mRoot = root;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070070 mUri = uri;
Garfield, Tan11d23482016-08-05 09:33:29 -070071 mModel = model;
Steve McKay7776aa52016-01-25 19:00:22 -080072 mDoc = doc;
Aga Wronskaaf5ace52016-02-17 13:50:42 -080073 mSearchMode = inSearchMode;
Ben Lin9fea3122016-10-10 18:32:26 -070074 mObserver = new LockingContentObserver(lock, this::onContentChanged);
Jeff Sharkeya5defe32013-08-05 17:56:48 -070075 }
76
77 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070078 public final DirectoryResult loadInBackground() {
79 synchronized (this) {
80 if (isLoadInBackgroundCanceled()) {
81 throw new OperationCanceledException();
82 }
83 mSignal = new CancellationSignal();
84 }
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070085
86 final ContentResolver resolver = getContext().getContentResolver();
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -070087 final String authority = mUri.getAuthority();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070088
89 final DirectoryResult result = new DirectoryResult();
Tomasz Mikolajewskie29e3412016-02-24 12:53:44 +090090 result.doc = mDoc;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070091
Jeff Sharkeyf63b7772013-10-01 17:57:41 -070092 ContentProviderClient client = null;
Garfield, Tan11d23482016-08-05 09:33:29 -070093 Cursor cursor;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070094 try {
Jeff Sharkeyf63b7772013-10-01 17:57:41 -070095 client = DocumentsApplication.acquireUnstableProviderOrThrow(resolver, authority);
Tomasz Mikolajewskib19061c2017-02-13 17:33:42 +090096 if (mDoc.isInArchive()) {
97 ArchivesProvider.acquireArchive(client, mUri);
98 }
99 result.client = client;
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700100 cursor = client.query(
Garfield, Tan11d23482016-08-05 09:33:29 -0700101 mUri, null, null, null, mModel.getDocumentSortQuery(), mSignal);
Makoto Onuki77778752015-07-01 14:55:14 -0700102 if (cursor == null) {
103 throw new RemoteException("Provider returned null");
104 }
105
Jeff Sharkey20b32272013-09-03 15:25:52 -0700106 cursor.registerContentObserver(mObserver);
107
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700108 cursor = new RootCursorWrapper(mUri.getAuthority(), mRoot.rootId, cursor, -1);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700109
Garfield Tan16868832016-09-26 10:01:45 -0700110 if (mSearchMode && !Shared.ENABLE_OMC_API_FEATURES) {
Garfield Tanb00bbc52016-11-01 14:23:35 -0700111 // There is no findDocumentPath API. Enable filtering on folders in search mode.
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700112 cursor = new FilteringCursorWrapper(cursor, null, SEARCH_REJECT_MIMES);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700113 }
114
Garfield Tan2010ff72016-09-30 14:55:32 -0700115 cursor = mModel.sortCursor(cursor);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700116 result.cursor = cursor;
Jeff Sharkey1d890e02013-08-15 11:24:03 -0700117 } catch (Exception e) {
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700118 Log.w(TAG, "Failed to query", e);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700119 result.exception = e;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700120 } finally {
121 synchronized (this) {
122 mSignal = null;
123 }
Tomasz Mikolajewskib19061c2017-02-13 17:33:42 +0900124 // TODO: Remove this call.
Ben Linf8f06e92017-01-27 17:15:48 -0800125 ContentProviderClient.releaseQuietly(client);
Jeff Sharkey1d890e02013-08-15 11:24:03 -0700126 }
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700127
Jeff Sharkey46899c82013-08-18 22:26:48 -0700128 return result;
Jeff Sharkey1d890e02013-08-15 11:24:03 -0700129 }
130
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700131 @Override
132 public void cancelLoadInBackground() {
133 super.cancelLoadInBackground();
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700134
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700135 synchronized (this) {
136 if (mSignal != null) {
137 mSignal.cancel();
Jeff Sharkey46899c82013-08-18 22:26:48 -0700138 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700139 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700140 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700141
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700142 @Override
143 public void deliverResult(DirectoryResult result) {
144 if (isReset()) {
145 IoUtils.closeQuietly(result);
146 return;
147 }
148 DirectoryResult oldResult = mResult;
149 mResult = result;
150
151 if (isStarted()) {
152 super.deliverResult(result);
153 }
154
155 if (oldResult != null && oldResult != result) {
156 IoUtils.closeQuietly(oldResult);
157 }
158 }
159
160 @Override
161 protected void onStartLoading() {
162 if (mResult != null) {
163 deliverResult(mResult);
164 }
165 if (takeContentChanged() || mResult == null) {
166 forceLoad();
167 }
168 }
169
170 @Override
171 protected void onStopLoading() {
172 cancelLoad();
173 }
174
175 @Override
176 public void onCanceled(DirectoryResult result) {
177 IoUtils.closeQuietly(result);
178 }
179
180 @Override
181 protected void onReset() {
182 super.onReset();
183
184 // Ensure the loader is stopped
185 onStopLoading();
186
187 IoUtils.closeQuietly(mResult);
188 mResult = null;
189
190 getContext().getContentResolver().unregisterContentObserver(mObserver);
191 }
Ben Lin9fea3122016-10-10 18:32:26 -0700192
193 private static final class LockingContentObserver extends ContentObserver {
194 private final DirectoryReloadLock mLock;
195 private final Runnable mContentChangedCallback;
196
197 public LockingContentObserver(DirectoryReloadLock lock, Runnable contentChangedCallback) {
198 super(new Handler());
199 mLock = lock;
200 mContentChangedCallback = contentChangedCallback;
201 }
202
203 @Override
204 public boolean deliverSelfNotifications() {
205 return true;
206 }
207
208 @Override
209 public void onChange(boolean selfChange) {
210 mLock.tryUpdate(mContentChangedCallback);
211 }
212 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700213}