From 86ec08993c8a74b4e331eb1892b0e3f87c7b37e9 Mon Sep 17 00:00:00 2001 From: MarcaD <152095496+MarcaDian@users.noreply.github.com> Date: Wed, 16 Jul 2025 22:26:20 +0300 Subject: [PATCH] refactor(YouTube - Settings): Back button/gesture closes search instead of exiting (#5439) --- .../youtube/settings/LicenseActivityHook.java | 7 ++++ .../settings/SearchViewController.java | 26 +++++-------- .../ReVancedPreferenceFragment.java | 2 +- .../youtube/misc/settings/SettingsPatch.kt | 39 +++++++++++++------ 4 files changed, 45 insertions(+), 29 deletions(-) diff --git a/extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/LicenseActivityHook.java b/extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/LicenseActivityHook.java index 4ba25c4c5..24d3a4f42 100644 --- a/extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/LicenseActivityHook.java +++ b/extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/LicenseActivityHook.java @@ -5,6 +5,7 @@ import static app.revanced.extension.shared.Utils.getResourceIdentifier; import android.annotation.SuppressLint; import android.app.Activity; import android.content.Context; +import android.content.res.Configuration; import android.preference.PreferenceFragment; import android.util.TypedValue; import android.view.ViewGroup; @@ -171,4 +172,10 @@ public class LicenseActivityHook extends Activity { Utils.setIsDarkModeEnabled(themeOrdinal == 1); } } + + public static void handleConfigurationChanged(Activity activity, Configuration newConfig) { + if (searchViewController != null) { + searchViewController.handleOrientationChange(newConfig.orientation); + } + } } diff --git a/extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/SearchViewController.java b/extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/SearchViewController.java index b99c885cf..10be641f7 100644 --- a/extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/SearchViewController.java +++ b/extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/SearchViewController.java @@ -95,6 +95,7 @@ public class SearchViewController { this.originalTitle = toolbar.getTitle(); this.showSettingsSearchHistory = Settings.SETTINGS_SEARCH_HISTORY.get(); this.searchHistory = new LinkedList<>(); + this.currentOrientation = activity.getResources().getConfiguration().orientation; StringSetting searchEntries = Settings.SETTINGS_SEARCH_ENTRIES; if (showSettingsSearchHistory) { String entries = searchEntries.get(); @@ -208,8 +209,6 @@ public class SearchViewController { Logger.printException(() -> "navigation click failure", ex); } }); - - monitorOrientationChanges(); } /** @@ -292,19 +291,14 @@ public class SearchViewController { } } - private void monitorOrientationChanges() { - currentOrientation = activity.getResources().getConfiguration().orientation; - - searchView.getViewTreeObserver().addOnGlobalLayoutListener(() -> { - int newOrientation = activity.getResources().getConfiguration().orientation; - if (newOrientation != currentOrientation) { - currentOrientation = newOrientation; - if (autoCompleteTextView != null) { - autoCompleteTextView.dismissDropDown(); - Logger.printDebug(() -> "Orientation changed, search history dismissed"); - } + public void handleOrientationChange(int newOrientation) { + if (newOrientation != currentOrientation) { + currentOrientation = newOrientation; + if (autoCompleteTextView != null) { + autoCompleteTextView.dismissDropDown(); + Logger.printDebug(() -> "Orientation changed, search history dismissed"); } - }); + } } /** @@ -350,14 +344,14 @@ public class SearchViewController { public static boolean handleBackPress() { if (LicenseActivityHook.searchViewController != null - && LicenseActivityHook.searchViewController.isSearchExpanded()) { + && LicenseActivityHook.searchViewController.isSearchActive()) { LicenseActivityHook.searchViewController.closeSearch(); return true; } return false; } - public boolean isSearchExpanded() { + public boolean isSearchActive() { return isSearchActive; } diff --git a/extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/preference/ReVancedPreferenceFragment.java b/extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/preference/ReVancedPreferenceFragment.java index e76451417..02f8db6cb 100644 --- a/extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/preference/ReVancedPreferenceFragment.java +++ b/extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/preference/ReVancedPreferenceFragment.java @@ -280,7 +280,7 @@ public class ReVancedPreferenceFragment extends AbstractPreferenceFragment { LicenseActivityHook.setToolbarLayoutParams(toolbar); if (LicenseActivityHook.searchViewController != null - && LicenseActivityHook.searchViewController.isSearchExpanded()) { + && LicenseActivityHook.searchViewController.isSearchActive()) { toolbar.post(() -> LicenseActivityHook.searchViewController.closeSearch()); } diff --git a/patches/src/main/kotlin/app/revanced/patches/youtube/misc/settings/SettingsPatch.kt b/patches/src/main/kotlin/app/revanced/patches/youtube/misc/settings/SettingsPatch.kt index 1e6732446..672999b6a 100644 --- a/patches/src/main/kotlin/app/revanced/patches/youtube/misc/settings/SettingsPatch.kt +++ b/patches/src/main/kotlin/app/revanced/patches/youtube/misc/settings/SettingsPatch.kt @@ -235,9 +235,9 @@ val settingsPatch = bytecodePatch( methods.removeIf { it.name != "onCreate" && !MethodUtil.isConstructor(it) } } - // Add context override to force a specific settings language. licenseActivityOnCreateFingerprint.classDef.apply { - val attachBaseContext = ImmutableMethod( + // Add attachBaseContext method to override the context for setting a specific language. + ImmutableMethod( type, "attachBaseContext", listOf(ImmutableMethodParameter("Landroid/content/Context;", null, null)), @@ -255,13 +255,10 @@ val settingsPatch = bytecodePatch( return-void """ ) - } + }.let(methods::add) - methods.add(attachBaseContext) - } - - licenseActivityOnCreateFingerprint.classDef.apply { - val onBackPressed = ImmutableMethod( + // Add onBackPressed method to handle back button presses, delegating to SearchViewController. + ImmutableMethod( type, "onBackPressed", emptyList(), @@ -269,7 +266,7 @@ val settingsPatch = bytecodePatch( AccessFlags.PUBLIC.value, null, null, - MutableMethodImplementation(3) + MutableMethodImplementation(3), ).toMutable().apply { addInstructions( """ @@ -281,10 +278,28 @@ val settingsPatch = bytecodePatch( return-void """ ) + }.let(methods::add) - }; - methods.add(onBackPressed); - }; + // Add onConfigurationChanged method to handle configuration changes (e.g., screen orientation). + ImmutableMethod( + type, + "onConfigurationChanged", + listOf(ImmutableMethodParameter("Landroid/content/res/Configuration;", null, null)), + "V", + AccessFlags.PUBLIC.value, + null, + null, + MutableMethodImplementation(3) + ).toMutable().apply { + addInstructions( + """ + invoke-super { p0, p1 }, Landroid/app/Activity;->onConfigurationChanged(Landroid/content/res/Configuration;)V + invoke-static { p0, p1 }, $EXTENSION_CLASS_DESCRIPTOR->handleConfigurationChanged(Landroid/app/Activity;Landroid/content/res/Configuration;)V + return-void + """ + ) + }.let(methods::add) + } // Update shared dark mode status based on YT theme. // This is needed because YT allows forcing light/dark mode