|
1 | 1 | package app.revanced.extension.youtube.patches;
|
2 | 2 |
|
| 3 | +import android.graphics.drawable.Drawable; |
| 4 | + |
3 | 5 | import androidx.annotation.Nullable;
|
4 | 6 |
|
5 | 7 | import java.util.Objects;
|
6 | 8 |
|
| 9 | +import app.revanced.extension.shared.Logger; |
7 | 10 | import app.revanced.extension.shared.Utils;
|
8 | 11 | import app.revanced.extension.youtube.settings.Settings;
|
9 | 12 |
|
10 | 13 | @SuppressWarnings("unused")
|
11 | 14 | public class ChangeHeaderPatch {
|
12 | 15 |
|
13 | 16 | 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"); |
20 | 23 |
|
21 | 24 | @Nullable
|
22 |
| - private final String resourceName; |
| 25 | + private final String attributeName; |
| 26 | + @Nullable |
| 27 | + private final String drawableName; |
23 | 28 |
|
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; |
26 | 32 | }
|
27 | 33 |
|
28 | 34 | /**
|
29 | 35 | * @return The attribute id of this header logo, or NULL if the logo should not be replaced.
|
30 | 36 | */
|
31 | 37 | @Nullable
|
32 | 38 | 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(); |
34 | 49 | return null;
|
35 | 50 | }
|
36 | 51 |
|
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; |
41 | 53 | }
|
42 |
| - } |
43 | 54 |
|
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 | + } |
46 | 74 |
|
47 | 75 | /**
|
48 | 76 | * Injection point.
|
49 | 77 | */
|
50 | 78 | 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; |
52 | 100 | }
|
53 | 101 | }
|
0 commit comments