blob: 5a08636c8fff775782af5967d93f565162226d05 [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 *
Ian Lake1f4e67b2017-12-18 10:36:18 -080042 * @deprecated Use the <a href="{@docRoot}tools/extras/support-library.html">Support Library</a>
43 * {@link android.support.v4.content.CursorLoader}
Jeff Hamilton9911b7f2010-05-15 02:20:31 -050044 */
Ian Lake0a1feb82017-11-13 10:26:46 -080045@Deprecated
Jeff Hamilton9911b7f2010-05-15 02:20:31 -050046public class CursorLoader extends AsyncTaskLoader<Cursor> {
Dianne Hackborna2ea7472010-12-20 12:10:01 -080047 final ForceLoadContentObserver mObserver;
48
Jeff Hamilton9911b7f2010-05-15 02:20:31 -050049 Uri mUri;
50 String[] mProjection;
51 String mSelection;
52 String[] mSelectionArgs;
53 String mSortOrder;
54
Dianne Hackborna2ea7472010-12-20 12:10:01 -080055 Cursor mCursor;
Jeff Brown4c1241d2012-02-02 17:05:00 -080056 CancellationSignal mCancellationSignal;
Dianne Hackborna2ea7472010-12-20 12:10:01 -080057
Jeff Hamilton9911b7f2010-05-15 02:20:31 -050058 /* Runs on a worker thread */
59 @Override
60 public Cursor loadInBackground() {
Jeff Brown75ea64f2012-01-25 19:37:13 -080061 synchronized (this) {
62 if (isLoadInBackgroundCanceled()) {
63 throw new OperationCanceledException();
64 }
Jeff Brown4c1241d2012-02-02 17:05:00 -080065 mCancellationSignal = new CancellationSignal();
Jeff Hamilton9911b7f2010-05-15 02:20:31 -050066 }
Jeff Brown75ea64f2012-01-25 19:37:13 -080067 try {
68 Cursor cursor = getContext().getContentResolver().query(mUri, mProjection, mSelection,
Jeff Brown4c1241d2012-02-02 17:05:00 -080069 mSelectionArgs, mSortOrder, mCancellationSignal);
Jeff Brown75ea64f2012-01-25 19:37:13 -080070 if (cursor != null) {
Jeff Brownc21b5a02013-01-07 17:15:12 -080071 try {
72 // Ensure the cursor window is filled.
73 cursor.getCount();
Scott Kennedyda2223f2013-03-13 14:40:11 -070074 cursor.registerContentObserver(mObserver);
Jeff Brownc21b5a02013-01-07 17:15:12 -080075 } catch (RuntimeException ex) {
76 cursor.close();
77 throw ex;
78 }
Jeff Brown75ea64f2012-01-25 19:37:13 -080079 }
80 return cursor;
81 } finally {
82 synchronized (this) {
Jeff Brown4c1241d2012-02-02 17:05:00 -080083 mCancellationSignal = null;
Jeff Brown75ea64f2012-01-25 19:37:13 -080084 }
85 }
86 }
87
88 @Override
Jeff Brownb19a71a2012-01-31 11:48:39 -080089 public void cancelLoadInBackground() {
90 super.cancelLoadInBackground();
Jeff Brown75ea64f2012-01-25 19:37:13 -080091
92 synchronized (this) {
Jeff Brown4c1241d2012-02-02 17:05:00 -080093 if (mCancellationSignal != null) {
94 mCancellationSignal.cancel();
Jeff Brown75ea64f2012-01-25 19:37:13 -080095 }
96 }
Jeff Hamilton9911b7f2010-05-15 02:20:31 -050097 }
98
99 /* Runs on the UI thread */
100 @Override
101 public void deliverResult(Cursor cursor) {
Dianne Hackborn0e3b8f422010-12-20 23:22:11 -0800102 if (isReset()) {
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500103 // An async query came in while the loader is stopped
Dmitri Plotnikov4565d522010-07-15 18:24:07 -0700104 if (cursor != null) {
Dianne Hackborn229edbc2011-10-09 16:01:40 -0700105 cursor.close();
Dmitri Plotnikov4565d522010-07-15 18:24:07 -0700106 }
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500107 return;
108 }
Ben Komalo8e6f69b2010-07-22 16:21:22 -0700109 Cursor oldCursor = mCursor;
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500110 mCursor = cursor;
Ben Komalo8e6f69b2010-07-22 16:21:22 -0700111
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800112 if (isStarted()) {
Dianne Hackbornc9189352010-12-15 14:57:25 -0800113 super.deliverResult(cursor);
114 }
115
116 if (oldCursor != null && oldCursor != cursor && !oldCursor.isClosed()) {
Dianne Hackborn229edbc2011-10-09 16:01:40 -0700117 oldCursor.close();
Ben Komalo8e6f69b2010-07-22 16:21:22 -0700118 }
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500119 }
120
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800121 /**
122 * Creates an empty unspecified CursorLoader. You must follow this with
123 * calls to {@link #setUri(Uri)}, {@link #setSelection(String)}, etc
124 * to specify the query to perform.
125 */
126 public CursorLoader(Context context) {
127 super(context);
128 mObserver = new ForceLoadContentObserver();
129 }
130
131 /**
132 * Creates a fully-specified CursorLoader. See
133 * {@link ContentResolver#query(Uri, String[], String, String[], String)
134 * ContentResolver.query()} for documentation on the meaning of the
135 * parameters. These will be passed as-is to that call.
136 */
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500137 public CursorLoader(Context context, Uri uri, String[] projection, String selection,
138 String[] selectionArgs, String sortOrder) {
139 super(context);
140 mObserver = new ForceLoadContentObserver();
141 mUri = uri;
142 mProjection = projection;
143 mSelection = selection;
144 mSelectionArgs = selectionArgs;
145 mSortOrder = sortOrder;
146 }
147
148 /**
Fyodor Kupolov96481de2017-11-17 15:30:03 -0800149 * Starts an asynchronous load of the data. When the result is ready the callbacks
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500150 * 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 -0700151 * the result may be passed to the callbacks immediately.
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500152 *
153 * Must be called from the UI thread
154 */
155 @Override
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800156 protected void onStartLoading() {
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500157 if (mCursor != null) {
158 deliverResult(mCursor);
Dianne Hackbornf73c75c2010-12-17 16:54:05 -0800159 }
Dianne Hackborn0e3b8f422010-12-20 23:22:11 -0800160 if (takeContentChanged() || mCursor == null) {
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500161 forceLoad();
162 }
163 }
164
165 /**
166 * Must be called from the UI thread
167 */
168 @Override
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800169 protected void onStopLoading() {
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500170 // Attempt to cancel the current load task if possible.
171 cancelLoad();
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500172 }
173
174 @Override
Dianne Hackborn327fbd22011-01-17 14:38:50 -0800175 public void onCanceled(Cursor cursor) {
Dmitri Plotnikovbef9c7a2010-06-16 15:38:07 -0700176 if (cursor != null && !cursor.isClosed()) {
Dianne Hackborn229edbc2011-10-09 16:01:40 -0700177 cursor.close();
Dmitri Plotnikovbef9c7a2010-06-16 15:38:07 -0700178 }
179 }
180
181 @Override
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800182 protected void onReset() {
Dianne Hackborn0e3b8f422010-12-20 23:22:11 -0800183 super.onReset();
184
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500185 // Ensure the loader is stopped
Dianne Hackborn0e3b8f422010-12-20 23:22:11 -0800186 onStopLoading();
Dianne Hackbornc9189352010-12-15 14:57:25 -0800187
188 if (mCursor != null && !mCursor.isClosed()) {
Dianne Hackborn229edbc2011-10-09 16:01:40 -0700189 mCursor.close();
Dianne Hackbornc9189352010-12-15 14:57:25 -0800190 }
191 mCursor = null;
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500192 }
193
194 public Uri getUri() {
195 return mUri;
196 }
197
198 public void setUri(Uri uri) {
199 mUri = uri;
200 }
201
202 public String[] getProjection() {
203 return mProjection;
204 }
205
206 public void setProjection(String[] projection) {
207 mProjection = projection;
208 }
209
210 public String getSelection() {
211 return mSelection;
212 }
213
214 public void setSelection(String selection) {
215 mSelection = selection;
216 }
217
218 public String[] getSelectionArgs() {
219 return mSelectionArgs;
220 }
221
222 public void setSelectionArgs(String[] selectionArgs) {
223 mSelectionArgs = selectionArgs;
224 }
225
226 public String getSortOrder() {
227 return mSortOrder;
228 }
229
230 public void setSortOrder(String sortOrder) {
231 mSortOrder = sortOrder;
232 }
Dianne Hackborna2ea7472010-12-20 12:10:01 -0800233
234 @Override
235 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
236 super.dump(prefix, fd, writer, args);
237 writer.print(prefix); writer.print("mUri="); writer.println(mUri);
238 writer.print(prefix); writer.print("mProjection=");
239 writer.println(Arrays.toString(mProjection));
240 writer.print(prefix); writer.print("mSelection="); writer.println(mSelection);
241 writer.print(prefix); writer.print("mSelectionArgs=");
242 writer.println(Arrays.toString(mSelectionArgs));
243 writer.print(prefix); writer.print("mSortOrder="); writer.println(mSortOrder);
244 writer.print(prefix); writer.print("mCursor="); writer.println(mCursor);
245 writer.print(prefix); writer.print("mContentChanged="); writer.println(mContentChanged);
246 }
Jeff Hamilton9911b7f2010-05-15 02:20:31 -0500247}