Skip to content

Commit

Permalink
Reduce disk hits when accessing shared preferences.
Browse files Browse the repository at this point in the history
While the same instance of SharedPreferences is returned each time, in
order to get it, the system has to do a file check each time it's with
a new context. We can safely cache the instance instead of paying that
file check each time and pay it only once.
  • Loading branch information
cody-signal committed Jul 21, 2022
1 parent 819f7a1 commit 9c914ab
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 22 deletions.
Expand Up @@ -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)
Expand Down Expand Up @@ -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())
Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -232,15 +234,15 @@ 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;
else return Base64.decode(encodedValue);
}

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

Expand Down Expand Up @@ -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;
}
}
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -246,7 +248,7 @@ public static long getPreferencesToSaveToBackupCount(@NonNull Context context) {
}

public static List<BackupProtos.SharedPreference> getPreferencesToSaveToBackup(@NonNull Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences preferences = getSharedPreferences(context);
List<BackupProtos.SharedPreference> backupProtos = new ArrayList<>();
String defaultFile = context.getPackageName() + "_preferences";

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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<String> getStringSetPreference(Context context, String key, Set<String> defaultValues) {
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
final SharedPreferences prefs = getSharedPreferences(context);
if (prefs.contains(key)) {
return prefs.getStringSet(key, Collections.<String>emptySet());
} else {
Expand All @@ -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
Expand Down

0 comments on commit 9c914ab

Please sign in to comment.