add custom-double-tap-length patch

This commit is contained in:
inotia00 2023-04-30 18:23:09 +09:00
parent 4ef5714ce4
commit e46f2adf7b
6 changed files with 112 additions and 15 deletions

View File

@ -60,6 +60,18 @@ class PatchOptions : ResourcePatch {
)
)
/**
* Custom Double-tap to seek Values
*/
internal var CustomDoubleTapLengthArrays: String? by option(
PatchOption.StringOption(
key = "CustomDoubleTapLengthArrays",
default = "3, 5, 10, 15, 20, 30, 60, 120, 180",
title = "Double-tap to seek Values",
description = "A list of custom double-tap to seek lengths. Be sure to separate them with commas (,)."
)
)
/**
* Custom Speed Values
*/

View File

@ -0,0 +1,67 @@
package app.revanced.patches.youtube.layout.etc.doubletaplength.patch
import app.revanced.patcher.annotation.Description
import app.revanced.patcher.annotation.Name
import app.revanced.patcher.annotation.Version
import app.revanced.patcher.data.ResourceContext
import app.revanced.patcher.patch.PatchResult
import app.revanced.patcher.patch.PatchResultError
import app.revanced.patcher.patch.PatchResultSuccess
import app.revanced.patcher.patch.ResourcePatch
import app.revanced.patcher.patch.annotations.DependsOn
import app.revanced.patcher.patch.annotations.Patch
import app.revanced.patches.shared.annotation.YouTubeCompatibility
import app.revanced.patches.shared.patch.options.PatchOptions
import app.revanced.patches.youtube.misc.settings.resource.patch.SettingsPatch
import app.revanced.util.resources.ResourceHelper.addEntryValues
import app.revanced.util.resources.ResourceUtils
import app.revanced.util.resources.ResourceUtils.copyResources
import app.revanced.util.resources.ResourceUtils.copyXmlNode
@Patch
@Name("custom-double-tap-length")
@Description("Add 'double-tap to seek' value.")
@DependsOn(
[
PatchOptions::class,
SettingsPatch::class
]
)
@YouTubeCompatibility
@Version("0.0.1")
class DoubleTapLengthPatch : ResourcePatch {
override fun execute(context: ResourceContext): PatchResult {
/**
* Copy arrays
*/
context.copyResources(
"youtube/doubletap",
ResourceUtils.ResourceGroup(
"values-v21",
"arrays.xml"
)
)
val speed = PatchOptions.CustomDoubleTapLengthArrays
?: return PatchResultError("Invalid double-tap length array.")
val splits = speed.replace(" ","").split(",")
if (splits.isEmpty()) throw IllegalArgumentException("Invalid speed elements")
val speedElements = splits.map { it }
for (index in 0 until splits.count()) {
context.addEntryValues(TARGET_ARRAY_PATH, speedElements[index], TARGET_ENTRY_VALUE_NAME)
context.addEntryValues(TARGET_ARRAY_PATH, speedElements[index], TARGET_ENTRIES_NAME)
}
SettingsPatch.updatePatchStatus("custom-double-tap-length")
return PatchResultSuccess()
}
private companion object {
private const val TARGET_ARRAY_PATH = "res/values-v21/arrays.xml"
private const val TARGET_ENTRIES_NAME = "double_tap_length_entries"
private const val TARGET_ENTRY_VALUE_NAME = "double_tap_length_values"
}
}

View File

@ -14,8 +14,8 @@ import app.revanced.patches.shared.annotation.YouTubeCompatibility
import app.revanced.patches.shared.patch.options.PatchOptions
import app.revanced.patches.youtube.misc.settings.resource.patch.SettingsPatch
import app.revanced.patches.youtube.video.customspeed.bytecode.patch.CustomVideoSpeedBytecodePatch
import app.revanced.util.resources.ResourceHelper.addSpeedEntries
import app.revanced.util.resources.ResourceHelper.addSpeedEntryValues
import app.revanced.util.resources.ResourceHelper.addEntries
import app.revanced.util.resources.ResourceHelper.addEntryValues
import app.revanced.util.resources.ResourceUtils.copyXmlNode
@Patch
@ -45,8 +45,8 @@ class CustomVideoSpeedPatch : ResourcePatch {
if (splits.isEmpty()) throw IllegalArgumentException("Invalid speed elements")
val speedElements = splits.map { it }
for (index in 0 until splits.count()) {
context.addSpeedEntries(speedElements[index] + "x")
context.addSpeedEntryValues(speedElements[index])
context.addEntries(TARGET_ARRAY_PATH, speedElements[index] + "x", TARGET_ENTRY_VALUE_NAME)
context.addEntryValues(TARGET_ARRAY_PATH, speedElements[index], TARGET_ENTRIES_NAME)
}
/*
@ -63,4 +63,9 @@ class CustomVideoSpeedPatch : ResourcePatch {
return PatchResultSuccess()
}
private companion object {
private const val TARGET_ARRAY_PATH = "res/values/arrays.xml"
private const val TARGET_ENTRIES_NAME = "revanced_custom_video_speed_entry"
private const val TARGET_ENTRY_VALUE_NAME = "revanced_custom_video_speed_entry_value"
}
}

View File

@ -16,8 +16,6 @@ private fun Node.insertNode(tagName: String, targetNode: Node, block: Element.()
internal object ResourceHelper {
private const val TARGET_ARRAY_PATH = "res/values/arrays.xml"
private const val TARGET_PREFERENCE_PATH = "res/xml/revanced_prefs.xml"
private const val YOUTUBE_SETTINGS_PATH = "res/xml/settings_fragment.xml"
@ -28,8 +26,12 @@ internal object ResourceHelper {
targetPackage = newPackage
}
internal fun ResourceContext.addSpeedEntryValues(speedEntryValues: String) {
xmlEditor[TARGET_ARRAY_PATH].use {
internal fun ResourceContext.addEntryValues(
path: String,
speedEntryValues: String,
attributeName: String
) {
xmlEditor[path].use {
with(it.file) {
val resourcesNode = getElementsByTagName("resources").item(0) as Element
@ -38,7 +40,7 @@ internal object ResourceHelper {
for (i in 0 until resourcesNode.childNodes.length) {
val node = resourcesNode.childNodes.item(i) as? Element ?: continue
if (node.getAttribute("name") == "revanced_custom_video_speed_entry_value") {
if (node.getAttribute("name") == attributeName) {
newElement.appendChild(createTextNode(speedEntryValues))
node.appendChild(newElement)
@ -48,8 +50,12 @@ internal object ResourceHelper {
}
}
internal fun ResourceContext.addSpeedEntries(speedEntries: String) {
xmlEditor[TARGET_ARRAY_PATH].use {
internal fun ResourceContext.addEntries(
path: String,
speedEntries: String,
attributeName: String
) {
xmlEditor[path].use {
with(it.file) {
val resourcesNode = getElementsByTagName("resources").item(0) as Element
@ -58,7 +64,7 @@ internal object ResourceHelper {
for (i in 0 until resourcesNode.childNodes.length) {
val node = resourcesNode.childNodes.item(i) as? Element ?: continue
if (node.getAttribute("name") == "revanced_custom_video_speed_entry") {
if (node.getAttribute("name") == attributeName) {
newElement.appendChild(createTextNode(speedEntries))
node.appendChild(newElement)
@ -66,8 +72,8 @@ internal object ResourceHelper {
}
}
}
this[TARGET_ARRAY_PATH].writeText(
this[TARGET_ARRAY_PATH].readText().replace("1.0x", "@string/shorts_speed_control_normal_label")
this[path].writeText(
this[path].readText().replace("1.0x", "@string/shorts_speed_control_normal_label")
)
}

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="double_tap_length_entries">
</string-array>
<string-array name="double_tap_length_values">
</string-array>
</resources>

View File

@ -550,7 +550,7 @@
<Preference android:title="return-youtube-dislike" android:summary="@string/revanced_patches_excluded" android:selectable="false"/>
<Preference android:title="sponsorblock" android:summary="@string/revanced_patches_excluded" android:selectable="false"/>
<Preference android:title="microg-support" android:summary="@string/revanced_patches_excluded" android:selectable="false"/>
<Preference android:title="client-spoof" android:summary="@string/revanced_patches_excluded" android:selectable="false"/>
<Preference android:title="custom-double-tap-length" android:summary="@string/revanced_patches_excluded" android:selectable="false"/>
<Preference android:title="hide-double-tap-overlay-filter" android:summary="@string/revanced_patches_excluded" android:selectable="false"/>
<Preference android:title="hide-pip-notification" android:summary="@string/revanced_patches_excluded" android:selectable="false"/>
<Preference android:title="hide-tooltip-content" android:summary="@string/revanced_patches_excluded" android:selectable="false"/>