From 6c07dca8e013b913567b38c29fc1c02f04a4768e Mon Sep 17 00:00:00 2001 From: Balint Kovacs Date: Wed, 4 May 2011 14:07:25 +0200 Subject: [PATCH] Implemented network state change listener that stops the service on connectivity loss and starts it once connectivity is regained Signed-off-by: Balint Kovacs --- AndroidManifest.xml | 7 ++++- .../blint/ssldroid/NetworkChangeReceiver.java | 31 +++++++++++++++++++ .../blint/ssldroid/SSLDroidTunnelDetails.java | 19 +++++++----- .../blint/ssldroid/TcpProxyServerThread.java | 1 - 4 files changed, 49 insertions(+), 9 deletions(-) create mode 100644 src/hu/blint/ssldroid/NetworkChangeReceiver.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 8dcbdb3..54dabc4 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -18,11 +18,16 @@ - + + + + + + diff --git a/src/hu/blint/ssldroid/NetworkChangeReceiver.java b/src/hu/blint/ssldroid/NetworkChangeReceiver.java new file mode 100644 index 0000000..05c0542 --- /dev/null +++ b/src/hu/blint/ssldroid/NetworkChangeReceiver.java @@ -0,0 +1,31 @@ +package hu.blint.ssldroid; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.net.ConnectivityManager; +import android.net.NetworkInfo; +import android.util.Log; + +public class NetworkChangeReceiver extends BroadcastReceiver{ + + @Override + public void onReceive(Context context, Intent intent) { + ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE ); + NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo(); + if ( activeNetInfo == null ) { + Intent i = new Intent(); + i.setAction("hu.blint.ssldroid.SSLDroid"); + context.stopService(i); + return; + } + Log.d("SSLDroid", activeNetInfo.toString()); + if (activeNetInfo.isAvailable()) { + Intent i = new Intent(); + i.setAction("hu.blint.ssldroid.SSLDroid"); + context.stopService(i); + context.startService(i); + } + } +} + diff --git a/src/hu/blint/ssldroid/SSLDroidTunnelDetails.java b/src/hu/blint/ssldroid/SSLDroidTunnelDetails.java index 052da93..6bbdebb 100644 --- a/src/hu/blint/ssldroid/SSLDroidTunnelDetails.java +++ b/src/hu/blint/ssldroid/SSLDroidTunnelDetails.java @@ -18,10 +18,12 @@ import java.util.ListIterator; import android.app.Activity; import android.app.AlertDialog; +import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.DialogInterface.OnClickListener; import android.database.Cursor; +import android.net.ConnectivityManager; import android.os.Bundle; import android.os.Environment; import android.util.Log; @@ -66,7 +68,6 @@ public class SSLDroidTunnelDetails extends Activity { } }); - rowId = null; Bundle extras = getIntent().getExtras(); rowId = (bundle == null) ? null : (Long) bundle @@ -119,12 +120,16 @@ public class SSLDroidTunnelDetails extends Activity { return; } else { - //remote host should exist - try { - InetAddress.getByName(remotehost.getText().toString()); - } catch (UnknownHostException e){ - Toast.makeText(getBaseContext(), "Remote host not found, please recheck...", Toast.LENGTH_LONG).show(); - } + //if we have interwebs access, the remote host should exist + ConnectivityManager conMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); + + if ( conMgr.getActiveNetworkInfo() != null || conMgr.getActiveNetworkInfo().isAvailable()) { + try { + InetAddress.getByName(remotehost.getText().toString()); + } catch (UnknownHostException e){ + Toast.makeText(getBaseContext(), "Remote host not found, please recheck...", Toast.LENGTH_LONG).show(); + } + } } //remote port validation if (remoteport.getText().length() == 0){ diff --git a/src/hu/blint/ssldroid/TcpProxyServerThread.java b/src/hu/blint/ssldroid/TcpProxyServerThread.java index 2a50041..3ef44a1 100644 --- a/src/hu/blint/ssldroid/TcpProxyServerThread.java +++ b/src/hu/blint/ssldroid/TcpProxyServerThread.java @@ -155,7 +155,6 @@ public class TcpProxyServerThread extends Thread { public void run() { while (true) { try { - //TODO: close client sockets if no data network is available Thread fromBrowserToServer = null; Thread fromServerToBrowser = null;