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