Skip to content

Commit

Permalink
Fix input method, Fix media button
Browse files Browse the repository at this point in the history
  • Loading branch information
Nep-Timeline committed Sep 17, 2024
1 parent 6533cff commit 9f510c5
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 11 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ android {
applicationId 'nep.timeline.cirno'
minSdk 31
targetSdk 35
versionCode 2
versionCode 3
versionName 'Baka'

externalNativeBuild {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
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;

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 {
Expand All @@ -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<Method> 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);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down

0 comments on commit 9f510c5

Please sign in to comment.