diff --git a/src/org/thoughtcrime/securesms/ApplicationContext.java b/src/org/thoughtcrime/securesms/ApplicationContext.java index f1c3c872db..bfa719a853 100644 --- a/src/org/thoughtcrime/securesms/ApplicationContext.java +++ b/src/org/thoughtcrime/securesms/ApplicationContext.java @@ -20,10 +20,14 @@ import android.annotation.SuppressLint; import android.arch.lifecycle.DefaultLifecycleObserver; import android.arch.lifecycle.LifecycleOwner; import android.arch.lifecycle.ProcessLifecycleOwner; +import android.content.ComponentName; import android.content.Context; +import android.content.Intent; import android.database.ContentObserver; import android.os.AsyncTask; import android.os.Build; +import android.os.Handler; +import android.preference.PreferenceManager; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.multidex.MultiDexApplication; @@ -38,6 +42,7 @@ import org.signal.aesgcmprovider.AesGcmProvider; import org.thoughtcrime.securesms.components.TypingStatusRepository; import org.thoughtcrime.securesms.components.TypingStatusSender; import org.thoughtcrime.securesms.crypto.IdentityKeyUtil; +import org.thoughtcrime.securesms.crypto.MasterSecretUtil; import org.thoughtcrime.securesms.database.DatabaseContentProviders; import org.thoughtcrime.securesms.database.DatabaseFactory; import org.thoughtcrime.securesms.dependencies.AxolotlStorageModule; @@ -154,8 +159,9 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc @Override public void onCreate() { super.onCreate(); - startKovenant(); Log.i(TAG, "onCreate()"); + checkNeedsDatabaseReset(); + startKovenant(); initializeSecurityProvider(); initializeLogging(); initializeCrashHandling(); @@ -588,4 +594,27 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc if (lokiMessengerUpdatesFeedPoller != null) lokiMessengerUpdatesFeedPoller.startIfNeeded(); } // endregion + + public void checkNeedsDatabaseReset() { + if (TextSecurePreferences.resetDatabase(this)) { + PreferenceManager.getDefaultSharedPreferences(this).edit().clear().commit(); + MasterSecretUtil.clear(this); + if (this.deleteDatabase("signal.db")) { + Log.d("Loki", "Deleted database"); + } + } + } + + public void clearData() { + TextSecurePreferences.setResetDatabase(this, true); + new Handler().postDelayed(this::restartApplication, 200); + } + + public void restartApplication() { + Intent intent = new Intent(this, ConversationListActivity.class); + ComponentName componentName = intent.getComponent(); + Intent mainIntent = Intent.makeRestartActivityTask(componentName); + this.startActivity(mainIntent); + Runtime.getRuntime().exit(0); + } } diff --git a/src/org/thoughtcrime/securesms/PassphraseRequiredActionBarActivity.java b/src/org/thoughtcrime/securesms/PassphraseRequiredActionBarActivity.java index f36aac543b..5f24485fac 100644 --- a/src/org/thoughtcrime/securesms/PassphraseRequiredActionBarActivity.java +++ b/src/org/thoughtcrime/securesms/PassphraseRequiredActionBarActivity.java @@ -5,6 +5,7 @@ import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; +import android.preference.PreferenceManager; import android.support.annotation.IdRes; import android.support.annotation.NonNull; import android.support.annotation.Nullable; diff --git a/src/org/thoughtcrime/securesms/crypto/MasterSecretUtil.java b/src/org/thoughtcrime/securesms/crypto/MasterSecretUtil.java index 0542dbd73f..4d964d0291 100644 --- a/src/org/thoughtcrime/securesms/crypto/MasterSecretUtil.java +++ b/src/org/thoughtcrime/securesms/crypto/MasterSecretUtil.java @@ -198,6 +198,10 @@ public class MasterSecretUtil { return preferences.getBoolean("passphrase_initialized", false); } + public static void clear(Context context) { + context.getSharedPreferences(PREFERENCES_NAME, 0).edit().clear().commit(); + } + private static void save(Context context, String key, int value) { if (!context.getSharedPreferences(PREFERENCES_NAME, 0) .edit() diff --git a/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java b/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java index ef82974f7f..c3c77a3b1f 100644 --- a/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java +++ b/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java @@ -1185,5 +1185,13 @@ public class TextSecurePreferences { public static void setMasterHexEncodedPublicKey(Context context, String masterHexEncodedPublicKey) { setStringPreference(context, "master_hex_encoded_public_key", masterHexEncodedPublicKey.toLowerCase()); } + + public static void setResetDatabase(Context context, boolean resetDatabase) { + PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean("database_reset", resetDatabase).commit(); + } + + public static boolean resetDatabase(Context context) { + return getBooleanPreference(context, "database_reset", false); + } // endregion }