From 0f6dee5bae6b8017a53830587e09079942bc24aa Mon Sep 17 00:00:00 2001 From: oSumAtrIX Date: Sun, 24 Dec 2023 18:38:33 +0100 Subject: [PATCH] feat(YouTube - Hide ads): Hide fullscreen ads --- .../patches/components/AdsFilter.java | 39 +++++++++++++++++-- .../integrations/settings/SettingsEnum.java | 1 + 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/app/revanced/integrations/patches/components/AdsFilter.java b/app/src/main/java/app/revanced/integrations/patches/components/AdsFilter.java index bcead636..edf7063b 100644 --- a/app/src/main/java/app/revanced/integrations/patches/components/AdsFilter.java +++ b/app/src/main/java/app/revanced/integrations/patches/components/AdsFilter.java @@ -1,15 +1,22 @@ package app.revanced.integrations.patches.components; +import android.app.Instrumentation; +import android.view.KeyEvent; import android.view.View; - import androidx.annotation.Nullable; - import app.revanced.integrations.settings.SettingsEnum; import app.revanced.integrations.utils.ReVancedUtils; import app.revanced.integrations.utils.StringTrieSearch; @SuppressWarnings("unused") public final class AdsFilter extends Filter { + // region Fullscreen ad + private static long lastTimeClosedFullscreenAd = 0; + private static final Instrumentation instrumentation = new Instrumentation(); + private final StringFilterGroup fullscreenAd; + + // endregion + private final StringTrieSearch exceptions = new StringTrieSearch(); private final StringFilterGroup shoppingLinks; @@ -24,6 +31,7 @@ public final class AdsFilter extends Filter { // Identifiers. + final var carouselAd = new StringFilterGroup( SettingsEnum.HIDE_GENERAL_ADS, "carousel_ad" @@ -32,6 +40,11 @@ public final class AdsFilter extends Filter { // Paths. + fullscreenAd = new StringFilterGroup( + SettingsEnum.HIDE_FULLSCREEN_ADS, + "fullscreen_ad" + ); + final var buttonedAd = new StringFilterGroup( SettingsEnum.HIDE_BUTTONED_ADS, "_buttoned_layout", @@ -102,6 +115,7 @@ public final class AdsFilter extends Filter { merchandise, viewProducts, selfSponsor, + fullscreenAd, webLinkPanel, shoppingLinks, movieAds @@ -112,7 +126,11 @@ public final class AdsFilter extends Filter { public boolean isFiltered(@Nullable String identifier, String path, byte[] protobufBufferArray, StringFilterGroup matchedGroup, FilterContentType contentType, int contentIndex) { if (exceptions.matches(path)) - return false; + return false; + + if (matchedGroup == fullscreenAd && path.contains("|ImageType|")) { + closeFullscreenAd(); + } // Check for the index because of likelihood of false positives. if (matchedGroup == shoppingLinks && contentIndex != 0) @@ -129,4 +147,19 @@ public final class AdsFilter extends Filter { public static void hideAdAttributionView(View view) { ReVancedUtils.hideViewBy1dpUnderCondition(SettingsEnum.HIDE_GENERAL_ADS, view); } + + /** + * Close the fullscreen ad. + *

+ * The strategy is to send a back button event to the app to close the fullscreen ad using the back button event. + */ + private static void closeFullscreenAd() { + final var currentTime = System.currentTimeMillis(); + + // Prevent spamming the back button. + if (currentTime - lastTimeClosedFullscreenAd < 10000) return; + lastTimeClosedFullscreenAd = currentTime; + + ReVancedUtils.runOnMainThreadDelayed(() -> instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK), 1000); + } } diff --git a/app/src/main/java/app/revanced/integrations/settings/SettingsEnum.java b/app/src/main/java/app/revanced/integrations/settings/SettingsEnum.java index 327f2f51..cd154686 100644 --- a/app/src/main/java/app/revanced/integrations/settings/SettingsEnum.java +++ b/app/src/main/java/app/revanced/integrations/settings/SettingsEnum.java @@ -43,6 +43,7 @@ public enum SettingsEnum { "0.25\n0.5\n0.75\n0.9\n0.95\n1.0\n1.05\n1.1\n1.25\n1.5\n1.75\n2.0\n3.0\n4.0\n5.0", true), // Ads + HIDE_FULLSCREEN_ADS("revanced_hide_fullscreen_ads", BOOLEAN, TRUE), HIDE_BUTTONED_ADS("revanced_hide_buttoned_ads", BOOLEAN, TRUE), HIDE_GENERAL_ADS("revanced_hide_general_ads", BOOLEAN, TRUE), HIDE_GET_PREMIUM("revanced_hide_get_premium", BOOLEAN, TRUE),