Commit shared preferences, clear instead of deleting.

This commit is contained in:
akwizgran
2018-05-11 09:54:49 +01:00
parent df7d48d54d
commit b77b885a94
4 changed files with 27 additions and 11 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

@@ -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,