1e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood/* 2e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood * Copyright (C) 2011 The Android Open Source Project 3e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood * 4e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood * Licensed under the Apache License, Version 2.0 (the "License"); 5e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood * you may not use this file except in compliance with the License. 6e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood * You may obtain a copy of the License at 7e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood * 8e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood * http://www.apache.org/licenses/LICENSE-2.0 9e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood * 10e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood * Unless required by applicable law or agreed to in writing, software 11e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood * distributed under the License is distributed on an "AS IS" BASIS, 12e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood * See the License for the specific language governing permissions and 14e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood * limitations under the License. 15e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood */ 16e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwoodpackage com.android.browser; 17e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood 18e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwoodimport android.os.Looper; 19e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwoodimport android.util.Log; 20e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwoodimport android.webkit.WebView; 21e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood 22e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood/** 23e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood * Centralised point for controlling WebView timers pausing and resuming. 24e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood * 25e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood * All methods on this class should only be called from the UI thread. 26e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood */ 27e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwoodpublic class WebViewTimersControl { 28e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood 29e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood private static final boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED; 30e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood private static final String LOGTAG = "WebViewTimersControl"; 31e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood 32e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood private static WebViewTimersControl sInstance; 33e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood 34e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood private boolean mBrowserActive; 35e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood private boolean mPrerenderActive; 36e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood 37e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood /** 38e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood * Get the static instance. Must be called from UI thread. 39e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood */ 40e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood public static WebViewTimersControl getInstance() { 41e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood if (Looper.myLooper() != Looper.getMainLooper()) { 42e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood throw new IllegalStateException("WebViewTimersControl.get() called on wrong thread"); 43e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood } 44e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood if (sInstance == null) { 45e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood sInstance = new WebViewTimersControl(); 46e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood } 47e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood return sInstance; 48e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood } 49e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood 50e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood private WebViewTimersControl() { 51e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood } 52e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood 53e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood private void resumeTimers(WebView wv) { 54e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood if (LOGD_ENABLED) Log.d(LOGTAG, "Resuming webview timers, view=" + wv); 55e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood if (wv != null) { 56e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood wv.resumeTimers(); 57e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood } 58e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood } 59e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood 60e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood private void maybePauseTimers(WebView wv) { 61e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood if (!mBrowserActive && !mPrerenderActive && wv != null) { 62e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood if (LOGD_ENABLED) Log.d(LOGTAG, "Pausing webview timers, view=" + wv); 63e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood wv.pauseTimers(); 64e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood } 65e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood } 66e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood 67e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood public void onBrowserActivityResume(WebView wv) { 68e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood if (LOGD_ENABLED) Log.d(LOGTAG, "onBrowserActivityResume"); 69e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood mBrowserActive = true; 70e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood resumeTimers(wv); 71e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood } 72e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood 73e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood public void onBrowserActivityPause(WebView wv) { 74e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood if (LOGD_ENABLED) Log.d(LOGTAG, "onBrowserActivityPause"); 75e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood mBrowserActive = false; 76e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood maybePauseTimers(wv); 77e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood } 78e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood 79e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood public void onPrerenderStart(WebView wv) { 80e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood if (LOGD_ENABLED) Log.d(LOGTAG, "onPrerenderStart"); 81e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood mPrerenderActive = true; 82e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood resumeTimers(wv); 83e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood } 84e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood 85e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood public void onPrerenderDone(WebView wv) { 86e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood if (LOGD_ENABLED) Log.d(LOGTAG, "onPrerenderDone"); 87e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood mPrerenderActive = false; 88e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood maybePauseTimers(wv); 89e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood } 90e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood 91e1dbb956d762c3f07033f247c05270a9882a79a7Mathew Inwood} 92