|
| 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.recent; |
| 20 | + |
| 21 | +import android.content.Context; |
| 22 | +import android.provider.Settings; |
| 23 | +import android.view.MotionEvent; |
| 24 | +import android.view.View; |
| 25 | + |
| 26 | +import com.sevtinge.hyperceiler.common.log.XposedLog; |
| 27 | +import com.sevtinge.hyperceiler.common.utils.PrefsBridge; |
| 28 | +import com.sevtinge.hyperceiler.libhook.base.BaseHook; |
| 29 | + |
| 30 | +public class GuidedAccessHome extends BaseHook { |
| 31 | + private static final String SETTING_KEY_LOCK_APP = "key_lock_app"; |
| 32 | + |
| 33 | + @Override |
| 34 | + public void init() { |
| 35 | + hookPointerEvent(); |
| 36 | + hookIsMistakeTouch(); |
| 37 | + hookScreenPinTouchResolution(); |
| 38 | + hookLandscapeOverviewGestureView(); |
| 39 | + hookScreenPinnedHelperStartDirectly(); |
| 40 | + hookHomeStartScreenPinningDirectly(); |
| 41 | + } |
| 42 | + |
| 43 | + private void hookPointerEvent() { |
| 44 | + findAndChainMethod("com.miui.home.recents.NavStubView", |
| 45 | + "onPointerEvent", |
| 46 | + chain -> { |
| 47 | + Context context = resolveContext(chain.getThisObject()); |
| 48 | + if (getLockApp(context) == -1) return chain.proceed(); |
| 49 | + return false; |
| 50 | + }, |
| 51 | + MotionEvent.class |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + private void hookIsMistakeTouch() { |
| 56 | + findAndChainMethod("com.miui.home.recents.NavStubView", "isMistakeTouch", chain -> { |
| 57 | + Context context = resolveContext(chain.getThisObject()); |
| 58 | + if (getLockApp(context) == -1) return chain.proceed(); |
| 59 | + return true; |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + private void hookScreenPinTouchResolution() { |
| 64 | + try { |
| 65 | + findAndChainMethod("com.miui.home.recents.NavStubView", |
| 66 | + "screenPinTouchResolution", |
| 67 | + chain -> { |
| 68 | + Context context = resolveContext(chain.getThisObject()); |
| 69 | + if (getLockApp(context) == -1) return chain.proceed(); |
| 70 | + return null; |
| 71 | + }, |
| 72 | + MotionEvent.class |
| 73 | + ); |
| 74 | + } catch (Throwable e) { |
| 75 | + // Pad variant may not include this method; keep other hooks alive. |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private void hookLandscapeOverviewGestureView() { |
| 80 | + findAndChainMethod("com.miui.home.recents.views.RecentsContainer", |
| 81 | + "showLandscapeOverviewGestureView", |
| 82 | + chain -> { |
| 83 | + Context context = resolveContext(chain.getThisObject()); |
| 84 | + if (getLockApp(context) == -1) return chain.proceed(); |
| 85 | + return null; |
| 86 | + }, |
| 87 | + boolean.class |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + private void hookHomeStartScreenPinningDirectly() { |
| 92 | + findAndChainMethod("com.miui.home.recents.SystemUiProxyWrapper", |
| 93 | + "startScreenPinning", |
| 94 | + chain -> { |
| 95 | + if (!PrefsBridge.getBoolean("system_framework_guided_access", false)) { |
| 96 | + return chain.proceed(); |
| 97 | + } |
| 98 | + int taskId = (int) chain.getArg(0); |
| 99 | + if (taskId < 0) return chain.proceed(); |
| 100 | + try { |
| 101 | + Class<?> activityTaskManager = findClassIfExists("android.app.ActivityTaskManager"); |
| 102 | + if (activityTaskManager == null) return chain.proceed(); |
| 103 | + Object service = callStaticMethod(activityTaskManager, "getService"); |
| 104 | + callMethod(service, "startSystemLockTaskMode", taskId); |
| 105 | + return true; |
| 106 | + } catch (Throwable e) { |
| 107 | + XposedLog.w(TAG, "home direct startSystemLockTaskMode E: " + e); |
| 108 | + return chain.proceed(); |
| 109 | + } |
| 110 | + }, |
| 111 | + int.class |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + private void hookScreenPinnedHelperStartDirectly() { |
| 116 | + findAndChainMethod("com.miui.home.recents.ScreenPinnedHelper", |
| 117 | + "startScreenPinning", |
| 118 | + chain -> { |
| 119 | + if (!PrefsBridge.getBoolean("system_framework_guided_access", false)) { |
| 120 | + return chain.proceed(); |
| 121 | + } |
| 122 | + int taskId = (int) chain.getArg(0); |
| 123 | + if (taskId < 0) return chain.proceed(); |
| 124 | + try { |
| 125 | + Class<?> activityTaskManager = findClassIfExists("android.app.ActivityTaskManager"); |
| 126 | + if (activityTaskManager == null) return chain.proceed(); |
| 127 | + Object service = callStaticMethod(activityTaskManager, "getService"); |
| 128 | + callMethod(service, "startSystemLockTaskMode", taskId); |
| 129 | + XposedLog.d(TAG, "home helper direct startSystemLockTaskMode taskId=" + taskId); |
| 130 | + return null; |
| 131 | + } catch (Throwable e) { |
| 132 | + XposedLog.w(TAG, "home helper direct startSystemLockTaskMode E: " + e); |
| 133 | + return chain.proceed(); |
| 134 | + } |
| 135 | + }, |
| 136 | + int.class |
| 137 | + ); |
| 138 | + } |
| 139 | + |
| 140 | + private Context resolveContext(Object target) { |
| 141 | + if (target instanceof Context) return (Context) target; |
| 142 | + if (target instanceof View) return ((View) target).getContext(); |
| 143 | + if (target == null) return null; |
| 144 | + try { |
| 145 | + Object context = getObjectField(target, "mContext"); |
| 146 | + if (context instanceof Context) return (Context) context; |
| 147 | + } catch (Throwable ignored) { |
| 148 | + } |
| 149 | + return null; |
| 150 | + } |
| 151 | + |
| 152 | + private int getLockApp(Context context) { |
| 153 | + if (context == null) return -1; |
| 154 | + try { |
| 155 | + return Settings.Global.getInt(context.getContentResolver(), SETTING_KEY_LOCK_APP); |
| 156 | + } catch (Throwable ignored) { |
| 157 | + return -1; |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments