diff --git a/app/src/main/java/org/thoughtcrime/securesms/crypto/MasterSecretUtil.java b/app/src/main/java/org/thoughtcrime/securesms/crypto/MasterSecretUtil.java index b003634ff16..73952301b6c 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/crypto/MasterSecretUtil.java +++ b/app/src/main/java/org/thoughtcrime/securesms/crypto/MasterSecretUtil.java @@ -65,6 +65,8 @@ public class MasterSecretUtil { private static final String ASYMMETRIC_LOCAL_PUBLIC_DJB = "asymmetric_master_secret_curve25519_public"; private static final String ASYMMETRIC_LOCAL_PRIVATE_DJB = "asymmetric_master_secret_curve25519_private"; + private static SharedPreferences preferences; + public static MasterSecret changeMasterSecretPassphrase(Context context, MasterSecret masterSecret, String newPassphrase) @@ -192,17 +194,17 @@ public static MasterSecret generateMasterSecret(Context context, String passphra } public static boolean hasAsymmericMasterSecret(Context context) { - SharedPreferences settings = context.getSharedPreferences(PREFERENCES_NAME, 0); + SharedPreferences settings = getSharedPreferences(context); return settings.contains(ASYMMETRIC_LOCAL_PUBLIC_DJB); } public static boolean isPassphraseInitialized(Context context) { - SharedPreferences preferences = context.getSharedPreferences(PREFERENCES_NAME, 0); + SharedPreferences preferences = getSharedPreferences(context); return preferences.getBoolean("passphrase_initialized", false); } private static void save(Context context, String key, int value) { - if (!context.getSharedPreferences(PREFERENCES_NAME, 0) + if (!getSharedPreferences(context) .edit() .putInt(key, value) .commit()) @@ -212,7 +214,7 @@ private static void save(Context context, String key, int value) { } private static void save(Context context, String key, byte[] value) { - if (!context.getSharedPreferences(PREFERENCES_NAME, 0) + if (!getSharedPreferences(context) .edit() .putString(key, Base64.encodeBytes(value)) .commit()) @@ -222,7 +224,7 @@ private static void save(Context context, String key, byte[] value) { } private static void save(Context context, String key, boolean value) { - if (!context.getSharedPreferences(PREFERENCES_NAME, 0) + if (!getSharedPreferences(context) .edit() .putBoolean(key, value) .commit()) @@ -232,7 +234,7 @@ private static void save(Context context, String key, boolean value) { } private static byte[] retrieve(Context context, String key) throws IOException { - SharedPreferences settings = context.getSharedPreferences(PREFERENCES_NAME, 0); + SharedPreferences settings = getSharedPreferences(context); String encodedValue = settings.getString(key, ""); if (TextUtils.isEmpty(encodedValue)) return null; @@ -240,7 +242,7 @@ private static byte[] retrieve(Context context, String key) throws IOException { } private static int retrieve(Context context, String key, int defaultValue) throws IOException { - SharedPreferences settings = context.getSharedPreferences(PREFERENCES_NAME, 0); + SharedPreferences settings = getSharedPreferences(context); return settings.getInt(key, defaultValue); } @@ -370,4 +372,11 @@ private static byte[] macWithPassphrase(byte[] macSalt, int iterations, byte[] d return result; } + + private static SharedPreferences getSharedPreferences(@NonNull Context context) { + if (preferences == null) { + preferences = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE); + } + return preferences; + } } diff --git a/app/src/main/java/org/thoughtcrime/securesms/util/TextSecurePreferences.java b/app/src/main/java/org/thoughtcrime/securesms/util/TextSecurePreferences.java index ce22fe885e2..da6cc7f6811 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/util/TextSecurePreferences.java +++ b/app/src/main/java/org/thoughtcrime/securesms/util/TextSecurePreferences.java @@ -5,13 +5,13 @@ import android.hardware.Camera.CameraInfo; import android.net.Uri; import android.os.Build; -import android.preference.PreferenceManager; import android.provider.Settings; import androidx.annotation.ArrayRes; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.core.app.NotificationCompat; +import androidx.preference.PreferenceManager; import org.greenrobot.eventbus.EventBus; import org.signal.core.util.logging.Log; @@ -220,8 +220,10 @@ public class TextSecurePreferences { MEDIA_DOWNLOAD_WIFI_PREF, MEDIA_DOWNLOAD_ROAMING_PREF}; + private static volatile SharedPreferences preferences = null; + public static long getPreferencesToSaveToBackupCount(@NonNull Context context) { - SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); + SharedPreferences preferences = getSharedPreferences(context); long count = 0; for (String booleanPreference : booleanPreferencesToBackup) { @@ -246,7 +248,7 @@ public static long getPreferencesToSaveToBackupCount(@NonNull Context context) { } public static List getPreferencesToSaveToBackup(@NonNull Context context) { - SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); + SharedPreferences preferences = getSharedPreferences(context); List backupProtos = new ArrayList<>(); String defaultFile = context.getPackageName() + "_preferences"; @@ -331,7 +333,7 @@ public static void setV1RegistrationLockEnabled(@NonNull Context context, boolea public static void clearRegistrationLockV1(@NonNull Context context) { //noinspection deprecation - PreferenceManager.getDefaultSharedPreferences(context) + getSharedPreferences(context) .edit() .remove(REGISTRATION_LOCK_PIN_PREF_V1) .apply(); @@ -1091,47 +1093,47 @@ public static void setArgon2Tested(Context context, boolean tested) { } public static void setBooleanPreference(Context context, String key, boolean value) { - PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean(key, value).apply(); + getSharedPreferences(context).edit().putBoolean(key, value).apply(); } public static boolean getBooleanPreference(Context context, String key, boolean defaultValue) { - return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(key, defaultValue); + return getSharedPreferences(context).getBoolean(key, defaultValue); } public static void setStringPreference(Context context, String key, String value) { - PreferenceManager.getDefaultSharedPreferences(context).edit().putString(key, value).apply(); + getSharedPreferences(context).edit().putString(key, value).apply(); } public static String getStringPreference(Context context, String key, String defaultValue) { - return PreferenceManager.getDefaultSharedPreferences(context).getString(key, defaultValue); + return getSharedPreferences(context).getString(key, defaultValue); } public static int getIntegerPreference(Context context, String key, int defaultValue) { - return PreferenceManager.getDefaultSharedPreferences(context).getInt(key, defaultValue); + return getSharedPreferences(context).getInt(key, defaultValue); } private static void setIntegerPrefrence(Context context, String key, int value) { - PreferenceManager.getDefaultSharedPreferences(context).edit().putInt(key, value).apply(); + getSharedPreferences(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(); + return getSharedPreferences(context).edit().putInt(key, value).commit(); } public static long getLongPreference(Context context, String key, long defaultValue) { - return PreferenceManager.getDefaultSharedPreferences(context).getLong(key, defaultValue); + return getSharedPreferences(context).getLong(key, defaultValue); } private static void setLongPreference(Context context, String key, long value) { - PreferenceManager.getDefaultSharedPreferences(context).edit().putLong(key, value).apply(); + getSharedPreferences(context).edit().putLong(key, value).apply(); } private static void removePreference(Context context, String key) { - PreferenceManager.getDefaultSharedPreferences(context).edit().remove(key).apply(); + getSharedPreferences(context).edit().remove(key).apply(); } private static Set getStringSetPreference(Context context, String key, Set defaultValues) { - final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + final SharedPreferences prefs = getSharedPreferences(context); if (prefs.contains(key)) { return prefs.getStringSet(key, Collections.emptySet()); } else { @@ -1148,6 +1150,13 @@ private static void clearLocalCredentials(Context context) { ApplicationDependencies.getGroupsV2Authorization().clear(); } + private static SharedPreferences getSharedPreferences(Context context) { + if (preferences == null) { + preferences = PreferenceManager.getDefaultSharedPreferences(context); + } + return preferences; + } + // NEVER rename these -- they're persisted by name public enum MediaKeyboardMode { EMOJI, STICKER, GIF