2 * Copyright (C) 2011 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package android.support.v2.content;
19 import android.content.Context;
20 import android.database.ContentObserver;
21 import android.database.Cursor;
22 import android.net.Uri;
24 import java.io.FileDescriptor;
25 import java.io.PrintWriter;
26 import java.util.Arrays;
29 * Static library support version of the framework's {@link android.content.CursorLoader}.
30 * Used to write apps that run on platforms prior to Android 3.0. When running
31 * on Android 3.0 or above, this implementation is still used; it does not try
32 * to switch to the framework's implementation. See the framework SDK
33 * documentation for a class overview.
35 public class CursorLoader extends AsyncTaskLoader<Cursor> {
36 final ForceLoadContentObserver mObserver;
41 String[] mSelectionArgs;
46 /* Runs on a worker thread */
48 public Cursor loadInBackground() {
49 Cursor cursor = getContext().getContentResolver().query(mUri, mProjection, mSelection,
50 mSelectionArgs, mSortOrder);
52 // Ensure the cursor window is filled
54 registerContentObserver(cursor, mObserver);
60 * Registers an observer to get notifications from the content provider
61 * when the cursor needs to be refreshed.
63 void registerContentObserver(Cursor cursor, ContentObserver observer) {
64 cursor.registerContentObserver(mObserver);
67 /* Runs on the UI thread */
69 public void deliverResult(Cursor cursor) {
71 // An async query came in while the loader is stopped
77 Cursor oldCursor = mCursor;
81 super.deliverResult(cursor);
84 if (oldCursor != null && oldCursor != cursor && !oldCursor.isClosed()) {
90 * Creates an empty unspecified CursorLoader. You must follow this with
91 * calls to {@link #setUri(Uri)}, {@link #setSelection(String)}, etc
92 * to specify the query to perform.
94 public CursorLoader(Context context) {
96 mObserver = new ForceLoadContentObserver();
100 * Creates a fully-specified CursorLoader. See
101 * {@link ContentResolver#query(Uri, String[], String, String[], String)
102 * ContentResolver.query()} for documentation on the meaning of the
103 * parameters. These will be passed as-is to that call.
105 public CursorLoader(Context context, Uri uri, String[] projection, String selection,
106 String[] selectionArgs, String sortOrder) {
108 mObserver = new ForceLoadContentObserver();
110 mProjection = projection;
111 mSelection = selection;
112 mSelectionArgs = selectionArgs;
113 mSortOrder = sortOrder;
117 * Starts an asynchronous load of the contacts list data. When the result is ready the callbacks
118 * will be called on the UI thread. If a previous load has been completed and is still valid
119 * the result may be passed to the callbacks immediately.
121 * Must be called from the UI thread
124 protected void onStartLoading() {
125 if (mCursor != null) {
126 deliverResult(mCursor);
128 if (takeContentChanged() || mCursor == null) {
134 * Must be called from the UI thread
137 protected void onStopLoading() {
138 // Attempt to cancel the current load task if possible.
143 public void onCanceled(Cursor cursor) {
144 if (cursor != null && !cursor.isClosed()) {
150 protected void onReset() {
153 // Ensure the loader is stopped
156 if (mCursor != null && !mCursor.isClosed()) {
162 public Uri getUri() {
166 public void setUri(Uri uri) {
170 public String[] getProjection() {
174 public void setProjection(String[] projection) {
175 mProjection = projection;
178 public String getSelection() {
182 public void setSelection(String selection) {
183 mSelection = selection;
186 public String[] getSelectionArgs() {
187 return mSelectionArgs;
190 public void setSelectionArgs(String[] selectionArgs) {
191 mSelectionArgs = selectionArgs;
194 public String getSortOrder() {
198 public void setSortOrder(String sortOrder) {
199 mSortOrder = sortOrder;
203 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
204 super.dump(prefix, fd, writer, args);
205 writer.print(prefix); writer.print("mUri="); writer.println(mUri);
206 writer.print(prefix); writer.print("mProjection=");
207 writer.println(Arrays.toString(mProjection));
208 writer.print(prefix); writer.print("mSelection="); writer.println(mSelection);
209 writer.print(prefix); writer.print("mSelectionArgs=");
210 writer.println(Arrays.toString(mSelectionArgs));
211 writer.print(prefix); writer.print("mSortOrder="); writer.println(mSortOrder);
212 writer.print(prefix); writer.print("mCursor="); writer.println(mCursor);
213 writer.print(prefix); writer.print("mContentChanged="); writer.println(mContentChanged);