|
| 1 | +package com.programminghoch10.SplitScreenMods |
| 2 | + |
| 3 | +import android.content.res.Resources |
| 4 | +import android.graphics.Rect |
| 5 | +import android.os.Build |
| 6 | +import com.programminghoch10.SplitScreenMods.AdditionalSnapTargetsHookConfig.enabled |
| 7 | +import com.programminghoch10.SplitScreenMods.BuildConfig.SHARED_PREFERENCES_NAME |
| 8 | +import de.binarynoise.logger.Logger.log |
| 9 | +import de.robv.android.xposed.IXposedHookInitPackageResources |
| 10 | +import de.robv.android.xposed.IXposedHookLoadPackage |
| 11 | +import de.robv.android.xposed.XC_MethodHook |
| 12 | +import de.robv.android.xposed.XSharedPreferences |
| 13 | +import de.robv.android.xposed.XposedBridge |
| 14 | +import de.robv.android.xposed.XposedHelpers |
| 15 | +import de.robv.android.xposed.callbacks.XC_InitPackageResources |
| 16 | +import de.robv.android.xposed.callbacks.XC_LoadPackage |
| 17 | + |
| 18 | +// https://github.com/LineageOS/android_frameworks_base/blob/f0964915f5992fd38cf1e5e3f87c0d3ac719aa09/libs/WindowManager/Shell/shared/src/com/android/wm/shell/shared/split/SplitScreenConstants.java |
| 19 | +const val SNAP_TO_2_33_66 = 0 |
| 20 | +const val SNAP_TO_2_50_50 = 1 |
| 21 | +const val SNAP_TO_2_66_33 = 2 |
| 22 | +const val SNAP_TO_NONE = 10 |
| 23 | +const val SNAP_TO_START_AND_DISMISS = 11 |
| 24 | +const val SNAP_TO_END_AND_DISMISS = 12 |
| 25 | + |
| 26 | +object AdditionalSnapTargetsHookConfig { |
| 27 | + val enabled = SnapModeHookConfig.enabled && CustomFixedRatioHookConfig.enabled && Build.VERSION.SDK_INT >= Build.VERSION_CODES.BAKLAVA |
| 28 | +} |
| 29 | + |
| 30 | +class AdditionalSnapTargetsHook : IXposedHookLoadPackage, IXposedHookInitPackageResources { |
| 31 | + override fun handleLoadPackage(lpparam: XC_LoadPackage.LoadPackageParam) { |
| 32 | + if (lpparam.packageName != "com.android.systemui") return |
| 33 | + if (!enabled) return |
| 34 | + log("handleLoadPackage(${lpparam.packageName} in process ${lpparam.processName})") |
| 35 | + val preferences = XSharedPreferences(BuildConfig.APPLICATION_ID, SHARED_PREFERENCES_NAME) |
| 36 | + val selectedSnapTargetsString = preferences.getString("SnapTargets", "SYSTEM")!! |
| 37 | + if (selectedSnapTargetsString == "SYSTEM" || selectedSnapTargetsString == "CUSTOM") return |
| 38 | + val selectedSnapTargets = selectedSnapTargetsString.split(",").map { it.toFloat() }.sorted().drop(1) |
| 39 | + log("additional snap targets are ${selectedSnapTargets.joinToString()}") |
| 40 | + |
| 41 | + if (selectedSnapTargets.isEmpty()) return // handled by resource hook below |
| 42 | + |
| 43 | + val DividerSnapAlgorithmClass = XposedHelpers.findClass("com.android.wm.shell.common.split.DividerSnapAlgorithm", lpparam.classLoader) |
| 44 | + val SnapTargetClass = XposedHelpers.findClass(DividerSnapAlgorithmClass.name + "\$SnapTarget", lpparam.classLoader) |
| 45 | + val SnapTargetClassConstructor = XposedHelpers.findConstructorExact(SnapTargetClass, Int::class.java, Int::class.java) |
| 46 | + |
| 47 | + XposedBridge.hookAllConstructors(DividerSnapAlgorithmClass, object : XC_MethodHook() { |
| 48 | + override fun afterHookedMethod(param: MethodHookParam) { |
| 49 | + log("after ${DividerSnapAlgorithmClass.simpleName} constructor") |
| 50 | + val res = param.args[0] as Resources |
| 51 | + val mTargets = XposedHelpers.getObjectField(param.thisObject, "mTargets") as ArrayList<Any?> |
| 52 | + if (mTargets.isEmpty()) return |
| 53 | + |
| 54 | + // reimplementation of inlined methods getStartInset() and getEndInset() |
| 55 | + val mIsLeftRightSplit = XposedHelpers.getBooleanField(param.thisObject, "mIsLeftRightSplit") |
| 56 | + val mInsets = XposedHelpers.getObjectField(param.thisObject, "mInsets") as Rect |
| 57 | + val startInset = if (mIsLeftRightSplit) mInsets.left else mInsets.top |
| 58 | + val endInset = if (mIsLeftRightSplit) mInsets.right else mInsets.bottom |
| 59 | + |
| 60 | + // basically reimplemented addFixedDivisionTargets |
| 61 | + val mDisplayWidth = XposedHelpers.getIntField(param.thisObject, "mDisplayWidth") |
| 62 | + val mDisplayHeight = XposedHelpers.getIntField(param.thisObject, "mDisplayHeight") |
| 63 | + val start = startInset |
| 64 | + val end = if (mIsLeftRightSplit) mDisplayWidth - endInset else mDisplayHeight - endInset |
| 65 | + val mDividerSize = XposedHelpers.getIntField(param.thisObject, "mDividerSize") |
| 66 | + val mCalculateRatiosBasedOnAvailableSpace = getCalculateRatiosBasedOnAvailableSpace(res) |
| 67 | + log("mCalculateRatiosBasedOnAvailableSpace=$mCalculateRatiosBasedOnAvailableSpace") |
| 68 | + val mMinimalSizeResizableTask = XposedHelpers.getIntField(param.thisObject, "mMinimalSizeResizableTask") |
| 69 | + log("mMinimalSizeResizableTask=$mMinimalSizeResizableTask") |
| 70 | + fun getSize(ratio: Float): Int { |
| 71 | + var size = (ratio * (end - start)).toInt() - mDividerSize / 2 |
| 72 | + if (mCalculateRatiosBasedOnAvailableSpace) size = Math.max(size, mMinimalSizeResizableTask) |
| 73 | + return size |
| 74 | + } |
| 75 | + |
| 76 | + for (snapTargetRatio in selectedSnapTargets) { |
| 77 | + val size = getSize(snapTargetRatio) |
| 78 | + val startPosition = start + size |
| 79 | + val endPosition = end - size |
| 80 | + |
| 81 | + // slightly changed reimplementation of inlined method maybeAddTarget() |
| 82 | + fun maybeAddTarget(position: Int, size: Int, snapPosition: Int?) { |
| 83 | + if (size < mMinimalSizeResizableTask) { |
| 84 | + log("Cannot add snap target ${snapTargetRatio} because it's size of ${size} would be less than minimal size ${mMinimalSizeResizableTask}!") |
| 85 | + return |
| 86 | + } |
| 87 | + log("adding snap target ${snapTargetRatio} at $position of size $size") |
| 88 | + val snapTarget = SnapTargetClassConstructor.newInstance(position, snapPosition ?: SNAP_TO_NONE) |
| 89 | + mTargets.add(snapTarget) |
| 90 | + } |
| 91 | + |
| 92 | + maybeAddTarget(startPosition, startPosition - startInset, SNAP_TO_2_33_66) |
| 93 | + maybeAddTarget(endPosition, end - endPosition, SNAP_TO_2_66_33) |
| 94 | + } |
| 95 | + |
| 96 | + log("final mTargets[${mTargets.size}]: ${mTargets.joinToString()}") |
| 97 | + } |
| 98 | + }) |
| 99 | + } |
| 100 | + |
| 101 | + fun getCalculateRatiosBasedOnAvailableSpace(res: Resources): Boolean { |
| 102 | + val mCalculateRatiosBasedOnAvailableSpaceId = res.getIdentifier("config_flexibleSplitRatios", "bool", "android") |
| 103 | + return res.getBoolean(mCalculateRatiosBasedOnAvailableSpaceId) |
| 104 | + } |
| 105 | + |
| 106 | + override fun handleInitPackageResources(resparam: XC_InitPackageResources.InitPackageResourcesParam) { |
| 107 | + if (resparam.packageName != "com.android.systemui") return |
| 108 | + if (!enabled) return |
| 109 | + log("handleInitPackageResources(${resparam.packageName})") |
| 110 | + val preferences = XSharedPreferences(BuildConfig.APPLICATION_ID, SHARED_PREFERENCES_NAME) |
| 111 | + val selectedSnapTargetsString = preferences.getString("SnapTargets", "SYSTEM")!! |
| 112 | + if (selectedSnapTargetsString == "SYSTEM" || selectedSnapTargetsString == "CUSTOM") return |
| 113 | + val selectedSnapTargets = selectedSnapTargetsString.split(",") |
| 114 | + if (selectedSnapTargets.isEmpty()) return |
| 115 | + val customRatio = selectedSnapTargets.map { it.toFloat() }.minOf { it } |
| 116 | + log("min split ratio is ${customRatio}") |
| 117 | + if (customRatio < 0 || customRatio >= 0.5f) return |
| 118 | + CustomFixedRatioHook.overrideFixedRatio(resparam, customRatio) |
| 119 | + } |
| 120 | +} |
0 commit comments