From f72e7384834f015283c54dd99bff5a412b99693d Mon Sep 17 00:00:00 2001 From: inotia00 Date: Tue, 20 Jun 2023 17:30:55 +0900 Subject: [PATCH] feat(music): add `enable-sleep-timer` patch --- .../fingerprints/SleepTimerFingerprint.kt | 10 ++++ .../sleeptimer/patch/SleepTimerPatch.kt | 51 +++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 src/main/kotlin/app/revanced/patches/music/layout/sleeptimer/fingerprints/SleepTimerFingerprint.kt create mode 100644 src/main/kotlin/app/revanced/patches/music/layout/sleeptimer/patch/SleepTimerPatch.kt diff --git a/src/main/kotlin/app/revanced/patches/music/layout/sleeptimer/fingerprints/SleepTimerFingerprint.kt b/src/main/kotlin/app/revanced/patches/music/layout/sleeptimer/fingerprints/SleepTimerFingerprint.kt new file mode 100644 index 000000000..31955874b --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/music/layout/sleeptimer/fingerprints/SleepTimerFingerprint.kt @@ -0,0 +1,10 @@ +package app.revanced.patches.music.layout.sleeptimer.fingerprints + +import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint +import app.revanced.util.bytecode.isWide32LiteralExists + +object SleepTimerFingerprint : MethodFingerprint( + returnType = "Z", + parameters = listOf(), + customFingerprint = { it, _ -> it.isWide32LiteralExists(45372767) } +) \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/patches/music/layout/sleeptimer/patch/SleepTimerPatch.kt b/src/main/kotlin/app/revanced/patches/music/layout/sleeptimer/patch/SleepTimerPatch.kt new file mode 100644 index 000000000..b8ac5c03a --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/music/layout/sleeptimer/patch/SleepTimerPatch.kt @@ -0,0 +1,51 @@ +package app.revanced.patches.music.layout.sleeptimer.patch + +import app.revanced.extensions.toErrorResult +import app.revanced.patcher.annotation.Description +import app.revanced.patcher.annotation.Name +import app.revanced.patcher.annotation.Version +import app.revanced.patcher.data.BytecodeContext +import app.revanced.patcher.extensions.InstructionExtensions.addInstructions +import app.revanced.patcher.extensions.InstructionExtensions.getInstruction +import app.revanced.patcher.patch.BytecodePatch +import app.revanced.patcher.patch.PatchResult +import app.revanced.patcher.patch.PatchResultSuccess +import app.revanced.patcher.patch.annotations.DependsOn +import app.revanced.patcher.patch.annotations.Patch +import app.revanced.patches.music.layout.sleeptimer.fingerprints.SleepTimerFingerprint +import app.revanced.patches.music.utils.settings.resource.patch.MusicSettingsPatch +import app.revanced.patches.shared.annotation.YouTubeMusicCompatibility +import app.revanced.util.enum.CategoryType +import app.revanced.util.integrations.Constants.MUSIC_LAYOUT +import org.jf.dexlib2.iface.instruction.OneRegisterInstruction + +@Patch +@Name("enable-sleep-timer") +@Description("Add sleep timer to flyout menu.") +@DependsOn([MusicSettingsPatch::class]) +@YouTubeMusicCompatibility +@Version("0.0.1") +class SleepTimerPatch : BytecodePatch( + listOf(SleepTimerFingerprint) +) { + override fun execute(context: BytecodeContext): PatchResult { + + SleepTimerFingerprint.result?.let { + it.mutableMethod.apply { + val insertIndex = implementation!!.instructions.size - 1 + val targetRegister = getInstruction(insertIndex).registerA + + addInstructions( + insertIndex, """ + invoke-static {v$targetRegister}, $MUSIC_LAYOUT->enableSleepTimer(Z)Z + move-result v$targetRegister + """ + ) + } + } ?: return SleepTimerFingerprint.toErrorResult() + + MusicSettingsPatch.addMusicPreference(CategoryType.LAYOUT, "revanced_enable_sleep_timer", "true") + + return PatchResultSuccess() + } +} \ No newline at end of file