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

Commit cf3dd6bc authored by Romain Guy's avatar Romain Guy
Browse files

Fix toasts.

Change-Id: I11916c55e8092169d2ab28c100bc7a152d60d432
parent 514fb188
Loading
Loading
Loading
Loading
+78 −81
Original line number Diff line number Diff line
@@ -33,8 +33,6 @@ import android.view.WindowManagerImpl;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityManager;

import java.lang.ref.WeakReference;

/**
 * A toast is a view containing a quick little message for the user.  The toast class
 * helps you create and show those.
@@ -72,11 +70,6 @@ public class Toast {
    final Context mContext;
    final TN mTN;
    int mDuration;
    int mGravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
    int mX, mY;
    float mHorizontalMargin;
    float mVerticalMargin;
    View mView;
    View mNextView;

    /**
@@ -88,8 +81,8 @@ public class Toast {
     */
    public Toast(Context context) {
        mContext = context;
        mTN = new TN(this);
        mY = context.getResources().getDimensionPixelSize(
        mTN = new TN();
        mTN.mY = context.getResources().getDimensionPixelSize(
                com.android.internal.R.dimen.toast_y_offset);
    }
    
@@ -103,9 +96,11 @@ public class Toast {

        INotificationManager service = getService();
        String pkg = mContext.getPackageName();
        TN tn = mTN;
        tn.mNextView = mNextView;

        try {
            service.enqueueToast(pkg, mTN, mDuration);
            service.enqueueToast(pkg, tn, mDuration);
        } catch (RemoteException e) {
            // Empty
        }
@@ -165,22 +160,22 @@ public class Toast {
     *        notification
     */
    public void setMargin(float horizontalMargin, float verticalMargin) {
        mHorizontalMargin = horizontalMargin;
        mVerticalMargin = verticalMargin;
        mTN.mHorizontalMargin = horizontalMargin;
        mTN.mVerticalMargin = verticalMargin;
    }

    /**
     * Return the horizontal margin.
     */
    public float getHorizontalMargin() {
        return mHorizontalMargin;
        return mTN.mHorizontalMargin;
    }

    /**
     * Return the vertical margin.
     */
    public float getVerticalMargin() {
        return mVerticalMargin;
        return mTN.mVerticalMargin;
    }

    /**
@@ -189,9 +184,9 @@ public class Toast {
     * @see #getGravity
     */
    public void setGravity(int gravity, int xOffset, int yOffset) {
        mGravity = gravity;
        mX = xOffset;
        mY = yOffset;
        mTN.mGravity = gravity;
        mTN.mX = xOffset;
        mTN.mY = yOffset;
    }

     /**
@@ -200,21 +195,21 @@ public class Toast {
     * @see #getGravity
     */
    public int getGravity() {
        return mGravity;
        return mTN.mGravity;
    }

    /**
     * Return the X offset in pixels to apply to the gravity's location.
     */
    public int getXOffset() {
        return mX;
        return mTN.mX;
    }
    
    /**
     * Return the Y offset in pixels to apply to the gravity's location.
     */
    public int getYOffset() {
        return mY;
        return mTN.mY;
    }
    
    /**
@@ -281,21 +276,6 @@ public class Toast {
        tv.setText(s);
    }

    private void trySendAccessibilityEvent() {
        AccessibilityManager accessibilityManager = AccessibilityManager.getInstance(mContext);
        if (!accessibilityManager.isEnabled()) {
            return;
        }
        // treat toasts as notifications since they are used to
        // announce a transient piece of information to the user
        AccessibilityEvent event = AccessibilityEvent.obtain(
                AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
        event.setClassName(getClass().getName());
        event.setPackageName(mContext.getPackageName());
        mView.dispatchPopulateAccessibilityEvent(event);
        accessibilityManager.sendAccessibilityEvent(event);
    }

    // =======================================================================================
    // All the gunk below is the interaction with the Notification Service, which handles
    // the proper ordering of these system-wide.
@@ -312,8 +292,6 @@ public class Toast {
    }

    private static class TN extends ITransientNotification.Stub {
        final Handler mHandler = new Handler();    

        final Runnable mShow = new Runnable() {
            public void run() {
                handleShow();
@@ -327,12 +305,20 @@ public class Toast {
        };

        private final WindowManager.LayoutParams mParams = new WindowManager.LayoutParams();
        private final WeakReference<Toast> mToast;
        final Handler mHandler = new Handler();    

        int mGravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
        int mX, mY;
        float mHorizontalMargin;
        float mVerticalMargin;

       
        View mView;
        View mNextView;
        
        WindowManagerImpl mWM;

        TN(Toast toast) {
            mToast = new WeakReference<Toast>(toast);
        TN() {
            // XXX This should be changed to use a Dialog, with a Theme.Toast
            // defined that sets up the layout params appropriately.
            final WindowManager.LayoutParams params = mParams;
@@ -364,16 +350,14 @@ public class Toast {
        }

        public void handleShow() {
            final Toast toast = mToast.get();
            if (toast != null) {
                if (localLOGV) Log.v(TAG, "HANDLE SHOW: " + this + " mView=" + toast.mView
                        + " mNextView=" + toast.mNextView);
                if (toast.mView != toast.mNextView) {
            if (localLOGV) Log.v(TAG, "HANDLE SHOW: " + this + " mView=" + mView
                    + " mNextView=" + mNextView);
            if (mView != mNextView) {
                // remove the old view if necessary
                handleHide();
                    toast.mView = toast.mNextView;
                mView = mNextView;
                mWM = WindowManagerImpl.getDefault();
                    final int gravity = toast.mGravity;
                final int gravity = mGravity;
                mParams.gravity = gravity;
                if ((gravity & Gravity.HORIZONTAL_GRAVITY_MASK) == Gravity.FILL_HORIZONTAL) {
                    mParams.horizontalWeight = 1.0f;
@@ -381,36 +365,49 @@ public class Toast {
                if ((gravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.FILL_VERTICAL) {
                    mParams.verticalWeight = 1.0f;
                }
                    mParams.x = toast.mX;
                    mParams.y = toast.mY;
                    mParams.verticalMargin = toast.mVerticalMargin;
                    mParams.horizontalMargin = toast.mHorizontalMargin;
                    if (toast.mView.getParent() != null) {
                        if (localLOGV) Log.v(TAG, "REMOVE! " + toast.mView + " in " + this);
                        mWM.removeView(toast.mView);
                mParams.x = mX;
                mParams.y = mY;
                mParams.verticalMargin = mVerticalMargin;
                mParams.horizontalMargin = mHorizontalMargin;
                if (mView.getParent() != null) {
                    if (localLOGV) Log.v(TAG, "REMOVE! " + mView + " in " + this);
                    mWM.removeView(mView);
                }
                    if (localLOGV) Log.v(TAG, "ADD! " + toast.mView + " in " + this);
                    mWM.addView(toast.mView, mParams);
                    toast.trySendAccessibilityEvent();
                if (localLOGV) Log.v(TAG, "ADD! " + mView + " in " + this);
                mWM.addView(mView, mParams);
                trySendAccessibilityEvent();
            }
        }

        private void trySendAccessibilityEvent() {
            AccessibilityManager accessibilityManager =
                    AccessibilityManager.getInstance(mView.getContext());
            if (!accessibilityManager.isEnabled()) {
                return;
            }
            // treat toasts as notifications since they are used to
            // announce a transient piece of information to the user
            AccessibilityEvent event = AccessibilityEvent.obtain(
                    AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
            event.setClassName(getClass().getName());
            event.setPackageName(mView.getContext().getPackageName());
            mView.dispatchPopulateAccessibilityEvent(event);
            accessibilityManager.sendAccessibilityEvent(event);
        }        

        public void handleHide() {
            final Toast toast = mToast.get();
            if (toast != null) {
                if (localLOGV) Log.v(TAG, "HANDLE HIDE: " + this + " mView=" + toast.mView);
                if (toast.mView != null) {
            if (localLOGV) Log.v(TAG, "HANDLE HIDE: " + this + " mView=" + mView);
            if (mView != null) {
                // note: checking parent() just to make sure the view has
                // been added...  i have seen cases where we get here when
                // the view isn't yet added, so let's try not to crash.
                    if (toast.mView.getParent() != null) {
                        if (localLOGV) Log.v(TAG, "REMOVE! " + toast.mView + " in " + this);
                        mWM.removeView(toast.mView);
                if (mView.getParent() != null) {
                    if (localLOGV) Log.v(TAG, "REMOVE! " + mView + " in " + this);
                    mWM.removeView(mView);
                }

                    toast.mView = null;
                }
                mView = null;
                mNextView = null;
            }
        }
    }