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

Commit ae6c8788 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Add Callback API to Toast"

parents d5a97cc0 3231f610
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -59223,6 +59223,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();
@@ -59233,6 +59234,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);
@@ -59244,6 +59246,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() {}
    }
}