From 34e147838a002df061bfa8b2044e6a057977d92d Mon Sep 17 00:00:00 2001 From: Jake McGinty Date: Mon, 5 May 2014 13:48:26 -0500 Subject: [PATCH] use apply for preferences instead of commit // FREEBIE --- .../securesms/AutoInitiateActivity.java | 2 +- .../securesms/MmsPreferencesActivity.java | 6 ++-- .../securesms/crypto/IdentityKeyUtil.java | 2 +- .../securesms/crypto/MasterSecretUtil.java | 33 ++++++++++++------- .../database/CanonicalSessionMigrator.java | 2 +- .../securesms/database/SmsMigrator.java | 2 +- .../service/ApplicationMigrationService.java | 2 +- .../thoughtcrime/securesms/util/Emoji.java | 2 +- .../securesms/util/TextSecurePreferences.java | 29 +++++++++++++--- .../securesms/util/VersionTracker.java | 13 ++++---- 10 files changed, 60 insertions(+), 33 deletions(-) diff --git a/src/org/thoughtcrime/securesms/AutoInitiateActivity.java b/src/org/thoughtcrime/securesms/AutoInitiateActivity.java index 028d59be2d..1842148da5 100644 --- a/src/org/thoughtcrime/securesms/AutoInitiateActivity.java +++ b/src/org/thoughtcrime/securesms/AutoInitiateActivity.java @@ -95,7 +95,7 @@ public class AutoInitiateActivity extends Activity { public static void exemptThread(Context context, long threadId) { SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); - sp.edit().putBoolean("pref_thread_auto_init_exempt_" + threadId, true).commit(); + sp.edit().putBoolean("pref_thread_auto_init_exempt_" + threadId, true).apply(); } public static boolean isValidAutoInitiateSituation(Context context, MasterSecret masterSecret, diff --git a/src/org/thoughtcrime/securesms/MmsPreferencesActivity.java b/src/org/thoughtcrime/securesms/MmsPreferencesActivity.java index db6dc555a2..3818b4be97 100644 --- a/src/org/thoughtcrime/securesms/MmsPreferencesActivity.java +++ b/src/org/thoughtcrime/securesms/MmsPreferencesActivity.java @@ -87,8 +87,7 @@ public class MmsPreferencesActivity extends PassphraseRequiredSherlockPreference private void initializePreferences() { if (!MmsDownloadHelper.isMmsConnectionParametersAvailable(this, null, false)) { - PreferenceManager.getDefaultSharedPreferences(this).edit() - .putBoolean(TextSecurePreferences.ENABLE_MANUAL_MMS_PREF, true).commit(); + TextSecurePreferences.setUseLocalApnsEnabled(this, true); addPreferencesFromResource(R.xml.mms_preferences); this.findPreference(TextSecurePreferences.ENABLE_MANUAL_MMS_PREF).setOnPreferenceChangeListener(new OverrideMmsChangeListener()); } else { @@ -127,8 +126,7 @@ public class MmsPreferencesActivity extends PassphraseRequiredSherlockPreference private class OverrideMmsChangeListener implements Preference.OnPreferenceChangeListener { @Override public boolean onPreferenceChange(Preference preference, Object o) { - PreferenceManager.getDefaultSharedPreferences(MmsPreferencesActivity.this).edit() - .putBoolean(TextSecurePreferences.ENABLE_MANUAL_MMS_PREF, true).commit(); + TextSecurePreferences.setUseLocalApnsEnabled(MmsPreferencesActivity.this, true); Toast.makeText(MmsPreferencesActivity.this, R.string.mms_preferences_activity__manual_mms_settings_are_required, Toast.LENGTH_SHORT).show(); return false; } diff --git a/src/org/thoughtcrime/securesms/crypto/IdentityKeyUtil.java b/src/org/thoughtcrime/securesms/crypto/IdentityKeyUtil.java index ddb1d5d95e..9c5832ae89 100644 --- a/src/org/thoughtcrime/securesms/crypto/IdentityKeyUtil.java +++ b/src/org/thoughtcrime/securesms/crypto/IdentityKeyUtil.java @@ -124,6 +124,6 @@ public class IdentityKeyUtil { Editor preferencesEditor = preferences.edit(); preferencesEditor.putString(key, value); - preferencesEditor.commit(); + if (!preferencesEditor.commit()) throw new AssertionError("failed to save identity key/value to shared preferences"); } } diff --git a/src/org/thoughtcrime/securesms/crypto/MasterSecretUtil.java b/src/org/thoughtcrime/securesms/crypto/MasterSecretUtil.java index b8a460bc61..231588a077 100644 --- a/src/org/thoughtcrime/securesms/crypto/MasterSecretUtil.java +++ b/src/org/thoughtcrime/securesms/crypto/MasterSecretUtil.java @@ -200,24 +200,33 @@ public class MasterSecretUtil { } private static void save(Context context, String key, int value) { - context.getSharedPreferences(PREFERENCES_NAME, 0) - .edit() - .putInt(key, value) - .commit(); + if (!context.getSharedPreferences(PREFERENCES_NAME, 0) + .edit() + .putInt(key, value) + .commit()) + { + throw new AssertionError("failed to save a shared pref in MasterSecretUtil"); + } } private static void save(Context context, String key, byte[] value) { - context.getSharedPreferences(PREFERENCES_NAME, 0) - .edit() - .putString(key, Base64.encodeBytes(value)) - .commit(); + if (!context.getSharedPreferences(PREFERENCES_NAME, 0) + .edit() + .putString(key, Base64.encodeBytes(value)) + .commit()) + { + throw new AssertionError("failed to save a shared pref in MasterSecretUtil"); + } } private static void save(Context context, String key, boolean value) { - context.getSharedPreferences(PREFERENCES_NAME, 0) - .edit() - .putBoolean(key, value) - .commit(); + if (!context.getSharedPreferences(PREFERENCES_NAME, 0) + .edit() + .putBoolean(key, value) + .commit()) + { + throw new AssertionError("failed to save a shared pref in MasterSecretUtil"); + } } private static byte[] retrieve(Context context, String key) throws IOException { diff --git a/src/org/thoughtcrime/securesms/database/CanonicalSessionMigrator.java b/src/org/thoughtcrime/securesms/database/CanonicalSessionMigrator.java index 8a92f61249..44375a63a9 100644 --- a/src/org/thoughtcrime/securesms/database/CanonicalSessionMigrator.java +++ b/src/org/thoughtcrime/securesms/database/CanonicalSessionMigrator.java @@ -64,7 +64,7 @@ public class CanonicalSessionMigrator { } } - context.getSharedPreferences("SecureSMS", Context.MODE_PRIVATE).edit().putBoolean("canonicalized", true).commit(); + context.getSharedPreferences("SecureSMS", Context.MODE_PRIVATE).edit().putBoolean("canonicalized", true).apply(); } } diff --git a/src/org/thoughtcrime/securesms/database/SmsMigrator.java b/src/org/thoughtcrime/securesms/database/SmsMigrator.java index bf72a18eda..3525884c5d 100644 --- a/src/org/thoughtcrime/securesms/database/SmsMigrator.java +++ b/src/org/thoughtcrime/securesms/database/SmsMigrator.java @@ -223,7 +223,7 @@ public class SmsMigrator { } context.getSharedPreferences("SecureSMS", Context.MODE_PRIVATE).edit() - .putBoolean("migrated", true).commit(); + .putBoolean("migrated", true).apply(); } public interface SmsMigrationProgressListener { diff --git a/src/org/thoughtcrime/securesms/service/ApplicationMigrationService.java b/src/org/thoughtcrime/securesms/service/ApplicationMigrationService.java index 21258085ca..f91af8cf3a 100644 --- a/src/org/thoughtcrime/securesms/service/ApplicationMigrationService.java +++ b/src/org/thoughtcrime/securesms/service/ApplicationMigrationService.java @@ -215,6 +215,6 @@ public class ApplicationMigrationService extends Service } public static void setDatabaseImported(Context context) { - context.getSharedPreferences(PREFERENCES_NAME, 0).edit().putBoolean(DATABASE_MIGRATED, true).commit(); + context.getSharedPreferences(PREFERENCES_NAME, 0).edit().putBoolean(DATABASE_MIGRATED, true).apply(); } } diff --git a/src/org/thoughtcrime/securesms/util/Emoji.java b/src/org/thoughtcrime/securesms/util/Emoji.java index 90cbcce1eb..67a8d3b7c6 100644 --- a/src/org/thoughtcrime/securesms/util/Emoji.java +++ b/src/org/thoughtcrime/securesms/util/Emoji.java @@ -194,7 +194,7 @@ public class Emoji { PreferenceManager.getDefaultSharedPreferences(context) .edit() .putString(EMOJI_LRU_PREFERENCE, serialized) - .commit(); + .apply(); } } } diff --git a/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java b/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java index 18d9aa5525..1e8a0f7d90 100644 --- a/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java +++ b/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java @@ -4,6 +4,8 @@ import android.content.Context; import android.preference.PreferenceManager; import android.util.Log; +import java.io.IOException; + public class TextSecurePreferences { public static final String IDENTITY_PREF = "pref_choose_identity"; @@ -18,6 +20,7 @@ public class TextSecurePreferences { public static final String THREAD_TRIM_NOW = "pref_trim_now"; public static final String ENABLE_MANUAL_MMS_PREF = "pref_enable_manual_mms"; + private static final String LAST_VERSION_CODE_PREF = "last_version_code"; public static final String RINGTONE_PREF = "pref_key_ringtone"; private static final String VIBRATE_PREF = "pref_key_vibrate"; private static final String NOTIFICATION_PREF = "pref_key_enable_notifications"; @@ -166,6 +169,20 @@ public class TextSecurePreferences { return getBooleanPreference(context, ENABLE_MANUAL_MMS_PREF, false); } + public static void setUseLocalApnsEnabled(Context context, boolean useLocal) { + setBooleanPreference(context, ENABLE_MANUAL_MMS_PREF, useLocal); + } + + public static int getLastVersionCode(Context context) { + return getIntegerPreference(context, LAST_VERSION_CODE_PREF, 0); + } + + public static void setLastVersionCode(Context context, int versionCode) throws IOException { + if (!setIntegerPrefrenceBlocking(context, LAST_VERSION_CODE_PREF, versionCode)) { + throw new IOException("couldn't write version code to sharedpreferences"); + } + } + public static String getTheme(Context context) { return getStringPreference(context, THEME_PREF, "light"); } @@ -272,7 +289,7 @@ public class TextSecurePreferences { } private static void setBooleanPreference(Context context, String key, boolean value) { - PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean(key, value).commit(); + PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean(key, value).apply(); } private static boolean getBooleanPreference(Context context, String key, boolean defaultValue) { @@ -280,7 +297,7 @@ public class TextSecurePreferences { } public static void setStringPreference(Context context, String key, String value) { - PreferenceManager.getDefaultSharedPreferences(context).edit().putString(key, value).commit(); + PreferenceManager.getDefaultSharedPreferences(context).edit().putString(key, value).apply(); } private static String getStringPreference(Context context, String key, String defaultValue) { @@ -292,7 +309,11 @@ public class TextSecurePreferences { } private static void setIntegerPrefrence(Context context, String key, int value) { - PreferenceManager.getDefaultSharedPreferences(context).edit().putInt(key, value).commit(); + PreferenceManager.getDefaultSharedPreferences(context).edit().putInt(key, value).apply(); + } + + private static boolean setIntegerPrefrenceBlocking(Context context, String key, int value) { + return PreferenceManager.getDefaultSharedPreferences(context).edit().putInt(key, value).commit(); } private static long getLongPreference(Context context, String key, long defaultValue) { @@ -300,7 +321,7 @@ public class TextSecurePreferences { } private static void setLongPreference(Context context, String key, long value) { - PreferenceManager.getDefaultSharedPreferences(context).edit().putLong(key, value).commit(); + PreferenceManager.getDefaultSharedPreferences(context).edit().putLong(key, value).apply(); } diff --git a/src/org/thoughtcrime/securesms/util/VersionTracker.java b/src/org/thoughtcrime/securesms/util/VersionTracker.java index c6386f116b..584d777f85 100644 --- a/src/org/thoughtcrime/securesms/util/VersionTracker.java +++ b/src/org/thoughtcrime/securesms/util/VersionTracker.java @@ -1,28 +1,27 @@ package org.thoughtcrime.securesms.util; import android.content.Context; -import android.content.SharedPreferences; import android.content.pm.PackageManager; -import android.preference.PreferenceManager; + +import java.io.IOException; public class VersionTracker { - private static final String LAST_VERSION_CODE = "last_version_code"; public static int getLastSeenVersion(Context context) { - SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); - return preferences.getInt(LAST_VERSION_CODE, 0); + return TextSecurePreferences.getLastVersionCode(context); } public static void updateLastSeenVersion(Context context) { try { - SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); int currentVersionCode = context.getPackageManager() .getPackageInfo(context.getPackageName(), 0) .versionCode; - preferences.edit().putInt(LAST_VERSION_CODE, currentVersionCode).commit(); + TextSecurePreferences.setLastVersionCode(context, currentVersionCode); } catch (PackageManager.NameNotFoundException e) { throw new AssertionError(e); + } catch (IOException ioe) { + throw new AssertionError(ioe); } } }