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

Commit d94094e0 authored by John Reck's avatar John Reck
Browse files

Illegal behavior -> Exception

Throw an IllegalStateException if the contract
of OnDrawListener is violated.

Change-Id: Ic86f6a0c8cd69ee7a8daa0ba135a3df76f56a213
parent b5c30800
Loading
Loading
Loading
Loading
+5 −3
Original line number Diff line number Diff line
@@ -19108,7 +19108,7 @@ public class View implements Drawable.Callback, KeyEvent.Callback,
            return mAttachInfo.mTreeObserver;
        }
        if (mFloatingTreeObserver == null) {
            mFloatingTreeObserver = new ViewTreeObserver();
            mFloatingTreeObserver = new ViewTreeObserver(mContext);
        }
        return mFloatingTreeObserver;
    }
@@ -23057,7 +23057,7 @@ public class View implements Drawable.Callback, KeyEvent.Callback,
         * The view tree observer used to dispatch global events like
         * layout, pre-draw, touch mode change, etc.
         */
        final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
        final ViewTreeObserver mTreeObserver;
        /**
         * A Canvas used by the view hierarchy to perform bitmap caching.
@@ -23179,7 +23179,8 @@ public class View implements Drawable.Callback, KeyEvent.Callback,
         * @param handler the events handler the view must use
         */
        AttachInfo(IWindowSession session, IWindow window, Display display,
                ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer) {
                ViewRootImpl viewRootImpl, Handler handler, Callbacks effectPlayer,
                Context context) {
            mSession = session;
            mWindow = window;
            mWindowToken = window.asBinder();
@@ -23187,6 +23188,7 @@ public class View implements Drawable.Callback, KeyEvent.Callback,
            mViewRootImpl = viewRootImpl;
            mHandler = handler;
            mRootCallbacks = effectPlayer;
            mTreeObserver = new ViewTreeObserver(context);
        }
    }
+2 −1
Original line number Diff line number Diff line
@@ -429,7 +429,8 @@ public final class ViewRootImpl implements ViewParent,
        mPreviousTransparentRegion = new Region();
        mFirst = true; // true for the first time the view is added
        mAdded = false;
        mAttachInfo = new View.AttachInfo(mWindowSession, mWindow, display, this, mHandler, this);
        mAttachInfo = new View.AttachInfo(mWindowSession, mWindow, display, this, mHandler, this,
                context);
        mAccessibilityManager = AccessibilityManager.getInstance(context);
        mAccessibilityInteractionConnectionManager =
            new AccessibilityInteractionConnectionManager();
+28 −1
Original line number Diff line number Diff line
@@ -16,8 +16,11 @@

package android.view;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.Region;
import android.os.Build;
import android.util.Log;

import java.util.ArrayList;
import java.util.concurrent.CopyOnWriteArrayList;
@@ -49,7 +52,9 @@ public final class ViewTreeObserver {
    private CopyOnWriteArray<OnWindowShownListener> mOnWindowShownListeners;

    // These listeners cannot be mutated during dispatch
    private boolean mInDispatchOnDraw;
    private ArrayList<OnDrawListener> mOnDrawListeners;
    private static boolean sIllegalOnDrawModificationIsFatal;

    /** Remains false until #dispatchOnWindowShown() is called. If a listener registers after
     * that the listener will be immediately called. */
@@ -327,7 +332,9 @@ public final class ViewTreeObserver {
    /**
     * Creates a new ViewTreeObserver. This constructor should not be called
     */
    ViewTreeObserver() {
    ViewTreeObserver(Context context) {
        sIllegalOnDrawModificationIsFatal =
                context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.N_MR1;
    }

    /**
@@ -657,6 +664,15 @@ public final class ViewTreeObserver {
            mOnDrawListeners = new ArrayList<OnDrawListener>();
        }

        if (mInDispatchOnDraw) {
            IllegalStateException ex = new IllegalStateException(
                    "Cannot call addOnDrawListener inside of onDraw");
            if (sIllegalOnDrawModificationIsFatal) {
                throw ex;
            } else {
                Log.e("ViewTreeObserver", ex.getMessage(), ex);
            }
        }
        mOnDrawListeners.add(listener);
    }

@@ -676,6 +692,15 @@ public final class ViewTreeObserver {
        if (mOnDrawListeners == null) {
            return;
        }
        if (mInDispatchOnDraw) {
            IllegalStateException ex = new IllegalStateException(
                    "Cannot call removeOnDrawListener inside of onDraw");
            if (sIllegalOnDrawModificationIsFatal) {
                throw ex;
            } else {
                Log.e("ViewTreeObserver", ex.getMessage(), ex);
            }
        }
        mOnDrawListeners.remove(victim);
    }

@@ -976,11 +1001,13 @@ public final class ViewTreeObserver {
     */
    public final void dispatchOnDraw() {
        if (mOnDrawListeners != null) {
            mInDispatchOnDraw = true;
            final ArrayList<OnDrawListener> listeners = mOnDrawListeners;
            int numListeners = listeners.size();
            for (int i = 0; i < numListeners; ++i) {
                listeners.get(i).onDraw();
            }
            mInDispatchOnDraw = false;
        }
    }

+1 −1
Original line number Diff line number Diff line
@@ -34,7 +34,7 @@ public class AttachInfo_Accessor {
        Display display = wm.getDefaultDisplay();
        ViewRootImpl root = new ViewRootImpl(context, display);
        AttachInfo info = new AttachInfo(new BridgeWindowSession(), new BridgeWindow(),
                display, root, new Handler(), null);
                display, root, new Handler(), null, context);
        info.mHasWindowFocus = true;
        info.mWindowVisibility = View.VISIBLE;
        info.mInTouchMode = false; // this is so that we can display selections.