blob: 33386e5ce58a3ddabc93b9ebff6e51c59836a969 [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
19import android.database.Cursor;
20import android.net.Uri;
Jeff Browna7771df2012-05-07 20:06:46 -070021import android.os.CancellationSignal;
22import android.os.OperationCanceledException;
Jeff Hamilton9911b7f2010-05-15 02:20:31 -050023
Dianne Hackborna2ea7472010-12-20 12:10:01 -080024import java.io.FileDescriptor;
25import java.io.PrintWriter;
26import java.util.Arrays;
27
Jeff Hamilton9911b7f2010-05-15 02:20:31 -050028/**
29 * A loader that queries the {@link ContentResolver} and returns a {@link Cursor}.
Dianne Hackborn327fbd22011-01-17 14:38:50 -080030 * This class implements the {@link Loader} protocol in a standard way for
31 * querying cursors, building on {@link AsyncTaskLoader} to perform the cursor
32 * query on a background thread so that it does not block the application's UI.
33 *
34 * <p>A CursorLoader must be built with the full information for the query to
35 * perform, either through the
36 * {@link #CursorLoader(Context, Uri, String[], String, String[], String)} or
37 * creating an empty instance with {@link #CursorLoader(Context)} and filling
38 * in the desired paramters with {@link #setUri(Uri)}, {@link #setSelection(String)},
39 * {@link #setSelectionArgs(String[])}, {@link #setSortOrder(String)},
40 * and {@link #setProjection(String[])}.
Ian Lake0a1feb82017-11-13 10:26:46 -080041 *
42 * @deprecated Use {@link android.support.v4.content.CursorLoader}
Jeff Hamilton9911b7f2010-05-15 02:20:31 -050043 */
Ian Lake0a1feb82017-11-13 10:26:46 -080044@Deprecated
Jeff Hamilton9911b7f2010-05-15 02:20:31 -050045public class CursorLoader extends AsyncTaskLoader<Cursor> {
Dianne Hackborna2ea7472010-12-20 12:10:01 -080046 final ForceLoadContentObserver mObserver;
47
Jeff Hamilton9911b7f2010-05-15 02:20:31 -050048 Uri mUri;
49 String[] mProjection;
50 String mSelection;
51 String[] mSelectionArgs;
52 String mSortOrder;
53
Dianne Hackborna2ea7472010-12-20 12:10:01 -080054 Cursor mCursor;
Jeff Brown4c1241d2012-02-02 17:05:00 -080055 CancellationSignal mCancellationSignal;
Dianne Hackborna2ea7472010-12-20 12:10:01 -080056
Jeff Hamilton9911b7f2010-05-15 02:20:31 -050057 /* Runs on a worker thread */
58 @Override
59 public Cursor loadInBackground() {
Jeff Brown75ea64f2012-01-25 19:37:13 -080060 synchronized (this) {
61 if (isLoadInBackgroundCanceled()) {
62 throw new OperationCanceledException();
63 }
Jeff Brown4c1241d2012-02-02 17:05:00 -080064 mCancellationSignal = new CancellationSignal();
Jeff Hamilton9911b7f2010-05-15 02:20:31 -050065 }
Jeff Brown75ea64f2012-01-25 19:37:13 -080066 try {
67 Cursor cursor = getContext().getContentResolver().query(mUri, mProjection, mSelection,
Jeff Brown4c1241d2012-02-02 17:05:00 -080068 mSelectionArgs, mSortOrder, mCancellationSignal);
Jeff Brown75ea64f2012-01-25 19:37:13 -080069 if (cursor != null) {
Jeff Brownc21b5a02013-01-07 17:15:12 -080070 try {
71 // Ensure the cursor window is filled.
72 cursor.getCount();
Scott Kennedyda2223f2013-03-13 14:40:11 -070073 cursor.registerContentObserver(mObserver);
Jeff Brownc21b5a02013-01-07 17:15:12 -080074 } catch (RuntimeException ex) {
75 cursor.close();
76 throw ex;
77 }
Jeff Brown75ea64f2012-01-25 19:37:13 -080078 }
79 return cursor;
80 } finally {
81 synchronized (this) {
Jeff Brown4c1241d2012-02-02 17:05:00 -080082 mCancellationSignal = null;
Jeff Brown75ea64f2012-01-25 19:37:13 -080083 }
84 }
85 }
86
87 @Override
Jeff Brownb19a71a2012-01-31 11:48:39 -080088 public void cancelLoadInBackground() {
89 super.cancelLoadInBackground();
Jeff Brown75ea64f2012-01-25 19:37:13 -080090
91 synchronized (this) {
Jeff Brown4c1241d2012-02-02 17:05:00 -080092 if (mCancellationSignal != null) {
93 mCancellationSignal.cancel();
Jeff Brown75ea64f2012-01-25 19:37:13 -080094 }
95 }
Jeff Hamilton9911b7f2010-05-15 02:20:31 -050096 }
97
98 /* Runs on the UI thread */
99 @Override
100 public void deliverResult(Cursor cursor) {
Dianne Hackborn0e3b8f422010-12-20 23:22:11 -0800101 if (isReset()) {
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500102 // An async query came in while the loader is stopped
Dmitri Plotnikov4565d522010-07-15 18:24:07 -0700103 if (cursor != null) {
Dianne Hackborn229edbc2011-10-09 16:01:40 -0700104 cursor.close();
Dmitri Plotnikov4565d522010-07-15 18:24:07 -0700105 }
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500106 return;
107 }
Ben Komalo8e6f69b2010-07-22 16:21:22 -0700108 Cursor oldCursor = mCursor;
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500109 mCursor = cursor;
Ben Komalo8e6f69b2010-07-22 16:21:22 -0700110
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800111 if (isStarted()) {
Dianne Hackbornc9189352010-12-15 14:57:25 -0800112 super.deliverResult(cursor);
113 }
114
115 if (oldCursor != null && oldCursor != cursor && !oldCursor.isClosed()) {
Dianne Hackborn229edbc2011-10-09 16:01:40 -0700116 oldCursor.close();
Ben Komalo8e6f69b2010-07-22 16:21:22 -0700117 }
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500118 }
119
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800120 /**
121 * Creates an empty unspecified CursorLoader. You must follow this with
122 * calls to {@link #setUri(Uri)}, {@link #setSelection(String)}, etc
123 * to specify the query to perform.
124 */
125 public CursorLoader(Context context) {
126 super(context);
127 mObserver = new ForceLoadContentObserver();
128 }
129
130 /**
131 * Creates a fully-specified CursorLoader. See
132 * {@link ContentResolver#query(Uri, String[], String, String[], String)
133 * ContentResolver.query()} for documentation on the meaning of the
134 * parameters. These will be passed as-is to that call.
135 */
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500136 public CursorLoader(Context context, Uri uri, String[] projection, String selection,
137 String[] selectionArgs, String sortOrder) {
138 super(context);
139 mObserver = new ForceLoadContentObserver();
140 mUri = uri;
141 mProjection = projection;
142 mSelection = selection;
143 mSelectionArgs = selectionArgs;
144 mSortOrder = sortOrder;
145 }
146
147 /**
148 * Starts an asynchronous load of the contacts list data. When the result is ready the callbacks
149 * 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 -0700150 * the result may be passed to the callbacks immediately.
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500151 *
152 * Must be called from the UI thread
153 */
154 @Override
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800155 protected void onStartLoading() {
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500156 if (mCursor != null) {
157 deliverResult(mCursor);
Dianne Hackbornf73c75c2010-12-17 16:54:05 -0800158 }
Dianne Hackborn0e3b8f422010-12-20 23:22:11 -0800159 if (takeContentChanged() || mCursor == null) {
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500160 forceLoad();
161 }
162 }
163
164 /**
165 * Must be called from the UI thread
166 */
167 @Override
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800168 protected void onStopLoading() {
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500169 // Attempt to cancel the current load task if possible.
170 cancelLoad();
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500171 }
172
173 @Override
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800174 public void onCanceled(Cursor cursor) {
Dmitri Plotnikovbef9c7a2010-06-16 15:38:07 -0700175 if (cursor != null && !cursor.isClosed()) {
Dianne Hackborn229edbc2011-10-09 16:01:40 -0700176 cursor.close();
Dmitri Plotnikovbef9c7a2010-06-16 15:38:07 -0700177 }
178 }
179
180 @Override
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800181 protected void onReset() {
Dianne Hackborn0e3b8f422010-12-20 23:22:11 -0800182 super.onReset();
183
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500184 // Ensure the loader is stopped
Dianne Hackborn0e3b8f422010-12-20 23:22:11 -0800185 onStopLoading();
Dianne Hackbornc9189352010-12-15 14:57:25 -0800186
187 if (mCursor != null && !mCursor.isClosed()) {
Dianne Hackborn229edbc2011-10-09 16:01:40 -0700188 mCursor.close();
Dianne Hackbornc9189352010-12-15 14:57:25 -0800189 }
190 mCursor = null;
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500191 }
192
193 public Uri getUri() {
194 return mUri;
195 }
196
197 public void setUri(Uri uri) {
198 mUri = uri;
199 }
200
201 public String[] getProjection() {
202 return mProjection;
203 }
204
205 public void setProjection(String[] projection) {
206 mProjection = projection;
207 }
208
209 public String getSelection() {
210 return mSelection;
211 }
212
213 public void setSelection(String selection) {
214 mSelection = selection;
215 }
216
217 public String[] getSelectionArgs() {
218 return mSelectionArgs;
219 }
220
221 public void setSelectionArgs(String[] selectionArgs) {
222 mSelectionArgs = selectionArgs;
223 }
224
225 public String getSortOrder() {
226 return mSortOrder;
227 }
228
229 public void setSortOrder(String sortOrder) {
230 mSortOrder = sortOrder;
231 }
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800232
233 @Override
234 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
235 super.dump(prefix, fd, writer, args);
236 writer.print(prefix); writer.print("mUri="); writer.println(mUri);
237 writer.print(prefix); writer.print("mProjection=");
238 writer.println(Arrays.toString(mProjection));
239 writer.print(prefix); writer.print("mSelection="); writer.println(mSelection);
240 writer.print(prefix); writer.print("mSelectionArgs=");
241 writer.println(Arrays.toString(mSelectionArgs));
242 writer.print(prefix); writer.print("mSortOrder="); writer.println(mSortOrder);
243 writer.print(prefix); writer.print("mCursor="); writer.println(mCursor);
244 writer.print(prefix); writer.print("mContentChanged="); writer.println(mContentChanged);
245 }
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500246}