Skip to content

Commit

Permalink
chore: merge branch dev to main (#431)
Browse files Browse the repository at this point in the history
  • Loading branch information
oSumAtrIX authored Jul 8, 2023
2 parents ebd215d + f6b1644 commit 9bfdff3
Show file tree
Hide file tree
Showing 13 changed files with 250 additions and 297 deletions.
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@


import android.os.Build;
import android.view.View;
import androidx.annotation.RequiresApi;
import app.revanced.integrations.settings.SettingsEnum;
import app.revanced.integrations.utils.ReVancedUtils;


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

private final CustomFilterGroup custom;

// region Mix playlists
private final ByteArrayAsStringFilterGroup mixPlaylists;
private final ByteArrayAsStringFilterGroup imageHosting;

// endregion
private static final ByteArrayAsStringFilterGroup mixPlaylists = new ByteArrayAsStringFilterGroup(
SettingsEnum.HIDE_MIX_PLAYLISTS,
"&list="
);

@RequiresApi(api = Build.VERSION_CODES.N)
public LayoutComponentsFilter() {
Expand Down Expand Up @@ -137,21 +136,6 @@ public LayoutComponentsFilter() {
"cell_divider" // layout residue (gray line above the buttoned ad),
);

// region Mix playlists

mixPlaylists = new ByteArrayAsStringFilterGroup(
SettingsEnum.HIDE_MIX_PLAYLISTS,
"&list=",
"YouTube Music"
);

imageHosting = new ByteArrayAsStringFilterGroup(
SettingsEnum.HIDE_MIX_PLAYLISTS, // Unused
"ggpht.com"
);

// endregion

this.pathFilterGroups.addAll(
channelBar,
communityPosts,
Expand All @@ -174,27 +158,7 @@ public LayoutComponentsFilter() {
channelMemberShelf
);

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

this.identifierFilterGroups.addAll(
graySeparator,
carouselAd
);
}

private boolean isMixPlaylistFiltered(final byte[] _protobufBufferArray) {
if (!mixPlaylists.isEnabled()) return false;

// Two checks are required to prevent false positives.

// First check if the current buffer potentially contains a mix playlist.
if (!mixPlaylists.check(_protobufBufferArray).isFiltered()) return false;

// Ensure that the buffer actually contains a mix playlist.
return imageHosting.check(_protobufBufferArray).isFiltered();
this.identifierFilterGroups.addAll(graySeparator);
}

@Override
Expand All @@ -205,18 +169,12 @@ public boolean isFiltered(final String path, final String identifier, final byte
if (ReVancedUtils.containsAny(path, exceptions))
return false; // Exceptions are not filtered.

if (super.isFiltered(path, identifier, _protobufBufferArray))
return true;

return isMixPlaylistFiltered(_protobufBufferArray);
return super.isFiltered(path, identifier, _protobufBufferArray);
}

/**
* Hide the view, which shows ads in the homepage.
*
* @param view The view, which shows ads.
*/
public static void hideAdAttributionView(View view) {
ReVancedUtils.hideViewBy1dpUnderCondition(SettingsEnum.HIDE_GENERAL_ADS, view);

// Called from a different place then the other filters.
public static boolean filterMixPlaylists(final byte[] bytes) {
return mixPlaylists.isEnabled() && mixPlaylists.check(bytes).isFiltered();
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package app.revanced.integrations.patches.components;

import android.os.Build;

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

import java.nio.ByteBuffer;
import java.util.ArrayList;
Expand All @@ -14,6 +12,10 @@
import java.util.Spliterator;
import java.util.function.Consumer;

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

abstract class FilterGroup<T> {
final static class FilterGroupResult {
private final boolean filtered;
Expand Down Expand Up @@ -49,7 +51,7 @@ public FilterGroup(final SettingsEnum setting, final T... filters) {
}

public boolean isEnabled() {
return setting.getBoolean();
return setting == null || setting.getBoolean();
}

public abstract FilterGroupResult check(final T stack);
Expand Down Expand Up @@ -208,7 +210,7 @@ abstract class Filter {

/**
* Check if the given path, identifier or protobuf buffer is filtered by any
* {@link FilterGroup}.
* {@link FilterGroup}. Method is called off the main thread.
*
* @return True if filtered, false otherwise.
*/
Expand Down Expand Up @@ -239,9 +241,12 @@ public final class LithoFilterPatch {
new DummyFilter() // Replaced by patch.
};

/**
* Injection point. Called off the main thread.
*/
@SuppressWarnings("unused")
public static boolean filter(final StringBuilder pathBuilder, final String identifier,
final ByteBuffer protobufBuffer) {
final ByteBuffer protobufBuffer) {
// TODO: Maybe this can be moved to the Filter class, to prevent unnecessary
// string creation
// because some filters might not need the path.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package app.revanced.integrations.patches.components;

import app.revanced.integrations.settings.SettingsEnum;

// Abuse LithoFilter for OldVideoQualityMenuPatch.
public final class VideoQualityMenuFilterPatch extends Filter {
// Must be volatile or synchronized, as litho filtering runs off main thread and this field is then access from the main thread.
public static volatile boolean isVideoQualityMenuVisible;

public VideoQualityMenuFilterPatch() {
pathFilterGroups.addAll(new StringFilterGroup(
SettingsEnum.SHOW_OLD_VIDEO_QUALITY_MENU,
"quick_quality_sheet_content.eml-js"
));
}

@Override
boolean isFiltered(final String path, final String identifier, final byte[] protobufBufferArray) {
isVideoQualityMenuVisible = super.isFiltered(path, identifier, protobufBufferArray);

return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package app.revanced.integrations.patches.components;

// Abuse LithoFilter for CustomVideoSpeedPatch.
public final class VideoSpeedMenuFilterPatch extends Filter {
// Must be volatile or synchronized, as litho filtering runs off main thread and this field is then access from the main thread.
public static volatile boolean isVideoSpeedMenuVisible;

public VideoSpeedMenuFilterPatch() {
pathFilterGroups.addAll(new StringFilterGroup(
null,
"playback_speed_sheet_content.eml-js"
));
}

@Override
boolean isFiltered(final String path, final String identifier, final byte[] protobufBufferArray) {
isVideoSpeedMenuVisible = super.isFiltered(path, identifier, protobufBufferArray);

return false;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package app.revanced.integrations.patches.playback.quality;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
import android.widget.ListView;

import androidx.annotation.NonNull;

import app.revanced.integrations.patches.components.VideoQualityMenuFilterPatch;
import app.revanced.integrations.settings.SettingsEnum;
import app.revanced.integrations.utils.LogHelper;
import com.facebook.litho.ComponentHost;
import kotlin.Deprecated;

// This patch contains the logic to show the old video quality menu.
// Two methods are required, because the quality menu is a RecyclerView in the new YouTube version
// and a ListView in the old one.
public final class OldVideoQualityMenuPatch {

public static void onFlyoutMenuCreate(final LinearLayout linearLayout) {
if (!SettingsEnum.SHOW_OLD_VIDEO_QUALITY_MENU.getBoolean()) return;

// The quality menu is a RecyclerView with 3 children. The third child is the "Advanced" quality menu.
addRecyclerListener(linearLayout, 3, 2, recyclerView -> {
// Check if the current view is the quality menu.
if (VideoQualityMenuFilterPatch.isVideoQualityMenuVisible) {// Hide the video quality menu.
linearLayout.setVisibility(View.GONE);

// Click the "Advanced" quality menu to show the "old" quality menu.
((ComponentHost) recyclerView.getChildAt(0)).getChildAt(3).performClick();
LogHelper.printDebug(() -> "Advanced quality menu in new type of quality menu clicked");
}
});
}

public static void addRecyclerListener(@NonNull LinearLayout linearLayout,
int expectedLayoutChildCount, int recyclerViewIndex,
@NonNull RecyclerViewGlobalLayoutListener listener) {
if (linearLayout.getChildCount() != expectedLayoutChildCount) return;

var layoutChild = linearLayout.getChildAt(recyclerViewIndex);
if (!(layoutChild instanceof RecyclerView)) return;
final var recyclerView = (RecyclerView) layoutChild;

recyclerView.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
try {
listener.recyclerOnGlobalLayout(recyclerView);
} catch (Exception ex) {
LogHelper.printException(() -> "addRecyclerListener failure", ex);
} finally {
// Remove the listener because it will be added again.
recyclerView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
}
}
);
}

public interface RecyclerViewGlobalLayoutListener {
void recyclerOnGlobalLayout(@NonNull RecyclerView recyclerView);
}

@Deprecated(message = "This patch is deprecated because the quality menu is not a ListView anymore")
public static void showOldVideoQualityMenu(final ListView listView) {
if (!SettingsEnum.SHOW_OLD_VIDEO_QUALITY_MENU.getBoolean()) return;

listView.setOnHierarchyChangeListener(new ViewGroup.OnHierarchyChangeListener() {
@Override
public void onChildViewAdded(View parent, View child) {
LogHelper.printDebug(() -> "Added listener to old type of quality menu");

parent.setVisibility(View.GONE);

final var indexOfAdvancedQualityMenuItem = 4;
if (listView.indexOfChild(child) != indexOfAdvancedQualityMenuItem) return;

LogHelper.printDebug(() -> "Found advanced menu item in old type of quality menu");

final var qualityItemMenuPosition = 4;
listView.performItemClick(null, qualityItemMenuPosition, 0);
}

@Override
public void onChildViewRemoved(View parent, View child) {
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,8 @@ public class RememberVideoQualityPatch {
private static List<Integer> videoQualities;

private static void changeDefaultQuality(int defaultQuality) {
NetworkType networkType = ReVancedUtils.getNetworkType();
if (networkType == NetworkType.NONE) {
ReVancedUtils.showToastShort("No internet connection");
return;
}
String networkTypeMessage;
if (networkType == NetworkType.MOBILE) {
if (ReVancedUtils.getNetworkType() == NetworkType.MOBILE) {
mobileQualitySetting.saveValue(defaultQuality);
networkTypeMessage = "mobile";
} else {
Expand Down Expand Up @@ -139,15 +134,24 @@ public static int setVideoQuality(Object[] qualities, final int originalQualityI
}

/**
* Injection point.
* Injection point. Old quality menu.
*/
public static void userChangedQuality(int selectedQuality) {
public static void userChangedQuality(int selectedQualityIndex) {
if (!SettingsEnum.REMEMBER_VIDEO_QUALITY_LAST_SELECTED.getBoolean()) return;

userSelectedQualityIndex = selectedQuality;
userSelectedQualityIndex = selectedQualityIndex;
userChangedDefaultQuality = true;
}

/**
* Injection point. New quality menu.
*/
public static void userChangedQualityInNewFlyout(int selectedQuality) {
if (!SettingsEnum.REMEMBER_VIDEO_QUALITY_LAST_SELECTED.getBoolean()) return;

changeDefaultQuality(selectedQuality); // Quality is human readable resolution (ie: 1080).
}

/**
* Injection point.
*/
Expand Down
Loading

0 comments on commit 9bfdff3

Please sign in to comment.