From 978233843d2dcf9a8a0ddf9c9517d25c973e8689 Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Wed, 15 May 2024 22:16:30 +0400 Subject: [PATCH] fix(YouTube - Hide Shorts components): Hide old layout like/dislike buttons without leaving empty space Also change hide by 1dp to 0dp, and consolidate two methods together --- .../revanced/integrations/shared/Utils.java | 29 ++++++++++++----- .../youtube/patches/components/AdsFilter.java | 2 +- .../patches/components/ShortsFilter.java | 31 ++++++------------- 3 files changed, 31 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/app/revanced/integrations/shared/Utils.java b/app/src/main/java/app/revanced/integrations/shared/Utils.java index 09e3dcba36..311e2a6279 100644 --- a/app/src/main/java/app/revanced/integrations/shared/Utils.java +++ b/app/src/main/java/app/revanced/integrations/shared/Utils.java @@ -15,6 +15,7 @@ import android.preference.PreferenceScreen; import android.view.View; import android.view.ViewGroup; +import android.view.ViewParent; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.FrameLayout; @@ -94,7 +95,7 @@ public static String getAppVersionName() { * @param condition The setting to check for hiding the view. * @param view The view to hide. */ - public static void hideViewBy1dpUnderCondition(BooleanSetting condition, View view) { + public static void hideViewBy0dpUnderCondition(BooleanSetting condition, View view) { if (!condition.get()) return; Logger.printDebug(() -> "Hiding view with setting: " + condition); @@ -116,6 +117,15 @@ public static void hideViewUnderCondition(BooleanSetting condition, View view) { view.setVisibility(View.GONE); } + public static void removeViewFromParentUnderConditions(BooleanSetting setting, View view) { + if (setting.get()) { + ViewParent parent = view.getParent(); + if (parent instanceof ViewGroup) { + ((ViewGroup) parent).removeView(view); + } + } + } + /** * General purpose pool for network calls and other background tasks. * All tasks run at max thread priority. @@ -412,27 +422,30 @@ public static NetworkType getNetworkType() { } /** - * Hide a view by setting its layout params to 1x1 + * Hide a view by setting its layout params to 0x0 * @param view The view to hide. */ public static void hideViewByLayoutParams(View view) { if (view instanceof LinearLayout) { - LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(1, 1); + LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0, 0); view.setLayoutParams(layoutParams); } else if (view instanceof FrameLayout) { - FrameLayout.LayoutParams layoutParams2 = new FrameLayout.LayoutParams(1, 1); + FrameLayout.LayoutParams layoutParams2 = new FrameLayout.LayoutParams(0, 0); view.setLayoutParams(layoutParams2); } else if (view instanceof RelativeLayout) { - RelativeLayout.LayoutParams layoutParams3 = new RelativeLayout.LayoutParams(1, 1); + RelativeLayout.LayoutParams layoutParams3 = new RelativeLayout.LayoutParams(0, 0); view.setLayoutParams(layoutParams3); } else if (view instanceof Toolbar) { - Toolbar.LayoutParams layoutParams4 = new Toolbar.LayoutParams(1, 1); + Toolbar.LayoutParams layoutParams4 = new Toolbar.LayoutParams(0, 0); view.setLayoutParams(layoutParams4); } else if (view instanceof ViewGroup) { - ViewGroup.LayoutParams layoutParams5 = new ViewGroup.LayoutParams(1, 1); + ViewGroup.LayoutParams layoutParams5 = new ViewGroup.LayoutParams(0, 0); view.setLayoutParams(layoutParams5); } else { - Logger.printDebug(() -> "Hidden view with id " + view.getId()); + ViewGroup.LayoutParams params = view.getLayoutParams(); + params.width = 0; + params.height = 0; + view.setLayoutParams(params); } } diff --git a/app/src/main/java/app/revanced/integrations/youtube/patches/components/AdsFilter.java b/app/src/main/java/app/revanced/integrations/youtube/patches/components/AdsFilter.java index 3e817b81d2..e7559b6565 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/patches/components/AdsFilter.java +++ b/app/src/main/java/app/revanced/integrations/youtube/patches/components/AdsFilter.java @@ -175,7 +175,7 @@ boolean isFiltered(@Nullable String identifier, String path, byte[] protobufBuff * @param view The view, which shows ads. */ public static void hideAdAttributionView(View view) { - Utils.hideViewBy1dpUnderCondition(Settings.HIDE_GENERAL_ADS, view); + Utils.hideViewBy0dpUnderCondition(Settings.HIDE_GENERAL_ADS, view); } /** diff --git a/app/src/main/java/app/revanced/integrations/youtube/patches/components/ShortsFilter.java b/app/src/main/java/app/revanced/integrations/youtube/patches/components/ShortsFilter.java index 3303d1cc26..6ca3b9cf29 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/patches/components/ShortsFilter.java +++ b/app/src/main/java/app/revanced/integrations/youtube/patches/components/ShortsFilter.java @@ -1,19 +1,16 @@ package app.revanced.integrations.youtube.patches.components; import static app.revanced.integrations.shared.Utils.hideViewUnderCondition; +import static app.revanced.integrations.shared.Utils.removeViewFromParentUnderConditions; import static app.revanced.integrations.youtube.shared.NavigationBar.NavigationButton; import android.view.View; -import android.view.ViewGroup; -import android.widget.TextView; import androidx.annotation.Nullable; import com.google.android.libraries.youtube.rendering.ui.pivotbar.PivotBar; -import app.revanced.integrations.shared.Logger; import app.revanced.integrations.shared.Utils; -import app.revanced.integrations.shared.settings.BooleanSetting; import app.revanced.integrations.youtube.settings.Settings; import app.revanced.integrations.youtube.shared.NavigationBar; import app.revanced.integrations.youtube.shared.PlayerType; @@ -317,29 +314,19 @@ public static void hideShortsShelf(final View shortsShelfView) { // region Hide the buttons in older versions of YouTube. New versions use Litho. - private static void hideTextViewUnderCondition(BooleanSetting setting, View view) { - try { - if (setting.get()) { - TextView textView = (TextView) view; - ViewGroup.LayoutParams params = textView.getLayoutParams(); - params.width = 0; - params.height = 0; - textView.setLayoutParams(params); - } - } catch (Exception ex) { - Logger.printException(() -> "hideTextViewUnderCondition failure", ex); - } - } - public static void hideLikeButton(final View likeButtonView) { - // Cannot simply set the visibility to gone for like/dislike, + // Cannot set the visibility to gone for like/dislike, // as some other unknown YT code also sets the visibility after this hook. - // Instead set the layout to a zero size. - hideTextViewUnderCondition(Settings.HIDE_SHORTS_LIKE_BUTTON, likeButtonView); + // + // Setting the view to 0dp works, but that leaves a blank space where + // the button was (only relevant for dislikes button). + // + // Instead remove the view from the parent. + removeViewFromParentUnderConditions(Settings.HIDE_SHORTS_LIKE_BUTTON, likeButtonView); } public static void hideDislikeButton(final View dislikeButtonView) { - hideTextViewUnderCondition(Settings.HIDE_SHORTS_DISLIKE_BUTTON, dislikeButtonView); + removeViewFromParentUnderConditions(Settings.HIDE_SHORTS_DISLIKE_BUTTON, dislikeButtonView); } public static void hideShortsCommentsButton(final View commentsButtonView) {