From 0c55d70370dad9275dfb5bc3817f71d4290f5a13 Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Sat, 20 May 2023 00:42:02 +0400 Subject: [PATCH] fix(youtube/settings): fix non functional back button in settings (#404) --- .../settingsmenu/ReVancedSettingActivity.java | 129 +++++++++--------- .../integrations/utils/ReVancedUtils.java | 34 ++++- .../integrations/utils/ThemeHelper.java | 11 +- .../social/licenses/LicenseActivity.java | 7 - 4 files changed, 102 insertions(+), 79 deletions(-) delete mode 100644 dummy/src/main/java/com/google/android/libraries/social/licenses/LicenseActivity.java diff --git a/app/src/main/java/app/revanced/integrations/settingsmenu/ReVancedSettingActivity.java b/app/src/main/java/app/revanced/integrations/settingsmenu/ReVancedSettingActivity.java index cda92cb9..e7cba140 100644 --- a/app/src/main/java/app/revanced/integrations/settingsmenu/ReVancedSettingActivity.java +++ b/app/src/main/java/app/revanced/integrations/settingsmenu/ReVancedSettingActivity.java @@ -1,17 +1,18 @@ package app.revanced.integrations.settingsmenu; +import static app.revanced.integrations.utils.ReVancedUtils.getChildView; +import static app.revanced.integrations.utils.ReVancedUtils.getResourceIdentifier; + +import android.annotation.SuppressLint; +import android.app.Activity; import android.preference.PreferenceFragment; -import android.view.View; import android.view.ViewGroup; import android.widget.ImageButton; import android.widget.TextView; -import androidx.annotation.Nullable; - -import com.google.android.libraries.social.licenses.LicenseActivity; +import java.util.Objects; import app.revanced.integrations.utils.LogHelper; -import app.revanced.integrations.utils.ReVancedUtils; import app.revanced.integrations.utils.ThemeHelper; public class ReVancedSettingActivity { @@ -19,76 +20,68 @@ public class ReVancedSettingActivity { /** * Injection point. */ - public static void setTheme(LicenseActivity base) { - final var whiteTheme = "Theme.YouTube.Settings"; - final var darkTheme = "Theme.YouTube.Settings.Dark"; - - final var theme = ThemeHelper.isDarkTheme() ? darkTheme : whiteTheme; - - LogHelper.printDebug(() -> "Using theme: " + theme); - base.setTheme(ReVancedUtils.getResourceIdentifier(theme, "style")); - } - - /** - * Injection point. - */ - public static void initializeSettings(LicenseActivity base) { - base.setContentView(ReVancedUtils.getResourceIdentifier("revanced_settings_with_toolbar", "layout")); - - PreferenceFragment preferenceFragment; - String preferenceIdentifier; - - String dataString = base.getIntent().getDataString(); - if (dataString.equalsIgnoreCase("sponsorblock_settings")) { - preferenceIdentifier = "sb_settings"; - preferenceFragment = new SponsorBlockSettingsFragment(); - } else if (dataString.equalsIgnoreCase("ryd_settings")) { - preferenceIdentifier = "revanced_ryd_settings_title"; - preferenceFragment = new ReturnYouTubeDislikeSettingsFragment(); - } else { - preferenceIdentifier = "revanced_settings"; - preferenceFragment = new ReVancedSettingsFragment(); - } - + public static void initializeSettings(Activity licenseActivity) { try { - TextView toolbar = getTextView((ViewGroup) base.findViewById(ReVancedUtils.getResourceIdentifier("toolbar", "id"))); - if (toolbar == null) { - // FIXME - // https://github.com/revanced/revanced-patches/issues/1384 - LogHelper.printDebug(() -> "Could not find toolbar"); - } else { - toolbar.setText(preferenceIdentifier); + ThemeHelper.setActivityTheme(licenseActivity); + licenseActivity.setContentView( + getResourceIdentifier("revanced_settings_with_toolbar", "layout")); + setBackButton(licenseActivity); + + PreferenceFragment fragment; + String toolbarTitleResourceName; + String dataString = licenseActivity.getIntent().getDataString(); + switch (dataString) { + case "sponsorblock_settings": + toolbarTitleResourceName = "revanced_sponsorblock_settings_title"; + fragment = new SponsorBlockSettingsFragment(); + break; + case "ryd_settings": + toolbarTitleResourceName = "revanced_ryd_settings_title"; + fragment = new ReturnYouTubeDislikeSettingsFragment(); + break; + case "revanced_settings": + toolbarTitleResourceName = "revanced_settings_title"; + fragment = new ReVancedSettingsFragment(); + break; + default: + LogHelper.printException(() -> "Unknown setting: " + dataString); + return; } - } catch (Exception e) { - LogHelper.printException(() -> "Could not set Toolbar title", e); + + setToolbarTitle(licenseActivity, toolbarTitleResourceName); + licenseActivity.getFragmentManager() + .beginTransaction() + .replace(getResourceIdentifier("revanced_settings_fragments", "id"), fragment) + .commit(); + } catch (Exception ex) { + LogHelper.printException(() -> "onCreate failure", ex); } - - base.getFragmentManager().beginTransaction().replace(ReVancedUtils.getResourceIdentifier("revanced_settings_fragments", "id"), preferenceFragment).commit(); } + private static void setToolbarTitle(Activity activity, String toolbarTitleResourceName) { + ViewGroup toolbar = activity.findViewById(getToolbarResourceId()); + TextView toolbarTextView = Objects.requireNonNull(getChildView(toolbar, view -> view instanceof TextView)); + toolbarTextView.setText(getResourceIdentifier(toolbarTitleResourceName, "string")); + } - @Nullable - public static T getView(Class typeClass, ViewGroup viewGroup) { - if (viewGroup == null) { - return null; + @SuppressLint("UseCompatLoadingForDrawables") + private static void setBackButton(Activity activity) { + ViewGroup toolbar = activity.findViewById(getToolbarResourceId()); + ImageButton imageButton = Objects.requireNonNull(getChildView(toolbar, view -> view instanceof ImageButton)); + final int backButtonResource = getResourceIdentifier(ThemeHelper.isDarkTheme() + ? "yt_outline_arrow_left_white_24" + : "yt_outline_arrow_left_black_24", + "drawable"); + imageButton.setImageDrawable(activity.getResources().getDrawable(backButtonResource)); + imageButton.setOnClickListener(view -> activity.onBackPressed()); + } + + private static int getToolbarResourceId() { + final int toolbarResourceId = getResourceIdentifier("revanced_toolbar", "id"); + if (toolbarResourceId == 0) { + throw new IllegalStateException("Could not find back button resource"); } - int childCount = viewGroup.getChildCount(); - for (int i = 0; i < childCount; i++) { - View childAt = viewGroup.getChildAt(i); - if (childAt.getClass() == typeClass) { - return (T) childAt; - } - } - return null; + return toolbarResourceId; } - @Nullable - public static ImageButton getImageButton(ViewGroup viewGroup) { - return getView(ImageButton.class, viewGroup); - } - - @Nullable - public static TextView getTextView(ViewGroup viewGroup) { - return getView(TextView.class, viewGroup); - } } diff --git a/app/src/main/java/app/revanced/integrations/utils/ReVancedUtils.java b/app/src/main/java/app/revanced/integrations/utils/ReVancedUtils.java index 73358524..564de2c3 100644 --- a/app/src/main/java/app/revanced/integrations/utils/ReVancedUtils.java +++ b/app/src/main/java/app/revanced/integrations/utils/ReVancedUtils.java @@ -10,15 +10,25 @@ import android.view.View; import android.view.ViewGroup; import android.view.animation.Animation; import android.view.animation.AnimationUtils; -import android.widget.*; +import android.widget.FrameLayout; +import android.widget.LinearLayout; +import android.widget.RelativeLayout; +import android.widget.Toast; +import android.widget.Toolbar; + import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import app.revanced.integrations.settings.SettingsEnum; import java.text.Bidi; import java.util.Locale; import java.util.Objects; -import java.util.concurrent.*; +import java.util.concurrent.Callable; +import java.util.concurrent.Future; +import java.util.concurrent.SynchronousQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +import app.revanced.integrations.settings.SettingsEnum; public class ReVancedUtils { @@ -124,6 +134,24 @@ public class ReVancedUtils { return getContext().getResources().getDimension(getResourceIdentifier(resourceIdentifierName, "dimen")); } + /** + * @return The first child view that matches the filter. + */ + @Nullable + public static T getChildView(@NonNull ViewGroup viewGroup, @NonNull MatchFilter filter) { + for (int i = 0, childCount = viewGroup.getChildCount(); i < childCount; i++) { + View childAt = viewGroup.getChildAt(i); + if (filter.matches(childAt)) { + return (T) childAt; + } + } + return null; + } + + public interface MatchFilter { + boolean matches(T object); + } + public static Context getContext() { if (context != null) { return context; diff --git a/app/src/main/java/app/revanced/integrations/utils/ThemeHelper.java b/app/src/main/java/app/revanced/integrations/utils/ThemeHelper.java index 3804d186..99fe57d3 100644 --- a/app/src/main/java/app/revanced/integrations/utils/ThemeHelper.java +++ b/app/src/main/java/app/revanced/integrations/utils/ThemeHelper.java @@ -1,5 +1,7 @@ package app.revanced.integrations.utils; +import android.app.Activity; + public class ThemeHelper { private static int themeValue; @@ -7,7 +9,7 @@ public class ThemeHelper { final int newOrdinalValue = ((Enum) value).ordinal(); if (themeValue != newOrdinalValue) { themeValue = newOrdinalValue; - LogHelper.printDebug(() -> "Theme value: " + themeValue); + LogHelper.printDebug(() -> "Theme value: " + newOrdinalValue); } } @@ -15,4 +17,11 @@ public class ThemeHelper { return themeValue == 1; } + public static void setActivityTheme(Activity activity) { + final var theme = isDarkTheme() + ? "Theme.YouTube.Settings.Dark" + : "Theme.YouTube.Settings"; + activity.setTheme(ReVancedUtils.getResourceIdentifier(theme, "style")); + } + } diff --git a/dummy/src/main/java/com/google/android/libraries/social/licenses/LicenseActivity.java b/dummy/src/main/java/com/google/android/libraries/social/licenses/LicenseActivity.java deleted file mode 100644 index 059a45c9..00000000 --- a/dummy/src/main/java/com/google/android/libraries/social/licenses/LicenseActivity.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.google.android.libraries.social.licenses; - -import android.app.Activity; - -// Dummy class -public final class LicenseActivity extends Activity { } -