Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 android.content; |
| 18 | |
Dmitri Plotnikov | 4d891e7 | 2010-09-22 11:04:10 -0700 | [diff] [blame] | 19 | import android.database.ContentObserver; |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 20 | import android.database.Cursor; |
| 21 | import android.net.Uri; |
Jeff Brown | a7771df | 2012-05-07 20:06:46 -0700 | [diff] [blame] | 22 | import android.os.CancellationSignal; |
| 23 | import android.os.OperationCanceledException; |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 24 | |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 25 | import java.io.FileDescriptor; |
| 26 | import java.io.PrintWriter; |
| 27 | import java.util.Arrays; |
| 28 | |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 29 | /** |
| 30 | * A loader that queries the {@link ContentResolver} and returns a {@link Cursor}. |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 31 | * This class implements the {@link Loader} protocol in a standard way for |
| 32 | * querying cursors, building on {@link AsyncTaskLoader} to perform the cursor |
| 33 | * query on a background thread so that it does not block the application's UI. |
| 34 | * |
| 35 | * <p>A CursorLoader must be built with the full information for the query to |
| 36 | * perform, either through the |
| 37 | * {@link #CursorLoader(Context, Uri, String[], String, String[], String)} or |
| 38 | * creating an empty instance with {@link #CursorLoader(Context)} and filling |
| 39 | * in the desired paramters with {@link #setUri(Uri)}, {@link #setSelection(String)}, |
| 40 | * {@link #setSelectionArgs(String[])}, {@link #setSortOrder(String)}, |
| 41 | * and {@link #setProjection(String[])}. |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 42 | */ |
| 43 | public class CursorLoader extends AsyncTaskLoader<Cursor> { |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 44 | final ForceLoadContentObserver mObserver; |
| 45 | |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 46 | Uri mUri; |
| 47 | String[] mProjection; |
| 48 | String mSelection; |
| 49 | String[] mSelectionArgs; |
| 50 | String mSortOrder; |
| 51 | |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 52 | Cursor mCursor; |
Jeff Brown | 4c1241d | 2012-02-02 17:05:00 -0800 | [diff] [blame] | 53 | CancellationSignal mCancellationSignal; |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 54 | |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 55 | /* Runs on a worker thread */ |
| 56 | @Override |
| 57 | public Cursor loadInBackground() { |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 58 | synchronized (this) { |
| 59 | if (isLoadInBackgroundCanceled()) { |
| 60 | throw new OperationCanceledException(); |
| 61 | } |
Jeff Brown | 4c1241d | 2012-02-02 17:05:00 -0800 | [diff] [blame] | 62 | mCancellationSignal = new CancellationSignal(); |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 63 | } |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 64 | try { |
| 65 | Cursor cursor = getContext().getContentResolver().query(mUri, mProjection, mSelection, |
Jeff Brown | 4c1241d | 2012-02-02 17:05:00 -0800 | [diff] [blame] | 66 | mSelectionArgs, mSortOrder, mCancellationSignal); |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 67 | if (cursor != null) { |
Jeff Brown | c21b5a0 | 2013-01-07 17:15:12 -0800 | [diff] [blame] | 68 | try { |
| 69 | // Ensure the cursor window is filled. |
| 70 | cursor.getCount(); |
Scott Kennedy | da2223f | 2013-03-13 14:40:11 -0700 | [diff] [blame] | 71 | cursor.registerContentObserver(mObserver); |
Jeff Brown | c21b5a0 | 2013-01-07 17:15:12 -0800 | [diff] [blame] | 72 | } catch (RuntimeException ex) { |
| 73 | cursor.close(); |
| 74 | throw ex; |
| 75 | } |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 76 | } |
| 77 | return cursor; |
| 78 | } finally { |
| 79 | synchronized (this) { |
Jeff Brown | 4c1241d | 2012-02-02 17:05:00 -0800 | [diff] [blame] | 80 | mCancellationSignal = null; |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | @Override |
Jeff Brown | b19a71a | 2012-01-31 11:48:39 -0800 | [diff] [blame] | 86 | public void cancelLoadInBackground() { |
| 87 | super.cancelLoadInBackground(); |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 88 | |
| 89 | synchronized (this) { |
Jeff Brown | 4c1241d | 2012-02-02 17:05:00 -0800 | [diff] [blame] | 90 | if (mCancellationSignal != null) { |
| 91 | mCancellationSignal.cancel(); |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 92 | } |
| 93 | } |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /* Runs on the UI thread */ |
| 97 | @Override |
| 98 | public void deliverResult(Cursor cursor) { |
Dianne Hackborn | 0e3b8f42 | 2010-12-20 23:22:11 -0800 | [diff] [blame] | 99 | if (isReset()) { |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 100 | // An async query came in while the loader is stopped |
Dmitri Plotnikov | 4565d52 | 2010-07-15 18:24:07 -0700 | [diff] [blame] | 101 | if (cursor != null) { |
Dianne Hackborn | 229edbc | 2011-10-09 16:01:40 -0700 | [diff] [blame] | 102 | cursor.close(); |
Dmitri Plotnikov | 4565d52 | 2010-07-15 18:24:07 -0700 | [diff] [blame] | 103 | } |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 104 | return; |
| 105 | } |
Ben Komalo | 8e6f69b | 2010-07-22 16:21:22 -0700 | [diff] [blame] | 106 | Cursor oldCursor = mCursor; |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 107 | mCursor = cursor; |
Ben Komalo | 8e6f69b | 2010-07-22 16:21:22 -0700 | [diff] [blame] | 108 | |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 109 | if (isStarted()) { |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 110 | super.deliverResult(cursor); |
| 111 | } |
| 112 | |
| 113 | if (oldCursor != null && oldCursor != cursor && !oldCursor.isClosed()) { |
Dianne Hackborn | 229edbc | 2011-10-09 16:01:40 -0700 | [diff] [blame] | 114 | oldCursor.close(); |
Ben Komalo | 8e6f69b | 2010-07-22 16:21:22 -0700 | [diff] [blame] | 115 | } |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 116 | } |
| 117 | |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 118 | /** |
| 119 | * Creates an empty unspecified CursorLoader. You must follow this with |
| 120 | * calls to {@link #setUri(Uri)}, {@link #setSelection(String)}, etc |
| 121 | * to specify the query to perform. |
| 122 | */ |
| 123 | public CursorLoader(Context context) { |
| 124 | super(context); |
| 125 | mObserver = new ForceLoadContentObserver(); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Creates a fully-specified CursorLoader. See |
| 130 | * {@link ContentResolver#query(Uri, String[], String, String[], String) |
| 131 | * ContentResolver.query()} for documentation on the meaning of the |
| 132 | * parameters. These will be passed as-is to that call. |
| 133 | */ |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 134 | public CursorLoader(Context context, Uri uri, String[] projection, String selection, |
| 135 | String[] selectionArgs, String sortOrder) { |
| 136 | super(context); |
| 137 | mObserver = new ForceLoadContentObserver(); |
| 138 | mUri = uri; |
| 139 | mProjection = projection; |
| 140 | mSelection = selection; |
| 141 | mSelectionArgs = selectionArgs; |
| 142 | mSortOrder = sortOrder; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Starts an asynchronous load of the contacts list data. When the result is ready the callbacks |
| 147 | * will be called on the UI thread. If a previous load has been completed and is still valid |
Dmitri Plotnikov | bef9c7a | 2010-06-16 15:38:07 -0700 | [diff] [blame] | 148 | * the result may be passed to the callbacks immediately. |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 149 | * |
| 150 | * Must be called from the UI thread |
| 151 | */ |
| 152 | @Override |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 153 | protected void onStartLoading() { |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 154 | if (mCursor != null) { |
| 155 | deliverResult(mCursor); |
Dianne Hackborn | f73c75c | 2010-12-17 16:54:05 -0800 | [diff] [blame] | 156 | } |
Dianne Hackborn | 0e3b8f42 | 2010-12-20 23:22:11 -0800 | [diff] [blame] | 157 | if (takeContentChanged() || mCursor == null) { |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 158 | forceLoad(); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Must be called from the UI thread |
| 164 | */ |
| 165 | @Override |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 166 | protected void onStopLoading() { |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 167 | // Attempt to cancel the current load task if possible. |
| 168 | cancelLoad(); |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | @Override |
Dianne Hackborn | 327fbd2 | 2011-01-17 14:38:50 -0800 | [diff] [blame] | 172 | public void onCanceled(Cursor cursor) { |
Dmitri Plotnikov | bef9c7a | 2010-06-16 15:38:07 -0700 | [diff] [blame] | 173 | if (cursor != null && !cursor.isClosed()) { |
Dianne Hackborn | 229edbc | 2011-10-09 16:01:40 -0700 | [diff] [blame] | 174 | cursor.close(); |
Dmitri Plotnikov | bef9c7a | 2010-06-16 15:38:07 -0700 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
| 178 | @Override |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 179 | protected void onReset() { |
Dianne Hackborn | 0e3b8f42 | 2010-12-20 23:22:11 -0800 | [diff] [blame] | 180 | super.onReset(); |
| 181 | |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 182 | // Ensure the loader is stopped |
Dianne Hackborn | 0e3b8f42 | 2010-12-20 23:22:11 -0800 | [diff] [blame] | 183 | onStopLoading(); |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 184 | |
| 185 | if (mCursor != null && !mCursor.isClosed()) { |
Dianne Hackborn | 229edbc | 2011-10-09 16:01:40 -0700 | [diff] [blame] | 186 | mCursor.close(); |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 187 | } |
| 188 | mCursor = null; |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | public Uri getUri() { |
| 192 | return mUri; |
| 193 | } |
| 194 | |
| 195 | public void setUri(Uri uri) { |
| 196 | mUri = uri; |
| 197 | } |
| 198 | |
| 199 | public String[] getProjection() { |
| 200 | return mProjection; |
| 201 | } |
| 202 | |
| 203 | public void setProjection(String[] projection) { |
| 204 | mProjection = projection; |
| 205 | } |
| 206 | |
| 207 | public String getSelection() { |
| 208 | return mSelection; |
| 209 | } |
| 210 | |
| 211 | public void setSelection(String selection) { |
| 212 | mSelection = selection; |
| 213 | } |
| 214 | |
| 215 | public String[] getSelectionArgs() { |
| 216 | return mSelectionArgs; |
| 217 | } |
| 218 | |
| 219 | public void setSelectionArgs(String[] selectionArgs) { |
| 220 | mSelectionArgs = selectionArgs; |
| 221 | } |
| 222 | |
| 223 | public String getSortOrder() { |
| 224 | return mSortOrder; |
| 225 | } |
| 226 | |
| 227 | public void setSortOrder(String sortOrder) { |
| 228 | mSortOrder = sortOrder; |
| 229 | } |
Dianne Hackborn | a2ea747 | 2010-12-20 12:10:01 -0800 | [diff] [blame] | 230 | |
| 231 | @Override |
| 232 | public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { |
| 233 | super.dump(prefix, fd, writer, args); |
| 234 | writer.print(prefix); writer.print("mUri="); writer.println(mUri); |
| 235 | writer.print(prefix); writer.print("mProjection="); |
| 236 | writer.println(Arrays.toString(mProjection)); |
| 237 | writer.print(prefix); writer.print("mSelection="); writer.println(mSelection); |
| 238 | writer.print(prefix); writer.print("mSelectionArgs="); |
| 239 | writer.println(Arrays.toString(mSelectionArgs)); |
| 240 | writer.print(prefix); writer.print("mSortOrder="); writer.println(mSortOrder); |
| 241 | writer.print(prefix); writer.print("mCursor="); writer.println(mCursor); |
| 242 | writer.print(prefix); writer.print("mContentChanged="); writer.println(mContentChanged); |
| 243 | } |
Jeff Hamilton | 9911b7f | 2010-05-15 02:20:31 -0500 | [diff] [blame] | 244 | } |