From 9f510c52b2955ab3f493639cf9e683b01829ed7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?YuSaki=E4=B8=B6Kanade?= <53483352+Nep-Timeline@users.noreply.github.com> Date: Tue, 17 Sep 2024 23:17:14 +0800 Subject: [PATCH] Fix input method, Fix media button --- app/build.gradle | 2 +- .../android/audio/SendMediaButtonHook.java | 42 +++++++++++++++---- .../input/InputMethodManagerService.java | 1 + .../cirno/services/FreezerService.java | 2 +- 4 files changed, 36 insertions(+), 11 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index abb1cff..25180a3 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -13,7 +13,7 @@ android { applicationId 'nep.timeline.cirno' minSdk 31 targetSdk 35 - versionCode 2 + versionCode 3 versionName 'Baka' externalNativeBuild { diff --git a/app/src/main/java/nep/timeline/cirno/hooks/android/audio/SendMediaButtonHook.java b/app/src/main/java/nep/timeline/cirno/hooks/android/audio/SendMediaButtonHook.java index ef1e224..5ebfe23 100644 --- a/app/src/main/java/nep/timeline/cirno/hooks/android/audio/SendMediaButtonHook.java +++ b/app/src/main/java/nep/timeline/cirno/hooks/android/audio/SendMediaButtonHook.java @@ -1,5 +1,6 @@ package nep.timeline.cirno.hooks.android.audio; +import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; @@ -7,6 +8,7 @@ import de.robv.android.xposed.XposedBridge; import de.robv.android.xposed.XposedHelpers; import nep.timeline.cirno.framework.AbstractMethodHook; +import nep.timeline.cirno.log.Log; import nep.timeline.cirno.services.FreezerService; public class SendMediaButtonHook { @@ -15,22 +17,44 @@ public SendMediaButtonHook(ClassLoader classLoader) { if (targetClass == null) return; + String fieldName = null; + + for (Field field : targetClass.getDeclaredFields()) { + if (field.getType().getName().equals("com.android.server.media.MediaSessionRecord")) { + fieldName = field.getName(); + break; + } + } + + if (fieldName == null) { + Log.e("无法监听媒体按键!"); + return; + } + List methods = new ArrayList<>(); for (Method method : targetClass.getDeclaredMethods()) { String methodName = method.getName(); - if (methodName.equals("sendMediaButton") || methodName.equals("play") || methodName.equals("next") || methodName.equals("previous")) + if (methodName.equals("sendMediaButton") || methodName.equals("play") || methodName.equals("playFromMediaId") || methodName.equals("playFromSearch") || methodName.equals("playFromUri") || methodName.equals("next") || methodName.equals("previous") || methodName.equals("seekTo")) methods.add(method); } for (Method method : methods) { - XposedBridge.hookMethod(method, new AbstractMethodHook() { - @Override - protected void beforeMethod(MethodHookParam param) { - String pkg = (String) param.args[0]; - int uid = (int) param.args[2]; - FreezerService.temporaryUnfreezeIfNeed(pkg, uid, "按下媒体按键", 3000); - } - }); + try { + String finalFieldName = fieldName; + XposedBridge.hookMethod(method, new AbstractMethodHook() { + @Override + protected void beforeMethod(MethodHookParam param) { + Object record = XposedHelpers.getObjectField(param.thisObject, finalFieldName); + if (record == null) + return; + + FreezerService.temporaryUnfreezeIfNeed(XposedHelpers.getIntField(record, "mOwnerUid"), "按下媒体按键", 3000); + } + }); + Log.i(method.getName() + " -> 成功Hook完毕!"); + } catch (Throwable throwable) { + Log.e(method.getName(), throwable); + } } } } diff --git a/app/src/main/java/nep/timeline/cirno/hooks/android/input/InputMethodManagerService.java b/app/src/main/java/nep/timeline/cirno/hooks/android/input/InputMethodManagerService.java index 0817acb..6d60a33 100644 --- a/app/src/main/java/nep/timeline/cirno/hooks/android/input/InputMethodManagerService.java +++ b/app/src/main/java/nep/timeline/cirno/hooks/android/input/InputMethodManagerService.java @@ -40,6 +40,7 @@ public Object[] getTargetParam() { public XC_MethodHook getTargetHook() { return new AbstractMethodHook() { @Override + @SuppressWarnings("unchecked") protected void beforeMethod(MethodHookParam param) { String id = (String) param.args[0]; if (id == null) diff --git a/app/src/main/java/nep/timeline/cirno/services/FreezerService.java b/app/src/main/java/nep/timeline/cirno/services/FreezerService.java index 5374b1d..dd413d9 100644 --- a/app/src/main/java/nep/timeline/cirno/services/FreezerService.java +++ b/app/src/main/java/nep/timeline/cirno/services/FreezerService.java @@ -31,7 +31,7 @@ public static void freezer(AppRecord appRecord) { public static void thaw(AppRecord appRecord) { FreezerHandler.removeAppMessage(appRecord); - if (!appRecord.isFrozen() || appRecord.isSystem()) + if (!appRecord.isFrozen()) return; for (ProcessRecord processRecord : appRecord.getProcessRecords()) {