blob: 8627ecfaa2f7ef152c1b404c50e2c2b6936c63cc [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 Sharkeya4d1f222013-09-07 14:45:03 -070019import static com.android.documentsui.DocumentsActivity.TAG;
20import static com.android.documentsui.DocumentsActivity.State.MODE_UNKNOWN;
Jeff Sharkeyb3620442013-09-01 18:41:04 -070021import static com.android.documentsui.DocumentsActivity.State.SORT_ORDER_DISPLAY_NAME;
22import static com.android.documentsui.DocumentsActivity.State.SORT_ORDER_LAST_MODIFIED;
23import static com.android.documentsui.DocumentsActivity.State.SORT_ORDER_SIZE;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070024import static com.android.documentsui.DocumentsActivity.State.SORT_ORDER_UNKNOWN;
25import static com.android.documentsui.model.DocumentInfo.getCursorInt;
Jeff Sharkeyb3620442013-09-01 18:41:04 -070026
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070027import android.content.AsyncTaskLoader;
28import android.content.ContentProviderClient;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070029import android.content.ContentResolver;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070030import android.content.Context;
31import android.database.Cursor;
32import android.net.Uri;
33import android.os.CancellationSignal;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070034import android.os.OperationCanceledException;
Jeff Sharkey0e8c8712013-09-12 21:59:06 -070035import android.provider.DocumentsContract;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070036import android.provider.DocumentsContract.Document;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070037import android.util.Log;
38
39import com.android.documentsui.DocumentsActivity.State;
40import com.android.documentsui.RecentsProvider.StateColumns;
41import com.android.documentsui.model.DocumentInfo;
42import com.android.documentsui.model.RootInfo;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070043
Jeff Sharkeya5defe32013-08-05 17:56:48 -070044import libcore.io.IoUtils;
45
Jeff Sharkey0e8c8712013-09-12 21:59:06 -070046import java.io.FileNotFoundException;
47
Jeff Sharkey46899c82013-08-18 22:26:48 -070048class DirectoryResult implements AutoCloseable {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070049 ContentProviderClient client;
Jeff Sharkey46899c82013-08-18 22:26:48 -070050 Cursor cursor;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070051 Exception exception;
Jeff Sharkey46899c82013-08-18 22:26:48 -070052
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070053 int mode = MODE_UNKNOWN;
54 int sortOrder = SORT_ORDER_UNKNOWN;
55
Jeff Sharkey46899c82013-08-18 22:26:48 -070056 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070057 public void close() {
Jeff Sharkey46899c82013-08-18 22:26:48 -070058 IoUtils.closeQuietly(cursor);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070059 ContentProviderClient.closeQuietly(client);
60 cursor = null;
61 client = null;
Jeff Sharkey46899c82013-08-18 22:26:48 -070062 }
63}
64
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070065public class DirectoryLoader extends AsyncTaskLoader<DirectoryResult> {
66 private final ForceLoadContentObserver mObserver = new ForceLoadContentObserver();
Jeff Sharkeya5defe32013-08-05 17:56:48 -070067
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070068 private final int mType;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070069 private final RootInfo mRoot;
Jeff Sharkey0e8c8712013-09-12 21:59:06 -070070 private DocumentInfo mDoc;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070071 private final Uri mUri;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070072 private final int mUserSortOrder;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070073
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070074 private CancellationSignal mSignal;
75 private DirectoryResult mResult;
76
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070077 public DirectoryLoader(Context context, int type, RootInfo root, DocumentInfo doc, Uri uri,
78 int userSortOrder) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070079 super(context);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070080 mType = type;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070081 mRoot = root;
82 mDoc = doc;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070083 mUri = uri;
Jeff Sharkeyd10f0492013-09-09 17:35:46 -070084 mUserSortOrder = userSortOrder;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070085 }
86
87 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070088 public final DirectoryResult loadInBackground() {
89 synchronized (this) {
90 if (isLoadInBackgroundCanceled()) {
91 throw new OperationCanceledException();
92 }
93 mSignal = new CancellationSignal();
94 }
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070095
96 final ContentResolver resolver = getContext().getContentResolver();
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -070097 final String authority = mUri.getAuthority();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070098
99 final DirectoryResult result = new DirectoryResult();
100
101 int userMode = State.MODE_UNKNOWN;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700102
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700103 // Use default document when searching
104 if (mType == DirectoryFragment.TYPE_SEARCH) {
105 final Uri docUri = DocumentsContract.buildDocumentUri(
106 mRoot.authority, mRoot.documentId);
107 try {
108 mDoc = DocumentInfo.fromUri(resolver, docUri);
109 } catch (FileNotFoundException e) {
110 Log.w(TAG, "Failed to query", e);
111 result.exception = e;
112 return result;
113 }
114 }
115
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700116 // Pick up any custom modes requested by user
117 Cursor cursor = null;
Jeff Sharkey1d890e02013-08-15 11:24:03 -0700118 try {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700119 final Uri stateUri = RecentsProvider.buildState(
120 mRoot.authority, mRoot.rootId, mDoc.documentId);
121 cursor = resolver.query(stateUri, null, null, null, null);
122 if (cursor.moveToFirst()) {
123 userMode = getCursorInt(cursor, StateColumns.MODE);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700124 }
125 } finally {
126 IoUtils.closeQuietly(cursor);
127 }
128
129 if (userMode != State.MODE_UNKNOWN) {
130 result.mode = userMode;
131 } else {
132 if ((mDoc.flags & Document.FLAG_DIR_PREFERS_GRID) != 0) {
133 result.mode = State.MODE_GRID;
134 } else {
135 result.mode = State.MODE_LIST;
136 }
137 }
138
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700139 if (mUserSortOrder != State.SORT_ORDER_UNKNOWN) {
140 result.sortOrder = mUserSortOrder;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700141 } else {
142 if ((mDoc.flags & Document.FLAG_DIR_PREFERS_LAST_MODIFIED) != 0) {
143 result.sortOrder = State.SORT_ORDER_LAST_MODIFIED;
144 } else {
145 result.sortOrder = State.SORT_ORDER_DISPLAY_NAME;
146 }
147 }
148
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700149 // Search always uses ranking from provider
150 if (mType == DirectoryFragment.TYPE_SEARCH) {
151 result.sortOrder = State.SORT_ORDER_UNKNOWN;
152 }
153
Jeff Sharkeyd10f0492013-09-09 17:35:46 -0700154 Log.d(TAG, "userMode=" + userMode + ", userSortOrder=" + mUserSortOrder + " --> mode="
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700155 + result.mode + ", sortOrder=" + result.sortOrder);
156
157 try {
158 result.client = resolver.acquireUnstableContentProviderClient(authority);
159 cursor = result.client.query(
160 mUri, null, null, null, getQuerySortOrder(result.sortOrder), mSignal);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700161 cursor.registerContentObserver(mObserver);
162
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700163 cursor = new RootCursorWrapper(mUri.getAuthority(), mRoot.rootId, cursor, -1);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700164
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -0700165 if (mType == DirectoryFragment.TYPE_SEARCH) {
166 // Filter directories out of search results, for now
167 cursor = new FilteringCursorWrapper(cursor, null, new String[] {
168 Document.MIME_TYPE_DIR });
169 } else {
170 // Normal directories should have sorting applied
171 cursor = new SortingCursorWrapper(cursor, result.sortOrder);
172 }
173
174 result.cursor = cursor;
Jeff Sharkey1d890e02013-08-15 11:24:03 -0700175 } catch (Exception e) {
Jeff Sharkey0e8c8712013-09-12 21:59:06 -0700176 Log.w(TAG, "Failed to query", e);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700177 result.exception = e;
178 ContentProviderClient.closeQuietly(result.client);
179 } finally {
180 synchronized (this) {
181 mSignal = null;
182 }
Jeff Sharkey1d890e02013-08-15 11:24:03 -0700183 }
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700184
Jeff Sharkey46899c82013-08-18 22:26:48 -0700185 return result;
Jeff Sharkey1d890e02013-08-15 11:24:03 -0700186 }
187
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700188 @Override
189 public void cancelLoadInBackground() {
190 super.cancelLoadInBackground();
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700191
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700192 synchronized (this) {
193 if (mSignal != null) {
194 mSignal.cancel();
Jeff Sharkey46899c82013-08-18 22:26:48 -0700195 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700196 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700197 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700198
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700199 @Override
200 public void deliverResult(DirectoryResult result) {
201 if (isReset()) {
202 IoUtils.closeQuietly(result);
203 return;
204 }
205 DirectoryResult oldResult = mResult;
206 mResult = result;
207
208 if (isStarted()) {
209 super.deliverResult(result);
210 }
211
212 if (oldResult != null && oldResult != result) {
213 IoUtils.closeQuietly(oldResult);
214 }
215 }
216
217 @Override
218 protected void onStartLoading() {
219 if (mResult != null) {
220 deliverResult(mResult);
221 }
222 if (takeContentChanged() || mResult == null) {
223 forceLoad();
224 }
225 }
226
227 @Override
228 protected void onStopLoading() {
229 cancelLoad();
230 }
231
232 @Override
233 public void onCanceled(DirectoryResult result) {
234 IoUtils.closeQuietly(result);
235 }
236
237 @Override
238 protected void onReset() {
239 super.onReset();
240
241 // Ensure the loader is stopped
242 onStopLoading();
243
244 IoUtils.closeQuietly(mResult);
245 mResult = null;
246
247 getContext().getContentResolver().unregisterContentObserver(mObserver);
248 }
249
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700250 public static String getQuerySortOrder(int sortOrder) {
251 switch (sortOrder) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700252 case SORT_ORDER_DISPLAY_NAME:
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700253 return Document.COLUMN_DISPLAY_NAME + " ASC";
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700254 case SORT_ORDER_LAST_MODIFIED:
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700255 return Document.COLUMN_LAST_MODIFIED + " DESC";
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700256 case SORT_ORDER_SIZE:
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700257 return Document.COLUMN_SIZE + " DESC";
258 default:
259 return null;
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700260 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700261 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700262}