| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.content; |
| 18 |
|
| 19 |
import android.compat.annotation.UnsupportedAppUsage; |
| 20 |
import android.database.Cursor; |
| 21 |
import android.net.Uri; |
| 22 |
import android.os.Build; |
| 23 |
import android.os.CancellationSignal; |
| 24 |
import android.os.OperationCanceledException; |
| 25 |
|
| 26 |
import java.io.FileDescriptor; |
| 27 |
import java.io.PrintWriter; |
| 28 |
import java.util.Arrays; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
@Deprecated |
| 48 |
public class CursorLoader extends AsyncTaskLoader<Cursor> { |
| 49 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 50 |
final ForceLoadContentObserver mObserver; |
| 51 |
|
| 52 |
Uri mUri; |
| 53 |
String[] mProjection; |
| 54 |
String mSelection; |
| 55 |
String[] mSelectionArgs; |
| 56 |
String mSortOrder; |
| 57 |
|
| 58 |
Cursor mCursor; |
| 59 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 60 |
CancellationSignal mCancellationSignal; |
| 61 |
|
| 62 |
|
| 63 |
@Override |
| 64 |
public Cursor loadInBackground() { |
| 65 |
synchronized (this) { |
| 66 |
if (isLoadInBackgroundCanceled()) { |
| 67 |
throw new OperationCanceledException(); |
| 68 |
} |
| 69 |
mCancellationSignal = new CancellationSignal(); |
| 70 |
} |
| 71 |
try { |
| 72 |
Cursor cursor = getContext().getContentResolver().query(mUri, mProjection, mSelection, |
| 73 |
mSelectionArgs, mSortOrder, mCancellationSignal); |
| 74 |
if (cursor != null) { |
| 75 |
try { |
| 76 |
|
| 77 |
cursor.getCount(); |
| 78 |
cursor.registerContentObserver(mObserver); |
| 79 |
} catch (RuntimeException ex) { |
| 80 |
cursor.close(); |
| 81 |
throw ex; |
| 82 |
} |
| 83 |
} |
| 84 |
return cursor; |
| 85 |
} finally { |
| 86 |
synchronized (this) { |
| 87 |
mCancellationSignal = null; |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
@Override |
| 93 |
public void cancelLoadInBackground() { |
| 94 |
super.cancelLoadInBackground(); |
| 95 |
|
| 96 |
synchronized (this) { |
| 97 |
if (mCancellationSignal != null) { |
| 98 |
mCancellationSignal.cancel(); |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
@Override |
| 105 |
public void deliverResult(Cursor cursor) { |
| 106 |
if (isReset()) { |
| 107 |
|
| 108 |
if (cursor != null) { |
| 109 |
cursor.close(); |
| 110 |
} |
| 111 |
return; |
| 112 |
} |
| 113 |
Cursor oldCursor = mCursor; |
| 114 |
mCursor = cursor; |
| 115 |
|
| 116 |
if (isStarted()) { |
| 117 |
super.deliverResult(cursor); |
| 118 |
} |
| 119 |
|
| 120 |
if (oldCursor != null && oldCursor != cursor && !oldCursor.isClosed()) { |
| 121 |
oldCursor.close(); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
public CursorLoader(Context context) { |
| 131 |
super(context); |
| 132 |
mObserver = new ForceLoadContentObserver(); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public CursorLoader(Context context, Uri uri, String[] projection, String selection, |
| 142 |
String[] selectionArgs, String sortOrder) { |
| 143 |
super(context); |
| 144 |
mObserver = new ForceLoadContentObserver(); |
| 145 |
mUri = uri; |
| 146 |
mProjection = projection; |
| 147 |
mSelection = selection; |
| 148 |
mSelectionArgs = selectionArgs; |
| 149 |
mSortOrder = sortOrder; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
@Override |
| 160 |
protected void onStartLoading() { |
| 161 |
if (mCursor != null) { |
| 162 |
deliverResult(mCursor); |
| 163 |
} |
| 164 |
if (takeContentChanged() || mCursor == null) { |
| 165 |
forceLoad(); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
@Override |
| 173 |
protected void onStopLoading() { |
| 174 |
|
| 175 |
cancelLoad(); |
| 176 |
} |
| 177 |
|
| 178 |
@Override |
| 179 |
public void onCanceled(Cursor cursor) { |
| 180 |
if (cursor != null && !cursor.isClosed()) { |
| 181 |
cursor.close(); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
@Override |
| 186 |
protected void onReset() { |
| 187 |
super.onReset(); |
| 188 |
|
| 189 |
|
| 190 |
onStopLoading(); |
| 191 |
|
| 192 |
if (mCursor != null && !mCursor.isClosed()) { |
| 193 |
mCursor.close(); |
| 194 |
} |
| 195 |
mCursor = null; |
| 196 |
} |
| 197 |
|
| 198 |
public Uri getUri() { |
| 199 |
return mUri; |
| 200 |
} |
| 201 |
|
| 202 |
public void setUri(Uri uri) { |
| 203 |
mUri = uri; |
| 204 |
} |
| 205 |
|
| 206 |
public String[] getProjection() { |
| 207 |
return mProjection; |
| 208 |
} |
| 209 |
|
| 210 |
public void setProjection(String[] projection) { |
| 211 |
mProjection = projection; |
| 212 |
} |
| 213 |
|
| 214 |
public String getSelection() { |
| 215 |
return mSelection; |
| 216 |
} |
| 217 |
|
| 218 |
public void setSelection(String selection) { |
| 219 |
mSelection = selection; |
| 220 |
} |
| 221 |
|
| 222 |
public String[] getSelectionArgs() { |
| 223 |
return mSelectionArgs; |
| 224 |
} |
| 225 |
|
| 226 |
public void setSelectionArgs(String[] selectionArgs) { |
| 227 |
mSelectionArgs = selectionArgs; |
| 228 |
} |
| 229 |
|
| 230 |
public String getSortOrder() { |
| 231 |
return mSortOrder; |
| 232 |
} |
| 233 |
|
| 234 |
public void setSortOrder(String sortOrder) { |
| 235 |
mSortOrder = sortOrder; |
| 236 |
} |
| 237 |
|
| 238 |
@Override |
| 239 |
public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { |
| 240 |
super.dump(prefix, fd, writer, args); |
| 241 |
writer.print(prefix); writer.print("mUri="); writer.println(mUri); |
| 242 |
writer.print(prefix); writer.print("mProjection="); |
| 243 |
writer.println(Arrays.toString(mProjection)); |
| 244 |
writer.print(prefix); writer.print("mSelection="); writer.println(mSelection); |
| 245 |
writer.print(prefix); writer.print("mSelectionArgs="); |
| 246 |
writer.println(Arrays.toString(mSelectionArgs)); |
| 247 |
writer.print(prefix); writer.print("mSortOrder="); writer.println(mSortOrder); |
| 248 |
writer.print(prefix); writer.print("mCursor="); writer.println(mCursor); |
| 249 |
writer.print(prefix); writer.print("mContentChanged="); writer.println(mContentChanged); |
| 250 |
} |
| 251 |
} |