CursorLoader.java revision 8e6f69b08fa3be56ad11aaffbecbcbead49afd33
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 Cursor oldCursor = mCursor; 59 mCursor = cursor; 60 super.deliverResult(cursor); 61 62 if (oldCursor != null && !oldCursor.isClosed()) { 63 oldCursor.close(); 64 } 65 } 66 67 public CursorLoader(Context context, Uri uri, String[] projection, String selection, 68 String[] selectionArgs, String sortOrder) { 69 super(context); 70 mObserver = new ForceLoadContentObserver(); 71 mUri = uri; 72 mProjection = projection; 73 mSelection = selection; 74 mSelectionArgs = selectionArgs; 75 mSortOrder = sortOrder; 76 } 77 78 /** 79 * Starts an asynchronous load of the contacts list data. When the result is ready the callbacks 80 * will be called on the UI thread. If a previous load has been completed and is still valid 81 * the result may be passed to the callbacks immediately. 82 * 83 * Must be called from the UI thread 84 */ 85 @Override 86 public void startLoading() { 87 mStopped = false; 88 89 if (mCursor != null) { 90 deliverResult(mCursor); 91 } else { 92 forceLoad(); 93 } 94 } 95 96 /** 97 * Must be called from the UI thread 98 */ 99 @Override 100 public void stopLoading() { 101 if (mCursor != null && !mCursor.isClosed()) { 102 mCursor.close(); 103 mCursor = null; 104 } 105 106 // Attempt to cancel the current load task if possible. 107 cancelLoad(); 108 109 // Make sure that any outstanding loads clean themselves up properly 110 mStopped = true; 111 } 112 113 @Override 114 public void onCancelled(Cursor cursor) { 115 if (cursor != null && !cursor.isClosed()) { 116 cursor.close(); 117 } 118 } 119 120 @Override 121 public void destroy() { 122 // Ensure the loader is stopped 123 stopLoading(); 124 } 125 126 public Uri getUri() { 127 return mUri; 128 } 129 130 public void setUri(Uri uri) { 131 mUri = uri; 132 } 133 134 public String[] getProjection() { 135 return mProjection; 136 } 137 138 public void setProjection(String[] projection) { 139 mProjection = projection; 140 } 141 142 public String getSelection() { 143 return mSelection; 144 } 145 146 public void setSelection(String selection) { 147 mSelection = selection; 148 } 149 150 public String[] getSelectionArgs() { 151 return mSelectionArgs; 152 } 153 154 public void setSelectionArgs(String[] selectionArgs) { 155 mSelectionArgs = selectionArgs; 156 } 157 158 public String getSortOrder() { 159 return mSortOrder; 160 } 161 162 public void setSortOrder(String sortOrder) { 163 mSortOrder = sortOrder; 164 } 165} 166