Jeff Sharkey | ef7184a | 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 | |
Steve McKay | 3eb2d07 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 19 | import static com.android.documentsui.Shared.DEBUG; |
Steve McKay | 58efce3 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 20 | import static com.android.documentsui.Shared.TAG; |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 21 | import static com.android.documentsui.State.SORT_ORDER_DISPLAY_NAME; |
| 22 | import static com.android.documentsui.State.SORT_ORDER_LAST_MODIFIED; |
| 23 | import static com.android.documentsui.State.SORT_ORDER_SIZE; |
Jeff Sharkey | b513311 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 24 | |
Jeff Sharkey | 5dfb345 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 25 | import android.content.AsyncTaskLoader; |
| 26 | import android.content.ContentProviderClient; |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 27 | import android.content.ContentResolver; |
Jeff Sharkey | ef7184a | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 28 | import android.content.Context; |
| 29 | import android.database.Cursor; |
| 30 | import android.net.Uri; |
| 31 | import android.os.CancellationSignal; |
Jeff Sharkey | 5dfb345 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 32 | import android.os.OperationCanceledException; |
Makoto Onuki | 14a6df7 | 2015-07-01 14:55:14 -0700 | [diff] [blame] | 33 | import android.os.RemoteException; |
Jeff Sharkey | 3e1189b | 2013-09-12 21:59:06 -0700 | [diff] [blame] | 34 | import android.provider.DocumentsContract; |
Jeff Sharkey | 5dfb345 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 35 | import android.provider.DocumentsContract.Document; |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 36 | import android.util.Log; |
| 37 | |
Steve McKay | f862155 | 2015-11-03 15:23:16 -0800 | [diff] [blame] | 38 | import com.android.documentsui.dirlist.DirectoryFragment; |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 39 | import com.android.documentsui.model.DocumentInfo; |
| 40 | import com.android.documentsui.model.RootInfo; |
Jeff Sharkey | ef7184a | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 41 | |
Jeff Sharkey | ef7184a | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 42 | import libcore.io.IoUtils; |
| 43 | |
Jeff Sharkey | 3e1189b | 2013-09-12 21:59:06 -0700 | [diff] [blame] | 44 | import java.io.FileNotFoundException; |
| 45 | |
Jeff Sharkey | 5dfb345 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 46 | public class DirectoryLoader extends AsyncTaskLoader<DirectoryResult> { |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 47 | |
| 48 | private static final String[] SEARCH_REJECT_MIMES = new String[] { Document.MIME_TYPE_DIR }; |
| 49 | |
Jeff Sharkey | 5dfb345 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 50 | private final ForceLoadContentObserver mObserver = new ForceLoadContentObserver(); |
Jeff Sharkey | ef7184a | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 51 | |
Jeff Sharkey | 4ec9739 | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 52 | private final int mType; |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 53 | private final RootInfo mRoot; |
Jeff Sharkey | 5dfb345 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 54 | private final Uri mUri; |
Jeff Sharkey | fb3445c | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 55 | private final int mUserSortOrder; |
Jeff Sharkey | ef7184a | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 56 | |
Steve McKay | 3eb2d07 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 57 | private DocumentInfo mDoc; |
Jeff Sharkey | 5dfb345 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 58 | private CancellationSignal mSignal; |
| 59 | private DirectoryResult mResult; |
| 60 | |
Jeff Sharkey | 4ec9739 | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 61 | public DirectoryLoader(Context context, int type, RootInfo root, DocumentInfo doc, Uri uri, |
| 62 | int userSortOrder) { |
Jeff Sharkey | d01571e | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 63 | super(context, ProviderExecutor.forAuthority(root.authority)); |
Jeff Sharkey | 4ec9739 | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 64 | mType = type; |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 65 | mRoot = root; |
Jeff Sharkey | 5dfb345 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 66 | mUri = uri; |
Jeff Sharkey | fb3445c | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 67 | mUserSortOrder = userSortOrder; |
Steve McKay | 3eb2d07 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 68 | mDoc = doc; |
Jeff Sharkey | ef7184a | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | @Override |
Jeff Sharkey | 5dfb345 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 72 | public final DirectoryResult loadInBackground() { |
| 73 | synchronized (this) { |
| 74 | if (isLoadInBackgroundCanceled()) { |
| 75 | throw new OperationCanceledException(); |
| 76 | } |
| 77 | mSignal = new CancellationSignal(); |
| 78 | } |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 79 | |
| 80 | final ContentResolver resolver = getContext().getContentResolver(); |
Jeff Sharkey | 251097b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 81 | final String authority = mUri.getAuthority(); |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 82 | |
| 83 | final DirectoryResult result = new DirectoryResult(); |
| 84 | |
Jeff Sharkey | 3e1189b | 2013-09-12 21:59:06 -0700 | [diff] [blame] | 85 | // 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 Sharkey | fb3445c | 2013-09-09 17:35:46 -0700 | [diff] [blame] | 98 | if (mUserSortOrder != State.SORT_ORDER_UNKNOWN) { |
| 99 | result.sortOrder = mUserSortOrder; |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 100 | } 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 Sharkey | 4ec9739 | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 108 | // Search always uses ranking from provider |
| 109 | if (mType == DirectoryFragment.TYPE_SEARCH) { |
| 110 | result.sortOrder = State.SORT_ORDER_UNKNOWN; |
| 111 | } |
| 112 | |
Steve McKay | 3eb2d07 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 113 | if (DEBUG) |
| 114 | Log.d(TAG, "userSortOrder=" + mUserSortOrder + ", sortOrder=" + result.sortOrder); |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 115 | |
Jeff Sharkey | d01571e | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 116 | ContentProviderClient client = null; |
Steve McKay | 3eb2d07 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 117 | Cursor cursor = null; |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 118 | try { |
Jeff Sharkey | d01571e | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 119 | client = DocumentsApplication.acquireUnstableProviderOrThrow(resolver, authority); |
Jeff Sharkey | d01571e | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 120 | cursor = client.query( |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 121 | mUri, null, null, null, getQuerySortOrder(result.sortOrder), mSignal); |
Makoto Onuki | 14a6df7 | 2015-07-01 14:55:14 -0700 | [diff] [blame] | 122 | if (cursor == null) { |
| 123 | throw new RemoteException("Provider returned null"); |
| 124 | } |
| 125 | |
Jeff Sharkey | 954be02 | 2013-09-03 15:25:52 -0700 | [diff] [blame] | 126 | cursor.registerContentObserver(mObserver); |
| 127 | |
Jeff Sharkey | 4ec9739 | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 128 | cursor = new RootCursorWrapper(mUri.getAuthority(), mRoot.rootId, cursor, -1); |
Jeff Sharkey | 251097b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 129 | |
Jeff Sharkey | 4ec9739 | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 130 | if (mType == DirectoryFragment.TYPE_SEARCH) { |
| 131 | // Filter directories out of search results, for now |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 132 | cursor = new FilteringCursorWrapper(cursor, null, SEARCH_REJECT_MIMES); |
Jeff Sharkey | 4ec9739 | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Jeff Sharkey | d01571e | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 135 | result.client = client; |
Jeff Sharkey | 4ec9739 | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 136 | result.cursor = cursor; |
Jeff Sharkey | 54ca29a | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 137 | } catch (Exception e) { |
Jeff Sharkey | 3e1189b | 2013-09-12 21:59:06 -0700 | [diff] [blame] | 138 | Log.w(TAG, "Failed to query", e); |
Jeff Sharkey | 5dfb345 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 139 | result.exception = e; |
Jeff Sharkey | d01571e | 2013-10-01 17:57:41 -0700 | [diff] [blame] | 140 | ContentProviderClient.releaseQuietly(client); |
Jeff Sharkey | 5dfb345 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 141 | } finally { |
| 142 | synchronized (this) { |
| 143 | mSignal = null; |
| 144 | } |
Jeff Sharkey | 54ca29a | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 145 | } |
Jeff Sharkey | d182bb6 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 146 | |
Jeff Sharkey | b448660 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 147 | return result; |
Jeff Sharkey | 54ca29a | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Jeff Sharkey | 5dfb345 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 150 | @Override |
| 151 | public void cancelLoadInBackground() { |
| 152 | super.cancelLoadInBackground(); |
Jeff Sharkey | ef7184a | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 153 | |
Jeff Sharkey | 5dfb345 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 154 | synchronized (this) { |
| 155 | if (mSignal != null) { |
| 156 | mSignal.cancel(); |
Jeff Sharkey | b448660 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 157 | } |
Jeff Sharkey | ef7184a | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 158 | } |
Jeff Sharkey | 5dfb345 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 159 | } |
Jeff Sharkey | ef7184a | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 160 | |
Jeff Sharkey | 5dfb345 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 161 | @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 Sharkey | 251097b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 212 | public static String getQuerySortOrder(int sortOrder) { |
| 213 | switch (sortOrder) { |
Jeff Sharkey | b513311 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 214 | case SORT_ORDER_DISPLAY_NAME: |
Jeff Sharkey | 5dfb345 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 215 | return Document.COLUMN_DISPLAY_NAME + " ASC"; |
Jeff Sharkey | b513311 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 216 | case SORT_ORDER_LAST_MODIFIED: |
Jeff Sharkey | 5dfb345 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 217 | return Document.COLUMN_LAST_MODIFIED + " DESC"; |
Jeff Sharkey | b513311 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 218 | case SORT_ORDER_SIZE: |
Jeff Sharkey | 5dfb345 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 219 | return Document.COLUMN_SIZE + " DESC"; |
| 220 | default: |
| 221 | return null; |
Jeff Sharkey | ef7184a | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 222 | } |
Jeff Sharkey | ef7184a | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 223 | } |
Jeff Sharkey | ef7184a | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 224 | } |