1
mirror of https://github.com/revanced/revanced-integrations synced 2024-11-19 13:57:14 +01:00

add sb whitelisting (TODO: fix checks)

This commit is contained in:
caneleex 2022-01-17 23:38:25 +01:00
parent f53b526957
commit 2128c2d7d6
9 changed files with 68 additions and 24 deletions

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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<WhitelistType, ArrayList<ChannelModel>> whitelistMap = new HashMap<>(whitelistTypes.length);
Map<WhitelistType, ArrayList<ChannelModel>> 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<WhitelistType, Boolean> parseEnabledMap(Context context) {
WhitelistType[] whitelistTypes = WhitelistType.values();
Map<WhitelistType, Boolean> enabledMap = new HashMap<>(whitelistTypes.length);
for (WhitelistType whitelistType : whitelistTypes) {
Map<WhitelistType, Boolean> 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<ChannelModel> channels = whitelistMap.get(whitelistType);
Iterator<ChannelModel> 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<ChannelModel> channels, Context context) {

View File

@ -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;
}

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -311,6 +311,8 @@
<string name="general_time_without_sb">Show time without segments</string>
<string name="general_time_without_sb_sum">This time appears in brackets next to the current time. This shows the total video duration minus any segments.</string>
<string name="general_whitelisting">Channel whitelisting</string>
<string name="general_whitelisting_sum">Use the whitelist button under the player to whitelist a channel</string>
<string name="segments_preview">Preview/Recap</string>
<string name="segments_preview_sum">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.</string>
<string name="stats">Stats</string>
@ -342,6 +344,10 @@
<string name="vanced_videoadwhitelisting_title">Video ad whitelisting</string>
<string name="vanced_videoadwhitelisting_summary_off">Video ad whitelisting is turned off</string>
<string name="vanced_videoadwhitelisting_summary_on">Video ad whitelisting is turned on. Use the ad button under the player to whitelist a channel</string>
<string name="vanced_whitelisting_added" formatted="false">Channel %s was added to the %s whitelist</string>
<string name="vanced_whitelisting_removed" formatted="false">Channel %s was removed from the %s whitelist</string>
<string name="vanced_whitelisting_add_failed" formatted="false">Failed to add channel %s to the %s whitelist</string>
<string name="vanced_whitelisting_remove_failed" formatted="false">Failed to remove channel %s from the %s whitelist</string>
<string name="vanced_button_location_entry_none">Hidden</string>
<string name="vanced_button_location_entry_player">In player</string>
<string name="vanced_button_location_entry_buttoncontainer">Under player</string>