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();
if (isConnectedWifi(context)) {
try {
SharedPrefHelper.saveString(context, SharedPrefHelper.SharedPrefNames.REVANCED_PREFS, "wifi_quality", defaultQuality + "");
SharedPrefHelper.saveString(SharedPrefHelper.SharedPrefNames.REVANCED_PREFS, "wifi_quality", defaultQuality + "");
} catch (Exception 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();
@ -34,7 +34,7 @@ public class RememberVideoQualityPatch {
Toast.makeText(context, "Changing default Wi-Fi quality to: " + defaultQuality, Toast.LENGTH_SHORT).show();
} else if (isConnectedMobile(context)) {
try {
SharedPrefHelper.saveString(context, SharedPrefHelper.SharedPrefNames.REVANCED_PREFS, "mobile_quality", defaultQuality + "");
SharedPrefHelper.saveString(SharedPrefHelper.SharedPrefNames.REVANCED_PREFS, "mobile_quality", defaultQuality + "");
} catch (Exception 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();
@ -92,10 +92,10 @@ public class RememberVideoQualityPatch {
return quality;
}
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);
} 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);
} else {
LogHelper.printDebug(() -> "No Internet connection!");
@ -137,7 +137,7 @@ public class RememberVideoQualityPatch {
public static void userChangedQuality(int selectedQuality) {
// 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;
userChangedQuality = true;

View File

@ -1,5 +1,9 @@
package app.revanced.integrations.patches.playback.speed;
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;
try {
Objects.requireNonNull(vote);
Context context = Objects.requireNonNull(ReVancedUtils.getContext());
if (SharedPrefHelper.getBoolean(context, SharedPrefHelper.SharedPrefNames.YOUTUBE, "user_signed_out", true)) {
if (SharedPrefHelper.getBoolean(SharedPrefHelper.SharedPrefNames.YOUTUBE, "user_signed_out", true)) {
return;
}

View File

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

View File

@ -70,20 +70,6 @@ public class ReVancedSettingsFragment extends PreferenceFragment {
break;
}
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 {
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);
setPreferenceScreen(preferenceScreen);
SponsorBlockSettings.update(context);
SponsorBlockSettings.update(getActivity());
{
SwitchPreference preference = new SwitchPreference(context);
@ -349,7 +349,6 @@ public class SponsorBlockSettingsFragment extends PreferenceFragment implements
return;
Context context = ((AlertDialog) dialog).getContext();
Context applicationContext = context.getApplicationContext();
SharedPreferences preferences = SharedPrefHelper.getPreferences(context, SharedPrefHelper.SharedPrefNames.SPONSOR_BLOCK);
switch (which) {
case DialogInterface.BUTTON_NEUTRAL:

View File

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

View File

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

View File

@ -514,7 +514,7 @@ public abstract class SponsorBlockUtils {
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();
for (SponsorBlockSettings.SegmentInfo category : categories) {
@ -605,7 +605,7 @@ public abstract class SponsorBlockUtils {
}
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 {

View File

@ -21,7 +21,7 @@ public class SwipeHelper {
try {
_frameLayout = (FrameLayout) obj;
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;
}
} catch (Exception e) {

View File

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

View File

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