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; |
| 35 | import android.provider.DocumentsContract.Document; |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame^] | 36 | import android.util.Log; |
| 37 | |
| 38 | import com.android.documentsui.DocumentsActivity.State; |
| 39 | import com.android.documentsui.RecentsProvider.StateColumns; |
| 40 | import com.android.documentsui.model.DocumentInfo; |
| 41 | import com.android.documentsui.model.RootInfo; |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 42 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 43 | import libcore.io.IoUtils; |
| 44 | |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 45 | class DirectoryResult implements AutoCloseable { |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 46 | ContentProviderClient client; |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 47 | Cursor cursor; |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 48 | Exception exception; |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 49 | |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame^] | 50 | int mode = MODE_UNKNOWN; |
| 51 | int sortOrder = SORT_ORDER_UNKNOWN; |
| 52 | |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 53 | @Override |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 54 | public void close() { |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 55 | IoUtils.closeQuietly(cursor); |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 56 | ContentProviderClient.closeQuietly(client); |
| 57 | cursor = null; |
| 58 | client = null; |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 62 | public class DirectoryLoader extends AsyncTaskLoader<DirectoryResult> { |
| 63 | private final ForceLoadContentObserver mObserver = new ForceLoadContentObserver(); |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 64 | |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame^] | 65 | private final RootInfo mRoot; |
| 66 | private final DocumentInfo mDoc; |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 67 | private final Uri mUri; |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 68 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 69 | private CancellationSignal mSignal; |
| 70 | private DirectoryResult mResult; |
| 71 | |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame^] | 72 | public DirectoryLoader(Context context, RootInfo root, DocumentInfo doc, Uri uri) { |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 73 | super(context); |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame^] | 74 | mRoot = root; |
| 75 | mDoc = doc; |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 76 | mUri = uri; |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | @Override |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 80 | public final DirectoryResult loadInBackground() { |
| 81 | synchronized (this) { |
| 82 | if (isLoadInBackgroundCanceled()) { |
| 83 | throw new OperationCanceledException(); |
| 84 | } |
| 85 | mSignal = new CancellationSignal(); |
| 86 | } |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame^] | 87 | |
| 88 | final ContentResolver resolver = getContext().getContentResolver(); |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 89 | final String authority = mUri.getAuthority(); |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame^] | 90 | |
| 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 Sharkey | 1d890e0 | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 98 | try { |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame^] | 99 | 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 Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 137 | cursor.registerContentObserver(mObserver); |
| 138 | |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame^] | 139 | final Cursor withRoot = new RootCursorWrapper( |
| 140 | mUri.getAuthority(), mRoot.rootId, cursor, -1); |
| 141 | final Cursor sorted = new SortingCursorWrapper(withRoot, result.sortOrder); |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 142 | |
| 143 | result.cursor = sorted; |
Jeff Sharkey | 1d890e0 | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 144 | } catch (Exception e) { |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame^] | 145 | Log.d(TAG, "Failed to query", e); |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 146 | result.exception = e; |
| 147 | ContentProviderClient.closeQuietly(result.client); |
| 148 | } finally { |
| 149 | synchronized (this) { |
| 150 | mSignal = null; |
| 151 | } |
Jeff Sharkey | 1d890e0 | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 152 | } |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame^] | 153 | |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 154 | return result; |
Jeff Sharkey | 1d890e0 | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 157 | @Override |
| 158 | public void cancelLoadInBackground() { |
| 159 | super.cancelLoadInBackground(); |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 160 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 161 | synchronized (this) { |
| 162 | if (mSignal != null) { |
| 163 | mSignal.cancel(); |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 164 | } |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 165 | } |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 166 | } |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 167 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 168 | @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 Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 219 | public static String getQuerySortOrder(int sortOrder) { |
| 220 | switch (sortOrder) { |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 221 | case SORT_ORDER_DISPLAY_NAME: |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 222 | return Document.COLUMN_DISPLAY_NAME + " ASC"; |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 223 | case SORT_ORDER_LAST_MODIFIED: |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 224 | return Document.COLUMN_LAST_MODIFIED + " DESC"; |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 225 | case SORT_ORDER_SIZE: |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 226 | return Document.COLUMN_SIZE + " DESC"; |
| 227 | default: |
| 228 | return null; |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 229 | } |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 230 | } |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 231 | } |