Skip to content

Commit

Permalink
feat(YouTube - Hide ads): Hide fullscreen ads
Browse files Browse the repository at this point in the history
  • Loading branch information
oSumAtrIX committed Dec 24, 2023
1 parent d241e43 commit 0f6dee5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -24,6 +31,7 @@ public AdsFilter() {

// Identifiers.


final var carouselAd = new StringFilterGroup(
SettingsEnum.HIDE_GENERAL_ADS,
"carousel_ad"
Expand All @@ -32,6 +40,11 @@ public AdsFilter() {

// Paths.

fullscreenAd = new StringFilterGroup(
SettingsEnum.HIDE_FULLSCREEN_ADS,
"fullscreen_ad"
);

final var buttonedAd = new StringFilterGroup(
SettingsEnum.HIDE_BUTTONED_ADS,
"_buttoned_layout",
Expand Down Expand Up @@ -102,6 +115,7 @@ public AdsFilter() {
merchandise,
viewProducts,
selfSponsor,
fullscreenAd,
webLinkPanel,
shoppingLinks,
movieAds
Expand All @@ -112,7 +126,11 @@ public AdsFilter() {
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)
Expand All @@ -129,4 +147,19 @@ public boolean isFiltered(@Nullable String identifier, String path, byte[] proto
public static void hideAdAttributionView(View view) {
ReVancedUtils.hideViewBy1dpUnderCondition(SettingsEnum.HIDE_GENERAL_ADS, view);
}

/**
* Close the fullscreen ad.
* <p>
* 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 0f6dee5

Please sign in to comment.