From 8ff5f0e68d2daeb2389899c52fe7125dfa5c4a46 Mon Sep 17 00:00:00 2001 From: oSumAtrIX Date: Sat, 11 Jun 2022 21:37:26 +0200 Subject: [PATCH] feat: make `containsAd` more efficient & add new values --- app/build.gradle | 5 +- app/src/main/AndroidManifest.xml | 5 +- .../youtube/litho/LithoAdRemoval.java | 134 ++++++++++-------- build.gradle | 3 +- gradle/wrapper/gradle-wrapper.properties | 2 +- settings.gradle | 2 +- 6 files changed, 80 insertions(+), 71 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index dab332d778..c75034bda7 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -2,11 +2,12 @@ apply plugin: 'com.android.application' android { compileSdkVersion 32 + namespace 'vanced.integrations' defaultConfig { applicationId "revanced.integrationsapp" minSdkVersion 23 - targetSdkVersion 31 + targetSdkVersion 32 versionCode 1 versionName "1.0" multiDexEnabled false @@ -33,6 +34,6 @@ android { dependencies { implementation 'androidx.annotation:annotation:1.3.0' - implementation "androidx.constraintlayout:constraintlayout:2.1.0" + implementation "androidx.constraintlayout:constraintlayout:2.1.4" } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 328d7e1aa4..6a21761cdd 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,7 +1,4 @@ - - + \ No newline at end of file diff --git a/app/src/main/java/fi/razerman/youtube/litho/LithoAdRemoval.java b/app/src/main/java/fi/razerman/youtube/litho/LithoAdRemoval.java index 7db137bc23..76bdc4b609 100644 --- a/app/src/main/java/fi/razerman/youtube/litho/LithoAdRemoval.java +++ b/app/src/main/java/fi/razerman/youtube/litho/LithoAdRemoval.java @@ -1,10 +1,14 @@ package fi.razerman.youtube.litho; +import android.os.Build; import android.util.Log; +import androidx.annotation.RequiresApi; + import com.google.android.apps.youtube.app.YouTubeTikTokRoot_Application; import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; import java.util.Objects; @@ -13,8 +17,6 @@ import fi.razerman.youtube.XGlobals; public class LithoAdRemoval { - private static final byte[] endRelatedPageAd = {112, 97, 103, 101, 97, 100}; - private static boolean getBoolean(String key, boolean _default) { return SharedPrefs.getBoolean(Objects.requireNonNull(YouTubeTikTokRoot_Application.getAppContext()), key, _default); } @@ -43,8 +45,8 @@ public static boolean isExperimentalCommunityPostRemoval() { return getBoolean("experimental_community_posts", false); } - public static boolean isExperimentalMovieUpsellRemoval() { - return getBoolean("experimental_movie_upsell", false); + public static boolean isExperimentalMovieRemoval() { + return getBoolean("experimental_movie", true); } public static boolean isExperimentalCompactBannerRemoval() { @@ -59,14 +61,6 @@ public static boolean isExperimentalCommentsRemoval() { return getBoolean("experimental_comments", false); } - public static boolean isExperimentalCompactMovieRemoval() { - return getBoolean("experimental_compact_movie", false); - } - - public static boolean isExperimentalHorizontalMovieShelfRemoval() { - return getBoolean("experimental_horizontal_movie_shelf", false); - } - public static boolean isInFeedSurvey() { return getBoolean("experimental_in_feed_survey", false); } @@ -79,12 +73,26 @@ public static boolean isCommunityGuidelines() { return getBoolean("experimental_community_guidelines", true); } + @RequiresApi(api = Build.VERSION_CODES.N) public static boolean containsAd(String value, ByteBuffer buffer) { try { - if (!(isExperimentalAdRemoval() || isExperimentalMerchandiseRemoval() || isExperimentalPaidContentRemoval() || isExperimentalCommunityPostRemoval() || isExperimentalMovieUpsellRemoval() || isExperimentalCompactBannerRemoval() || isExperimentalCommentsRemoval() || isExperimentalCompactMovieRemoval() || isExperimentalHorizontalMovieShelfRemoval() || isInFeedSurvey() || isShortsShelf() || isCommunityGuidelines()) || value == null || value.isEmpty()) { + if (!(isExperimentalAdRemoval() || + isExperimentalMerchandiseRemoval() || + isExperimentalPaidContentRemoval() || isExperimentalCommunityPostRemoval() || + isExperimentalMovieRemoval() || + isExperimentalCompactBannerRemoval() || + isExperimentalCommentsRemoval() || + isInFeedSurvey() || + isShortsShelf() || + isCommunityGuidelines()) || + value == null || + value.isEmpty() + ) { return false; } List blockList = new ArrayList<>(); + List bufferBlockList = new ArrayList<>(); + if (isExperimentalAdRemoval()) { blockList.add("_ad"); blockList.add("ad_badge"); @@ -93,16 +101,32 @@ public static boolean containsAd(String value, ByteBuffer buffer) { blockList.add("shelf_header"); blockList.add("cell_divider"); blockList.add("watch_metadata_app_promo"); + + bufferBlockList.add("ad_cpn"); + } + if (isExperimentalMovieRemoval()) { + blockList.add("movie_and_show_upsell_card"); + blockList.add("compact_movie"); + blockList.add("horizontal_movie_shelf"); + + bufferBlockList.add("YouTube Movies"); } + + if ( + value.contains("related_video_with_context") && + bufferBlockList + .stream() + .anyMatch(StandardCharsets.UTF_8.decode(buffer).toString()::contains) + ) return true; + + if (isExperimentalMerchandiseRemoval()) { blockList.add("product_carousel"); } if (isExperimentalCommunityPostRemoval()) { blockList.add("post_base_wrapper"); } - if (isExperimentalMovieUpsellRemoval()) { - blockList.add("movie_and_show_upsell_card"); - } + if (isExperimentalPaidContentRemoval()) { blockList.add("paid_content_overlay"); } @@ -121,12 +145,6 @@ public static boolean containsAd(String value, ByteBuffer buffer) { if (isExperimentalCommentsRemoval()) { blockList.add("comments_composite_entry_point"); } - if (isExperimentalCompactMovieRemoval()) { - blockList.add("compact_movie"); - } - if (isExperimentalHorizontalMovieShelfRemoval()) { - blockList.add("horizontal_movie_shelf"); - } if (isInFeedSurvey()) { blockList.add("in_feed_survey"); } @@ -136,53 +154,45 @@ public static boolean containsAd(String value, ByteBuffer buffer) { if (isCommunityGuidelines()) { blockList.add("community_guidelines"); } - if (!value.contains("related_video_with_context") || indexOf(buffer.array(), endRelatedPageAd) <= 0) { - for (String s : blockList) { - if (value.contains(s)) { - if (XGlobals.debug) { - Log.d("TemplateBlocked", value); - } - return true; - } - } - if (!XGlobals.debug) { - return false; - } - if (value.contains("related_video_with_context")) { - Log.d("Template", value + " | " + bytesToHex(buffer.array())); - return false; - } - Log.d("Template", value); - return false; + + if (containsAny(value, + "home_video_with_context", + "related_video_with_context", + "search_video_with_context", + "menu", + "root", + "-count", + "-space", + "-button" + )) { + if (XGlobals.debug) Log.d("TemplateBlocked", value); + return true; + } + + if (blockList.stream().anyMatch(value::contains)) { + if (XGlobals.debug) Log.d("TemplateBlocked", value); + return true; } - if (XGlobals.debug) { - Log.d("TemplateBlocked", value); + + if (!XGlobals.debug) return false; + if (value.contains("related_video_with_context")) { + Log.d("Template", value + " | " + bytesToHex(buffer.array())); + return false; } - return true; - } catch (Exception ex) { + Log.d("Template", value); + return false; + } catch ( + Exception ex) { Log.e("Template", ex.getMessage(), ex); return false; } - } - public static int indexOf(byte[] array, byte[] target) { - if (target.length == 0) { - return 0; - } + } - for (int i = 0; i < array.length - target.length + 1; i++) { - boolean targetFound = true; - for (int j = 0; j < target.length; j++) { - if (array[i + j] != target[j]) { - targetFound = false; - break; - } - } - if (targetFound) { - return i; - } - } - return -1; + private static boolean containsAny(String value, String... targets) { + for (String string : targets) + if (value.contains(string)) return true; + return false; } private static String bytesToHex(byte[] bytes) { diff --git a/build.gradle b/build.gradle index c005e8f78f..0a5a862695 100644 --- a/build.gradle +++ b/build.gradle @@ -5,11 +5,12 @@ buildscript { mavenCentral() } dependencies { - classpath 'com.android.tools.build:gradle:7.1.3' + classpath 'com.android.tools.build:gradle:7.2.1' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } + } allprojects { diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 25999d569e..f0d3aed97f 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ #Mon Jun 07 19:51:48 CEST 2021 distributionBase=GRADLE_USER_HOME -distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip distributionPath=wrapper/dists zipStorePath=wrapper/dists zipStoreBase=GRADLE_USER_HOME diff --git a/settings.gradle b/settings.gradle index 20a37f58da..9ad70f4fba 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,2 +1,2 @@ include ':app' -rootProject.name = "sb" \ No newline at end of file +rootProject.name = "integrations" \ No newline at end of file