feat(YouTube): Add Custom player overlay opacity patch (#2721)

This gets rid of the `Hide player overlay` patch.

Co-authored-by: oSumAtrIX <johan.melkonyan1@web.de>
This commit is contained in:
johnconner122
2023-08-26 02:12:30 +02:00
committed by GitHub
co-authored by oSumAtrIX
parent c378869941
commit 004aae8258
4 changed files with 36 additions and 28 deletions
@@ -0,0 +1,8 @@
package app.revanced.patches.youtube.layout.player.overlay.annotations
import app.revanced.patcher.annotation.Compatibility
import app.revanced.patcher.annotation.Package
@Compatibility([Package("com.google.android.youtube")])
@Target(AnnotationTarget.CLASS)
internal annotation class PlayerOverlayPatchCompatibility
@@ -0,0 +1,22 @@
package app.revanced.patches.youtube.layout.player.overlay.bytecode.fingerprints
import app.revanced.extensions.containsConstantInstructionValue
import app.revanced.patcher.extensions.or
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import app.revanced.patches.youtube.layout.player.overlay.resource.patch.CustomPlayerOverlayOpacityResourcePatch
import com.android.tools.smali.dexlib2.AccessFlags
import com.android.tools.smali.dexlib2.Opcode
object CreatePlayerOverviewFingerprint : MethodFingerprint(
returnType = "V",
accessFlags = AccessFlags.PRIVATE or AccessFlags.FINAL,
opcodes = listOf(
Opcode.CONST,
Opcode.INVOKE_VIRTUAL,
Opcode.MOVE_RESULT_OBJECT,
Opcode.CHECK_CAST
),
customFingerprint = { methodDef, _ ->
methodDef.containsConstantInstructionValue(CustomPlayerOverlayOpacityResourcePatch.scrimOverlayId)
}
)
@@ -0,0 +1,45 @@
package app.revanced.patches.youtube.layout.player.overlay.bytecode.patch
import app.revanced.extensions.indexOfFirstConstantInstructionValue
import app.revanced.extensions.exception
import app.revanced.patcher.annotation.Description
import app.revanced.patcher.annotation.Name
import app.revanced.patcher.data.BytecodeContext
import app.revanced.patcher.extensions.InstructionExtensions.addInstruction
import app.revanced.patcher.extensions.InstructionExtensions.getInstruction
import app.revanced.patcher.patch.BytecodePatch
import app.revanced.patcher.patch.annotations.DependsOn
import app.revanced.patcher.patch.annotations.Patch
import app.revanced.patches.youtube.layout.player.overlay.annotations.PlayerOverlayPatchCompatibility
import app.revanced.patches.youtube.layout.player.overlay.bytecode.fingerprints.CreatePlayerOverviewFingerprint
import app.revanced.patches.youtube.layout.player.overlay.resource.patch.CustomPlayerOverlayOpacityResourcePatch
import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction
@Patch
@Name("Custom player overlay opacity")
@Description("Change the opacity of the player background, when player controls are visible.")
@DependsOn([CustomPlayerOverlayOpacityResourcePatch::class])
@PlayerOverlayPatchCompatibility
class CustomPlayerOverlayOpacityPatch : BytecodePatch(listOf(CreatePlayerOverviewFingerprint)) {
override fun execute(context: BytecodeContext) {
CreatePlayerOverviewFingerprint.result?.let { result ->
result.mutableMethod.apply {
val viewRegisterIndex =
indexOfFirstConstantInstructionValue(CustomPlayerOverlayOpacityResourcePatch.scrimOverlayId) + 3
val viewRegister =
getInstruction<OneRegisterInstruction>(viewRegisterIndex).registerA
val insertIndex = viewRegisterIndex + 1
addInstruction(
insertIndex,
"invoke-static { v$viewRegister }, " +
"$INTEGRATIONS_CLASS_DESCRIPTOR->changeOpacity(Landroid/widget/ImageView;)V"
)
}
} ?: throw CreatePlayerOverviewFingerprint.exception
}
private companion object {
const val INTEGRATIONS_CLASS_DESCRIPTOR = "Lapp/revanced/integrations/patches/CustomPlayerOverlayOpacityPatch;"
}
}
@@ -0,0 +1,38 @@
package app.revanced.patches.youtube.layout.player.overlay.resource.patch
import app.revanced.patcher.data.ResourceContext
import app.revanced.patcher.patch.ResourcePatch
import app.revanced.patcher.patch.annotations.DependsOn
import app.revanced.patches.shared.mapping.misc.patch.ResourceMappingPatch
import app.revanced.patches.shared.settings.preference.impl.InputType
import app.revanced.patches.shared.settings.preference.impl.StringResource
import app.revanced.patches.shared.settings.preference.impl.TextPreference
import app.revanced.patches.youtube.misc.settings.bytecode.patch.SettingsPatch
@DependsOn([SettingsPatch::class, ResourceMappingPatch::class])
class CustomPlayerOverlayOpacityResourcePatch : ResourcePatch {
override fun execute(context: ResourceContext) {
SettingsPatch.PreferenceScreen.LAYOUT.addPreferences(
TextPreference(
"revanced_player_overlay_opacity",
StringResource(
"revanced_player_overlay_opacity_title",
"Player overlay opacity"
),
StringResource(
"revanced_player_overlay_opacity_summary",
"Opacity value between 0-100, where 0 is transparent"
),
InputType.NUMBER
)
)
scrimOverlayId = ResourceMappingPatch.resourceMappings.single {
it.type == "id" && it.name == "scrim_overlay"
}.id
}
internal companion object {
var scrimOverlayId = -1L
}
}