Skip to content

Commit b2348b8

Browse files
create snaptarget instances using Unsafe
this removes a lot of sdk int checks and should be more reliable since the constructor parameters cannot be randomly swapped anymore
1 parent 62c7182 commit b2348b8

File tree

3 files changed

+11
-14
lines changed

3 files changed

+11
-14
lines changed

SplitScreenMods/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ dependencies {
1717
implementation(libs.androidx.fragment.ktx)
1818
implementation(libs.androidx.preference.ktx)
1919
implementation(project(":logger"))
20+
compileOnly(libs.androidx.performance.unsafe)
2021
}

SplitScreenMods/src/main/java/com/programminghoch10/SplitScreenMods/AdditionalSnapTargetsHook.kt

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import de.robv.android.xposed.XposedBridge
1414
import de.robv.android.xposed.XposedHelpers
1515
import de.robv.android.xposed.callbacks.XC_InitPackageResources
1616
import de.robv.android.xposed.callbacks.XC_LoadPackage
17+
import sun.misc.Unsafe
1718

1819
object AdditionalSnapTargetsHookConfig {
1920
val enabled = SnapModeHookConfig.enabled && CustomFixedRatioHookConfig.enabled && Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE
@@ -34,21 +35,14 @@ class AdditionalSnapTargetsHook : IXposedHookLoadPackage, IXposedHookInitPackage
3435

3536
val DividerSnapAlgorithmClass = XposedHelpers.findClass("com.android.wm.shell.common.split.DividerSnapAlgorithm", lpparam.classLoader)
3637
val SnapTargetClass = XposedHelpers.findClass(DividerSnapAlgorithmClass.name + "\$SnapTarget", lpparam.classLoader)
37-
val SnapTargetClassConstructor = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) XposedHelpers.findConstructorExact(
38-
SnapTargetClass,
39-
Int::class.java,
40-
Int::class.java,
41-
)
42-
else XposedHelpers.findConstructorExact(
43-
SnapTargetClass,
44-
Int::class.java,
45-
Int::class.java,
46-
Int::class.java,
47-
)
48-
38+
val UnsafeClass = XposedHelpers.findClass("sun.misc.Unsafe", lpparam.classLoader)
39+
val unsafe = XposedHelpers.callStaticMethod(UnsafeClass, "getUnsafe") as Unsafe
4940
fun createNewSnapTargetInstance(position: Int, snapPosition: Int): Any {
50-
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) SnapTargetClassConstructor.newInstance(position, snapPosition)
51-
else SnapTargetClassConstructor.newInstance(position, 0, snapPosition)
41+
val snapTarget = unsafe.allocateInstance(SnapTargetClass)
42+
XposedHelpers.setIntField(snapTarget, "position", position)
43+
XposedHelpers.setIntField(snapTarget, "snapPosition", snapPosition)
44+
XposedHelpers.setFloatField(snapTarget, "distanceMultiplier", 1f)
45+
return snapTarget
5246
}
5347

5448
XposedBridge.hookAllConstructors(DividerSnapAlgorithmClass, object : XC_MethodHook() {

gradle/libs.versions.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ jebrainsAnnotations = "26.0.2-1"
1212
kotlin = "2.2.20"
1313
kotlinxCoroutinesGuava = "1.10.2"
1414
libsu = "6.0.0"
15+
performanceUnsafe = "1.0.0-alpha01"
1516
preference = "1.2.1"
1617
xposed = "82"
1718

@@ -23,6 +24,7 @@ androidx-annotation = { module = "androidx.annotation:annotation", version.ref =
2324
androidx-collection-ktx = { module = "androidx.collection:collection-ktx", version.ref = "collection" }
2425
androidx-core-ktx = { module = "androidx.core:core-ktx", version.ref = "core" }
2526
androidx-fragment-ktx = { group = "androidx.fragment", name = "fragment-ktx", version.ref = "fragment" }
27+
androidx-performance-unsafe = { module = "androidx.performance:performance-unsafe", version.ref = "performanceUnsafe" }
2628
androidx-preference = { group = "androidx.preference", name = "preference", version.ref = "preference" }
2729
androidx-preference-ktx = { group = "androidx.preference", name = "preference-ktx", version.ref = "preference" }
2830
github-api = { module = "org.kohsuke:github-api", version.ref = "githubApi" }

0 commit comments

Comments
 (0)