diff --git a/app/src/main/java/fi/vanced/libraries/youtube/ryd/ReturnYouTubeDislikes.java b/app/src/main/java/fi/vanced/libraries/youtube/ryd/ReturnYouTubeDislikes.java index e03c3b6..d8f2a1e 100644 --- a/app/src/main/java/fi/vanced/libraries/youtube/ryd/ReturnYouTubeDislikes.java +++ b/app/src/main/java/fi/vanced/libraries/youtube/ryd/ReturnYouTubeDislikes.java @@ -13,7 +13,6 @@ import android.os.Build; import android.util.Log; import android.view.View; import android.widget.TextView; -import android.widget.Toast; import com.google.android.apps.youtube.app.YouTubeTikTokRoot_Application; @@ -240,8 +239,6 @@ public class ReturnYouTubeDislikes { Log.d(TAG, "Like button " + likeActive + " | Dislike button " + dislikeActive); } - Toast.makeText(YouTubeTikTokRoot_Application.getAppContext(), "Voting value: " + votingValue, Toast.LENGTH_SHORT).show(); - sendVote(votingValue); } catch (Exception ex) { diff --git a/app/src/main/java/fi/vanced/libraries/youtube/ui/AdButton.java b/app/src/main/java/fi/vanced/libraries/youtube/ui/AdButton.java index f184a1d..1e3a5e9 100644 --- a/app/src/main/java/fi/vanced/libraries/youtube/ui/AdButton.java +++ b/app/src/main/java/fi/vanced/libraries/youtube/ui/AdButton.java @@ -22,7 +22,7 @@ public class AdButton extends SlimButton { public AdButton(Context context, ViewGroup container) { super(context, container, SlimButton.SLIM_METADATA_BUTTON_ID, - SharedPrefUtils.getBoolean(context, "youtube", WhitelistType.ADS.getPreferenceEnabledName(), false)); + SharedPrefUtils.getBoolean(context, WhitelistType.ADS.getSharedPreferencesName(), WhitelistType.ADS.getPreferenceEnabledName(), false)); initialize(); } @@ -55,7 +55,7 @@ public class AdButton extends SlimButton { private void removeFromWhitelist() { try { Whitelist.removeFromWhitelist(WhitelistType.ADS, this.context, VideoInformation.channelName); - this.button_icon.setEnabled(false); + changeEnabled(false); } catch (Exception ex) { Log.e(TAG, "Failed to remove from whitelist", ex); diff --git a/app/src/main/java/fi/vanced/libraries/youtube/ui/SBWhitelistButton.java b/app/src/main/java/fi/vanced/libraries/youtube/ui/SBWhitelistButton.java index 19a25f5..c759ee6 100644 --- a/app/src/main/java/fi/vanced/libraries/youtube/ui/SBWhitelistButton.java +++ b/app/src/main/java/fi/vanced/libraries/youtube/ui/SBWhitelistButton.java @@ -2,6 +2,7 @@ package fi.vanced.libraries.youtube.ui; import static fi.razerman.youtube.XGlobals.debug; import static fi.vanced.libraries.youtube.player.VideoInformation.currentVideoId; +import static pl.jakubweg.StringRef.str; import android.content.Context; import android.util.Log; @@ -15,35 +16,34 @@ import fi.vanced.libraries.youtube.whitelisting.WhitelistType; import fi.vanced.libraries.youtube.whitelisting.requests.WhitelistRequester; import fi.vanced.utils.SharedPrefUtils; import fi.vanced.utils.VancedUtils; -import pl.jakubweg.SponsorBlockSettings; public class SBWhitelistButton extends SlimButton { public static final String TAG = "VI - SBWhitelistButton"; public SBWhitelistButton(Context context, ViewGroup container) { super(context, container, SlimButton.SLIM_METADATA_BUTTON_ID, - SharedPrefUtils.getBoolean(context, SponsorBlockSettings.PREFERENCES_NAME, WhitelistType.SPONSORBLOCK.getPreferenceEnabledName(), false)); + SharedPrefUtils.getBoolean(context, WhitelistType.SPONSORBLOCK.getSharedPreferencesName(), WhitelistType.SPONSORBLOCK.getPreferenceEnabledName(), false)); initialize(); } private void initialize() { - this.button_icon.setImageResource(VancedUtils.getIdentifier("vanced_sb_logo", "drawable")); - this.button_text.setText("SB"); - changeEnabled(Whitelist.shouldShowSegments()); + this.button_icon.setImageResource(VancedUtils.getIdentifier("vanced_yt_sb_button", "drawable")); + this.button_text.setText(str("action_segments")); + changeEnabled(Whitelist.isChannelSBWhitelisted()); } public void changeEnabled(boolean enabled) { if (debug) { Log.d(TAG, "changeEnabled " + enabled); } - this.button_icon.setEnabled(enabled); + this.button_icon.setEnabled(!enabled); // enabled == true -> strikethrough (no segments), enabled == false -> clear (segments) } @Override public void onClick(View view) { this.view.setEnabled(false); - if (this.button_icon.isEnabled()) { + if (Whitelist.isChannelSBWhitelisted()) { removeFromWhitelist(); return; } @@ -55,7 +55,7 @@ public class SBWhitelistButton extends SlimButton { private void removeFromWhitelist() { try { Whitelist.removeFromWhitelist(WhitelistType.SPONSORBLOCK, this.context, VideoInformation.channelName); - this.button_icon.setEnabled(false); + changeEnabled(false); } catch (Exception ex) { Log.e(TAG, "Failed to remove from whitelist", ex); diff --git a/app/src/main/java/fi/vanced/libraries/youtube/ui/SlimButtonContainer.java b/app/src/main/java/fi/vanced/libraries/youtube/ui/SlimButtonContainer.java index bfceb36..12ae3e8 100644 --- a/app/src/main/java/fi/vanced/libraries/youtube/ui/SlimButtonContainer.java +++ b/app/src/main/java/fi/vanced/libraries/youtube/ui/SlimButtonContainer.java @@ -78,7 +78,7 @@ public class SlimButtonContainer extends SlimMetadataScrollableButtonContainerLa WhitelistType whitelistAds = WhitelistType.ADS; String adsEnabledPreferenceName = whitelistAds.getPreferenceEnabledName(); if (adsEnabledPreferenceName.equals(key) && adBlockButton != null) { - boolean enabled = SharedPrefUtils.getBoolean(context, "youtube", adsEnabledPreferenceName, false); + boolean enabled = SharedPrefUtils.getBoolean(context, whitelistAds.getSharedPreferencesName(), adsEnabledPreferenceName, false); Whitelist.setEnabled(whitelistAds, enabled); adBlockButton.setVisible(enabled); return; @@ -86,7 +86,7 @@ public class SlimButtonContainer extends SlimMetadataScrollableButtonContainerLa WhitelistType whitelistSB = WhitelistType.SPONSORBLOCK; String sbEnabledPreferenceName = whitelistSB.getPreferenceEnabledName(); if (sbEnabledPreferenceName.equals(key) && sbWhitelistButton != null) { - boolean enabled = SharedPrefUtils.getBoolean(context, "youtube", sbEnabledPreferenceName, false); + boolean enabled = SharedPrefUtils.getBoolean(context, whitelistSB.getSharedPreferencesName(), sbEnabledPreferenceName, false); Whitelist.setEnabled(whitelistSB, enabled); sbWhitelistButton.setVisible(enabled); return; @@ -97,7 +97,9 @@ public class SlimButtonContainer extends SlimMetadataScrollableButtonContainerLa } }; - context.getSharedPreferences("youtube", Context.MODE_PRIVATE) + context.getSharedPreferences(WhitelistType.ADS.getSharedPreferencesName(), Context.MODE_PRIVATE) + .registerOnSharedPreferenceChangeListener(listener); + context.getSharedPreferences(WhitelistType.SPONSORBLOCK.getSharedPreferencesName(), Context.MODE_PRIVATE) .registerOnSharedPreferenceChangeListener(listener); } } diff --git a/app/src/main/java/fi/vanced/libraries/youtube/whitelisting/Whitelist.java b/app/src/main/java/fi/vanced/libraries/youtube/whitelisting/Whitelist.java index 553b804..21a9ab0 100644 --- a/app/src/main/java/fi/vanced/libraries/youtube/whitelisting/Whitelist.java +++ b/app/src/main/java/fi/vanced/libraries/youtube/whitelisting/Whitelist.java @@ -51,14 +51,14 @@ public class Whitelist { adBlockButton.changeEnabled(shouldShowAds()); } if (enabledMap.get(WhitelistType.SPONSORBLOCK) && sbWhitelistButton != null) { - sbWhitelistButton.changeEnabled(shouldShowSegments()); + sbWhitelistButton.changeEnabled(isChannelSBWhitelisted()); } } // the rest - public static boolean shouldShowSegments() { - return !isWhitelisted(WhitelistType.SPONSORBLOCK); + public static boolean isChannelSBWhitelisted() { + return isWhitelisted(WhitelistType.SPONSORBLOCK); } private static Map> parseWhitelist(Context context) { @@ -98,7 +98,7 @@ public class Whitelist { private static Map parseEnabledMap(Context context) { Map enabledMap = new EnumMap<>(WhitelistType.class); for (WhitelistType whitelistType : WhitelistType.values()) { - enabledMap.put(whitelistType, SharedPrefUtils.getBoolean(context, "youtube", whitelistType.getPreferenceEnabledName())); + enabledMap.put(whitelistType, SharedPrefUtils.getBoolean(context, whitelistType.getSharedPreferencesName(), whitelistType.getPreferenceEnabledName())); } return enabledMap; } diff --git a/app/src/main/java/fi/vanced/libraries/youtube/whitelisting/WhitelistType.java b/app/src/main/java/fi/vanced/libraries/youtube/whitelisting/WhitelistType.java index 0e9a7e5..261470a 100644 --- a/app/src/main/java/fi/vanced/libraries/youtube/whitelisting/WhitelistType.java +++ b/app/src/main/java/fi/vanced/libraries/youtube/whitelisting/WhitelistType.java @@ -1,15 +1,19 @@ package fi.vanced.libraries.youtube.whitelisting; +import pl.jakubweg.SponsorBlockSettings; + public enum WhitelistType { - ADS("Ads", "vanced_whitelist_ads_enabled"), - SPONSORBLOCK("SponsorBlock", "vanced_whitelist_sb_enabled"); + ADS("Ads", "youtube", "vanced_whitelist_ads_enabled"), + SPONSORBLOCK("SponsorBlock", SponsorBlockSettings.PREFERENCES_NAME, "vanced_whitelist_sb_enabled"); private final String friendlyName; private final String preferencesName; + private final String sharedPreferencesName; private final String preferenceEnabledName; - WhitelistType(String friendlyName, String preferenceEnabledName) { + WhitelistType(String friendlyName, String sharedPreferencesName, String preferenceEnabledName) { this.friendlyName = friendlyName; + this.sharedPreferencesName = sharedPreferencesName; this.preferencesName = "whitelist_" + name(); this.preferenceEnabledName = preferenceEnabledName; } @@ -18,6 +22,10 @@ public enum WhitelistType { return friendlyName; } + public String getSharedPreferencesName() { + return sharedPreferencesName; + } + public String getPreferencesName() { return preferencesName; } diff --git a/app/src/main/java/fi/vanced/libraries/youtube/whitelisting/requests/WhitelistRequester.java b/app/src/main/java/fi/vanced/libraries/youtube/whitelisting/requests/WhitelistRequester.java index 534d740..45fd8d1 100644 --- a/app/src/main/java/fi/vanced/libraries/youtube/whitelisting/requests/WhitelistRequester.java +++ b/app/src/main/java/fi/vanced/libraries/youtube/whitelisting/requests/WhitelistRequester.java @@ -47,7 +47,8 @@ public class WhitelistRequester { byte[] input = jsonInputString.getBytes(StandardCharsets.UTF_8); os.write(input, 0, input.length); } - if (connection.getResponseCode() == 200) { + int responseCode = connection.getResponseCode(); + if (responseCode == 200) { JSONObject json = getJSONObject(connection); JSONObject videoInfo = json.getJSONObject("videoDetails"); ChannelModel channelModel = new ChannelModel(videoInfo.getString("author"), videoInfo.getString("channelId")); @@ -58,36 +59,41 @@ public class WhitelistRequester { boolean success = Whitelist.addToWhitelist(whitelistType, context, channelModel); String whitelistTypeName = whitelistType.getFriendlyName(); - new Handler(Looper.getMainLooper()).post(() -> { + runOnMainThread(() -> { if (success) { - buttonIcon.setEnabled(true); + buttonIcon.setEnabled(whitelistType != WhitelistType.SPONSORBLOCK); Toast.makeText(context, str("vanced_whitelisting_added", author, whitelistTypeName), Toast.LENGTH_SHORT).show(); } else { - buttonIcon.setEnabled(false); + buttonIcon.setEnabled(whitelistType == WhitelistType.SPONSORBLOCK); Toast.makeText(context, str("vanced_whitelisting_add_failed", author, whitelistTypeName), Toast.LENGTH_SHORT).show(); } - view.setEnabled(true); }); } else { if (debug) { - Log.d(TAG, "player fetch response was " + connection.getResponseCode()); + Log.d(TAG, "player fetch response was " + responseCode); } - - buttonIcon.setEnabled(false); - view.setEnabled(true); + runOnMainThread(() -> { + Toast.makeText(context, str("vanced_whitelisting_fetch_failed", responseCode), Toast.LENGTH_SHORT).show(); + buttonIcon.setEnabled(true); + view.setEnabled(true); + }); } } catch (Exception ex) { Log.e(TAG, "Failed to fetch channelId", ex); - view.setEnabled(true); + runOnMainThread(() -> view.setEnabled(true)); } } // helpers + private static void runOnMainThread(Runnable runnable) { + new Handler(Looper.getMainLooper()).post(runnable); + } + private static HttpURLConnection getConnectionFromRoute(Route route, String... params) throws IOException { return Requester.getConnectionFromRoute(YT_API_URL, route, params); } diff --git a/app/src/main/java/pl/jakubweg/PlayerController.java b/app/src/main/java/pl/jakubweg/PlayerController.java index edbd27c..256278a 100644 --- a/app/src/main/java/pl/jakubweg/PlayerController.java +++ b/app/src/main/java/pl/jakubweg/PlayerController.java @@ -128,7 +128,7 @@ public class PlayerController { public static void executeDownloadSegments(String videoId) { videoHasSegments = false; timeWithoutSegments = ""; - if (!Whitelist.shouldShowSegments()) + if (Whitelist.isChannelSBWhitelisted()) return; SponsorSegment[] segments = SBRequester.getSegments(videoId); Arrays.sort(segments); diff --git a/app/src/main/res/drawable/vanced_yt_sb.xml b/app/src/main/res/drawable/vanced_yt_sb.xml new file mode 100644 index 0000000..ba07674 --- /dev/null +++ b/app/src/main/res/drawable/vanced_yt_sb.xml @@ -0,0 +1,14 @@ + + + + diff --git a/app/src/main/res/drawable/vanced_yt_sb_blocked.xml b/app/src/main/res/drawable/vanced_yt_sb_blocked.xml new file mode 100644 index 0000000..2a98161 --- /dev/null +++ b/app/src/main/res/drawable/vanced_yt_sb_blocked.xml @@ -0,0 +1,14 @@ + + + + diff --git a/app/src/main/res/drawable/vanced_yt_sb_button.xml b/app/src/main/res/drawable/vanced_yt_sb_button.xml new file mode 100644 index 0000000..6bba818 --- /dev/null +++ b/app/src/main/res/drawable/vanced_yt_sb_button.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7c91628..aa8b082 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -339,6 +339,7 @@ Copy TCopy Ads + Segments Video ad settings Video ad whitelisting @@ -348,6 +349,7 @@ Channel %s was removed from the %s whitelist Failed to add channel %s to the %s whitelist Failed to remove channel %s from the %s whitelist + Failed to retrieve channel details, received code %d Hidden In player Under player