diff --git a/briar-android/src/org/briarproject/android/BaseActivity.java b/briar-android/src/org/briarproject/android/BaseActivity.java index e8c3dd98a9fee80761e8340e5e185361402c9f5d..35cfab4b4d2e55e6cb5f591ce5ac71f8b638b19b 100644 --- a/briar-android/src/org/briarproject/android/BaseActivity.java +++ b/briar-android/src/org/briarproject/android/BaseActivity.java @@ -38,8 +38,9 @@ import static org.briarproject.android.TestingConstants.PREVENT_SCREENSHOTS; public abstract class BaseActivity extends AppCompatActivity implements RoboContext { - private final static String PREFS_DB = "db"; - private final static String PREFS_KEY = "key"; + public final static String PREFS_NAME = "db"; + public final static String PREF_DB_KEY = "key"; + public final static String PREF_SEEN_WELCOME_MESSAGE = "welcome_message"; private final HashMap<Key<?>, Object> scopedObjects = new HashMap<Key<?>, Object>(); @@ -128,18 +129,17 @@ public abstract class BaseActivity extends AppCompatActivity return scopedObjects; } - // FIXME: Factor out prefs code so it can be used by SplashScreenActivity private SharedPreferences getSharedPrefs() { - return getSharedPreferences(PREFS_DB, MODE_PRIVATE); + return getSharedPreferences(PREFS_NAME, MODE_PRIVATE); } protected String getEncryptedDatabaseKey() { - return getSharedPrefs().getString(PREFS_KEY, null); + return getSharedPrefs().getString(PREF_DB_KEY, null); } protected void storeEncryptedDatabaseKey(final String hex) { SharedPreferences.Editor editor = getSharedPrefs().edit(); - editor.putString(PREFS_KEY, hex); + editor.putString(PREF_DB_KEY, hex); editor.apply(); } diff --git a/briar-android/src/org/briarproject/android/NavDrawerActivity.java b/briar-android/src/org/briarproject/android/NavDrawerActivity.java index f56f06a6dcdf4fa72dbac24da90b86f32d9b0c4c..01760869268f103686e04a771ffd0a77026af16a 100644 --- a/briar-android/src/org/briarproject/android/NavDrawerActivity.java +++ b/briar-android/src/org/briarproject/android/NavDrawerActivity.java @@ -1,6 +1,5 @@ package org.briarproject.android; -import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.res.Configuration; @@ -20,9 +19,9 @@ import android.widget.ImageView; import android.widget.TextView; import org.briarproject.R; -import org.briarproject.android.fragment.BaseFragment; import org.briarproject.android.contact.ContactListFragment; import org.briarproject.android.forum.ForumListFragment; +import org.briarproject.android.fragment.BaseFragment; import org.briarproject.android.fragment.SettingsFragment; import org.briarproject.android.util.CustomAnimations; import org.briarproject.api.TransportId; @@ -59,9 +58,6 @@ public class NavDrawerActivity extends BriarFragmentActivity implements private static final Logger LOG = Logger.getLogger(NavDrawerActivity.class.getName()); - private final static String PREFS_USER_SETTINGS = "prefs_user_settings"; - private final static String KEY_SEEN_WELCOME_MESSAGE = "welcome_message"; - private ActionBarDrawerToggle drawerToggle; @Inject @@ -149,12 +145,12 @@ public class NavDrawerActivity extends BriarFragmentActivity implements } private void welcomeMessageCheck() { - SharedPreferences prefs = getSharedPreferences(PREFS_USER_SETTINGS, - Context.MODE_PRIVATE); - if (!prefs.getBoolean(KEY_SEEN_WELCOME_MESSAGE, false)) { + SharedPreferences prefs = getSharedPreferences(PREFS_NAME, + MODE_PRIVATE); + if (!prefs.getBoolean(PREF_SEEN_WELCOME_MESSAGE, false)) { showMessageDialog(R.string.dialog_title_welcome, R.string.dialog_welcome_message); - prefs.edit().putBoolean(KEY_SEEN_WELCOME_MESSAGE, true).apply(); + prefs.edit().putBoolean(PREF_SEEN_WELCOME_MESSAGE, true).apply(); } } diff --git a/briar-android/src/org/briarproject/android/PasswordActivity.java b/briar-android/src/org/briarproject/android/PasswordActivity.java index 9a08062f59e7de3cca67ac88ea3b99ba2f0d4d89..f9f60ed1a5c5143ad498d6fc3edce432e61d7162 100644 --- a/briar-android/src/org/briarproject/android/PasswordActivity.java +++ b/briar-android/src/org/briarproject/android/PasswordActivity.java @@ -21,7 +21,6 @@ import org.briarproject.api.crypto.CryptoComponent; import org.briarproject.api.crypto.CryptoExecutor; import org.briarproject.api.crypto.SecretKey; import org.briarproject.api.db.DatabaseConfig; -import org.briarproject.util.FileUtils; import org.briarproject.util.StringUtils; import java.util.concurrent.Executor; @@ -53,7 +52,7 @@ public class PasswordActivity extends BaseActivity { String hex = getEncryptedDatabaseKey(); if (hex == null || !databaseConfig.databaseExists()) { - clearSharedPrefsAndDeleteDatabase(); + clearSharedPrefsAndDeleteEverything(); return; } encrypted = StringUtils.fromHexString(hex); @@ -96,9 +95,9 @@ public class PasswordActivity extends BaseActivity { startActivity(intent); } - private void clearSharedPrefsAndDeleteDatabase() { + private void clearSharedPrefsAndDeleteEverything() { clearSharedPrefs(); - FileUtils.deleteFileOrDir(databaseConfig.getDatabaseDirectory()); + AndroidUtils.deleteAppData(this); setResult(RESULT_CANCELED); startActivity(new Intent(this, SetupActivity.class)); finish(); @@ -118,7 +117,7 @@ public class PasswordActivity extends BaseActivity { new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - clearSharedPrefsAndDeleteDatabase(); + clearSharedPrefsAndDeleteEverything(); } }); AlertDialog dialog = builder.create(); diff --git a/briar-android/src/org/briarproject/android/SplashScreenActivity.java b/briar-android/src/org/briarproject/android/SplashScreenActivity.java index 49eab780a57e7f388a389c7a9a255f844f4bb20a..c58427834b261d4c641354529fa6c03994705b92 100644 --- a/briar-android/src/org/briarproject/android/SplashScreenActivity.java +++ b/briar-android/src/org/briarproject/android/SplashScreenActivity.java @@ -14,9 +14,9 @@ import android.widget.LinearLayout; import com.google.inject.Injector; import org.briarproject.R; +import org.briarproject.android.util.AndroidUtils; import org.briarproject.android.util.LayoutUtils; import org.briarproject.api.db.DatabaseConfig; -import org.briarproject.util.FileUtils; import java.util.logging.Logger; @@ -26,6 +26,8 @@ import roboguice.activity.RoboSplashActivity; import static android.view.Gravity.CENTER; import static android.view.WindowManager.LayoutParams.FLAG_SECURE; import static java.util.logging.Level.INFO; +import static org.briarproject.android.BaseActivity.PREFS_NAME; +import static org.briarproject.android.BaseActivity.PREF_DB_KEY; import static org.briarproject.android.TestingConstants.DEFAULT_LOG_LEVEL; import static org.briarproject.android.TestingConstants.PREVENT_SCREENSHOTS; import static org.briarproject.android.TestingConstants.TESTING; @@ -79,16 +81,16 @@ public class SplashScreenActivity extends RoboSplashActivity { LOG.info("Expired"); startActivity(new Intent(this, ExpiredActivity.class)); } else { - SharedPreferences prefs = getSharedPreferences("db", MODE_PRIVATE); - String hex = prefs.getString("key", null); + SharedPreferences prefs = getSharedPreferences(PREFS_NAME, + MODE_PRIVATE); + String hex = prefs.getString(PREF_DB_KEY, null); Injector i = RoboGuice.getBaseApplicationInjector(getApplication()); DatabaseConfig databaseConfig = i.getInstance(DatabaseConfig.class); if (hex != null && databaseConfig.databaseExists()) { startActivity(new Intent(this, NavDrawerActivity.class)); } else { prefs.edit().clear().apply(); - FileUtils.deleteFileOrDir( - databaseConfig.getDatabaseDirectory()); + AndroidUtils.deleteAppData(this); startActivity(new Intent(this, SetupActivity.class)); } } diff --git a/briar-android/src/org/briarproject/android/panic/PanicResponderActivity.java b/briar-android/src/org/briarproject/android/panic/PanicResponderActivity.java index 674ce556efef10045500420cb64f856b4ab72cd1..2dd25617c0b7bb938b763b5464fe29cb8c9fc853 100644 --- a/briar-android/src/org/briarproject/android/panic/PanicResponderActivity.java +++ b/briar-android/src/org/briarproject/android/panic/PanicResponderActivity.java @@ -8,14 +8,11 @@ import android.os.Bundle; import android.support.v7.preference.PreferenceManager; import org.briarproject.android.BriarActivity; -import org.briarproject.api.db.DatabaseConfig; -import org.briarproject.util.FileUtils; +import org.briarproject.android.util.AndroidUtils; import org.iilab.IilabEngineeringRSA2048Pin; import java.util.logging.Logger; -import javax.inject.Inject; - import info.guardianproject.GuardianProjectRSA4096; import info.guardianproject.panic.Panic; import info.guardianproject.panic.PanicResponder; @@ -29,7 +26,6 @@ public class PanicResponderActivity extends BriarActivity { private static final Logger LOG = Logger.getLogger(PanicResponderActivity.class.getName()); - @Inject private DatabaseConfig databaseConfig; @Override public void onCreate(Bundle savedInstanceState) { @@ -105,9 +101,7 @@ public class PanicResponderActivity extends BriarActivity { public void run() { clearSharedPrefs(); // TODO somehow delete/shred the database more thoroughly - FileUtils - .deleteFileOrDir( - databaseConfig.getDatabaseDirectory()); + AndroidUtils.deleteAppData(PanicResponderActivity.this); PanicResponder.deleteAllAppData(PanicResponderActivity.this); // nothing left to do after everything is deleted, diff --git a/briar-android/src/org/briarproject/android/util/AndroidUtils.java b/briar-android/src/org/briarproject/android/util/AndroidUtils.java index 41599dd4ed8a24036d265a02cfb89704fe2a0798..005b20eb9b7d1aa8cb955b772d225c8143b65b47 100644 --- a/briar-android/src/org/briarproject/android/util/AndroidUtils.java +++ b/briar-android/src/org/briarproject/android/util/AndroidUtils.java @@ -7,8 +7,10 @@ import android.os.Build; import android.provider.Settings; import android.support.design.widget.TextInputLayout; +import org.briarproject.util.FileUtils; import org.briarproject.util.StringUtils; +import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -71,4 +73,15 @@ public class AndroidUtils { && BluetoothAdapter.checkBluetoothAddress(address) && !address.equals(FAKE_BLUETOOTH_ADDRESS); } + + public static void deleteAppData(Context ctx) { + File dataDir = new File(ctx.getApplicationInfo().dataDir); + File[] children = dataDir.listFiles(); + if (children != null) { + for (File child : children) { + if (!child.getName().equals("lib")) + FileUtils.deleteFileOrDir(child); + } + } + } } diff --git a/briar-core/src/org/briarproject/util/FileUtils.java b/briar-core/src/org/briarproject/util/FileUtils.java index 109841317e1fcafd801a66cee170d6a0cfa6a4ff..6bd5886f6b4c1635d15bb6a9f64dbaafd3e5f6f7 100644 --- a/briar-core/src/org/briarproject/util/FileUtils.java +++ b/briar-core/src/org/briarproject/util/FileUtils.java @@ -8,7 +8,9 @@ public class FileUtils { if (f.isFile()) { f.delete(); } else if (f.isDirectory()) { - for (File child : f.listFiles()) deleteFileOrDir(child); + File[] children = f.listFiles(); + if (children != null) + for (File child : children) deleteFileOrDir(child); f.delete(); } }