Skip to content

Commit 5029568

Browse files
authored
refactor(YouTube - Settings): Back button/gesture closes search instead of exiting (#5439)
1 parent 8469e2d commit 5029568

File tree

4 files changed

+45
-29
lines changed

4 files changed

+45
-29
lines changed

extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/LicenseActivityHook.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.annotation.SuppressLint;
66
import android.app.Activity;
77
import android.content.Context;
8+
import android.content.res.Configuration;
89
import android.preference.PreferenceFragment;
910
import android.util.TypedValue;
1011
import android.view.ViewGroup;
@@ -171,4 +172,10 @@ public static void updateLightDarkModeStatus(Enum<?> value) {
171172
Utils.setIsDarkModeEnabled(themeOrdinal == 1);
172173
}
173174
}
175+
176+
public static void handleConfigurationChanged(Activity activity, Configuration newConfig) {
177+
if (searchViewController != null) {
178+
searchViewController.handleOrientationChange(newConfig.orientation);
179+
}
180+
}
174181
}

extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/SearchViewController.java

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ private SearchViewController(Activity activity, Toolbar toolbar, ReVancedPrefere
9595
this.originalTitle = toolbar.getTitle();
9696
this.showSettingsSearchHistory = Settings.SETTINGS_SEARCH_HISTORY.get();
9797
this.searchHistory = new LinkedList<>();
98+
this.currentOrientation = activity.getResources().getConfiguration().orientation;
9899
StringSetting searchEntries = Settings.SETTINGS_SEARCH_ENTRIES;
99100
if (showSettingsSearchHistory) {
100101
String entries = searchEntries.get();
@@ -208,8 +209,6 @@ public boolean onQueryTextChange(String newText) {
208209
Logger.printException(() -> "navigation click failure", ex);
209210
}
210211
});
211-
212-
monitorOrientationChanges();
213212
}
214213

215214
/**
@@ -292,19 +291,14 @@ private void updateSearchHistoryAdapter() {
292291
}
293292
}
294293

295-
private void monitorOrientationChanges() {
296-
currentOrientation = activity.getResources().getConfiguration().orientation;
297-
298-
searchView.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
299-
int newOrientation = activity.getResources().getConfiguration().orientation;
300-
if (newOrientation != currentOrientation) {
301-
currentOrientation = newOrientation;
302-
if (autoCompleteTextView != null) {
303-
autoCompleteTextView.dismissDropDown();
304-
Logger.printDebug(() -> "Orientation changed, search history dismissed");
305-
}
294+
public void handleOrientationChange(int newOrientation) {
295+
if (newOrientation != currentOrientation) {
296+
currentOrientation = newOrientation;
297+
if (autoCompleteTextView != null) {
298+
autoCompleteTextView.dismissDropDown();
299+
Logger.printDebug(() -> "Orientation changed, search history dismissed");
306300
}
307-
});
301+
}
308302
}
309303

310304
/**
@@ -350,14 +344,14 @@ public void closeSearch() {
350344

351345
public static boolean handleBackPress() {
352346
if (LicenseActivityHook.searchViewController != null
353-
&& LicenseActivityHook.searchViewController.isSearchExpanded()) {
347+
&& LicenseActivityHook.searchViewController.isSearchActive()) {
354348
LicenseActivityHook.searchViewController.closeSearch();
355349
return true;
356350
}
357351
return false;
358352
}
359353

360-
public boolean isSearchExpanded() {
354+
public boolean isSearchActive() {
361355
return isSearchActive;
362356
}
363357

extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/preference/ReVancedPreferenceFragment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ private void setPreferenceScreenToolbar(PreferenceScreen parentScreen) {
280280
LicenseActivityHook.setToolbarLayoutParams(toolbar);
281281

282282
if (LicenseActivityHook.searchViewController != null
283-
&& LicenseActivityHook.searchViewController.isSearchExpanded()) {
283+
&& LicenseActivityHook.searchViewController.isSearchActive()) {
284284
toolbar.post(() -> LicenseActivityHook.searchViewController.closeSearch());
285285
}
286286

patches/src/main/kotlin/app/revanced/patches/youtube/misc/settings/SettingsPatch.kt

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,9 @@ val settingsPatch = bytecodePatch(
235235
methods.removeIf { it.name != "onCreate" && !MethodUtil.isConstructor(it) }
236236
}
237237

238-
// Add context override to force a specific settings language.
239238
licenseActivityOnCreateFingerprint.classDef.apply {
240-
val attachBaseContext = ImmutableMethod(
239+
// Add attachBaseContext method to override the context for setting a specific language.
240+
ImmutableMethod(
241241
type,
242242
"attachBaseContext",
243243
listOf(ImmutableMethodParameter("Landroid/content/Context;", null, null)),
@@ -255,21 +255,18 @@ val settingsPatch = bytecodePatch(
255255
return-void
256256
"""
257257
)
258-
}
259-
260-
methods.add(attachBaseContext)
261-
}
258+
}.let(methods::add)
262259

263-
licenseActivityOnCreateFingerprint.classDef.apply {
264-
val onBackPressed = ImmutableMethod(
260+
// Add onBackPressed method to handle back button presses, delegating to SearchViewController.
261+
ImmutableMethod(
265262
type,
266263
"onBackPressed",
267264
emptyList(),
268265
"V",
269266
AccessFlags.PUBLIC.value,
270267
null,
271268
null,
272-
MutableMethodImplementation(3)
269+
MutableMethodImplementation(3),
273270
).toMutable().apply {
274271
addInstructions(
275272
"""
@@ -281,10 +278,28 @@ val settingsPatch = bytecodePatch(
281278
return-void
282279
"""
283280
)
281+
}.let(methods::add)
284282

285-
};
286-
methods.add(onBackPressed);
287-
};
283+
// Add onConfigurationChanged method to handle configuration changes (e.g., screen orientation).
284+
ImmutableMethod(
285+
type,
286+
"onConfigurationChanged",
287+
listOf(ImmutableMethodParameter("Landroid/content/res/Configuration;", null, null)),
288+
"V",
289+
AccessFlags.PUBLIC.value,
290+
null,
291+
null,
292+
MutableMethodImplementation(3)
293+
).toMutable().apply {
294+
addInstructions(
295+
"""
296+
invoke-super { p0, p1 }, Landroid/app/Activity;->onConfigurationChanged(Landroid/content/res/Configuration;)V
297+
invoke-static { p0, p1 }, $EXTENSION_CLASS_DESCRIPTOR->handleConfigurationChanged(Landroid/app/Activity;Landroid/content/res/Configuration;)V
298+
return-void
299+
"""
300+
)
301+
}.let(methods::add)
302+
}
288303

289304
// Update shared dark mode status based on YT theme.
290305
// This is needed because YT allows forcing light/dark mode

0 commit comments

Comments
 (0)