Skip to content

Commit 960506e

Browse files
fix custom icon/name settings missing when using some selective patch combinations
1 parent b7f77a8 commit 960506e

File tree

4 files changed

+78
-35
lines changed

4 files changed

+78
-35
lines changed

extensions/music/src/main/java/app/revanced/extension/music/settings/preference/MusicPreferenceFragment.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.widget.Toolbar;
66

77
import app.revanced.extension.music.settings.MusicActivityHook;
8+
import app.revanced.extension.shared.GmsCoreSupport;
89
import app.revanced.extension.shared.Logger;
910
import app.revanced.extension.shared.Utils;
1011
import app.revanced.extension.shared.settings.preference.ToolbarPreferenceFragment;
@@ -30,6 +31,17 @@ protected void initialize() {
3031
preferenceScreen = getPreferenceScreen();
3132
Utils.sortPreferenceGroups(preferenceScreen);
3233
setPreferenceScreenToolbar(preferenceScreen);
34+
35+
// Clunky work around until preferences are custom classes that manage themselves.
36+
// Custom branding only works with non-root install. But the preferences must be
37+
// added during patched because of difficulties detecting during patching if it's
38+
// a root install. So instead the non-functional preferences are removed during runtime
39+
// if the app is mount (root) installation.
40+
if (GmsCoreSupport.isPackageNameOriginal()) {
41+
removePreferencesIfRooted(
42+
"revanced_custom_branding_name",
43+
"revanced_custom_branding_icon");
44+
}
3345
} catch (Exception ex) {
3446
Logger.printException(() -> "initialize failure", ex);
3547
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import android.graphics.drawable.Drawable;
77
import android.os.Build;
88
import android.preference.Preference;
9+
import android.preference.PreferenceGroup;
910
import android.preference.PreferenceScreen;
1011
import android.util.TypedValue;
1112
import android.view.ViewGroup;
@@ -22,6 +23,25 @@
2223

2324
@SuppressWarnings({"deprecation", "NewApi"})
2425
public class ToolbarPreferenceFragment extends AbstractPreferenceFragment {
26+
27+
/**
28+
* Removes the list of preferences from this fragment, if they exist.
29+
* @param keys Preference keys.
30+
*/
31+
protected void removePreferencesIfRooted(String ... keys) {
32+
for (String key : keys) {
33+
Preference pref = findPreference(key);
34+
if (pref != null) {
35+
Logger.printDebug(() -> "Removing root incompatible preference: " + key);
36+
// remove the preference
37+
PreferenceGroup parent = pref.getParent();
38+
if (parent != null) {
39+
parent.removePreference(pref);
40+
}
41+
}
42+
}
43+
}
44+
2545
/**
2646
* Sets toolbar for all nested preference screens.
2747
*/

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.preference.PreferenceScreen;
55
import android.widget.Toolbar;
66

7+
import app.revanced.extension.shared.GmsCoreSupport;
78
import app.revanced.extension.shared.Logger;
89
import app.revanced.extension.shared.Utils;
910
import app.revanced.extension.shared.settings.preference.ToolbarPreferenceFragment;
@@ -30,6 +31,17 @@ protected void initialize() {
3031
preferenceScreen = getPreferenceScreen();
3132
Utils.sortPreferenceGroups(preferenceScreen);
3233
setPreferenceScreenToolbar(preferenceScreen);
34+
35+
// Clunky work around until preferences are custom classes that manage themselves.
36+
// Custom branding only works with non-root install. But the preferences must be
37+
// added during patched because of difficulties detecting during patching if it's
38+
// a root install. So instead the non-functional preferences are removed during runtime
39+
// if the app is mount (root) installation.
40+
if (GmsCoreSupport.isPackageNameOriginal()) {
41+
removePreferencesIfRooted(
42+
"revanced_custom_branding_name",
43+
"revanced_custom_branding_icon");
44+
}
3345
} catch (Exception ex) {
3446
Logger.printException(() -> "initialize failure", ex);
3547
}

patches/src/main/kotlin/app/revanced/patches/shared/layout/branding/BaseCustomBrandingPatch.kt

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -166,49 +166,49 @@ internal fun baseCustomBrandingPatch(
166166
}
167167
}
168168
},
169-
resourcePatch {
170-
// Use a second resource patch so patching doesn't show custom branding finishing last.
171-
finalize {
172-
val useCustomName = customName != null
173-
val useCustomIcon = customIcon != null
174-
175-
if (setOrGetFallbackPackageName(originalAppPackageName) == originalAppPackageName) {
176-
if (useCustomName || useCustomIcon) {
177-
Logger.getLogger(this::class.java.name).warning(
178-
"Custom branding does not work with root installation. No changes applied."
179-
)
180-
}
181-
return@finalize
182-
}
169+
)
183170

184-
preferenceScreen.addPreferences(
185-
if (useCustomName) {
186-
ListPreference(
187-
key = "revanced_custom_branding_name",
188-
entriesKey = "revanced_custom_branding_name_custom_entries",
189-
entryValuesKey = "revanced_custom_branding_name_custom_entry_values"
190-
)
191-
} else {
192-
ListPreference("revanced_custom_branding_name")
193-
},
194-
if (useCustomIcon) {
195-
ListPreference(
196-
key = "revanced_custom_branding_icon",
197-
entriesKey = "revanced_custom_branding_icon_custom_entries",
198-
entryValuesKey = "revanced_custom_branding_icon_custom_entry_values"
199-
)
200-
} else {
201-
ListPreference("revanced_custom_branding_icon")
202-
}
171+
finalize {
172+
// Can only check if app is root installation by checking if change package name patch is in use.
173+
// and can only do that in the finalize block here.
174+
// The UI preferences cannot be selectively added here, because the settings finalize block
175+
// may have already run and the settings are already wrote to file.
176+
// Instead, show a warning if any patch option was used (A rooted device launcher ignores the manifest changes),
177+
// and the non-functional in-app settings are removed on app startup by extension code.
178+
if (customName != null || customIcon != null) {
179+
if (setOrGetFallbackPackageName(originalAppPackageName) == originalAppPackageName) {
180+
Logger.getLogger(this::class.java.name).warning(
181+
"Custom branding does not work with root installation. No changes applied."
203182
)
204183
}
205184
}
206-
)
185+
}
207186

208187
execute {
209188
addResources("shared", "layout.branding.baseCustomBrandingPatch")
210189
addResources(addResourcePatchName, "layout.branding.customBrandingPatch")
211190

191+
preferenceScreen.addPreferences(
192+
if (customName != null ) {
193+
ListPreference(
194+
key = "revanced_custom_branding_name",
195+
entriesKey = "revanced_custom_branding_name_custom_entries",
196+
entryValuesKey = "revanced_custom_branding_name_custom_entry_values"
197+
)
198+
} else {
199+
ListPreference("revanced_custom_branding_name")
200+
},
201+
if (customIcon != null) {
202+
ListPreference(
203+
key = "revanced_custom_branding_icon",
204+
entriesKey = "revanced_custom_branding_icon_custom_entries",
205+
entryValuesKey = "revanced_custom_branding_icon_custom_entry_values"
206+
)
207+
} else {
208+
ListPreference("revanced_custom_branding_icon")
209+
}
210+
)
211+
212212
val useCustomName = customName != null
213213
val useCustomIcon = customIcon != null
214214

@@ -228,7 +228,6 @@ internal fun baseCustomBrandingPatch(
228228
)
229229
}
230230

231-
232231
copyResources(
233232
"custom-branding",
234233
// Push notification 'small' icon.

0 commit comments

Comments
 (0)