Remove logic from DatabaseConfig.

This commit is contained in:
akwizgran
2018-07-26 15:57:37 +01:00
parent f9495b49d6
commit 6f6ba38de1
8 changed files with 7 additions and 54 deletions

View File

@@ -7,8 +7,6 @@ import java.io.File;
@NotNullByDefault
public interface DatabaseConfig {
boolean databaseExists();
File getDatabaseDirectory();
File getDatabaseKeyDirectory();

View File

@@ -50,8 +50,7 @@ class H2Database extends JdbcDatabase {
public boolean open(SecretKey key, @Nullable MigrationListener listener)
throws DbException {
this.key = key;
boolean reopen = config.databaseExists();
if (!reopen) config.getDatabaseDirectory().mkdirs();
boolean reopen = !config.getDatabaseDirectory().mkdirs();
super.open("org.h2.Driver", reopen, key, listener);
return reopen;
}

View File

@@ -52,8 +52,7 @@ class HyperSqlDatabase extends JdbcDatabase {
public boolean open(SecretKey key, @Nullable MigrationListener listener)
throws DbException {
this.key = key;
boolean reopen = config.databaseExists();
if (!reopen) config.getDatabaseDirectory().mkdirs();
boolean reopen = !config.getDatabaseDirectory().mkdirs();
super.open("org.hsqldb.jdbc.JDBCDriver", reopen, key, listener);
return reopen;
}

View File

@@ -17,13 +17,6 @@ public class TestDatabaseConfig implements DatabaseConfig {
this.maxSize = maxSize;
}
@Override
public boolean databaseExists() {
if (!dbDir.isDirectory()) return false;
File[] files = dbDir.listFiles();
return files != null && files.length > 0;
}
@Override
public File getDatabaseDirectory() {
return dbDir;

View File

@@ -4,16 +4,10 @@ import org.briarproject.bramble.api.db.DatabaseConfig;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import java.io.File;
import java.util.logging.Logger;
import static java.util.logging.Level.INFO;
@NotNullByDefault
class AndroidDatabaseConfig implements DatabaseConfig {
private static final Logger LOG =
Logger.getLogger(AndroidDatabaseConfig.class.getName());
private final File dbDir, keyDir;
AndroidDatabaseConfig(File dbDir, File keyDir) {
@@ -21,38 +15,13 @@ class AndroidDatabaseConfig implements DatabaseConfig {
this.keyDir = keyDir;
}
@Override
public boolean databaseExists() {
// FIXME should not run on UiThread #620
if (!dbDir.isDirectory()) {
if (LOG.isLoggable(INFO))
LOG.info(dbDir.getAbsolutePath() + " is not a directory");
return false;
}
File[] files = dbDir.listFiles();
if (LOG.isLoggable(INFO)) {
if (files == null) {
LOG.info("Could not list files in " + dbDir.getAbsolutePath());
} else {
LOG.info("Files in " + dbDir.getAbsolutePath() + ":");
for (File f : files) LOG.info(f.getName());
}
LOG.info("Database exists: " + (files != null && files.length > 0));
}
return files != null && files.length > 0;
}
@Override
public File getDatabaseDirectory() {
if (LOG.isLoggable(INFO))
LOG.info("Database directory: " + dbDir.getAbsolutePath());
return dbDir;
}
@Override
public File getDatabaseKeyDirectory() {
if (LOG.isLoggable(INFO))
LOG.info("Database key directory: " + keyDir.getAbsolutePath());
return keyDir;
}

View File

@@ -11,9 +11,7 @@ import org.briarproject.bramble.api.db.DatabaseConfig;
import org.briarproject.bramble.api.event.EventBus;
import org.briarproject.bramble.api.lifecycle.IoExecutor;
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
import org.briarproject.bramble.api.plugin.BackoffFactory;
import org.briarproject.bramble.api.plugin.PluginConfig;
import org.briarproject.bramble.api.plugin.duplex.DuplexPluginFactory;
@@ -86,11 +84,7 @@ public class AppModule {
File dbDir = app.getApplicationContext().getDir("db", MODE_PRIVATE);
File keyDir = app.getApplicationContext().getDir("key", MODE_PRIVATE);
StrictMode.setThreadPolicy(tp);
@MethodsNotNullByDefault
@ParametersNotNullByDefault
DatabaseConfig databaseConfig =
new AndroidDatabaseConfig(dbDir, keyDir);
return databaseConfig;
return new AndroidDatabaseConfig(dbDir, keyDir);
}
@Provides

View File

@@ -76,8 +76,8 @@ public class ConfigControllerImpl implements ConfigController {
@Override
public boolean accountExists() {
String hex = getEncryptedDatabaseKey();
return hex != null && databaseConfig.databaseExists();
return getEncryptedDatabaseKey() != null &&
databaseConfig.getDatabaseDirectory().isDirectory();
}
@Override

View File

@@ -51,7 +51,8 @@ public class SignInReminderReceiver extends BroadcastReceiver {
if (action == null) return;
if (action.equals(ACTION_BOOT_COMPLETED) ||
action.equals(ACTION_MY_PACKAGE_REPLACED)) {
if (databaseConfig.databaseExists()) {
// TODO: Use account manager to check whether account exists
if (databaseConfig.getDatabaseDirectory().isDirectory()) {
SharedPreferences prefs = app.getDefaultSharedPreferences();
if (prefs.getBoolean(NOTIFY_SIGN_IN, true)) {
showSignInNotification(ctx);