From b234b0bded73cf878fdc8521a337bef6b2f8fb62 Mon Sep 17 00:00:00 2001 From: Joe Bowser Date: Tue, 24 Jul 2012 15:54:01 -0700 Subject: [PATCH] Utility Methods based on Feedback --- .../org/apache/cordova/CordovaWebView.java | 58 +++++++++++++++++++ .../src/org/apache/cordova/DroidGap.java | 49 ++++------------ 2 files changed, 69 insertions(+), 38 deletions(-) diff --git a/framework/src/org/apache/cordova/CordovaWebView.java b/framework/src/org/apache/cordova/CordovaWebView.java index f36403ba..234ff65c 100755 --- a/framework/src/org/apache/cordova/CordovaWebView.java +++ b/framework/src/org/apache/cordova/CordovaWebView.java @@ -785,4 +785,62 @@ public class CordovaWebView extends WebView { { return this.bound; } + + public void handlePause(boolean keepRunning) + { + // Send pause event to JavaScript + this.loadUrl("javascript:try{cordova.fireDocumentEvent('pause');}catch(e){console.log('exception firing pause event from native');};"); + + // Forward to plugins + if (this.pluginManager != null) { + this.pluginManager.onPause(keepRunning); + } + + // If app doesn't want to run in background + if (keepRunning) { + // Pause JavaScript timers (including setInterval) + this.pauseTimers(); + } + + } + + public void handleResume(boolean keepRunning, boolean activityResultKeepRunning) + { + + // Send resume event to JavaScript + this.loadUrl("javascript:try{cordova.fireDocumentEvent('resume');}catch(e){console.log('exception firing resume event from native');};"); + + // Forward to plugins + if (this.pluginManager != null) { + this.pluginManager.onResume(keepRunning); + } + + // If app doesn't want to run in background + if (!keepRunning || activityResultKeepRunning) { + // Resume JavaScript timers (including setInterval) + this.resumeTimers(); + } + } + + public void handleDestroy() + { + // Send destroy event to JavaScript + this.loadUrl("javascript:try{cordova.require('cordova/channel').onDestroy.fire();}catch(e){console.log('exception firing destroy event from native');};"); + + // Load blank page so that JavaScript onunload is called + this.loadUrl("about:blank"); + + // Forward to plugins + if (this.pluginManager != null) { + this.pluginManager.onDestroy(); + } + } + + public void onNewIntent(Intent intent) + { + //Forward to plugins + if (this.pluginManager != null) { + this.pluginManager.onNewIntent(intent); + } + } } diff --git a/framework/src/org/apache/cordova/DroidGap.java b/framework/src/org/apache/cordova/DroidGap.java index fe47df46..f48e2114 100755 --- a/framework/src/org/apache/cordova/DroidGap.java +++ b/framework/src/org/apache/cordova/DroidGap.java @@ -38,6 +38,7 @@ import android.graphics.Color; import android.media.AudioManager; import android.os.Bundle; import android.os.Handler; +import android.util.Log; import android.view.Display; import android.view.KeyEvent; import android.view.Menu; @@ -600,6 +601,8 @@ public class DroidGap extends Activity implements CordovaInterface { protected void onPause() { super.onPause(); + LOG.d(TAG, "Paused the application!"); + // Don't process pause if shutting down, since onDestroy() will be called if (this.activityState == ACTIVITY_EXITING) { return; @@ -608,19 +611,9 @@ public class DroidGap extends Activity implements CordovaInterface { if (this.appView == null) { return; } - - // Send pause event to JavaScript - this.appView.loadUrl("javascript:try{cordova.fireDocumentEvent('pause');}catch(e){console.log('exception firing pause event from native');};"); - - // Forward to plugins - if (this.appView.pluginManager != null) { - this.appView.pluginManager.onPause(this.keepRunning); - } - - // If app doesn't want to run in background - if (!this.keepRunning) { - // Pause JavaScript timers (including setInterval) - this.appView.pauseTimers(); + else + { + this.appView.handlePause(this.keepRunning); } // hide the splash screen to avoid leaking a window @@ -633,11 +626,9 @@ public class DroidGap extends Activity implements CordovaInterface { **/ protected void onNewIntent(Intent intent) { super.onNewIntent(intent); - //Forward to plugins - if ((this.appView != null) && (this.appView.pluginManager != null)) { - this.appView.pluginManager.onNewIntent(intent); - } + if (this.appView != null) + this.appView.onNewIntent(intent); } @Override @@ -647,6 +638,7 @@ public class DroidGap extends Activity implements CordovaInterface { protected void onResume() { super.onResume(); + LOG.d(TAG, "Resuming the App"); if (this.activityState == ACTIVITY_STARTING) { this.activityState = ACTIVITY_RUNNING; return; @@ -656,13 +648,7 @@ public class DroidGap extends Activity implements CordovaInterface { return; } - // Send resume event to JavaScript - this.appView.loadUrl("javascript:try{cordova.fireDocumentEvent('resume');}catch(e){console.log('exception firing resume event from native');};"); - - // Forward to plugins - if (this.appView.pluginManager != null) { - this.appView.pluginManager.onResume(this.keepRunning || this.activityResultKeepRunning); - } + this.appView.handleResume(this.keepRunning, this.activityResultKeepRunning); // If app doesn't want to run in background if (!this.keepRunning || this.activityResultKeepRunning) { @@ -672,9 +658,6 @@ public class DroidGap extends Activity implements CordovaInterface { this.keepRunning = this.activityResultKeepRunning; this.activityResultKeepRunning = false; } - - // Resume JavaScript timers (including setInterval) - this.appView.resumeTimers(); } } @@ -690,17 +673,7 @@ public class DroidGap extends Activity implements CordovaInterface { this.removeSplashScreen(); if (this.appView != null) { - - // Send destroy event to JavaScript - this.appView.loadUrl("javascript:try{cordova.require('cordova/channel').onDestroy.fire();}catch(e){console.log('exception firing destroy event from native');};"); - - // Load blank page so that JavaScript onunload is called - this.appView.loadUrl("about:blank"); - - // Forward to plugins - if (this.appView.pluginManager != null) { - this.appView.pluginManager.onDestroy(); - } + appView.handleDestroy(); } else { this.endActivity();