blob: ab79d011fd830ffea4917a700e55a723cf2ca187 [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
Steve McKayd0805062016-09-15 14:30:38 -070032import com.android.documentsui.base.DocumentInfo;
Steve McKayd9caa6a2016-09-15 16:36:45 -070033import com.android.documentsui.base.FilteringCursorWrapper;
Steve McKayd0805062016-09-15 14:30:38 -070034import com.android.documentsui.base.RootInfo;
Steve McKayd9caa6a2016-09-15 16:36:45 -070035import com.android.documentsui.roots.RootCursorWrapper;
Garfield, Tan11d23482016-08-05 09:33:29 -070036import com.android.documentsui.sorting.SortModel;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070037
Jeff Sharkey0e8c8712013-09-12 21:59:06 -070038import java.io.FileNotFoundException;
39
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
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070048 private final ForceLoadContentObserver mObserver = new ForceLoadContentObserver();
Jeff Sharkeya5defe32013-08-05 17:56:48 -070049
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,
65 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;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070073 }
74
75 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070076 public final DirectoryResult loadInBackground() {
77 synchronized (this) {
78 if (isLoadInBackgroundCanceled()) {
79 throw new OperationCanceledException();
80 }
81 mSignal = new CancellationSignal();
82 }
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070083
84 final ContentResolver resolver = getContext().getContentResolver();
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -070085 final String authority = mUri.getAuthority();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070086
87 final DirectoryResult result = new DirectoryResult();
Tomasz Mikolajewskie29e3412016-02-24 12:53:44 +090088 result.doc = mDoc;
Garfield, Tan11d23482016-08-05 09:33:29 -070089 result.sortModel = mModel;
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
Aga Wronskaaf5ace52016-02-17 13:50:42 -0800105 if (mSearchMode) {
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700106 // Filter directories out of search results, for now
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
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700110 result.client = client;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700111 result.cursor = cursor;
Jeff Sharkey1d890e02013-08-15 11:24:03 -0700112 } catch (Exception e) {
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700113 Log.w(TAG, "Failed to query", e);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700114 result.exception = e;
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700115 ContentProviderClient.releaseQuietly(client);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700116 } finally {
117 synchronized (this) {
118 mSignal = null;
119 }
Jeff Sharkey1d890e02013-08-15 11:24:03 -0700120 }
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700121
Jeff Sharkey46899c82013-08-18 22:26:48 -0700122 return result;
Jeff Sharkey1d890e02013-08-15 11:24:03 -0700123 }
124
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700125 @Override
126 public void cancelLoadInBackground() {
127 super.cancelLoadInBackground();
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700128
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700129 synchronized (this) {
130 if (mSignal != null) {
131 mSignal.cancel();
Jeff Sharkey46899c82013-08-18 22:26:48 -0700132 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700133 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700134 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700135
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700136 @Override
137 public void deliverResult(DirectoryResult result) {
138 if (isReset()) {
139 IoUtils.closeQuietly(result);
140 return;
141 }
142 DirectoryResult oldResult = mResult;
143 mResult = result;
144
145 if (isStarted()) {
146 super.deliverResult(result);
147 }
148
149 if (oldResult != null && oldResult != result) {
150 IoUtils.closeQuietly(oldResult);
151 }
152 }
153
154 @Override
155 protected void onStartLoading() {
156 if (mResult != null) {
157 deliverResult(mResult);
158 }
159 if (takeContentChanged() || mResult == null) {
160 forceLoad();
161 }
162 }
163
164 @Override
165 protected void onStopLoading() {
166 cancelLoad();
167 }
168
169 @Override
170 public void onCanceled(DirectoryResult result) {
171 IoUtils.closeQuietly(result);
172 }
173
174 @Override
175 protected void onReset() {
176 super.onReset();
177
178 // Ensure the loader is stopped
179 onStopLoading();
180
181 IoUtils.closeQuietly(mResult);
182 mResult = null;
183
184 getContext().getContentResolver().unregisterContentObserver(mObserver);
185 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700186}