blob: 4e46d5716c7b02670daf785e990f2938c439f977 [file] [log] [blame]
Jeff Hamilton9911b7f2010-05-15 02:20:31 -05001/*
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
17package android.content;
18
Mathew Inwood1c77a112018-08-14 14:06:26 +010019import android.annotation.UnsupportedAppUsage;
Jeff Hamilton9911b7f2010-05-15 02:20:31 -050020import android.database.Cursor;
21import android.net.Uri;
Jeff Browna7771df2012-05-07 20:06:46 -070022import android.os.CancellationSignal;
23import android.os.OperationCanceledException;
Jeff Hamilton9911b7f2010-05-15 02:20:31 -050024
Dianne Hackborna2ea7472010-12-20 12:10:01 -080025import java.io.FileDescriptor;
26import java.io.PrintWriter;
27import java.util.Arrays;
28
Jeff Hamilton9911b7f2010-05-15 02:20:31 -050029/**
30 * A loader that queries the {@link ContentResolver} and returns a {@link Cursor}.
Dianne Hackborn327fbd22011-01-17 14:38:50 -080031 * 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[])}.
Ian Lake0a1feb82017-11-13 10:26:46 -080042 *
Ian Lake1f4e67b2017-12-18 10:36:18 -080043 * @deprecated Use the <a href="{@docRoot}tools/extras/support-library.html">Support Library</a>
44 * {@link android.support.v4.content.CursorLoader}
Jeff Hamilton9911b7f2010-05-15 02:20:31 -050045 */
Ian Lake0a1feb82017-11-13 10:26:46 -080046@Deprecated
Jeff Hamilton9911b7f2010-05-15 02:20:31 -050047public class CursorLoader extends AsyncTaskLoader<Cursor> {
Mathew Inwood1c77a112018-08-14 14:06:26 +010048 @UnsupportedAppUsage
Dianne Hackborna2ea7472010-12-20 12:10:01 -080049 final ForceLoadContentObserver mObserver;
50
Jeff Hamilton9911b7f2010-05-15 02:20:31 -050051 Uri mUri;
52 String[] mProjection;
53 String mSelection;
54 String[] mSelectionArgs;
55 String mSortOrder;
56
Dianne Hackborna2ea7472010-12-20 12:10:01 -080057 Cursor mCursor;
Mathew Inwood1c77a112018-08-14 14:06:26 +010058 @UnsupportedAppUsage
Jeff Brown4c1241d2012-02-02 17:05:00 -080059 CancellationSignal mCancellationSignal;
Dianne Hackborna2ea7472010-12-20 12:10:01 -080060
Jeff Hamilton9911b7f2010-05-15 02:20:31 -050061 /* Runs on a worker thread */
62 @Override
63 public Cursor loadInBackground() {
Jeff Brown75ea64f2012-01-25 19:37:13 -080064 synchronized (this) {
65 if (isLoadInBackgroundCanceled()) {
66 throw new OperationCanceledException();
67 }
Jeff Brown4c1241d2012-02-02 17:05:00 -080068 mCancellationSignal = new CancellationSignal();
Jeff Hamilton9911b7f2010-05-15 02:20:31 -050069 }
Jeff Brown75ea64f2012-01-25 19:37:13 -080070 try {
71 Cursor cursor = getContext().getContentResolver().query(mUri, mProjection, mSelection,
Jeff Brown4c1241d2012-02-02 17:05:00 -080072 mSelectionArgs, mSortOrder, mCancellationSignal);
Jeff Brown75ea64f2012-01-25 19:37:13 -080073 if (cursor != null) {
Jeff Brownc21b5a02013-01-07 17:15:12 -080074 try {
75 // Ensure the cursor window is filled.
76 cursor.getCount();
Scott Kennedyda2223f2013-03-13 14:40:11 -070077 cursor.registerContentObserver(mObserver);
Jeff Brownc21b5a02013-01-07 17:15:12 -080078 } catch (RuntimeException ex) {
79 cursor.close();
80 throw ex;
81 }
Jeff Brown75ea64f2012-01-25 19:37:13 -080082 }
83 return cursor;
84 } finally {
85 synchronized (this) {
Jeff Brown4c1241d2012-02-02 17:05:00 -080086 mCancellationSignal = null;
Jeff Brown75ea64f2012-01-25 19:37:13 -080087 }
88 }
89 }
90
91 @Override
Jeff Brownb19a71a2012-01-31 11:48:39 -080092 public void cancelLoadInBackground() {
93 super.cancelLoadInBackground();
Jeff Brown75ea64f2012-01-25 19:37:13 -080094
95 synchronized (this) {
Jeff Brown4c1241d2012-02-02 17:05:00 -080096 if (mCancellationSignal != null) {
97 mCancellationSignal.cancel();
Jeff Brown75ea64f2012-01-25 19:37:13 -080098 }
99 }
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500100 }
101
102 /* Runs on the UI thread */
103 @Override
104 public void deliverResult(Cursor cursor) {
Dianne Hackborn0e3b8f422010-12-20 23:22:11 -0800105 if (isReset()) {
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500106 // An async query came in while the loader is stopped
Dmitri Plotnikov4565d522010-07-15 18:24:07 -0700107 if (cursor != null) {
Dianne Hackborn229edbc2011-10-09 16:01:40 -0700108 cursor.close();
Dmitri Plotnikov4565d522010-07-15 18:24:07 -0700109 }
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500110 return;
111 }
Ben Komalo8e6f69b2010-07-22 16:21:22 -0700112 Cursor oldCursor = mCursor;
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500113 mCursor = cursor;
Ben Komalo8e6f69b2010-07-22 16:21:22 -0700114
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800115 if (isStarted()) {
Dianne Hackbornc9189352010-12-15 14:57:25 -0800116 super.deliverResult(cursor);
117 }
118
119 if (oldCursor != null && oldCursor != cursor && !oldCursor.isClosed()) {
Dianne Hackborn229edbc2011-10-09 16:01:40 -0700120 oldCursor.close();
Ben Komalo8e6f69b2010-07-22 16:21:22 -0700121 }
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500122 }
123
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800124 /**
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 Hamilton9911b7f2010-05-15 02:20:31 -0500140 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 Kupolov96481de2017-11-17 15:30:03 -0800152 * Starts an asynchronous load of the data. When the result is ready the callbacks
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500153 * will be called on the UI thread. If a previous load has been completed and is still valid
Dmitri Plotnikovbef9c7a2010-06-16 15:38:07 -0700154 * the result may be passed to the callbacks immediately.
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500155 *
156 * Must be called from the UI thread
157 */
158 @Override
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800159 protected void onStartLoading() {
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500160 if (mCursor != null) {
161 deliverResult(mCursor);
Dianne Hackbornf73c75c2010-12-17 16:54:05 -0800162 }
Dianne Hackborn0e3b8f422010-12-20 23:22:11 -0800163 if (takeContentChanged() || mCursor == null) {
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500164 forceLoad();
165 }
166 }
167
168 /**
169 * Must be called from the UI thread
170 */
171 @Override
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800172 protected void onStopLoading() {
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500173 // Attempt to cancel the current load task if possible.
174 cancelLoad();
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500175 }
176
177 @Override
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800178 public void onCanceled(Cursor cursor) {
Dmitri Plotnikovbef9c7a2010-06-16 15:38:07 -0700179 if (cursor != null && !cursor.isClosed()) {
Dianne Hackborn229edbc2011-10-09 16:01:40 -0700180 cursor.close();
Dmitri Plotnikovbef9c7a2010-06-16 15:38:07 -0700181 }
182 }
183
184 @Override
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800185 protected void onReset() {
Dianne Hackborn0e3b8f422010-12-20 23:22:11 -0800186 super.onReset();
187
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500188 // Ensure the loader is stopped
Dianne Hackborn0e3b8f422010-12-20 23:22:11 -0800189 onStopLoading();
Dianne Hackbornc9189352010-12-15 14:57:25 -0800190
191 if (mCursor != null && !mCursor.isClosed()) {
Dianne Hackborn229edbc2011-10-09 16:01:40 -0700192 mCursor.close();
Dianne Hackbornc9189352010-12-15 14:57:25 -0800193 }
194 mCursor = null;
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500195 }
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 Hackborna2ea7472010-12-20 12:10:01 -0800236
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 Hamilton9911b7f2010-05-15 02:20:31 -0500250}