move some requests and prepare for unifying

This commit is contained in:
caneleex 2022-01-17 18:12:09 +01:00
parent 55e1212cd9
commit 2f57347f72
4 changed files with 69 additions and 16 deletions

View File

@ -10,9 +10,10 @@ import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import fi.vanced.libraries.youtube.ads.AdsRequester;
import fi.vanced.libraries.youtube.ads.VideoAds;
import fi.vanced.libraries.youtube.player.VideoInformation;
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;
@ -68,7 +69,7 @@ public class AdBlock extends SlimButton {
if (debug) {
Log.d(TAG, "Fetching channelId for " + currentVideoId);
}
AdsRequester.retrieveChannelDetails(this.view, this.button_icon, this.context);
WhitelistRequester.addChannelToWhitelist(WhitelistType.ADS, this.view, this.button_icon, this.context);
}).start();
}
}

View File

@ -2,6 +2,7 @@ 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.utils.VancedUtils.getPreferences;
import android.content.Context;
import android.content.SharedPreferences;
@ -9,8 +10,11 @@ import android.util.Log;
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.Iterator;
import java.util.List;
import java.util.Map;
@ -21,7 +25,7 @@ import fi.vanced.utils.VancedUtils;
public class Whitelist {
private static final String TAG = "VI - Whitelisting";
private static final Map<WhitelistType, List<ChannelModel>> whitelistMap = parseWhitelist(YouTubeTikTokRoot_Application.getAppContext());
private static final Map<WhitelistType, ArrayList<ChannelModel>> whitelistMap = parseWhitelist(YouTubeTikTokRoot_Application.getAppContext());
private static final Map<WhitelistType, Boolean> enabledMap = parseEnabledMap(YouTubeTikTokRoot_Application.getAppContext());
private Whitelist() {}
@ -34,12 +38,12 @@ public class Whitelist {
return !isWhitelisted(WhitelistType.SPONSORBLOCK);
}
private static Map<WhitelistType, List<ChannelModel>> parseWhitelist(Context context) {
private static Map<WhitelistType, ArrayList<ChannelModel>> parseWhitelist(Context context) {
if (context == null) {
return Collections.emptyMap();
}
WhitelistType[] whitelistTypes = WhitelistType.values();
Map<WhitelistType, List<ChannelModel>> whitelistMap = new HashMap<>(whitelistTypes.length);
Map<WhitelistType, ArrayList<ChannelModel>> whitelistMap = new HashMap<>(whitelistTypes.length);
for (WhitelistType whitelistType : whitelistTypes) {
SharedPreferences preferences = VancedUtils.getPreferences(context, whitelistType.getPreferencesName());
@ -48,10 +52,10 @@ public class Whitelist {
if (debug) {
Log.d(TAG, String.format("channels string was null for %s whitelisting", whitelistType));
}
return Collections.emptyMap();
continue;
}
try {
List<ChannelModel> deserializedChannels = (List<ChannelModel>) ObjectSerializer.deserialize(serializedChannels);
ArrayList<ChannelModel> deserializedChannels = (ArrayList<ChannelModel>) ObjectSerializer.deserialize(serializedChannels);
if (debug) {
Log.d(TAG, serializedChannels);
for (ChannelModel channel : deserializedChannels) {
@ -98,4 +102,50 @@ public class Whitelist {
}
return false;
}
public static boolean addToWhitelist(WhitelistType whitelistType, Context context, ChannelModel channel) {
ArrayList<ChannelModel> whitelisted = whitelistMap.get(whitelistType);
for (ChannelModel whitelistedChannel : whitelisted) {
String channelId = channel.getChannelId();
if (whitelistedChannel.getChannelId().equals(channelId)) {
if (debug) {
Log.d(TAG, String.format("Tried whitelisting an existing channel again. Old info (%1$s | %2$s) - New info (%3$s | %4$s)",
whitelistedChannel.getAuthor(), channelId, channelName, channelId));
}
return true;
}
}
whitelisted.add(channel);
return updateWhitelist(whitelistType, whitelisted, context);
}
public static boolean removeFromWhitelist(WhitelistType whitelistType, Context context, String channelName) {
ArrayList<ChannelModel> channels = whitelistMap.get(whitelistType);
Iterator<ChannelModel> iterator = channels.iterator();
while (iterator.hasNext()) {
ChannelModel channel = iterator.next();
if (channel.getAuthor().equals(channelName)) {
iterator.remove();
break;
}
}
return updateWhitelist(whitelistType, channels, context);
}
private static boolean updateWhitelist(WhitelistType whitelistType, ArrayList<ChannelModel> channels, Context context) {
if (context == null) {
return false;
}
SharedPreferences preferences = getPreferences(context, whitelistType.getPreferencesName());
SharedPreferences.Editor editor = preferences.edit();
try {
editor.putString("channels", ObjectSerializer.serialize(channels));
editor.apply();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}

View File

@ -1,4 +1,4 @@
package fi.vanced.libraries.youtube.ads;
package fi.vanced.libraries.youtube.whitelisting.requests;
import static fi.razerman.youtube.XGlobals.debug;
import static fi.vanced.libraries.youtube.player.VideoInformation.currentVideoId;
@ -20,16 +20,18 @@ import java.net.HttpURLConnection;
import java.nio.charset.StandardCharsets;
import fi.vanced.libraries.youtube.player.ChannelModel;
import fi.vanced.libraries.youtube.whitelisting.Whitelist;
import fi.vanced.libraries.youtube.whitelisting.WhitelistType;
import fi.vanced.utils.requests.Requester;
import fi.vanced.utils.requests.Route;
public class AdsRequester {
public class WhitelistRequester {
private static final String YT_API_URL = "https://www.youtube.com/youtubei/v1/";
private static final String YT_API_KEY = "replaceMeWithTheYouTubeAPIKey";
public static void retrieveChannelDetails(View view, ImageView buttonIcon, Context context) {
public static void addChannelToWhitelist(WhitelistType whitelistType, View view, ImageView buttonIcon, Context context) {
try {
HttpURLConnection connection = getConnectionFromRoute(AdsRoutes.GET_CHANNEL_DETAILS, YT_API_KEY);
HttpURLConnection connection = getConnectionFromRoute(WhitelistRoutes.GET_CHANNEL_DETAILS, YT_API_KEY);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json; utf-8");
connection.setRequestProperty("Accept", "application/json");
@ -50,7 +52,7 @@ public class AdsRequester {
Log.d(TAG, "channelId " + channelModel.getChannelId() + " fetched for author " + channelModel.getAuthor());
}
boolean success = VideoAds.addToWhitelist(context, channelModel.getAuthor(), channelModel.getChannelId());
boolean success = Whitelist.addToWhitelist(whitelistType, context, channelModel);
new Handler(Looper.getMainLooper()).post(() -> {
if (success) {
buttonIcon.setEnabled(true);

View File

@ -1,11 +1,11 @@
package fi.vanced.libraries.youtube.ads;
package fi.vanced.libraries.youtube.whitelisting.requests;
import static fi.vanced.utils.requests.Route.Method.GET;
import fi.vanced.utils.requests.Route;
public class AdsRoutes {
public static final Route GET_CHANNEL_DETAILS = new Route(GET, "player?key={api_key}");
public class WhitelistRoutes {
public static final Route GET_CHANNEL_DETAILS = new Route(GET, "player?key={api_key");
private AdsRoutes() {}
private WhitelistRoutes() {}
}