From 84f585492e4be3604c6c7680ffb3bebcea5a675f Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Fri, 28 Mar 2025 19:34:00 +0100 Subject: [PATCH] fix(Spotify): Ignore optional attributes if not present (#4688) --- .../spotify/misc/UnlockPremiumPatch.java | 99 ++++++++++++------- 1 file changed, 66 insertions(+), 33 deletions(-) diff --git a/extensions/spotify/src/main/java/app/revanced/extension/spotify/misc/UnlockPremiumPatch.java b/extensions/spotify/src/main/java/app/revanced/extension/spotify/misc/UnlockPremiumPatch.java index b295b39e1..1410b308e 100644 --- a/extensions/spotify/src/main/java/app/revanced/extension/spotify/misc/UnlockPremiumPatch.java +++ b/extensions/spotify/src/main/java/app/revanced/extension/spotify/misc/UnlockPremiumPatch.java @@ -1,54 +1,87 @@ package app.revanced.extension.spotify.misc; +import static java.lang.Boolean.FALSE; +import static java.lang.Boolean.TRUE; + import com.spotify.remoteconfig.internal.AccountAttribute; -import java.util.HashMap; +import java.util.List; import java.util.Map; +import java.util.Objects; import app.revanced.extension.shared.Logger; -/** - * @noinspection unused - */ +@SuppressWarnings("unused") public final class UnlockPremiumPatch { - private static final Map OVERRIDES = new HashMap<>() {{ - // Disables player and app ads. - put("ads", false); - // Works along on-demand, allows playing any song without restriction. - put("player-license", "premium"); - // Disables shuffle being initially enabled when first playing a playlist. - put("shuffle", false); - // Allows playing any song on-demand, without a shuffled order. - put("on-demand", true); - // Make sure playing songs is not disabled remotely and playlists show up. - put("streaming", true); - // Allows adding songs to queue and removes the smart shuffle mode restriction, - // allowing to pick any of the other modes. - put("pick-and-shuffle", false); - // Disables shuffle-mode streaming-rule, which forces songs to be played shuffled - // and breaks the player when other patches are applied. - put("streaming-rules", ""); - // Enables premium UI in settings and removes the premium button in the nav-bar. - put("nft-disabled", "1"); - // Enable Cross-Platform Spotify Car Thing. - put("can_use_superbird", true); - // Removes the premium button in the nav-bar for tablet users. - put("tablet-free", false); - }}; + private static class OverrideAttribute { + /** + * Account attribute key. + */ + final String key; + + /** + * Override value. + */ + final Object overrideValue; + + /** + * If this attribute is expected to be present in all situations. + * If false, then no error is raised if the attribute is missing. + */ + final boolean isExpected; + + OverrideAttribute(String key, Object overrideValue) { + this(key, overrideValue, true); + } + + OverrideAttribute(String key, Object overrideValue, boolean isExpected) { + this.key = Objects.requireNonNull(key); + this.overrideValue = Objects.requireNonNull(overrideValue); + this.isExpected = isExpected; + } + } + + private static final List OVERRIDES = List.of( + // Disables player and app ads. + new OverrideAttribute("ads", FALSE), + // Works along on-demand, allows playing any song without restriction. + new OverrideAttribute("player-license", "premium"), + // Disables shuffle being initially enabled when first playing a playlist. + new OverrideAttribute("shuffle", FALSE), + // Allows playing any song on-demand, without a shuffled order. + new OverrideAttribute("on-demand", TRUE), + // Make sure playing songs is not disabled remotely and playlists show up. + new OverrideAttribute("streaming", TRUE), + // Allows adding songs to queue and removes the smart shuffle mode restriction, + // allowing to pick any of the other modes. + new OverrideAttribute("pick-and-shuffle", FALSE), + // Disables shuffle-mode streaming-rule, which forces songs to be played shuffled + // and breaks the player when other patches are applied. + new OverrideAttribute("streaming-rules", ""), + // Enables premium UI in settings and removes the premium button in the nav-bar. + new OverrideAttribute("nft-disabled", "1"), + // Enable Spotify Car Thing hardware device. + // Device is discontinued and no longer works with the latest releases, + // but it might still work with older app targets. + new OverrideAttribute("can_use_superbird", TRUE, false), + // Removes the premium button in the nav-bar for tablet users. + new OverrideAttribute("tablet-free", FALSE, false) + ); /** * Injection point. */ public static void overrideAttribute(Map attributes) { try { - for (var entry : OVERRIDES.entrySet()) { - var key = entry.getKey(); - var attribute = attributes.get(key); + for (var override : OVERRIDES) { + var attribute = attributes.get(override.key); if (attribute == null) { - Logger.printException(() -> "Account attribute not found: " + key); + if (override.isExpected) { + Logger.printException(() -> "''" + override.key + "' expected but not found"); + } } else { - attribute.value_ = entry.getValue(); + attribute.value_ = override.overrideValue; } } } catch (Exception ex) {