Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3231f610 authored by Bernardo Rufino's avatar Bernardo Rufino
Browse files

Add Callback API to Toast

In preparation for moving text toasts to system UI, we need a new way of
telling if a toast is being displayed or not since the view won't live
in the app's process. This is needed for our CTS tests and good to have
for developers so we don't take the functionality away from them, so
making it a public API.

Also updated tests in topic CL.

Bug: 128611929
Bug: 144754526
Test: atest android.widget.cts.ToastTest
Change-Id: I41ff1ed11d5af66e415c772c2e5f611a3d1b17fc
parent d8d553b9
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -58417,6 +58417,7 @@ package android.widget {
  public class Toast {
    ctor public Toast(android.content.Context);
    method public void addCallback(@NonNull android.widget.Toast.Callback);
    method public void cancel();
    method public int getDuration();
    method public int getGravity();
@@ -58427,6 +58428,7 @@ package android.widget {
    method public int getYOffset();
    method public static android.widget.Toast makeText(android.content.Context, CharSequence, int);
    method public static android.widget.Toast makeText(android.content.Context, @StringRes int, int) throws android.content.res.Resources.NotFoundException;
    method public void removeCallback(@NonNull android.widget.Toast.Callback);
    method public void setDuration(int);
    method public void setGravity(int, int, int);
    method public void setMargin(float, float);
@@ -58438,6 +58440,12 @@ package android.widget {
    field public static final int LENGTH_SHORT = 0; // 0x0
  }
  public abstract static class Toast.Callback {
    ctor public Toast.Callback();
    method public void onToastHidden();
    method public void onToastShown();
  }
  public class ToggleButton extends android.widget.CompoundButton {
    ctor public ToggleButton(android.content.Context, android.util.AttributeSet, int, int);
    ctor public ToggleButton(android.content.Context, android.util.AttributeSet, int);
+63 −0
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package android.widget;

import static com.android.internal.util.Preconditions.checkNotNull;

import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
@@ -42,8 +44,12 @@ import android.view.WindowManager;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityManager;

import com.android.internal.annotations.GuardedBy;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.List;

/**
 * A toast is a view containing a quick little message for the user.  The toast class
@@ -261,6 +267,29 @@ public class Toast {
        return mTN.mY;
    }

    /**
     * Adds a callback to be notified when the toast is shown or hidden.
     *
     * Note that if the toast is blocked for some reason you won't get a call back.
     *
     * @see #removeCallback(Callback)
     */
    public void addCallback(@NonNull Callback callback) {
        checkNotNull(callback);
        synchronized (mTN.mCallbacks) {
            mTN.mCallbacks.add(callback);
        }
    }

    /**
     * Removes a callback previously added with {@link #addCallback(Callback)}.
     */
    public void removeCallback(@NonNull Callback callback) {
        synchronized (mTN.mCallbacks) {
            mTN.mCallbacks.remove(callback);
        }
    }

    /**
     * Gets the LayoutParams for the Toast window.
     * @hide
@@ -389,6 +418,9 @@ public class Toast {

        String mPackageName;

        @GuardedBy("mCallbacks")
        private final List<Callback> mCallbacks = new ArrayList<>();

        static final long SHORT_DURATION_TIMEOUT = 4000;
        static final long LONG_DURATION_TIMEOUT = 7000;

@@ -449,6 +481,12 @@ public class Toast {
            };
        }

        private List<Callback> getCallbacks() {
            synchronized (mCallbacks) {
                return new ArrayList<>(mCallbacks);
            }
        }

        /**
         * schedule handleShow into the right thread
         */
@@ -522,6 +560,9 @@ public class Toast {
                try {
                    mWM.addView(mView, mParams);
                    trySendAccessibilityEvent();
                    for (Callback callback : getCallbacks()) {
                        callback.onToastShown();
                    }
                } catch (WindowManager.BadTokenException e) {
                    /* ignore */
                }
@@ -564,8 +605,30 @@ public class Toast {
                } catch (RemoteException e) {
                }

                for (Callback callback : getCallbacks()) {
                    callback.onToastHidden();
                }
                mView = null;
            }
        }
    }

    /**
     * Callback object to be called when the toast is shown or hidden.
     *
     * Callback methods will be called on the looper thread provided on construction.
     *
     * @see #addCallback(Callback)
     */
    public abstract static class Callback {
        /**
         * Called when the toast is displayed on the screen.
         */
        public void onToastShown() {}

        /**
         * Called when the toast is hidden.
         */
        public void onToastHidden() {}
    }
}