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 | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 19 | import static com.android.documentsui.DocumentsActivity.State.SORT_ORDER_DISPLAY_NAME; |
| 20 | import static com.android.documentsui.DocumentsActivity.State.SORT_ORDER_LAST_MODIFIED; |
| 21 | import static com.android.documentsui.DocumentsActivity.State.SORT_ORDER_SIZE; |
| 22 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 23 | import android.content.AsyncTaskLoader; |
| 24 | import android.content.ContentProviderClient; |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 25 | import android.content.Context; |
| 26 | import android.database.Cursor; |
| 27 | import android.net.Uri; |
| 28 | import android.os.CancellationSignal; |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 29 | import android.os.OperationCanceledException; |
| 30 | import android.provider.DocumentsContract.Document; |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 31 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 32 | import libcore.io.IoUtils; |
| 33 | |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 34 | class DirectoryResult implements AutoCloseable { |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 35 | ContentProviderClient client; |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 36 | Cursor cursor; |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 37 | Exception exception; |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 38 | |
| 39 | @Override |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 40 | public void close() { |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 41 | IoUtils.closeQuietly(cursor); |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 42 | ContentProviderClient.closeQuietly(client); |
| 43 | cursor = null; |
| 44 | client = null; |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 48 | public class DirectoryLoader extends AsyncTaskLoader<DirectoryResult> { |
| 49 | private final ForceLoadContentObserver mObserver = new ForceLoadContentObserver(); |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 50 | |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 51 | private final String mRootId; |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 52 | private final Uri mUri; |
| 53 | private final int mSortOrder; |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 54 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 55 | private CancellationSignal mSignal; |
| 56 | private DirectoryResult mResult; |
| 57 | |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 58 | public DirectoryLoader(Context context, String rootId, Uri uri, int sortOrder) { |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 59 | super(context); |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 60 | mRootId = rootId; |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 61 | mUri = uri; |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 62 | mSortOrder = sortOrder; |
| 63 | } |
| 64 | |
| 65 | @Override |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 66 | public final DirectoryResult loadInBackground() { |
| 67 | synchronized (this) { |
| 68 | if (isLoadInBackgroundCanceled()) { |
| 69 | throw new OperationCanceledException(); |
| 70 | } |
| 71 | mSignal = new CancellationSignal(); |
| 72 | } |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 73 | final DirectoryResult result = new DirectoryResult(); |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 74 | final String authority = mUri.getAuthority(); |
Jeff Sharkey | 1d890e0 | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 75 | try { |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 76 | result.client = getContext() |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 77 | .getContentResolver().acquireUnstableContentProviderClient(authority); |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 78 | final Cursor cursor = result.client.query( |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 79 | mUri, null, null, null, getQuerySortOrder(mSortOrder), mSignal); |
Jeff Sharkey | 20b3227 | 2013-09-03 15:25:52 -0700 | [diff] [blame^] | 80 | cursor.registerContentObserver(mObserver); |
| 81 | |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 82 | final Cursor withRoot = new RootCursorWrapper(mUri.getAuthority(), mRootId, cursor, -1); |
| 83 | final Cursor sorted = new SortingCursorWrapper(withRoot, mSortOrder); |
| 84 | |
| 85 | result.cursor = sorted; |
Jeff Sharkey | 1d890e0 | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 86 | } catch (Exception e) { |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 87 | result.exception = e; |
| 88 | ContentProviderClient.closeQuietly(result.client); |
| 89 | } finally { |
| 90 | synchronized (this) { |
| 91 | mSignal = null; |
| 92 | } |
Jeff Sharkey | 1d890e0 | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 93 | } |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 94 | return result; |
Jeff Sharkey | 1d890e0 | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 97 | @Override |
| 98 | public void cancelLoadInBackground() { |
| 99 | super.cancelLoadInBackground(); |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 100 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 101 | synchronized (this) { |
| 102 | if (mSignal != null) { |
| 103 | mSignal.cancel(); |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame] | 104 | } |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 105 | } |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 106 | } |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 107 | |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 108 | @Override |
| 109 | public void deliverResult(DirectoryResult result) { |
| 110 | if (isReset()) { |
| 111 | IoUtils.closeQuietly(result); |
| 112 | return; |
| 113 | } |
| 114 | DirectoryResult oldResult = mResult; |
| 115 | mResult = result; |
| 116 | |
| 117 | if (isStarted()) { |
| 118 | super.deliverResult(result); |
| 119 | } |
| 120 | |
| 121 | if (oldResult != null && oldResult != result) { |
| 122 | IoUtils.closeQuietly(oldResult); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | protected void onStartLoading() { |
| 128 | if (mResult != null) { |
| 129 | deliverResult(mResult); |
| 130 | } |
| 131 | if (takeContentChanged() || mResult == null) { |
| 132 | forceLoad(); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | protected void onStopLoading() { |
| 138 | cancelLoad(); |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public void onCanceled(DirectoryResult result) { |
| 143 | IoUtils.closeQuietly(result); |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | protected void onReset() { |
| 148 | super.onReset(); |
| 149 | |
| 150 | // Ensure the loader is stopped |
| 151 | onStopLoading(); |
| 152 | |
| 153 | IoUtils.closeQuietly(mResult); |
| 154 | mResult = null; |
| 155 | |
| 156 | getContext().getContentResolver().unregisterContentObserver(mObserver); |
| 157 | } |
| 158 | |
Jeff Sharkey | d82b26b | 2013-09-02 15:07:28 -0700 | [diff] [blame] | 159 | public static String getQuerySortOrder(int sortOrder) { |
| 160 | switch (sortOrder) { |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 161 | case SORT_ORDER_DISPLAY_NAME: |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 162 | return Document.COLUMN_DISPLAY_NAME + " ASC"; |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 163 | case SORT_ORDER_LAST_MODIFIED: |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 164 | return Document.COLUMN_LAST_MODIFIED + " DESC"; |
Jeff Sharkey | b362044 | 2013-09-01 18:41:04 -0700 | [diff] [blame] | 165 | case SORT_ORDER_SIZE: |
Jeff Sharkey | ac9e627 | 2013-08-31 21:27:44 -0700 | [diff] [blame] | 166 | return Document.COLUMN_SIZE + " DESC"; |
| 167 | default: |
| 168 | return null; |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 169 | } |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 170 | } |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 171 | } |