Add DisableNetwork support

DisableNetwork tells tor to drop the current circuits and stop
building new ones. A BroadcastReciever is triggered when there
are changes in the network connection which toggles DisableNetwork.
This commit is contained in:
Sathyanarayanan Gunasekaran 2012-09-30 20:41:54 +05:30 committed by n8fr8
parent efc6d95314
commit dbdadf2115
2 changed files with 26 additions and 3 deletions

View File

@ -5,8 +5,9 @@
android:versionCode="45" android:versionCode="45"
android:installLocation="auto" android:installLocation="auto"
> >
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="15"/> <uses-sdk android:minSdkVersion="5" android:targetSdkVersion="15"/>
<application android:name=".OrbotApp" android:icon="@drawable/ic_launcher" <application android:name=".OrbotApp" android:icon="@drawable/ic_launcher"

View File

@ -38,10 +38,13 @@ import android.app.Notification;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.app.Service; import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor; import android.content.SharedPreferences.Editor;
import android.net.ConnectivityManager;
import android.os.Bundle; import android.os.Bundle;
import android.os.IBinder; import android.os.IBinder;
import android.os.Looper; import android.os.Looper;
@ -275,6 +278,8 @@ public class TorService extends Service implements TorServiceConstants, TorConst
{ {
initTor(); initTor();
isRunning = true; isRunning = true;
IntentFilter mNetworkStateFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(mNetworkStateReceiver , mNetworkStateFilter);
} }
catch (Exception e) catch (Exception e)
{ {
@ -1496,6 +1501,23 @@ public class TorService extends Service implements TorServiceConstants, TorConst
} }
/*
* Another way to do this would be to use the Observer pattern by defining the
* BroadcastReciever in the Android manifest.
*/
private final BroadcastReceiver mNetworkStateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
try {
mBinder.updateConfiguration("DisableNetwork", noConnectivity ? "1" : "0", false);
mBinder.saveConfiguration();
} catch (RemoteException e) {
logException ("error applying prefs",e);
}
}
};
private boolean updateTorConfiguration () throws RemoteException private boolean updateTorConfiguration () throws RemoteException
{ {