Code cleanup.

This commit is contained in:
akwizgran
2016-01-20 10:30:36 +00:00
parent 2216e2b0f2
commit f5f572139a
6 changed files with 31 additions and 59 deletions

View File

@@ -23,14 +23,13 @@ import org.briarproject.android.util.HorizontalBorder;
import org.briarproject.android.util.LayoutUtils; import org.briarproject.android.util.LayoutUtils;
import org.briarproject.android.util.ListLoadingProgressBar; import org.briarproject.android.util.ListLoadingProgressBar;
import org.briarproject.api.Settings; import org.briarproject.api.Settings;
import org.briarproject.api.settings.SettingsManager;
import org.briarproject.api.TransportId;
import org.briarproject.api.db.DatabaseComponent; import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.db.DbException; import org.briarproject.api.db.DbException;
import org.briarproject.api.event.Event; import org.briarproject.api.event.Event;
import org.briarproject.api.event.EventBus; import org.briarproject.api.event.EventBus;
import org.briarproject.api.event.EventListener; import org.briarproject.api.event.EventListener;
import org.briarproject.api.event.SettingsUpdatedEvent; import org.briarproject.api.event.SettingsUpdatedEvent;
import org.briarproject.api.settings.SettingsManager;
import org.briarproject.util.StringUtils; import org.briarproject.util.StringUtils;
import java.util.logging.Logger; import java.util.logging.Logger;
@@ -263,8 +262,8 @@ OnClickListener {
runOnDbThread(new Runnable() { runOnDbThread(new Runnable() {
public void run() { public void run() {
try { try {
settings = settingsManager.getSettings("settings-activity");
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
settings = settingsManager.getSettings("settings-activity");
Settings btSettings = settingsManager.getSettings("bt"); Settings btSettings = settingsManager.getSettings("bt");
Settings torSettings = settingsManager.getSettings("tor"); Settings torSettings = settingsManager.getSettings("tor");
long duration = System.currentTimeMillis() - now; long duration = System.currentTimeMillis() - now;
@@ -335,7 +334,7 @@ OnClickListener {
if (adapter != null) { if (adapter != null) {
AndroidUtils.setBluetooth(adapter, bluetoothSetting); AndroidUtils.setBluetooth(adapter, bluetoothSetting);
} }
storeBluetoothSetting(); storeBluetoothSettings();
displaySettings(); displaySettings();
} else if (view == torOverWifi || view == torOverWifiHint) { } else if (view == torOverWifi || view == torOverWifiHint) {
torSetting = !torSetting; torSetting = !torSetting;
@@ -385,7 +384,7 @@ OnClickListener {
settingsManager.mergeSettings(s, "tor"); settingsManager.mergeSettings(s, "tor");
long duration = System.currentTimeMillis() - now; long duration = System.currentTimeMillis() - now;
if (LOG.isLoggable(INFO)) if (LOG.isLoggable(INFO))
LOG.info("Merging config took " + duration + " ms"); LOG.info("Merging settings took " + duration + " ms");
} catch (DbException e) { } catch (DbException e) {
if (LOG.isLoggable(WARNING)) if (LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e); LOG.log(WARNING, e.toString(), e);
@@ -394,7 +393,7 @@ OnClickListener {
}); });
} }
private void storeBluetoothSetting() { private void storeBluetoothSettings() {
runOnDbThread(new Runnable() { runOnDbThread(new Runnable() {
public void run() { public void run() {
try { try {
@@ -404,7 +403,7 @@ OnClickListener {
settingsManager.mergeSettings(s, "bt"); settingsManager.mergeSettings(s, "bt");
long duration = System.currentTimeMillis() - now; long duration = System.currentTimeMillis() - now;
if (LOG.isLoggable(INFO)) if (LOG.isLoggable(INFO))
LOG.info("Merging config took " + duration + " ms"); LOG.info("Merging settings took " + duration + " ms");
} catch (DbException e) { } catch (DbException e) {
if (LOG.isLoggable(WARNING)) if (LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e); LOG.log(WARNING, e.toString(), e);

View File

@@ -190,7 +190,7 @@ public interface DatabaseComponent {
Map<ContactId, TransportProperties> getRemoteProperties(TransportId t) Map<ContactId, TransportProperties> getRemoteProperties(TransportId t)
throws DbException; throws DbException;
/** Returns all settings for a given namespace. */ /** Returns all settings in the given namespace. */
Settings getSettings(String namespace) throws DbException; Settings getSettings(String namespace) throws DbException;
/** Returns all contacts who subscribe to the given group. */ /** Returns all contacts who subscribe to the given group. */
@@ -223,7 +223,10 @@ public interface DatabaseComponent {
void mergeLocalProperties(TransportId t, TransportProperties p) void mergeLocalProperties(TransportId t, TransportProperties p)
throws DbException; throws DbException;
/** Merges the given settings with the existing settings. */ /**
* Merges the given settings with the existing settings in the given
* namespace.
*/
void mergeSettings(Settings s, String namespace) throws DbException; void mergeSettings(Settings s, String namespace) throws DbException;
/** Processes an ack from the given contact. */ /** Processes an ack from the given contact. */

View File

@@ -1,20 +1,16 @@
package org.briarproject.api.settings; package org.briarproject.api.settings;
import org.briarproject.api.db.DbException;
import org.briarproject.api.Settings; import org.briarproject.api.Settings;
import org.briarproject.api.db.DbException;
public interface SettingsManager { public interface SettingsManager {
/** /** Returns all settings in the given namespace. */
* Returns the settings object identified by the provided namespace
* string
*/
Settings getSettings(String namespace) throws DbException; Settings getSettings(String namespace) throws DbException;
/** /**
* Merges (read syncs) the provided settings identified by the provided namespace * Merges the given settings with the existing settings in the given
* string * namespace.
*/ */
void mergeSettings(Settings s, String namespace) throws DbException; void mergeSettings(Settings s, String namespace) throws DbException;
} }

View File

@@ -411,7 +411,7 @@ interface Database<T> {
int maxLength) throws DbException; int maxLength) throws DbException;
/** /**
* Returns all settings that belong to a namespace. * Returns all settings in the given namespace.
* <p> * <p>
* Locking: read. * Locking: read.
*/ */
@@ -526,11 +526,12 @@ interface Database<T> {
throws DbException; throws DbException;
/** /**
* Merges the given settings with the existing settings. * Merges the given settings with the existing settings in the given
* namespace.
* <p> * <p>
* Locking: write. * Locking: write.
*/ */
void mergeSettings(T txn, Settings s, String Namespace) throws DbException; void mergeSettings(T txn, Settings s, String namespace) throws DbException;
/** /**
* Marks a message as needing to be acknowledged to the given contact. * Marks a message as needing to be acknowledged to the given contact.

View File

@@ -1,5 +1,6 @@
package org.briarproject.plugins; package org.briarproject.plugins;
import org.briarproject.api.Settings;
import org.briarproject.api.TransportId; import org.briarproject.api.TransportId;
import org.briarproject.api.TransportProperties; import org.briarproject.api.TransportProperties;
import org.briarproject.api.contact.ContactId; import org.briarproject.api.contact.ContactId;
@@ -27,8 +28,6 @@ import org.briarproject.api.plugins.simplex.SimplexPluginFactory;
import org.briarproject.api.property.TransportPropertyManager; import org.briarproject.api.property.TransportPropertyManager;
import org.briarproject.api.system.Clock; import org.briarproject.api.system.Clock;
import org.briarproject.api.ui.UiCallback; import org.briarproject.api.ui.UiCallback;
import org.briarproject.api.Settings;
import org.briarproject.api.settings.SettingsManager;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
@@ -168,9 +167,7 @@ class PluginManagerImpl implements PluginManager {
public void run() { public void run() {
try { try {
TransportId id = factory.getId(); TransportId id = factory.getId();
String namespace = id.toString(); SimplexCallback callback = new SimplexCallback(id);
SimplexCallback callback = new SimplexCallback(id, namespace);
SimplexPlugin plugin = factory.createPlugin(callback); SimplexPlugin plugin = factory.createPlugin(callback);
if (plugin == null) { if (plugin == null) {
if (LOG.isLoggable(INFO)) { if (LOG.isLoggable(INFO)) {
@@ -233,8 +230,7 @@ class PluginManagerImpl implements PluginManager {
public void run() { public void run() {
try { try {
TransportId id = factory.getId(); TransportId id = factory.getId();
String namespace = id.toString(); DuplexCallback callback = new DuplexCallback(id);
DuplexCallback callback = new DuplexCallback(id, namespace);
DuplexPlugin plugin = factory.createPlugin(callback); DuplexPlugin plugin = factory.createPlugin(callback);
if (plugin == null) { if (plugin == null) {
if (LOG.isLoggable(INFO)) { if (LOG.isLoggable(INFO)) {
@@ -313,22 +309,18 @@ class PluginManagerImpl implements PluginManager {
private abstract class PluginCallbackImpl implements PluginCallback { private abstract class PluginCallbackImpl implements PluginCallback {
protected final TransportId id; protected final TransportId id;
protected final String namespace;
protected PluginCallbackImpl(TransportId id, String namespace) { protected PluginCallbackImpl(TransportId id) {
this.id = id; this.id = id;
this.namespace = namespace;
} }
public Settings getSettings() { public Settings getSettings() {
try { try {
return db.getSettings(namespace); return db.getSettings(id.getString());
} catch (DbException e) { } catch (DbException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e); if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
return new Settings(); return new Settings();
} }
} }
public TransportProperties getLocalProperties() { public TransportProperties getLocalProperties() {
@@ -352,13 +344,11 @@ class PluginManagerImpl implements PluginManager {
} }
public void mergeSettings(Settings s) { public void mergeSettings(Settings s) {
try { try {
db.mergeSettings(s, namespace); db.mergeSettings(s, id.getString());
} catch (DbException e) { } catch (DbException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e); if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
} }
} }
public void mergeLocalProperties(TransportProperties p) { public void mergeLocalProperties(TransportProperties p) {
@@ -383,7 +373,6 @@ class PluginManagerImpl implements PluginManager {
public void transportEnabled() { public void transportEnabled() {
eventBus.broadcast(new TransportEnabledEvent(id)); eventBus.broadcast(new TransportEnabledEvent(id));
Plugin p = plugins.get(id); Plugin p = plugins.get(id);
if (p != null) poller.pollNow(p); if (p != null) poller.pollNow(p);
} }
@@ -396,8 +385,8 @@ class PluginManagerImpl implements PluginManager {
private class SimplexCallback extends PluginCallbackImpl private class SimplexCallback extends PluginCallbackImpl
implements SimplexPluginCallback { implements SimplexPluginCallback {
private SimplexCallback(TransportId id, String namespace) { private SimplexCallback(TransportId id) {
super(id, namespace); super(id);
} }
public void readerCreated(TransportConnectionReader r) { public void readerCreated(TransportConnectionReader r) {
@@ -412,8 +401,8 @@ class PluginManagerImpl implements PluginManager {
private class DuplexCallback extends PluginCallbackImpl private class DuplexCallback extends PluginCallbackImpl
implements DuplexPluginCallback { implements DuplexPluginCallback {
private DuplexCallback(TransportId id, String namespace) { private DuplexCallback(TransportId id) {
super(id, namespace); super(id);
} }
public void incomingConnectionCreated(DuplexTransportConnection d) { public void incomingConnectionCreated(DuplexTransportConnection d) {

View File

@@ -2,43 +2,27 @@ package org.briarproject.settings;
import com.google.inject.Inject; import com.google.inject.Inject;
import org.briarproject.api.settings.SettingsManager;
import org.briarproject.api.Settings; import org.briarproject.api.Settings;
import org.briarproject.api.db.DatabaseComponent; import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.db.DbException; import org.briarproject.api.db.DbException;
import java.util.logging.Logger; import org.briarproject.api.settings.SettingsManager;
import java.util.Collection;
class SettingsManagerImpl implements SettingsManager { class SettingsManagerImpl implements SettingsManager {
private final DatabaseComponent db; private final DatabaseComponent db;
private static final Logger LOG =
Logger.getLogger("SettingsManagerImpl");
@Inject @Inject
SettingsManagerImpl(DatabaseComponent db) { SettingsManagerImpl(DatabaseComponent db) {
this.db = db; this.db = db;
} }
/**
* Returns the settings object identified by the provided namespace
* string
*/
@Override @Override
public Settings getSettings(String namespace) throws DbException { public Settings getSettings(String namespace) throws DbException {
return db.getSettings(namespace); return db.getSettings(namespace);
} }
/**
* Merges (read syncs) the provided settings identified by the provided namespace
* string
*/
@Override @Override
public void mergeSettings(Settings s, String namespace) throws DbException { public void mergeSettings(Settings s, String namespace) throws DbException {
db.mergeSettings(s, namespace); db.mergeSettings(s, namespace);
} }
} }