mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 18:59:06 +01:00
merge and update
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package org.briarproject.android.controller;
|
||||
|
||||
|
||||
import org.briarproject.android.controller.handler.ResultHandler;
|
||||
|
||||
public interface BriarController extends ActivityLifecycleController {
|
||||
void runOnDbThread(final Runnable task);
|
||||
|
||||
@@ -10,5 +12,5 @@ public interface BriarController extends ActivityLifecycleController {
|
||||
|
||||
boolean encryptionKey();
|
||||
|
||||
void signOut(ResultHandler<Void, RuntimeException> eventHandler);
|
||||
void signOut(ResultHandler<Void> eventHandler);
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import android.support.annotation.CallSuper;
|
||||
|
||||
import org.briarproject.android.BriarService;
|
||||
import org.briarproject.android.BriarService.BriarServiceConnection;
|
||||
import org.briarproject.android.controller.handler.ResultHandler;
|
||||
import org.briarproject.api.db.DatabaseConfig;
|
||||
import org.briarproject.api.db.DatabaseExecutor;
|
||||
import org.briarproject.api.lifecycle.LifecycleManager;
|
||||
@@ -75,7 +76,7 @@ public class BriarControllerImp implements BriarController {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void signOut(final ResultHandler<Void, RuntimeException> eventHandler) {
|
||||
public void signOut(final ResultHandler<Void> eventHandler) {
|
||||
new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
@@ -90,14 +91,8 @@ public class BriarControllerImp implements BriarController {
|
||||
service.waitForShutdown();
|
||||
} catch (InterruptedException e) {
|
||||
LOG.warning("Interrupted while waiting for service");
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
activity.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
eventHandler.onResult(null);
|
||||
}
|
||||
});
|
||||
eventHandler.onResult(null);
|
||||
}
|
||||
}.start();
|
||||
}
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
package org.briarproject.android.controller;
|
||||
|
||||
public class EncryptedKeyNullException extends NullPointerException {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Encrypted key can't be null";
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.briarproject.android.controller;
|
||||
|
||||
import org.briarproject.android.controller.handler.ResultExceptionHandler;
|
||||
import org.briarproject.api.TransportId;
|
||||
import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.identity.LocalAuthor;
|
||||
@@ -10,7 +11,7 @@ public interface NavDrawerController extends BriarController {
|
||||
boolean transportRunning(TransportId transportId);
|
||||
|
||||
void storeLocalAuthor(LocalAuthor author,
|
||||
ResultHandler<Void, DbException> resultHandler);
|
||||
ResultExceptionHandler<Void, DbException> resultHandler);
|
||||
|
||||
LocalAuthor removeAuthorHandle(long handle);
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package org.briarproject.android.controller;
|
||||
import android.app.Activity;
|
||||
|
||||
import org.briarproject.android.api.ReferenceManager;
|
||||
import org.briarproject.android.controller.handler.ResultExceptionHandler;
|
||||
import org.briarproject.api.TransportId;
|
||||
import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.event.Event;
|
||||
@@ -84,7 +85,8 @@ public class NavDrawerControllerImp extends BriarControllerImp
|
||||
}
|
||||
}
|
||||
|
||||
private void transportStateUpdate(final TransportId id, final boolean enabled) {
|
||||
private void transportStateUpdate(final TransportId id,
|
||||
final boolean enabled) {
|
||||
activity.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
@@ -108,7 +110,8 @@ public class NavDrawerControllerImp extends BriarControllerImp
|
||||
|
||||
@Override
|
||||
public void storeLocalAuthor(final LocalAuthor author,
|
||||
final ResultHandler<Void, DbException> resultHandler) {
|
||||
final ResultExceptionHandler<Void, DbException> resultHandler) {
|
||||
|
||||
runOnDbThread(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
@@ -117,22 +120,11 @@ public class NavDrawerControllerImp extends BriarControllerImp
|
||||
long duration = System.currentTimeMillis() - now;
|
||||
if (LOG.isLoggable(INFO))
|
||||
LOG.info("Storing author took " + duration + " ms");
|
||||
activity.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
resultHandler.onResult(null);
|
||||
}
|
||||
});
|
||||
resultHandler.onResult(null);
|
||||
} catch (final DbException e) {
|
||||
if (LOG.isLoggable(WARNING))
|
||||
LOG.log(WARNING, e.toString(), e);
|
||||
|
||||
activity.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
resultHandler.onException(e);
|
||||
}
|
||||
});
|
||||
resultHandler.onException(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package org.briarproject.android.controller;
|
||||
|
||||
import org.briarproject.android.controller.handler.ResultHandler;
|
||||
|
||||
public interface PasswordController extends ConfigController {
|
||||
void validatePassword(String password,
|
||||
ResultHandler<Boolean, EncryptedKeyNullException> resultHandler);
|
||||
ResultHandler<Boolean> resultHandler);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package org.briarproject.android.controller;
|
||||
|
||||
import android.app.Activity;
|
||||
|
||||
import org.briarproject.android.controller.handler.ResultHandler;
|
||||
import org.briarproject.api.crypto.CryptoComponent;
|
||||
import org.briarproject.api.crypto.CryptoExecutor;
|
||||
import org.briarproject.api.crypto.SecretKey;
|
||||
@@ -29,35 +30,21 @@ public class PasswordControllerImp extends ConfigControllerImp
|
||||
|
||||
@Override
|
||||
public void validatePassword(final String password,
|
||||
final ResultHandler<Boolean, EncryptedKeyNullException> resultHandler) {
|
||||
final ResultHandler<Boolean> resultHandler) {
|
||||
final byte[] encrypted = getEncryptedKey();
|
||||
if (encrypted == null) {
|
||||
resultHandler.onException(new EncryptedKeyNullException());
|
||||
}
|
||||
cryptoExecutor.execute(new Runnable() {
|
||||
public void run() {
|
||||
byte[] key = crypto.decryptWithPassword(encrypted, password);
|
||||
if (key == null) {
|
||||
onPasswordValidated(false, resultHandler);
|
||||
resultHandler.onResult(false);
|
||||
} else {
|
||||
databaseConfig.setEncryptionKey(new SecretKey(key));
|
||||
onPasswordValidated(true, resultHandler);
|
||||
resultHandler.onResult(true);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void onPasswordValidated(final boolean validated,
|
||||
final ResultHandler<Boolean, EncryptedKeyNullException> resultHandler) {
|
||||
activity.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
resultHandler.onResult(validated);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
private byte[] getEncryptedKey() {
|
||||
String hex = getEncryptedDatabaseKey();
|
||||
return hex == null ? null : StringUtils.fromHexString(hex);
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
package org.briarproject.android.controller;
|
||||
|
||||
public interface ResultHandler<R, E> {
|
||||
void onResult(R result);
|
||||
void onException(E exception);
|
||||
}
|
||||
@@ -1,8 +1,10 @@
|
||||
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,
|
||||
ResultHandler<Long, RuntimeException> resultHandler);
|
||||
ResultHandler<Long> resultHandler);
|
||||
}
|
||||
|
||||
@@ -3,8 +3,8 @@ package org.briarproject.android.controller;
|
||||
import android.app.Activity;
|
||||
import android.content.SharedPreferences;
|
||||
|
||||
import org.briarproject.android.BaseActivity;
|
||||
import org.briarproject.android.api.ReferenceManager;
|
||||
import org.briarproject.android.controller.handler.ResultHandler;
|
||||
import org.briarproject.api.crypto.CryptoComponent;
|
||||
import org.briarproject.api.crypto.CryptoExecutor;
|
||||
import org.briarproject.api.crypto.KeyPair;
|
||||
@@ -83,7 +83,7 @@ public class SetupControllerImp implements SetupController {
|
||||
|
||||
@Override
|
||||
public void createIdentity(final String nickname, final String password,
|
||||
final ResultHandler<Long, RuntimeException> resultHandler) {
|
||||
final ResultHandler<Long> resultHandler) {
|
||||
cryptoExecutor.execute(new Runnable() {
|
||||
public void run() {
|
||||
SecretKey key = crypto.generateSecretKey();
|
||||
@@ -93,17 +93,6 @@ public class SetupControllerImp implements SetupController {
|
||||
final LocalAuthor localAuthor = createLocalAuthor(nickname);
|
||||
long handle = referenceManager.putReference(localAuthor,
|
||||
LocalAuthor.class);
|
||||
onIdentityCreated(handle, resultHandler);
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void onIdentityCreated(final long handle,
|
||||
final ResultHandler<Long, RuntimeException> resultHandler) {
|
||||
activity.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
resultHandler.onResult(handle);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
package org.briarproject.android.controller.handler;
|
||||
|
||||
public interface ResultExceptionHandler<R, E extends Exception> {
|
||||
void onResult(R result);
|
||||
void onException(E exception);
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package org.briarproject.android.controller.handler;
|
||||
|
||||
public interface ResultHandler<R> {
|
||||
void onResult(R result);
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package org.briarproject.android.controller.handler;
|
||||
|
||||
import android.app.Activity;
|
||||
|
||||
public abstract class UiResultExceptionHandler<R, E extends Exception>
|
||||
implements ResultExceptionHandler<R, E> {
|
||||
|
||||
private final Activity activity;
|
||||
|
||||
public UiResultExceptionHandler(Activity activity) {
|
||||
this.activity = activity;
|
||||
}
|
||||
|
||||
public void onResult(final R result) {
|
||||
activity.runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
onResultUi(result);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void onException(final E exception) {
|
||||
activity.runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
onExceptionUi(exception);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public abstract void onResultUi(R result);
|
||||
|
||||
public abstract void onExceptionUi(E exception);
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package org.briarproject.android.controller.handler;
|
||||
|
||||
import android.app.Activity;
|
||||
|
||||
public abstract class UiResultHandler<R> implements ResultHandler<R> {
|
||||
|
||||
private final Activity activity;
|
||||
|
||||
public UiResultHandler(Activity activity) {
|
||||
this.activity = activity;
|
||||
}
|
||||
|
||||
public void onResult(final R result) {
|
||||
activity.runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
onResultUi(result);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public abstract void onResultUi(R result);
|
||||
}
|
||||
Reference in New Issue
Block a user