|
| 1 | +package io.github.chsbuffer.revancedxposed.youtube.misc |
| 2 | + |
| 3 | +import android.view.View |
| 4 | +import android.view.ViewGroup |
| 5 | +import android.view.ViewStub |
| 6 | +import android.widget.ImageView |
| 7 | +import app.revanced.extension.shared.Utils |
| 8 | +import app.revanced.extension.youtube.patches.PlayerControlsPatch |
| 9 | +import io.github.chsbuffer.revancedxposed.AccessFlags |
| 10 | +import io.github.chsbuffer.revancedxposed.fingerprint |
| 11 | +import io.github.chsbuffer.revancedxposed.scopedHook |
| 12 | +import io.github.chsbuffer.revancedxposed.youtube.YoutubeHook |
| 13 | +import org.luckypray.dexkit.wrap.DexMethod |
| 14 | + |
| 15 | +class BottomControl( |
| 16 | + val id: Int, |
| 17 | + @JvmField val initializeButton: (controlsView: ViewGroup) -> Unit, |
| 18 | + @JvmField val setVisibility: (Boolean, Boolean) -> Unit, |
| 19 | + @JvmField val setVisibilityImmediate: (Boolean) -> Unit, |
| 20 | + // Patch works without this hook, but it is needed to use the correct fade out animation |
| 21 | + // duration when tapping the overlay to dismiss. |
| 22 | + @JvmField val setVisibilityNegatedImmediate: () -> Unit |
| 23 | +) |
| 24 | + |
| 25 | +@JvmField |
| 26 | +var visibilityImmediateCallbacksExistModified = false |
| 27 | + |
| 28 | +@JvmField |
| 29 | +val bottomControls = mutableListOf<BottomControl>() |
| 30 | + |
| 31 | +private val bottomControlLayouts = mutableListOf<Int>() |
| 32 | +fun addBottomControl(layout: Int) { |
| 33 | + bottomControlLayouts.add(layout) |
| 34 | +} |
| 35 | + |
| 36 | +fun initializeBottomControl(bottomControl: BottomControl) { |
| 37 | + bottomControls.add(bottomControl) |
| 38 | + |
| 39 | + if (!visibilityImmediateCallbacksExistModified) { |
| 40 | + visibilityImmediateCallbacksExistModified = true |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +fun YoutubeHook.PlayerControls() { |
| 45 | + DexMethod("Landroid/view/ViewStub;->inflate()Landroid/view/View;").hookMethod { |
| 46 | + after { |
| 47 | + val viewStub = it.thisObject as ViewStub |
| 48 | + val viewStubName = Utils.getContext().resources.getResourceName(viewStub.id) |
| 49 | +// Logger.printDebug { "ViewStub->inflate()" + viewStubName } |
| 50 | + |
| 51 | + if (viewStubName.endsWith("bottom_ui_container_stub")) { |
| 52 | +// Logger.printDebug { "inject into $viewStubName" } |
| 53 | + val viewGroup = it.result as ViewGroup |
| 54 | + |
| 55 | + bottomControlLayouts.forEach { layout -> |
| 56 | + viewStub.layoutInflater.inflate(layout, viewGroup, true) |
| 57 | + } |
| 58 | + |
| 59 | + bottomControls.forEach { bottomControl -> bottomControl.initializeButton(viewGroup) } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + initInjectVisibilityCheckCall() |
| 64 | + |
| 65 | + val youtube_controls_bottom_ui_container = |
| 66 | + Utils.getResourceIdentifier("youtube_controls_bottom_ui_container", "id") |
| 67 | + |
| 68 | + DexMethod("Landroid/support/constraint/ConstraintLayout;->onLayout(ZIIII)V").hookMethod { |
| 69 | + after { |
| 70 | + val controlsView = it.thisObject as ViewGroup |
| 71 | + if (controlsView.id != youtube_controls_bottom_ui_container) return@after |
| 72 | + |
| 73 | + var rightButton = |
| 74 | + Utils.getChildViewByResourceName<View>(controlsView, "fullscreen_button") |
| 75 | + |
| 76 | + for (bottomControl in bottomControls) { |
| 77 | + val leftButton = controlsView.findViewById<View>(bottomControl.id) |
| 78 | + // put this button to the left |
| 79 | + leftButton.x = rightButton.x - leftButton.width |
| 80 | + leftButton.y = rightButton.y |
| 81 | + rightButton = leftButton |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +private fun YoutubeHook.initInjectVisibilityCheckCall() { |
| 89 | + getDexMethod("controlsOverlayVisibilityFingerprint") { |
| 90 | + val controls_layout_stub_id = Utils.getResourceIdentifier("controls_layout_stub", "id") |
| 91 | + val playerTopControlsInflateFingerprint = fingerprint { |
| 92 | + accessFlags(AccessFlags.PUBLIC, AccessFlags.FINAL) |
| 93 | + returns("V") |
| 94 | + parameters() |
| 95 | + literal { controls_layout_stub_id } |
| 96 | + } |
| 97 | + |
| 98 | + fingerprint { |
| 99 | + accessFlags(AccessFlags.PRIVATE, AccessFlags.FINAL) |
| 100 | + returns("V") |
| 101 | + parameters("Z", "Z") |
| 102 | + classMatcher { |
| 103 | + descriptor = playerTopControlsInflateFingerprint.declaredClass!!.descriptor |
| 104 | + } |
| 105 | + } |
| 106 | + }.hookMethod { |
| 107 | + before { param -> |
| 108 | + bottomControls.forEach { |
| 109 | + it.setVisibility(param.args[0] as Boolean, param.args[1] as Boolean) |
| 110 | + } |
| 111 | +// Logger.printDebug { "setVisibility(visible: ${param.args[0]}, animated: ${param.args[1]})" } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + // Hook the fullscreen close button. Used to fix visibility |
| 116 | + // when seeking and other situations. |
| 117 | + val fullscreen_button_id = Utils.getResourceIdentifier("fullscreen_button", "id") |
| 118 | + getDexMethod("overlayViewInflateFingerprint") { |
| 119 | + val heatseeker_viewstub_id = Utils.getResourceIdentifier("heatseeker_viewstub", "id") |
| 120 | + fingerprint { |
| 121 | + accessFlags(AccessFlags.PUBLIC, AccessFlags.FINAL) |
| 122 | + returns("V") |
| 123 | + parameters("Landroid/view/View;") |
| 124 | + methodMatcher { |
| 125 | + addUsingNumber(fullscreen_button_id) |
| 126 | + addUsingNumber(heatseeker_viewstub_id) |
| 127 | + } |
| 128 | + } |
| 129 | + }.hookMethod(scopedHook(DexMethod("Landroid/view/View;->findViewById(I)Landroid/view/View;").toMember()) { |
| 130 | + after { |
| 131 | + if (it.args[0] == fullscreen_button_id) { |
| 132 | + PlayerControlsPatch.setFullscreenCloseButton(it.result as ImageView) |
| 133 | + } |
| 134 | + } |
| 135 | + }) |
| 136 | + |
| 137 | + // |
| 138 | + getDexMethod("motionEventFingerprint") { |
| 139 | + val youtubeControlsOverlayFingerprint = fingerprint { |
| 140 | + accessFlags(AccessFlags.PRIVATE, AccessFlags.FINAL) |
| 141 | + returns("V") |
| 142 | + parameters() |
| 143 | + methodMatcher { |
| 144 | + addInvoke { name = "setFocusableInTouchMode" } |
| 145 | + addUsingNumber(Utils.getResourceIdentifier("inset_overlay_view_layout", "id")) |
| 146 | + addUsingNumber(Utils.getResourceIdentifier("scrim_overlay", "id")) |
| 147 | + } |
| 148 | + } |
| 149 | + fingerprint { |
| 150 | + returns("V") |
| 151 | + parameters("Landroid/view/MotionEvent;") |
| 152 | + methodMatcher { |
| 153 | + addInvoke { name = "setTranslationY" } |
| 154 | + } |
| 155 | + classMatcher { |
| 156 | + descriptor = youtubeControlsOverlayFingerprint.declaredClass!!.descriptor |
| 157 | + } |
| 158 | + } |
| 159 | + }.hookMethod(scopedHook(DexMethod("Landroid/view/View;->setTranslationY(F)V").toMethod()) { |
| 160 | + after { |
| 161 | + // FIXME Animation lags behind |
| 162 | + bottomControls.forEach { it.setVisibilityNegatedImmediate() } |
| 163 | +// Logger.printDebug { "setVisibilityNegatedImmediate()" } |
| 164 | + } |
| 165 | + }) |
| 166 | +} |
0 commit comments