blob: c0f69fc5dee7d92386520e8ba831b4e277b8755d [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
Steve McKayd0805062016-09-15 14:30:38 -070033import com.android.documentsui.base.DocumentInfo;
Steve McKayd9caa6a2016-09-15 16:36:45 -070034import com.android.documentsui.base.FilteringCursorWrapper;
Steve McKayd0805062016-09-15 14:30:38 -070035import com.android.documentsui.base.RootInfo;
Garfield Tan16868832016-09-26 10:01:45 -070036import com.android.documentsui.base.Shared;
Steve McKayd9caa6a2016-09-15 16:36:45 -070037import com.android.documentsui.roots.RootCursorWrapper;
Garfield, Tan11d23482016-08-05 09:33:29 -070038import com.android.documentsui.sorting.SortModel;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070039
Steve McKayc88f83c2016-08-31 12:01:43 -070040import libcore.io.IoUtils;
41
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070042public class DirectoryLoader extends AsyncTaskLoader<DirectoryResult> {
Jeff Sharkey9dd02622013-09-27 16:44:11 -070043
Garfield, Tan11d23482016-08-05 09:33:29 -070044 private static final String TAG = "DirectoryLoader";
45
Jeff Sharkey9dd02622013-09-27 16:44:11 -070046 private static final String[] SEARCH_REJECT_MIMES = new String[] { Document.MIME_TYPE_DIR };
47
Ben Lin9fea3122016-10-10 18:32:26 -070048 private final LockingContentObserver mObserver;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070049 private final RootInfo mRoot;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070050 private final Uri mUri;
Garfield, Tan11d23482016-08-05 09:33:29 -070051 private final SortModel mModel;
Aga Wronskaaf5ace52016-02-17 13:50:42 -080052 private final boolean mSearchMode;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070053
Steve McKay7776aa52016-01-25 19:00:22 -080054 private DocumentInfo mDoc;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070055 private CancellationSignal mSignal;
56 private DirectoryResult mResult;
57
Steve McKayc88f83c2016-08-31 12:01:43 -070058 public DirectoryLoader(
59 Context context,
60 RootInfo root,
61 DocumentInfo doc,
62 Uri uri,
63 SortModel model,
Ben Lin9fea3122016-10-10 18:32:26 -070064 DirectoryReloadLock lock,
Steve McKayc88f83c2016-08-31 12:01:43 -070065 boolean inSearchMode) {
66
Jeff Sharkeyf63b7772013-10-01 17:57:41 -070067 super(context, ProviderExecutor.forAuthority(root.authority));
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070068 mRoot = root;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070069 mUri = uri;
Garfield, Tan11d23482016-08-05 09:33:29 -070070 mModel = model;
Steve McKay7776aa52016-01-25 19:00:22 -080071 mDoc = doc;
Aga Wronskaaf5ace52016-02-17 13:50:42 -080072 mSearchMode = inSearchMode;
Ben Lin9fea3122016-10-10 18:32:26 -070073 mObserver = new LockingContentObserver(lock, this::onContentChanged);
Jeff Sharkeya5defe32013-08-05 17:56:48 -070074 }
75
76 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070077 public final DirectoryResult loadInBackground() {
78 synchronized (this) {
79 if (isLoadInBackgroundCanceled()) {
80 throw new OperationCanceledException();
81 }
82 mSignal = new CancellationSignal();
83 }
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070084
85 final ContentResolver resolver = getContext().getContentResolver();
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -070086 final String authority = mUri.getAuthority();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070087
88 final DirectoryResult result = new DirectoryResult();
Tomasz Mikolajewskie29e3412016-02-24 12:53:44 +090089 result.doc = mDoc;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070090
Jeff Sharkeyf63b7772013-10-01 17:57:41 -070091 ContentProviderClient client = null;
Garfield, Tan11d23482016-08-05 09:33:29 -070092 Cursor cursor;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070093 try {
Jeff Sharkeyf63b7772013-10-01 17:57:41 -070094 client = DocumentsApplication.acquireUnstableProviderOrThrow(resolver, authority);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -070095 cursor = client.query(
Garfield, Tan11d23482016-08-05 09:33:29 -070096 mUri, null, null, null, mModel.getDocumentSortQuery(), mSignal);
Makoto Onuki77778752015-07-01 14:55:14 -070097 if (cursor == null) {
98 throw new RemoteException("Provider returned null");
99 }
100
Jeff Sharkey20b32272013-09-03 15:25:52 -0700101 cursor.registerContentObserver(mObserver);
102
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700103 cursor = new RootCursorWrapper(mUri.getAuthority(), mRoot.rootId, cursor, -1);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700104
Garfield Tan16868832016-09-26 10:01:45 -0700105 if (mSearchMode && !Shared.ENABLE_OMC_API_FEATURES) {
Garfield Tanb00bbc52016-11-01 14:23:35 -0700106 // There is no findDocumentPath API. Enable filtering on folders in search mode.
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700107 cursor = new FilteringCursorWrapper(cursor, null, SEARCH_REJECT_MIMES);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700108 }
109
Garfield Tan2010ff72016-09-30 14:55:32 -0700110 cursor = mModel.sortCursor(cursor);
111
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700112 result.client = client;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700113 result.cursor = cursor;
Jeff Sharkey1d890e02013-08-15 11:24:03 -0700114 } catch (Exception e) {
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700115 Log.w(TAG, "Failed to query", e);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700116 result.exception = e;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700117 } finally {
118 synchronized (this) {
119 mSignal = null;
120 }
Ben Linf8f06e92017-01-27 17:15:48 -0800121 ContentProviderClient.releaseQuietly(client);
Jeff Sharkey1d890e02013-08-15 11:24:03 -0700122 }
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700123
Jeff Sharkey46899c82013-08-18 22:26:48 -0700124 return result;
Jeff Sharkey1d890e02013-08-15 11:24:03 -0700125 }
126
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700127 @Override
128 public void cancelLoadInBackground() {
129 super.cancelLoadInBackground();
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700130
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700131 synchronized (this) {
132 if (mSignal != null) {
133 mSignal.cancel();
Jeff Sharkey46899c82013-08-18 22:26:48 -0700134 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700135 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700136 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700137
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700138 @Override
139 public void deliverResult(DirectoryResult result) {
140 if (isReset()) {
141 IoUtils.closeQuietly(result);
142 return;
143 }
144 DirectoryResult oldResult = mResult;
145 mResult = result;
146
147 if (isStarted()) {
148 super.deliverResult(result);
149 }
150
151 if (oldResult != null && oldResult != result) {
152 IoUtils.closeQuietly(oldResult);
153 }
154 }
155
156 @Override
157 protected void onStartLoading() {
158 if (mResult != null) {
159 deliverResult(mResult);
160 }
161 if (takeContentChanged() || mResult == null) {
162 forceLoad();
163 }
164 }
165
166 @Override
167 protected void onStopLoading() {
168 cancelLoad();
169 }
170
171 @Override
172 public void onCanceled(DirectoryResult result) {
173 IoUtils.closeQuietly(result);
174 }
175
176 @Override
177 protected void onReset() {
178 super.onReset();
179
180 // Ensure the loader is stopped
181 onStopLoading();
182
183 IoUtils.closeQuietly(mResult);
184 mResult = null;
185
186 getContext().getContentResolver().unregisterContentObserver(mObserver);
187 }
Ben Lin9fea3122016-10-10 18:32:26 -0700188
189 private static final class LockingContentObserver extends ContentObserver {
190 private final DirectoryReloadLock mLock;
191 private final Runnable mContentChangedCallback;
192
193 public LockingContentObserver(DirectoryReloadLock lock, Runnable contentChangedCallback) {
194 super(new Handler());
195 mLock = lock;
196 mContentChangedCallback = contentChangedCallback;
197 }
198
199 @Override
200 public boolean deliverSelfNotifications() {
201 return true;
202 }
203
204 @Override
205 public void onChange(boolean selfChange) {
206 mLock.tryUpdate(mContentChangedCallback);
207 }
208 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700209}