UI code cleanup.

This commit is contained in:
akwizgran
2016-05-11 13:37:32 +01:00
parent 7a3bcc58ae
commit 6873dbc493
70 changed files with 524 additions and 531 deletions

View File

@@ -1,9 +1,9 @@
package org.briarproject.android.controller;
import org.briarproject.android.controller.handler.ResultHandler;
public interface BriarController extends ActivityLifecycleController {
void runOnDbThread(final Runnable task);
void startAndBindService();

View File

@@ -26,14 +26,15 @@ public class BriarControllerImpl implements BriarController {
protected BriarServiceConnection serviceConnection;
@Inject
protected DatabaseConfig databaseConfig;
@Inject
protected Activity activity;
// Fields that are accessed from background threads must be volatile
@Inject
@DatabaseExecutor
protected volatile Executor dbExecutor;
@Inject
protected volatile LifecycleManager lifecycleManager;
@Inject
protected Activity activity;
private boolean bound = false;
@@ -64,6 +65,7 @@ public class BriarControllerImpl implements BriarController {
unbindService();
}
@Override
public void startAndBindService() {
activity.startService(new Intent(activity, BriarService.class));
bound = activity.bindService(new Intent(activity, BriarService.class),
@@ -83,7 +85,8 @@ public class BriarControllerImpl implements BriarController {
try {
// Wait for the service to finish starting up
IBinder binder = serviceConnection.waitForBinder();
BriarService service = ((BriarService.BriarBinder) binder).getService();
BriarService service =
((BriarService.BriarBinder) binder).getService();
service.waitForStartup();
// Shut down the service and wait for it to shut down
LOG.info("Shutting down service");
@@ -101,8 +104,10 @@ public class BriarControllerImpl implements BriarController {
if (bound) activity.unbindService(serviceConnection);
}
@Override
public void runOnDbThread(final Runnable task) {
dbExecutor.execute(new Runnable() {
@Override
public void run() {
try {
lifecycleManager.waitForDatabase();
@@ -114,5 +119,4 @@ public class BriarControllerImpl implements BriarController {
}
});
}
}

View File

@@ -1,9 +1,12 @@
package org.briarproject.android.controller;
import android.content.Context;
public interface ConfigController {
String getEncryptedDatabaseKey();
void clearPrefs();
void deleteAccount(Context ctx);
boolean initialized();
boolean accountExists();
}

View File

@@ -1,14 +1,16 @@
package org.briarproject.android.controller;
import android.content.Context;
import android.content.SharedPreferences;
import org.briarproject.android.util.AndroidUtils;
import org.briarproject.api.db.DatabaseConfig;
import javax.inject.Inject;
public class ConfigControllerImpl implements ConfigController {
private final static String PREF_DB_KEY = "key";
private static final String PREF_DB_KEY = "key";
@Inject
protected SharedPreferences briarPrefs;
@@ -20,22 +22,22 @@ public class ConfigControllerImpl implements ConfigController {
}
@Override
public String getEncryptedDatabaseKey() {
return briarPrefs.getString(PREF_DB_KEY, null);
}
public void clearPrefs() {
@Override
public void deleteAccount(Context ctx) {
SharedPreferences.Editor editor = briarPrefs.edit();
editor.clear();
editor.apply();
AndroidUtils.deleteAppData(ctx);
}
@Override
public boolean initialized() {
public boolean accountExists() {
String hex = getEncryptedDatabaseKey();
if (hex != null && databaseConfig.databaseExists()) {
return true;
}
return false;
return hex != null && databaseConfig.databaseExists();
}
}

View File

@@ -5,6 +5,7 @@ import org.briarproject.api.TransportId;
import org.briarproject.api.identity.LocalAuthor;
public interface NavDrawerController extends BriarController {
void setTransportListener(TransportStateListener transportListener);
boolean isTransportRunning(TransportId transportId);

View File

@@ -16,8 +16,6 @@ import org.briarproject.api.identity.LocalAuthor;
import org.briarproject.api.plugins.Plugin;
import org.briarproject.api.plugins.PluginManager;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import javax.inject.Inject;
@@ -34,15 +32,15 @@ public class NavDrawerControllerImpl extends BriarControllerImpl
@Inject
protected ReferenceManager referenceManager;
@Inject
protected volatile IdentityManager identityManager;
@Inject
protected PluginManager pluginManager;
@Inject
protected EventBus eventBus;
@Inject
protected Activity activity;
private List<Plugin> transports = new ArrayList<Plugin>();
// Fields that are accessed from background threads must be volatile
@Inject
protected volatile IdentityManager identityManager;
private TransportStateListener transportStateListener;
@@ -112,6 +110,7 @@ public class NavDrawerControllerImpl extends BriarControllerImpl
public void storeLocalAuthor(final LocalAuthor author,
final UiResultHandler<Void> resultHandler) {
runOnDbThread(new Runnable() {
@Override
public void run() {
try {
long now = System.currentTimeMillis();
@@ -130,7 +129,6 @@ public class NavDrawerControllerImpl extends BriarControllerImpl
@Override
public LocalAuthor removeAuthorHandle(long handle) {
return referenceManager.removeReference(handle,
LocalAuthor.class);
return referenceManager.removeReference(handle, LocalAuthor.class);
}
}

View File

@@ -3,6 +3,7 @@ package org.briarproject.android.controller;
import org.briarproject.android.controller.handler.ResultHandler;
public interface PasswordController extends ConfigController {
void validatePassword(String password,
ResultHandler<Boolean> resultHandler);
}

View File

@@ -33,6 +33,7 @@ public class PasswordControllerImpl extends ConfigControllerImpl
final ResultHandler<Boolean> resultHandler) {
final byte[] encrypted = getEncryptedKey();
cryptoExecutor.execute(new Runnable() {
@Override
public void run() {
byte[] key = crypto.decryptWithPassword(encrypted, password);
if (key == null) {
@@ -48,7 +49,7 @@ public class PasswordControllerImpl extends ConfigControllerImpl
private byte[] getEncryptedKey() {
String hex = getEncryptedDatabaseKey();
if (hex == null)
throw new IllegalStateException("Encrypted database key is null.");
throw new IllegalStateException("Encrypted database key is null");
return StringUtils.fromHexString(hex);
}
}

View File

@@ -3,6 +3,7 @@ package org.briarproject.android.controller;
import org.briarproject.android.controller.handler.ResultHandler;
public interface SetupController {
float estimatePasswordStrength(String password);
void createIdentity(String nickname, String password,

View File

@@ -34,6 +34,10 @@ public class SetupControllerImpl implements SetupController {
protected Executor cryptoExecutor;
@Inject
protected PasswordStrengthEstimator strengthEstimator;
@Inject
protected Activity activity;
@Inject
protected SharedPreferences briarPrefs;
// Fields that are accessed from background threads must be volatile
@Inject
@@ -44,10 +48,6 @@ public class SetupControllerImpl implements SetupController {
protected volatile AuthorFactory authorFactory;
@Inject
protected volatile ReferenceManager referenceManager;
@Inject
protected Activity activity;
@Inject
protected SharedPreferences briarPrefs;
@Inject
public SetupControllerImpl() {
@@ -85,6 +85,7 @@ public class SetupControllerImpl implements SetupController {
public void createIdentity(final String nickname, final String password,
final ResultHandler<Long> resultHandler) {
cryptoExecutor.execute(new Runnable() {
@Override
public void run() {
SecretKey key = crypto.generateSecretKey();
databaseConfig.setEncryptionKey(key);
@@ -98,10 +99,9 @@ public class SetupControllerImpl implements SetupController {
});
}
private void storeEncryptedDatabaseKey(final String hex) {
private void storeEncryptedDatabaseKey(String hex) {
SharedPreferences.Editor editor = briarPrefs.edit();
editor.putString(PREF_DB_KEY, hex);
editor.apply();
}
}

View File

@@ -3,5 +3,6 @@ package org.briarproject.android.controller;
import org.briarproject.api.TransportId;
public interface TransportStateListener {
void stateUpdate(TransportId id, boolean enabled);
}

View File

@@ -1,6 +1,8 @@
package org.briarproject.android.controller.handler;
public interface ResultExceptionHandler<R, E extends Exception> {
void onResult(R result);
void onException(E exception);
}

View File

@@ -1,5 +1,6 @@
package org.briarproject.android.controller.handler;
public interface ResultHandler<R> {
void onResult(R result);
}

View File

@@ -11,16 +11,20 @@ public abstract class UiResultExceptionHandler<R, E extends Exception>
this.activity = activity;
}
@Override
public void onResult(final R result) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
onResultUi(result);
}
});
}
@Override
public void onException(final E exception) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
onExceptionUi(exception);
}

View File

@@ -10,8 +10,10 @@ public abstract class UiResultHandler<R> implements ResultHandler<R> {
this.activity = activity;
}
@Override
public void onResult(final R result) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
onResultUi(result);
}