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