blob: b0542b9eb5be59977ec6f84006c40d85fea35cd3 [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
Steve McKay7776aa52016-01-25 19:00:22 -080019import static com.android.documentsui.Shared.DEBUG;
Steve McKayfefcd702015-08-20 16:19:38 +000020import static com.android.documentsui.Shared.TAG;
Steve McKayf8a5e082015-09-23 17:21:40 -070021import static com.android.documentsui.State.SORT_ORDER_DISPLAY_NAME;
22import static com.android.documentsui.State.SORT_ORDER_LAST_MODIFIED;
23import static com.android.documentsui.State.SORT_ORDER_SIZE;
Jeff Sharkeyb3620442013-09-01 18:41:04 -070024
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070025import android.content.AsyncTaskLoader;
26import android.content.ContentProviderClient;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070027import android.content.ContentResolver;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070028import android.content.Context;
29import android.database.Cursor;
30import android.net.Uri;
31import android.os.CancellationSignal;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070032import android.os.OperationCanceledException;
Makoto Onuki77778752015-07-01 14:55:14 -070033import android.os.RemoteException;
Jeff Sharkey0e8c8712013-09-12 21:59:06 -070034import android.provider.DocumentsContract;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070035import android.provider.DocumentsContract.Document;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070036import android.util.Log;
37
Steve McKayf68210e2015-11-03 15:23:16 -080038import com.android.documentsui.dirlist.DirectoryFragment;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070039import com.android.documentsui.model.DocumentInfo;
40import com.android.documentsui.model.RootInfo;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070041
Jeff Sharkeya5defe32013-08-05 17:56:48 -070042import libcore.io.IoUtils;
43
Jeff Sharkey0e8c8712013-09-12 21:59:06 -070044import java.io.FileNotFoundException;
45
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070046public class DirectoryLoader extends AsyncTaskLoader<DirectoryResult> {
Jeff Sharkey9dd02622013-09-27 16:44:11 -070047
48 private static final String[] SEARCH_REJECT_MIMES = new String[] { Document.MIME_TYPE_DIR };
49
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070050 private final ForceLoadContentObserver mObserver = new ForceLoadContentObserver();
Jeff Sharkeya5defe32013-08-05 17:56:48 -070051
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070052 private final int mType;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070053 private final RootInfo mRoot;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070054 private final Uri mUri;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070055 private final int mUserSortOrder;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070056
Steve McKay7776aa52016-01-25 19:00:22 -080057 private DocumentInfo mDoc;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070058 private CancellationSignal mSignal;
59 private DirectoryResult mResult;
60
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070061 public DirectoryLoader(Context context, int type, RootInfo root, DocumentInfo doc, Uri uri,
62 int userSortOrder) {
Jeff Sharkeyf63b7772013-10-01 17:57:41 -070063 super(context, ProviderExecutor.forAuthority(root.authority));
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070064 mType = type;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070065 mRoot = root;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070066 mUri = uri;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070067 mUserSortOrder = userSortOrder;
Steve McKay7776aa52016-01-25 19:00:22 -080068 mDoc = doc;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070069 }
70
71 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070072 public final DirectoryResult loadInBackground() {
73 synchronized (this) {
74 if (isLoadInBackgroundCanceled()) {
75 throw new OperationCanceledException();
76 }
77 mSignal = new CancellationSignal();
78 }
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070079
80 final ContentResolver resolver = getContext().getContentResolver();
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -070081 final String authority = mUri.getAuthority();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070082
83 final DirectoryResult result = new DirectoryResult();
84
Jeff Sharkey0e8c8712013-09-12 21:59:06 -070085 // Use default document when searching
86 if (mType == DirectoryFragment.TYPE_SEARCH) {
87 final Uri docUri = DocumentsContract.buildDocumentUri(
88 mRoot.authority, mRoot.documentId);
89 try {
90 mDoc = DocumentInfo.fromUri(resolver, docUri);
91 } catch (FileNotFoundException e) {
92 Log.w(TAG, "Failed to query", e);
93 result.exception = e;
94 return result;
95 }
96 }
97
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070098 if (mUserSortOrder != State.SORT_ORDER_UNKNOWN) {
99 result.sortOrder = mUserSortOrder;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700100 } else {
101 if ((mDoc.flags & Document.FLAG_DIR_PREFERS_LAST_MODIFIED) != 0) {
102 result.sortOrder = State.SORT_ORDER_LAST_MODIFIED;
103 } else {
104 result.sortOrder = State.SORT_ORDER_DISPLAY_NAME;
105 }
106 }
107
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700108 // Search always uses ranking from provider
109 if (mType == DirectoryFragment.TYPE_SEARCH) {
110 result.sortOrder = State.SORT_ORDER_UNKNOWN;
111 }
112
Steve McKay7776aa52016-01-25 19:00:22 -0800113 if (DEBUG)
114 Log.d(TAG, "userSortOrder=" + mUserSortOrder + ", sortOrder=" + result.sortOrder);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700115
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700116 ContentProviderClient client = null;
Steve McKay7776aa52016-01-25 19:00:22 -0800117 Cursor cursor = null;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700118 try {
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700119 client = DocumentsApplication.acquireUnstableProviderOrThrow(resolver, authority);
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700120 cursor = client.query(
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700121 mUri, null, null, null, getQuerySortOrder(result.sortOrder), mSignal);
Makoto Onuki77778752015-07-01 14:55:14 -0700122 if (cursor == null) {
123 throw new RemoteException("Provider returned null");
124 }
125
Jeff Sharkey20b32272013-09-03 15:25:52 -0700126 cursor.registerContentObserver(mObserver);
127
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700128 cursor = new RootCursorWrapper(mUri.getAuthority(), mRoot.rootId, cursor, -1);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700129
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700130 if (mType == DirectoryFragment.TYPE_SEARCH) {
131 // Filter directories out of search results, for now
Jeff Sharkey9dd02622013-09-27 16:44:11 -0700132 cursor = new FilteringCursorWrapper(cursor, null, SEARCH_REJECT_MIMES);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700133 }
134
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700135 result.client = client;
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700136 result.cursor = cursor;
Jeff Sharkey1d890e02013-08-15 11:24:03 -0700137 } catch (Exception e) {
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700138 Log.w(TAG, "Failed to query", e);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700139 result.exception = e;
Jeff Sharkeyf63b7772013-10-01 17:57:41 -0700140 ContentProviderClient.releaseQuietly(client);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700141 } finally {
142 synchronized (this) {
143 mSignal = null;
144 }
Jeff Sharkey1d890e02013-08-15 11:24:03 -0700145 }
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700146
Jeff Sharkey46899c82013-08-18 22:26:48 -0700147 return result;
Jeff Sharkey1d890e02013-08-15 11:24:03 -0700148 }
149
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700150 @Override
151 public void cancelLoadInBackground() {
152 super.cancelLoadInBackground();
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700153
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700154 synchronized (this) {
155 if (mSignal != null) {
156 mSignal.cancel();
Jeff Sharkey46899c82013-08-18 22:26:48 -0700157 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700158 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700159 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700160
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700161 @Override
162 public void deliverResult(DirectoryResult result) {
163 if (isReset()) {
164 IoUtils.closeQuietly(result);
165 return;
166 }
167 DirectoryResult oldResult = mResult;
168 mResult = result;
169
170 if (isStarted()) {
171 super.deliverResult(result);
172 }
173
174 if (oldResult != null && oldResult != result) {
175 IoUtils.closeQuietly(oldResult);
176 }
177 }
178
179 @Override
180 protected void onStartLoading() {
181 if (mResult != null) {
182 deliverResult(mResult);
183 }
184 if (takeContentChanged() || mResult == null) {
185 forceLoad();
186 }
187 }
188
189 @Override
190 protected void onStopLoading() {
191 cancelLoad();
192 }
193
194 @Override
195 public void onCanceled(DirectoryResult result) {
196 IoUtils.closeQuietly(result);
197 }
198
199 @Override
200 protected void onReset() {
201 super.onReset();
202
203 // Ensure the loader is stopped
204 onStopLoading();
205
206 IoUtils.closeQuietly(mResult);
207 mResult = null;
208
209 getContext().getContentResolver().unregisterContentObserver(mObserver);
210 }
211
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700212 public static String getQuerySortOrder(int sortOrder) {
213 switch (sortOrder) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700214 case SORT_ORDER_DISPLAY_NAME:
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700215 return Document.COLUMN_DISPLAY_NAME + " ASC";
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700216 case SORT_ORDER_LAST_MODIFIED:
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700217 return Document.COLUMN_LAST_MODIFIED + " DESC";
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700218 case SORT_ORDER_SIZE:
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700219 return Document.COLUMN_SIZE + " DESC";
220 default:
221 return null;
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700222 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700223 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700224}