| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.app; |
| 18 |
|
| 19 |
import android.annotation.IntDef; |
| 20 |
import android.annotation.NonNull; |
| 21 |
import android.annotation.Nullable; |
| 22 |
import android.content.ClipData; |
| 23 |
import android.content.ClipDescription; |
| 24 |
import android.content.Intent; |
| 25 |
import android.net.Uri; |
| 26 |
import android.os.Bundle; |
| 27 |
import android.os.Parcel; |
| 28 |
import android.os.Parcelable; |
| 29 |
import android.util.ArraySet; |
| 30 |
|
| 31 |
import java.lang.annotation.Retention; |
| 32 |
import java.lang.annotation.RetentionPolicy; |
| 33 |
import java.util.HashMap; |
| 34 |
import java.util.Map; |
| 35 |
import java.util.Set; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public final class RemoteInput implements Parcelable { |
| 72 |
|
| 73 |
public static final String RESULTS_CLIP_LABEL = "android.remoteinput.results"; |
| 74 |
|
| 75 |
|
| 76 |
public static final String EXTRA_RESULTS_DATA = "android.remoteinput.resultsData"; |
| 77 |
|
| 78 |
|
| 79 |
private static final String EXTRA_DATA_TYPE_RESULTS_DATA = |
| 80 |
"android.remoteinput.dataTypeResultsData"; |
| 81 |
|
| 82 |
|
| 83 |
private static final String EXTRA_RESULTS_SOURCE = "android.remoteinput.resultsSource"; |
| 84 |
|
| 85 |
|
| 86 |
@IntDef(prefix = {"SOURCE_"}, value = {SOURCE_FREE_FORM_INPUT, SOURCE_CHOICE}) |
| 87 |
@Retention(RetentionPolicy.SOURCE) |
| 88 |
public @interface Source {} |
| 89 |
|
| 90 |
|
| 91 |
public static final int SOURCE_FREE_FORM_INPUT = 0; |
| 92 |
|
| 93 |
|
| 94 |
public static final int SOURCE_CHOICE = 1; |
| 95 |
|
| 96 |
|
| 97 |
@IntDef(prefix = {"EDIT_CHOICES_BEFORE_SENDING_"}, |
| 98 |
value = {EDIT_CHOICES_BEFORE_SENDING_AUTO, EDIT_CHOICES_BEFORE_SENDING_DISABLED, |
| 99 |
EDIT_CHOICES_BEFORE_SENDING_ENABLED}) |
| 100 |
@Retention(RetentionPolicy.SOURCE) |
| 101 |
public @interface EditChoicesBeforeSending {} |
| 102 |
|
| 103 |
|
| 104 |
public static final int EDIT_CHOICES_BEFORE_SENDING_AUTO = 0; |
| 105 |
|
| 106 |
|
| 107 |
public static final int EDIT_CHOICES_BEFORE_SENDING_DISABLED = 1; |
| 108 |
|
| 109 |
|
| 110 |
public static final int EDIT_CHOICES_BEFORE_SENDING_ENABLED = 2; |
| 111 |
|
| 112 |
|
| 113 |
private static final int FLAG_ALLOW_FREE_FORM_INPUT = 0x1; |
| 114 |
|
| 115 |
|
| 116 |
private static final int DEFAULT_FLAGS = FLAG_ALLOW_FREE_FORM_INPUT; |
| 117 |
|
| 118 |
private final String mResultKey; |
| 119 |
private final CharSequence mLabel; |
| 120 |
private final CharSequence[] mChoices; |
| 121 |
private final int mFlags; |
| 122 |
@EditChoicesBeforeSending private final int mEditChoicesBeforeSending; |
| 123 |
private final Bundle mExtras; |
| 124 |
private final ArraySet<String> mAllowedDataTypes; |
| 125 |
|
| 126 |
private RemoteInput(String resultKey, CharSequence label, CharSequence[] choices, |
| 127 |
int flags, int editChoicesBeforeSending, Bundle extras, |
| 128 |
ArraySet<String> allowedDataTypes) { |
| 129 |
this.mResultKey = resultKey; |
| 130 |
this.mLabel = label; |
| 131 |
this.mChoices = choices; |
| 132 |
this.mFlags = flags; |
| 133 |
this.mEditChoicesBeforeSending = editChoicesBeforeSending; |
| 134 |
this.mExtras = extras; |
| 135 |
this.mAllowedDataTypes = allowedDataTypes; |
| 136 |
if (getEditChoicesBeforeSending() == EDIT_CHOICES_BEFORE_SENDING_ENABLED |
| 137 |
&& !getAllowFreeFormInput()) { |
| 138 |
throw new IllegalArgumentException( |
| 139 |
"setEditChoicesBeforeSending requires setAllowFreeFormInput"); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
public String getResultKey() { |
| 148 |
return mResultKey; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
public CharSequence getLabel() { |
| 155 |
return mLabel; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
public CharSequence[] getChoices() { |
| 162 |
return mChoices; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
public Set<String> getAllowedDataTypes() { |
| 171 |
return mAllowedDataTypes; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
public boolean isDataOnly() { |
| 180 |
return !getAllowFreeFormInput() |
| 181 |
&& (getChoices() == null || getChoices().length == 0) |
| 182 |
&& !getAllowedDataTypes().isEmpty(); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
public boolean getAllowFreeFormInput() { |
| 192 |
return (mFlags & FLAG_ALLOW_FREE_FORM_INPUT) != 0; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
@EditChoicesBeforeSending |
| 200 |
public int getEditChoicesBeforeSending() { |
| 201 |
return mEditChoicesBeforeSending; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
public Bundle getExtras() { |
| 208 |
return mExtras; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
public static final class Builder { |
| 215 |
private final String mResultKey; |
| 216 |
private final ArraySet<String> mAllowedDataTypes = new ArraySet<>(); |
| 217 |
private final Bundle mExtras = new Bundle(); |
| 218 |
private CharSequence mLabel; |
| 219 |
private CharSequence[] mChoices; |
| 220 |
private int mFlags = DEFAULT_FLAGS; |
| 221 |
@EditChoicesBeforeSending |
| 222 |
private int mEditChoicesBeforeSending = EDIT_CHOICES_BEFORE_SENDING_AUTO; |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
public Builder(@NonNull String resultKey) { |
| 230 |
if (resultKey == null) { |
| 231 |
throw new IllegalArgumentException("Result key can't be null"); |
| 232 |
} |
| 233 |
mResultKey = resultKey; |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
@NonNull |
| 243 |
public Builder setLabel(@Nullable CharSequence label) { |
| 244 |
mLabel = Notification.safeCharSequence(label); |
| 245 |
return this; |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
@NonNull |
| 261 |
public Builder setChoices(@Nullable CharSequence[] choices) { |
| 262 |
if (choices == null) { |
| 263 |
mChoices = null; |
| 264 |
} else { |
| 265 |
mChoices = new CharSequence[choices.length]; |
| 266 |
for (int i = 0; i < choices.length; i++) { |
| 267 |
mChoices[i] = Notification.safeCharSequence(choices[i]); |
| 268 |
} |
| 269 |
} |
| 270 |
return this; |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
@NonNull |
| 286 |
public Builder setAllowDataType(@NonNull String mimeType, boolean doAllow) { |
| 287 |
if (doAllow) { |
| 288 |
mAllowedDataTypes.add(mimeType); |
| 289 |
} else { |
| 290 |
mAllowedDataTypes.remove(mimeType); |
| 291 |
} |
| 292 |
return this; |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
@NonNull |
| 306 |
public Builder setAllowFreeFormInput(boolean allowFreeFormTextInput) { |
| 307 |
setFlag(FLAG_ALLOW_FREE_FORM_INPUT, allowFreeFormTextInput); |
| 308 |
return this; |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
@NonNull |
| 318 |
public Builder setEditChoicesBeforeSending( |
| 319 |
@EditChoicesBeforeSending int editChoicesBeforeSending) { |
| 320 |
mEditChoicesBeforeSending = editChoicesBeforeSending; |
| 321 |
return this; |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
@NonNull |
| 332 |
public Builder addExtras(@NonNull Bundle extras) { |
| 333 |
if (extras != null) { |
| 334 |
mExtras.putAll(extras); |
| 335 |
} |
| 336 |
return this; |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
@NonNull |
| 345 |
public Bundle getExtras() { |
| 346 |
return mExtras; |
| 347 |
} |
| 348 |
|
| 349 |
private void setFlag(int mask, boolean value) { |
| 350 |
if (value) { |
| 351 |
mFlags |= mask; |
| 352 |
} else { |
| 353 |
mFlags &= ~mask; |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
@NonNull |
| 362 |
public RemoteInput build() { |
| 363 |
return new RemoteInput(mResultKey, mLabel, mChoices, mFlags, mEditChoicesBeforeSending, |
| 364 |
mExtras, mAllowedDataTypes); |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
private RemoteInput(Parcel in) { |
| 369 |
mResultKey = in.readString(); |
| 370 |
mLabel = in.readCharSequence(); |
| 371 |
mChoices = in.readCharSequenceArray(); |
| 372 |
mFlags = in.readInt(); |
| 373 |
mEditChoicesBeforeSending = in.readInt(); |
| 374 |
mExtras = in.readBundle(); |
| 375 |
mAllowedDataTypes = (ArraySet<String>) in.readArraySet(null); |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
public static Map<String, Uri> getDataResultsFromIntent( |
| 395 |
Intent intent, String remoteInputResultKey) { |
| 396 |
Intent clipDataIntent = getClipDataIntentFromIntent(intent); |
| 397 |
if (clipDataIntent == null) { |
| 398 |
return null; |
| 399 |
} |
| 400 |
Map<String, Uri> results = new HashMap<>(); |
| 401 |
Bundle extras = clipDataIntent.getExtras(); |
| 402 |
for (String key : extras.keySet()) { |
| 403 |
if (key.startsWith(EXTRA_DATA_TYPE_RESULTS_DATA)) { |
| 404 |
String mimeType = key.substring(EXTRA_DATA_TYPE_RESULTS_DATA.length()); |
| 405 |
if (mimeType == null || mimeType.isEmpty()) { |
| 406 |
continue; |
| 407 |
} |
| 408 |
Bundle bundle = clipDataIntent.getBundleExtra(key); |
| 409 |
String uriStr = bundle.getString(remoteInputResultKey); |
| 410 |
if (uriStr == null || uriStr.isEmpty()) { |
| 411 |
continue; |
| 412 |
} |
| 413 |
results.put(mimeType, Uri.parse(uriStr)); |
| 414 |
} |
| 415 |
} |
| 416 |
return results.isEmpty() ? null : results; |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
public static Bundle getResultsFromIntent(Intent intent) { |
| 428 |
Intent clipDataIntent = getClipDataIntentFromIntent(intent); |
| 429 |
if (clipDataIntent == null) { |
| 430 |
return null; |
| 431 |
} |
| 432 |
return clipDataIntent.getExtras().getParcelable(EXTRA_RESULTS_DATA); |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
public static void addResultsToIntent(RemoteInput[] remoteInputs, Intent intent, |
| 447 |
Bundle results) { |
| 448 |
Intent clipDataIntent = getClipDataIntentFromIntent(intent); |
| 449 |
if (clipDataIntent == null) { |
| 450 |
clipDataIntent = new Intent(); |
| 451 |
} |
| 452 |
Bundle resultsBundle = clipDataIntent.getBundleExtra(EXTRA_RESULTS_DATA); |
| 453 |
if (resultsBundle == null) { |
| 454 |
resultsBundle = new Bundle(); |
| 455 |
} |
| 456 |
for (RemoteInput remoteInput : remoteInputs) { |
| 457 |
Object result = results.get(remoteInput.getResultKey()); |
| 458 |
if (result instanceof CharSequence) { |
| 459 |
resultsBundle.putCharSequence(remoteInput.getResultKey(), (CharSequence) result); |
| 460 |
} |
| 461 |
} |
| 462 |
clipDataIntent.putExtra(EXTRA_RESULTS_DATA, resultsBundle); |
| 463 |
intent.setClipData(ClipData.newIntent(RESULTS_CLIP_LABEL, clipDataIntent)); |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
public static void addDataResultToIntent(RemoteInput remoteInput, Intent intent, |
| 479 |
Map<String, Uri> results) { |
| 480 |
Intent clipDataIntent = getClipDataIntentFromIntent(intent); |
| 481 |
if (clipDataIntent == null) { |
| 482 |
clipDataIntent = new Intent(); |
| 483 |
} |
| 484 |
for (Map.Entry<String, Uri> entry : results.entrySet()) { |
| 485 |
String mimeType = entry.getKey(); |
| 486 |
Uri uri = entry.getValue(); |
| 487 |
if (mimeType == null) { |
| 488 |
continue; |
| 489 |
} |
| 490 |
Bundle resultsBundle = |
| 491 |
clipDataIntent.getBundleExtra(getExtraResultsKeyForData(mimeType)); |
| 492 |
if (resultsBundle == null) { |
| 493 |
resultsBundle = new Bundle(); |
| 494 |
} |
| 495 |
resultsBundle.putString(remoteInput.getResultKey(), uri.toString()); |
| 496 |
|
| 497 |
clipDataIntent.putExtra(getExtraResultsKeyForData(mimeType), resultsBundle); |
| 498 |
} |
| 499 |
intent.setClipData(ClipData.newIntent(RESULTS_CLIP_LABEL, clipDataIntent)); |
| 500 |
} |
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
public static void setResultsSource(Intent intent, @Source int source) { |
| 516 |
Intent clipDataIntent = getClipDataIntentFromIntent(intent); |
| 517 |
if (clipDataIntent == null) { |
| 518 |
clipDataIntent = new Intent(); |
| 519 |
} |
| 520 |
clipDataIntent.putExtra(EXTRA_RESULTS_SOURCE, source); |
| 521 |
intent.setClipData(ClipData.newIntent(RESULTS_CLIP_LABEL, clipDataIntent)); |
| 522 |
} |
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
@Source |
| 536 |
public static int getResultsSource(Intent intent) { |
| 537 |
Intent clipDataIntent = getClipDataIntentFromIntent(intent); |
| 538 |
if (clipDataIntent == null) { |
| 539 |
return SOURCE_FREE_FORM_INPUT; |
| 540 |
} |
| 541 |
return clipDataIntent.getExtras().getInt(EXTRA_RESULTS_SOURCE, SOURCE_FREE_FORM_INPUT); |
| 542 |
} |
| 543 |
|
| 544 |
private static String getExtraResultsKeyForData(String mimeType) { |
| 545 |
return EXTRA_DATA_TYPE_RESULTS_DATA + mimeType; |
| 546 |
} |
| 547 |
|
| 548 |
@Override |
| 549 |
public int describeContents() { |
| 550 |
return 0; |
| 551 |
} |
| 552 |
|
| 553 |
@Override |
| 554 |
public void writeToParcel(Parcel out, int flags) { |
| 555 |
out.writeString(mResultKey); |
| 556 |
out.writeCharSequence(mLabel); |
| 557 |
out.writeCharSequenceArray(mChoices); |
| 558 |
out.writeInt(mFlags); |
| 559 |
out.writeInt(mEditChoicesBeforeSending); |
| 560 |
out.writeBundle(mExtras); |
| 561 |
out.writeArraySet(mAllowedDataTypes); |
| 562 |
} |
| 563 |
|
| 564 |
public static final @android.annotation.NonNull Creator<RemoteInput> CREATOR = new Creator<RemoteInput>() { |
| 565 |
@Override |
| 566 |
public RemoteInput createFromParcel(Parcel in) { |
| 567 |
return new RemoteInput(in); |
| 568 |
} |
| 569 |
|
| 570 |
@Override |
| 571 |
public RemoteInput[] newArray(int size) { |
| 572 |
return new RemoteInput[size]; |
| 573 |
} |
| 574 |
}; |
| 575 |
|
| 576 |
private static Intent getClipDataIntentFromIntent(Intent intent) { |
| 577 |
ClipData clipData = intent.getClipData(); |
| 578 |
if (clipData == null) { |
| 579 |
return null; |
| 580 |
} |
| 581 |
ClipDescription clipDescription = clipData.getDescription(); |
| 582 |
if (!clipDescription.hasMimeType(ClipDescription.MIMETYPE_TEXT_INTENT)) { |
| 583 |
return null; |
| 584 |
} |
| 585 |
if (!clipDescription.getLabel().equals(RESULTS_CLIP_LABEL)) { |
| 586 |
return null; |
| 587 |
} |
| 588 |
return clipData.getItemAt(0).getIntent(); |
| 589 |
} |
| 590 |
} |