From 2128c2d7d67cf6ffafcdcd321d4f87bb445ae150 Mon Sep 17 00:00:00 2001 From: caneleex Date: Mon, 17 Jan 2022 23:38:25 +0100 Subject: [PATCH] add sb whitelisting (TODO: fix checks) --- .../vanced/libraries/youtube/ui/AdButton.java | 7 +++--- .../youtube/ui/SBWhitelistButton.java | 14 ++++++----- .../youtube/whitelisting/Whitelist.java | 23 +++++++++++++------ .../youtube/whitelisting/WhitelistType.java | 12 +++++++--- .../requests/WhitelistRequester.java | 11 ++++++--- .../java/pl/jakubweg/PlayerController.java | 7 ++++++ .../SponsorBlockPreferenceFragment.java | 10 ++++++++ .../pl/jakubweg/requests/SBRequester.java | 2 -- app/src/main/res/values/strings.xml | 6 +++++ 9 files changed, 68 insertions(+), 24 deletions(-) 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 3d536fa0..f184a1d3 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 @@ -8,6 +8,7 @@ import android.content.Context; import android.util.Log; import android.view.View; import android.view.ViewGroup; +import android.widget.ImageView; import fi.vanced.libraries.youtube.player.VideoInformation; import fi.vanced.libraries.youtube.whitelisting.Whitelist; @@ -48,7 +49,7 @@ public class AdButton extends SlimButton { } //this.button_icon.setEnabled(!this.button_icon.isEnabled()); - addToWhiteList(); + addToWhiteList(this.view, this.button_icon); } private void removeFromWhitelist() { @@ -64,12 +65,12 @@ public class AdButton extends SlimButton { this.view.setEnabled(true); } - private void addToWhiteList() { + private void addToWhiteList(View view, ImageView buttonIcon) { new Thread(() -> { if (debug) { Log.d(TAG, "Fetching channelId for " + currentVideoId); } - WhitelistRequester.addChannelToWhitelist(WhitelistType.ADS, this.view, this.button_icon, this.context); + WhitelistRequester.addChannelToWhitelist(WhitelistType.ADS, view, buttonIcon, this.context); }).start(); } } 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 22f08c6a..9d7f2e42 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 @@ -7,6 +7,7 @@ import android.content.Context; import android.util.Log; import android.view.View; import android.view.ViewGroup; +import android.widget.ImageView; import fi.vanced.libraries.youtube.player.VideoInformation; import fi.vanced.libraries.youtube.whitelisting.Whitelist; @@ -14,13 +15,14 @@ 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, "youtube", WhitelistType.SPONSORBLOCK.getPreferenceEnabledName(), false)); + SharedPrefUtils.getBoolean(context, SponsorBlockSettings.PREFERENCES_NAME, WhitelistType.SPONSORBLOCK.getPreferenceEnabledName(), false)); initialize(); } @@ -32,9 +34,9 @@ public class SBWhitelistButton extends SlimButton { } public void changeEnabled(boolean enabled) { - if (debug) { + //if (debug) { Log.d(TAG, "changeEnabled " + enabled); - } + //} this.button_icon.setEnabled(enabled); } @@ -47,7 +49,7 @@ public class SBWhitelistButton extends SlimButton { } //this.button_icon.setEnabled(!this.button_icon.isEnabled()); - addToWhiteList(); + addToWhiteList(this.view, this.button_icon); } private void removeFromWhitelist() { @@ -63,12 +65,12 @@ public class SBWhitelistButton extends SlimButton { this.view.setEnabled(true); } - private void addToWhiteList() { + private void addToWhiteList(View view, ImageView buttonIcon) { new Thread(() -> { if (debug) { Log.d(TAG, "Fetching channelId for " + currentVideoId); } - WhitelistRequester.addChannelToWhitelist(WhitelistType.SPONSORBLOCK, this.view, this.button_icon, this.context); + WhitelistRequester.addChannelToWhitelist(WhitelistType.SPONSORBLOCK, view, buttonIcon, this.context); }).start(); } } 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 e6d0bf66..553b8040 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 @@ -5,17 +5,19 @@ import static fi.vanced.libraries.youtube.player.VideoInformation.channelName; import static fi.vanced.libraries.youtube.ui.SlimButtonContainer.adBlockButton; import static fi.vanced.libraries.youtube.ui.SlimButtonContainer.sbWhitelistButton; import static fi.vanced.utils.VancedUtils.getPreferences; +import static pl.jakubweg.StringRef.str; import android.content.Context; import android.content.SharedPreferences; import android.util.Log; +import android.widget.Toast; import com.google.android.apps.youtube.app.YouTubeTikTokRoot_Application; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; -import java.util.HashMap; +import java.util.EnumMap; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -64,7 +66,7 @@ public class Whitelist { return Collections.emptyMap(); } WhitelistType[] whitelistTypes = WhitelistType.values(); - Map> whitelistMap = new HashMap<>(whitelistTypes.length); + Map> whitelistMap = new EnumMap<>(WhitelistType.class); for (WhitelistType whitelistType : whitelistTypes) { SharedPreferences preferences = VancedUtils.getPreferences(context, whitelistType.getPreferencesName()); @@ -73,6 +75,7 @@ public class Whitelist { if (debug) { Log.d(TAG, String.format("channels string was null for %s whitelisting", whitelistType)); } + whitelistMap.put(whitelistType, new ArrayList<>()); continue; } try { @@ -93,9 +96,8 @@ public class Whitelist { } private static Map parseEnabledMap(Context context) { - WhitelistType[] whitelistTypes = WhitelistType.values(); - Map enabledMap = new HashMap<>(whitelistTypes.length); - for (WhitelistType whitelistType : whitelistTypes) { + Map enabledMap = new EnumMap<>(WhitelistType.class); + for (WhitelistType whitelistType : WhitelistType.values()) { enabledMap.put(whitelistType, SharedPrefUtils.getBoolean(context, "youtube", whitelistType.getPreferenceEnabledName())); } return enabledMap; @@ -140,7 +142,7 @@ public class Whitelist { return updateWhitelist(whitelistType, whitelisted, context); } - public static boolean removeFromWhitelist(WhitelistType whitelistType, Context context, String channelName) { + public static void removeFromWhitelist(WhitelistType whitelistType, Context context, String channelName) { ArrayList channels = whitelistMap.get(whitelistType); Iterator iterator = channels.iterator(); while (iterator.hasNext()) { @@ -150,7 +152,14 @@ public class Whitelist { break; } } - return updateWhitelist(whitelistType, channels, context); + boolean success = updateWhitelist(whitelistType, channels, context); + String friendlyName = whitelistType.getFriendlyName(); + if (success) { + Toast.makeText(context, str("vanced_whitelisting_removed", channelName, friendlyName), Toast.LENGTH_SHORT).show(); + } + else { + Toast.makeText(context, str("vanced_whitelisting_remove_failed", channelName, friendlyName), Toast.LENGTH_SHORT).show(); + } } private static boolean updateWhitelist(WhitelistType whitelistType, ArrayList channels, Context context) { 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 bdeec3a8..0e9a7e56 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,17 +1,23 @@ package fi.vanced.libraries.youtube.whitelisting; public enum WhitelistType { - ADS("vanced_whitelist_ads_enabled"), - SPONSORBLOCK("vanced_whitelist_sb_enabled"); + ADS("Ads", "vanced_whitelist_ads_enabled"), + SPONSORBLOCK("SponsorBlock", "vanced_whitelist_sb_enabled"); + private final String friendlyName; private final String preferencesName; private final String preferenceEnabledName; - WhitelistType(String preferenceEnabledName) { + WhitelistType(String friendlyName, String preferenceEnabledName) { + this.friendlyName = friendlyName; this.preferencesName = "whitelist_" + name(); this.preferenceEnabledName = preferenceEnabledName; } + public String getFriendlyName() { + return friendlyName; + } + 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 978127ba..534d7402 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 @@ -3,6 +3,7 @@ package fi.vanced.libraries.youtube.whitelisting.requests; import static fi.razerman.youtube.XGlobals.debug; import static fi.vanced.libraries.youtube.player.VideoInformation.currentVideoId; import static fi.vanced.libraries.youtube.ui.AdButton.TAG; +import static pl.jakubweg.StringRef.str; import android.content.Context; import android.os.Handler; @@ -29,6 +30,8 @@ public class WhitelistRequester { private static final String YT_API_URL = "https://www.youtube.com/youtubei/v1/"; private static final String YT_API_KEY = "replaceMeWithTheYouTubeAPIKey"; + private WhitelistRequester() {} + public static void addChannelToWhitelist(WhitelistType whitelistType, View view, ImageView buttonIcon, Context context) { try { HttpURLConnection connection = getConnectionFromRoute(WhitelistRoutes.GET_CHANNEL_DETAILS, YT_API_KEY); @@ -48,19 +51,21 @@ public class WhitelistRequester { JSONObject json = getJSONObject(connection); JSONObject videoInfo = json.getJSONObject("videoDetails"); ChannelModel channelModel = new ChannelModel(videoInfo.getString("author"), videoInfo.getString("channelId")); + String author = channelModel.getAuthor(); if (debug) { - Log.d(TAG, "channelId " + channelModel.getChannelId() + " fetched for author " + channelModel.getAuthor()); + Log.d(TAG, "channelId " + channelModel.getChannelId() + " fetched for author " + author); } boolean success = Whitelist.addToWhitelist(whitelistType, context, channelModel); + String whitelistTypeName = whitelistType.getFriendlyName(); new Handler(Looper.getMainLooper()).post(() -> { if (success) { buttonIcon.setEnabled(true); - Toast.makeText(context, "Channel " + channelModel.getAuthor() + " whitelisted", Toast.LENGTH_SHORT).show(); + Toast.makeText(context, str("vanced_whitelisting_added", author, whitelistTypeName), Toast.LENGTH_SHORT).show(); } else { buttonIcon.setEnabled(false); - Toast.makeText(context, "Channel " + channelModel.getAuthor() + " failed to whitelist", Toast.LENGTH_SHORT).show(); + Toast.makeText(context, str("vanced_whitelisting_add_failed", author, whitelistTypeName), Toast.LENGTH_SHORT).show(); } view.setEnabled(true); diff --git a/app/src/main/java/pl/jakubweg/PlayerController.java b/app/src/main/java/pl/jakubweg/PlayerController.java index d618351a..edbd27c8 100644 --- a/app/src/main/java/pl/jakubweg/PlayerController.java +++ b/app/src/main/java/pl/jakubweg/PlayerController.java @@ -2,6 +2,8 @@ package pl.jakubweg; import static pl.jakubweg.SponsorBlockSettings.skippedSegments; import static pl.jakubweg.SponsorBlockSettings.skippedTime; +import static pl.jakubweg.SponsorBlockUtils.timeWithoutSegments; +import static pl.jakubweg.SponsorBlockUtils.videoHasSegments; import android.annotation.SuppressLint; import android.app.Activity; @@ -25,6 +27,7 @@ import java.util.Timer; import java.util.TimerTask; import fi.vanced.libraries.youtube.player.VideoInformation; +import fi.vanced.libraries.youtube.whitelisting.Whitelist; import pl.jakubweg.objects.SponsorSegment; import pl.jakubweg.requests.SBRequester; @@ -123,6 +126,10 @@ public class PlayerController { } public static void executeDownloadSegments(String videoId) { + videoHasSegments = false; + timeWithoutSegments = ""; + if (!Whitelist.shouldShowSegments()) + return; SponsorSegment[] segments = SBRequester.getSegments(videoId); Arrays.sort(segments); diff --git a/app/src/main/java/pl/jakubweg/SponsorBlockPreferenceFragment.java b/app/src/main/java/pl/jakubweg/SponsorBlockPreferenceFragment.java index 1662dc27..4af3e7a3 100644 --- a/app/src/main/java/pl/jakubweg/SponsorBlockPreferenceFragment.java +++ b/app/src/main/java/pl/jakubweg/SponsorBlockPreferenceFragment.java @@ -38,6 +38,7 @@ import android.widget.Toast; import java.text.DecimalFormat; import java.util.ArrayList; +import fi.vanced.libraries.youtube.whitelisting.WhitelistType; import pl.jakubweg.requests.SBRequester; @SuppressWarnings({"unused", "deprecation"}) // injected @@ -278,6 +279,15 @@ public class SponsorBlockPreferenceFragment extends PreferenceFragment implement screen.addPreference(preference); } + { + Preference preference = new SwitchPreference(context); + preference.setTitle(str("general_whitelisting")); + preference.setSummary(str("general_whitelisting_sum")); + preference.setKey(WhitelistType.SPONSORBLOCK.getPreferenceEnabledName()); + preferencesToDisableWhenSBDisabled.add(preference); + screen.addPreference(preference); + } + { EditTextPreference preference = new EditTextPreference(context); preference.getEditText().setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED); diff --git a/app/src/main/java/pl/jakubweg/requests/SBRequester.java b/app/src/main/java/pl/jakubweg/requests/SBRequester.java index 08850b1c..e1cb1922 100644 --- a/app/src/main/java/pl/jakubweg/requests/SBRequester.java +++ b/app/src/main/java/pl/jakubweg/requests/SBRequester.java @@ -39,8 +39,6 @@ public class SBRequester { try { HttpURLConnection connection = getConnectionFromRoute(SBRoutes.GET_SEGMENTS, videoId, SponsorBlockSettings.sponsorBlockUrlCategories); int responseCode = connection.getResponseCode(); - videoHasSegments = false; - timeWithoutSegments = ""; if (responseCode == 200) { JSONArray responseArray = Requester.getJSONArray(connection); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e19765aa..7c91628e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -311,6 +311,8 @@ Show time without segments This time appears in brackets next to the current time. This shows the total video duration minus any segments. + Channel whitelisting + Use the whitelist button under the player to whitelist a channel Preview/Recap Quick recap of previous episodes, or a preview of what\'s coming up later in the current video. Meant for edited together clips, not for spoken summaries. Stats @@ -342,6 +344,10 @@ Video ad whitelisting Video ad whitelisting is turned off Video ad whitelisting is turned on. Use the ad button under the player to whitelist a channel + Channel %s was added to the %s whitelist + 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 Hidden In player Under player