blob: 72dfa30377ecefcb07e2a1419d521baca8c4d19d [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;
35import android.provider.DocumentsContract.Document;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070036import android.util.Log;
37
38import com.android.documentsui.DocumentsActivity.State;
39import com.android.documentsui.RecentsProvider.StateColumns;
40import com.android.documentsui.model.DocumentInfo;
41import com.android.documentsui.model.RootInfo;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070042
Jeff Sharkeya5defe32013-08-05 17:56:48 -070043import libcore.io.IoUtils;
44
Jeff Sharkey46899c82013-08-18 22:26:48 -070045class DirectoryResult implements AutoCloseable {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070046 ContentProviderClient client;
Jeff Sharkey46899c82013-08-18 22:26:48 -070047 Cursor cursor;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070048 Exception exception;
Jeff Sharkey46899c82013-08-18 22:26:48 -070049
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070050 int mode = MODE_UNKNOWN;
51 int sortOrder = SORT_ORDER_UNKNOWN;
52
Jeff Sharkey46899c82013-08-18 22:26:48 -070053 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070054 public void close() {
Jeff Sharkey46899c82013-08-18 22:26:48 -070055 IoUtils.closeQuietly(cursor);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070056 ContentProviderClient.closeQuietly(client);
57 cursor = null;
58 client = null;
Jeff Sharkey46899c82013-08-18 22:26:48 -070059 }
60}
61
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070062public class DirectoryLoader extends AsyncTaskLoader<DirectoryResult> {
63 private final ForceLoadContentObserver mObserver = new ForceLoadContentObserver();
Jeff Sharkeya5defe32013-08-05 17:56:48 -070064
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070065 private final RootInfo mRoot;
66 private final DocumentInfo mDoc;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070067 private final Uri mUri;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070068
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070069 private CancellationSignal mSignal;
70 private DirectoryResult mResult;
71
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070072 public DirectoryLoader(Context context, RootInfo root, DocumentInfo doc, Uri uri) {
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070073 super(context);
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070074 mRoot = root;
75 mDoc = doc;
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070076 mUri = uri;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070077 }
78
79 @Override
Jeff Sharkeyac9e6272013-08-31 21:27:44 -070080 public final DirectoryResult loadInBackground() {
81 synchronized (this) {
82 if (isLoadInBackgroundCanceled()) {
83 throw new OperationCanceledException();
84 }
85 mSignal = new CancellationSignal();
86 }
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070087
88 final ContentResolver resolver = getContext().getContentResolver();
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -070089 final String authority = mUri.getAuthority();
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070090
91 final DirectoryResult result = new DirectoryResult();
92
93 int userMode = State.MODE_UNKNOWN;
94 int userSortOrder = State.SORT_ORDER_UNKNOWN;
95
96 // Pick up any custom modes requested by user
97 Cursor cursor = null;
Jeff Sharkey1d890e02013-08-15 11:24:03 -070098 try {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070099 final Uri stateUri = RecentsProvider.buildState(
100 mRoot.authority, mRoot.rootId, mDoc.documentId);
101 cursor = resolver.query(stateUri, null, null, null, null);
102 if (cursor.moveToFirst()) {
103 userMode = getCursorInt(cursor, StateColumns.MODE);
104 userSortOrder = getCursorInt(cursor, StateColumns.SORT_ORDER);
105 }
106 } finally {
107 IoUtils.closeQuietly(cursor);
108 }
109
110 if (userMode != State.MODE_UNKNOWN) {
111 result.mode = userMode;
112 } else {
113 if ((mDoc.flags & Document.FLAG_DIR_PREFERS_GRID) != 0) {
114 result.mode = State.MODE_GRID;
115 } else {
116 result.mode = State.MODE_LIST;
117 }
118 }
119
120 if (userSortOrder != State.SORT_ORDER_UNKNOWN) {
121 result.sortOrder = userSortOrder;
122 } else {
123 if ((mDoc.flags & Document.FLAG_DIR_PREFERS_LAST_MODIFIED) != 0) {
124 result.sortOrder = State.SORT_ORDER_LAST_MODIFIED;
125 } else {
126 result.sortOrder = State.SORT_ORDER_DISPLAY_NAME;
127 }
128 }
129
130 Log.d(TAG, "userMode=" + userMode + ", userSortOrder=" + userSortOrder + " --> mode="
131 + result.mode + ", sortOrder=" + result.sortOrder);
132
133 try {
134 result.client = resolver.acquireUnstableContentProviderClient(authority);
135 cursor = result.client.query(
136 mUri, null, null, null, getQuerySortOrder(result.sortOrder), mSignal);
Jeff Sharkey20b32272013-09-03 15:25:52 -0700137 cursor.registerContentObserver(mObserver);
138
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700139 final Cursor withRoot = new RootCursorWrapper(
140 mUri.getAuthority(), mRoot.rootId, cursor, -1);
141 final Cursor sorted = new SortingCursorWrapper(withRoot, result.sortOrder);
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700142
143 result.cursor = sorted;
Jeff Sharkey1d890e02013-08-15 11:24:03 -0700144 } catch (Exception e) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700145 Log.d(TAG, "Failed to query", e);
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700146 result.exception = e;
147 ContentProviderClient.closeQuietly(result.client);
148 } finally {
149 synchronized (this) {
150 mSignal = null;
151 }
Jeff Sharkey1d890e02013-08-15 11:24:03 -0700152 }
Jeff Sharkeya4d1f222013-09-07 14:45:03 -0700153
Jeff Sharkey46899c82013-08-18 22:26:48 -0700154 return result;
Jeff Sharkey1d890e02013-08-15 11:24:03 -0700155 }
156
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700157 @Override
158 public void cancelLoadInBackground() {
159 super.cancelLoadInBackground();
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700160
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700161 synchronized (this) {
162 if (mSignal != null) {
163 mSignal.cancel();
Jeff Sharkey46899c82013-08-18 22:26:48 -0700164 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700165 }
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700166 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700167
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700168 @Override
169 public void deliverResult(DirectoryResult result) {
170 if (isReset()) {
171 IoUtils.closeQuietly(result);
172 return;
173 }
174 DirectoryResult oldResult = mResult;
175 mResult = result;
176
177 if (isStarted()) {
178 super.deliverResult(result);
179 }
180
181 if (oldResult != null && oldResult != result) {
182 IoUtils.closeQuietly(oldResult);
183 }
184 }
185
186 @Override
187 protected void onStartLoading() {
188 if (mResult != null) {
189 deliverResult(mResult);
190 }
191 if (takeContentChanged() || mResult == null) {
192 forceLoad();
193 }
194 }
195
196 @Override
197 protected void onStopLoading() {
198 cancelLoad();
199 }
200
201 @Override
202 public void onCanceled(DirectoryResult result) {
203 IoUtils.closeQuietly(result);
204 }
205
206 @Override
207 protected void onReset() {
208 super.onReset();
209
210 // Ensure the loader is stopped
211 onStopLoading();
212
213 IoUtils.closeQuietly(mResult);
214 mResult = null;
215
216 getContext().getContentResolver().unregisterContentObserver(mObserver);
217 }
218
Jeff Sharkeyd82b26b2013-09-02 15:07:28 -0700219 public static String getQuerySortOrder(int sortOrder) {
220 switch (sortOrder) {
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700221 case SORT_ORDER_DISPLAY_NAME:
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700222 return Document.COLUMN_DISPLAY_NAME + " ASC";
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700223 case SORT_ORDER_LAST_MODIFIED:
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700224 return Document.COLUMN_LAST_MODIFIED + " DESC";
Jeff Sharkeyb3620442013-09-01 18:41:04 -0700225 case SORT_ORDER_SIZE:
Jeff Sharkeyac9e6272013-08-31 21:27:44 -0700226 return Document.COLUMN_SIZE + " DESC";
227 default:
228 return null;
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700229 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700230 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700231}