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

Commit b66ad57c authored by Dianne Hackborn's avatar Dianne Hackborn
Browse files

Fix issue #33876762 com.android.browser: java.lang.NullPointerException...

Unable to destroy activity {com.android.browser/com.android.browser.BrowserActivity}:
java.lang.NullPointerException at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:2809)

Add a bunch of guards to not call on to the window callback after the
window is destroyed.

Change-Id: I66ca0705f421bafdfe953d03654c8f78d9e68249
parent 6c2193a7
Loading
Loading
Loading
Loading
+59 −42
Original line number Diff line number Diff line
@@ -222,7 +222,7 @@ public class PhoneWindow extends Window implements MenuBuilder.Callback {
        }
        mLayoutInflater.inflate(layoutResID, mContentParent);
        final Callback cb = getCallback();
        if (cb != null) {
        if (cb != null && !isDestroyed()) {
            cb.onContentChanged();
        }
    }
@@ -241,7 +241,7 @@ public class PhoneWindow extends Window implements MenuBuilder.Callback {
        }
        mContentParent.addView(view, params);
        final Callback cb = getCallback();
        if (cb != null) {
        if (cb != null && !isDestroyed()) {
            cb.onContentChanged();
        }
    }
@@ -253,7 +253,7 @@ public class PhoneWindow extends Window implements MenuBuilder.Callback {
        }
        mContentParent.addView(view, params);
        final Callback cb = getCallback();
        if (cb != null) {
        if (cb != null && !isDestroyed()) {
            cb.onContentChanged();
        }
    }
