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