Reduce disk hits when accessing shared preferences.

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.
This commit is contained in:
Cody Henthorne
2022-07-14 13:16:47 -04:00
parent 819f7a170f
commit 9c914ab715
2 changed files with 40 additions and 22 deletions

View File

@@ -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 class MasterSecretUtil {
}
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 @@ public class MasterSecretUtil {
}
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 @@ public class MasterSecretUtil {
}
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 @@ public class MasterSecretUtil {
}
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 @@ public class MasterSecretUtil {
}
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 @@ public class MasterSecretUtil {
return result;
}
private static SharedPreferences getSharedPreferences(@NonNull Context context) {
if (preferences == null) {
preferences = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
}
return preferences;
}
}

View File

@@ -5,13 +5,13 @@ import android.content.SharedPreferences;
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 class TextSecurePreferences {
}
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";
@@ -331,7 +333,7 @@ public class TextSecurePreferences {
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 class TextSecurePreferences {
}
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 {
@@ -1148,6 +1150,13 @@ public class TextSecurePreferences {
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