Skip to content

Commit b2982a0

Browse files
committed
refactor: Replace HookFunction with a dedicated Patch class
1 parent 2dd897c commit b2982a0

File tree

60 files changed

+365
-231
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+365
-231
lines changed

app/src/main/java/io/github/chsbuffer/revancedxposed/BaseHook.kt

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ import org.luckypray.dexkit.wrap.DexMethod
2626
import java.lang.reflect.Constructor
2727
import java.lang.reflect.Member
2828
import java.lang.reflect.Method
29-
import kotlin.reflect.KFunction0
3029
import kotlin.reflect.KProperty0
3130
import kotlin.system.measureTimeMillis
3231

33-
private typealias HookFunction = KFunction0<Unit>
32+
fun patch(name: String = "", description: String = "", func: BaseHook.() -> Unit) =
33+
Patch(name, description, func)
34+
35+
class Patch(val name: String, val description: String, val run: BaseHook.() -> Unit)
3436

3537
interface IHook {
3638
val classLoader: ClassLoader
@@ -91,7 +93,8 @@ class SharedPrefCache(app: Application) : DexKitCacheBridge.Cache {
9193

9294
override fun getList(
9395
key: String, default: List<String>?
94-
): List<String>? = map.getOrDefault(key, null)?.takeIf(String::isNotBlank)?.split('|') ?: default
96+
): List<String>? =
97+
map.getOrDefault(key, null)?.takeIf(String::isNotBlank)?.split('|') ?: default
9598

9699
override fun put(key: String, value: String) {
97100
map.put(key, value)
@@ -122,10 +125,9 @@ class DependedHookFailedException(
122125
abstract class BaseHook(private val app: Application, val lpparam: LoadPackageParam) : IHook {
123126
override val classLoader = lpparam.classLoader!!
124127

125-
// hooks
126-
abstract val hooks: Array<HookFunction>
127-
private val appliedHooks = mutableSetOf<HookFunction>()
128-
private val failedHooks = mutableListOf<HookFunction>()
128+
abstract val patches: Array<Patch>
129+
private val appliedPatches = mutableSetOf<Patch>()
130+
private val failedPatches = mutableListOf<Patch>()
129131

130132
// cache
131133
private val moduleRel = BuildConfig.COMMIT_HASH
@@ -172,32 +174,32 @@ abstract class BaseHook(private val app: Application, val lpparam: LoadPackagePa
172174
}
173175

174176
private fun applyHooks() {
175-
hooks.forEach { hook ->
176-
if (appliedHooks.contains(hook)) return@forEach
177-
runCatching(hook).onFailure { err ->
177+
patches.forEach { hook ->
178+
if (appliedPatches.contains(hook)) return@forEach
179+
runCatching { hook.run(this) }.onFailure { err ->
178180
XposedBridge.log(err)
179-
failedHooks.add(hook)
181+
failedPatches.add(hook)
180182
}.onSuccess {
181-
appliedHooks.add(hook)
183+
appliedPatches.add(hook)
182184
}
183185
}
184186
}
185187

186188
private fun handleResult() {
187189
cache.saveCache()
188-
val success = failedHooks.isEmpty()
190+
val success = failedPatches.isEmpty()
189191
if (!success) {
190192
XposedBridge.log("${lpparam.appInfo.packageName} version: ${getAppVersion()}")
191-
Utils.showToastLong("Error while apply following Hooks:\n${failedHooks.joinToString { it.name }}")
193+
Utils.showToastLong("Error while apply following patches:\n${failedPatches.joinToString { it.name }}")
192194
}
193195
}
194196

195197
private fun logDebugInfo() {
196-
val success = failedHooks.isEmpty()
198+
val success = failedPatches.isEmpty()
197199
if (DEBUG) {
198200
XposedBridge.log("${lpparam.appInfo.packageName} version: ${getAppVersion()}")
199201
if (success) {
200-
Utils.showToastLong("apply hooks success")
202+
Utils.showToastLong("apply patches success")
201203
}
202204
}
203205
}
@@ -213,18 +215,18 @@ abstract class BaseHook(private val app: Application, val lpparam: LoadPackagePa
213215
return "$versionName ($versionCode)"
214216
}
215217

216-
fun dependsOn(vararg hooks: HookFunction) {
217-
hooks.forEach { hook ->
218-
if (appliedHooks.contains(hook)) return@forEach
219-
runCatching(hook).onFailure { err ->
218+
fun dependsOn(vararg patches: Patch) {
219+
patches.forEach { hook ->
220+
if (appliedPatches.contains(hook)) return@forEach
221+
runCatching { (hook.run(this)) }.onFailure { err ->
220222
throw DependedHookFailedException(hook.name, err)
221223
}.onSuccess {
222-
appliedHooks.add(hook)
224+
appliedPatches.add(hook)
223225
}
224226
}
225227
}
226228

227-
fun ExtensionResourceHook() {
229+
val ExtensionResourceHook = patch {
228230
app.addModuleAssets()
229231
StringRef.resources = app.resources
230232
StringRef.packageName = BuildConfig.APPLICATION_ID

app/src/main/java/io/github/chsbuffer/revancedxposed/meta/MetaHook.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ import io.github.chsbuffer.revancedxposed.meta.ads.HideAds
77

88
class MetaHook(app: Application, lpparam: XC_LoadPackage.LoadPackageParam) :
99
BaseHook(app, lpparam) {
10-
override val hooks = arrayOf(::HideAds)
10+
override val patches = arrayOf(HideAds)
1111
}
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package io.github.chsbuffer.revancedxposed.meta.ads
22

33
import de.robv.android.xposed.XC_MethodReplacement
4-
import io.github.chsbuffer.revancedxposed.meta.MetaHook
4+
import io.github.chsbuffer.revancedxposed.patch
55

6-
fun MetaHook.HideAds() {
6+
val HideAds = patch(
7+
name = "Hide ads",
8+
) {
79
::adInjectorFingerprint.hookMethod(XC_MethodReplacement.DO_NOTHING)
810
}

app/src/main/java/io/github/chsbuffer/revancedxposed/music/MusicHook.kt

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ import io.github.chsbuffer.revancedxposed.music.misc.settings.SettingsHook
1414
import io.github.chsbuffer.revancedxposed.shared.misc.CheckRecycleBitmapMediaSession
1515

1616
class MusicHook(app: Application, lpparam: LoadPackageParam) : BaseHook(app, lpparam) {
17-
override val hooks = arrayOf(
18-
::ExtensionResourceHook,
19-
::HideVideoAds,
20-
::BackgroundPlayback,
21-
::HideUpgradeButton,
22-
::HideGetPremium,
23-
::EnableExclusiveAudioPlayback,
24-
::CheckRecycleBitmapMediaSession,
25-
::EnableDebugging,
26-
::SanitizeSharingLinks,
27-
::SettingsHook
17+
override val patches = arrayOf(
18+
ExtensionResourceHook,
19+
HideVideoAds,
20+
BackgroundPlayback,
21+
HideUpgradeButton,
22+
HideGetPremium,
23+
EnableExclusiveAudioPlayback,
24+
CheckRecycleBitmapMediaSession,
25+
EnableDebugging,
26+
SanitizeSharingLinks,
27+
SettingsHook
2828
)
2929
}

app/src/main/java/io/github/chsbuffer/revancedxposed/music/ad/video/HideVideoAds.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package io.github.chsbuffer.revancedxposed.music.ad.video
22

33
import app.revanced.extension.music.patches.HideVideoAdsPatch
4-
import io.github.chsbuffer.revancedxposed.music.MusicHook
4+
import io.github.chsbuffer.revancedxposed.patch
55
import io.github.chsbuffer.revancedxposed.music.misc.settings.PreferenceScreen
66
import io.github.chsbuffer.revancedxposed.scopedHook
77
import io.github.chsbuffer.revancedxposed.shared.misc.settings.preference.SwitchPreference
88

9-
fun MusicHook.HideVideoAds() {
9+
val HideVideoAds = patch(
10+
name = "Hide music video ads",
11+
description = "Adds an option to hide ads that appear while listening to or streaming music videos, podcasts, or songs.",
12+
) {
1013
PreferenceScreen.ADS.addPreferences(
1114
SwitchPreference("revanced_music_hide_video_ads"),
1215
)
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package io.github.chsbuffer.revancedxposed.music.audio.exclusiveaudio
22

33
import de.robv.android.xposed.XC_MethodReplacement
4-
import io.github.chsbuffer.revancedxposed.music.MusicHook
4+
import io.github.chsbuffer.revancedxposed.patch
55

6-
fun MusicHook.EnableExclusiveAudioPlayback() {
6+
val EnableExclusiveAudioPlayback = patch(
7+
name = "Enable exclusive audio playback",
8+
description = "Enables the option to play audio without video.",
9+
) {
710
::AllowExclusiveAudioPlaybackFingerprint.hookMethod(XC_MethodReplacement.returnConstant(true))
811
}

app/src/main/java/io/github/chsbuffer/revancedxposed/music/layout/premium/HideGetPremiumPatch.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ import android.view.View
44
import app.revanced.extension.music.patches.HideGetPremiumPatch
55
import app.revanced.extension.shared.Logger
66
import app.revanced.extension.shared.Utils
7-
import io.github.chsbuffer.revancedxposed.music.MusicHook
7+
import io.github.chsbuffer.revancedxposed.patch
88
import io.github.chsbuffer.revancedxposed.music.misc.settings.PreferenceScreen
99
import io.github.chsbuffer.revancedxposed.shared.misc.settings.preference.SwitchPreference
1010

11-
fun MusicHook.HideGetPremium() {
11+
val HideGetPremium = patch(
12+
name = "Hide 'Get Music Premium'",
13+
description = "Adds an option to hide the \"Get Music Premium\" label in the settings and account menu.",
14+
) {
1215
PreferenceScreen.ADS.addPreferences(
1316
SwitchPreference("revanced_music_hide_get_premium_label"),
1417
)

app/src/main/java/io/github/chsbuffer/revancedxposed/music/layout/upgradebutton/HideUpgradeButtonPatch.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package io.github.chsbuffer.revancedxposed.music.layout.upgradebutton
22

33
import de.robv.android.xposed.XposedHelpers
4-
import io.github.chsbuffer.revancedxposed.music.MusicHook
4+
import io.github.chsbuffer.revancedxposed.patch
55

6-
fun MusicHook.HideUpgradeButton() {
6+
val HideUpgradeButton = patch(
7+
name = "Hide upgrade button",
8+
description = "Hides the upgrade tab from the pivot bar.",
9+
) {
10+
// TODO Patch is obsolete and was replaced by navigation bar patch
711
::pivotBarConstructorFingerprint.hookMethod {
812
val pivotBarElementField = ::pivotBarElementField.field
913

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package io.github.chsbuffer.revancedxposed.music.misc.backgroundplayback
22

33
import de.robv.android.xposed.XC_MethodReplacement
4-
import io.github.chsbuffer.revancedxposed.music.MusicHook
4+
import io.github.chsbuffer.revancedxposed.patch
55

6-
fun MusicHook.BackgroundPlayback() {
6+
val BackgroundPlayback = patch(
7+
name = "Remove background playback restrictions",
8+
description = "Removes restrictions on background playback, including playing kids videos in the background.",
9+
) {
710
::backgroundPlaybackDisableFingerprint.hookMethod(XC_MethodReplacement.returnConstant(true))
811
::kidsBackgroundPlaybackPolicyControllerFingerprint.hookMethod(XC_MethodReplacement.DO_NOTHING)
912
}

app/src/main/java/io/github/chsbuffer/revancedxposed/music/misc/debugging/EnableDebuggingPatch.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package io.github.chsbuffer.revancedxposed.music.misc.debugging
22

3-
import io.github.chsbuffer.revancedxposed.shared.misc.debugging.EnableDebugging
4-
import io.github.chsbuffer.revancedxposed.music.MusicHook
3+
import io.github.chsbuffer.revancedxposed.patch
54
import io.github.chsbuffer.revancedxposed.music.misc.settings.PreferenceScreen
5+
import io.github.chsbuffer.revancedxposed.shared.misc.debugging.EnableDebugging
66

7-
fun MusicHook.EnableDebugging() {
7+
val EnableDebugging = patch(
8+
name = "Enable debugging",
9+
description = "Adds options for debugging and exporting ReVanced logs to the clipboard.",
10+
) {
811
EnableDebugging(
912
preferenceScreen = PreferenceScreen.MISC
1013
)

0 commit comments

Comments
 (0)