Skip to content

Commit 62df596

Browse files
fix(Twitch): Resolve setting menu crashes (ReVanced#4025)
1 parent 414e1da commit 62df596

File tree

5 files changed

+87
-74
lines changed

5 files changed

+87
-74
lines changed

extensions/shared/src/main/java/app/revanced/extension/shared/settings/preference/AbstractPreferenceFragment.java

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,13 @@
11
package app.revanced.extension.shared.settings.preference;
22

33
import static app.revanced.extension.shared.StringRef.str;
4-
import static app.revanced.extension.shared.Utils.getResourceIdentifier;
54

65
import android.annotation.SuppressLint;
76
import android.app.AlertDialog;
8-
import android.app.Dialog;
97
import android.content.Context;
108
import android.content.SharedPreferences;
11-
import android.graphics.Insets;
12-
import android.graphics.drawable.Drawable;
13-
import android.os.Build;
149
import android.os.Bundle;
1510
import android.preference.*;
16-
import android.util.TypedValue;
17-
import android.view.ViewGroup;
18-
import android.view.WindowInsets;
19-
import android.widget.TextView;
20-
import android.widget.Toolbar;
2111

2212
import androidx.annotation.NonNull;
2313
import androidx.annotation.Nullable;
@@ -29,7 +19,6 @@
2919
import app.revanced.extension.shared.settings.BaseSettings;
3020
import app.revanced.extension.shared.settings.BooleanSetting;
3121
import app.revanced.extension.shared.settings.Setting;
32-
import app.revanced.extension.youtube.ThemeHelper;
3322

3423
@SuppressWarnings("deprecation")
3524
public abstract class AbstractPreferenceFragment extends PreferenceFragment {
@@ -85,14 +74,6 @@ public abstract class AbstractPreferenceFragment extends PreferenceFragment {
8574
}
8675
};
8776

88-
@SuppressLint("UseCompatLoadingForDrawables")
89-
public static Drawable getBackButtonDrawable() {
90-
final int backButtonResource = getResourceIdentifier(ThemeHelper.isDarkTheme()
91-
? "yt_outline_arrow_left_white_24"
92-
: "yt_outline_arrow_left_black_24",
93-
"drawable");
94-
return Utils.getContext().getResources().getDrawable(backButtonResource);
95-
}
9677

9778
/**
9879
* Initialize this instance, and do any custom behavior.
@@ -284,7 +265,6 @@ public void onCreate(Bundle savedInstanceState) {
284265
// causes a callback to the listener even though nothing changed.
285266
initialize();
286267
updateUIToSettingValues();
287-
setPreferenceScreenToolbar(getPreferenceScreen());
288268

289269
preferenceManager.getSharedPreferences().registerOnSharedPreferenceChangeListener(listener);
290270
} catch (Exception ex) {
@@ -297,56 +277,4 @@ public void onDestroy() {
297277
getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(listener);
298278
super.onDestroy();
299279
}
300-
301-
private void setPreferenceScreenToolbar(PreferenceScreen parentScreen) {
302-
for (int i = 0, preferenceCount = parentScreen.getPreferenceCount(); i < preferenceCount; i++) {
303-
Preference childPreference = parentScreen.getPreference(i);
304-
if (childPreference instanceof PreferenceScreen) {
305-
// Recursively set sub preferences.
306-
setPreferenceScreenToolbar((PreferenceScreen) childPreference);
307-
308-
childPreference.setOnPreferenceClickListener(
309-
childScreen -> {
310-
Dialog preferenceScreenDialog = ((PreferenceScreen) childScreen).getDialog();
311-
ViewGroup rootView = (ViewGroup) preferenceScreenDialog
312-
.findViewById(android.R.id.content)
313-
.getParent();
314-
315-
// Fix required for Android 15 and YT 19.45+
316-
// FIXME:
317-
// On Android 15 the text layout is not aligned the same as the parent
318-
// screen and it looks a little off. Otherwise this works.
319-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
320-
rootView.setOnApplyWindowInsetsListener((v, insets) -> {
321-
Insets statusInsets = insets.getInsets(WindowInsets.Type.statusBars());
322-
v.setPadding(0, statusInsets.top, 0, 0);
323-
return insets;
324-
});
325-
}
326-
327-
Toolbar toolbar = new Toolbar(childScreen.getContext());
328-
toolbar.setTitle(childScreen.getTitle());
329-
toolbar.setNavigationIcon(getBackButtonDrawable());
330-
toolbar.setNavigationOnClickListener(view -> preferenceScreenDialog.dismiss());
331-
332-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
333-
final int margin = (int) TypedValue.applyDimension(
334-
TypedValue.COMPLEX_UNIT_DIP, 16, getResources().getDisplayMetrics()
335-
);
336-
toolbar.setTitleMargin(margin, 0, margin, 0);
337-
}
338-
339-
TextView toolbarTextView = Utils.getChildView(toolbar,
340-
true, TextView.class::isInstance);
341-
if (toolbarTextView != null) {
342-
toolbarTextView.setTextColor(ThemeHelper.getForegroundColor());
343-
}
344-
345-
rootView.addView(toolbar, 0);
346-
return false;
347-
}
348-
);
349-
}
350-
}
351-
}
352280
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import android.widget.ImageButton;
88
import android.widget.TextView;
99
import app.revanced.extension.shared.Logger;
10-
import app.revanced.extension.shared.settings.preference.AbstractPreferenceFragment;
1110
import app.revanced.extension.youtube.ThemeHelper;
1211
import app.revanced.extension.youtube.settings.preference.ReVancedPreferenceFragment;
1312
import app.revanced.extension.youtube.settings.preference.ReturnYouTubeDislikePreferenceFragment;
@@ -83,7 +82,7 @@ private static void setBackButton(Activity activity) {
8382
ViewGroup toolbar = activity.findViewById(getToolbarResourceId());
8483
ImageButton imageButton = Objects.requireNonNull(getChildView(toolbar, false,
8584
view -> view instanceof ImageButton));
86-
imageButton.setImageDrawable(AbstractPreferenceFragment.getBackButtonDrawable());
85+
imageButton.setImageDrawable(ReVancedPreferenceFragment.getBackButtonDrawable());
8786
imageButton.setOnClickListener(view -> activity.onBackPressed());
8887
}
8988

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

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
package app.revanced.extension.youtube.settings.preference;
22

3+
import static app.revanced.extension.shared.Utils.getResourceIdentifier;
4+
5+
import android.annotation.SuppressLint;
6+
import android.app.Dialog;
7+
import android.graphics.Insets;
8+
import android.graphics.drawable.Drawable;
39
import android.os.Build;
410
import android.preference.ListPreference;
511
import android.preference.Preference;
12+
import android.preference.PreferenceScreen;
13+
import android.util.TypedValue;
14+
import android.view.ViewGroup;
15+
import android.view.WindowInsets;
16+
import android.widget.TextView;
17+
import android.widget.Toolbar;
618

719
import androidx.annotation.RequiresApi;
820

921
import app.revanced.extension.shared.Logger;
22+
import app.revanced.extension.shared.Utils;
1023
import app.revanced.extension.shared.settings.preference.AbstractPreferenceFragment;
24+
import app.revanced.extension.youtube.ThemeHelper;
1125
import app.revanced.extension.youtube.patches.playback.speed.CustomPlaybackSpeedPatch;
1226
import app.revanced.extension.youtube.settings.Settings;
1327

@@ -18,12 +32,23 @@
1832
*/
1933
public class ReVancedPreferenceFragment extends AbstractPreferenceFragment {
2034

35+
@SuppressLint("UseCompatLoadingForDrawables")
36+
public static Drawable getBackButtonDrawable() {
37+
final int backButtonResource = getResourceIdentifier(ThemeHelper.isDarkTheme()
38+
? "yt_outline_arrow_left_white_24"
39+
: "yt_outline_arrow_left_black_24",
40+
"drawable");
41+
return Utils.getContext().getResources().getDrawable(backButtonResource);
42+
}
43+
2144
@RequiresApi(api = Build.VERSION_CODES.O)
2245
@Override
2346
protected void initialize() {
2447
super.initialize();
2548

2649
try {
50+
setPreferenceScreenToolbar(getPreferenceScreen());
51+
2752
// If the preference was included, then initialize it based on the available playback speed.
2853
Preference defaultSpeedPreference = findPreference(Settings.PLAYBACK_SPEED_DEFAULT.key);
2954
if (defaultSpeedPreference instanceof ListPreference) {
@@ -33,4 +58,56 @@ protected void initialize() {
3358
Logger.printException(() -> "initialize failure", ex);
3459
}
3560
}
61+
62+
private void setPreferenceScreenToolbar(PreferenceScreen parentScreen) {
63+
for (int i = 0, preferenceCount = parentScreen.getPreferenceCount(); i < preferenceCount; i++) {
64+
Preference childPreference = parentScreen.getPreference(i);
65+
if (childPreference instanceof PreferenceScreen) {
66+
// Recursively set sub preferences.
67+
setPreferenceScreenToolbar((PreferenceScreen) childPreference);
68+
69+
childPreference.setOnPreferenceClickListener(
70+
childScreen -> {
71+
Dialog preferenceScreenDialog = ((PreferenceScreen) childScreen).getDialog();
72+
ViewGroup rootView = (ViewGroup) preferenceScreenDialog
73+
.findViewById(android.R.id.content)
74+
.getParent();
75+
76+
// Fix required for Android 15 and YT 19.45+
77+
// FIXME:
78+
// On Android 15 the text layout is not aligned the same as the parent
79+
// screen and it looks a little off. Otherwise this works.
80+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
81+
rootView.setOnApplyWindowInsetsListener((v, insets) -> {
82+
Insets statusInsets = insets.getInsets(WindowInsets.Type.statusBars());
83+
v.setPadding(0, statusInsets.top, 0, 0);
84+
return insets;
85+
});
86+
}
87+
88+
Toolbar toolbar = new Toolbar(childScreen.getContext());
89+
toolbar.setTitle(childScreen.getTitle());
90+
toolbar.setNavigationIcon(getBackButtonDrawable());
91+
toolbar.setNavigationOnClickListener(view -> preferenceScreenDialog.dismiss());
92+
93+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
94+
final int margin = (int) TypedValue.applyDimension(
95+
TypedValue.COMPLEX_UNIT_DIP, 16, getResources().getDisplayMetrics()
96+
);
97+
toolbar.setTitleMargin(margin, 0, margin, 0);
98+
}
99+
100+
TextView toolbarTextView = Utils.getChildView(toolbar,
101+
true, TextView.class::isInstance);
102+
if (toolbarTextView != null) {
103+
toolbarTextView.setTextColor(ThemeHelper.getForegroundColor());
104+
}
105+
106+
rootView.addView(toolbar, 0);
107+
return false;
108+
}
109+
);
110+
}
111+
}
112+
}
36113
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import app.revanced.patches.all.misc.resources.addResources
1111
import app.revanced.patches.all.misc.resources.addResourcesPatch
1212
import app.revanced.patches.shared.misc.settings.preference.BasePreference
1313
import app.revanced.patches.shared.misc.settings.preference.BasePreferenceScreen
14+
import app.revanced.patches.shared.misc.settings.preference.NonInteractivePreference
1415
import app.revanced.patches.shared.misc.settings.preference.PreferenceCategory
1516
import app.revanced.patches.shared.misc.settings.preference.SwitchPreference
1617
import app.revanced.patches.shared.misc.settings.settingsPatch
@@ -58,6 +59,12 @@ val settingsPatch = bytecodePatch(
5859
execute {
5960
addResources("twitch", "misc.settings.settingsPatch")
6061

62+
preferences += NonInteractivePreference(
63+
key = "revanced_about",
64+
tag = "app.revanced.extension.shared.settings.preference.ReVancedAboutPreference",
65+
selectable = true,
66+
)
67+
6168
PreferenceScreen.MISC.OTHER.addPreferences(
6269
// The debug setting is shared across multiple apps and the key must be the same.
6370
// But the title and summary must be different, otherwise when the strings file is flattened

patches/src/main/resources/addresources/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,6 +1269,8 @@ This is because Crowdin requires temporarily flattening this file and removing t
12691269
</patch>
12701270
<patch id="misc.settings.settingsPatch">
12711271
<string name="revanced_settings">ReVanced Settings</string>
1272+
<string name="revanced_about_title">About</string>
1273+
<string name="revanced_about_summary">About ReVanced</string>
12721274
<string name="revanced_ads_screen_title">Ads</string>
12731275
<string name="revanced_ads_screen_summary">Ad blocking settings</string>
12741276
<string name="revanced_chat_screen_title">Chat</string>

0 commit comments

Comments
 (0)