@@ -316,6 +316,10 @@ public class PhoneWindow extends Window implements MenuBuilder.Callback {
     *         returns false.
     */
    public final boolean preparePanel(PanelFeatureState st, KeyEvent event) {
        if (isDestroyed()) {
            return false;
        }

        // Already prepared (isPrepared will be reset to false later)
        if (st.isPrepared)
            return true;
@@ -437,7 +441,7 @@ public class PhoneWindow extends Window implements MenuBuilder.Callback {
        // System.out.println("Open panel: isOpen=" + st.isOpen);

        // Already open, return
        if (st.isOpen) {
        if (st.isOpen || isDestroyed()) {
            return;
        }

@@ -609,7 +613,7 @@ public class PhoneWindow extends Window implements MenuBuilder.Callback {
            closed = true;
        }
        Callback cb = getCallback();
        if (cb != null && closed) {
        if (cb != null && closed && !isDestroyed()) {
            cb.onPanelClosed(FEATURE_ACTION_BAR, menu);
        }
        mClosingActionMenu = false;
@@ -688,7 +692,7 @@ public class PhoneWindow extends Window implements MenuBuilder.Callback {
                if (mActionBar.getVisibility() == View.VISIBLE) {
                    if (!mActionBar.isOverflowMenuShowing()) {
                        final Callback cb = getCallback();
                        if (cb != null &&
                        if (cb != null && !isDestroyed() &&
                                cb.onPreparePanel(featureId, st.createdPanelView, st.menu)) {
                            playSoundEffect = mActionBar.showOverflowMenu();
                        }
@@ -837,7 +841,7 @@ public class PhoneWindow extends Window implements MenuBuilder.Callback {

    public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) {
        final Callback cb = getCallback();
        if (cb != null) {
        if (cb != null && !isDestroyed()) {
            final PanelFeatureState panel = findMenuPanel(menu.getRootMenu());
            if (panel != null) {
                return cb.onMenuItemSelected(panel.featureId, item);
@@ -881,7 +885,7 @@ public class PhoneWindow extends Window implements MenuBuilder.Callback {
                    mActionButtonPopup = new ActionButtonSubmenu(getContext(), subMenu);
                    mActionButtonPopup.show();
                    Callback cb = getCallback();
                    if (cb != null) {
                    if (cb != null && !isDestroyed()) {
                        cb.onMenuOpened(FEATURE_ACTION_BAR, subMenu);
                    }
                }
@@ -902,7 +906,7 @@ public class PhoneWindow extends Window implements MenuBuilder.Callback {
        if (mActionBar != null) {
            final Callback cb = getCallback();
            if (!mActionBar.isOverflowMenuShowing() || !toggleMenuMode) {
                if (cb != null && mActionBar.getVisibility() == View.VISIBLE) {
                if (cb != null && !isDestroyed() && mActionBar.getVisibility() == View.VISIBLE) {
                    final PanelFeatureState st = getPanelState(FEATURE_OPTIONS_PANEL, true);
                    if (cb.onPreparePanel(FEATURE_OPTIONS_PANEL, st.createdPanelView, st.menu)) {
                        cb.onMenuOpened(FEATURE_ACTION_BAR, st.menu);
@@ -911,7 +915,7 @@ public class PhoneWindow extends Window implements MenuBuilder.Callback {
                }
            } else {
                mActionBar.hideOverflowMenu();
                if (cb != null) {
                if (cb != null && !isDestroyed()) {
                    final PanelFeatureState st = getPanelState(FEATURE_OPTIONS_PANEL, true);
                    cb.onPanelClosed(FEATURE_ACTION_BAR, st.menu);
                }
@@ -1640,12 +1644,14 @@ public class PhoneWindow extends Window implements MenuBuilder.Callback {
                return true;
            }

            if (!isDestroyed()) {
                final Callback cb = getCallback();
                final boolean handled = cb != null && mFeatureId < 0 ? cb.dispatchKeyEvent(event)
                        : super.dispatchKeyEvent(event);
                if (handled) {
                    return true;
                }
            }
            return isDown ? PhoneWindow.this.onKeyDown(mFeatureId, event.getKeyCode(), event)
                    : PhoneWindow.this.onKeyUp(mFeatureId, event.getKeyCode(), event);
        }
@@ -1666,29 +1672,29 @@ public class PhoneWindow extends Window implements MenuBuilder.Callback {

            // Shortcut not handled by the panel.  Dispatch to the view hierarchy.
            final Callback cb = getCallback();
            return cb != null && mFeatureId < 0 ? cb.dispatchKeyShortcutEvent(ev) : super
                    .dispatchKeyShortcutEvent(ev);
            return cb != null && !isDestroyed() && mFeatureId < 0 ? cb.dispatchKeyShortcutEvent(ev)
                    : super.dispatchKeyShortcutEvent(ev);
        }

        @Override
        public boolean dispatchTouchEvent(MotionEvent ev) {
            final Callback cb = getCallback();
            return cb != null && mFeatureId < 0 ? cb.dispatchTouchEvent(ev) : super
                    .dispatchTouchEvent(ev);
            return cb != null && !isDestroyed() && mFeatureId < 0 ? cb.dispatchTouchEvent(ev)
                    : super.dispatchTouchEvent(ev);
        }

        @Override
        public boolean dispatchTrackballEvent(MotionEvent ev) {
            final Callback cb = getCallback();
            return cb != null && mFeatureId < 0 ? cb.dispatchTrackballEvent(ev) : super
                    .dispatchTrackballEvent(ev);
            return cb != null && !isDestroyed() && mFeatureId < 0 ? cb.dispatchTrackballEvent(ev)
                    : super.dispatchTrackballEvent(ev);
        }

        @Override
        public boolean dispatchGenericMotionEvent(MotionEvent ev) {
            final Callback cb = getCallback();
            return cb != null && mFeatureId < 0 ? cb.dispatchGenericMotionEvent(ev) : super
                    .dispatchGenericMotionEvent(ev);
            return cb != null && !isDestroyed() && mFeatureId < 0 ? cb.dispatchGenericMotionEvent(ev)
                    : super.dispatchGenericMotionEvent(ev);
        }

        public boolean superDispatchKeyEvent(KeyEvent event) {
@@ -1821,7 +1827,7 @@ public class PhoneWindow extends Window implements MenuBuilder.Callback {
        @Override
        public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
            final Callback cb = getCallback();
            if (cb != null) {
            if (cb != null && !isDestroyed()) {
                if (cb.dispatchPopulateAccessibilityEvent(event)) {
                    return true;
                }
@@ -1952,11 +1958,13 @@ public class PhoneWindow extends Window implements MenuBuilder.Callback {

            final ActionMode.Callback wrappedCallback = new ActionModeCallbackWrapper(callback);
            ActionMode mode = null;
            if (getCallback() != null && !isDestroyed()) {
                try {
                    mode = getCallback().onWindowStartingActionMode(wrappedCallback);
                } catch (AbstractMethodError ame) {
                    // Older apps might not implement this callback method.
                }
            }
            if (mode != null) {
                mActionMode = mode;
            } else {
@@ -2002,7 +2010,7 @@ public class PhoneWindow extends Window implements MenuBuilder.Callback {
                    }
                }
            }
            if (mActionMode != null) {
            if (mActionMode != null && getCallback() != null && !isDestroyed()) {
                try {
                    getCallback().onActionModeStarted(mActionMode);
                } catch (AbstractMethodError ame) {
@@ -2134,7 +2142,7 @@ public class PhoneWindow extends Window implements MenuBuilder.Callback {
            }

            final Callback cb = getCallback();
            if (cb != null && mFeatureId < 0) {
            if (cb != null && !isDestroyed() && mFeatureId < 0) {
                cb.onWindowFocusChanged(hasWindowFocus);
            }
        }
@@ -2152,7 +2160,7 @@ public class PhoneWindow extends Window implements MenuBuilder.Callback {
            updateWindowResizeState();
            
            final Callback cb = getCallback();
            if (cb != null && mFeatureId < 0) {
            if (cb != null && !isDestroyed() && mFeatureId < 0) {
                cb.onAttachedToWindow();
            }

@@ -2173,7 +2181,7 @@ public class PhoneWindow extends Window implements MenuBuilder.Callback {
            super.onDetachedFromWindow();
            
            final Callback cb = getCallback();
            if (cb != null && mFeatureId < 0) {
            if (cb != null && !isDestroyed() && mFeatureId < 0) {
                cb.onDetachedFromWindow();
            }

@@ -2253,11 +2261,13 @@ public class PhoneWindow extends Window implements MenuBuilder.Callback {
                if (mActionModeView != null) {
                    mActionModeView.removeAllViews();
                }
                if (getCallback() != null && !isDestroyed()) {
                    try {
                        getCallback().onActionModeFinished(mActionMode);
                    } catch (AbstractMethodError ame) {
                        // Older apps might not implement this callback method.
                    }
                }
                mActionMode = null;
            }
        }
@@ -2805,8 +2815,10 @@ public class PhoneWindow extends Window implements MenuBuilder.Callback {
        if ((panel != null) && (!panel.isOpen))
            return;

        if (!isDestroyed()) {
            cb.onPanelClosed(featureId, menu);
        }
    }

    /**
     * Helper method for adding launch-search to most applications. Opens the
@@ -2816,7 +2828,7 @@ public class PhoneWindow extends Window implements MenuBuilder.Callback {
     */
    private boolean launchDefaultSearch() {
        final Callback cb = getCallback();
        if (cb == null) {
        if (cb == null || isDestroyed()) {
            return false;
        } else {
            sendCloseSystemWindows("search");
@@ -3050,7 +3062,9 @@ public class PhoneWindow extends Window implements MenuBuilder.Callback {
        public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {
            if (allMenusAreClosing) {
                Callback callback = getCallback();
                if (callback != null) callback.onPanelClosed(mFeatureId, menu);
                if (callback != null && !isDestroyed()) {
                    callback.onPanelClosed(mFeatureId, menu);
                }

                if (menu == mContextMenu) {
                    dismissContextMenu();
@@ -3066,12 +3080,15 @@ public class PhoneWindow extends Window implements MenuBuilder.Callback {

        public void onCloseSubMenu(SubMenuBuilder menu) {
            Callback callback = getCallback();
            if (callback != null) callback.onPanelClosed(mFeatureId, menu.getRootMenu());
            if (callback != null && !isDestroyed()) {
                callback.onPanelClosed(mFeatureId, menu.getRootMenu());
            }
        }

        public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) {
            Callback callback = getCallback();
            return (callback != null) && callback.onMenuItemSelected(mFeatureId, item);
            return (callback != null && !isDestroyed())
                    && callback.onMenuItemSelected(mFeatureId, item);
        }

        public void onMenuModeChange(MenuBuilder menu) {
@@ -3130,7 +3147,7 @@ public class PhoneWindow extends Window implements MenuBuilder.Callback {
        public void run() {
            if (tryShow()) {
                Callback cb = getCallback();
                if (cb != null) {
                if (cb != null && !isDestroyed()) {
                    cb.onMenuOpened(FEATURE_ACTION_BAR, mSubMenu);
                }
            }