refactor: simplify SharedPrefHelper class

Signed-off-by: oSumAtrIX <johan.melkonyan1@web.de>
This commit is contained in:
oSumAtrIX 2023-01-07 09:38:55 +01:00
parent 114a1c4004
commit 2e74fc55eb
No known key found for this signature in database
GPG Key ID: A9B3094ACDB604B4
13 changed files with 66 additions and 267 deletions

View File

@ -1,177 +0,0 @@
package app.revanced.integrations.patches;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import app.revanced.integrations.settings.SettingsEnum;
import app.revanced.integrations.utils.LogHelper;
import app.revanced.integrations.utils.ReVancedUtils;
public class VideoSpeedPatch {
public static final float[] videoSpeeds = { 0, 0 }; // Values are useless as they are being overridden by the respective patch
private static Boolean userChangedSpeed = false;
public static int getDefaultSpeed(Object[] speeds, int speed, Object qInterface) {
int speed2;
Exception e;
if (!ReVancedUtils.isNewVideoStarted()) {
return speed;
}
ReVancedUtils.setNewVideo(false);
LogHelper.printDebug(() -> "Speed: " + speed);
float preferredSpeed = SettingsEnum.PREFERRED_VIDEO_SPEED.getFloat();
LogHelper.printDebug(() -> "Preferred speed: " + preferredSpeed);
if (preferredSpeed == -2.0f) {
return speed;
}
Class<?> floatType = Float.TYPE;
ArrayList<Float> iStreamSpeeds = new ArrayList<>();
try {
for (Object streamSpeed : speeds) {
Field[] fields = streamSpeed.getClass().getFields();
for (Field field : fields) {
if (field.getType().isAssignableFrom(floatType)) {
float value = field.getFloat(streamSpeed);
if (field.getName().length() <= 2) {
iStreamSpeeds.add(value);
}
}
}
}
} catch (Exception ignored) {
}
Iterator<Float> it = iStreamSpeeds.iterator();
int index = 0;
while (it.hasNext()) {
float streamSpeed2 = it.next();
final int logIndex = index;
LogHelper.printDebug(() -> "Speed at index " + logIndex + ": " + streamSpeed2);
index++;
}
int speed3 = -1;
for (float streamSpeed3 : iStreamSpeeds) {
if (streamSpeed3 <= preferredSpeed) {
speed3++;
final int speed3ToLog = speed3;
LogHelper.printDebug(() -> "Speed loop at index " + speed3ToLog + ": " + streamSpeed3);
}
}
if (speed3 == -1) {
LogHelper.printDebug(() -> "Speed was not found");
speed2 = 3;
} else {
speed2 = speed3;
}
try {
Method[] declaredMethods = qInterface.getClass().getDeclaredMethods();
for (Method method : declaredMethods) {
if (method.getName().length() <= 2) {
LogHelper.printDebug(() -> "Method name: " + method.getName());
try {
try {
method.invoke(qInterface, videoSpeeds[speed2]);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ignored) {
} catch (final Exception e6) {
LogHelper.printException(() -> (e6.getMessage()));
return speed2;
}
} catch (Exception ignored) {
}
}
}
} catch (Exception e10) {
e = e10;
}
LogHelper.printDebug(() -> "Speed changed to: " + speed2);
return speed2;
}
public static void userChangedSpeed() {
userChangedSpeed = true;
}
public static float getSpeedValue(Object[] speeds, int speed) {
int i = 0;
if (!ReVancedUtils.isNewVideoStarted() || userChangedSpeed) {
if (SettingsEnum.DEBUG.getBoolean() && userChangedSpeed) {
LogHelper.printDebug(() -> "Skipping speed change because user changed it: " + speed);
}
userChangedSpeed = false;
return -1.0f;
}
ReVancedUtils.setNewVideo(false);
LogHelper.printDebug(() -> "Speed: " + speed);
float preferredSpeed = SettingsEnum.PREFERRED_VIDEO_SPEED.getFloat();
LogHelper.printDebug(() -> "Preferred speed: " + preferredSpeed);
if (preferredSpeed == -2.0f) {
return -1.0f;
}
Class<?> floatType = Float.TYPE;
ArrayList<Float> iStreamSpeeds = new ArrayList<>();
try {
int length = speeds.length;
int i2 = 0;
while (i2 < length) {
Object streamSpeed = speeds[i2];
Field[] fields = streamSpeed.getClass().getFields();
int length2 = fields.length;
while (i < length2) {
Field field = fields[i];
if (field.getType().isAssignableFrom(floatType)) {
float value = field.getFloat(streamSpeed);
if (field.getName().length() <= 2) {
iStreamSpeeds.add(value);
}
}
i++;
}
i2++;
i = 0;
}
} catch (Exception ignored) {
}
int index = 0;
for (Float iStreamSpeed : iStreamSpeeds) {
float streamSpeed2 = iStreamSpeed;
final int indexToLog = index;
LogHelper.printDebug(() -> "Speed at index " + indexToLog + ": " + streamSpeed2);
index++;
}
int newSpeedIndex = -1;
for (Float iStreamSpeed : iStreamSpeeds) {
float streamSpeed3 = iStreamSpeed;
if (streamSpeed3 <= preferredSpeed) {
newSpeedIndex++;
final int newSpeedIndexToLog = newSpeedIndex;
LogHelper.printDebug(() -> "Speed loop at index " + newSpeedIndexToLog + ": " + streamSpeed3);
}
}
if (newSpeedIndex == -1) {
LogHelper.printDebug(() -> "Speed was not found");
newSpeedIndex = 3;
}
if (newSpeedIndex == speed) {
final int newSpeedIndexToLog = newSpeedIndex;
LogHelper.printDebug(() -> "Trying to set speed to what it already is, skipping...: " + newSpeedIndexToLog);
return -1.0f;
}
final int newSpeedIndexToLog = newSpeedIndex;
LogHelper.printDebug(() -> "Speed changed to: " + newSpeedIndexToLog);
return getSpeedByIndex(newSpeedIndex);
}
private static float getSpeedByIndex(int index) {
if (index == -2) {
return 1.0f;
}
try {
return videoSpeeds[index];
} catch (Exception e) {
return 1.0f;
}
}
}

