Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.documentsui; |
| 18 | |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 19 | import static com.android.documentsui.DocumentsActivity.TAG; |
| 20 | import static com.android.documentsui.DocumentsActivity.State.MODE_UNKNOWN; |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 21 | import static com.android.documentsui.DocumentsActivity.State.SORT_ORDER_DISPLAY_NAME; |
| 22 | import static com.android.documentsui.DocumentsActivity.State.SORT_ORDER_LAST_MODIFIED; |
| 23 | import static com.android.documentsui.DocumentsActivity.State.SORT_ORDER_SIZE; |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 24 | import static com.android.documentsui.DocumentsActivity.State.SORT_ORDER_UNKNOWN; |
| 25 | import static com.android.documentsui.model.DocumentInfo.getCursorInt; |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 26 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 27 | import android.content.AsyncTaskLoader; |
| 28 | import android.content.ContentProviderClient; |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 29 | import android.content.ContentResolver; |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 30 | import android.content.Context; |
| 31 | import android.database.Cursor; |
| 32 | import android.net.Uri; |
| 33 | import android.os.CancellationSignal; |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 34 | import android.os.OperationCanceledException; |
Jeff Sharkey | 0e8c871 | 2013-09-12 21:59:06 -0700 | [diff] [blame] | 35 | import android.provider.DocumentsContract; |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 36 | import android.provider.DocumentsContract.Document; |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 37 | import android.util.Log; |
| 38 | |
| 39 | import com.android.documentsui.DocumentsActivity.State; |
| 40 | import com.android.documentsui.RecentsProvider.StateColumns; |
| 41 | import com.android.documentsui.model.DocumentInfo; |
| 42 | import com.android.documentsui.model.RootInfo; |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 43 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 44 | import libcore.io.IoUtils; |
| 45 | |
Jeff Sharkey | 0e8c871 | 2013-09-12 21:59:06 -0700 | [diff] [blame] | 46 | import java.io.FileNotFoundException; |
| 47 | |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 48 | class DirectoryResult implements AutoCloseable { |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 49 | ContentProviderClient client; |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 50 | Cursor cursor; |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 51 | Exception exception; |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 52 | |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 53 | int mode = MODE_UNKNOWN; |
| 54 | int sortOrder = SORT_ORDER_UNKNOWN; |
| 55 | |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 56 | @Override |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 57 | public void close() { |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 58 | IoUtils.closeQuietly(cursor); |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 59 | ContentProviderClient.releaseQuietly(client); |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 60 | cursor = null; |
| 61 | client = null; |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 65 | public class DirectoryLoader extends AsyncTaskLoader<DirectoryResult> { |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 66 | |
| 67 | private static final String[] SEARCH_REJECT_MIMES = new String[] { Document.MIME_TYPE_DIR }; |
| 68 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 69 | private final ForceLoadContentObserver mObserver = new ForceLoadContentObserver(); |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 70 | |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 71 | private final int mType; |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 72 | private final RootInfo mRoot; |
Jeff Sharkey | 0e8c871 | 2013-09-12 21:59:06 -0700 | [diff] [blame] | 73 | private DocumentInfo mDoc; |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 74 | private final Uri mUri; |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 75 | private final int mUserSortOrder; |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 76 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 77 | private CancellationSignal mSignal; |
| 78 | private DirectoryResult mResult; |
| 79 | |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 80 | public DirectoryLoader(Context context, int type, RootInfo root, DocumentInfo doc, Uri uri, |
| 81 | int userSortOrder) { |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 82 | super(context); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 83 | mType = type; |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 84 | mRoot = root; |
| 85 | mDoc = doc; |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 86 | mUri = uri; |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 87 | mUserSortOrder = userSortOrder; |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | @Override |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 91 | public final DirectoryResult loadInBackground() { |
| 92 | synchronized (this) { |
| 93 | if (isLoadInBackgroundCanceled()) { |
| 94 | throw new OperationCanceledException(); |
| 95 | } |
| 96 | mSignal = new CancellationSignal(); |
| 97 | } |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 98 | |
| 99 | final ContentResolver resolver = getContext().getContentResolver(); |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 100 | final String authority = mUri.getAuthority(); |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 101 | |
| 102 | final DirectoryResult result = new DirectoryResult(); |
| 103 | |
| 104 | int userMode = State.MODE_UNKNOWN; |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 105 | |
Jeff Sharkey | 0e8c871 | 2013-09-12 21:59:06 -0700 | [diff] [blame] | 106 | // Use default document when searching |
| 107 | if (mType == DirectoryFragment.TYPE_SEARCH) { |
| 108 | final Uri docUri = DocumentsContract.buildDocumentUri( |
| 109 | mRoot.authority, mRoot.documentId); |
| 110 | try { |
| 111 | mDoc = DocumentInfo.fromUri(resolver, docUri); |
| 112 | } catch (FileNotFoundException e) { |
| 113 | Log.w(TAG, "Failed to query", e); |
| 114 | result.exception = e; |
| 115 | return result; |
| 116 | } |
| 117 | } |
| 118 | |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 119 | // Pick up any custom modes requested by user |
| 120 | Cursor cursor = null; |
Jeff Sharkey | 1d890e0 | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 121 | try { |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 122 | final Uri stateUri = RecentsProvider.buildState( |
| 123 | mRoot.authority, mRoot.rootId, mDoc.documentId); |
| 124 | cursor = resolver.query(stateUri, null, null, null, null); |
| 125 | if (cursor.moveToFirst()) { |
| 126 | userMode = getCursorInt(cursor, StateColumns.MODE); |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 127 | } |
| 128 | } finally { |
| 129 | IoUtils.closeQuietly(cursor); |
| 130 | } |
| 131 | |
| 132 | if (userMode != State.MODE_UNKNOWN) { |
| 133 | result.mode = userMode; |
| 134 | } else { |
| 135 | if ((mDoc.flags & Document.FLAG_DIR_PREFERS_GRID) != 0) { |
| 136 | result.mode = State.MODE_GRID; |
| 137 | } else { |
| 138 | result.mode = State.MODE_LIST; |
| 139 | } |
| 140 | } |
| 141 | |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 142 | if (mUserSortOrder != State.SORT_ORDER_UNKNOWN) { |
| 143 | result.sortOrder = mUserSortOrder; |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 144 | } else { |
| 145 | if ((mDoc.flags & Document.FLAG_DIR_PREFERS_LAST_MODIFIED) != 0) { |
| 146 | result.sortOrder = State.SORT_ORDER_LAST_MODIFIED; |
| 147 | } else { |
| 148 | result.sortOrder = State.SORT_ORDER_DISPLAY_NAME; |
| 149 | } |
| 150 | } |
| 151 | |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 152 | // Search always uses ranking from provider |
| 153 | if (mType == DirectoryFragment.TYPE_SEARCH) { |
| 154 | result.sortOrder = State.SORT_ORDER_UNKNOWN; |
| 155 | } |
| 156 | |
Jeff Sharkey | d10f049 | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 157 | Log.d(TAG, "userMode=" + userMode + ", userSortOrder=" + mUserSortOrder + " --> mode=" |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 158 | + result.mode + ", sortOrder=" + result.sortOrder); |
| 159 | |
| 160 | try { |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 161 | result.client = DocumentsApplication.acquireUnstableProviderOrThrow( |
| 162 | resolver, authority); |
| 163 | |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 164 | cursor = result.client.query( |
| 165 | mUri, null, null, null, getQuerySortOrder(result.sortOrder), mSignal); |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 166 | cursor.registerContentObserver(mObserver); |
| 167 | |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 168 | cursor = new RootCursorWrapper(mUri.getAuthority(), mRoot.rootId, cursor, -1); |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 169 | |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 170 | if (mType == DirectoryFragment.TYPE_SEARCH) { |
| 171 | // Filter directories out of search results, for now |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 172 | cursor = new FilteringCursorWrapper(cursor, null, SEARCH_REJECT_MIMES); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 173 | } else { |
| 174 | // Normal directories should have sorting applied |
| 175 | cursor = new SortingCursorWrapper(cursor, result.sortOrder); |
| 176 | } |
| 177 | |
| 178 | result.cursor = cursor; |
Jeff Sharkey | 1d890e0 | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 179 | } catch (Exception e) { |
Jeff Sharkey | 0e8c871 | 2013-09-12 21:59:06 -0700 | [diff] [blame] | 180 | Log.w(TAG, "Failed to query", e); |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 181 | result.exception = e; |
Jeff Sharkey | 3fd1177 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 182 | ContentProviderClient.releaseQuietly(result.client); |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 183 | } finally { |
| 184 | synchronized (this) { |
| 185 | mSignal = null; |
| 186 | } |
Jeff Sharkey | 1d890e0 | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 187 | } |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 188 | |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 189 | return result; |
Jeff Sharkey | 1d890e0 | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 192 | @Override |
| 193 | public void cancelLoadInBackground() { |
| 194 | super.cancelLoadInBackground(); |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 195 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 196 | synchronized (this) { |
| 197 | if (mSignal != null) { |
| 198 | mSignal.cancel(); |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 199 | } |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 200 | } |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 201 | } |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 202 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 203 | @Override |
| 204 | public void deliverResult(DirectoryResult result) { |
| 205 | if (isReset()) { |
| 206 | IoUtils.closeQuietly(result); |
| 207 | return; |
| 208 | } |
| 209 | DirectoryResult oldResult = mResult; |
| 210 | mResult = result; |
| 211 | |
| 212 | if (isStarted()) { |
| 213 | super.deliverResult(result); |
| 214 | } |
| 215 | |
| 216 | if (oldResult != null && oldResult != result) { |
| 217 | IoUtils.closeQuietly(oldResult); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | @Override |
| 222 | protected void onStartLoading() { |
| 223 | if (mResult != null) { |
| 224 | deliverResult(mResult); |
| 225 | } |
| 226 | if (takeContentChanged() || mResult == null) { |
| 227 | forceLoad(); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | @Override |
| 232 | protected void onStopLoading() { |
| 233 | cancelLoad(); |
| 234 | } |
| 235 | |
| 236 | @Override |
| 237 | public void onCanceled(DirectoryResult result) { |
| 238 | IoUtils.closeQuietly(result); |
| 239 | } |
| 240 | |
| 241 | @Override |
| 242 | protected void onReset() { |
| 243 | super.onReset(); |
| 244 | |
| 245 | // Ensure the loader is stopped |
| 246 | onStopLoading(); |
| 247 | |
| 248 | IoUtils.closeQuietly(mResult); |
| 249 | mResult = null; |
| 250 | |
| 251 | getContext().getContentResolver().unregisterContentObserver(mObserver); |
| 252 | } |
| 253 | |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 254 | public static String getQuerySortOrder(int sortOrder) { |
| 255 | switch (sortOrder) { |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 256 | case SORT_ORDER_DISPLAY_NAME: |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 257 | return Document.COLUMN_DISPLAY_NAME + " ASC"; |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 258 | case SORT_ORDER_LAST_MODIFIED: |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 259 | return Document.COLUMN_LAST_MODIFIED + " DESC"; |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 260 | case SORT_ORDER_SIZE: |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 261 | return Document.COLUMN_SIZE + " DESC"; |
| 262 | default: |
| 263 | return null; |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 264 | } |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 265 | } |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 266 | } |