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

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

Don't let toasts "leak" Context instances.

Change-Id: I05a936579e60360cdb3453ad5cbcead8ca03191a
parent fdbec3e4
Loading
Loading
Loading
Loading
+51 −45
Original line number Diff line number Diff line
@@ -33,6 +33,8 @@ 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.
@@ -67,7 +69,6 @@ public class Toast {
     */
    public static final int LENGTH_LONG = 1;

    final Handler mHandler = new Handler();    
    final Context mContext;
    final TN mTN;
    int mDuration;
@@ -87,7 +88,7 @@ public class Toast {
     */
    public Toast(Context context) {
        mContext = context;
        mTN = new TN();
        mTN = new TN(this);
        mY = context.getResources().getDimensionPixelSize(
                com.android.internal.R.dimen.toast_y_offset);
    }
@@ -101,13 +102,10 @@ public class Toast {
        }

        INotificationManager service = getService();

        String pkg = mContext.getPackageName();

        TN tn = mTN;

        try {
            service.enqueueToast(pkg, tn, mDuration);
            service.enqueueToast(pkg, mTN, mDuration);
        } catch (RemoteException e) {
            // Empty
        }
@@ -313,7 +311,9 @@ public class Toast {
        return sService;
    }

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

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

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

        TN() {
        TN(Toast toast) {
            mToast = new WeakReference<Toast>(toast);
            // 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;
@@ -362,14 +364,16 @@ public class Toast {
        }

        public void handleShow() {
            if (localLOGV) Log.v(TAG, "HANDLE SHOW: " + this + " mView=" + mView
                    + " mNextView=" + mNextView);
            if (mView != mNextView) {
            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) {
                    // remove the old view if necessary
                    handleHide();
                mView = mNextView;
                    toast.mView = toast.mNextView;
                    mWM = WindowManagerImpl.getDefault();
                final int gravity = mGravity;
                    final int gravity = toast.mGravity;
                    mParams.gravity = gravity;
                    if ((gravity & Gravity.HORIZONTAL_GRAVITY_MASK) == Gravity.FILL_HORIZONTAL) {
                        mParams.horizontalWeight = 1.0f;
@@ -377,34 +381,36 @@ public class Toast {
                    if ((gravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.FILL_VERTICAL) {
                        mParams.verticalWeight = 1.0f;
                    }
                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);
                    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);
                    }
                    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();
            }
        }

        public void handleHide() {
            if (localLOGV) Log.v(TAG, "HANDLE HIDE: " + this + " mView=" + mView);
            if (mView != null) {
            final Toast toast = mToast.get();
            if (toast != null) {
                if (localLOGV) Log.v(TAG, "HANDLE HIDE: " + this + " mView=" + toast.mView);
                if (toast.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 (mView.getParent() != null) {
                    if (localLOGV) Log.v(
                            TAG, "REMOVE! " + mView + " in " + this);
                    mWM.removeView(mView);
                    if (toast.mView.getParent() != null) {
                        if (localLOGV) Log.v(TAG, "REMOVE! " + toast.mView + " in " + this);
                        mWM.removeView(toast.mView);
                    }
    
                mView = null;
                    toast.mView = null;
                }
            }
        }
    }