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

Commit d205d5b5 authored by Mike Reed's avatar Mike Reed
Browse files

add onPause and onResume apis, to inform the view when it can pause

its activities associated with the DOM.
parent cc77841f
Loading
Loading
Loading
Loading
+45 −5
Original line number Diff line number Diff line
@@ -370,6 +370,9 @@ public class WebView extends AbsoluteLayout
    // Whether or not to draw the focus ring.
    private boolean mDrawFocusRing = true;

    // true if onPause has been called (and not onResume)
    private boolean mIsPaused;

    /**
     * Customizable constant
     */
@@ -997,7 +1000,7 @@ public class WebView extends AbsoluteLayout

    /**
     * If platform notifications are enabled, this should be called
     * from onPause() or onStop().
     * from the Activity's onPause() or onStop().
     */
    public static void disablePlatformNotifications() {
        Network.disablePlatformNotifications();
@@ -1938,21 +1941,58 @@ public class WebView extends AbsoluteLayout
    }

    /**
     * Pause all layout, parsing, and javascript timers. This can be useful if
     * the WebView is not visible or the application has been paused.
     * Pause all layout, parsing, and javascript timers for all webviews. This
     * is a global requests, not restricted to just this webview. This can be
     * useful if the application has been paused.
     */
    public void pauseTimers() {
        mWebViewCore.sendMessage(EventHub.PAUSE_TIMERS);
    }

    /**
     * Resume all layout, parsing, and javascript timers. This will resume
     * dispatching all timers.
     * Resume all layout, parsing, and javascript timers for all webviews.
     * This will resume dispatching all timers.
     */
    public void resumeTimers() {
        mWebViewCore.sendMessage(EventHub.RESUME_TIMERS);
    }

    /**
     * Call this to pause any extra processing associated with this view and
     * its associated DOM/plugins/javascript/etc. For example, if the view is
     * taken offscreen, this could be called to reduce unnecessary CPU and/or
     * network traffic. When the view is again "active", call onResume().
     *
     * Note that this differs from pauseTimers(), which affects all views/DOMs
     * @hide
     */
    public void onPause() {
        if (!mIsPaused) {
            mIsPaused = true;
            mWebViewCore.sendMessage(EventHub.ON_PAUSE);
        }
    }

    /**
     * Call this to balanace a previous call to onPause()
     * @hide
     */
    public void onResume() {
        if (mIsPaused) {
            mIsPaused = false;
            mWebViewCore.sendMessage(EventHub.ON_RESUME);
        }
    }

    /**
     * Returns true if the view is paused, meaning onPause() was called. Calling
     * onResume() sets the paused state back to false.
     * @hide
     */
    public boolean isPaused() {
        return mIsPaused;
    }

    /**
     * Clear the resource cache. Note that the cache is per-application, so
     * this will clear the cache for all WebViews used.
+18 −0
Original line number Diff line number Diff line
@@ -591,6 +591,8 @@ final class WebViewCore {
            "TOUCH_UP", // = 140;
            "TOUCH_EVENT", // = 141;
            "SET_ACTIVE", // = 142;
            "ON_PAUSE",     // = 143
            "ON_RESUME",    // = 144
        };

    class EventHub {
@@ -647,6 +649,11 @@ final class WebViewCore {
        // or not, based on whether the WebView has focus.
        static final int SET_ACTIVE = 142;

        // pause/resume activity for just this DOM (unlike pauseTimers, which
        // is global)
        static final int ON_PAUSE = 143;
        static final int ON_RESUME = 144;

        // Network-based messaging
        static final int CLEAR_SSL_PREF_TABLE = 150;

@@ -841,6 +848,14 @@ final class WebViewCore {
                            }
                            break;

                        case ON_PAUSE:
                            nativePause();
                            break;

                        case ON_RESUME:
                            nativeResume();
                            break;

                        case SET_NETWORK_STATE:
                            if (BrowserFrame.sJavaBridge == null) {
                                throw new IllegalStateException("No WebView " +
@@ -1750,4 +1765,7 @@ final class WebViewCore {
        }
        
    }

    private native void nativePause();
    private native void nativeResume();
}