| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.content; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.annotation.Nullable; |
| 21 |
import android.compat.annotation.UnsupportedAppUsage; |
| 22 |
import android.database.Cursor; |
| 23 |
import android.net.Uri; |
| 24 |
import android.os.Bundle; |
| 25 |
import android.os.Parcel; |
| 26 |
import android.os.Parcelable; |
| 27 |
import android.text.TextUtils; |
| 28 |
import android.util.ArrayMap; |
| 29 |
import android.util.Log; |
| 30 |
import android.util.SparseArray; |
| 31 |
|
| 32 |
import java.util.ArrayList; |
| 33 |
import java.util.Map; |
| 34 |
import java.util.Objects; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public class ContentProviderOperation implements Parcelable { |
| 42 |
|
| 43 |
@UnsupportedAppUsage |
| 44 |
public final static int TYPE_INSERT = 1; |
| 45 |
|
| 46 |
@UnsupportedAppUsage |
| 47 |
public final static int TYPE_UPDATE = 2; |
| 48 |
|
| 49 |
@UnsupportedAppUsage |
| 50 |
public final static int TYPE_DELETE = 3; |
| 51 |
|
| 52 |
public final static int TYPE_ASSERT = 4; |
| 53 |
|
| 54 |
public final static int TYPE_CALL = 5; |
| 55 |
|
| 56 |
@UnsupportedAppUsage |
| 57 |
private final int mType; |
| 58 |
@UnsupportedAppUsage |
| 59 |
private final Uri mUri; |
| 60 |
private final String mMethod; |
| 61 |
private final String mArg; |
| 62 |
private final ArrayMap<String, Object> mValues; |
| 63 |
private final ArrayMap<String, Object> mExtras; |
| 64 |
@UnsupportedAppUsage |
| 65 |
private final String mSelection; |
| 66 |
private final SparseArray<Object> mSelectionArgs; |
| 67 |
private final Integer mExpectedCount; |
| 68 |
private final boolean mYieldAllowed; |
| 69 |
private final boolean mExceptionAllowed; |
| 70 |
|
| 71 |
private final static String TAG = "ContentProviderOperation"; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
private ContentProviderOperation(Builder builder) { |
| 78 |
mType = builder.mType; |
| 79 |
mUri = builder.mUri; |
| 80 |
mMethod = builder.mMethod; |
| 81 |
mArg = builder.mArg; |
| 82 |
mValues = builder.mValues; |
| 83 |
mExtras = builder.mExtras; |
| 84 |
mSelection = builder.mSelection; |
| 85 |
mSelectionArgs = builder.mSelectionArgs; |
| 86 |
mExpectedCount = builder.mExpectedCount; |
| 87 |
mYieldAllowed = builder.mYieldAllowed; |
| 88 |
mExceptionAllowed = builder.mExceptionAllowed; |
| 89 |
} |
| 90 |
|
| 91 |
private ContentProviderOperation(Parcel source) { |
| 92 |
mType = source.readInt(); |
| 93 |
mUri = Uri.CREATOR.createFromParcel(source); |
| 94 |
mMethod = source.readInt() != 0 ? source.readString8() : null; |
| 95 |
mArg = source.readInt() != 0 ? source.readString8() : null; |
| 96 |
final int valuesSize = source.readInt(); |
| 97 |
if (valuesSize != -1) { |
| 98 |
mValues = new ArrayMap<>(valuesSize); |
| 99 |
source.readArrayMap(mValues, null); |
| 100 |
} else { |
| 101 |
mValues = null; |
| 102 |
} |
| 103 |
final int extrasSize = source.readInt(); |
| 104 |
if (extrasSize != -1) { |
| 105 |
mExtras = new ArrayMap<>(extrasSize); |
| 106 |
source.readArrayMap(mExtras, null); |
| 107 |
} else { |
| 108 |
mExtras = null; |
| 109 |
} |
| 110 |
mSelection = source.readInt() != 0 ? source.readString8() : null; |
| 111 |
mSelectionArgs = source.readSparseArray(null, java.lang.Object.class); |
| 112 |
mExpectedCount = source.readInt() != 0 ? source.readInt() : null; |
| 113 |
mYieldAllowed = source.readInt() != 0; |
| 114 |
mExceptionAllowed = source.readInt() != 0; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
public ContentProviderOperation(ContentProviderOperation cpo, Uri withUri) { |
| 119 |
mType = cpo.mType; |
| 120 |
mUri = withUri; |
| 121 |
mMethod = cpo.mMethod; |
| 122 |
mArg = cpo.mArg; |
| 123 |
mValues = cpo.mValues; |
| 124 |
mExtras = cpo.mExtras; |
| 125 |
mSelection = cpo.mSelection; |
| 126 |
mSelectionArgs = cpo.mSelectionArgs; |
| 127 |
mExpectedCount = cpo.mExpectedCount; |
| 128 |
mYieldAllowed = cpo.mYieldAllowed; |
| 129 |
mExceptionAllowed = cpo.mExceptionAllowed; |
| 130 |
} |
| 131 |
|
| 132 |
@Override |
| 133 |
public void writeToParcel(Parcel dest, int flags) { |
| 134 |
dest.writeInt(mType); |
| 135 |
Uri.writeToParcel(dest, mUri); |
| 136 |
if (mMethod != null) { |
| 137 |
dest.writeInt(1); |
| 138 |
dest.writeString8(mMethod); |
| 139 |
} else { |
| 140 |
dest.writeInt(0); |
| 141 |
} |
| 142 |
if (mArg != null) { |
| 143 |
dest.writeInt(1); |
| 144 |
dest.writeString8(mArg); |
| 145 |
} else { |
| 146 |
dest.writeInt(0); |
| 147 |
} |
| 148 |
if (mValues != null) { |
| 149 |
dest.writeInt(mValues.size()); |
| 150 |
dest.writeArrayMap(mValues); |
| 151 |
} else { |
| 152 |
dest.writeInt(-1); |
| 153 |
} |
| 154 |
if (mExtras != null) { |
| 155 |
dest.writeInt(mExtras.size()); |
| 156 |
dest.writeArrayMap(mExtras); |
| 157 |
} else { |
| 158 |
dest.writeInt(-1); |
| 159 |
} |
| 160 |
if (mSelection != null) { |
| 161 |
dest.writeInt(1); |
| 162 |
dest.writeString8(mSelection); |
| 163 |
} else { |
| 164 |
dest.writeInt(0); |
| 165 |
} |
| 166 |
dest.writeSparseArray(mSelectionArgs); |
| 167 |
if (mExpectedCount != null) { |
| 168 |
dest.writeInt(1); |
| 169 |
dest.writeInt(mExpectedCount); |
| 170 |
} else { |
| 171 |
dest.writeInt(0); |
| 172 |
} |
| 173 |
dest.writeInt(mYieldAllowed ? 1 : 0); |
| 174 |
dest.writeInt(mExceptionAllowed ? 1 : 0); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
public static @NonNull Builder newInsert(@NonNull Uri uri) { |
| 184 |
return new Builder(TYPE_INSERT, uri); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
public static @NonNull Builder newUpdate(@NonNull Uri uri) { |
| 194 |
return new Builder(TYPE_UPDATE, uri); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
public static @NonNull Builder newDelete(@NonNull Uri uri) { |
| 204 |
return new Builder(TYPE_DELETE, uri); |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
public static @NonNull Builder newAssertQuery(@NonNull Uri uri) { |
| 213 |
return new Builder(TYPE_ASSERT, uri); |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
public static @NonNull Builder newCall(@NonNull Uri uri, @Nullable String method, |
| 223 |
@Nullable String arg) { |
| 224 |
return new Builder(TYPE_CALL, uri, method, arg); |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
public @NonNull Uri getUri() { |
| 231 |
return mUri; |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
public boolean isYieldAllowed() { |
| 241 |
return mYieldAllowed; |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
public boolean isExceptionAllowed() { |
| 250 |
return mExceptionAllowed; |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
@UnsupportedAppUsage |
| 255 |
public int getType() { |
| 256 |
return mType; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
public boolean isInsert() { |
| 266 |
return mType == TYPE_INSERT; |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
public boolean isDelete() { |
| 276 |
return mType == TYPE_DELETE; |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
public boolean isUpdate() { |
| 286 |
return mType == TYPE_UPDATE; |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
public boolean isAssertQuery() { |
| 295 |
return mType == TYPE_ASSERT; |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
public boolean isCall() { |
| 305 |
return mType == TYPE_CALL; |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
public boolean isWriteOperation() { |
| 316 |
return mType == TYPE_DELETE || mType == TYPE_INSERT || mType == TYPE_UPDATE; |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
public boolean isReadOperation() { |
| 325 |
return mType == TYPE_ASSERT; |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
public @NonNull ContentProviderResult apply(@NonNull ContentProvider provider, |
| 343 |
@NonNull ContentProviderResult[] backRefs, int numBackRefs) |
| 344 |
throws OperationApplicationException { |
| 345 |
if (mExceptionAllowed) { |
| 346 |
try { |
| 347 |
return applyInternal(provider, backRefs, numBackRefs); |
| 348 |
} catch (Exception e) { |
| 349 |
return new ContentProviderResult(e); |
| 350 |
} |
| 351 |
} else { |
| 352 |
return applyInternal(provider, backRefs, numBackRefs); |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
private ContentProviderResult applyInternal(ContentProvider provider, |
| 357 |
ContentProviderResult[] backRefs, int numBackRefs) |
| 358 |
throws OperationApplicationException { |
| 359 |
final ContentValues values = resolveValueBackReferences(backRefs, numBackRefs); |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
Bundle extras = resolveExtrasBackReferences(backRefs, numBackRefs); |
| 364 |
if (mSelection != null) { |
| 365 |
extras = (extras != null) ? extras : new Bundle(); |
| 366 |
extras.putString(ContentResolver.QUERY_ARG_SQL_SELECTION, mSelection); |
| 367 |
} |
| 368 |
if (mSelectionArgs != null) { |
| 369 |
extras = (extras != null) ? extras : new Bundle(); |
| 370 |
extras.putStringArray(ContentResolver.QUERY_ARG_SQL_SELECTION_ARGS, |
| 371 |
resolveSelectionArgsBackReferences(backRefs, numBackRefs)); |
| 372 |
} |
| 373 |
|
| 374 |
if (mType == TYPE_INSERT) { |
| 375 |
final Uri newUri = provider.insert(mUri, values, extras); |
| 376 |
if (newUri != null) { |
| 377 |
return new ContentProviderResult(newUri); |
| 378 |
} else { |
| 379 |
throw new OperationApplicationException( |
| 380 |
"Insert into " + mUri + " returned no result"); |
| 381 |
} |
| 382 |
} else if (mType == TYPE_CALL) { |
| 383 |
final Bundle res = provider.call(mUri.getAuthority(), mMethod, mArg, extras); |
| 384 |
return new ContentProviderResult(res); |
| 385 |
} |
| 386 |
|
| 387 |
final int numRows; |
| 388 |
if (mType == TYPE_DELETE) { |
| 389 |
numRows = provider.delete(mUri, extras); |
| 390 |
} else if (mType == TYPE_UPDATE) { |
| 391 |
numRows = provider.update(mUri, values, extras); |
| 392 |
} else if (mType == TYPE_ASSERT) { |
| 393 |
|
| 394 |
String[] projection = null; |
| 395 |
if (values != null) { |
| 396 |
|
| 397 |
final ArrayList<String> projectionList = new ArrayList<String>(); |
| 398 |
for (Map.Entry<String, Object> entry : values.valueSet()) { |
| 399 |
projectionList.add(entry.getKey()); |
| 400 |
} |
| 401 |
projection = projectionList.toArray(new String[projectionList.size()]); |
| 402 |
} |
| 403 |
final Cursor cursor = provider.query(mUri, projection, extras, null); |
| 404 |
try { |
| 405 |
numRows = cursor.getCount(); |
| 406 |
if (projection != null) { |
| 407 |
while (cursor.moveToNext()) { |
| 408 |
for (int i = 0; i < projection.length; i++) { |
| 409 |
final String cursorValue = cursor.getString(i); |
| 410 |
final String expectedValue = values.getAsString(projection[i]); |
| 411 |
if (!TextUtils.equals(cursorValue, expectedValue)) { |
| 412 |
|
| 413 |
throw new OperationApplicationException("Found value " + cursorValue |
| 414 |
+ " when expected " + expectedValue + " for column " |
| 415 |
+ projection[i]); |
| 416 |
} |
| 417 |
} |
| 418 |
} |
| 419 |
} |
| 420 |
} finally { |
| 421 |
cursor.close(); |
| 422 |
} |
| 423 |
} else { |
| 424 |
throw new IllegalStateException("bad type, " + mType); |
| 425 |
} |
| 426 |
|
| 427 |
if (mExpectedCount != null && mExpectedCount != numRows) { |
| 428 |
throw new OperationApplicationException( |
| 429 |
"Expected " + mExpectedCount + " rows but actual " + numRows); |
| 430 |
} |
| 431 |
|
| 432 |
return new ContentProviderResult(numRows); |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
public @Nullable ContentValues resolveValueBackReferences( |
| 443 |
@NonNull ContentProviderResult[] backRefs, int numBackRefs) { |
| 444 |
if (mValues != null) { |
| 445 |
final ContentValues values = new ContentValues(); |
| 446 |
for (int i = 0; i < mValues.size(); i++) { |
| 447 |
final Object value = mValues.valueAt(i); |
| 448 |
final Object resolved; |
| 449 |
if (value instanceof BackReference) { |
| 450 |
resolved = ((BackReference) value).resolve(backRefs, numBackRefs); |
| 451 |
} else { |
| 452 |
resolved = value; |
| 453 |
} |
| 454 |
values.putObject(mValues.keyAt(i), resolved); |
| 455 |
} |
| 456 |
return values; |
| 457 |
} else { |
| 458 |
return null; |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
public @Nullable Bundle resolveExtrasBackReferences( |
| 470 |
@NonNull ContentProviderResult[] backRefs, int numBackRefs) { |
| 471 |
if (mExtras != null) { |
| 472 |
final Bundle extras = new Bundle(); |
| 473 |
for (int i = 0; i < mExtras.size(); i++) { |
| 474 |
final Object value = mExtras.valueAt(i); |
| 475 |
final Object resolved; |
| 476 |
if (value instanceof BackReference) { |
| 477 |
resolved = ((BackReference) value).resolve(backRefs, numBackRefs); |
| 478 |
} else { |
| 479 |
resolved = value; |
| 480 |
} |
| 481 |
extras.putObject(mExtras.keyAt(i), resolved); |
| 482 |
} |
| 483 |
return extras; |
| 484 |
} else { |
| 485 |
return null; |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
public @Nullable String[] resolveSelectionArgsBackReferences( |
| 497 |
@NonNull ContentProviderResult[] backRefs, int numBackRefs) { |
| 498 |
if (mSelectionArgs != null) { |
| 499 |
int max = -1; |
| 500 |
for (int i = 0; i < mSelectionArgs.size(); i++) { |
| 501 |
max = Math.max(max, mSelectionArgs.keyAt(i)); |
| 502 |
} |
| 503 |
|
| 504 |
final String[] selectionArgs = new String[max + 1]; |
| 505 |
for (int i = 0; i < mSelectionArgs.size(); i++) { |
| 506 |
final Object value = mSelectionArgs.valueAt(i); |
| 507 |
final Object resolved; |
| 508 |
if (value instanceof BackReference) { |
| 509 |
resolved = ((BackReference) value).resolve(backRefs, numBackRefs); |
| 510 |
} else { |
| 511 |
resolved = value; |
| 512 |
} |
| 513 |
selectionArgs[mSelectionArgs.keyAt(i)] = String.valueOf(resolved); |
| 514 |
} |
| 515 |
return selectionArgs; |
| 516 |
} else { |
| 517 |
return null; |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
|
| 522 |
public static String typeToString(int type) { |
| 523 |
switch (type) { |
| 524 |
case TYPE_INSERT: return "insert"; |
| 525 |
case TYPE_UPDATE: return "update"; |
| 526 |
case TYPE_DELETE: return "delete"; |
| 527 |
case TYPE_ASSERT: return "assert"; |
| 528 |
case TYPE_CALL: return "call"; |
| 529 |
default: return Integer.toString(type); |
| 530 |
} |
| 531 |
} |
| 532 |
|
| 533 |
@Override |
| 534 |
public String toString() { |
| 535 |
final StringBuilder sb = new StringBuilder("ContentProviderOperation("); |
| 536 |
sb.append("type=").append(typeToString(mType)).append(' '); |
| 537 |
if (mUri != null) { |
| 538 |
sb.append("uri=").append(mUri).append(' '); |
| 539 |
} |
| 540 |
if (mValues != null) { |
| 541 |
sb.append("values=").append(mValues).append(' '); |
| 542 |
} |
| 543 |
if (mSelection != null) { |
| 544 |
sb.append("selection=").append(mSelection).append(' '); |
| 545 |
} |
| 546 |
if (mSelectionArgs != null) { |
| 547 |
sb.append("selectionArgs=").append(mSelectionArgs).append(' '); |
| 548 |
} |
| 549 |
if (mExpectedCount != null) { |
| 550 |
sb.append("expectedCount=").append(mExpectedCount).append(' '); |
| 551 |
} |
| 552 |
if (mYieldAllowed) { |
| 553 |
sb.append("yieldAllowed "); |
| 554 |
} |
| 555 |
if (mExceptionAllowed) { |
| 556 |
sb.append("exceptionAllowed "); |
| 557 |
} |
| 558 |
sb.deleteCharAt(sb.length() - 1); |
| 559 |
sb.append(")"); |
| 560 |
return sb.toString(); |
| 561 |
} |
| 562 |
|
| 563 |
@Override |
| 564 |
public int describeContents() { |
| 565 |
return 0; |
| 566 |
} |
| 567 |
|
| 568 |
public static final @android.annotation.NonNull Creator<ContentProviderOperation> CREATOR = |
| 569 |
new Creator<ContentProviderOperation>() { |
| 570 |
@Override |
| 571 |
public ContentProviderOperation createFromParcel(Parcel source) { |
| 572 |
return new ContentProviderOperation(source); |
| 573 |
} |
| 574 |
|
| 575 |
@Override |
| 576 |
public ContentProviderOperation[] newArray(int size) { |
| 577 |
return new ContentProviderOperation[size]; |
| 578 |
} |
| 579 |
}; |
| 580 |
|
| 581 |
|
| 582 |
public static class BackReference implements Parcelable { |
| 583 |
private final int fromIndex; |
| 584 |
private final String fromKey; |
| 585 |
|
| 586 |
private BackReference(int fromIndex, String fromKey) { |
| 587 |
this.fromIndex = fromIndex; |
| 588 |
this.fromKey = fromKey; |
| 589 |
} |
| 590 |
|
| 591 |
public BackReference(Parcel src) { |
| 592 |
this.fromIndex = src.readInt(); |
| 593 |
if (src.readInt() != 0) { |
| 594 |
this.fromKey = src.readString8(); |
| 595 |
} else { |
| 596 |
this.fromKey = null; |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
public Object resolve(ContentProviderResult[] backRefs, int numBackRefs) { |
| 601 |
if (fromIndex >= numBackRefs) { |
| 602 |
Log.e(TAG, this.toString()); |
| 603 |
throw new ArrayIndexOutOfBoundsException("asked for back ref " + fromIndex |
| 604 |
+ " but there are only " + numBackRefs + " back refs"); |
| 605 |
} |
| 606 |
ContentProviderResult backRef = backRefs[fromIndex]; |
| 607 |
Object backRefValue; |
| 608 |
if (backRef.extras != null) { |
| 609 |
backRefValue = backRef.extras.get(fromKey); |
| 610 |
} else if (backRef.uri != null) { |
| 611 |
backRefValue = ContentUris.parseId(backRef.uri); |
| 612 |
} else { |
| 613 |
backRefValue = (long) backRef.count; |
| 614 |
} |
| 615 |
return backRefValue; |
| 616 |
} |
| 617 |
|
| 618 |
@Override |
| 619 |
public void writeToParcel(Parcel dest, int flags) { |
| 620 |
dest.writeInt(fromIndex); |
| 621 |
if (fromKey != null) { |
| 622 |
dest.writeInt(1); |
| 623 |
dest.writeString8(fromKey); |
| 624 |
} else { |
| 625 |
dest.writeInt(0); |
| 626 |
} |
| 627 |
} |
| 628 |
|
| 629 |
@Override |
| 630 |
public int describeContents() { |
| 631 |
return 0; |
| 632 |
} |
| 633 |
|
| 634 |
public static final @android.annotation.NonNull Creator<BackReference> CREATOR = |
| 635 |
new Creator<BackReference>() { |
| 636 |
@Override |
| 637 |
public BackReference createFromParcel(Parcel source) { |
| 638 |
return new BackReference(source); |
| 639 |
} |
| 640 |
|
| 641 |
@Override |
| 642 |
public BackReference[] newArray(int size) { |
| 643 |
return new BackReference[size]; |
| 644 |
} |
| 645 |
}; |
| 646 |
} |
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
public static class Builder { |
| 659 |
private final int mType; |
| 660 |
private final Uri mUri; |
| 661 |
private final String mMethod; |
| 662 |
private final String mArg; |
| 663 |
private ArrayMap<String, Object> mValues; |
| 664 |
private ArrayMap<String, Object> mExtras; |
| 665 |
private String mSelection; |
| 666 |
private SparseArray<Object> mSelectionArgs; |
| 667 |
private Integer mExpectedCount; |
| 668 |
private boolean mYieldAllowed; |
| 669 |
private boolean mExceptionAllowed; |
| 670 |
|
| 671 |
private Builder(int type, Uri uri) { |
| 672 |
this(type, uri, null, null); |
| 673 |
} |
| 674 |
|
| 675 |
private Builder(int type, Uri uri, String method, String arg) { |
| 676 |
mType = type; |
| 677 |
mUri = Objects.requireNonNull(uri); |
| 678 |
mMethod = method; |
| 679 |
mArg = arg; |
| 680 |
} |
| 681 |
|
| 682 |
|
| 683 |
public @NonNull ContentProviderOperation build() { |
| 684 |
if (mType == TYPE_UPDATE) { |
| 685 |
if ((mValues == null || mValues.isEmpty())) { |
| 686 |
throw new IllegalArgumentException("Empty values"); |
| 687 |
} |
| 688 |
} |
| 689 |
if (mType == TYPE_ASSERT) { |
| 690 |
if ((mValues == null || mValues.isEmpty()) |
| 691 |
&& (mExpectedCount == null)) { |
| 692 |
throw new IllegalArgumentException("Empty values"); |
| 693 |
} |
| 694 |
} |
| 695 |
return new ContentProviderOperation(this); |
| 696 |
} |
| 697 |
|
| 698 |
private void ensureValues() { |
| 699 |
if (mValues == null) { |
| 700 |
mValues = new ArrayMap<>(); |
| 701 |
} |
| 702 |
} |
| 703 |
|
| 704 |
private void ensureExtras() { |
| 705 |
if (mExtras == null) { |
| 706 |
mExtras = new ArrayMap<>(); |
| 707 |
} |
| 708 |
} |
| 709 |
|
| 710 |
private void ensureSelectionArgs() { |
| 711 |
if (mSelectionArgs == null) { |
| 712 |
mSelectionArgs = new SparseArray<>(); |
| 713 |
} |
| 714 |
} |
| 715 |
|
| 716 |
private void setValue(@NonNull String key, @NonNull Object value) { |
| 717 |
ensureValues(); |
| 718 |
final boolean oldReference = mValues.get(key) instanceof BackReference; |
| 719 |
final boolean newReference = value instanceof BackReference; |
| 720 |
if (!oldReference || newReference) { |
| 721 |
mValues.put(key, value); |
| 722 |
} |
| 723 |
} |
| 724 |
|
| 725 |
private void setExtra(@NonNull String key, @NonNull Object value) { |
| 726 |
ensureExtras(); |
| 727 |
final boolean oldReference = mExtras.get(key) instanceof BackReference; |
| 728 |
final boolean newReference = value instanceof BackReference; |
| 729 |
if (!oldReference || newReference) { |
| 730 |
mExtras.put(key, value); |
| 731 |
} |
| 732 |
} |
| 733 |
|
| 734 |
private void setSelectionArg(int index, @NonNull Object value) { |
| 735 |
ensureSelectionArgs(); |
| 736 |
final boolean oldReference = mSelectionArgs.get(index) instanceof BackReference; |
| 737 |
final boolean newReference = value instanceof BackReference; |
| 738 |
if (!oldReference || newReference) { |
| 739 |
mSelectionArgs.put(index, value); |
| 740 |
} |
| 741 |
} |
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
public @NonNull Builder withValues(@NonNull ContentValues values) { |
| 753 |
assertValuesAllowed(); |
| 754 |
ensureValues(); |
| 755 |
final ArrayMap<String, Object> rawValues = values.getValues(); |
| 756 |
for (int i = 0; i < rawValues.size(); i++) { |
| 757 |
setValue(rawValues.keyAt(i), rawValues.valueAt(i)); |
| 758 |
} |
| 759 |
return this; |
| 760 |
} |
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
public @NonNull Builder withValue(@NonNull String key, @Nullable Object value) { |
| 769 |
assertValuesAllowed(); |
| 770 |
if (!ContentValues.isSupportedValue(value)) { |
| 771 |
throw new IllegalArgumentException("bad value type: " + value.getClass().getName()); |
| 772 |
} |
| 773 |
setValue(key, value); |
| 774 |
return this; |
| 775 |
} |
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
public @NonNull Builder withValueBackReferences(@NonNull ContentValues backReferences) { |
| 788 |
assertValuesAllowed(); |
| 789 |
final ArrayMap<String, Object> rawValues = backReferences.getValues(); |
| 790 |
for (int i = 0; i < rawValues.size(); i++) { |
| 791 |
setValue(rawValues.keyAt(i), |
| 792 |
new BackReference((int) rawValues.valueAt(i), null)); |
| 793 |
} |
| 794 |
return this; |
| 795 |
} |
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
public @NonNull Builder withValueBackReference(@NonNull String key, int fromIndex) { |
| 807 |
assertValuesAllowed(); |
| 808 |
setValue(key, new BackReference(fromIndex, null)); |
| 809 |
return this; |
| 810 |
} |
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
public @NonNull Builder withValueBackReference(@NonNull String key, int fromIndex, |
| 825 |
@NonNull String fromKey) { |
| 826 |
assertValuesAllowed(); |
| 827 |
setValue(key, new BackReference(fromIndex, fromKey)); |
| 828 |
return this; |
| 829 |
} |
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
public @NonNull Builder withExtras(@NonNull Bundle extras) { |
| 841 |
assertExtrasAllowed(); |
| 842 |
ensureExtras(); |
| 843 |
for (String key : extras.keySet()) { |
| 844 |
setExtra(key, extras.get(key)); |
| 845 |
} |
| 846 |
return this; |
| 847 |
} |
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
public @NonNull Builder withExtra(@NonNull String key, @Nullable Object value) { |
| 856 |
assertExtrasAllowed(); |
| 857 |
setExtra(key, value); |
| 858 |
return this; |
| 859 |
} |
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
public @NonNull Builder withExtraBackReference(@NonNull String key, int fromIndex) { |
| 871 |
assertExtrasAllowed(); |
| 872 |
setExtra(key, new BackReference(fromIndex, null)); |
| 873 |
return this; |
| 874 |
} |
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
public @NonNull Builder withExtraBackReference(@NonNull String key, int fromIndex, |
| 889 |
@NonNull String fromKey) { |
| 890 |
assertExtrasAllowed(); |
| 891 |
setExtra(key, new BackReference(fromIndex, fromKey)); |
| 892 |
return this; |
| 893 |
} |
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
public @NonNull Builder withSelection(@Nullable String selection, |
| 909 |
@Nullable String[] selectionArgs) { |
| 910 |
assertSelectionAllowed(); |
| 911 |
mSelection = selection; |
| 912 |
if (selectionArgs != null) { |
| 913 |
ensureSelectionArgs(); |
| 914 |
for (int i = 0; i < selectionArgs.length; i++) { |
| 915 |
setSelectionArg(i, selectionArgs[i]); |
| 916 |
} |
| 917 |
} |
| 918 |
return this; |
| 919 |
} |
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
public @NonNull Builder withSelectionBackReference(int index, int fromIndex) { |
| 933 |
assertSelectionAllowed(); |
| 934 |
setSelectionArg(index, new BackReference(fromIndex, null)); |
| 935 |
return this; |
| 936 |
} |
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
public @NonNull Builder withSelectionBackReference(int index, int fromIndex, |
| 953 |
@NonNull String fromKey) { |
| 954 |
assertSelectionAllowed(); |
| 955 |
setSelectionArg(index, new BackReference(fromIndex, fromKey)); |
| 956 |
return this; |
| 957 |
} |
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
public @NonNull Builder withExpectedCount(int count) { |
| 966 |
if (mType != TYPE_UPDATE && mType != TYPE_DELETE && mType != TYPE_ASSERT) { |
| 967 |
throw new IllegalArgumentException( |
| 968 |
"only updates, deletes, and asserts can have expected counts"); |
| 969 |
} |
| 970 |
mExpectedCount = count; |
| 971 |
return this; |
| 972 |
} |
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
public @NonNull Builder withYieldAllowed(boolean yieldAllowed) { |
| 981 |
mYieldAllowed = yieldAllowed; |
| 982 |
return this; |
| 983 |
} |
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
public @NonNull Builder withExceptionAllowed(boolean exceptionAllowed) { |
| 992 |
mExceptionAllowed = exceptionAllowed; |
| 993 |
return this; |
| 994 |
} |
| 995 |
|
| 996 |
|
| 997 |
public @NonNull Builder withFailureAllowed(boolean failureAllowed) { |
| 998 |
return withExceptionAllowed(failureAllowed); |
| 999 |
} |
| 1000 |
|
| 1001 |
private void assertValuesAllowed() { |
| 1002 |
switch (mType) { |
| 1003 |
case TYPE_INSERT: |
| 1004 |
case TYPE_UPDATE: |
| 1005 |
case TYPE_ASSERT: |
| 1006 |
break; |
| 1007 |
default: |
| 1008 |
throw new IllegalArgumentException( |
| 1009 |
"Values not supported for " + typeToString(mType)); |
| 1010 |
} |
| 1011 |
} |
| 1012 |
|
| 1013 |
private void assertSelectionAllowed() { |
| 1014 |
switch (mType) { |
| 1015 |
case TYPE_UPDATE: |
| 1016 |
case TYPE_DELETE: |
| 1017 |
case TYPE_ASSERT: |
| 1018 |
break; |
| 1019 |
default: |
| 1020 |
throw new IllegalArgumentException( |
| 1021 |
"Selection not supported for " + typeToString(mType)); |
| 1022 |
} |
| 1023 |
} |
| 1024 |
|
| 1025 |
private void assertExtrasAllowed() { |
| 1026 |
switch (mType) { |
| 1027 |
case TYPE_INSERT: |
| 1028 |
case TYPE_UPDATE: |
| 1029 |
case TYPE_DELETE: |
| 1030 |
case TYPE_ASSERT: |
| 1031 |
case TYPE_CALL: |
| 1032 |
break; |
| 1033 |
default: |
| 1034 |
throw new IllegalArgumentException( |
| 1035 |
"Extras not supported for " + typeToString(mType)); |
| 1036 |
} |
| 1037 |
} |
| 1038 |
} |
| 1039 |
} |