feat: make containsAd more efficient & add new values

This commit is contained in:
oSumAtrIX 2022-06-11 21:37:26 +02:00
parent 468c72e2a5
commit 8ff5f0e68d
No known key found for this signature in database
GPG Key ID: A9B3094ACDB604B4
6 changed files with 80 additions and 71 deletions

View File

@ -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"
}

View File

@ -1,7 +1,4 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="vanced.integrations">
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
</manifest>

View File

@ -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.Helpers.SharedPrefs;
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 class LithoAdRemoval {
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 class LithoAdRemoval {
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 class LithoAdRemoval {
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<String> blockList = new ArrayList<>();
List<String> bufferBlockList = new ArrayList<>();
if (isExperimentalAdRemoval()) {
blockList.add("_ad");
blockList.add("ad_badge");
@ -93,16 +101,32 @@ public class LithoAdRemoval {
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 class LithoAdRemoval {
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 class LithoAdRemoval {
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);
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) return false;
if (value.contains("related_video_with_context")) {
Log.d("Template", value + " | " + bytesToHex(buffer.array()));
return false;
}
if (XGlobals.debug) {
Log.d("TemplateBlocked", value);
}
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) {

View File

@ -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 {

View File

@ -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

View File

@ -1,2 +1,2 @@
include ':app'
rootProject.name = "sb"
rootProject.name = "integrations"