Merge branch '1219-commit-shared-prefs' into 'master'

Commit shared preferences, clear instead of deleting

See merge request akwizgran/briar!794
This commit is contained in:
akwizgran
2018-05-14 14:18:49 +00:00
6 changed files with 30 additions and 14 deletions

View File

@@ -1,7 +1,9 @@
package org.briarproject.bramble.util; package org.briarproject.bramble.util;
import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build; import android.os.Build;
import android.provider.Settings; import android.provider.Settings;
@@ -42,6 +44,7 @@ public class AndroidUtils {
public static String getBluetoothAddress(Context ctx, public static String getBluetoothAddress(Context ctx,
BluetoothAdapter adapter) { BluetoothAdapter adapter) {
// Return the adapter's address if it's valid and not fake // Return the adapter's address if it's valid and not fake
@SuppressLint("HardwareIds")
String address = adapter.getAddress(); String address = adapter.getAddress();
if (isValidBluetoothAddress(address)) return address; if (isValidBluetoothAddress(address)) return address;
// Return the address from settings if it's valid and not fake // Return the address from settings if it's valid and not fake
@@ -96,14 +99,25 @@ public class AndroidUtils {
} }
} }
public static void deleteAppData(Context ctx) { @SuppressLint("ApplySharedPref")
public static void deleteAppData(Context ctx, SharedPreferences... clear) {
// Clear and commit shared preferences
for (SharedPreferences prefs : clear) {
boolean cleared = prefs.edit().clear().commit();
if (LOG.isLoggable(INFO)) {
if (cleared) LOG.info("Cleared shared preferences");
else LOG.info("Could not clear shared preferences");
}
}
// Delete files, except lib and shared_prefs directories
File dataDir = new File(ctx.getApplicationInfo().dataDir); File dataDir = new File(ctx.getApplicationInfo().dataDir);
if (LOG.isLoggable(INFO)) if (LOG.isLoggable(INFO))
LOG.info("Deleting app data from " + dataDir.getAbsolutePath()); LOG.info("Deleting app data from " + dataDir.getAbsolutePath());
File[] children = dataDir.listFiles(); File[] children = dataDir.listFiles();
if (children != null) { if (children != null) {
for (File child : children) { for (File child : children) {
if (!child.getName().equals("lib")) { String name = child.getName();
if (!name.equals("lib") && !name.equals("shared_prefs")) {
if (LOG.isLoggable(INFO)) if (LOG.isLoggable(INFO))
LOG.info("Deleting " + child.getAbsolutePath()); LOG.info("Deleting " + child.getAbsolutePath());
IoUtils.deleteFileOrDir(child); IoUtils.deleteFileOrDir(child);

View File

@@ -1,7 +1,9 @@
package org.briarproject.briar.android.controller; package org.briarproject.briar.android.controller;
import android.annotation.SuppressLint;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.support.v7.preference.PreferenceManager;
import org.briarproject.bramble.api.db.DatabaseConfig; import org.briarproject.bramble.api.db.DatabaseConfig;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
@@ -42,20 +44,18 @@ public class ConfigControllerImpl implements ConfigController {
} }
@Override @Override
@SuppressLint("ApplySharedPref")
public void storeEncryptedDatabaseKey(String hex) { public void storeEncryptedDatabaseKey(String hex) {
LOG.info("Storing database key in preferences"); LOG.info("Storing database key in preferences");
SharedPreferences.Editor editor = briarPrefs.edit(); briarPrefs.edit().putString(PREF_DB_KEY, hex).commit();
editor.putString(PREF_DB_KEY, hex);
editor.apply();
} }
@Override @Override
public void deleteAccount(Context ctx) { public void deleteAccount(Context ctx) {
LOG.info("Deleting account"); LOG.info("Deleting account");
SharedPreferences.Editor editor = briarPrefs.edit(); SharedPreferences defaultPrefs =
editor.clear(); PreferenceManager.getDefaultSharedPreferences(ctx);
editor.apply(); AndroidUtils.deleteAppData(ctx, briarPrefs, defaultPrefs);
AndroidUtils.deleteAppData(ctx);
AndroidUtils.logDataDirContents(ctx); AndroidUtils.logDataDirContents(ctx);
} }

View File

@@ -38,6 +38,8 @@ public class SetupActivity extends BaseActivity
setContentView(R.layout.activity_fragment_container); setContentView(R.layout.activity_fragment_container);
if (state == null) { if (state == null) {
if (setupController.accountExists())
throw new AssertionError();
showInitialFragment(AuthorNameFragment.newInstance()); showInitialFragment(AuthorNameFragment.newInstance());
} else { } else {
authorName = state.getString(STATE_KEY_AUTHOR_NAME); authorName = state.getString(STATE_KEY_AUTHOR_NAME);

View File

@@ -3,7 +3,7 @@ package org.briarproject.briar.android.login;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
@NotNullByDefault @NotNullByDefault
public interface SetupController { public interface SetupController extends PasswordController {
void setSetupActivity(SetupActivity setupActivity); void setSetupActivity(SetupActivity setupActivity);
@@ -13,8 +13,6 @@ public interface SetupController {
void setPassword(String password); void setPassword(String password);
float estimatePasswordStrength(String password);
/** /**
* This should be called after the author name has been set. * This should be called after the author name has been set.
*/ */

View File

@@ -54,7 +54,8 @@ public class PasswordControllerImplTest extends BrambleMockTestCase {
oneOf(briarPrefs).edit(); oneOf(briarPrefs).edit();
will(returnValue(editor)); will(returnValue(editor));
oneOf(editor).putString("key", newEncryptedHex); oneOf(editor).putString("key", newEncryptedHex);
oneOf(editor).apply(); will(returnValue(editor));
oneOf(editor).commit();
}}); }});
PasswordControllerImpl p = new PasswordControllerImpl(briarPrefs, PasswordControllerImpl p = new PasswordControllerImpl(briarPrefs,

View File

@@ -81,7 +81,8 @@ public class SetupControllerImplTest extends BrambleMockTestCase {
oneOf(briarPrefs).edit(); oneOf(briarPrefs).edit();
will(returnValue(editor)); will(returnValue(editor));
oneOf(editor).putString("key", encryptedHex); oneOf(editor).putString("key", encryptedHex);
oneOf(editor).apply(); will(returnValue(editor));
oneOf(editor).commit();
}}); }});
SetupControllerImpl s = new SetupControllerImpl(briarPrefs, SetupControllerImpl s = new SetupControllerImpl(briarPrefs,