Skip to content

Commit

Permalink
Support binder
Browse files Browse the repository at this point in the history
  • Loading branch information
Nep-Timeline committed Sep 9, 2024
1 parent 1add177 commit 05add26
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import de.robv.android.xposed.XC_MethodHook;
import nep.timeline.cirno.framework.AbstractMethodHook;
import nep.timeline.cirno.framework.MethodHook;
import nep.timeline.cirno.services.FreezerService;
import nep.timeline.cirno.utils.SystemChecker;

public class HansKernelUnfreezeHook extends MethodHook {
Expand Down Expand Up @@ -34,6 +35,8 @@ protected void beforeMethod(MethodHookParam param) {
if (type != 1) // Sync binder
return;
int target = (int) param.args[4];

FreezerService.temporaryUnfreezeIfNeed(target, "Binder", 3000);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import de.robv.android.xposed.XC_MethodHook;
import nep.timeline.cirno.framework.AbstractMethodHook;
import nep.timeline.cirno.framework.MethodHook;
import nep.timeline.cirno.services.FreezerService;
import nep.timeline.cirno.utils.SystemChecker;

public class MilletBinderTransHook extends MethodHook {
Expand Down Expand Up @@ -35,6 +36,8 @@ protected void beforeMethod(MethodHookParam param) {
return;

int dstUid = (int) param.args[0];

FreezerService.temporaryUnfreezeIfNeed(dstUid, "Binder", 3000);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import de.robv.android.xposed.XC_MethodHook;
import nep.timeline.cirno.framework.AbstractMethodHook;
import nep.timeline.cirno.framework.MethodHook;
import nep.timeline.cirno.services.FreezerService;
import nep.timeline.cirno.utils.SystemChecker;

public class SamsungBinderTransHook extends MethodHook {
Expand Down Expand Up @@ -38,6 +39,8 @@ protected void beforeMethod(MethodHookParam param) {
if (flags == 1) // Async binder
return;
int uid = (int) param.args[1];

FreezerService.temporaryUnfreezeIfNeed(uid, "Binder", 3000);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,14 @@ public static ApplicationInfo getApplicationInfo(String packageName, int userId)
}
return null;
}

public static String[] getPackagesForUid(int uid) {
Context context = getContext();

if (context == null)
return null;

PackageManager packageManager = context.getPackageManager();
return packageManager.getPackagesForUid(uid);
}
}
41 changes: 41 additions & 0 deletions app/src/main/java/nep/timeline/cirno/services/AppService.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package nep.timeline.cirno.services;

import android.content.pm.ApplicationInfo;
import android.os.Process;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import nep.timeline.cirno.entity.AppRecord;
import nep.timeline.cirno.utils.PKGUtils;

public class AppService {
private static final Map<Integer, Map<String, AppRecord>> APP_RECORD_MAP = new ConcurrentHashMap<>();
private static final Map<Integer, List<AppRecord>> UID_RECORD_MAP = new ConcurrentHashMap<>();

public static AppRecord get(String packageName, int userId) {
if (packageName == null || packageName.equals("android"))
Expand All @@ -25,4 +30,40 @@ public static AppRecord get(String packageName, int userId) {

return appRecords.put(packageName, new AppRecord(applicationInfo));
}

public static List<AppRecord> getByUid(int uid) {
try {
if (!UID_RECORD_MAP.containsKey(uid))
putAppToCacheByUid(uid);
List<AppRecord> records = UID_RECORD_MAP.get(uid);
if (records == null)
return new ArrayList<>();
return records;
} catch (Throwable ignored) {

}
return null;
}

private static synchronized void putAppToCacheByUid(int uid) {
if (uid <= Process.SYSTEM_UID) {
UID_RECORD_MAP.put(uid, null);
return;
}

String[] keys = ActivityManagerService.getPackagesForUid(uid);
if (keys == null || keys.length == 0) {
UID_RECORD_MAP.put(uid, null);
return;
}

List<AppRecord> appRecords = new ArrayList<>();
for (String key : keys) {
String[] split = key.split(":");
int userId = split.length == 1 ? PKGUtils.getUserId(uid) : Integer.parseInt(split[1].trim());
appRecords.add(get(split[0], userId));
}

UID_RECORD_MAP.put(uid, appRecords);
}
}
32 changes: 32 additions & 0 deletions app/src/main/java/nep/timeline/cirno/services/FreezerService.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package nep.timeline.cirno.services;

import java.util.List;

import nep.timeline.cirno.entity.AppRecord;
import nep.timeline.cirno.entity.ProcessRecord;
import nep.timeline.cirno.log.Log;
import nep.timeline.cirno.threads.FreezerHandler;
import nep.timeline.cirno.utils.FrozenRW;

Expand Down Expand Up @@ -34,4 +37,33 @@ public static void thaw(AppRecord appRecord) {
processRecord.setFrozen(false);
}
}

public static void temporaryUnfreezeIfNeed(int uid, String reason, long interval) {
List<AppRecord> appRecords = AppService.getByUid(uid);

if (appRecords == null || appRecords.isEmpty())
return;

for (AppRecord appRecord : appRecords) {
if (appRecord == null)
continue;

temporaryUnfreezeIfNeed(appRecord, reason, interval);
}
}

public static void temporaryUnfreezeIfNeed(String packageName, int userId, String reason, long interval) {
temporaryUnfreezeIfNeed(AppService.get(packageName, userId), reason, interval);
}

public static void temporaryUnfreezeIfNeed(AppRecord appRecord, String reason, long interval) {
if (appRecord == null || appRecord.isSystem())
return;

if (appRecord.isFrozen())
Log.i(appRecord.getPackageNameWithUser() + " " + reason);

thaw(appRecord);
FreezerHandler.sendFreezeMessageIgnoreMessages(appRecord, interval);
}
}

0 comments on commit 05add26

Please sign in to comment.