| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.widget; |
| 18 |
|
| 19 |
import static com.android.internal.util.Preconditions.checkNotNull; |
| 20 |
import static com.android.internal.util.Preconditions.checkState; |
| 21 |
|
| 22 |
import android.annotation.IntDef; |
| 23 |
import android.annotation.NonNull; |
| 24 |
import android.annotation.Nullable; |
| 25 |
import android.annotation.StringRes; |
| 26 |
import android.app.INotificationManager; |
| 27 |
import android.app.ITransientNotification; |
| 28 |
import android.app.ITransientNotificationCallback; |
| 29 |
import android.compat.Compatibility; |
| 30 |
import android.compat.annotation.ChangeId; |
| 31 |
import android.compat.annotation.EnabledAfter; |
| 32 |
import android.compat.annotation.UnsupportedAppUsage; |
| 33 |
import android.content.Context; |
| 34 |
import android.content.res.Resources; |
| 35 |
import android.graphics.drawable.Drawable; |
| 36 |
import android.os.Binder; |
| 37 |
import android.os.Build; |
| 38 |
import android.os.Handler; |
| 39 |
import android.os.IBinder; |
| 40 |
import android.os.Looper; |
| 41 |
import android.os.Message; |
| 42 |
import android.os.RemoteException; |
| 43 |
import android.os.ServiceManager; |
| 44 |
import android.util.Log; |
| 45 |
import android.view.View; |
| 46 |
import android.view.WindowManager; |
| 47 |
import android.view.accessibility.IAccessibilityManager; |
| 48 |
|
| 49 |
import com.android.internal.annotations.GuardedBy; |
| 50 |
|
| 51 |
import java.lang.annotation.Retention; |
| 52 |
import java.lang.annotation.RetentionPolicy; |
| 53 |
import java.util.ArrayList; |
| 54 |
import java.util.List; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public class Toast { |
| 90 |
static final String TAG = "Toast"; |
| 91 |
static final boolean localLOGV = false; |
| 92 |
|
| 93 |
|
| 94 |
@IntDef(prefix = { "LENGTH_" }, value = { |
| 95 |
LENGTH_SHORT, |
| 96 |
LENGTH_LONG |
| 97 |
}) |
| 98 |
@Retention(RetentionPolicy.SOURCE) |
| 99 |
public @interface Duration {} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public static final int LENGTH_SHORT = 0; |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
public static final int LENGTH_LONG = 1; |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
@ChangeId |
| 120 |
@EnabledAfter(targetSdkVersion = Build.VERSION_CODES.Q) |
| 121 |
private static final long CHANGE_TEXT_TOASTS_IN_THE_SYSTEM = 147798919L; |
| 122 |
|
| 123 |
|
| 124 |
private final Binder mToken; |
| 125 |
private final Context mContext; |
| 126 |
private final Handler mHandler; |
| 127 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
| 128 |
final TN mTN; |
| 129 |
@UnsupportedAppUsage |
| 130 |
int mDuration; |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
@GuardedBy("mCallbacks") |
| 137 |
private final List<Callback> mCallbacks; |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
@Nullable |
| 144 |
private View mNextView; |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
@Nullable |
| 151 |
private CharSequence mText; |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
public Toast(Context context) { |
| 161 |
this(context, null); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
public Toast(@NonNull Context context, @Nullable Looper looper) { |
| 169 |
mContext = context; |
| 170 |
mToken = new Binder(); |
| 171 |
looper = getLooper(looper); |
| 172 |
mHandler = new Handler(looper); |
| 173 |
mCallbacks = new ArrayList<>(); |
| 174 |
mTN = new TN(context, context.getPackageName(), mToken, |
| 175 |
mCallbacks, looper); |
| 176 |
mTN.mY = context.getResources().getDimensionPixelSize( |
| 177 |
com.android.internal.R.dimen.toast_y_offset); |
| 178 |
mTN.mGravity = context.getResources().getInteger( |
| 179 |
com.android.internal.R.integer.config_toastDefaultGravity); |
| 180 |
} |
| 181 |
|
| 182 |
private Looper getLooper(@Nullable Looper looper) { |
| 183 |
if (looper != null) { |
| 184 |
return looper; |
| 185 |
} |
| 186 |
return checkNotNull(Looper.myLooper(), |
| 187 |
"Can't toast on a thread that has not called Looper.prepare()"); |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
public void show() { |
| 197 |
if (Compatibility.isChangeEnabled(CHANGE_TEXT_TOASTS_IN_THE_SYSTEM)) { |
| 198 |
checkState(mNextView != null || mText != null, "You must either set a text or a view"); |
| 199 |
} else { |
| 200 |
if (mNextView == null) { |
| 201 |
throw new RuntimeException("setView must have been called"); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
INotificationManager service = getService(); |
| 206 |
String pkg = mContext.getOpPackageName(); |
| 207 |
TN tn = mTN; |
| 208 |
tn.mNextView = mNextView; |
| 209 |
final int displayId = mContext.getDisplayId(); |
| 210 |
|
| 211 |
try { |
| 212 |
if (Compatibility.isChangeEnabled(CHANGE_TEXT_TOASTS_IN_THE_SYSTEM)) { |
| 213 |
if (mNextView != null) { |
| 214 |
|
| 215 |
service.enqueueToast(pkg, mToken, tn, mDuration, displayId); |
| 216 |
} else { |
| 217 |
|
| 218 |
ITransientNotificationCallback callback = |
| 219 |
new CallbackBinder(mCallbacks, mHandler); |
| 220 |
service.enqueueTextToast(pkg, mToken, mText, mDuration, displayId, callback); |
| 221 |
} |
| 222 |
} else { |
| 223 |
service.enqueueToast(pkg, mToken, tn, mDuration, displayId); |
| 224 |
} |
| 225 |
} catch (RemoteException e) { |
| 226 |
|
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
public void cancel() { |
| 236 |
if (Compatibility.isChangeEnabled(CHANGE_TEXT_TOASTS_IN_THE_SYSTEM) |
| 237 |
&& mNextView == null) { |
| 238 |
try { |
| 239 |
getService().cancelToast(mContext.getOpPackageName(), mToken); |
| 240 |
} catch (RemoteException e) { |
| 241 |
|
| 242 |
} |
| 243 |
} else { |
| 244 |
mTN.cancel(); |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
@Deprecated |
| 260 |
public void setView(View view) { |
| 261 |
mNextView = view; |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
@Deprecated |
| 285 |
@Nullable public View getView() { |
| 286 |
return mNextView; |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
public void setDuration(@Duration int duration) { |
| 295 |
mDuration = duration; |
| 296 |
mTN.mDuration = duration; |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
@Duration |
| 304 |
public int getDuration() { |
| 305 |
return mDuration; |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
public void setMargin(float horizontalMargin, float verticalMargin) { |
| 323 |
if (isSystemRenderedTextToast()) { |
| 324 |
Log.e(TAG, "setMargin() shouldn't be called on text toasts, the values won't be used"); |
| 325 |
} |
| 326 |
mTN.mHorizontalMargin = horizontalMargin; |
| 327 |
mTN.mVerticalMargin = verticalMargin; |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
public float getHorizontalMargin() { |
| 339 |
if (isSystemRenderedTextToast()) { |
| 340 |
Log.e(TAG, "getHorizontalMargin() shouldn't be called on text toasts, the result may " |
| 341 |
+ "not reflect actual values."); |
| 342 |
} |
| 343 |
return mTN.mHorizontalMargin; |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
public float getVerticalMargin() { |
| 355 |
if (isSystemRenderedTextToast()) { |
| 356 |
Log.e(TAG, "getVerticalMargin() shouldn't be called on text toasts, the result may not" |
| 357 |
+ " reflect actual values."); |
| 358 |
} |
| 359 |
return mTN.mVerticalMargin; |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
public void setGravity(int gravity, int xOffset, int yOffset) { |
| 373 |
if (isSystemRenderedTextToast()) { |
| 374 |
Log.e(TAG, "setGravity() shouldn't be called on text toasts, the values won't be used"); |
| 375 |
} |
| 376 |
mTN.mGravity = gravity; |
| 377 |
mTN.mX = xOffset; |
| 378 |
mTN.mY = yOffset; |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
public int getGravity() { |
| 393 |
if (isSystemRenderedTextToast()) { |
| 394 |
Log.e(TAG, "getGravity() shouldn't be called on text toasts, the result may not reflect" |
| 395 |
+ " actual values."); |
| 396 |
} |
| 397 |
return mTN.mGravity; |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
public int getXOffset() { |
| 409 |
if (isSystemRenderedTextToast()) { |
| 410 |
Log.e(TAG, "getXOffset() shouldn't be called on text toasts, the result may not reflect" |
| 411 |
+ " actual values."); |
| 412 |
} |
| 413 |
return mTN.mX; |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
public int getYOffset() { |
| 425 |
if (isSystemRenderedTextToast()) { |
| 426 |
Log.e(TAG, "getYOffset() shouldn't be called on text toasts, the result may not reflect" |
| 427 |
+ " actual values."); |
| 428 |
} |
| 429 |
return mTN.mY; |
| 430 |
} |
| 431 |
|
| 432 |
private boolean isSystemRenderedTextToast() { |
| 433 |
return Compatibility.isChangeEnabled(CHANGE_TEXT_TOASTS_IN_THE_SYSTEM) && mNextView == null; |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
public void addCallback(@NonNull Callback callback) { |
| 444 |
checkNotNull(callback); |
| 445 |
synchronized (mCallbacks) { |
| 446 |
mCallbacks.add(callback); |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
public void removeCallback(@NonNull Callback callback) { |
| 454 |
synchronized (mCallbacks) { |
| 455 |
mCallbacks.remove(callback); |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
@UnsupportedAppUsage |
| 464 |
@Nullable public WindowManager.LayoutParams getWindowParams() { |
| 465 |
if (Compatibility.isChangeEnabled(CHANGE_TEXT_TOASTS_IN_THE_SYSTEM)) { |
| 466 |
if (mNextView != null) { |
| 467 |
|
| 468 |
return mTN.mParams; |
| 469 |
} else { |
| 470 |
|
| 471 |
return null; |
| 472 |
} |
| 473 |
} else { |
| 474 |
|
| 475 |
return mTN.mParams; |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
public static Toast makeText(Context context, CharSequence text, @Duration int duration) { |
| 490 |
return makeText(context, null, text, duration); |
| 491 |
} |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
public static Toast makeText(@NonNull Context context, @Nullable Looper looper, |
| 500 |
@NonNull CharSequence text, @Duration int duration) { |
| 501 |
Toast result = new Toast(context, looper); |
| 502 |
|
| 503 |
if (Compatibility.isChangeEnabled(CHANGE_TEXT_TOASTS_IN_THE_SYSTEM)) { |
| 504 |
result.mText = text; |
| 505 |
} else { |
| 506 |
result.mNextView = ToastPresenter.getTextToastView(context, text); |
| 507 |
} |
| 508 |
|
| 509 |
result.mDuration = duration; |
| 510 |
return result; |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
public static Toast makeCustomToastWithIcon(@NonNull Context context, @Nullable Looper looper, |
| 524 |
@NonNull CharSequence text, @Duration int duration, @NonNull Drawable icon) { |
| 525 |
if (icon == null) { |
| 526 |
throw new IllegalArgumentException("Drawable icon should not be null " |
| 527 |
+ "for makeCustomToastWithIcon"); |
| 528 |
} |
| 529 |
|
| 530 |
Toast result = new Toast(context, looper); |
| 531 |
result.mNextView = ToastPresenter.getTextToastViewWithIcon(context, text, icon); |
| 532 |
result.mDuration = duration; |
| 533 |
return result; |
| 534 |
} |
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
public static Toast makeText(Context context, @StringRes int resId, @Duration int duration) |
| 548 |
throws Resources.NotFoundException { |
| 549 |
return makeText(context, context.getResources().getText(resId), duration); |
| 550 |
} |
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
public void setText(@StringRes int resId) { |
| 557 |
setText(mContext.getText(resId)); |
| 558 |
} |
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
public void setText(CharSequence s) { |
| 565 |
if (Compatibility.isChangeEnabled(CHANGE_TEXT_TOASTS_IN_THE_SYSTEM)) { |
| 566 |
if (mNextView != null) { |
| 567 |
throw new IllegalStateException( |
| 568 |
"Text provided for custom toast, remove previous setView() calls if you " |
| 569 |
+ "want a text toast instead."); |
| 570 |
} |
| 571 |
mText = s; |
| 572 |
} else { |
| 573 |
if (mNextView == null) { |
| 574 |
throw new RuntimeException("This Toast was not created with Toast.makeText()"); |
| 575 |
} |
| 576 |
TextView tv = mNextView.findViewById(com.android.internal.R.id.message); |
| 577 |
if (tv == null) { |
| 578 |
throw new RuntimeException("This Toast was not created with Toast.makeText()"); |
| 579 |
} |
| 580 |
tv.setText(s); |
| 581 |
} |
| 582 |
} |
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
| 590 |
private static INotificationManager sService; |
| 591 |
|
| 592 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
| 593 |
static private INotificationManager getService() { |
| 594 |
if (sService != null) { |
| 595 |
return sService; |
| 596 |
} |
| 597 |
sService = INotificationManager.Stub.asInterface( |
| 598 |
ServiceManager.getService(Context.NOTIFICATION_SERVICE)); |
| 599 |
return sService; |
| 600 |
} |
| 601 |
|
| 602 |
private static class TN extends ITransientNotification.Stub { |
| 603 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
| 604 |
private final WindowManager.LayoutParams mParams; |
| 605 |
|
| 606 |
private static final int SHOW = 0; |
| 607 |
private static final int HIDE = 1; |
| 608 |
private static final int CANCEL = 2; |
| 609 |
final Handler mHandler; |
| 610 |
|
| 611 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
| 612 |
int mGravity; |
| 613 |
int mX; |
| 614 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
| 615 |
int mY; |
| 616 |
float mHorizontalMargin; |
| 617 |
float mVerticalMargin; |
| 618 |
|
| 619 |
|
| 620 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
| 621 |
View mView; |
| 622 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
| 623 |
View mNextView; |
| 624 |
int mDuration; |
| 625 |
|
| 626 |
WindowManager mWM; |
| 627 |
|
| 628 |
final String mPackageName; |
| 629 |
final Binder mToken; |
| 630 |
private final ToastPresenter mPresenter; |
| 631 |
|
| 632 |
@GuardedBy("mCallbacks") |
| 633 |
private final List<Callback> mCallbacks; |
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
TN(Context context, String packageName, Binder token, List<Callback> callbacks, |
| 642 |
@Nullable Looper looper) { |
| 643 |
IAccessibilityManager accessibilityManager = IAccessibilityManager.Stub.asInterface( |
| 644 |
ServiceManager.getService(Context.ACCESSIBILITY_SERVICE)); |
| 645 |
mPresenter = new ToastPresenter(context, accessibilityManager, getService(), |
| 646 |
packageName); |
| 647 |
mParams = mPresenter.getLayoutParams(); |
| 648 |
mPackageName = packageName; |
| 649 |
mToken = token; |
| 650 |
mCallbacks = callbacks; |
| 651 |
|
| 652 |
mHandler = new Handler(looper, null) { |
| 653 |
@Override |
| 654 |
public void handleMessage(Message msg) { |
| 655 |
switch (msg.what) { |
| 656 |
case SHOW: { |
| 657 |
IBinder token = (IBinder) msg.obj; |
| 658 |
handleShow(token); |
| 659 |
break; |
| 660 |
} |
| 661 |
case HIDE: { |
| 662 |
handleHide(); |
| 663 |
|
| 664 |
|
| 665 |
mNextView = null; |
| 666 |
break; |
| 667 |
} |
| 668 |
case CANCEL: { |
| 669 |
handleHide(); |
| 670 |
|
| 671 |
|
| 672 |
mNextView = null; |
| 673 |
try { |
| 674 |
getService().cancelToast(mPackageName, mToken); |
| 675 |
} catch (RemoteException e) { |
| 676 |
} |
| 677 |
break; |
| 678 |
} |
| 679 |
} |
| 680 |
} |
| 681 |
}; |
| 682 |
} |
| 683 |
|
| 684 |
private List<Callback> getCallbacks() { |
| 685 |
synchronized (mCallbacks) { |
| 686 |
return new ArrayList<>(mCallbacks); |
| 687 |
} |
| 688 |
} |
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
@Override |
| 694 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
| 695 |
public void show(IBinder windowToken) { |
| 696 |
if (localLOGV) Log.v(TAG, "SHOW: " + this); |
| 697 |
mHandler.obtainMessage(SHOW, windowToken).sendToTarget(); |
| 698 |
} |
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
@Override |
| 704 |
public void hide() { |
| 705 |
if (localLOGV) Log.v(TAG, "HIDE: " + this); |
| 706 |
mHandler.obtainMessage(HIDE).sendToTarget(); |
| 707 |
} |
| 708 |
|
| 709 |
public void cancel() { |
| 710 |
if (localLOGV) Log.v(TAG, "CANCEL: " + this); |
| 711 |
mHandler.obtainMessage(CANCEL).sendToTarget(); |
| 712 |
} |
| 713 |
|
| 714 |
public void handleShow(IBinder windowToken) { |
| 715 |
if (localLOGV) Log.v(TAG, "HANDLE SHOW: " + this + " mView=" + mView |
| 716 |
+ " mNextView=" + mNextView); |
| 717 |
|
| 718 |
|
| 719 |
if (mHandler.hasMessages(CANCEL) || mHandler.hasMessages(HIDE)) { |
| 720 |
return; |
| 721 |
} |
| 722 |
if (mView != mNextView) { |
| 723 |
|
| 724 |
handleHide(); |
| 725 |
mView = mNextView; |
| 726 |
mPresenter.show(mView, mToken, windowToken, mDuration, mGravity, mX, mY, |
| 727 |
mHorizontalMargin, mVerticalMargin, |
| 728 |
new CallbackBinder(getCallbacks(), mHandler)); |
| 729 |
} |
| 730 |
} |
| 731 |
|
| 732 |
@UnsupportedAppUsage |
| 733 |
public void handleHide() { |
| 734 |
if (localLOGV) Log.v(TAG, "HANDLE HIDE: " + this + " mView=" + mView); |
| 735 |
if (mView != null) { |
| 736 |
checkState(mView == mPresenter.getView(), |
| 737 |
"Trying to hide toast view different than the last one displayed"); |
| 738 |
mPresenter.hide(new CallbackBinder(getCallbacks(), mHandler)); |
| 739 |
mView = null; |
| 740 |
} |
| 741 |
} |
| 742 |
} |
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
public abstract static class Callback { |
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
public void onToastShown() {} |
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
public void onToastHidden() {} |
| 760 |
} |
| 761 |
|
| 762 |
private static class CallbackBinder extends ITransientNotificationCallback.Stub { |
| 763 |
private final Handler mHandler; |
| 764 |
|
| 765 |
@GuardedBy("mCallbacks") |
| 766 |
private final List<Callback> mCallbacks; |
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
private CallbackBinder(List<Callback> callbacks, Handler handler) { |
| 775 |
mCallbacks = callbacks; |
| 776 |
mHandler = handler; |
| 777 |
} |
| 778 |
|
| 779 |
@Override |
| 780 |
public void onToastShown() { |
| 781 |
mHandler.post(() -> { |
| 782 |
for (Callback callback : getCallbacks()) { |
| 783 |
callback.onToastShown(); |
| 784 |
} |
| 785 |
}); |
| 786 |
} |
| 787 |
|
| 788 |
@Override |
| 789 |
public void onToastHidden() { |
| 790 |
mHandler.post(() -> { |
| 791 |
for (Callback callback : getCallbacks()) { |
| 792 |
callback.onToastHidden(); |
| 793 |
} |
| 794 |
}); |
| 795 |
} |
| 796 |
|
| 797 |
private List<Callback> getCallbacks() { |
| 798 |
synchronized (mCallbacks) { |
| 799 |
return new ArrayList<>(mCallbacks); |
| 800 |
} |
| 801 |
} |
| 802 |
} |
| 803 |
} |