Skip to content

Commit

Permalink
refactor(YouTube - Litho filter): Simplify filtering callbacks (ReVan…
Browse files Browse the repository at this point in the history
  • Loading branch information
LisoUseInAIKyrios authored Dec 12, 2023
1 parent 56cf001 commit aedb3ed
Show file tree
Hide file tree
Showing 13 changed files with 197 additions and 147 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package app.revanced.integrations.patches.components;


import android.view.View;

import androidx.annotation.Nullable;
Expand All @@ -9,7 +8,7 @@
import app.revanced.integrations.utils.ReVancedUtils;
import app.revanced.integrations.utils.StringTrieSearch;


@SuppressWarnings("unused")
public final class AdsFilter extends Filter {
private final StringTrieSearch exceptions = new StringTrieSearch();
private final StringFilterGroup shoppingLinks;
Expand All @@ -23,6 +22,16 @@ public AdsFilter() {
"library_recent_shelf"
);

// Identifiers.

final var carouselAd = new StringFilterGroup(
SettingsEnum.HIDE_GENERAL_ADS,
"carousel_ad"
);
addIdentifierCallbacks(carouselAd);

// Paths.

final var buttonedAd = new StringFilterGroup(
SettingsEnum.HIDE_BUTTONED_ADS,
"_buttoned_layout",
Expand Down Expand Up @@ -61,11 +70,6 @@ public AdsFilter() {
"offer_module_root"
);

final var carouselAd = new StringFilterGroup(
SettingsEnum.HIDE_GENERAL_ADS,
"carousel_ad"
);

final var viewProducts = new StringFilterGroup(
SettingsEnum.HIDE_PRODUCTS_BANNER,
"product_item",
Expand All @@ -92,7 +96,7 @@ public AdsFilter() {
"cta_shelf_card"
);

this.pathFilterGroupList.addAll(
addPathCallbacks(
generalAds,
buttonedAd,
merchandise,
Expand All @@ -102,20 +106,19 @@ public AdsFilter() {
shoppingLinks,
movieAds
);
this.identifierFilterGroupList.addAll(carouselAd);
}

@Override
public boolean isFiltered(@Nullable String identifier, String path, byte[] protobufBufferArray,
FilterGroupList matchedList, FilterGroup matchedGroup, int matchedIndex) {
StringFilterGroup matchedGroup, FilterContentType contentType, int contentIndex) {
if (exceptions.matches(path))
return false;

// Check for the index because of likelihood of false positives.
if (matchedGroup == shoppingLinks && matchedIndex != 0)
if (matchedGroup == shoppingLinks && contentIndex != 0)
return false;

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

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import app.revanced.integrations.settings.SettingsEnum;

@SuppressWarnings("unused")
@RequiresApi(api = Build.VERSION_CODES.N)
final class ButtonsFilter extends Filter {
private static final String VIDEO_ACTION_BAR_PATH = "video_action_bar.eml";
Expand All @@ -20,14 +21,14 @@ public ButtonsFilter() {
null,
VIDEO_ACTION_BAR_PATH
);
identifierFilterGroupList.addAll(actionBarGroup);
addIdentifierCallbacks(actionBarGroup);


bufferFilterPathGroup = new StringFilterGroup(
null,
"|CellType|CollectionType|CellType|ContainerType|button.eml|"
);
pathFilterGroupList.addAll(
addPathCallbacks(
new StringFilterGroup(
SettingsEnum.HIDE_LIKE_DISLIKE_BUTTON,
"|segmented_like_dislike_button"
Expand All @@ -48,41 +49,41 @@ public ButtonsFilter() {
);

bufferButtonsGroupList.addAll(
new ByteArrayAsStringFilterGroup(
new ByteArrayFilterGroup(
SettingsEnum.HIDE_LIVE_CHAT_BUTTON,
"yt_outline_message_bubble_overlap"
),
new ByteArrayAsStringFilterGroup(
new ByteArrayFilterGroup(
SettingsEnum.HIDE_REPORT_BUTTON,
"yt_outline_flag"
),
new ByteArrayAsStringFilterGroup(
new ByteArrayFilterGroup(
SettingsEnum.HIDE_SHARE_BUTTON,
"yt_outline_share"
),
new ByteArrayAsStringFilterGroup(
new ByteArrayFilterGroup(
SettingsEnum.HIDE_REMIX_BUTTON,
"yt_outline_youtube_shorts_plus"
),
// Check for clip button both here and using a path filter,
// as there's a chance the path is a generic action button and won't contain 'clip_button'
new ByteArrayAsStringFilterGroup(
new ByteArrayFilterGroup(
SettingsEnum.HIDE_CLIP_BUTTON,
"yt_outline_scissors"
),
new ByteArrayAsStringFilterGroup(
new ByteArrayFilterGroup(
SettingsEnum.HIDE_SHOP_BUTTON,
"yt_outline_bag"
),
new ByteArrayAsStringFilterGroup(
new ByteArrayFilterGroup(
SettingsEnum.HIDE_THANKS_BUTTON,
"yt_outline_dollar_sign_heart"
)
);
}

private boolean isEveryFilterGroupEnabled() {
for (var group : pathFilterGroupList)
for (var group : pathCallbacks)
if (!group.isEnabled()) return false;

for (var group : bufferButtonsGroupList)
Expand All @@ -93,7 +94,7 @@ private boolean isEveryFilterGroupEnabled() {

@Override
public boolean isFiltered(@Nullable String identifier, String path, byte[] protobufBufferArray,
FilterGroupList matchedList, FilterGroup matchedGroup, int matchedIndex) {
StringFilterGroup matchedGroup, FilterContentType contentType, int contentIndex) {
// If the current matched group is the action bar group,
// in case every filter group is enabled, hide the action bar.
if (matchedGroup == actionBarGroup) {
Expand All @@ -109,6 +110,6 @@ public boolean isFiltered(@Nullable String identifier, String path, byte[] proto
if (!bufferButtonsGroupList.check(protobufBufferArray).isFiltered()) return false;
}

return super.isFiltered(identifier, path, protobufBufferArray, matchedList, matchedGroup, matchedIndex);
return super.isFiltered(identifier, path, protobufBufferArray, matchedGroup, contentType, contentIndex);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import app.revanced.integrations.settings.SettingsEnum;

@SuppressWarnings("unused")
final class CommentsFilter extends Filter {

public CommentsFilter() {
Expand All @@ -18,7 +19,7 @@ public CommentsFilter() {
"comments_entry_point_simplebox"
);

this.pathFilterGroupList.addAll(
addPathCallbacks(
comments,
previewComment
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import app.revanced.integrations.settings.SettingsEnum;
import app.revanced.integrations.utils.StringTrieSearch;

@SuppressWarnings("unused")
final class DescriptionComponentsFilter extends Filter {

private final StringTrieSearch exceptions = new StringTrieSearch();
Expand Down Expand Up @@ -48,7 +49,7 @@ public DescriptionComponentsFilter() {
"transcript_section"
);

pathFilterGroupList.addAll(
addPathCallbacks(
chapterSection,
infoCardsSection,
gameSection,
Expand All @@ -61,9 +62,9 @@ public DescriptionComponentsFilter() {

@Override
boolean isFiltered(@Nullable String identifier, String path, byte[] protobufBufferArray,
FilterGroupList matchedList, FilterGroup matchedGroup, int matchedIndex) {
StringFilterGroup matchedGroup, FilterContentType contentType, int contentIndex) {
if (exceptions.matches(path)) return false;

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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import app.revanced.integrations.settings.SettingsEnum;

@SuppressWarnings("unused")
public final class HideInfoCardsFilterPatch extends Filter {

public HideInfoCardsFilterPatch() {
identifierFilterGroupList.addAll(
addIdentifierCallbacks(
new StringFilterGroup(
SettingsEnum.HIDE_INFO_CARDS,
"info_card_teaser_overlay.eml"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
package app.revanced.integrations.patches.components;


import android.os.Build;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

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

@SuppressWarnings("unused")
@RequiresApi(api = Build.VERSION_CODES.N)
public final class LayoutComponentsFilter extends Filter {
private final StringTrieSearch exceptions = new StringTrieSearch();
private static final StringTrieSearch mixPlaylistsExceptions = new StringTrieSearch();
private static final ByteArrayAsStringFilterGroup mixPlaylistsExceptions2 = new ByteArrayAsStringFilterGroup(
private static final ByteArrayFilterGroup mixPlaylistsExceptions2 = new ByteArrayFilterGroup(
null,
"cell_description_body"
);
private final CustomFilterGroup custom;

private static final ByteArrayAsStringFilterGroup mixPlaylists = new ByteArrayAsStringFilterGroup(
private static final ByteArrayFilterGroup mixPlaylists = new ByteArrayFilterGroup(
SettingsEnum.HIDE_MIX_PLAYLISTS,
"&list="
);
Expand All @@ -44,6 +46,25 @@ public LayoutComponentsFilter() {
"library_recent_shelf"
);

// Identifiers.

final var graySeparator = new StringFilterGroup(
SettingsEnum.HIDE_GRAY_SEPARATOR,
"cell_divider" // layout residue (gray line above the buttoned ad),
);

final var chipsShelf = new StringFilterGroup(
SettingsEnum.HIDE_CHIPS_SHELF,
"chips_shelf"
);

addIdentifierCallbacks(
graySeparator,
chipsShelf
);

// Paths.

custom = new CustomFilterGroup(
SettingsEnum.CUSTOM_FILTER,
SettingsEnum.CUSTOM_FILTER_STRINGS
Expand Down Expand Up @@ -155,10 +176,6 @@ public LayoutComponentsFilter() {
"image_shelf"
);

final var graySeparator = new StringFilterGroup(
SettingsEnum.HIDE_GRAY_SEPARATOR,
"cell_divider" // layout residue (gray line above the buttoned ad),
);

final var timedReactions = new StringFilterGroup(
SettingsEnum.HIDE_TIMED_REACTIONS,
Expand All @@ -181,11 +198,6 @@ public LayoutComponentsFilter() {
"compact_sponsor_button"
);

final var chipsShelf = new StringFilterGroup(
SettingsEnum.HIDE_CHIPS_SHELF,
"chips_shelf"
);

final var channelWatermark = new StringFilterGroup(
SettingsEnum.HIDE_VIDEO_CHANNEL_WATERMARK,
"featured_channel_watermark_overlay"
Expand All @@ -196,21 +208,22 @@ public LayoutComponentsFilter() {
"mixed_content_shelf"
);

this.pathFilterGroupList.addAll(
addPathCallbacks(
custom,
expandableMetadata,
inFeedSurvey,
notifyMe,
channelBar,
communityPosts,
paidContent,
latestPosts,
channelWatermark,
communityGuidelines,
quickActions,
expandableMetadata,
relatedVideos,
compactBanner,
inFeedSurvey,
joinMembership,
medicalPanel,
notifyMe,
videoQualityMenuFooter,
infoPanel,
subscribersCommunityGuidelines,
Expand All @@ -220,32 +233,26 @@ public LayoutComponentsFilter() {
timedReactions,
imageShelf,
channelMemberShelf,
forYouShelf,
custom
);

this.identifierFilterGroupList.addAll(
graySeparator,
chipsShelf
forYouShelf
);
}

@Override
public boolean isFiltered(@Nullable String identifier, String path, byte[] protobufBufferArray,
FilterGroupList matchedList, FilterGroup matchedGroup, int matchedIndex) {
StringFilterGroup matchedGroup, FilterContentType contentType, int contentIndex) {

// The groups are excluded from the filter due to the exceptions list below.
// Filter them separately here.
if (matchedGroup == notifyMe || matchedGroup == inFeedSurvey || matchedGroup == expandableMetadata)
return super.isFiltered(identifier, path, protobufBufferArray, matchedList, matchedGroup, matchedIndex);
return super.isFiltered(identifier, path, protobufBufferArray, matchedGroup, contentType, contentIndex);

if (matchedGroup != custom && exceptions.matches(path))
return false; // Exceptions are not filtered.

// TODO: This also hides the feed Shorts shelf header
if (matchedGroup == searchResultShelfHeader && matchedIndex != 0) return false;
if (matchedGroup == searchResultShelfHeader && contentIndex != 0) return false;

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

/**
Expand Down
Loading

0 comments on commit aedb3ed

Please sign in to comment.