View File

@ -25,7 +25,7 @@ public class RememberVideoQualityPatch {
Context context = ReVancedUtils.getContext(); Context context = ReVancedUtils.getContext();
if (isConnectedWifi(context)) { if (isConnectedWifi(context)) {
try { try {
SharedPrefHelper.saveString(context, SharedPrefHelper.SharedPrefNames.REVANCED_PREFS, "wifi_quality", defaultQuality + ""); SharedPrefHelper.saveString(SharedPrefHelper.SharedPrefNames.REVANCED_PREFS, "wifi_quality", defaultQuality + "");
} catch (Exception ex) { } catch (Exception ex) {
LogHelper.printException(() -> ("Failed to change default WI-FI quality:" + ex)); LogHelper.printException(() -> ("Failed to change default WI-FI quality:" + ex));
Toast.makeText(context, "Failed to change default WI-FI quality:", Toast.LENGTH_SHORT).show(); Toast.makeText(context, "Failed to change default WI-FI quality:", Toast.LENGTH_SHORT).show();
@ -34,7 +34,7 @@ public class RememberVideoQualityPatch {
Toast.makeText(context, "Changing default Wi-Fi quality to: " + defaultQuality, Toast.LENGTH_SHORT).show(); Toast.makeText(context, "Changing default Wi-Fi quality to: " + defaultQuality, Toast.LENGTH_SHORT).show();
} else if (isConnectedMobile(context)) { } else if (isConnectedMobile(context)) {
try { try {
SharedPrefHelper.saveString(context, SharedPrefHelper.SharedPrefNames.REVANCED_PREFS, "mobile_quality", defaultQuality + ""); SharedPrefHelper.saveString(SharedPrefHelper.SharedPrefNames.REVANCED_PREFS, "mobile_quality", defaultQuality + "");
} catch (Exception ex) { } catch (Exception ex) {
LogHelper.printDebug(() -> "Failed to change default mobile data quality" + ex); LogHelper.printDebug(() -> "Failed to change default mobile data quality" + ex);
Toast.makeText(context, "Failed to change default mobile data quality", Toast.LENGTH_SHORT).show(); Toast.makeText(context, "Failed to change default mobile data quality", Toast.LENGTH_SHORT).show();
@ -92,10 +92,10 @@ public class RememberVideoQualityPatch {
return quality; return quality;
} }
if (isConnectedWifi(context)) { if (isConnectedWifi(context)) {
preferredQuality = SharedPrefHelper.getInt(context, SharedPrefHelper.SharedPrefNames.REVANCED_PREFS, "wifi_quality", -2); preferredQuality = SharedPrefHelper.getInt(SharedPrefHelper.SharedPrefNames.REVANCED_PREFS, "wifi_quality", -2);
LogHelper.printDebug(() -> "Wi-Fi connection detected, preferred quality: " + preferredQuality); LogHelper.printDebug(() -> "Wi-Fi connection detected, preferred quality: " + preferredQuality);
} else if (isConnectedMobile(context)) { } else if (isConnectedMobile(context)) {
preferredQuality = SharedPrefHelper.getInt(context, SharedPrefHelper.SharedPrefNames.REVANCED_PREFS, "mobile_quality", -2); preferredQuality = SharedPrefHelper.getInt(SharedPrefHelper.SharedPrefNames.REVANCED_PREFS, "mobile_quality", -2);
LogHelper.printDebug(() -> "Mobile data connection detected, preferred quality: " + preferredQuality); LogHelper.printDebug(() -> "Mobile data connection detected, preferred quality: " + preferredQuality);
} else { } else {
LogHelper.printDebug(() -> "No Internet connection!"); LogHelper.printDebug(() -> "No Internet connection!");
@ -137,7 +137,7 @@ public class RememberVideoQualityPatch {
public static void userChangedQuality(int selectedQuality) { public static void userChangedQuality(int selectedQuality) {
// Do not remember a **new** quality if REMEMBER_VIDEO_QUALITY is false // Do not remember a **new** quality if REMEMBER_VIDEO_QUALITY is false
if (SettingsEnum.REMEMBER_VIDEO_QUALITY_LAST_SELECTED.getBoolean() == false) return; if (!SettingsEnum.REMEMBER_VIDEO_QUALITY_LAST_SELECTED.getBoolean()) return;
selectedQuality1 = selectedQuality; selectedQuality1 = selectedQuality;
userChangedQuality = true; userChangedQuality = true;

View File

@ -1,5 +1,9 @@
package app.revanced.integrations.patches.playback.speed; package app.revanced.integrations.patches.playback.speed;
public class CustomVideoSpeedPatch { public class CustomVideoSpeedPatch {
public static final float[] videoSpeeds = { 0, 0 }; // Values are useless as they are being overridden by the respective patch // Values are useless as they are being overridden by the respective patch.
// This generates a .array segment in Dalvik bytecode
// which the patch utilizes to store the video speeds in, only
// if it has two or more default values.
public static final float[] videoSpeeds = { 0, 0 };
} }

View File

@ -187,8 +187,7 @@ public class ReturnYouTubeDislike {
if (!isEnabled) return; if (!isEnabled) return;
try { try {
Objects.requireNonNull(vote); Objects.requireNonNull(vote);
Context context = Objects.requireNonNull(ReVancedUtils.getContext()); if (SharedPrefHelper.getBoolean(SharedPrefHelper.SharedPrefNames.YOUTUBE, "user_signed_out", true)) {
if (SharedPrefHelper.getBoolean(context, SharedPrefHelper.SharedPrefNames.YOUTUBE, "user_signed_out", true)) {
return; return;
} }

View File

@ -22,8 +22,9 @@ public enum SettingsEnum {
// Video settings // Video settings
OLD_STYLE_VIDEO_QUALITY_PLAYER_SETTINGS("revanced_use_old_style_quality_settings", true, ReturnType.BOOLEAN), OLD_STYLE_VIDEO_QUALITY_PLAYER_SETTINGS("revanced_use_old_style_quality_settings", true, ReturnType.BOOLEAN),
PREFERRED_VIDEO_SPEED("revanced_pref_video_speed", -2.0f, ReturnType.FLOAT),
REMEMBER_VIDEO_QUALITY_LAST_SELECTED("revanced_remember_video_quality_last_selected", true, ReturnType.BOOLEAN), REMEMBER_VIDEO_QUALITY_LAST_SELECTED("revanced_remember_video_quality_last_selected", true, ReturnType.BOOLEAN),
REMEMBER_PLAYBACK_RATE_SELECTED("revanced_remember_playback_rate_selected", true, ReturnType.BOOLEAN),
// Whitelist settings // Whitelist settings
//ToDo: Not used atm, Patch missing //ToDo: Not used atm, Patch missing
@ -288,21 +289,23 @@ public enum SettingsEnum {
//LogHelper is not initialized here //LogHelper is not initialized here
Log.d("revanced: SettingsEnum", "Loading Setting: " + setting.name()); Log.d("revanced: SettingsEnum", "Loading Setting: " + setting.name());
var path = setting.getPath();
var defaultValue = setting.getDefaultValue();
switch (setting.getReturnType()) { switch (setting.getReturnType()) {
case FLOAT: case FLOAT:
value = SharedPrefHelper.getFloat(context, setting.sharedPref, setting.getPath(), (float) setting.getDefaultValue()); value = SharedPrefHelper.getFloat(setting.sharedPref, path, (float) defaultValue);
break; break;
case LONG: case LONG:
value = SharedPrefHelper.getLong(context, setting.sharedPref, setting.getPath(), (long) setting.getDefaultValue()); value = SharedPrefHelper.getLong(setting.sharedPref, path, (long) defaultValue);
break; break;
case BOOLEAN: case BOOLEAN:
value = SharedPrefHelper.getBoolean(context, setting.sharedPref, setting.getPath(), (boolean) setting.getDefaultValue()); value = SharedPrefHelper.getBoolean(setting.sharedPref, path, (boolean) defaultValue);
break; break;
case INTEGER: case INTEGER:
value = SharedPrefHelper.getInt(context, setting.sharedPref, setting.getPath(), (int) setting.getDefaultValue()); value = SharedPrefHelper.getInt(setting.sharedPref, path, (int) defaultValue);
break; break;
case STRING: case STRING:
value = SharedPrefHelper.getString(context, setting.sharedPref, setting.getPath(), (String) setting.getDefaultValue()); value = SharedPrefHelper.getString(setting.sharedPref, path, (String) defaultValue);
break; break;
default: default:
LogHelper.printException(() -> ("Setting does not have a valid Type. Name is: " + setting.name())); LogHelper.printException(() -> ("Setting does not have a valid Type. Name is: " + setting.name()));
@ -345,9 +348,9 @@ public enum SettingsEnum {
Context context = ReVancedUtils.getContext(); Context context = ReVancedUtils.getContext();
if (context != null) { if (context != null) {
if (returnType == ReturnType.BOOLEAN) { if (returnType == ReturnType.BOOLEAN) {
SharedPrefHelper.saveBoolean(context, sharedPref, path, (Boolean) newValue); SharedPrefHelper.saveBoolean(sharedPref, path, (Boolean) newValue);
} else { } else {
SharedPrefHelper.saveString(context, sharedPref, path, newValue + ""); SharedPrefHelper.saveString(sharedPref, path, newValue + "");
} }
value = newValue; value = newValue;
} else { } else {

View File

@ -70,20 +70,6 @@ public class ReVancedSettingsFragment extends PreferenceFragment {
break; break;
} }
setting.setValue(value); setting.setValue(value);
} else if (pref instanceof ListPreference) {
ListPreference listPref = (ListPreference) pref;
if (setting == SettingsEnum.PREFERRED_VIDEO_SPEED) {
try {
String value = sharedPreferences.getString(setting.getPath(), setting.getDefaultValue() + "");
listPref.setDefaultValue(value);
listPref.setSummary(videoSpeedEntries[listPref.findIndexOfValue(value)]);
SettingsEnum.PREFERRED_VIDEO_SPEED.saveValue(value);
} catch (Throwable th) {
LogHelper.printException(() -> ("Error setting value of speed" + th));
}
} else {
LogHelper.printException(() -> ("No valid setting found: " + setting.toString()));
}
} else { } else {
LogHelper.printException(() -> ("Setting cannot be handled! " + pref.toString())); LogHelper.printException(() -> ("Setting cannot be handled! " + pref.toString()));
} }

View File

@ -52,7 +52,7 @@ public class SponsorBlockSettingsFragment extends PreferenceFragment implements
PreferenceScreen preferenceScreen = getPreferenceManager().createPreferenceScreen(context); PreferenceScreen preferenceScreen = getPreferenceManager().createPreferenceScreen(context);
setPreferenceScreen(preferenceScreen); setPreferenceScreen(preferenceScreen);
SponsorBlockSettings.update(context); SponsorBlockSettings.update(getActivity());
{ {
SwitchPreference preference = new SwitchPreference(context); SwitchPreference preference = new SwitchPreference(context);
@ -349,7 +349,6 @@ public class SponsorBlockSettingsFragment extends PreferenceFragment implements
return; return;
Context context = ((AlertDialog) dialog).getContext(); Context context = ((AlertDialog) dialog).getContext();
Context applicationContext = context.getApplicationContext(); Context applicationContext = context.getApplicationContext();
SharedPreferences preferences = SharedPrefHelper.getPreferences(context, SharedPrefHelper.SharedPrefNames.SPONSOR_BLOCK);
switch (which) { switch (which) {
case DialogInterface.BUTTON_NEUTRAL: case DialogInterface.BUTTON_NEUTRAL:

View File

@ -56,8 +56,7 @@ public class PlayerController {
return; return;
} }
Context context = ReVancedUtils.getContext(); SponsorBlockSettings.update(null);
SponsorBlockSettings.update(context);
if (!SettingsEnum.SB_ENABLED.getBoolean()) { if (!SettingsEnum.SB_ENABLED.getBoolean()) {
currentVideoId = null; currentVideoId = null;

View File

@ -2,7 +2,7 @@ package app.revanced.integrations.sponsorblock;
import static app.revanced.integrations.sponsorblock.StringRef.sf; import static app.revanced.integrations.sponsorblock.StringRef.sf;
import android.content.Context; import android.app.Activity;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.Paint; import android.graphics.Paint;
@ -16,7 +16,6 @@ import java.util.Map;
import java.util.UUID; import java.util.UUID;
import app.revanced.integrations.settings.SettingsEnum; import app.revanced.integrations.settings.SettingsEnum;
import app.revanced.integrations.utils.LogHelper;
import app.revanced.integrations.utils.SharedPrefHelper; import app.revanced.integrations.utils.SharedPrefHelper;
public class SponsorBlockSettings { public class SponsorBlockSettings {
@ -25,10 +24,8 @@ public class SponsorBlockSettings {
public static final SegmentBehaviour DefaultBehaviour = SegmentBehaviour.IGNORE; public static final SegmentBehaviour DefaultBehaviour = SegmentBehaviour.IGNORE;
public static String sponsorBlockUrlCategories = "[]"; public static String sponsorBlockUrlCategories = "[]";
public static void update(Context context) { public static void update(Activity _activity) {
if (context == null) return; SharedPreferences preferences = SharedPrefHelper.getPreferences(SharedPrefHelper.SharedPrefNames.SPONSOR_BLOCK);
SharedPreferences preferences = SharedPrefHelper.getPreferences(context, SharedPrefHelper.SharedPrefNames.SPONSOR_BLOCK);
if (!SettingsEnum.SB_ENABLED.getBoolean()) { if (!SettingsEnum.SB_ENABLED.getBoolean()) {
SkipSegmentView.hide(); SkipSegmentView.hide();

View File

@ -514,7 +514,7 @@ public abstract class SponsorBlockUtils {
JSONArray categorySelectionsArray = settingsJson.getJSONArray("categorySelections"); JSONArray categorySelectionsArray = settingsJson.getJSONArray("categorySelections");
SharedPreferences.Editor editor = SharedPrefHelper.getPreferences(context, SharedPrefHelper.SharedPrefNames.SPONSOR_BLOCK).edit(); SharedPreferences.Editor editor = SharedPrefHelper.getPreferences(SharedPrefHelper.SharedPrefNames.SPONSOR_BLOCK).edit();
SponsorBlockSettings.SegmentInfo[] categories = SponsorBlockSettings.SegmentInfo.valuesWithoutUnsubmitted(); SponsorBlockSettings.SegmentInfo[] categories = SponsorBlockSettings.SegmentInfo.valuesWithoutUnsubmitted();
for (SponsorBlockSettings.SegmentInfo category : categories) { for (SponsorBlockSettings.SegmentInfo category : categories) {
@ -605,7 +605,7 @@ public abstract class SponsorBlockUtils {
} }
public static boolean isSBButtonEnabled(Context context, String key) { public static boolean isSBButtonEnabled(Context context, String key) {
return SettingsEnum.SB_ENABLED.getBoolean() && SharedPrefHelper.getBoolean(context, SharedPrefHelper.SharedPrefNames.SPONSOR_BLOCK, key, false); return SettingsEnum.SB_ENABLED.getBoolean() && SharedPrefHelper.getBoolean(SharedPrefHelper.SharedPrefNames.SPONSOR_BLOCK, key, false);
} }
public enum VoteOption { public enum VoteOption {

View File

@ -21,7 +21,7 @@ public class SwipeHelper {
try { try {
_frameLayout = (FrameLayout) obj; _frameLayout = (FrameLayout) obj;
Context appContext = ReVancedUtils.getContext(); Context appContext = ReVancedUtils.getContext();
if (ReVancedUtils.isTablet(appContext) || SharedPrefHelper.getBoolean(appContext, SharedPrefHelper.SharedPrefNames.YOUTUBE,"pref_swipe_tablet", false)) { if (ReVancedUtils.isTablet(appContext) || SharedPrefHelper.getBoolean(SharedPrefHelper.SharedPrefNames.YOUTUBE, "pref_swipe_tablet", false)) {
isTabletMode = true; isTabletMode = true;
} }
} catch (Exception e) { } catch (Exception e) {

View File

@ -5,12 +5,12 @@ import android.content.Context;
import app.revanced.integrations.utils.SharedPrefHelper; import app.revanced.integrations.utils.SharedPrefHelper;
public class ButtonVisibility { public class ButtonVisibility {
public static Visibility getButtonVisibility(Context context, String key) { public static Visibility getButtonVisibility(String key) {
return getButtonVisibility(context, key, SharedPrefHelper.SharedPrefNames.YOUTUBE); return getButtonVisibility(key, SharedPrefHelper.SharedPrefNames.YOUTUBE);
} }
public static Visibility getButtonVisibility(Context context, String key, SharedPrefHelper.SharedPrefNames name) { public static Visibility getButtonVisibility(String key, SharedPrefHelper.SharedPrefNames name) {
String value = SharedPrefHelper.getString(context, name, key, null); String value = SharedPrefHelper.getString(name, key, null);
if (value == null || value.isEmpty()) return Visibility.NONE; if (value == null || value.isEmpty()) return Visibility.NONE;
@ -26,12 +26,12 @@ public class ButtonVisibility {
} }
} }
public static boolean isVisibleInContainer(Context context, String key) { public static boolean isVisibleInContainer(String key) {
return isVisibleInContainer(getButtonVisibility(context, key)); return isVisibleInContainer(getButtonVisibility(key));
} }
public static boolean isVisibleInContainer(Context context, String key, SharedPrefHelper.SharedPrefNames name) { public static boolean isVisibleInContainer(String key, SharedPrefHelper.SharedPrefNames name) {
return isVisibleInContainer(getButtonVisibility(context, key, name)); return isVisibleInContainer(getButtonVisibility(key, name));
} }
public static boolean isVisibleInContainer(Visibility visibility) { public static boolean isVisibleInContainer(Visibility visibility) {

View File

@ -3,62 +3,51 @@ package app.revanced.integrations.utils;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import java.util.Objects;
public class SharedPrefHelper { public class SharedPrefHelper {
public static void saveString(Context context, SharedPrefNames prefName, String key, String value) { public static void saveString(SharedPrefNames prefName, String key, String value) {
SharedPreferences sharedPreferences = getPreferences(context, prefName); getPreferences(prefName).edit().putString(key, value).apply();
sharedPreferences.edit().putString(key, value).apply();
} }
public static void saveBoolean(Context context, SharedPrefNames prefName, String key, Boolean value) { public static void saveBoolean(SharedPrefNames prefName, String key, Boolean value) {
SharedPreferences sharedPreferences = getPreferences(context, prefName); getPreferences(prefName).edit().putBoolean(key, value).apply();
sharedPreferences.edit().putBoolean(key, value).apply();
} }
public static String getString(Context context, SharedPrefNames prefName, String key, String _default) { public static void saveFloat(SharedPrefNames prefName, String key, Float value) {
SharedPreferences sharedPreferences = getPreferences(context, prefName); getPreferences(prefName).edit().putFloat(key, value).apply();
return (sharedPreferences.getString(key, _default));
} }
public static Boolean getBoolean(Context context, SharedPrefNames prefName, String key, Boolean _default) { public static void saveInt(SharedPrefNames prefName, String key, Integer value) {
SharedPreferences sharedPreferences = getPreferences(context, prefName); getPreferences(prefName).edit().putInt(key, value).apply();
return (sharedPreferences.getBoolean(key, _default));
} }
public static Long getLong(Context context, SharedPrefNames prefName, String key, Long _default) { public static void saveLong(SharedPrefNames prefName, String key, Long value) {
SharedPreferences sharedPreferences = getPreferences(context, prefName); getPreferences(prefName).edit().putLong(key, value).apply();
try {
return Long.valueOf(sharedPreferences.getString(key, _default + ""));
} catch (ClassCastException ex) {
return sharedPreferences.getLong(key, _default);
}
} }
public static Float getFloat(Context context, SharedPrefNames prefName, String key, Float _default) { public static String getString(SharedPrefNames prefName, String key, String _default) {
SharedPreferences sharedPreferences = getPreferences(context, prefName); return getPreferences(prefName).getString(key, _default);
try {
return Float.valueOf(sharedPreferences.getString(key, _default + ""));
} catch (ClassCastException ex) {
return sharedPreferences.getFloat(key, _default);
}
} }
public static Integer getInt(Context context, SharedPrefNames prefName, String key, Integer _default) { public static Boolean getBoolean(SharedPrefNames prefName, String key, Boolean _default) {
SharedPreferences sharedPreferences = getPreferences(context, prefName); return getPreferences(prefName).getBoolean(key, _default);
try {
return Integer.valueOf(sharedPreferences.getString(key, _default + ""));
} catch (ClassCastException ex) {
return sharedPreferences.getInt(key, _default);
}
} }
public static SharedPreferences getPreferences(Context context, SharedPrefNames name) { public static Long getLong(SharedPrefNames prefName, String key, Long _default) {
if (context == null) return null; return getPreferences(prefName).getLong(key, _default);
return context.getSharedPreferences(name.getName(), Context.MODE_PRIVATE);
} }
public static SharedPreferences getPreferences(Context context, String name) { public static Float getFloat(SharedPrefNames prefName, String key, Float _default) {
if (context == null) return null; return getPreferences(prefName).getFloat(key, _default);
return context.getSharedPreferences(name, Context.MODE_PRIVATE); }
public static Integer getInt(SharedPrefNames prefName, String key, Integer _default) {
return getPreferences(prefName).getInt(key, _default);
}
public static SharedPreferences getPreferences(SharedPrefNames name) {
return Objects.requireNonNull(ReVancedUtils.getContext()).getSharedPreferences(name.getName(), Context.MODE_PRIVATE);
} }
public enum SharedPrefNames { public enum SharedPrefNames {