mirror of
https://github.com/revanced/revanced-integrations.git
synced 2025-01-05 17:45:49 +01:00
unify whitelisting
This commit is contained in:
parent
a8f6cca852
commit
f53b526957
@ -1,174 +0,0 @@
|
||||
package fi.vanced.libraries.youtube.ads;
|
||||
|
||||
import static fi.razerman.youtube.XGlobals.debug;
|
||||
import static fi.vanced.libraries.youtube.player.VideoInformation.channelName;
|
||||
import static fi.vanced.libraries.youtube.ui.SlimButtonContainer.adBlockButton;
|
||||
import static fi.vanced.utils.VancedUtils.getPreferences;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.util.Log;
|
||||
|
||||
import com.google.android.apps.youtube.app.YouTubeTikTokRoot_Application;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
|
||||
import fi.razerman.youtube.XGlobals;
|
||||
import fi.vanced.libraries.youtube.player.ChannelModel;
|
||||
import fi.vanced.libraries.youtube.player.VideoInformation;
|
||||
import fi.vanced.utils.ObjectSerializer;
|
||||
import fi.vanced.utils.SharedPrefUtils;
|
||||
|
||||
public class VideoAds {
|
||||
public static final String TAG = "VI - VideoAds";
|
||||
public static final String PREFERENCES_NAME = "channel-whitelist";
|
||||
public static boolean isEnabled;
|
||||
private static final String YT_API_URL = "https://www.youtube.com/youtubei/v1";
|
||||
private static final String YT_API_KEY = "replaceMeWithTheYouTubeAPIKey";
|
||||
private static ArrayList<ChannelModel> whiteList;
|
||||
private static Thread fetchThread = null;
|
||||
|
||||
static {
|
||||
whiteList = parseWhitelist(YouTubeTikTokRoot_Application.getAppContext());
|
||||
isEnabled = SharedPrefUtils.getBoolean(YouTubeTikTokRoot_Application.getAppContext(), "youtube", "vanced_videoadwhitelisting_enabled", false);
|
||||
}
|
||||
|
||||
// Call to this needs to be injected in YT code
|
||||
public static void setChannelName(String channelName) {
|
||||
if (debug) {
|
||||
Log.d(TAG, "channel name set to " + channelName);
|
||||
}
|
||||
VideoInformation.channelName = channelName;
|
||||
|
||||
if (!isEnabled) return;
|
||||
|
||||
if (adBlockButton != null) {
|
||||
adBlockButton.changeEnabled(getShouldShowAds());
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean getShouldShowAds() {
|
||||
if (!isEnabled) return false;
|
||||
|
||||
if (channelName == null || channelName.isEmpty() || channelName.trim().isEmpty()) {
|
||||
if (XGlobals.debug) {
|
||||
Log.d(TAG, "getShouldShowAds skipped because channelId was null");
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
for (ChannelModel channelModel: whiteList) {
|
||||
if (channelModel.getAuthor().equals(channelName)) {
|
||||
if (XGlobals.debug) {
|
||||
Log.d(TAG, "Video ad whitelist for " + channelName);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean addToWhitelist(Context context, String channelName, String channelId) {
|
||||
try {
|
||||
// Check that the channel doesn't exist already (can happen if for example the channel changes the name)
|
||||
// If it exists, remove it
|
||||
Iterator<ChannelModel> iterator = whiteList.iterator();
|
||||
while(iterator.hasNext())
|
||||
{
|
||||
ChannelModel value = iterator.next();
|
||||
if (value.getChannelId().equals(channelId))
|
||||
{
|
||||
if (XGlobals.debug) {
|
||||
Log.d(TAG, String.format("Tried whitelisting an existing channel again. Old info (%1$s | %2$s) - New info (%3$s | %4$s)",
|
||||
value.getAuthor(), value.getChannelId(), channelName, channelId));
|
||||
}
|
||||
iterator.remove();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
whiteList.add(new ChannelModel(channelName, channelId));
|
||||
updateWhitelist(context);
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex) {
|
||||
Log.d(TAG, "Unable to add " + channelName + " with id " + channelId + " to whitelist");
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean removeFromWhitelist(Context context, String channelName) {
|
||||
try {
|
||||
//whiteList.removeIf(x -> x.getAuthor().equals(channelName)); // Requires Android N
|
||||
|
||||
Iterator<ChannelModel> iterator = whiteList.iterator();
|
||||
while(iterator.hasNext())
|
||||
{
|
||||
ChannelModel value = iterator.next();
|
||||
if (value.getAuthor().equals(channelName))
|
||||
{
|
||||
iterator.remove();
|
||||
break;
|
||||
}
|
||||
}
|
||||
updateWhitelist(context);
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex) {
|
||||
Log.d(TAG, "Unable to remove " + channelName + " from whitelist");
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static void updateWhitelist(Context context) {
|
||||
if (context == null) return;
|
||||
|
||||
SharedPreferences preferences = getPreferences(context, PREFERENCES_NAME);
|
||||
SharedPreferences.Editor editor = preferences.edit();
|
||||
|
||||
try {
|
||||
editor.putString("channels", ObjectSerializer.serialize(whiteList));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
editor.apply();
|
||||
}
|
||||
|
||||
private static ArrayList<ChannelModel> parseWhitelist(Context context) {
|
||||
if (context == null) return new ArrayList<>();
|
||||
|
||||
SharedPreferences preferences = getPreferences(context, PREFERENCES_NAME);
|
||||
try {
|
||||
String channels = preferences.getString("channels", null);
|
||||
if (channels == null) {
|
||||
if (debug) {
|
||||
Log.d(TAG, "channels string was null for ad whitelisting");
|
||||
}
|
||||
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
ArrayList<ChannelModel> channelModels = (ArrayList<ChannelModel>) ObjectSerializer.deserialize(channels);
|
||||
if (debug) {
|
||||
Log.d(TAG, channels);
|
||||
for (ChannelModel channelModel: channelModels) {
|
||||
Log.d(TAG, "Ad whitelisted " + channelModel.getAuthor() + " with id of " + channelModel.getChannelId());
|
||||
}
|
||||
}
|
||||
|
||||
return channelModels;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return new ArrayList<>();
|
||||
}
|
||||
}
|
@ -1,7 +1,6 @@
|
||||
package fi.vanced.libraries.youtube.ui;
|
||||
|
||||
import static fi.razerman.youtube.XGlobals.debug;
|
||||
import static fi.vanced.libraries.youtube.ads.VideoAds.getShouldShowAds;
|
||||
import static fi.vanced.libraries.youtube.player.VideoInformation.currentVideoId;
|
||||
import static pl.jakubweg.StringRef.str;
|
||||
|
||||
@ -10,18 +9,19 @@ import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import fi.vanced.libraries.youtube.ads.VideoAds;
|
||||
import fi.vanced.libraries.youtube.player.VideoInformation;
|
||||
import fi.vanced.libraries.youtube.whitelisting.Whitelist;
|
||||
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;
|
||||
|
||||
public class AdBlock extends SlimButton {
|
||||
public static final String TAG = "VI - AdBlock - Button";
|
||||
public class AdButton extends SlimButton {
|
||||
public static final String TAG = "VI - AdButton - Button";
|
||||
|
||||
public AdBlock(Context context, ViewGroup container) {
|
||||
super(context, container, SlimButton.SLIM_METADATA_BUTTON_ID, SharedPrefUtils.getBoolean(context, "youtube", "vanced_videoadwhitelisting_enabled", false));
|
||||
public AdButton(Context context, ViewGroup container) {
|
||||
super(context, container, SlimButton.SLIM_METADATA_BUTTON_ID,
|
||||
SharedPrefUtils.getBoolean(context, "youtube", WhitelistType.ADS.getPreferenceEnabledName(), false));
|
||||
|
||||
initialize();
|
||||
}
|
||||
@ -29,7 +29,7 @@ public class AdBlock extends SlimButton {
|
||||
private void initialize() {
|
||||
this.button_icon.setImageResource(VancedUtils.getIdentifier("vanced_yt_ad_button", "drawable"));
|
||||
this.button_text.setText(str("action_ads"));
|
||||
changeEnabled(getShouldShowAds());
|
||||
changeEnabled(Whitelist.shouldShowAds());
|
||||
}
|
||||
|
||||
public void changeEnabled(boolean enabled) {
|
||||
@ -53,7 +53,7 @@ public class AdBlock extends SlimButton {
|
||||
|
||||
private void removeFromWhitelist() {
|
||||
try {
|
||||
VideoAds.removeFromWhitelist(this.context, VideoInformation.channelName);
|
||||
Whitelist.removeFromWhitelist(WhitelistType.ADS, this.context, VideoInformation.channelName);
|
||||
this.button_icon.setEnabled(false);
|
||||
}
|
||||
catch (Exception ex) {
|
@ -0,0 +1,74 @@
|
||||
package fi.vanced.libraries.youtube.ui;
|
||||
|
||||
import static fi.razerman.youtube.XGlobals.debug;
|
||||
import static fi.vanced.libraries.youtube.player.VideoInformation.currentVideoId;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import fi.vanced.libraries.youtube.player.VideoInformation;
|
||||
import fi.vanced.libraries.youtube.whitelisting.Whitelist;
|
||||
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;
|
||||
|
||||
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));
|
||||
|
||||
initialize();
|
||||
}
|
||||
|
||||
private void initialize() {
|
||||
this.button_icon.setImageResource(VancedUtils.getIdentifier("vanced_sb_logo", "drawable"));
|
||||
this.button_text.setText("SB");
|
||||
changeEnabled(Whitelist.shouldShowSegments());
|
||||
}
|
||||
|
||||
public void changeEnabled(boolean enabled) {
|
||||
if (debug) {
|
||||
Log.d(TAG, "changeEnabled " + enabled);
|
||||
}
|
||||
this.button_icon.setEnabled(enabled);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
this.view.setEnabled(false);
|
||||
if (this.button_icon.isEnabled()) {
|
||||
removeFromWhitelist();
|
||||
return;
|
||||
}
|
||||
//this.button_icon.setEnabled(!this.button_icon.isEnabled());
|
||||
|
||||
addToWhiteList();
|
||||
}
|
||||
|
||||
private void removeFromWhitelist() {
|
||||
try {
|
||||
Whitelist.removeFromWhitelist(WhitelistType.SPONSORBLOCK, this.context, VideoInformation.channelName);
|
||||
this.button_icon.setEnabled(false);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
Log.e(TAG, "Failed to remove from whitelist", ex);
|
||||
return;
|
||||
}
|
||||
|
||||
this.view.setEnabled(true);
|
||||
}
|
||||
|
||||
private void addToWhiteList() {
|
||||
new Thread(() -> {
|
||||
if (debug) {
|
||||
Log.d(TAG, "Fetching channelId for " + currentVideoId);
|
||||
}
|
||||
WhitelistRequester.addChannelToWhitelist(WhitelistType.SPONSORBLOCK, this.view, this.button_icon, this.context);
|
||||
}).start();
|
||||
}
|
||||
}
|
@ -10,7 +10,8 @@ import android.view.ViewGroup;
|
||||
|
||||
import com.google.android.apps.youtube.app.ui.SlimMetadataScrollableButtonContainerLayout;
|
||||
|
||||
import fi.vanced.libraries.youtube.ads.VideoAds;
|
||||
import fi.vanced.libraries.youtube.whitelisting.Whitelist;
|
||||
import fi.vanced.libraries.youtube.whitelisting.WhitelistType;
|
||||
import fi.vanced.utils.SharedPrefUtils;
|
||||
import fi.vanced.utils.VancedUtils;
|
||||
|
||||
@ -19,7 +20,8 @@ public class SlimButtonContainer extends SlimMetadataScrollableButtonContainerLa
|
||||
private ViewGroup container;
|
||||
private CopyButton copyButton;
|
||||
private CopyWithTimestamp copyWithTimestampButton;
|
||||
public static AdBlock adBlockButton;
|
||||
public static AdButton adBlockButton;
|
||||
public static SBWhitelistButton sbWhitelistButton;
|
||||
private final Context context;
|
||||
SharedPreferences.OnSharedPreferenceChangeListener listener;
|
||||
|
||||
@ -48,8 +50,8 @@ public class SlimButtonContainer extends SlimMetadataScrollableButtonContainerLa
|
||||
|
||||
copyButton = new CopyButton(context, this);
|
||||
copyWithTimestampButton = new CopyWithTimestamp(context, this);
|
||||
adBlockButton = new AdBlock(context, this);
|
||||
new SponsorBlock(context, this);
|
||||
adBlockButton = new AdButton(context, this);
|
||||
sbWhitelistButton = new SBWhitelistButton(context, this);
|
||||
new SponsorBlockVoting(context, this);
|
||||
|
||||
addSharedPrefsChangeListener();
|
||||
@ -73,9 +75,20 @@ public class SlimButtonContainer extends SlimMetadataScrollableButtonContainerLa
|
||||
copyWithTimestampButton.setVisible(ButtonVisibility.isVisibleInContainer(context, "pref_copy_video_url_timestamp_button_list"));
|
||||
return;
|
||||
}
|
||||
if ("vanced_videoadwhitelisting_enabled".equals(key) && adBlockButton != null) {
|
||||
VideoAds.isEnabled = SharedPrefUtils.getBoolean(context, "youtube", "vanced_videoadwhitelisting_enabled", false);
|
||||
adBlockButton.setVisible(VideoAds.isEnabled);
|
||||
WhitelistType whitelistAds = WhitelistType.ADS;
|
||||
String adsEnabledPreferenceName = whitelistAds.getPreferenceEnabledName();
|
||||
if (adsEnabledPreferenceName.equals(key) && adBlockButton != null) {
|
||||
boolean enabled = SharedPrefUtils.getBoolean(context, "youtube", adsEnabledPreferenceName, false);
|
||||
Whitelist.setEnabled(whitelistAds, enabled);
|
||||
adBlockButton.setVisible(enabled);
|
||||
return;
|
||||
}
|
||||
WhitelistType whitelistSB = WhitelistType.SPONSORBLOCK;
|
||||
String sbEnabledPreferenceName = whitelistSB.getPreferenceEnabledName();
|
||||
if (sbEnabledPreferenceName.equals(key) && sbWhitelistButton != null) {
|
||||
boolean enabled = SharedPrefUtils.getBoolean(context, "youtube", sbEnabledPreferenceName, false);
|
||||
Whitelist.setEnabled(whitelistSB, enabled);
|
||||
sbWhitelistButton.setVisible(enabled);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -1,31 +0,0 @@
|
||||
package fi.vanced.libraries.youtube.ui;
|
||||
|
||||
import static pl.jakubweg.StringRef.str;
|
||||
|
||||
import android.content.Context;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.google.android.apps.youtube.app.YouTubeTikTokRoot_Application;
|
||||
|
||||
import fi.vanced.libraries.youtube.player.VideoHelpers;
|
||||
import fi.vanced.utils.VancedUtils;
|
||||
|
||||
public class SponsorBlock extends SlimButton {
|
||||
public SponsorBlock(Context context, ViewGroup container) {
|
||||
super(context, container, SlimButton.SLIM_METADATA_BUTTON_ID, false);
|
||||
|
||||
initialize();
|
||||
}
|
||||
|
||||
private void initialize() {
|
||||
this.button_icon.setImageResource(VancedUtils.getIdentifier("vanced_sb_logo", "drawable"));
|
||||
this.button_text.setText("SB");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
Toast.makeText(YouTubeTikTokRoot_Application.getAppContext(), "Nothing atm", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
@ -2,6 +2,8 @@ package fi.vanced.libraries.youtube.whitelisting;
|
||||
|
||||
import static fi.razerman.youtube.XGlobals.debug;
|
||||
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 android.content.Context;
|
||||
@ -19,6 +21,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import fi.vanced.libraries.youtube.player.ChannelModel;
|
||||
import fi.vanced.libraries.youtube.player.VideoInformation;
|
||||
import fi.vanced.utils.ObjectSerializer;
|
||||
import fi.vanced.utils.SharedPrefUtils;
|
||||
import fi.vanced.utils.VancedUtils;
|
||||
@ -30,10 +33,28 @@ public class Whitelist {
|
||||
|
||||
private Whitelist() {}
|
||||
|
||||
// injected calls
|
||||
|
||||
public static boolean shouldShowAds() {
|
||||
return isWhitelisted(WhitelistType.ADS);
|
||||
}
|
||||
|
||||
public static void setChannelName(String channelName) {
|
||||
if (debug) {
|
||||
Log.d(TAG, "channel name set to " + channelName);
|
||||
}
|
||||
VideoInformation.channelName = channelName;
|
||||
|
||||
if (enabledMap.get(WhitelistType.ADS) && adBlockButton != null) {
|
||||
adBlockButton.changeEnabled(shouldShowAds());
|
||||
}
|
||||
if (enabledMap.get(WhitelistType.SPONSORBLOCK) && sbWhitelistButton != null) {
|
||||
sbWhitelistButton.changeEnabled(shouldShowSegments());
|
||||
}
|
||||
}
|
||||
|
||||
// the rest
|
||||
|
||||
public static boolean shouldShowSegments() {
|
||||
return !isWhitelisted(WhitelistType.SPONSORBLOCK);
|
||||
}
|
||||
@ -148,4 +169,8 @@ public class Whitelist {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static void setEnabled(WhitelistType whitelistType, boolean enabled) {
|
||||
enabledMap.put(whitelistType, enabled);
|
||||
}
|
||||
}
|
@ -2,7 +2,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.AdBlock.TAG;
|
||||
import static fi.vanced.libraries.youtube.ui.AdButton.TAG;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Handler;
|
||||
|
@ -5,7 +5,7 @@ import static fi.vanced.utils.requests.Route.Method.GET;
|
||||
import fi.vanced.utils.requests.Route;
|
||||
|
||||
public class WhitelistRoutes {
|
||||
public static final Route GET_CHANNEL_DETAILS = new Route(GET, "player?key={api_key");
|
||||
public static final Route GET_CHANNEL_DETAILS = new Route(GET, "player?key={api_key}");
|
||||
|
||||
private WhitelistRoutes() {}
|
||||
}
|
Loading…
Reference in New Issue
Block a user