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

Commit

Permalink
feat: make containsAd more efficient & add new values
Browse files Browse the repository at this point in the history
  • Loading branch information
oSumAtrIX committed Jun 11, 2022
1 parent 468c72e commit 8ff5f0e
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 71 deletions.
5 changes: 3 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
}

5 changes: 1 addition & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -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>
134 changes: 72 additions & 62 deletions app/src/main/java/fi/razerman/youtube/litho/LithoAdRemoval.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}
Expand Down Expand Up @@ -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() {
Expand All @@ -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);
}
Expand All @@ -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<String> blockList = new ArrayList<>();
List<String> bufferBlockList = new ArrayList<>();

if (isExperimentalAdRemoval()) {
blockList.add("_ad");
blockList.add("ad_badge");
Expand All @@ -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");
}
Expand All @@ -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");
}
Expand All @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
include ':app'
rootProject.name = "sb"
rootProject.name = "integrations"

0 comments on commit 8ff5f0e

Please sign in to comment.