blob: 30f88afdf10460230e0e10f204ebb9d0e7cba873 [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 Sharkey0e8c8712013-09-12 21:59:06 -070028import android.provider.DocumentsContract;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070029import android.provider.DocumentsContract.Document;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070030import android.util.Log;
31
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070032import com.android.documentsui.model.DocumentInfo;
33import com.android.documentsui.model.RootInfo;
Garfield, Tan11d23482016-08-05 09:33:29 -070034import com.android.documentsui.sorting.SortModel;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070035
Jeff Sharkey0e8c8712013-09-12 21:59:06 -070036import java.io.FileNotFoundException;
37
Steve McKayc88f83c2016-08-31 12:01:43 -070038import libcore.io.IoUtils;
39
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070040public class DirectoryLoader extends AsyncTaskLoader<DirectoryResult> {
Jeff Sharkey9dd02622013-09-27 16:44:11 -070041
Garfield, Tan11d23482016-08-05 09:33:29 -070042 private static final String TAG = "DirectoryLoader";
43
Jeff Sharkey9dd02622013-09-27 16:44:11 -070044 private static final String[] SEARCH_REJECT_MIMES = new String[] { Document.MIME_TYPE_DIR };
45
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070046 private final ForceLoadContentObserver mObserver = new ForceLoadContentObserver();
Jeff Sharkeya5defe32013-08-05 17:56:48 -070047
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070048 private final RootInfo mRoot;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070049 private final Uri mUri;
Garfield, Tan11d23482016-08-05 09:33:29 -070050 private final SortModel mModel;
Aga Wronskaaf5ace52016-02-17 13:50:42 -080051 private final boolean mSearchMode;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070052
Steve McKay7776aa52016-01-25 19:00:22 -080053 private DocumentInfo mDoc;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070054 private CancellationSignal mSignal;
55 private DirectoryResult mResult;
56
Steve McKayc88f83c2016-08-31 12:01:43 -070057 public DirectoryLoader(
58 Context context,
59 RootInfo root,
60 DocumentInfo doc,
61 Uri uri,
62 SortModel model,
63 boolean inSearchMode) {
64
Jeff Sharkeyf63b7772013-10-01 17:57:41 -070065 super(context, ProviderExecutor.forAuthority(root.authority));
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070066 mRoot = root;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070067 mUri = uri;
Garfield, Tan11d23482016-08-05 09:33:29 -070068 mModel = model;
Steve McKay7776aa52016-01-25 19:00:22 -080069 mDoc = doc;
Aga Wronskaaf5ace52016-02-17 13:50:42 -080070 mSearchMode = inSearchMode;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070071 }
72
73 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070074 public final DirectoryResult loadInBackground() {
75 synchronized (this) {
76 if (isLoadInBackgroundCanceled()) {
77 throw new OperationCanceledException();
78 }
79 mSignal = new CancellationSignal();
80 }
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070081
82 final ContentResolver resolver = getContext().getContentResolver();
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -070083 final String authority = mUri.getAuthority();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070084
85 final DirectoryResult result = new DirectoryResult();
Tomasz Mikolajewskie29e3412016-02-24 12:53:44 +090086 result.doc = mDoc;
Garfield, Tan11d23482016-08-05 09:33:29 -070087 result.sortModel = mModel;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070088
Jeff Sharkey0e8c8712013-09-12 21:59:06 -070089 // Use default document when searching
Aga Wronskaaf5ace52016-02-17 13:50:42 -080090 if (mSearchMode) {
Jeff Sharkey0e8c8712013-09-12 21:59:06 -070091 final Uri docUri = DocumentsContract.buildDocumentUri(
92 mRoot.authority, mRoot.documentId);
93 try {
94 mDoc = DocumentInfo.fromUri(resolver, docUri);
95 } catch (FileNotFoundException e) {
96 Log.w(TAG, "Failed to query", e);
97 result.exception = e;
98 return result;
99 }
100 }
101
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700102 ContentProviderClient client = null;
Garfield, Tan11d23482016-08-05 09:33:29 -0700103 Cursor cursor;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700104 try {
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700105 client = DocumentsApplication.acquireUnstableProviderOrThrow(resolver, authority);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700106 cursor = client.query(
Garfield, Tan11d23482016-08-05 09:33:29 -0700107 mUri, null, null, null, mModel.getDocumentSortQuery(), mSignal);
Makoto Onuki77778752015-07-01 14:55:14 -0700108 if (cursor == null) {
109 throw new RemoteException("Provider returned null");
110 }
111
Jeff Sharkey20b32272013-09-03 15:25:52 -0700112 cursor.registerContentObserver(mObserver);
113
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700114 cursor = new RootCursorWrapper(mUri.getAuthority(), mRoot.rootId, cursor, -1);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700115
Aga Wronskaaf5ace52016-02-17 13:50:42 -0800116 if (mSearchMode) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700117 // Filter directories out of search results, for now
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700118 cursor = new FilteringCursorWrapper(cursor, null, SEARCH_REJECT_MIMES);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700119 }
120
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700121 result.client = client;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700122 result.cursor = cursor;
Jeff Sharkey1d890e02013-08-15 11:24:03 -0700123 } catch (Exception e) {
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700124 Log.w(TAG, "Failed to query", e);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700125 result.exception = e;
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700126 ContentProviderClient.releaseQuietly(client);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700127 } finally {
128 synchronized (this) {
129 mSignal = null;
130 }
Jeff Sharkey1d890e02013-08-15 11:24:03 -0700131 }
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700132
Jeff Sharkey46899c82013-08-18 22:26:48 -0700133 return result;
Jeff Sharkey1d890e02013-08-15 11:24:03 -0700134 }
135
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700136 @Override
137 public void cancelLoadInBackground() {
138 super.cancelLoadInBackground();
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700139
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700140 synchronized (this) {
141 if (mSignal != null) {
142 mSignal.cancel();
Jeff Sharkey46899c82013-08-18 22:26:48 -0700143 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700144 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700145 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700146
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700147 @Override
148 public void deliverResult(DirectoryResult result) {
149 if (isReset()) {
150 IoUtils.closeQuietly(result);
151 return;
152 }
153 DirectoryResult oldResult = mResult;
154 mResult = result;
155
156 if (isStarted()) {
157 super.deliverResult(result);
158 }
159
160 if (oldResult != null && oldResult != result) {
161 IoUtils.closeQuietly(oldResult);
162 }
163 }
164
165 @Override
166 protected void onStartLoading() {
167 if (mResult != null) {
168 deliverResult(mResult);
169 }
170 if (takeContentChanged() || mResult == null) {
171 forceLoad();
172 }
173 }
174
175 @Override
176 protected void onStopLoading() {
177 cancelLoad();
178 }
179
180 @Override
181 public void onCanceled(DirectoryResult result) {
182 IoUtils.closeQuietly(result);
183 }
184
185 @Override
186 protected void onReset() {
187 super.onReset();
188
189 // Ensure the loader is stopped
190 onStopLoading();
191
192 IoUtils.closeQuietly(mResult);
193 mResult = null;
194
195 getContext().getContentResolver().unregisterContentObserver(mObserver);
196 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700197}