Skip to content
This repository has been archived by the owner on Dec 9, 2024. It is now read-only.

Commit

Permalink
feat(Channel Whitelist): Bello world !
Browse files Browse the repository at this point in the history
  • Loading branch information
YT-Advanced committed Jan 27, 2024
1 parent f43c512 commit 256d7f1
Show file tree
Hide file tree
Showing 16 changed files with 767 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@
import app.revanced.integrations.youtube.requests.Route;
import app.revanced.integrations.youtube.utils.LogHelper;

final class PlayerRoutes {
public final class PlayerRoutes {
static final Route.CompiledRoute GET_CHANNEL_INFORMATION = new Route(
Route.Method.POST,
"player" +
"?fields=videoDetails.channelId," +
"videoDetails.author"
).compile();
static final Route.CompiledRoute GET_STORYBOARD_SPEC_RENDERER = new Route(
Route.Method.POST,
"player" +
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
package app.revanced.integrations.youtube.patches.overlaybutton;

import static app.revanced.integrations.youtube.utils.ResourceUtils.anim;
import static app.revanced.integrations.youtube.utils.ResourceUtils.findView;
import static app.revanced.integrations.youtube.utils.ResourceUtils.integer;
import static app.revanced.integrations.youtube.utils.StringRef.str;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.support.constraint.ConstraintLayout;
import android.view.View;
import android.view.animation.Animation;
import android.widget.ImageView;

import java.lang.ref.WeakReference;

import app.revanced.integrations.youtube.patches.utils.PatchStatus;
import app.revanced.integrations.youtube.patches.video.VideoInformation;
import app.revanced.integrations.youtube.settings.SettingsEnum;
import app.revanced.integrations.youtube.utils.LogHelper;
import app.revanced.integrations.youtube.whitelist.Whitelist;
import app.revanced.integrations.youtube.whitelist.WhitelistType;
import app.revanced.integrations.youtube.whitelist.requests.WhitelistRequester;

public class Whitelists {
static WeakReference<ImageView> buttonView = new WeakReference<>(null);
@SuppressLint("StaticFieldLeak")
static ConstraintLayout constraintLayout;
static int fadeDurationFast;
static int fadeDurationScheduled;
static Animation fadeIn;
static Animation fadeOut;
public static boolean isButtonEnabled;
static boolean isShowing;
static boolean isScrubbed;

static boolean isSBWhitelisted;
static boolean isSPEEDWhitelisted;

static boolean isSBIncluded;
static boolean isSPEEDIncluded;

public static void initialize(Object obj) {
try {
constraintLayout = (ConstraintLayout) obj;

isSBWhitelisted = Whitelist.isChannelSBWhitelisted();
isSPEEDWhitelisted = Whitelist.isChannelSPEEDWhitelisted();

isSBIncluded = PatchStatus.SponsorBlock();
isSPEEDIncluded = PatchStatus.VideoSpeed();

isButtonEnabled = setValue();

ImageView imageView = findView(constraintLayout, "whitelist_button");
imageView.setOnClickListener(view -> Whitelists.OpenDialog(view.getContext()));
buttonView = new WeakReference<>(imageView);

fadeDurationFast = integer("fade_duration_fast");
fadeDurationScheduled = integer("fade_duration_scheduled");

fadeIn = anim("fade_in");
fadeIn.setDuration(fadeDurationFast);

fadeOut = anim("fade_out");
fadeOut.setDuration(fadeDurationScheduled);

isShowing = true;
isScrubbed = false;
changeVisibility(false);

} catch (Exception ex) {
LogHelper.printException(() -> "Unable to set FrameLayout", ex);
}
}

public static void refreshVisibility() {
isButtonEnabled = setValue();
}

private static boolean setValue() {
boolean isEnabled = SettingsEnum.OVERLAY_BUTTON_WHITELIST.getBoolean();

return isEnabled && (isSBIncluded || isSPEEDIncluded);
}

public static void changeVisibility(boolean currentVisibility) {
ImageView imageView = buttonView.get();

if (isShowing == currentVisibility || constraintLayout == null || imageView == null) return;

isShowing = currentVisibility;

if (isScrubbed && isButtonEnabled) {
isScrubbed = false;
imageView.setVisibility(View.VISIBLE);
return;
}

if (currentVisibility && isButtonEnabled) {
imageView.setVisibility(View.VISIBLE);
imageView.startAnimation(fadeIn);
} else if (imageView.getVisibility() == View.VISIBLE) {
imageView.startAnimation(fadeOut);
imageView.setVisibility(View.GONE);
}
}

public static void changeVisibilityNegatedImmediate(boolean isUserScrubbing) {
ImageView imageView = buttonView.get();

if (constraintLayout == null || imageView == null || !isUserScrubbing) return;

isShowing = false;
isScrubbed = true;
imageView.setVisibility(View.GONE);
}

public static void OpenDialog(Context context) {
String included = str("revanced_whitelisting_included");
String excluded = str("revanced_whitelisting_excluded");

isSBWhitelisted = Whitelist.isChannelSBWhitelisted();
isSPEEDWhitelisted = Whitelist.isChannelSPEEDWhitelisted();

AlertDialog.Builder builder;
builder = new AlertDialog.Builder(context);

builder.setTitle(str("revanced_whitelisting_title"));

StringBuilder msgBuilder = new StringBuilder();
msgBuilder.append(str("revanced_whitelisting_channel_name"));
msgBuilder.append(":\n");
msgBuilder.append(VideoInformation.getChannelName());
msgBuilder.append("\n\n");

if (isSPEEDIncluded) {
msgBuilder.append(str("revanced_whitelisting_speed"));
msgBuilder.append(":\n");
msgBuilder.append(isSPEEDWhitelisted ? included : excluded);
msgBuilder.append("\n\n");
builder.setNeutralButton(str("revanced_whitelisting_speed_button"),
(dialog, id) -> {
WhitelistListener(WhitelistType.SPEED, isSPEEDWhitelisted, context);
dialog.dismiss();
}
);
}

if (isSBIncluded) {
msgBuilder.append(str("revanced_whitelisting_sponsorblock"));
msgBuilder.append(":\n");
msgBuilder.append(isSBWhitelisted ? included : excluded);
builder.setPositiveButton(str("revanced_whitelisting_sponsorblock_button"),
(dialog, id) -> {
WhitelistListener(WhitelistType.SPONSORBLOCK, isSBWhitelisted, context);
dialog.dismiss();
}
);
}

builder.setMessage(msgBuilder.toString());

AlertDialog dialog = builder.create();
dialog.show();
}

public static void WhitelistListener(WhitelistType whitelistType, boolean status, Context context) {
if (!status) {
addToWhiteList(whitelistType);
} else {
removeFromWhitelist(whitelistType, context);
}
}

private static void removeFromWhitelist(WhitelistType whitelistType, Context context) {
ImageView imageView = buttonView.get();
if (constraintLayout == null || imageView == null) return;

try {
Whitelist.removeFromWhitelist(whitelistType, VideoInformation.getChannelName(), context);
} catch (Exception ex) {
LogHelper.printException(() -> "Failed to remove from whitelist", ex);
}
}

private static void addToWhiteList(WhitelistType whitelistType) {
new Thread(() -> WhitelistRequester.addChannelToWhitelist(whitelistType)).start();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,14 @@ public static boolean DefaultPlaybackSpeed() {
// Replace this with true if the Default playback speed patch succeeds
return false;
}

public static boolean SponsorBlock() {
// Replace this with true if the SponsorBlock patch succeeds
return false;
}

public static boolean VideoSpeed() {
// Replace this with true if the Default Video Speed patch succeeds
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import app.revanced.integrations.youtube.settings.SettingsEnum;
import app.revanced.integrations.youtube.utils.LogHelper;
import app.revanced.integrations.youtube.whitelist.Whitelist;

@SuppressWarnings("unused")
public class PlaybackSpeedPatch {
Expand All @@ -23,6 +24,8 @@ public static void newVideoStarted(final String contentCpn) {

if (SettingsEnum.DISABLE_DEFAULT_PLAYBACK_SPEED_LIVE.getBoolean() && isLiveStream)
return;
if (Whitelist.isChannelSPEEDWhitelisted())
return;

currentPlaybackSpeed = SettingsEnum.DEFAULT_PLAYBACK_SPEED.getFloat();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package app.revanced.integrations.youtube.patches.video;

import java.io.Serializable;

public class VideoChannel implements Serializable {
private String author;
private String channelId;

public VideoChannel(String author, String channelId) {
this.author = author;
this.channelId = channelId;
}

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}

public String getChannelId() {
return channelId;
}

public void setChannelId(String channelId) {
this.channelId = channelId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public final class VideoInformation {
private static WeakReference<Object> playerControllerRef;
private static Method seekMethod;

@NonNull
private static String channelName = "";

@NonNull
private static String videoId = "";

Expand Down Expand Up @@ -138,6 +141,17 @@ public static String getVideoId() {
return videoId;
}

/**
* Channel name of the current video playing.
* <b>Currently this does not function for Shorts playback.</b>
*
* @return The channel name of the video. Empty string if not set yet.
*/
@NonNull
public static String getChannelName() {
return channelName;
}

/**
* Injection point.
*
Expand All @@ -150,6 +164,16 @@ public static void setVideoId(@NonNull String newlyLoadedVideoId) {
videoId = newlyLoadedVideoId;
}

/**
* Injection point.
*
* @param newlyLoadedChannelName channel name of the current video
*/
public static void setChannelName(@NonNull String newlyLoadedChannelName) {
if (!channelName.equals(newlyLoadedChannelName))
channelName = newlyLoadedChannelName;
}

/**
* Differs from {@link #videoId} as this is the video id for the
* last player response received, which may not be the last video opened.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,10 @@ public enum SettingsEnum {
OVERLAY_BUTTON_EXTERNAL_DOWNLOADER("revanced_overlay_button_external_downloader", BOOLEAN, FALSE),
OVERLAY_BUTTON_SPEED_DIALOG("revanced_overlay_button_speed_dialog", BOOLEAN, FALSE),
EXTERNAL_DOWNLOADER_PACKAGE_NAME("revanced_external_downloader_package_name", STRING, "com.deniscerri.ytdl", true),
OVERLAY_BUTTON_WHITELIST("revanced_overlay_button_whitelist", BOOLEAN, FALSE),
// Channel Whitelist
SPEED_WHITELIST("revanced_whitelist_speed", BOOLEAN, FALSE),
SB_WHITELIST("revanced_whitelisting_sponsorblock", BOOLEAN, FALSE),

// Experimental Flags
HOOK_DOWNLOAD_BUTTON("revanced_hook_download_button", BOOLEAN, FALSE),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@
import app.revanced.integrations.youtube.patches.overlaybutton.CopyVideoUrlTimestamp;
import app.revanced.integrations.youtube.patches.overlaybutton.ExternalDownload;
import app.revanced.integrations.youtube.patches.overlaybutton.SpeedDialog;
import app.revanced.integrations.youtube.patches.overlaybutton.Whitelists;
import app.revanced.integrations.youtube.patches.video.CustomPlaybackSpeedPatch;
import app.revanced.integrations.youtube.patches.utils.PatchStatus;
import app.revanced.integrations.youtube.settings.SettingsEnum;
import app.revanced.integrations.youtube.settings.SettingsUtils;
import app.revanced.integrations.youtube.whitelist.Whitelist;
import app.revanced.integrations.youtube.whitelist.WhitelistType;
import app.revanced.integrations.youtube.utils.LogHelper;
import app.revanced.integrations.youtube.utils.ReVancedHelper;
import app.revanced.integrations.youtube.utils.ResourceType;
Expand Down Expand Up @@ -90,6 +94,7 @@ public class ReVancedSettingsFragment extends PreferenceFragment {
case OVERLAY_BUTTON_COPY_VIDEO_URL_TIMESTAMP ->
CopyVideoUrlTimestamp.refreshVisibility();
case OVERLAY_BUTTON_EXTERNAL_DOWNLOADER -> ExternalDownload.refreshVisibility();
case OVERLAY_BUTTON_WHITELIST -> Whitelists.refreshVisibility();
case OVERLAY_BUTTON_SPEED_DIALOG -> SpeedDialog.refreshVisibility();
}
} else if (mPreference instanceof EditTextPreference editTextPreference) {
Expand Down
Loading

0 comments on commit 256d7f1

Please sign in to comment.