From 7b843abc1c7cd53096557909f31216b93c3ca674 Mon Sep 17 00:00:00 2001 From: Bernardo Rufino Date: Fri, 17 Jan 2020 19:15:36 +0000 Subject: Deprecate custom toasts Deprecating custom toasts as discussed, the reasons are: * We're blocking background custom toasts for security reasons (go/toast-abuse). * This means custom toasts are only possible if the app is in the foreground. In the foreground the app has control over its own view hierarchy and is capable of creating any visual elements it would otherwise use custom toasts for. * If we were to declare ongoing support for foreground toasts the developer would be in a situation where they either check for foreground status of the app before posting a custom toast or they accept that the information they want to display may not be shown at all. This is not great. * There is also a desire to avoid custom toasts altogether since they hurt UX consistency (https://docs.google.com/presentation/d/1r5WEofZ_G3B9M65nS37uD4RqA4iV9HUmngyE6ZpBSsw/edit#slide=id.g7b69852da2_0_0). Also added a recommendation to use Snackbars while the app is in the foreground. Bug: 128611929 Bug: 144754526 Test: Builds Test: atest android.widget.cts.ToastTest Change-Id: I4ed0bd671e308491590c28ff24087c76d0623f13 --- api/current.txt | 4 ++-- core/java/android/widget/Toast.java | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/api/current.txt b/api/current.txt index 04f3399bede6..c482dd0fe693 100644 --- a/api/current.txt +++ b/api/current.txt @@ -59289,7 +59289,7 @@ package android.widget { method public int getGravity(); method public float getHorizontalMargin(); method public float getVerticalMargin(); - method public android.view.View getView(); + method @Deprecated public android.view.View getView(); method public int getXOffset(); method public int getYOffset(); method public static android.widget.Toast makeText(android.content.Context, CharSequence, int); @@ -59300,7 +59300,7 @@ package android.widget { method public void setMargin(float, float); method public void setText(@StringRes int); method public void setText(CharSequence); - method public void setView(android.view.View); + method @Deprecated public void setView(android.view.View); method public void show(); field public static final int LENGTH_LONG = 1; // 0x1 field public static final int LENGTH_SHORT = 0; // 0x0 diff --git a/core/java/android/widget/Toast.java b/core/java/android/widget/Toast.java index d119b2e1992b..4e705db06809 100644 --- a/core/java/android/widget/Toast.java +++ b/core/java/android/widget/Toast.java @@ -66,6 +66,10 @@ import java.util.List; *

* The easiest way to use this class is to call one of the static methods that constructs * everything you need and returns a new Toast object. + *

+ * Note that + * Snackbars are + * preferred for brief messages while the app is in the foreground. * *

*

Developer Guides

@@ -169,8 +173,16 @@ public class Toast { /** * Set the view to show. + * * @see #getView + * @deprecated Custom toast views are deprecated. Apps can create a standard text toast with the + * {@link #makeText(Context, CharSequence, int)} method, or use a + * Snackbar + * when in the foreground. Starting from Android {@link Build.VERSION_CODES#R}, apps + * targeting API level {@link Build.VERSION_CODES#R} or higher that are in the background + * will not have custom toast views displayed. */ + @Deprecated public void setView(View view) { mIsCustomToast = true; mNextView = view; @@ -178,7 +190,14 @@ public class Toast { /** * Return the view. + * * @see #setView + * @deprecated Custom toast views are deprecated. Apps can create a standard text toast with the + * {@link #makeText(Context, CharSequence, int)} method, or use a + * Snackbar + * when in the foreground. Starting from Android {@link Build.VERSION_CODES#R}, apps + * targeting API level {@link Build.VERSION_CODES#R} or higher that are in the background + * will not have custom toast views displayed. */ public View getView() { mIsCustomToast = true; -- cgit v1.2.3-59-g8ed1b