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