Commit cb29c9bf authored by akwizgran's avatar akwizgran

Create minimal AccountManager interface.

parent 8283760e
package org.briarproject.bramble.api.account;
public interface AccountManager {
boolean hasDatabaseKey();
}
package org.briarproject.bramble;
import org.briarproject.bramble.account.AccountModule;
import org.briarproject.bramble.client.ClientModule;
import org.briarproject.bramble.contact.ContactModule;
import org.briarproject.bramble.crypto.CryptoExecutorModule;
......@@ -26,6 +27,7 @@ import org.briarproject.bramble.versioning.VersioningModule;
import dagger.Module;
@Module(includes = {
AccountModule.class,
ClientModule.class,
ContactModule.class,
CryptoModule.class,
......
package org.briarproject.bramble.account;
import org.briarproject.bramble.api.account.AccountManager;
import org.briarproject.bramble.api.db.DatabaseConfig;
import javax.inject.Inject;
class AccountManagerImpl implements AccountManager {
private final DatabaseConfig databaseConfig;
@Inject
AccountManagerImpl(DatabaseConfig databaseConfig) {
this.databaseConfig = databaseConfig;
}
@Override
public boolean hasDatabaseKey() {
return databaseConfig.getEncryptionKey() != null;
}
}
package org.briarproject.bramble.account;
import org.briarproject.bramble.api.account.AccountManager;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
@Module
public class AccountModule {
@Provides
@Singleton
AccountManager provideAccountManager(AccountManagerImpl accountManager) {
return accountManager;
}
}
......@@ -5,6 +5,7 @@ import android.content.SharedPreferences;
import org.briarproject.bramble.BrambleAndroidModule;
import org.briarproject.bramble.BrambleCoreEagerSingletons;
import org.briarproject.bramble.BrambleCoreModule;
import org.briarproject.bramble.api.account.AccountManager;
import org.briarproject.bramble.api.contact.ContactExchangeTask;
import org.briarproject.bramble.api.contact.ContactManager;
import org.briarproject.bramble.api.crypto.CryptoComponent;
......@@ -151,6 +152,8 @@ public interface AndroidComponent
@IoExecutor
Executor ioExecutor();
AccountManager accountManager();
void inject(SignInReminderReceiver briarService);
void inject(BriarService briarService);
......
......@@ -17,7 +17,7 @@ import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.ContextCompat;
import org.briarproject.bramble.api.db.DatabaseConfig;
import org.briarproject.bramble.api.account.AccountManager;
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
import org.briarproject.bramble.api.lifecycle.LifecycleManager.StartResult;
import org.briarproject.bramble.api.system.AndroidExecutor;
......@@ -75,12 +75,13 @@ public class BriarService extends Service {
private BroadcastReceiver receiver = null;
@Inject
protected DatabaseConfig databaseConfig;
AccountManager accountManager;
// Fields that are accessed from background threads must be volatile
@Inject
protected volatile LifecycleManager lifecycleManager;
volatile LifecycleManager lifecycleManager;
@Inject
protected volatile AndroidExecutor androidExecutor;
volatile AndroidExecutor androidExecutor;
private volatile boolean started = false;
@Override
......@@ -96,7 +97,7 @@ public class BriarService extends Service {
stopSelf();
return;
}
if (databaseConfig.getEncryptionKey() == null) {
if (!accountManager.hasDatabaseKey()) {
LOG.info("No database key");
stopSelf();
return;
......
......@@ -61,7 +61,7 @@ public abstract class BriarActivity extends BaseActivity {
@Override
public void onStart() {
super.onStart();
if (!briarController.hasEncryptionKey() && !isFinishing()) {
if (!briarController.accountSignedIn() && !isFinishing()) {
Intent i = new Intent(this, PasswordActivity.class);
startActivityForResult(i, REQUEST_PASSWORD);
} else if (SDK_INT >= 23) {
......@@ -138,7 +138,7 @@ public abstract class BriarActivity extends BaseActivity {
}
protected void signOut(boolean removeFromRecentApps) {
if (briarController.hasEncryptionKey()) {
if (briarController.accountSignedIn()) {
// Don't use UiResultHandler because we want the result even if
// this activity has been destroyed
briarController.signOut(result -> runOnUiThread(
......
......@@ -6,7 +6,7 @@ public interface BriarController extends ActivityLifecycleController {
void startAndBindService();
boolean hasEncryptionKey();
boolean accountSignedIn();
/**
* Returns true via the handler when the app has dozed
......
......@@ -5,7 +5,7 @@ import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.CallSuper;
import org.briarproject.bramble.api.db.DatabaseConfig;
import org.briarproject.bramble.api.account.AccountManager;
import org.briarproject.bramble.api.db.DatabaseExecutor;
import org.briarproject.bramble.api.db.DbException;
import org.briarproject.bramble.api.settings.Settings;
......@@ -33,8 +33,7 @@ public class BriarControllerImpl implements BriarController {
public static final String DOZE_ASK_AGAIN = "dozeAskAgain";
private final BriarServiceConnection serviceConnection;
private final DatabaseConfig databaseConfig;
@DatabaseExecutor
private final AccountManager accountManager;
private final Executor databaseExecutor;
private final SettingsManager settingsManager;
private final DozeWatchdog dozeWatchdog;
......@@ -44,12 +43,12 @@ public class BriarControllerImpl implements BriarController {
@Inject
BriarControllerImpl(BriarServiceConnection serviceConnection,
DatabaseConfig databaseConfig,
AccountManager accountManager,
@DatabaseExecutor Executor databaseExecutor,
SettingsManager settingsManager, DozeWatchdog dozeWatchdog,
Activity activity) {
this.serviceConnection = serviceConnection;
this.databaseConfig = databaseConfig;
this.accountManager = accountManager;
this.databaseExecutor = databaseExecutor;
this.settingsManager = settingsManager;
this.dozeWatchdog = dozeWatchdog;
......@@ -59,7 +58,7 @@ public class BriarControllerImpl implements BriarController {
@Override
@CallSuper
public void onActivityCreate(Activity activity) {
if (databaseConfig.getEncryptionKey() != null) startAndBindService();
if (accountManager.hasDatabaseKey()) startAndBindService();
}
@Override
......@@ -84,8 +83,8 @@ public class BriarControllerImpl implements BriarController {
}
@Override
public boolean hasEncryptionKey() {
return databaseConfig.getEncryptionKey() != null;
public boolean accountSignedIn() {
return accountManager.hasDatabaseKey();
}
@Override
......
......@@ -4,6 +4,7 @@ import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.preference.PreferenceManager;
import org.briarproject.bramble.api.account.AccountManager;
import org.briarproject.bramble.api.db.DatabaseConfig;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.util.AndroidUtils;
......@@ -34,12 +35,14 @@ public class ConfigControllerImpl implements ConfigController {
private final SharedPreferences briarPrefs;
private final File dbKeyFile, dbKeyBackupFile;
private final AccountManager accountManager;
protected final DatabaseConfig databaseConfig;
@Inject
public ConfigControllerImpl(SharedPreferences briarPrefs,
DatabaseConfig databaseConfig) {
AccountManager accountManager, DatabaseConfig databaseConfig) {
this.briarPrefs = briarPrefs;
this.accountManager = accountManager;
this.databaseConfig = databaseConfig;
File keyDir = databaseConfig.getDatabaseKeyDirectory();
dbKeyFile = new File(keyDir, DB_KEY_FILENAME);
......@@ -166,6 +169,6 @@ public class ConfigControllerImpl implements ConfigController {
@Override
public boolean accountSignedIn() {
return databaseConfig.getEncryptionKey() != null;
return accountManager.hasDatabaseKey();
}
}
......@@ -85,7 +85,7 @@ public class PasswordActivity extends BaseActivity {
public void onStart() {
super.onStart();
// If the user has already signed in, clean up this instance
if (briarController.hasEncryptionKey()) {
if (briarController.accountSignedIn()) {
setResult(RESULT_OK);
finish();
}
......
......@@ -2,6 +2,7 @@ package org.briarproject.briar.android.login;
import android.content.SharedPreferences;
import org.briarproject.bramble.api.account.AccountManager;
import org.briarproject.bramble.api.crypto.CryptoComponent;
import org.briarproject.bramble.api.crypto.CryptoExecutor;
import org.briarproject.bramble.api.crypto.PasswordStrengthEstimator;
......@@ -33,10 +34,10 @@ public class PasswordControllerImpl extends ConfigControllerImpl
@Inject
PasswordControllerImpl(SharedPreferences briarPrefs,
DatabaseConfig databaseConfig,
AccountManager accountManager, DatabaseConfig databaseConfig,
@CryptoExecutor Executor cryptoExecutor, CryptoComponent crypto,
PasswordStrengthEstimator strengthEstimator) {
super(briarPrefs, databaseConfig);
super(briarPrefs, accountManager, databaseConfig);
this.cryptoExecutor = cryptoExecutor;
this.crypto = crypto;
this.strengthEstimator = strengthEstimator;
......
......@@ -3,6 +3,7 @@ package org.briarproject.briar.android.login;
import android.content.SharedPreferences;
import android.support.annotation.Nullable;
import org.briarproject.bramble.api.account.AccountManager;
import org.briarproject.bramble.api.crypto.CryptoComponent;
import org.briarproject.bramble.api.crypto.CryptoExecutor;
import org.briarproject.bramble.api.crypto.PasswordStrengthEstimator;
......@@ -33,12 +34,12 @@ public class SetupControllerImpl extends PasswordControllerImpl
@Inject
SetupControllerImpl(SharedPreferences briarPrefs,
DatabaseConfig databaseConfig,
AccountManager accountManager, DatabaseConfig databaseConfig,
@CryptoExecutor Executor cryptoExecutor, CryptoComponent crypto,
PasswordStrengthEstimator strengthEstimator,
IdentityManager identityManager) {
super(briarPrefs, databaseConfig, cryptoExecutor, crypto,
strengthEstimator);
super(briarPrefs, accountManager, databaseConfig, cryptoExecutor,
crypto, strengthEstimator);
this.identityManager = identityManager;
}
......
......@@ -3,6 +3,7 @@ package org.briarproject.briar.android.controller;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import org.briarproject.bramble.api.account.AccountManager;
import org.briarproject.bramble.api.db.DatabaseConfig;
import org.briarproject.bramble.test.BrambleMockTestCase;
import org.jmock.Expectations;
......@@ -26,6 +27,8 @@ public class ConfigControllerImplTest extends BrambleMockTestCase {
private final SharedPreferences prefs =
context.mock(SharedPreferences.class);
private final AccountManager accountManager =
context.mock(AccountManager.class);
private final DatabaseConfig databaseConfig =
context.mock(DatabaseConfig.class);
private final Editor editor = context.mock(Editor.class);
......@@ -56,7 +59,7 @@ public class ConfigControllerImplTest extends BrambleMockTestCase {
assertFalse(keyFile.exists());
assertFalse(keyBackupFile.exists());
ConfigControllerImpl c = new ConfigControllerImpl(prefs,
ConfigControllerImpl c = new ConfigControllerImpl(prefs, accountManager,
databaseConfig);
assertEquals(encryptedKeyHex, c.getEncryptedDatabaseKey());
......@@ -85,7 +88,7 @@ public class ConfigControllerImplTest extends BrambleMockTestCase {
assertFalse(keyBackupFile.exists());
assertEquals(encryptedKeyHex, loadDatabaseKey(keyFile));
ConfigControllerImpl c = new ConfigControllerImpl(prefs,
ConfigControllerImpl c = new ConfigControllerImpl(prefs, accountManager,
databaseConfig);
assertEquals(encryptedKeyHex, c.getEncryptedDatabaseKey());
......@@ -113,7 +116,7 @@ public class ConfigControllerImplTest extends BrambleMockTestCase {
assertTrue(keyBackupFile.exists());
assertEquals(encryptedKeyHex, loadDatabaseKey(keyBackupFile));
ConfigControllerImpl c = new ConfigControllerImpl(prefs,
ConfigControllerImpl c = new ConfigControllerImpl(prefs, accountManager,
databaseConfig);
assertEquals(encryptedKeyHex, c.getEncryptedDatabaseKey());
......@@ -135,7 +138,7 @@ public class ConfigControllerImplTest extends BrambleMockTestCase {
assertFalse(keyFile.exists());
assertFalse(keyBackupFile.exists());
ConfigControllerImpl c = new ConfigControllerImpl(prefs,
ConfigControllerImpl c = new ConfigControllerImpl(prefs, accountManager,
databaseConfig);
assertNull(c.getEncryptedDatabaseKey());
......@@ -160,7 +163,7 @@ public class ConfigControllerImplTest extends BrambleMockTestCase {
assertFalse(keyBackupFile.exists());
assertEquals(oldEncryptedKeyHex, loadDatabaseKey(keyFile));
ConfigController c = new ConfigControllerImpl(prefs,
ConfigController c = new ConfigControllerImpl(prefs, accountManager,
databaseConfig);
assertTrue(c.storeEncryptedDatabaseKey(encryptedKeyHex));
......@@ -187,7 +190,7 @@ public class ConfigControllerImplTest extends BrambleMockTestCase {
assertTrue(keyBackupFile.exists());
assertEquals(oldEncryptedKeyHex, loadDatabaseKey(keyBackupFile));
ConfigController c = new ConfigControllerImpl(prefs,
ConfigController c = new ConfigControllerImpl(prefs, accountManager,
databaseConfig);
assertTrue(c.storeEncryptedDatabaseKey(encryptedKeyHex));
......
......@@ -45,7 +45,7 @@ public class TestForumActivity extends ForumActivity {
protected BriarController provideBriarController(
BriarControllerImpl briarController) {
BriarController c = Mockito.mock(BriarController.class);
Mockito.when(c.hasEncryptionKey()).thenReturn(true);
Mockito.when(c.accountSignedIn()).thenReturn(true);
return c;
}
......
......@@ -2,6 +2,7 @@ package org.briarproject.briar.android.login;
import android.content.SharedPreferences;
import org.briarproject.bramble.api.account.AccountManager;
import org.briarproject.bramble.api.crypto.CryptoComponent;
import org.briarproject.bramble.api.crypto.PasswordStrengthEstimator;
import org.briarproject.bramble.api.db.DatabaseConfig;
......@@ -30,6 +31,8 @@ public class PasswordControllerImplTest extends BrambleMockTestCase {
private final SharedPreferences briarPrefs =
context.mock(SharedPreferences.class);
private final AccountManager accountManager =
context.mock(AccountManager.class);
private final DatabaseConfig databaseConfig =
context.mock(DatabaseConfig.class);
private final CryptoComponent crypto = context.mock(CryptoComponent.class);
......@@ -70,7 +73,8 @@ public class PasswordControllerImplTest extends BrambleMockTestCase {
storeDatabaseKey(keyBackupFile, toHexString(oldEncryptedKey));
PasswordControllerImpl p = new PasswordControllerImpl(briarPrefs,
databaseConfig, cryptoExecutor, crypto, estimator);
accountManager, databaseConfig, cryptoExecutor, crypto,
estimator);
AtomicBoolean capturedResult = new AtomicBoolean(false);
p.changePassword(oldPassword, newPassword, capturedResult::set);
......@@ -104,7 +108,8 @@ public class PasswordControllerImplTest extends BrambleMockTestCase {
storeDatabaseKey(keyBackupFile, toHexString(oldEncryptedKey));
PasswordControllerImpl p = new PasswordControllerImpl(briarPrefs,
databaseConfig, cryptoExecutor, crypto, estimator);
accountManager, databaseConfig, cryptoExecutor, crypto,
estimator);
AtomicBoolean capturedResult = new AtomicBoolean(true);
p.changePassword(oldPassword, newPassword, capturedResult::set);
......
......@@ -3,6 +3,7 @@ package org.briarproject.briar.android.login;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import org.briarproject.bramble.api.account.AccountManager;
import org.briarproject.bramble.api.crypto.CryptoComponent;
import org.briarproject.bramble.api.crypto.PasswordStrengthEstimator;
import org.briarproject.bramble.api.crypto.SecretKey;
......@@ -37,6 +38,8 @@ public class SetupControllerImplTest extends BrambleMockTestCase {
private final SharedPreferences briarPrefs =
context.mock(SharedPreferences.class);
private final AccountManager accountManager =
context.mock(AccountManager.class);
private final DatabaseConfig databaseConfig =
context.mock(DatabaseConfig.class);
private final CryptoComponent crypto = context.mock(CryptoComponent.class);
......@@ -101,8 +104,8 @@ public class SetupControllerImplTest extends BrambleMockTestCase {
assertFalse(keyBackupFile.exists());
SetupControllerImpl s = new SetupControllerImpl(briarPrefs,
databaseConfig, cryptoExecutor, crypto, estimator,
identityManager);
accountManager, databaseConfig, cryptoExecutor, crypto,
estimator, identityManager);
s.setSetupActivity(setupActivity);
AtomicBoolean called = new AtomicBoolean(false);
......
package org.briarproject.briar.test;
import org.briarproject.bramble.account.AccountModule;
import org.briarproject.bramble.api.client.ClientHelper;
import org.briarproject.bramble.api.contact.ContactManager;
import org.briarproject.bramble.api.db.DatabaseComponent;
......@@ -54,6 +55,7 @@ import dagger.Component;
TestDatabaseModule.class,
TestPluginConfigModule.class,
TestSecureRandomModule.class,
AccountModule.class,
BlogModule.class,
BriarClientModule.class,
ClientModule.class,
......
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