Remove unnecessary inner class, state checks.

This commit is contained in:
akwizgran
2020-01-16 13:08:16 +00:00
parent 0aada89625
commit 0a5f93edf9
2 changed files with 69 additions and 104 deletions

View File

@@ -74,7 +74,6 @@ class AndroidTorPlugin extends TorPlugin {
@Override @Override
protected void enableNetwork(boolean enable) throws IOException { protected void enableNetwork(boolean enable) throws IOException {
if (!state.isTorRunning()) return;
if (enable) wakeLock.acquire(); if (enable) wakeLock.acquire();
super.enableNetwork(enable); super.enableNetwork(enable);
if (!enable) wakeLock.release(); if (!enable) wakeLock.release();

View File

@@ -57,7 +57,6 @@ import java.util.zip.ZipInputStream;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import javax.annotation.concurrent.GuardedBy; import javax.annotation.concurrent.GuardedBy;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.ThreadSafe; import javax.annotation.concurrent.ThreadSafe;
import javax.net.SocketFactory; import javax.net.SocketFactory;
@@ -480,7 +479,6 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
} }
protected void enableNetwork(boolean enable) throws IOException { protected void enableNetwork(boolean enable) throws IOException {
if (!state.isTorRunning()) return;
state.enableNetwork(enable); state.enableNetwork(enable);
callback.pluginStateChanged(getState()); callback.pluginStateChanged(getState());
controlConnection.setConf("DisableNetwork", enable ? "0" : "1"); controlConnection.setConf("DisableNetwork", enable ? "0" : "1");
@@ -713,6 +711,7 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
@Override @Override
public void message(String severity, String msg) { public void message(String severity, String msg) {
if (LOG.isLoggable(INFO)) LOG.info(severity + " " + msg);
if (LOG.isLoggable(INFO)) LOG.info(severity + " " + msg); if (LOG.isLoggable(INFO)) LOG.info(severity + " " + msg);
if (severity.equals("NOTICE") && msg.startsWith("Bootstrapped 100%")) { if (severity.equals("NOTICE") && msg.startsWith("Bootstrapped 100%")) {
state.setBootstrapped(); state.setBootstrapped();
@@ -754,7 +753,7 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
private void disableNetwork() { private void disableNetwork() {
connectionStatusExecutor.execute(() -> { connectionStatusExecutor.execute(() -> {
try { try {
enableNetwork(false); if (state.isTorRunning()) enableNetwork(false);
} catch (IOException ex) { } catch (IOException ex) {
logException(LOG, WARNING, ex); logException(LOG, WARNING, ex);
} }
@@ -765,22 +764,13 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
boolean charging) { boolean charging) {
connectionStatusExecutor.execute(() -> { connectionStatusExecutor.execute(() -> {
if (!state.isTorRunning()) return; if (!state.isTorRunning()) return;
NetworkConfig config = getNetworkConfig(status, charging);
state.setDisabledBySettings(config.disabledBySettings,
config.reasonDisabled);
callback.pluginStateChanged(getState());
applyNetworkConfig(config);
});
}
private NetworkConfig getNetworkConfig(NetworkStatus status,
boolean charging) {
boolean online = status.isConnected(); boolean online = status.isConnected();
boolean wifi = status.isWifi(); boolean wifi = status.isWifi();
String country = locationUtils.getCurrentCountry(); String country = locationUtils.getCurrentCountry();
boolean blocked = circumventionProvider.isTorProbablyBlocked(country); boolean blocked =
int network = circumventionProvider.isTorProbablyBlocked(country);
settings.getInt(PREF_TOR_NETWORK, PREF_TOR_NETWORK_AUTOMATIC); int network = settings.getInt(PREF_TOR_NETWORK,
PREF_TOR_NETWORK_AUTOMATIC);
boolean useMobile = settings.getBoolean(PREF_TOR_MOBILE, true); boolean useMobile = settings.getBoolean(PREF_TOR_MOBILE, true);
boolean onlyWhenCharging = boolean onlyWhenCharging =
settings.getBoolean(PREF_TOR_ONLY_WHEN_CHARGING, false); settings.getBoolean(PREF_TOR_ONLY_WHEN_CHARGING, false);
@@ -840,16 +830,13 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
LOG.info("Disabling connection padding"); LOG.info("Disabling connection padding");
} }
return new NetworkConfig(enableNetwork, enableBridges, useMeek, state.setDisabledBySettings(disabledBySettings, reasonDisabled);
enableConnectionPadding, disabledBySettings, reasonDisabled); callback.pluginStateChanged(getState());
}
private void applyNetworkConfig(NetworkConfig config) {
connectionStatusExecutor.execute(() -> {
try { try {
enableBridges(config.enableBridges, config.useMeek); enableBridges(enableBridges, useMeek);
enableNetwork(config.enableNetwork); enableNetwork(enableNetwork);
enableConnectionPadding(config.enableConnectionPadding); enableConnectionPadding(enableConnectionPadding);
} catch (IOException e) { } catch (IOException e) {
logException(LOG, WARNING, e); logException(LOG, WARNING, e);
} }
@@ -857,30 +844,9 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
} }
private void enableConnectionPadding(boolean enable) throws IOException { private void enableConnectionPadding(boolean enable) throws IOException {
if (!state.isTorRunning()) return;
controlConnection.setConf("ConnectionPadding", enable ? "1" : "0"); controlConnection.setConf("ConnectionPadding", enable ? "1" : "0");
} }
@Immutable
@NotNullByDefault
private static class NetworkConfig {
private final boolean enableNetwork, enableBridges, useMeek;
private final boolean enableConnectionPadding, disabledBySettings;
private final int reasonDisabled;
private NetworkConfig(boolean enableNetwork, boolean enableBridges,
boolean useMeek, boolean enableConnectionPadding,
boolean disabledBySettings, int reasonDisabled) {
this.enableNetwork = enableNetwork;
this.enableBridges = enableBridges;
this.useMeek = useMeek;
this.enableConnectionPadding = enableConnectionPadding;
this.disabledBySettings = disabledBySettings;
this.reasonDisabled = reasonDisabled;
}
}
@ThreadSafe @ThreadSafe
@NotNullByDefault @NotNullByDefault
protected static class PluginState { protected static class PluginState {