From 7bfa63b55644fbf2da1fe6a24b3be0267529202c Mon Sep 17 00:00:00 2001 From: Mike Reed Date: Thu, 28 May 2009 11:08:32 -0400 Subject: call webview's onPause/onResume in the right places: - pause when a view is in a non-current tab - pause the current view when the activity itself is paused --- src/com/android/browser/BrowserActivity.java | 27 +++++----- src/com/android/browser/TabControl.java | 76 ++++++++++++++++++---------- 2 files changed, 64 insertions(+), 39 deletions(-) (limited to 'src') diff --git a/src/com/android/browser/BrowserActivity.java b/src/com/android/browser/BrowserActivity.java index 513ecc6..7d6be9e 100644 --- a/src/com/android/browser/BrowserActivity.java +++ b/src/com/android/browser/BrowserActivity.java @@ -1091,8 +1091,9 @@ public class BrowserActivity extends Activity return; } + mTabControl.resumeCurrentTab(); mActivityInPause = false; - resumeWebView(); + resumeWebViewTimers(); if (mWakeLock.isHeld()) { mHandler.removeMessages(RELEASE_WAKELOCK); @@ -1150,8 +1151,9 @@ public class BrowserActivity extends Activity return; } + mTabControl.pauseCurrentTab(); mActivityInPause = true; - if (mTabControl.getCurrentIndex() >= 0 && !pauseWebView()) { + if (mTabControl.getCurrentIndex() >= 0 && !pauseWebViewTimers()) { mWakeLock.acquire(); mHandler.sendMessageDelayed(mHandler .obtainMessage(RELEASE_WAKELOCK), WAKELOCK_TIMEOUT); @@ -1252,7 +1254,7 @@ public class BrowserActivity extends Activity mTabControl.freeMemory(); } - private boolean resumeWebView() { + private boolean resumeWebViewTimers() { if ((!mActivityInPause && !mPageStarted) || (mActivityInPause && mPageStarted)) { CookieSyncManager.getInstance().startSync(); @@ -1266,7 +1268,7 @@ public class BrowserActivity extends Activity } } - private boolean pauseWebView() { + private boolean pauseWebViewTimers() { if (mActivityInPause && !mPageStarted) { CookieSyncManager.getInstance().stopSync(); WebView w = mTabControl.getCurrentWebView(); @@ -2510,9 +2512,9 @@ public class BrowserActivity extends Activity finish(); return; } - // call pauseWebView() now, we won't be able to call it in - // onPause() as the WebView won't be valid. - pauseWebView(); + // call pauseWebViewTimers() now, we won't be able to call + // it in onPause() as the WebView won't be valid. + pauseWebViewTimers(); removeTabFromContentView(current); mTabControl.removeTab(current); } @@ -2808,8 +2810,9 @@ public class BrowserActivity extends Activity if (!mPageStarted) { mPageStarted = true; - // if onResume() has been called, resumeWebView() does nothing. - resumeWebView(); + // if onResume() has been called, resumeWebViewTimers() does + // nothing. + resumeWebViewTimers(); } // reset sync timer to avoid sync starts during loading a page @@ -2930,9 +2933,9 @@ public class BrowserActivity extends Activity if (mPageStarted) { mPageStarted = false; - // pauseWebView() will do nothing and return false if onPause() - // is not called yet. - if (pauseWebView()) { + // pauseWebViewTimers() will do nothing and return false if + // onPause() is not called yet. + if (pauseWebViewTimers()) { if (mWakeLock.isHeld()) { mHandler.removeMessages(RELEASE_WAKELOCK); mWakeLock.release(); diff --git a/src/com/android/browser/TabControl.java b/src/com/android/browser/TabControl.java index 9de0198..d880d40 100644 --- a/src/com/android/browser/TabControl.java +++ b/src/com/android/browser/TabControl.java @@ -867,6 +867,48 @@ class TabControl { return setCurrentTab(newTab, false); } + /*package*/ void pauseCurrentTab() { + Tab t = getCurrentTab(); + if (t != null) { + t.mMainView.onPause(); + if (t.mSubView != null) { + t.mSubView.onPause(); + } + } + } + + /*package*/ void resumeCurrentTab() { + Tab t = getCurrentTab(); + if (t != null) { + t.mMainView.onResume(); + if (t.mSubView != null) { + t.mSubView.onResume(); + } + } + } + + private void putViewInForeground(WebView v, WebViewClient vc, + WebChromeClient cc) { + v.setWebViewClient(vc); + v.setWebChromeClient(cc); + v.setOnCreateContextMenuListener(mActivity); + v.setDownloadListener(mActivity); + v.onResume(); + } + + private void putViewInBackground(WebView v) { + // Set an empty callback so that default actions are not triggered. + v.setWebViewClient(mEmptyClient); + v.setWebChromeClient(mBackgroundChromeClient); + v.setOnCreateContextMenuListener(null); + // Leave the DownloadManager attached so that downloads can start in + // a non-active window. This can happen when going to a site that does + // a redirect after a period of time. The user could have switched to + // another tab while waiting for the download to start. + v.setDownloadListener(mActivity); + v.onPause(); + } + /** * If force is true, this method skips the check for newTab == current. */ @@ -892,7 +934,6 @@ class TabControl { mTabQueue.add(newTab); WebView mainView; - WebView subView; // Display the new current tab mCurrentTab = mTabs.indexOf(newTab); @@ -902,17 +943,12 @@ class TabControl { // Same work as in createNewTab() except don't do new Tab() newTab.mMainView = mainView = createNewWebView(); } - mainView.setWebViewClient(mActivity.getWebViewClient()); - mainView.setWebChromeClient(mActivity.getWebChromeClient()); - mainView.setOnCreateContextMenuListener(mActivity); - mainView.setDownloadListener(mActivity); + putViewInForeground(mainView, mActivity.getWebViewClient(), + mActivity.getWebChromeClient()); // Add the subwindow if it exists if (newTab.mSubViewContainer != null) { - subView = newTab.mSubView; - subView.setWebViewClient(newTab.mSubViewClient); - subView.setWebChromeClient(newTab.mSubViewChromeClient); - subView.setOnCreateContextMenuListener(mActivity); - subView.setDownloadListener(mActivity); + putViewInForeground(newTab.mSubView, newTab.mSubViewClient, + newTab.mSubViewChromeClient); } if (needRestore) { // Have to finish setCurrentTab work before calling restoreState @@ -927,23 +963,9 @@ class TabControl { * Put the tab in the background using all the empty/background clients. */ private void putTabInBackground(Tab t) { - WebView mainView = t.mMainView; - // Set an empty callback so that default actions are not triggered. - mainView.setWebViewClient(mEmptyClient); - mainView.setWebChromeClient(mBackgroundChromeClient); - mainView.setOnCreateContextMenuListener(null); - // Leave the DownloadManager attached so that downloads can start in - // a non-active window. This can happen when going to a site that does - // a redirect after a period of time. The user could have switched to - // another tab while waiting for the download to start. - mainView.setDownloadListener(mActivity); - WebView subView = t.mSubView; - if (subView != null) { - // Set an empty callback so that default actions are not triggered. - subView.setWebViewClient(mEmptyClient); - subView.setWebChromeClient(mBackgroundChromeClient); - subView.setOnCreateContextMenuListener(null); - subView.setDownloadListener(mActivity); + putViewInBackground(t.mMainView); + if (t.mSubView != null) { + putViewInBackground(t.mSubView); } } -- cgit v1.1