From 10e1808c5671ae15b11fa5bb9eb48f3228b2d8f8 Mon Sep 17 00:00:00 2001 From: Bryce Curtis Date: Tue, 31 May 2011 15:11:02 -0500 Subject: [PATCH 1/2] Clean up CallbackServer when about:blank page has loaded. This fixes errors when shutting down. --- framework/src/com/phonegap/DroidGap.java | 30 ++++++++++++++---------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/framework/src/com/phonegap/DroidGap.java b/framework/src/com/phonegap/DroidGap.java index 91eeb173..fae84535 100755 --- a/framework/src/com/phonegap/DroidGap.java +++ b/framework/src/com/phonegap/DroidGap.java @@ -658,21 +658,18 @@ public class DroidGap extends PhonegapActivity { if (this.appView != null) { - // Make sure pause event is sent if onPause hasn't been called before onDestroy - this.appView.loadUrl("javascript:try{PhoneGap.onPause.fire();}catch(e){};"); + // Make sure pause event is sent if onPause hasn't been called before onDestroy + this.appView.loadUrl("javascript:try{PhoneGap.onPause.fire();}catch(e){};"); - // Send destroy event to JavaScript - this.appView.loadUrl("javascript:try{PhoneGap.onDestroy.fire();}catch(e){};"); + // Send destroy event to JavaScript + this.appView.loadUrl("javascript:try{PhoneGap.onDestroy.fire();}catch(e){};"); - // Load blank page so that JavaScript onunload is called - this.appView.loadUrl("about:blank"); + // Load blank page so that JavaScript onunload is called + this.appView.loadUrl("about:blank"); - // Forward to plugins - this.pluginManager.onDestroy(); + // Forward to plugins + this.pluginManager.onDestroy(); - if (this.callbackServer != null) { - this.callbackServer.destroy(); - } } } @@ -1065,7 +1062,9 @@ public class DroidGap extends PhonegapActivity { // Try firing the onNativeReady event in JS. If it fails because the JS is // not loaded yet then just set a flag so that the onNativeReady can be fired // from the JS side when the JS gets to that code. - appView.loadUrl("javascript:try{ PhoneGap.onNativeReady.fire();}catch(e){_nativeReady = true;}"); + if (!url.equals("about:blank")) { + appView.loadUrl("javascript:try{ PhoneGap.onNativeReady.fire();}catch(e){_nativeReady = true;}"); + } // Make app view visible appView.setVisibility(View.VISIBLE); @@ -1081,6 +1080,13 @@ public class DroidGap extends PhonegapActivity { this.ctx.clearHistory = false; this.ctx.appView.clearHistory(); } + + // Shutdown if blank loaded + if (url.equals("about:blank")) { + if (this.ctx.callbackServer != null) { + this.ctx.callbackServer.destroy(); + } + } } /** From 39ec9c095d12ee1e2afbfcae294ef760770242ca Mon Sep 17 00:00:00 2001 From: Bryce Curtis Date: Tue, 31 May 2011 15:13:54 -0500 Subject: [PATCH 2/2] Need to unregister for network intent receiver on shutdown to prevent leaks. --- .../src/com/phonegap/NetworkManager.java | 40 ++++++++++++++----- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/framework/src/com/phonegap/NetworkManager.java b/framework/src/com/phonegap/NetworkManager.java index 779e1c71..0a2fdb1b 100755 --- a/framework/src/com/phonegap/NetworkManager.java +++ b/framework/src/com/phonegap/NetworkManager.java @@ -57,13 +57,15 @@ public class NetworkManager extends Plugin { private static final String LOG_TAG = "NetworkManager"; private String connectionCallbackId; - ConnectivityManager sockMan; - TelephonyManager telephonyManager; + ConnectivityManager sockMan; + TelephonyManager telephonyManager; + BroadcastReceiver receiver; /** * Constructor. */ public NetworkManager() { + this.receiver = null; } /** @@ -76,18 +78,23 @@ public class NetworkManager extends Plugin { super.setContext(ctx); this.sockMan = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE); this.telephonyManager = ((TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE)); - + this.connectionCallbackId = null; + // We need to listen to connectivity events to update navigator.connection IntentFilter intentFilter = new IntentFilter() ; intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); - ctx.registerReceiver(new BroadcastReceiver() { - @Override - public void onReceive(Context context, Intent intent) { - updateConnectionInfo((NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO)); - } - }, intentFilter); - } + if (this.receiver == null) { + this.receiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + updateConnectionInfo((NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO)); + } + }; + ctx.registerReceiver(this.receiver, intentFilter); + } + } + /** * Executes the request and returns PluginResult. * @@ -135,6 +142,19 @@ public class NetworkManager extends Plugin { // All methods take a while, so always use async return false; } + + /** + * Stop network receiver. + */ + public void onDestroy() { + if (this.receiver != null) { + try { + this.ctx.unregisterReceiver(this.receiver); + } catch (Exception e) { + System.out.println("Error unregistering network receiver: " + e.getMessage()); + } + } + } //-------------------------------------------------------------------------- // LOCAL METHODS