blob: d2c7d100ce1df825d656f7ccad6ca366631bf99c [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;
23import android.database.Cursor;
24import android.net.Uri;
25import android.os.CancellationSignal;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070026import android.os.OperationCanceledException;
Makoto Onuki77778752015-07-01 14:55:14 -070027import android.os.RemoteException;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070028import android.provider.DocumentsContract.Document;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070029import android.util.Log;
30
Steve McKayd0805062016-09-15 14:30:38 -070031import com.android.documentsui.base.DocumentInfo;
Steve McKayd9caa6a2016-09-15 16:36:45 -070032import com.android.documentsui.base.FilteringCursorWrapper;
Steve McKayd0805062016-09-15 14:30:38 -070033import com.android.documentsui.base.RootInfo;
Steve McKayd9caa6a2016-09-15 16:36:45 -070034import com.android.documentsui.roots.RootCursorWrapper;
Garfield, Tan11d23482016-08-05 09:33:29 -070035import com.android.documentsui.sorting.SortModel;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070036
Steve McKayc88f83c2016-08-31 12:01:43 -070037import libcore.io.IoUtils;
38
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070039public class DirectoryLoader extends AsyncTaskLoader<DirectoryResult> {
Jeff Sharkey9dd02622013-09-27 16:44:11 -070040
Garfield, Tan11d23482016-08-05 09:33:29 -070041 private static final String TAG = "DirectoryLoader";
42
Jeff Sharkey9dd02622013-09-27 16:44:11 -070043 private static final String[] SEARCH_REJECT_MIMES = new String[] { Document.MIME_TYPE_DIR };
44
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070045 private final ForceLoadContentObserver mObserver = new ForceLoadContentObserver();
Jeff Sharkeya5defe32013-08-05 17:56:48 -070046
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070047 private final RootInfo mRoot;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070048 private final Uri mUri;
Garfield, Tan11d23482016-08-05 09:33:29 -070049 private final SortModel mModel;
Aga Wronskaaf5ace52016-02-17 13:50:42 -080050 private final boolean mSearchMode;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070051
Steve McKay7776aa52016-01-25 19:00:22 -080052 private DocumentInfo mDoc;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070053 private CancellationSignal mSignal;
54 private DirectoryResult mResult;
55
Steve McKayc88f83c2016-08-31 12:01:43 -070056 public DirectoryLoader(
57 Context context,
58 RootInfo root,
59 DocumentInfo doc,
60 Uri uri,
61 SortModel model,
62 boolean inSearchMode) {
63
Jeff Sharkeyf63b7772013-10-01 17:57:41 -070064 super(context, ProviderExecutor.forAuthority(root.authority));
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070065 mRoot = root;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070066 mUri = uri;
Garfield, Tan11d23482016-08-05 09:33:29 -070067 mModel = model;
Steve McKay7776aa52016-01-25 19:00:22 -080068 mDoc = doc;
Aga Wronskaaf5ace52016-02-17 13:50:42 -080069 mSearchMode = inSearchMode;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070070 }
71
72 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070073 public final DirectoryResult loadInBackground() {
74 synchronized (this) {
75 if (isLoadInBackgroundCanceled()) {
76 throw new OperationCanceledException();
77 }
78 mSignal = new CancellationSignal();
79 }
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070080
81 final ContentResolver resolver = getContext().getContentResolver();
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -070082 final String authority = mUri.getAuthority();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070083
84 final DirectoryResult result = new DirectoryResult();
Tomasz Mikolajewskie29e3412016-02-24 12:53:44 +090085 result.doc = mDoc;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070086
Jeff Sharkeyf63b7772013-10-01 17:57:41 -070087 ContentProviderClient client = null;
Garfield, Tan11d23482016-08-05 09:33:29 -070088 Cursor cursor;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070089 try {
Jeff Sharkeyf63b7772013-10-01 17:57:41 -070090 client = DocumentsApplication.acquireUnstableProviderOrThrow(resolver, authority);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -070091 cursor = client.query(
Garfield, Tan11d23482016-08-05 09:33:29 -070092 mUri, null, null, null, mModel.getDocumentSortQuery(), mSignal);
Makoto Onuki77778752015-07-01 14:55:14 -070093 if (cursor == null) {
94 throw new RemoteException("Provider returned null");
95 }
96
Jeff Sharkey20b32272013-09-03 15:25:52 -070097 cursor.registerContentObserver(mObserver);
98
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070099 cursor = new RootCursorWrapper(mUri.getAuthority(), mRoot.rootId, cursor, -1);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700100
Aga Wronskaaf5ace52016-02-17 13:50:42 -0800101 if (mSearchMode) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700102 // Filter directories out of search results, for now
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700103 cursor = new FilteringCursorWrapper(cursor, null, SEARCH_REJECT_MIMES);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700104 }
105
Garfield Tan2010ff72016-09-30 14:55:32 -0700106 cursor = mModel.sortCursor(cursor);
107
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700108 result.client = client;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700109 result.cursor = cursor;
Jeff Sharkey1d890e02013-08-15 11:24:03 -0700110 } catch (Exception e) {
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700111 Log.w(TAG, "Failed to query", e);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700112 result.exception = e;
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700113 ContentProviderClient.releaseQuietly(client);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700114 } finally {
115 synchronized (this) {
116 mSignal = null;
117 }
Jeff Sharkey1d890e02013-08-15 11:24:03 -0700118 }
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700119
Jeff Sharkey46899c82013-08-18 22:26:48 -0700120 return result;
Jeff Sharkey1d890e02013-08-15 11:24:03 -0700121 }
122
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700123 @Override
124 public void cancelLoadInBackground() {
125 super.cancelLoadInBackground();
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700126
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700127 synchronized (this) {
128 if (mSignal != null) {
129 mSignal.cancel();
Jeff Sharkey46899c82013-08-18 22:26:48 -0700130 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700131 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700132 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700133
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700134 @Override
135 public void deliverResult(DirectoryResult result) {
136 if (isReset()) {
137 IoUtils.closeQuietly(result);
138 return;
139 }
140 DirectoryResult oldResult = mResult;
141 mResult = result;
142
143 if (isStarted()) {
144 super.deliverResult(result);
145 }
146
147 if (oldResult != null && oldResult != result) {
148 IoUtils.closeQuietly(oldResult);
149 }
150 }
151
152 @Override
153 protected void onStartLoading() {
154 if (mResult != null) {
155 deliverResult(mResult);
156 }
157 if (takeContentChanged() || mResult == null) {
158 forceLoad();
159 }
160 }
161
162 @Override
163 protected void onStopLoading() {
164 cancelLoad();
165 }
166
167 @Override
168 public void onCanceled(DirectoryResult result) {
169 IoUtils.closeQuietly(result);
170 }
171
172 @Override
173 protected void onReset() {
174 super.onReset();
175
176 // Ensure the loader is stopped
177 onStopLoading();
178
179 IoUtils.closeQuietly(mResult);
180 mResult = null;
181
182 getContext().getContentResolver().unregisterContentObserver(mObserver);
183 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700184}