Verified Commit 7b116f15 authored by akwizgran's avatar akwizgran

Attach updated settings to SettingsUpdatedEvent.

parent de631419
Pipeline #2242 passed with stage
in 11 minutes and 16 seconds
......@@ -2,6 +2,7 @@ package org.briarproject.bramble.api.settings.event;
import org.briarproject.bramble.api.event.Event;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.settings.Settings;
import javax.annotation.concurrent.Immutable;
......@@ -13,12 +14,18 @@ import javax.annotation.concurrent.Immutable;
public class SettingsUpdatedEvent extends Event {
private final String namespace;
private final Settings settings;
public SettingsUpdatedEvent(String namespace) {
public SettingsUpdatedEvent(String namespace, Settings settings) {
this.namespace = namespace;
this.settings = settings;
}
public String getNamespace() {
return namespace;
}
public Settings getSettings() {
return settings;
}
}
......@@ -655,7 +655,7 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
merged.putAll(s);
if (!merged.equals(old)) {
db.mergeSettings(txn, s, namespace);
transaction.attach(new SettingsUpdatedEvent(namespace));
transaction.attach(new SettingsUpdatedEvent(namespace, merged));
}
}
......
......@@ -21,6 +21,7 @@ import org.briarproject.bramble.api.plugin.event.BluetoothEnabledEvent;
import org.briarproject.bramble.api.plugin.event.DisableBluetoothEvent;
import org.briarproject.bramble.api.plugin.event.EnableBluetoothEvent;
import org.briarproject.bramble.api.properties.TransportProperties;
import org.briarproject.bramble.api.settings.Settings;
import org.briarproject.bramble.api.settings.event.SettingsUpdatedEvent;
import org.briarproject.bramble.util.StringUtils;
......@@ -146,16 +147,15 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
}
updateProperties();
running = true;
loadSettings();
loadSettings(callback.getSettings());
if (shouldAllowContactConnections()) {
if (isAdapterEnabled()) bind();
else enableAdapter();
}
}
private void loadSettings() {
contactConnections =
callback.getSettings().getBoolean(PREF_BT_ENABLE, false);
private void loadSettings(Settings settings) {
contactConnections = settings.getBoolean(PREF_BT_ENABLE, false);
}
private boolean shouldAllowContactConnections() {
......@@ -387,7 +387,7 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
} else if (e instanceof SettingsUpdatedEvent) {
SettingsUpdatedEvent s = (SettingsUpdatedEvent) e;
if (s.getNamespace().equals(ID.getString()))
ioExecutor.execute(this::onSettingsUpdated);
ioExecutor.execute(() -> onSettingsUpdated(s.getSettings()));
} else if (e instanceof KeyAgreementListeningEvent) {
ioExecutor.execute(connectionLimiter::keyAgreementStarted);
} else if (e instanceof KeyAgreementStoppedListeningEvent) {
......@@ -395,9 +395,9 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
}
}
private void onSettingsUpdated() {
private void onSettingsUpdated(Settings settings) {
boolean wasAllowed = shouldAllowContactConnections();
loadSettings();
loadSettings(settings);
boolean isAllowed = shouldAllowContactConnections();
if (wasAllowed && !isAllowed) {
LOG.info("Contact connections disabled");
......
......@@ -108,6 +108,7 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
private volatile ServerSocket socket = null;
private volatile Socket controlSocket = null;
private volatile TorControlConnection controlConnection = null;
private volatile Settings settings = null;
protected volatile boolean running = false;
......@@ -172,6 +173,8 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
throw new PluginException();
}
}
// Load the settings
settings = callback.getSettings();
// Install or update the assets if necessary
if (!assetsAreUpToDate()) installAssets();
if (cookieFile.exists() && !cookieFile.delete())
......@@ -357,7 +360,7 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
private void bind() {
ioExecutor.execute(() -> {
// If there's already a port number stored in config, reuse it
String portString = callback.getSettings().get(PREF_TOR_PORT);
String portString = settings.get(PREF_TOR_PORT);
int port;
if (StringUtils.isNullOrEmpty(portString)) port = 0;
else port = Integer.parseInt(portString);
......@@ -402,7 +405,7 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
private void publishHiddenService(String port) {
if (!running) return;
LOG.info("Creating hidden service");
String privKey = callback.getSettings().get(HS_PRIVKEY);
String privKey = settings.get(HS_PRIVKEY);
Map<Integer, String> portLines =
Collections.singletonMap(80, "127.0.0.1:" + port);
Map<String, String> response;
......@@ -623,6 +626,7 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
SettingsUpdatedEvent s = (SettingsUpdatedEvent) e;
if (s.getNamespace().equals(ID.getString())) {
LOG.info("Tor settings updated");
settings = s.getSettings();
updateConnectionStatus(networkManager.getNetworkStatus());
}
} else if (e instanceof NetworkStatusEvent) {
......@@ -638,10 +642,9 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
String country = locationUtils.getCurrentCountry();
boolean blocked =
circumventionProvider.isTorProbablyBlocked(country);
Settings s = callback.getSettings();
int network =
s.getInt(PREF_TOR_NETWORK, PREF_TOR_NETWORK_AUTOMATIC);
boolean useMobile = s.getBoolean(PREF_TOR_MOBILE, true);
int network = settings.getInt(PREF_TOR_NETWORK,
PREF_TOR_NETWORK_AUTOMATIC);
boolean useMobile = settings.getBoolean(PREF_TOR_MOBILE, true);
boolean bridgesWork = circumventionProvider.doBridgesWork(country);
boolean automatic = network == PREF_TOR_NETWORK_AUTOMATIC;
......
......@@ -16,7 +16,6 @@ import android.support.v4.app.TaskStackBuilder;
import org.briarproject.bramble.api.Multiset;
import org.briarproject.bramble.api.contact.ContactId;
import org.briarproject.bramble.api.db.DatabaseExecutor;
import org.briarproject.bramble.api.db.DbException;
import org.briarproject.bramble.api.event.Event;
import org.briarproject.bramble.api.event.EventListener;
......@@ -53,11 +52,9 @@ import org.briarproject.briar.api.sharing.event.InvitationResponseReceivedEvent;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Logger;
import javax.annotation.concurrent.ThreadSafe;
import javax.inject.Inject;
......@@ -78,8 +75,6 @@ import static android.support.v4.app.NotificationCompat.PRIORITY_LOW;
import static android.support.v4.app.NotificationCompat.PRIORITY_MIN;
import static android.support.v4.app.NotificationCompat.VISIBILITY_SECRET;
import static android.support.v4.content.ContextCompat.getColor;
import static java.util.logging.Level.WARNING;
import static org.briarproject.bramble.util.LogUtils.logException;
import static org.briarproject.briar.android.activity.BriarActivity.GROUP_ID;
import static org.briarproject.briar.android.contact.ConversationActivity.CONTACT_ID;
import static org.briarproject.briar.android.navdrawer.NavDrawerActivity.INTENT_BLOGS;
......@@ -96,10 +91,6 @@ class AndroidNotificationManagerImpl implements AndroidNotificationManager,
private static final long SOUND_DELAY = TimeUnit.SECONDS.toMillis(2);
private static final Logger LOG =
Logger.getLogger(AndroidNotificationManagerImpl.class.getName());
private final Executor dbExecutor;
private final SettingsManager settingsManager;
private final AndroidExecutor androidExecutor;
private final Clock clock;
......@@ -125,10 +116,8 @@ class AndroidNotificationManagerImpl implements AndroidNotificationManager,
private volatile Settings settings = new Settings();
@Inject
AndroidNotificationManagerImpl(@DatabaseExecutor Executor dbExecutor,
SettingsManager settingsManager, AndroidExecutor androidExecutor,
Application app, Clock clock) {
this.dbExecutor = dbExecutor;
AndroidNotificationManagerImpl(SettingsManager settingsManager,
AndroidExecutor androidExecutor, Application app, Clock clock) {
this.settingsManager = settingsManager;
this.androidExecutor = androidExecutor;
this.clock = clock;
......@@ -232,7 +221,8 @@ class AndroidNotificationManagerImpl implements AndroidNotificationManager,
public void eventOccurred(Event e) {
if (e instanceof SettingsUpdatedEvent) {
SettingsUpdatedEvent s = (SettingsUpdatedEvent) e;
if (s.getNamespace().equals(SETTINGS_NAMESPACE)) loadSettings();
if (s.getNamespace().equals(SETTINGS_NAMESPACE))
settings = s.getSettings();
} else if (e instanceof PrivateMessageReceivedEvent) {
PrivateMessageReceivedEvent p = (PrivateMessageReceivedEvent) e;
showContactNotification(p.getContactId());
......@@ -263,15 +253,6 @@ class AndroidNotificationManagerImpl implements AndroidNotificationManager,
}
}
private void loadSettings() {
dbExecutor.execute(() -> {
try {
settings = settingsManager.getSettings(SETTINGS_NAMESPACE);
} catch (DbException e) {
logException(LOG, WARNING, e);
}
});
}
@UiThread
@Override
......
......@@ -135,7 +135,8 @@ public class SettingsFragment extends PreferenceFragmentCompat
private Preference notifySound;
// Fields that are accessed from background threads must be volatile
volatile Settings settings;
private volatile Settings settings, btSettings, torSettings;
@Inject
volatile SettingsManager settingsManager;
@Inject
......@@ -241,7 +242,6 @@ public class SettingsFragment extends PreferenceFragmentCompat
if (testing == null) throw new AssertionError();
testing.setVisible(false);
}
}
@Override
......@@ -347,30 +347,31 @@ public class SettingsFragment extends PreferenceFragmentCompat
try {
long start = now();
settings = settingsManager.getSettings(SETTINGS_NAMESPACE);
Settings btSettings = settingsManager.getSettings(BT_NAMESPACE);
Settings torSettings =
settingsManager.getSettings(TOR_NAMESPACE);
btSettings = settingsManager.getSettings(BT_NAMESPACE);
torSettings = settingsManager.getSettings(TOR_NAMESPACE);
logDuration(LOG, "Loading settings", start);
boolean btSetting =
btSettings.getBoolean(PREF_BT_ENABLE, false);
int torNetworkSetting = torSettings.getInt(PREF_TOR_NETWORK,
PREF_TOR_NETWORK_AUTOMATIC);
boolean torMobileSetting =
torSettings.getBoolean(PREF_TOR_MOBILE, true);
displaySettings(btSetting, torNetworkSetting, torMobileSetting);
displaySettings();
} catch (DbException e) {
logException(LOG, WARNING, e);
}
});
}
private void displaySettings(boolean btSetting, int torNetworkSetting,
boolean torMobileSetting) {
private void displaySettings() {
listener.runOnUiThreadUnlessDestroyed(() -> {
enableBluetooth.setValue(Boolean.toString(btSetting));
boolean btEnabledSetting =
btSettings.getBoolean(PREF_BT_ENABLE, false);
enableBluetooth.setValue(Boolean.toString(btEnabledSetting));
int torNetworkSetting = torSettings.getInt(PREF_TOR_NETWORK,
PREF_TOR_NETWORK_AUTOMATIC);
torNetwork.setValue(Integer.toString(torNetworkSetting));
setTorNetworkSummary(torNetworkSetting);
boolean torMobileSetting =
torSettings.getBoolean(PREF_TOR_MOBILE, true);
torMobile.setChecked(torMobileSetting);
displayScreenLockSetting();
if (SDK_INT < 26) {
......@@ -660,12 +661,20 @@ public class SettingsFragment extends PreferenceFragmentCompat
@Override
public void eventOccurred(Event e) {
if (e instanceof SettingsUpdatedEvent) {
String namespace = ((SettingsUpdatedEvent) e).getNamespace();
if (namespace.equals(BT_NAMESPACE)
|| namespace.equals(TOR_NAMESPACE)
|| namespace.equals(SETTINGS_NAMESPACE)) {
SettingsUpdatedEvent s = (SettingsUpdatedEvent) e;
String namespace = s.getNamespace();
if (namespace.equals(SETTINGS_NAMESPACE)) {
LOG.info("Settings updated");
loadSettings();
settings = s.getSettings();
displaySettings();
} else if (namespace.equals(BT_NAMESPACE)) {
LOG.info("Bluetooth settings updated");
btSettings = s.getSettings();
displaySettings();
} else if (namespace.equals(TOR_NAMESPACE)) {
LOG.info("Tor settings updated");
torSettings = s.getSettings();
displaySettings();
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment