Add a setting to disable Tor when running on battery

This commit is contained in:
Torsten Grote
2019-02-05 13:13:16 -02:00
parent e964dae64b
commit 75c37a258e
6 changed files with 49 additions and 10 deletions

View File

@@ -19,9 +19,7 @@ import javax.inject.Inject;
import static android.content.Intent.ACTION_BATTERY_CHANGED; import static android.content.Intent.ACTION_BATTERY_CHANGED;
import static android.content.Intent.ACTION_POWER_CONNECTED; import static android.content.Intent.ACTION_POWER_CONNECTED;
import static android.content.Intent.ACTION_POWER_DISCONNECTED; import static android.content.Intent.ACTION_POWER_DISCONNECTED;
import static android.os.BatteryManager.BATTERY_STATUS_CHARGING; import static android.os.BatteryManager.EXTRA_PLUGGED;
import static android.os.BatteryManager.BATTERY_STATUS_FULL;
import static android.os.BatteryManager.EXTRA_STATUS;
import static java.util.logging.Level.INFO; import static java.util.logging.Level.INFO;
import static java.util.logging.Logger.getLogger; import static java.util.logging.Logger.getLogger;
@@ -48,9 +46,8 @@ class AndroidBatteryManager implements BatteryManager, Service {
IntentFilter filter = new IntentFilter(ACTION_BATTERY_CHANGED); IntentFilter filter = new IntentFilter(ACTION_BATTERY_CHANGED);
Intent i = appContext.registerReceiver(null, filter); Intent i = appContext.registerReceiver(null, filter);
if (i == null) return false; if (i == null) return false;
int status = i.getIntExtra(EXTRA_STATUS, -1); int status = i.getIntExtra(EXTRA_PLUGGED, 0);
return status == BATTERY_STATUS_CHARGING || return status != 0;
status == BATTERY_STATUS_FULL;
} }
@Override @Override

View File

