Skip to content

Commit

Permalink
perf(YouTube): Filter litho components using prefix tree (#447)
Browse files Browse the repository at this point in the history
Co-authored-by: oSumAtrIX <johan.melkonyan1@web.de>
  • Loading branch information
LisoUseInAIKyrios and oSumAtrIX committed Aug 1, 2023
1 parent 0207496 commit 18f2900
Show file tree
Hide file tree
Showing 13 changed files with 790 additions and 211 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@


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;


public final class AdsFilter extends Filter {
private final String[] exceptions;
private final StringTrieSearch exceptions = new StringTrieSearch();

public AdsFilter() {
exceptions = new String[]{
exceptions.addPatterns(
"home_video_with_context", // Don't filter anything in the home page video component.
"related_video_with_context", // Don't filter anything in the related video component.
"comment_thread", // Don't filter anything in the comments.
"|comment.", // Don't filter anything in the comments replies.
"library_recent_shelf",
};
"library_recent_shelf"
);

final var buttonedAd = new StringFilterGroup(
SettingsEnum.HIDE_BUTTONED_ADS,
Expand Down Expand Up @@ -95,11 +99,12 @@ public AdsFilter() {
}

@Override
public boolean isFiltered(final String path, final String identifier, final byte[] _protobufBufferArray) {
if (ReVancedUtils.containsAny(path, exceptions))
public boolean isFiltered(String path, @Nullable String identifier, byte[] protobufBufferArray,
FilterGroupList matchedList, FilterGroup matchedGroup, int matchedIndex) {
if (exceptions.matches(path))
return false;

return super.isFiltered(path, identifier, _protobufBufferArray);
return super.isFiltered(path, identifier, protobufBufferArray, matchedList, matchedGroup, matchedIndex);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package app.revanced.integrations.patches.components;

import androidx.annotation.Nullable;

import app.revanced.integrations.settings.SettingsEnum;

final class ButtonsFilter extends Filter {
Expand Down Expand Up @@ -33,7 +35,8 @@ public ButtonsFilter() {
SettingsEnum.HIDE_ACTION_BUTTONS,
"ContainerType|video_action_button",
"|CellType|CollectionType|CellType|ContainerType|button.eml|"
)
),
actionBarRule
);
}

Expand All @@ -45,10 +48,12 @@ private boolean isEveryFilterGroupEnabled() {
}

@Override
public boolean isFiltered(final String path, final String identifier, final byte[] _protobufBufferArray) {
if (isEveryFilterGroupEnabled())
if (actionBarRule.check(identifier).isFiltered()) return true;
public boolean isFiltered(String path, @Nullable String identifier, byte[] protobufBufferArray,
FilterGroupList matchedList, FilterGroup matchedGroup, int matchedIndex) {
if (matchedGroup == actionBarRule) {
return isEveryFilterGroupEnabled();
}

return super.isFiltered(path, identifier, _protobufBufferArray);
return super.isFiltered(path, identifier, protobufBufferArray, matchedList, matchedGroup, matchedIndex);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@


import android.os.Build;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import app.revanced.integrations.settings.SettingsEnum;
import app.revanced.integrations.utils.ReVancedUtils;

import app.revanced.integrations.settings.SettingsEnum;
import app.revanced.integrations.utils.StringTrieSearch;

@RequiresApi(api = Build.VERSION_CODES.N)
public final class LayoutComponentsFilter extends Filter {
private final String[] exceptions;

private final StringTrieSearch exceptions = new StringTrieSearch();
private final CustomFilterGroup custom;

private static final ByteArrayAsStringFilterGroup mixPlaylists = new ByteArrayAsStringFilterGroup(
Expand All @@ -20,13 +21,13 @@ public final class LayoutComponentsFilter extends Filter {

@RequiresApi(api = Build.VERSION_CODES.N)
public LayoutComponentsFilter() {
exceptions = new String[]{
exceptions.addPatterns(
"home_video_with_context",
"related_video_with_context",
"comment_thread", // skip filtering anything in the comments
"|comment.", // skip filtering anything in the comments replies
"library_recent_shelf",
};
"library_recent_shelf"
);

custom = new CustomFilterGroup(
SettingsEnum.CUSTOM_FILTER,
Expand Down Expand Up @@ -160,7 +161,8 @@ public LayoutComponentsFilter() {
artistCard,
imageShelf,
subscribersCommunityGuidelines,
channelMemberShelf
channelMemberShelf,
custom
);

this.identifierFilterGroups.addAll(
Expand All @@ -170,19 +172,21 @@ public LayoutComponentsFilter() {
}

@Override
public boolean isFiltered(final String path, final String identifier, final byte[] _protobufBufferArray) {
if (custom.isEnabled() && custom.check(path).isFiltered())
return true;

if (ReVancedUtils.containsAny(path, exceptions))
public boolean isFiltered(String path, @Nullable String identifier, byte[] protobufBufferArray,
FilterGroupList matchedList, FilterGroup matchedGroup, int matchedIndex) {
if (matchedGroup != custom && exceptions.matches(path))
return false; // Exceptions are not filtered.

return super.isFiltered(path, identifier, _protobufBufferArray);
return super.isFiltered(path, identifier, protobufBufferArray, matchedList, matchedGroup, matchedIndex);
}


// Called from a different place then the other filters.
/**
* Injection point.
*
* Called from a different place then the other filters.
*/
public static boolean filterMixPlaylists(final byte[] bytes) {
return mixPlaylists.isEnabled() && mixPlaylists.check(bytes).isFiltered();
return mixPlaylists.check(bytes).isFiltered();
}
}
Loading

0 comments on commit 18f2900

Please sign in to comment.