Skip to content

Commit 20cc141

Browse files
fix(YouTube - Hide layout components): Show correct custom header logo if 'Hide YouTube Doodles' is enabled (#5431)
1 parent 5e0da6d commit 20cc141

File tree

4 files changed

+92
-34
lines changed

4 files changed

+92
-34
lines changed
Lines changed: 66 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,101 @@
11
package app.revanced.extension.youtube.patches;
22

3+
import android.graphics.drawable.Drawable;
4+
35
import androidx.annotation.Nullable;
46

57
import java.util.Objects;
68

9+
import app.revanced.extension.shared.Logger;
710
import app.revanced.extension.shared.Utils;
811
import app.revanced.extension.youtube.settings.Settings;
912

1013
@SuppressWarnings("unused")
1114
public class ChangeHeaderPatch {
1215

1316
public enum HeaderLogo {
14-
DEFAULT(null),
15-
REGULAR("ytWordmarkHeader"),
16-
PREMIUM("ytPremiumWordmarkHeader"),
17-
REVANCED("revanced_header_logo"),
18-
REVANCED_MINIMAL("revanced_header_logo_minimal"),
19-
CUSTOM("custom_header");
17+
DEFAULT(null, null),
18+
REGULAR("ytWordmarkHeader", "yt_ringo2_wordmark_header"),
19+
PREMIUM("ytPremiumWordmarkHeader", "yt_ringo2_premium_wordmark_header"),
20+
REVANCED("revanced_header_logo", "revanced_header_logo"),
21+
REVANCED_MINIMAL("revanced_header_logo_minimal", "revanced_header_logo_minimal"),
22+
CUSTOM("custom_header", "custom_header");
2023

2124
@Nullable
22-
private final String resourceName;
25+
private final String attributeName;
26+
@Nullable
27+
private final String drawableName;
2328

24-
HeaderLogo(@Nullable String resourceName) {
25-
this.resourceName = resourceName;
29+
HeaderLogo(@Nullable String attributeName, @Nullable String drawableName) {
30+
this.attributeName = attributeName;
31+
this.drawableName = drawableName;
2632
}
2733

2834
/**
2935
* @return The attribute id of this header logo, or NULL if the logo should not be replaced.
3036
*/
3137
@Nullable
3238
private Integer getAttributeId() {
33-
if (resourceName == null) {
39+
if (attributeName == null) {
40+
return null;
41+
}
42+
43+
final int identifier = Utils.getResourceIdentifier(attributeName, "attr");
44+
if (identifier == 0) {
45+
// Identifier is zero if custom header setting was included in imported settings
46+
// and a custom image was not included during patching.
47+
Logger.printDebug(() -> "Could not find attribute: " + drawableName);
48+
Settings.HEADER_LOGO.resetToDefault();
3449
return null;
3550
}
3651

37-
final int identifier = Utils.getResourceIdentifier(resourceName, "attr");
38-
// Identifier is zero if custom header setting was included in imported settings
39-
// and a custom image was not included during patching.
40-
return identifier == 0 ? null : identifier;
52+
return identifier;
4153
}
42-
}
4354

44-
@Nullable
45-
private static final Integer headerLogoResource = Settings.HEADER_LOGO.get().getAttributeId();
55+
@Nullable
56+
public Drawable getDrawable() {
57+
if (drawableName == null) {
58+
return null;
59+
}
60+
61+
String drawableFullName = drawableName + (Utils.isDarkModeEnabled()
62+
? "_dark"
63+
: "_light");
64+
65+
final int identifier = Utils.getResourceIdentifier(drawableFullName, "drawable");
66+
if (identifier == 0) {
67+
Logger.printDebug(() -> "Could not find drawable: " + drawableFullName);
68+
Settings.HEADER_LOGO.resetToDefault();
69+
return null;
70+
}
71+
return Utils.getContext().getDrawable(identifier);
72+
}
73+
}
4674

4775
/**
4876
* Injection point.
4977
*/
5078
public static int getHeaderAttributeId(int original) {
51-
return Objects.requireNonNullElse(headerLogoResource, original);
79+
return Objects.requireNonNullElse(Settings.HEADER_LOGO.get().getAttributeId(), original);
80+
}
81+
82+
public static Drawable getDrawable(Drawable original) {
83+
Drawable logo = Settings.HEADER_LOGO.get().getDrawable();
84+
if (logo != null) {
85+
return logo;
86+
}
87+
88+
// TODO: If 'Hide Doodles' is enabled, this will force the regular logo regardless
89+
// what account the user has. This can be improved the next time a Doodle is
90+
// active and the attribute id is passed to this method so the correct
91+
// regular/premium logo is returned.
92+
logo = HeaderLogo.REGULAR.getDrawable();
93+
if (logo != null) {
94+
return logo;
95+
}
96+
97+
// Should never happen.
98+
Logger.printException(() -> "Could not find regular header logo resource");
99+
return original;
52100
}
53101
}

extensions/youtube/src/main/java/app/revanced/extension/youtube/patches/components/LayoutComponentsFilter.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44

55
import android.graphics.drawable.Drawable;
66
import android.view.View;
7+
import android.widget.ImageView;
78

89
import androidx.annotation.Nullable;
910

1011
import app.revanced.extension.shared.Logger;
1112
import app.revanced.extension.shared.Utils;
1213
import app.revanced.extension.youtube.StringTrieSearch;
14+
import app.revanced.extension.youtube.patches.ChangeHeaderPatch;
1315
import app.revanced.extension.youtube.settings.Settings;
1416
import app.revanced.extension.youtube.shared.NavigationBar;
1517
import app.revanced.extension.youtube.shared.PlayerType;
@@ -437,13 +439,11 @@ public static void hideInRelatedVideos(View chipView) {
437439
/**
438440
* Injection point.
439441
*/
440-
@Nullable
441-
public static Drawable hideYoodles(Drawable animatedYoodle) {
442-
if (HIDE_DOODLES_ENABLED) {
443-
return null;
444-
}
445-
446-
return animatedYoodle;
442+
public static void setDoodleDrawable(ImageView imageView, Drawable original) {
443+
Drawable replacement = HIDE_DOODLES_ENABLED
444+
? ChangeHeaderPatch.getDrawable(original)
445+
: original;
446+
imageView.setImageDrawable(replacement);
447447
}
448448

449449
private static final boolean HIDE_SHOW_MORE_BUTTON_ENABLED = Settings.HIDE_SHOW_MORE_BUTTON.get();

patches/src/main/kotlin/app/revanced/patches/youtube/layout/branding/header/ChangeHeaderPatch.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,26 @@ import app.revanced.util.forEachLiteralValueInstruction
2222
import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction
2323
import java.io.File
2424

25+
private val variants = arrayOf("light", "dark")
26+
2527
private const val EXTENSION_CLASS_DESCRIPTOR =
2628
"Lapp/revanced/extension/youtube/patches/ChangeHeaderPatch;"
2729

2830
private val changeHeaderBytecodePatch = bytecodePatch {
2931
dependsOn(resourceMappingPatch)
3032

3133
execute {
34+
// Resources are not used during patching, but extension code uses these
35+
// images so verify they exist.
36+
arrayOf(
37+
"yt_ringo2_wordmark_header",
38+
"yt_ringo2_premium_wordmark_header"
39+
).forEach { resource ->
40+
variants.forEach { theme ->
41+
resourceMappings["drawable", resource + "_" + theme]
42+
}
43+
}
44+
3245
arrayOf(
3346
"ytWordmarkHeader",
3447
"ytPremiumWordmarkHeader"
@@ -57,7 +70,6 @@ private val targetResourceDirectoryNames = mapOf(
5770
"mdpi" to "129px x 48px"
5871
).mapKeys { (dpi, _) -> "drawable-$dpi" }
5972

60-
private val variants = arrayOf("light", "dark")
6173

6274
/**
6375
* Header logos built into this patch.

patches/src/main/kotlin/app/revanced/patches/youtube/layout/hide/general/HideLayoutComponentsPatch.kt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import app.revanced.patcher.extensions.InstructionExtensions.addInstructionsWith
88
import app.revanced.patcher.extensions.InstructionExtensions.getInstruction
99
import app.revanced.patcher.extensions.InstructionExtensions.instructions
1010
import app.revanced.patcher.extensions.InstructionExtensions.removeInstruction
11+
import app.revanced.patcher.extensions.InstructionExtensions.replaceInstruction
1112
import app.revanced.patcher.patch.bytecodePatch
1213
import app.revanced.patcher.patch.resourcePatch
1314
import app.revanced.patcher.util.smali.ExternalLabel
@@ -379,16 +380,13 @@ val hideLayoutComponentsPatch = bytecodePatch(
379380
findInstructionIndicesReversedOrThrow {
380381
getReference<MethodReference>()?.name == "setImageDrawable"
381382
}.forEach { insertIndex ->
382-
val register = getInstruction<FiveRegisterInstruction>(insertIndex).registerD
383+
val drawableRegister = getInstruction<FiveRegisterInstruction>(insertIndex).registerD
384+
val imageViewRegister = getInstruction<FiveRegisterInstruction>(insertIndex).registerC
383385

384-
addInstructionsWithLabels(
386+
replaceInstruction(
385387
insertIndex,
386-
"""
387-
invoke-static { v$register }, $LAYOUT_COMPONENTS_FILTER_CLASS_DESCRIPTOR->hideYoodles(Landroid/graphics/drawable/Drawable;)Landroid/graphics/drawable/Drawable;
388-
move-result-object v$register
389-
if-eqz v$register, :hide
390-
""",
391-
ExternalLabel("hide", getInstruction(insertIndex + 1)),
388+
"invoke-static { v$imageViewRegister, v$drawableRegister }, $LAYOUT_COMPONENTS_FILTER_CLASS_DESCRIPTOR->" +
389+
"setDoodleDrawable(Landroid/widget/ImageView;Landroid/graphics/drawable/Drawable;)V"
392390
)
393391
}
394392
}

0 commit comments

Comments
 (0)