@@ -16,6 +16,7 @@ public interface TorConstants {
String PREF_TOR_NETWORK = "network2"; String PREF_TOR_NETWORK = "network2";
String PREF_TOR_PORT = "port"; String PREF_TOR_PORT = "port";
String PREF_TOR_MOBILE = "useMobileData"; String PREF_TOR_MOBILE = "useMobileData";
String PREF_TOR_ONLY_WHEN_CHARGING = "onlyWhenCharging";
int PREF_TOR_NETWORK_AUTOMATIC = 0; int PREF_TOR_NETWORK_AUTOMATIC = 0;
int PREF_TOR_NETWORK_WITHOUT_BRIDGES = 1; int PREF_TOR_NETWORK_WITHOUT_BRIDGES = 1;

View File

@@ -69,6 +69,7 @@ import static org.briarproject.bramble.api.plugin.TorConstants.PREF_TOR_NETWORK;
import static org.briarproject.bramble.api.plugin.TorConstants.PREF_TOR_NETWORK_AUTOMATIC; import static org.briarproject.bramble.api.plugin.TorConstants.PREF_TOR_NETWORK_AUTOMATIC;
import static org.briarproject.bramble.api.plugin.TorConstants.PREF_TOR_NETWORK_NEVER; import static org.briarproject.bramble.api.plugin.TorConstants.PREF_TOR_NETWORK_NEVER;
import static org.briarproject.bramble.api.plugin.TorConstants.PREF_TOR_NETWORK_WITH_BRIDGES; import static org.briarproject.bramble.api.plugin.TorConstants.PREF_TOR_NETWORK_WITH_BRIDGES;
import static org.briarproject.bramble.api.plugin.TorConstants.PREF_TOR_ONLY_WHEN_CHARGING;
import static org.briarproject.bramble.api.plugin.TorConstants.PREF_TOR_PORT; import static org.briarproject.bramble.api.plugin.TorConstants.PREF_TOR_PORT;
import static org.briarproject.bramble.api.plugin.TorConstants.PROP_ONION_V2; import static org.briarproject.bramble.api.plugin.TorConstants.PROP_ONION_V2;
import static org.briarproject.bramble.api.plugin.TorConstants.PROP_ONION_V3; import static org.briarproject.bramble.api.plugin.TorConstants.PROP_ONION_V3;
@@ -648,8 +649,10 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
if (s.getNamespace().equals(ID.getString())) { if (s.getNamespace().equals(ID.getString())) {
LOG.info("Tor settings updated"); LOG.info("Tor settings updated");
settings = s.getSettings(); settings = s.getSettings();
// Works around a bug introduced in Tor 0.3.4.8. Could be // Works around a bug introduced in Tor 0.3.4.8.
// replaced with callback.transportDisabled() when fixed. // https://trac.torproject.org/projects/tor/ticket/28027
// Could be replaced with callback.transportDisabled()
// when fixed.
disableNetwork(); disableNetwork();
updateConnectionStatus(networkManager.getNetworkStatus(), updateConnectionStatus(networkManager.getNetworkStatus(),
batteryManager.isCharging()); batteryManager.isCharging());
@@ -685,6 +688,8 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
int network = settings.getInt(PREF_TOR_NETWORK, int network = settings.getInt(PREF_TOR_NETWORK,
PREF_TOR_NETWORK_AUTOMATIC); PREF_TOR_NETWORK_AUTOMATIC);
boolean useMobile = settings.getBoolean(PREF_TOR_MOBILE, true); boolean useMobile = settings.getBoolean(PREF_TOR_MOBILE, true);
boolean onlyWhenCharging =
settings.getBoolean(PREF_TOR_ONLY_WHEN_CHARGING, false);
boolean bridgesWork = circumventionProvider.doBridgesWork(country); boolean bridgesWork = circumventionProvider.doBridgesWork(country);
boolean automatic = network == PREF_TOR_NETWORK_AUTOMATIC; boolean automatic = network == PREF_TOR_NETWORK_AUTOMATIC;
@@ -696,8 +701,12 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
} }
try { try {
if (!online) { if (!online || (!charging && onlyWhenCharging)) {
LOG.info("Disabling network, device is offline"); if (!online) {
LOG.info("Disabling network, device is offline");
} else {
LOG.info("Disabling network, device is on battery");
}
enableNetwork(false); enableNetwork(false);
} else if (network == PREF_TOR_NETWORK_NEVER || } else if (network == PREF_TOR_NETWORK_NEVER ||
(!useMobile && !wifi)) { (!useMobile && !wifi)) {

View File

@@ -75,6 +75,7 @@ import static org.briarproject.bramble.api.plugin.BluetoothConstants.PREF_BT_ENA
import static org.briarproject.bramble.api.plugin.TorConstants.PREF_TOR_MOBILE; import static org.briarproject.bramble.api.plugin.TorConstants.PREF_TOR_MOBILE;
import static org.briarproject.bramble.api.plugin.TorConstants.PREF_TOR_NETWORK; import static org.briarproject.bramble.api.plugin.TorConstants.PREF_TOR_NETWORK;
import static org.briarproject.bramble.api.plugin.TorConstants.PREF_TOR_NETWORK_AUTOMATIC; import static org.briarproject.bramble.api.plugin.TorConstants.PREF_TOR_NETWORK_AUTOMATIC;
import static org.briarproject.bramble.api.plugin.TorConstants.PREF_TOR_ONLY_WHEN_CHARGING;
import static org.briarproject.bramble.util.LogUtils.logDuration; import static org.briarproject.bramble.util.LogUtils.logDuration;
import static org.briarproject.bramble.util.LogUtils.logException; import static org.briarproject.bramble.util.LogUtils.logException;
import static org.briarproject.bramble.util.LogUtils.now; import static org.briarproject.bramble.util.LogUtils.now;
@@ -111,6 +112,8 @@ public class SettingsFragment extends PreferenceFragmentCompat
public static final String NOTIFY_SIGN_IN = "pref_key_notify_sign_in"; public static final String NOTIFY_SIGN_IN = "pref_key_notify_sign_in";
public static final String TOR_NETWORK = "pref_key_tor_network"; public static final String TOR_NETWORK = "pref_key_tor_network";
public static final String TOR_MOBILE = "pref_key_tor_mobile_data"; public static final String TOR_MOBILE = "pref_key_tor_mobile_data";
public static final String TOR_ONLY_WHEN_CHARGING =
"pref_key_tor_only_when_charging";
private static final Logger LOG = private static final Logger LOG =
Logger.getLogger(SettingsFragment.class.getName()); Logger.getLogger(SettingsFragment.class.getName());
@@ -120,6 +123,7 @@ public class SettingsFragment extends PreferenceFragmentCompat
private ListPreference enableBluetooth; private ListPreference enableBluetooth;
private ListPreference torNetwork; private ListPreference torNetwork;
private SwitchPreference torMobile; private SwitchPreference torMobile;
private SwitchPreference torOnlyWhenCharging;
private SwitchPreference screenLock; private SwitchPreference screenLock;
private ListPreference screenLockTimeout; private ListPreference screenLockTimeout;
private SwitchPreference notifyPrivateMessages; private SwitchPreference notifyPrivateMessages;
@@ -165,6 +169,8 @@ public class SettingsFragment extends PreferenceFragmentCompat
enableBluetooth = (ListPreference) findPreference("pref_key_bluetooth"); enableBluetooth = (ListPreference) findPreference("pref_key_bluetooth");
torNetwork = (ListPreference) findPreference(TOR_NETWORK); torNetwork = (ListPreference) findPreference(TOR_NETWORK);
torMobile = (SwitchPreference) findPreference(TOR_MOBILE); torMobile = (SwitchPreference) findPreference(TOR_MOBILE);
torOnlyWhenCharging =
(SwitchPreference) findPreference(TOR_ONLY_WHEN_CHARGING);
screenLock = (SwitchPreference) findPreference(PREF_SCREEN_LOCK); screenLock = (SwitchPreference) findPreference(PREF_SCREEN_LOCK);
screenLockTimeout = screenLockTimeout =
(ListPreference) findPreference(PREF_SCREEN_LOCK_TIMEOUT); (ListPreference) findPreference(PREF_SCREEN_LOCK_TIMEOUT);
@@ -202,6 +208,7 @@ public class SettingsFragment extends PreferenceFragmentCompat
enableBluetooth.setOnPreferenceChangeListener(this); enableBluetooth.setOnPreferenceChangeListener(this);
torNetwork.setOnPreferenceChangeListener(this); torNetwork.setOnPreferenceChangeListener(this);
torMobile.setOnPreferenceChangeListener(this); torMobile.setOnPreferenceChangeListener(this);
torOnlyWhenCharging.setOnPreferenceChangeListener(this);
screenLock.setOnPreferenceChangeListener(this); screenLock.setOnPreferenceChangeListener(this);
screenLockTimeout.setOnPreferenceChangeListener(this); screenLockTimeout.setOnPreferenceChangeListener(this);
@@ -362,6 +369,10 @@ public class SettingsFragment extends PreferenceFragmentCompat
torSettings.getBoolean(PREF_TOR_MOBILE, true); torSettings.getBoolean(PREF_TOR_MOBILE, true);
torMobile.setChecked(torMobileSetting); torMobile.setChecked(torMobileSetting);
boolean torChargingSetting =
torSettings.getBoolean(PREF_TOR_ONLY_WHEN_CHARGING, false);
torOnlyWhenCharging.setChecked(torChargingSetting);
displayScreenLockSetting(); displayScreenLockSetting();
if (SDK_INT < 26) { if (SDK_INT < 26) {
@@ -423,6 +434,7 @@ public class SettingsFragment extends PreferenceFragmentCompat
enableBluetooth.setEnabled(enabled); enableBluetooth.setEnabled(enabled);
torNetwork.setEnabled(enabled); torNetwork.setEnabled(enabled);
torMobile.setEnabled(enabled); torMobile.setEnabled(enabled);
torOnlyWhenCharging.setEnabled(enabled);
if (!enabled) screenLock.setEnabled(false); if (!enabled) screenLock.setEnabled(false);
notifyPrivateMessages.setEnabled(enabled); notifyPrivateMessages.setEnabled(enabled);
notifyGroupMessages.setEnabled(enabled); notifyGroupMessages.setEnabled(enabled);
@@ -519,6 +531,9 @@ public class SettingsFragment extends PreferenceFragmentCompat
} else if (preference == torMobile) { } else if (preference == torMobile) {
boolean torMobileSetting = (Boolean) newValue; boolean torMobileSetting = (Boolean) newValue;
storeTorMobileSetting(torMobileSetting); storeTorMobileSetting(torMobileSetting);
} else if (preference == torOnlyWhenCharging) {
boolean torChargingSetting = (Boolean) newValue;
storeTorChargingSetting(torChargingSetting);
} else if (preference == screenLock) { } else if (preference == screenLock) {
Settings s = new Settings(); Settings s = new Settings();
s.putBoolean(PREF_SCREEN_LOCK, (Boolean) newValue); s.putBoolean(PREF_SCREEN_LOCK, (Boolean) newValue);
@@ -585,6 +600,12 @@ public class SettingsFragment extends PreferenceFragmentCompat
mergeSettings(s, TOR_NAMESPACE); mergeSettings(s, TOR_NAMESPACE);
} }
private void storeTorChargingSetting(boolean torChargingSetting) {
Settings s = new Settings();
s.putBoolean(PREF_TOR_ONLY_WHEN_CHARGING, torChargingSetting);
mergeSettings(s, TOR_NAMESPACE);
}
private void storeBluetoothSettings(boolean btSetting) { private void storeBluetoothSettings(boolean btSetting) {
Settings s = new Settings(); Settings s = new Settings();
s.putBoolean(PREF_BT_ENABLE, btSetting); s.putBoolean(PREF_BT_ENABLE, btSetting);

View File

@@ -380,6 +380,8 @@
<!-- How and when Tor will connect after Automatic: E.g. Don't connect (in China) or Use Tor with bridges (in Belarus) --> <!-- How and when Tor will connect after Automatic: E.g. Don't connect (in China) or Use Tor with bridges (in Belarus) -->
<string name="tor_network_setting_summary">Automatic: %1$s (in %2$s)</string> <string name="tor_network_setting_summary">Automatic: %1$s (in %2$s)</string>
<string name="tor_mobile_data_title">Use mobile data</string> <string name="tor_mobile_data_title">Use mobile data</string>
<string name="tor_only_when_charging_title">Connect to Internet (Tor) only when charging</string>
<string name="tor_only_when_charging_summary">Disables internet connection when device runs on battery</string>
<!-- Settings Security and Panic --> <!-- Settings Security and Panic -->
<string name="security_settings_title">Security</string> <string name="security_settings_title">Security</string>

View File

@@ -57,6 +57,15 @@
android:widgetLayout="@layout/preference_switch_compat" android:widgetLayout="@layout/preference_switch_compat"
app:iconSpaceReserved="false"/> app:iconSpaceReserved="false"/>
<SwitchPreference
android:defaultValue="false"
android:key="pref_key_tor_only_when_charging"
android:persistent="false"
android:title="@string/tor_only_when_charging_title"
android:summary="@string/tor_only_when_charging_summary"
android:widgetLayout="@layout/preference_switch_compat"
app:iconSpaceReserved="false"/>
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory <PreferenceCategory