use apply for preferences instead of commit

// FREEBIE
This commit is contained in:
Jake McGinty 2014-05-05 13:48:26 -05:00 committed by Moxie Marlinspike
parent d8e6a93584
commit 34e147838a
10 changed files with 60 additions and 33 deletions

View File

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

View File

@ -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;
}

View File

@ -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");
}
}

View File

@ -200,24 +200,33 @@ public class MasterSecretUtil {
}
private static void save(Context context, String key, int value) {
context.getSharedPreferences(PREFERENCES_NAME, 0)
if (!context.getSharedPreferences(PREFERENCES_NAME, 0)
.edit()
.putInt(key, value)
.commit();
.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)
if (!context.getSharedPreferences(PREFERENCES_NAME, 0)
.edit()
.putString(key, Base64.encodeBytes(value))
.commit();
.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)
if (!context.getSharedPreferences(PREFERENCES_NAME, 0)
.edit()
.putBoolean(key, value)
.commit();
.commit())
{
throw new AssertionError("failed to save a shared pref in MasterSecretUtil");
}
}
private static byte[] retrieve(Context context, String key) throws IOException {

View File

@ -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();
}
}

View File

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

View File

@ -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();
}
}

View File

@ -194,7 +194,7 @@ public class Emoji {
PreferenceManager.getDefaultSharedPreferences(context)
.edit()
.putString(EMOJI_LRU_PREFERENCE, serialized)
.commit();
.apply();
}
}
}

View File

@ -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();
}

View File

@ -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);
}
}
}