|
| 1 | +/* |
| 2 | + * This file is part of HyperCeiler. |
| 3 | + * |
| 4 | + * HyperCeiler is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU Affero General Public License as |
| 6 | + * published by the Free Software Foundation, either version 3 of the |
| 7 | + * License. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU Affero General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU Affero General Public License |
| 15 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + * |
| 17 | + * Copyright (C) 2023-2026 HyperCeiler Contributions |
| 18 | + */ |
| 19 | +package com.sevtinge.hyperceiler.libhook.rules.home.gesture; |
| 20 | + |
| 21 | +import com.sevtinge.hyperceiler.common.log.XposedLog; |
| 22 | +import com.sevtinge.hyperceiler.common.utils.PrefsBridge; |
| 23 | +import com.sevtinge.hyperceiler.libhook.base.BaseHook; |
| 24 | +import com.sevtinge.hyperceiler.libhook.callback.IMethodHook; |
| 25 | + |
| 26 | +import java.lang.reflect.Method; |
| 27 | +import java.util.Set; |
| 28 | + |
| 29 | +import io.github.kyuubiran.ezxhelper.xposed.common.HookParam; |
| 30 | + |
| 31 | +// https://github.com/HowieHChen/XiaomiHelper/blob/fb2462aa819eed36b6b8875c85f9cb0887eb1ccd/app/src/main/kotlin/dev/lackluster/mihelper/hook/rules/miuihome/gesture/BackGestureHaptic.kt |
| 32 | +public class BackGestureHaptic extends BaseHook { |
| 33 | + |
| 34 | + private static final String TIME_OUT_BLOCKER_KEY = "BLOCKER_ID_FOR_HAPTIC_GESTURE_BACK"; |
| 35 | + |
| 36 | + @Override |
| 37 | + public void init() { |
| 38 | + int mode = PrefsBridge.getStringAsInt("home_gesture_back_haptic", 0); |
| 39 | + if (mode == 1) { |
| 40 | + initEnhancedMode(); |
| 41 | + } else if (mode == 2) { |
| 42 | + initDisabledMode(); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + private void initEnhancedMode() { |
| 47 | + Class<?> hapticFeedbackCompatV2 = findClassIfExists("com.miui.home.common.hapticfeedback.HapticFeedbackCompatV2"); |
| 48 | + if (hapticFeedbackCompatV2 == null) { |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + hookNoArgMethods(hapticFeedbackCompatV2, "performGestureReadyBack", new IMethodHook() { |
| 53 | + @Override |
| 54 | + public void after(HookParam param) { |
| 55 | + startTimeOutBlocker(); |
| 56 | + } |
| 57 | + }); |
| 58 | + |
| 59 | + hookNoArgMethods(hapticFeedbackCompatV2, "performGestureBackHandUp", new IMethodHook() { |
| 60 | + @Override |
| 61 | + public void before(HookParam param) { |
| 62 | + if (isTimeOutBlocked()) { |
| 63 | + param.setResult(null); |
| 64 | + } |
| 65 | + } |
| 66 | + }); |
| 67 | + |
| 68 | + hookLambdaMethods(hapticFeedbackCompatV2, "performGestureReadyBack", new IMethodHook() { |
| 69 | + @Override |
| 70 | + public void before(HookParam param) { |
| 71 | + performGestureHaptic(param.getThisObject(), 0); |
| 72 | + param.setResult(null); |
| 73 | + } |
| 74 | + }); |
| 75 | + |
| 76 | + hookLambdaMethods(hapticFeedbackCompatV2, "performGestureBackHandUp", new IMethodHook() { |
| 77 | + @Override |
| 78 | + public void before(HookParam param) { |
| 79 | + performGestureHaptic(param.getThisObject(), 1); |
| 80 | + param.setResult(null); |
| 81 | + } |
| 82 | + }); |
| 83 | + |
| 84 | + Class<?> gestureStubViewClass = findClassIfExists("com.miui.home.recents.GestureStubView"); |
| 85 | + if (gestureStubViewClass != null) { |
| 86 | + for (Method method : gestureStubViewClass.getDeclaredMethods()) { |
| 87 | + if (!"injectBackKeyEvent".equals(method.getName())) { |
| 88 | + continue; |
| 89 | + } |
| 90 | + Class<?>[] parameterTypes = method.getParameterTypes(); |
| 91 | + if (parameterTypes.length != 1 || parameterTypes[0] != boolean.class) { |
| 92 | + continue; |
| 93 | + } |
| 94 | + method.setAccessible(true); |
| 95 | + hookMethod(method, new IMethodHook() { |
| 96 | + @Override |
| 97 | + public void before(HookParam param) { |
| 98 | + param.getArgs()[0] = true; |
| 99 | + } |
| 100 | + }); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private void initDisabledMode() { |
| 106 | + Set<String> classNames = Set.of( |
| 107 | + "com.miui.home.common.hapticfeedback.HapticFeedbackCompatLinear", |
| 108 | + "com.miui.home.common.hapticfeedback.HapticFeedbackCompatNormal", |
| 109 | + "com.miui.home.common.hapticfeedback.HapticFeedbackCompatV2" |
| 110 | + ); |
| 111 | + |
| 112 | + for (String className : classNames) { |
| 113 | + Class<?> hapticClass = findClassIfExists(className); |
| 114 | + if (hapticClass == null) { |
| 115 | + continue; |
| 116 | + } |
| 117 | + IMethodHook disabledHook = new IMethodHook() { |
| 118 | + @Override |
| 119 | + public void before(HookParam param) { |
| 120 | + param.setResult(null); |
| 121 | + } |
| 122 | + }; |
| 123 | + hookNoArgMethods(hapticClass, "performGestureReadyBack", disabledHook); |
| 124 | + hookNoArgMethods(hapticClass, "performGestureBackHandUp", disabledHook); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private void startTimeOutBlocker() { |
| 129 | + try { |
| 130 | + Class<?> backgroundThreadClass = findClassIfExists("com.miui.home.common.multithread.BackgroundThread"); |
| 131 | + Class<?> timeOutBlockerClass = findClassIfExists("com.miui.home.common.utils.TimeOutBlocker"); |
| 132 | + if (backgroundThreadClass == null || timeOutBlockerClass == null) { |
| 133 | + return; |
| 134 | + } |
| 135 | + Object handler = callStaticMethod(backgroundThreadClass, "getHandler"); |
| 136 | + if (handler != null) { |
| 137 | + callStaticMethod(timeOutBlockerClass, "startCountDown", handler, 140L, TIME_OUT_BLOCKER_KEY); |
| 138 | + } |
| 139 | + } catch (Throwable throwable) { |
| 140 | + XposedLog.w(TAG, getPackageName(), "start back gesture haptic blocker failed", throwable); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + private boolean isTimeOutBlocked() { |
| 145 | + try { |
| 146 | + Class<?> timeOutBlockerClass = findClassIfExists("com.miui.home.common.utils.TimeOutBlocker"); |
| 147 | + if (timeOutBlockerClass == null) { |
| 148 | + return false; |
| 149 | + } |
| 150 | + Object blocked = callStaticMethod(timeOutBlockerClass, "isBlocked", TIME_OUT_BLOCKER_KEY); |
| 151 | + return blocked instanceof Boolean && (Boolean) blocked; |
| 152 | + } catch (Throwable throwable) { |
| 153 | + XposedLog.w(TAG, getPackageName(), "check back gesture haptic blocker failed", throwable); |
| 154 | + return false; |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + private void performGestureHaptic(Object target, int effectId) { |
| 159 | + Object hapticHelper = getObjectField(target, "mHapticHelper"); |
| 160 | + if (hapticHelper == null) { |
| 161 | + return; |
| 162 | + } |
| 163 | + try { |
| 164 | + callMethod(hapticHelper, "performExtHapticFeedback", effectId); |
| 165 | + } catch (Throwable throwable) { |
| 166 | + XposedLog.w(TAG, getPackageName(), "perform back gesture haptic feedback failed", throwable); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + private void hookNoArgMethods(Class<?> targetClass, String methodName, IMethodHook callback) { |
| 171 | + for (Method method : targetClass.getDeclaredMethods()) { |
| 172 | + if (!methodName.equals(method.getName()) || method.getParameterCount() != 0) { |
| 173 | + continue; |
| 174 | + } |
| 175 | + method.setAccessible(true); |
| 176 | + hookMethod(method, callback); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + private void hookLambdaMethods(Class<?> targetClass, String methodTag, IMethodHook callback) { |
| 181 | + for (Method method : targetClass.getDeclaredMethods()) { |
| 182 | + String methodName = method.getName(); |
| 183 | + if (!methodName.startsWith("lambda") || !methodName.contains(methodTag)) { |
| 184 | + continue; |
| 185 | + } |
| 186 | + method.setAccessible(true); |
| 187 | + hookMethod(method, callback); |
| 188 | + } |
| 189 | + } |
| 190 | +} |
0 commit comments