Skip to content

Commit d699ddc

Browse files
committed
fix(YouTube - Settings): Back button/gesture closes search instead of exiting
1 parent 7fdd90a commit d699ddc

File tree

3 files changed

+51
-27
lines changed

3 files changed

+51
-27
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,15 @@
2424
* This class is responsible for injecting our own fragment by replacing the LicenseActivity.
2525
*/
2626
@SuppressWarnings("unused")
27-
public class LicenseActivityHook {
27+
public class LicenseActivityHook extends Activity {
2828

2929
private static int currentThemeValueOrdinal = -1; // Must initially be a non-valid enum ordinal value.
3030

3131
private static ViewGroup.LayoutParams toolbarLayoutParams;
3232

33+
@SuppressLint("StaticFieldLeak")
34+
public static SearchViewController searchViewController;
35+
3336
public static void setToolbarLayoutParams(Toolbar toolbar) {
3437
if (toolbarLayoutParams != null) {
3538
toolbar.setLayoutParams(toolbarLayoutParams);
@@ -131,7 +134,7 @@ private static void createToolbar(Activity activity, PreferenceFragment fragment
131134

132135
// Add Search Icon and EditText for ReVancedPreferenceFragment only.
133136
if (fragment instanceof ReVancedPreferenceFragment) {
134-
SearchViewController.addSearchViewComponents(activity, toolbar, (ReVancedPreferenceFragment) fragment);
137+
searchViewController = SearchViewController.addSearchViewComponents(activity, toolbar, (ReVancedPreferenceFragment) fragment);
135138
}
136139

137140
toolBarParent.addView(toolbar, 0);

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ public static int getSearchViewBackground() {
8383
/**
8484
* Adds search view components to the activity.
8585
*/
86-
public static void addSearchViewComponents(Activity activity, Toolbar toolbar, ReVancedPreferenceFragment fragment) {
87-
new SearchViewController(activity, toolbar, fragment);
86+
public static SearchViewController addSearchViewComponents(Activity activity, Toolbar toolbar, ReVancedPreferenceFragment fragment) {
87+
return new SearchViewController(activity, toolbar, fragment);
8888
}
8989

9090
private SearchViewController(Activity activity, Toolbar toolbar, ReVancedPreferenceFragment fragment) {
@@ -197,7 +197,7 @@ public boolean onQueryTextChange(String newText) {
197197
if (isSearchActive) {
198198
closeSearch();
199199
} else {
200-
activity.onBackPressed();
200+
activity.finish();
201201
}
202202
} catch (Exception ex) {
203203
Logger.printException(() -> "navigation click failure", ex);
@@ -313,7 +313,7 @@ private void openSearch() {
313313
/**
314314
* Closes the search view and hides the keyboard.
315315
*/
316-
private void closeSearch() {
316+
public void closeSearch() {
317317
isSearchActive = false;
318318
toolbar.getMenu().findItem(getResourceIdentifier(
319319
"action_search", "id")).setVisible(true);
@@ -326,6 +326,19 @@ private void closeSearch() {
326326
imm.hideSoftInputFromWindow(searchView.getWindowToken(), 0);
327327
}
328328

329+
public static boolean handleBackPress() {
330+
if (LicenseActivityHook.searchViewController != null
331+
&& LicenseActivityHook.searchViewController.isSearchExpanded()) {
332+
LicenseActivityHook.searchViewController.closeSearch();
333+
return true;
334+
}
335+
return false;
336+
}
337+
338+
public boolean isSearchExpanded() {
339+
return isSearchActive;
340+
}
341+
329342
/**
330343
* Custom ArrayAdapter for search history.
331344
*/

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

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,15 @@ import app.revanced.patches.shared.misc.mapping.get
1212
import app.revanced.patches.shared.misc.mapping.resourceMappingPatch
1313
import app.revanced.patches.shared.misc.mapping.resourceMappings
1414
import app.revanced.patches.shared.misc.settings.overrideThemeColors
15-
import app.revanced.patches.shared.misc.settings.preference.BasePreference
16-
import app.revanced.patches.shared.misc.settings.preference.BasePreferenceScreen
17-
import app.revanced.patches.shared.misc.settings.preference.InputType
18-
import app.revanced.patches.shared.misc.settings.preference.IntentPreference
19-
import app.revanced.patches.shared.misc.settings.preference.ListPreference
20-
import app.revanced.patches.shared.misc.settings.preference.NonInteractivePreference
21-
import app.revanced.patches.shared.misc.settings.preference.PreferenceCategory
22-
import app.revanced.patches.shared.misc.settings.preference.PreferenceScreenPreference
15+
import app.revanced.patches.shared.misc.settings.preference.*
2316
import app.revanced.patches.shared.misc.settings.preference.PreferenceScreenPreference.Sorting
24-
import app.revanced.patches.shared.misc.settings.preference.SwitchPreference
25-
import app.revanced.patches.shared.misc.settings.preference.TextPreference
2617
import app.revanced.patches.shared.misc.settings.settingsPatch
2718
import app.revanced.patches.youtube.misc.check.checkEnvironmentPatch
2819
import app.revanced.patches.youtube.misc.extension.sharedExtensionPatch
2920
import app.revanced.patches.youtube.misc.fix.playbackspeed.fixPlaybackSpeedWhilePlayingPatch
3021
import app.revanced.patches.youtube.misc.playservice.is_19_34_or_greater
3122
import app.revanced.patches.youtube.misc.playservice.versionCheckPatch
32-
import app.revanced.util.ResourceGroup
33-
import app.revanced.util.addInstructionsAtControlFlowLabel
34-
import app.revanced.util.copyResources
35-
import app.revanced.util.copyXmlNode
36-
import app.revanced.util.findElementByAttributeValueOrThrow
37-
import app.revanced.util.findInstructionIndicesReversedOrThrow
38-
import app.revanced.util.inputStreamFromBundledResource
39-
import app.revanced.util.insertLiteralOverride
23+
import app.revanced.util.*
4024
import com.android.tools.smali.dexlib2.AccessFlags
4125
import com.android.tools.smali.dexlib2.Opcode
4226
import com.android.tools.smali.dexlib2.builder.MutableMethodImplementation
@@ -267,6 +251,32 @@ val settingsPatch = bytecodePatch(
267251
methods.add(attachBaseContext)
268252
}
269253

254+
licenseActivityOnCreateFingerprint.classDef.apply {
255+
val onBackPressed = ImmutableMethod(
256+
type,
257+
"onBackPressed",
258+
emptyList(),
259+
"V",
260+
AccessFlags.PUBLIC.value,
261+
null,
262+
null,
263+
MutableMethodImplementation(3)
264+
).toMutable().apply {
265+
addInstructions(
266+
"""
267+
invoke-static {}, Lapp/revanced/extension/youtube/settings/SearchViewController;->handleBackPress()Z
268+
move-result v0
269+
if-nez v0, :search_handled
270+
invoke-virtual { p0 }, Landroid/app/Activity;->finish()V
271+
:search_handled
272+
return-void
273+
"""
274+
)
275+
276+
};
277+
methods.add(onBackPressed);
278+
};
279+
270280
// Update shared dark mode status based on YT theme.
271281
// This is needed because YT allows forcing light/dark mode
272282
// which then differs from the system dark mode status.
@@ -338,20 +348,18 @@ object PreferenceScreen : BasePreferenceScreen() {
338348
icon = "@drawable/revanced_settings_screen_05_player",
339349
layout = "@layout/preference_with_icon",
340350
)
341-
342351
val SHORTS = Screen(
343352
key = "revanced_settings_screen_06_shorts",
344353
summaryKey = null,
345354
icon = "@drawable/revanced_settings_screen_06_shorts",
346355
layout = "@layout/preference_with_icon",
347356
)
348-
349357
val SEEKBAR = Screen(
350358
key = "revanced_settings_screen_07_seekbar",
351359
summaryKey = null,
352360
icon = "@drawable/revanced_settings_screen_07_seekbar",
353361
layout = "@layout/preference_with_icon",
354-
)
362+
)
355363
val SWIPE_CONTROLS = Screen(
356364
key = "revanced_settings_screen_08_swipe_controls",
357365
summaryKey = null,

0 commit comments

Comments
 (0)