Skip to content

Commit

Permalink
fix: inconsistencies in preference switches (ReVanced#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
LisoUseInAIKyrios authored Dec 6, 2022
1 parent b93b03d commit d64a0f9
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package app.revanced.integrations.patches;

import app.revanced.integrations.adremover.AdRemoverAPI;
import app.revanced.integrations.settings.SettingsEnum;

public class BrandingWaterMarkPatch {

//Used by: app.revanced.patches.youtube.layout.watermark.patch.HideWatermarkPatch
// Used by: app.revanced.patches.youtube.layout.watermark.patch.HideWatermarkPatch
public static boolean isBrandingWatermarkShown() {
return SettingsEnum.BRANDING_SHOWN.getBoolean();
return SettingsEnum.HIDE_VIDEO_WATERMARK.getBoolean() == false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
public class FullscreenPanelsRemoverPatch {

public static int getFullscreenPanelsVisibility() {
return SettingsEnum.FULLSCREEN_PANELS_SHOWN.getBoolean() ? View.VISIBLE : View.GONE;
return SettingsEnum.HIDE_FULLSCREEN_PANELS.getBoolean() ? View.GONE : View.VISIBLE;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
public class HideAutoplayButtonPatch {

public static boolean isButtonShown() {
return SettingsEnum.AUTOPLAY_BUTTON_SHOWN.getBoolean();
return SettingsEnum.HIDE_AUTOPLAY_BUTTON.getBoolean() == false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

public class HideCastButtonPatch {

//Used by app.revanced.patches.youtube.layout.castbutton.patch.HideCastButonPatch
// Used by app.revanced.patches.youtube.layout.castbutton.patch.HideCastButonPatch
public static int getCastButtonOverrideV2(int original) {
return SettingsEnum.CAST_BUTTON_SHOWN.getBoolean() ? original : View.GONE;
return SettingsEnum.HIDE_CAST_BUTTON.getBoolean() ? View.GONE : original;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ public class HideCreateButtonPatch {

//Used by app.revanced.patches.youtube.layout.createbutton.patch.CreateButtonRemoverPatch
public static void hideCreateButton(View view) {
boolean enabled = SettingsEnum.CREATE_BUTTON_ENABLED.getBoolean();
String message = "Create button: " + (enabled ? "shown" : "hidden");
boolean hidden = SettingsEnum.HIDE_CREATE_BUTTON.getBoolean();
String message = "Create button: " + (hidden ? "hidden" : "shown");
LogHelper.printDebug(() -> message);
view.setVisibility(enabled ? View.VISIBLE : View.GONE);
view.setVisibility(hidden ? View.GONE : View.VISIBLE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class HideReelsPatch {
* @param view
*/
public static void HideReel(View view) {
if (!SettingsEnum.REEL_BUTTON_SHOWN.getBoolean()) {
if (SettingsEnum.HIDE_REEL_BUTTON.getBoolean()) {
AdRemoverAPI.HideViewWithLayout1dp(view);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@

public class HideShortsButtonPatch {

//Used by app.revanced.patches.youtube.layout.shorts.button.patch.ShortsButtonRemoverPatch
// Used by app.revanced.patches.youtube.layout.shorts.button.patch.ShortsButtonRemoverPatch
public static void hideShortsButton(View view) {
if (lastPivotTab != null && lastPivotTab.name() == "TAB_SHORTS") {
boolean show = SettingsEnum.SHORTS_BUTTON_SHOWN.getBoolean();
String message = show ? "Shorts button: shown" : "Shorts button: hidden";
boolean hide = SettingsEnum.HIDE_SHORTS_BUTTON.getBoolean();
String message = hide ? "Shorts button: hidden" : "Shorts button: shown";
LogHelper.printDebug(() -> message);
if (!show) {
view.setVisibility(show ? View.VISIBLE : View.GONE);
if (hide) {
view.setVisibility(hide ? View.GONE : View.VISIBLE);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public class OldQualityLayoutPatch {
public static void showOldQualityMenu(ListView listView)
{
if (!SettingsEnum.OLD_STYLE_QUALITY_SETTINGS.getBoolean()) return;
if (!SettingsEnum.OLD_STYLE_VIDEO_QUALITY_PLAYER_SETTINGS.getBoolean()) return;

listView.setOnHierarchyChangeListener(new ViewGroup.OnHierarchyChangeListener() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ public static int setVideoQuality(Object[] qualities, int quality, Object qInter
}

public static void userChangedQuality(int selectedQuality) {
// Do not remember a **new** quality if REMEMBER_VIDEO_QUALITY is true
if (SettingsEnum.REMEMBER_VIDEO_QUALITY.getBoolean()) return;
// Do not remember a **new** quality if REMEMBER_VIDEO_QUALITY is false
if (SettingsEnum.REMEMBER_VIDEO_QUALITY_LAST_SELECTED.getBoolean() == false) return;

selectedQuality1 = selectedQuality;
userChangedQuality = true;
Expand Down
182 changes: 142 additions & 40 deletions app/src/main/java/app/revanced/integrations/settings/SettingsEnum.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public enum SettingsEnum {
DOWNLOADS_PACKAGE_NAME("revanced_downloads_package_name", "org.schabi.newpipe" /* NewPipe */, ReturnType.STRING),

// Video settings
OLD_STYLE_QUALITY_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("revanced_remember_video_quality_selection", false, ReturnType.BOOLEAN),
REMEMBER_VIDEO_QUALITY_LAST_SELECTED("revanced_remember_video_quality_last_selected", true, ReturnType.BOOLEAN),

// Whitelist settings
//ToDo: Not used atm, Patch missing
Expand Down Expand Up @@ -50,37 +50,37 @@ public enum SettingsEnum {
ADREMOVER_GRAY_SEPARATOR("revanced_adremover_separator", true, ReturnType.BOOLEAN),

// Action buttons
HIDE_LIKE_BUTTON("revanced_like_button", false, ReturnType.BOOLEAN, false),
HIDE_DISLIKE_BUTTON("revanced_dislike_button", false, ReturnType.BOOLEAN, false),
HIDE_DOWNLOAD_BUTTON("revanced_download_button", false, ReturnType.BOOLEAN, false),
HIDE_PLAYLIST_BUTTON("revanced_playlist_button", false, ReturnType.BOOLEAN, false),
HIDE_ACTION_BUTTON("revanced_action_button", false, ReturnType.BOOLEAN, false),
HIDE_SHARE_BUTTON("revanced_share_button", false, ReturnType.BOOLEAN, false),
HIDE_LIKE_BUTTON("revanced_hide_like_button", false, ReturnType.BOOLEAN, false),
HIDE_DISLIKE_BUTTON("revanced_hide_dislike_button", false, ReturnType.BOOLEAN, false),
HIDE_DOWNLOAD_BUTTON("revanced_hide_download_button", false, ReturnType.BOOLEAN, false),
HIDE_PLAYLIST_BUTTON("revanced_hide_playlist_button", false, ReturnType.BOOLEAN, false),
HIDE_ACTION_BUTTON("revanced_hide_action_button", false, ReturnType.BOOLEAN, false),
HIDE_SHARE_BUTTON("revanced_hide_share_button", false, ReturnType.BOOLEAN, false),

// Layout settings
REEL_BUTTON_SHOWN("revanced_reel_button_enabled", false, ReturnType.BOOLEAN, true),
HIDE_INFO_CARDS("revanced_hide_infocards", true, ReturnType.BOOLEAN),
BRANDING_SHOWN("revanced_branding_watermark_enabled", false, ReturnType.BOOLEAN),
HIDE_ENDSCREEN_CARDS("revanced_hide_endscreen_cards", true, ReturnType.BOOLEAN),
CAST_BUTTON_SHOWN("revanced_cast_button_enabled", false, ReturnType.BOOLEAN, true),
AUTOPLAY_BUTTON_SHOWN("revanced_autoplay_button_enabled", false, ReturnType.BOOLEAN, true),
DISABLE_STARTUP_SHORTS_PLAYER("revanced_startup_shorts_player_enabled", false, ReturnType.BOOLEAN),
PLAYER_POPUP_PANELS("revanced_player_popup_panels_enabled", false, ReturnType.BOOLEAN),
USE_TABLET_MINIPLAYER("revanced_tablet_miniplayer", false, ReturnType.BOOLEAN, true),
CREATE_BUTTON_ENABLED("revanced_create_button_enabled", false, ReturnType.BOOLEAN, true),
WIDE_SEARCHBAR("revanced_wide_searchbar", false, ReturnType.BOOLEAN, true),
SHORTS_BUTTON_SHOWN("revanced_shorts_button_enabled", false, ReturnType.BOOLEAN, true),
FULLSCREEN_PANELS_SHOWN("revanced_fullscreen_panels_enabled", false, ReturnType.BOOLEAN), //ToDo: Add to prefs
PLAYER_POPUP_PANELS("revanced_player_popup_panels_enabled", false, ReturnType.BOOLEAN),
HIDE_TIME_AND_SEEKBAR("revanced_hide_time_and_seekbar", false, ReturnType.BOOLEAN),
HIDE_EMAIL_ADDRESS("revanced_hide_email_address", false, ReturnType.BOOLEAN),
DISABLE_STARTUP_SHORTS_PLAYER("revanced_startup_shorts_player_enabled", false, ReturnType.BOOLEAN),
HIDE_ALBUM_CARDS("revanced_hide_album_cards", false, ReturnType.BOOLEAN, true),
HIDE_ARTIST_CARD("revanced_hide_artist_card", false, ReturnType.BOOLEAN),
HIDE_AUTOPLAY_BUTTON("revanced_hide_autoplay_button", true, ReturnType.BOOLEAN, true),
HIDE_VIDEO_WATERMARK("revanced_hide_video_watermark", true, ReturnType.BOOLEAN),
HIDE_CAPTIONS_BUTTON("revanced_hide_captions_button", false, ReturnType.BOOLEAN),
HIDE_MIX_PLAYLISTS("revanced_mix_playlists_hidden", false, ReturnType.BOOLEAN, true),
HIDE_CROWDFUNDING_BOX("revanced_hide_crowdfunding_box", false, ReturnType.BOOLEAN, true),
HIDE_CAST_BUTTON("revanced_hide_cast_button", true, ReturnType.BOOLEAN, true),
HIDE_COMMENTS_SECTION("revanced_hide_comments_section", false, ReturnType.BOOLEAN, true),
HIDE_SHORTS_COMMENTS_BUTTON("revanced_hide_shorts_comments_button", false, ReturnType.BOOLEAN),
HIDE_CREATE_BUTTON("revanced_hide_create_button", true, ReturnType.BOOLEAN, true),
HIDE_CROWDFUNDING_BOX("revanced_hide_crowdfunding_box", false, ReturnType.BOOLEAN, true),
HIDE_EMAIL_ADDRESS("revanced_hide_email_address", false, ReturnType.BOOLEAN),
HIDE_ENDSCREEN_CARDS("revanced_hide_endscreen_cards", true, ReturnType.BOOLEAN),
HIDE_FULLSCREEN_PANELS("revanced_hide_fullscreen_panels", true, ReturnType.BOOLEAN), //ToDo: Add to prefs
HIDE_INFO_CARDS("revanced_hide_infocards", true, ReturnType.BOOLEAN),
HIDE_MIX_PLAYLISTS("revanced_hide_mix_playlists", false, ReturnType.BOOLEAN, true),
HIDE_PREVIEW_COMMENT("revanced_hide_preview_comment", false, ReturnType.BOOLEAN, true),
HIDE_ALBUM_CARDS("revanced_hide_album_cards", false, ReturnType.BOOLEAN, true),
HIDE_ARTIST_CARD("revanced_hide_artist_card", false, ReturnType.BOOLEAN),
HIDE_REEL_BUTTON("revanced_hide_reel_button", true, ReturnType.BOOLEAN, true),
HIDE_SHORTS_BUTTON("revanced_hide_shorts_button", true, ReturnType.BOOLEAN, true),
HIDE_SHORTS_COMMENTS_BUTTON("revanced_hide_shorts_comments_button", false, ReturnType.BOOLEAN),
HIDE_TIME_AND_SEEKBAR("revanced_hide_time_and_seekbar", false, ReturnType.BOOLEAN),
HIDE_WATCH_IN_VR("revanced_hide_watch_in_vr", false, ReturnType.BOOLEAN, true),

// Misc. Settings
Expand Down Expand Up @@ -135,44 +135,138 @@ public enum SettingsEnum {
SB_IS_VIP("sb-is-vip", false, SharedPrefHelper.SharedPrefNames.SPONSOR_BLOCK, ReturnType.BOOLEAN),
SB_LAST_VIP_CHECK("sb-last-vip-check", 0L, SharedPrefHelper.SharedPrefNames.SPONSOR_BLOCK, ReturnType.LONG),
SB_SHOW_BROWSER_BUTTON("sb-browser-button", false, SharedPrefHelper.SharedPrefNames.SPONSOR_BLOCK, ReturnType.BOOLEAN),
SB_API_URL("sb-api-url", "https://sponsor.ajay.app/api/", SharedPrefHelper.SharedPrefNames.SPONSOR_BLOCK, ReturnType.STRING);
SB_API_URL("sb-api-url", "https://sponsor.ajay.app/api/", SharedPrefHelper.SharedPrefNames.SPONSOR_BLOCK, ReturnType.STRING),


//
// old deprecated settings, kept around to migrate user settings on existing installations
// FIXME: after a few months, eventually delete these settings
//
@Deprecated
DEPRECATED_HIDE_MIX_PLAYLISTS("revanced_mix_playlists_hidden", false, ReturnType.BOOLEAN, true),
@Deprecated
DEPRECATED_HIDE_LIKE_BUTTON("revanced_like_button", false, ReturnType.BOOLEAN, false),
@Deprecated
DEPRECATED_HIDE_DISLIKE_BUTTON("revanced_dislike_button", false, ReturnType.BOOLEAN, false),
@Deprecated
DEPRECATED_HIDE_DOWNLOAD_BUTTON("revanced_download_button", false, ReturnType.BOOLEAN, false),
@Deprecated
DEPRECATED_HIDE_PLAYLIST_BUTTON("revanced_playlist_button", false, ReturnType.BOOLEAN, false),
@Deprecated
DEPRECATED_HIDE_ACTION_BUTTON("revanced_action_button", false, ReturnType.BOOLEAN, false),
@Deprecated
DEPRECATED_HIDE_SHARE_BUTTON("revanced_share_button", false, ReturnType.BOOLEAN, false),
@Deprecated
DEPRECATED_FULLSCREEN_PANELS_SHOWN("revanced_fullscreen_panels_enabled", false, ReturnType.BOOLEAN),
@Deprecated
DEPRECATED_CREATE_BUTTON_ENABLED("revanced_create_button_enabled", false, ReturnType.BOOLEAN, true),
@Deprecated
DEPRECATED_SHORTS_BUTTON_SHOWN("revanced_shorts_button_enabled", false, ReturnType.BOOLEAN, true),
@Deprecated
DEPRECATED_REEL_BUTTON_SHOWN("revanced_reel_button_enabled", false, ReturnType.BOOLEAN, true),
@Deprecated
DEPRECATED_AUTOPLAY_BUTTON_SHOWN("revanced_autoplay_button_enabled", false, ReturnType.BOOLEAN, true),
@Deprecated
DEPRECATED_CAST_BUTTON_SHOWN("revanced_cast_button_enabled", false, ReturnType.BOOLEAN, true),
@Deprecated
DEPRECATED_BRANDING_SHOWN("revanced_branding_watermark_enabled", false, ReturnType.BOOLEAN),
@Deprecated
DEPRECATED_REMEMBER_VIDEO_QUALITY("revanced_remember_video_quality_selection", false, ReturnType.BOOLEAN);
//
// end deprecated settings
//

private final String path;
private final Object defaultValue;
private final SharedPrefHelper.SharedPrefNames sharedPref;
private final ReturnType returnType;
private final boolean rebootApp;

// must be volatile, as some settings are changed from non-main threads
// must be volatile, as some settings are read/write from different threads
// of note, the object value is persistently stored using SharedPreferences (which is thread safe)
private volatile Object value;

SettingsEnum(String path, Object defaultValue, ReturnType returnType) {
this.path = path;
this.defaultValue = defaultValue;
this.sharedPref = SharedPrefHelper.SharedPrefNames.YOUTUBE;
this.returnType = returnType;
this.rebootApp = false;
this(path, defaultValue, SharedPrefHelper.SharedPrefNames.YOUTUBE, returnType, false);
}

SettingsEnum(String path, Object defaultValue, ReturnType returnType, boolean rebootApp) {
this(path, defaultValue, SharedPrefHelper.SharedPrefNames.YOUTUBE, returnType, rebootApp);
}

SettingsEnum(String path, Object defaultValue, SharedPrefHelper.SharedPrefNames prefName, ReturnType returnType) {
this.path = path;
this.defaultValue = defaultValue;
this.sharedPref = prefName;
this.returnType = returnType;
this.rebootApp = false;
this(path, defaultValue, prefName, returnType, false);
}

SettingsEnum(String path, Object defaultValue, ReturnType returnType, Boolean rebootApp) {
SettingsEnum(String path, Object defaultValue, SharedPrefHelper.SharedPrefNames prefName, ReturnType returnType, boolean rebootApp) {
this.path = path;
this.defaultValue = defaultValue;
this.sharedPref = SharedPrefHelper.SharedPrefNames.YOUTUBE;
this.sharedPref = prefName;
this.returnType = returnType;
this.rebootApp = rebootApp;
}

static {
load();

//
// temporary code to migrate user configuration of old settings into current settings
// FIXME: eventually delete this code
//

// old/new settings where old is default off, and new has inverted value and is default on
SettingsEnum invertedSettingsToMigrate[][] = {
{DEPRECATED_FULLSCREEN_PANELS_SHOWN, HIDE_FULLSCREEN_PANELS},
{DEPRECATED_CREATE_BUTTON_ENABLED, HIDE_CREATE_BUTTON},
{DEPRECATED_SHORTS_BUTTON_SHOWN, HIDE_SHORTS_BUTTON},
{DEPRECATED_REEL_BUTTON_SHOWN, HIDE_REEL_BUTTON},
{DEPRECATED_AUTOPLAY_BUTTON_SHOWN, HIDE_AUTOPLAY_BUTTON},
{DEPRECATED_CAST_BUTTON_SHOWN, HIDE_CAST_BUTTON},
{DEPRECATED_BRANDING_SHOWN, HIDE_VIDEO_WATERMARK},
{DEPRECATED_REMEMBER_VIDEO_QUALITY, REMEMBER_VIDEO_QUALITY_LAST_SELECTED},
};
for (SettingsEnum oldNewSetting[] : invertedSettingsToMigrate) {
// by default, old setting was default off
// migrate to new setting of default on
SettingsEnum oldSetting = oldNewSetting[0];
SettingsEnum newSetting = oldNewSetting[1];

// only need to check if old setting was turned on
if (oldSetting.getBoolean()) {
// this code will only run once
LogHelper.printInfo(() -> "Migrating setting: " + oldSetting + " of 'true' to new setting: "
+ newSetting + " of 'false'");
newSetting.saveValue(false); // set opposite of old value
oldSetting.saveValue(false); // clear old value
}
}

//
// migrate preference of prior 'default off' settings, into replacement setting with different path name but otherwise is identical
//
SettingsEnum renamedSettings[][] = {
{DEPRECATED_HIDE_MIX_PLAYLISTS, HIDE_MIX_PLAYLISTS},
{DEPRECATED_HIDE_LIKE_BUTTON, HIDE_LIKE_BUTTON},
{DEPRECATED_HIDE_DISLIKE_BUTTON, HIDE_DISLIKE_BUTTON},
{DEPRECATED_HIDE_DOWNLOAD_BUTTON, HIDE_DOWNLOAD_BUTTON},
{DEPRECATED_HIDE_PLAYLIST_BUTTON, HIDE_PLAYLIST_BUTTON},
{DEPRECATED_HIDE_ACTION_BUTTON, HIDE_ACTION_BUTTON},
{DEPRECATED_HIDE_SHARE_BUTTON, HIDE_SHARE_BUTTON},
};
for (SettingsEnum oldNewSetting[] : invertedSettingsToMigrate) {
SettingsEnum oldSetting = oldNewSetting[0];
SettingsEnum newSetting = oldNewSetting[1];

if (oldSetting.getBoolean()) {
LogHelper.printInfo(() -> "Migrating enabled setting from: " + oldSetting
+ " into replacement setting: " + newSetting);
newSetting.saveValue(true);
oldSetting.saveValue(false);
}
}
//
// end temporary code
//
}

private static void load() {
Expand Down Expand Up @@ -228,10 +322,18 @@ public static List<SettingsEnum> getAdRemovalSettings() {
return list;
}

/**
* Sets, but does _not_ persistently save the value.
*
* @see #saveValue(Object)
*/
public void setValue(Object newValue) {
this.value = newValue;
}

/**
* Sets the value, and persistently saves it
*/
public void saveValue(Object newValue) {
Context context = ReVancedUtils.getContext();
if (context != null) {
Expand Down

0 comments on commit d64a0f9

Please sign in to comment.