CursorLoader.java revision 4565d52bdd60490b05f9f8f7c1fd3c552974948c
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
17package android.content;
18
19import android.database.Cursor;
20import android.net.Uri;
21
22/**
23 * A loader that queries the {@link ContentResolver} and returns a {@link Cursor}.
24 */
25public class CursorLoader extends AsyncTaskLoader<Cursor> {
26    Cursor mCursor;
27    ForceLoadContentObserver mObserver;
28    boolean mStopped;
29    Uri mUri;
30    String[] mProjection;
31    String mSelection;
32    String[] mSelectionArgs;
33    String mSortOrder;
34
35    /* Runs on a worker thread */
36    @Override
37    public Cursor loadInBackground() {
38        Cursor cursor = getContext().getContentResolver().query(mUri, mProjection, mSelection,
39                mSelectionArgs, mSortOrder);
40        // Ensure the cursor window is filled
41        if (cursor != null) {
42            cursor.getCount();
43            cursor.registerContentObserver(mObserver);
44        }
45        return cursor;
46    }
47
48    /* Runs on the UI thread */
49    @Override
50    public void deliverResult(Cursor cursor) {
51        if (mStopped) {
52            // An async query came in while the loader is stopped
53            if (cursor != null) {
54                cursor.close();
55            }
56            return;
57        }
58        mCursor = cursor;
59        super.deliverResult(cursor);
60    }
61
62    public CursorLoader(Context context, Uri uri, String[] projection, String selection,
63            String[] selectionArgs, String sortOrder) {
64        super(context);
65        mObserver = new ForceLoadContentObserver();
66        mUri = uri;
67        mProjection = projection;
68        mSelection = selection;
69        mSelectionArgs = selectionArgs;
70        mSortOrder = sortOrder;
71    }
72
73    /**
74     * Starts an asynchronous load of the contacts list data. When the result is ready the callbacks
75     * will be called on the UI thread. If a previous load has been completed and is still valid
76     * the result may be passed to the callbacks immediately.
77     *
78     * Must be called from the UI thread
79     */
80    @Override
81    public void startLoading() {
82        mStopped = false;
83
84        if (mCursor != null) {
85            deliverResult(mCursor);
86        } else {
87            forceLoad();
88        }
89    }
90
91    /**
92     * Must be called from the UI thread
93     */
94    @Override
95    public void stopLoading() {
96        if (mCursor != null && !mCursor.isClosed()) {
97            mCursor.close();
98            mCursor = null;
99        }
100
101        // Attempt to cancel the current load task if possible.
102        cancelLoad();
103
104        // Make sure that any outstanding loads clean themselves up properly
105        mStopped = true;
106    }
107
108    @Override
109    public void onCancelled(Cursor cursor) {
110        if (cursor != null && !cursor.isClosed()) {
111            cursor.close();
112        }
113    }
114
115    @Override
116    public void destroy() {
117        // Ensure the loader is stopped
118        stopLoading();
119    }
120
121    public Uri getUri() {
122        return mUri;
123    }
124
125    public void setUri(Uri uri) {
126        mUri = uri;
127    }
128
129    public String[] getProjection() {
130        return mProjection;
131    }
132
133    public void setProjection(String[] projection) {
134        mProjection = projection;
135    }
136
137    public String getSelection() {
138        return mSelection;
139    }
140
141    public void setSelection(String selection) {
142        mSelection = selection;
143    }
144
145    public String[] getSelectionArgs() {
146        return mSelectionArgs;
147    }
148
149    public void setSelectionArgs(String[] selectionArgs) {
150        mSelectionArgs = selectionArgs;
151    }
152
153    public String getSortOrder() {
154        return mSortOrder;
155    }
156
157    public void setSortOrder(String sortOrder) {
158        mSortOrder = sortOrder;
159    }
160}
161