Skip to content

Commit 04bc6f8

Browse files
authored
尝试修复切换用户不执行任务的bug(2)
1 parent 8bde774 commit 04bc6f8

1 file changed

Lines changed: 32 additions & 24 deletions

File tree

app/src/main/java/fansirsqi/xposed/sesame/hook/ApplicationHook.java

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package fansirsqi.xposed.sesame.hook;
22

3-
43
import android.annotation.SuppressLint;
54
import android.app.Activity;
65
import android.app.Application;
@@ -166,7 +165,6 @@ public static void setAlarmScheduler(AlarmScheduler scheduler) {
166165
dayCalendar.set(Calendar.SECOND, 0);
167166
}
168167

169-
170168
private final static Method deoptimizeMethod;
171169

172170
static {
@@ -237,7 +235,6 @@ private void scheduleNextExecution(long lastExecTime) {
237235
}
238236
}
239237

240-
241238
@SuppressLint("UnsafeDynamicallyLoadedCode")
242239
private void loadNativeLibs(Context context, File soFile) {
243240
try {
@@ -593,43 +590,47 @@ private static void unsetWakenAtTimeAlarm() {
593590

594591
private static synchronized Boolean initHandler(Boolean force) {
595592
try {
596-
if (init && !force) { // 如果已经初始化且非强制,则跳过
593+
if (init && !force) { // 已经初始化 & 非强制,直接跳过
597594
return true;
598595
}
599-
// 只有在重新初始化时才销毁旧的handler
596+
600597
if (init) {
601-
destroyHandler(true);
598+
destroyHandler(true); // 重新初始化时销毁旧的handler
602599
}
603-
604-
// 重新初始化AlarmScheduler(如果已被销毁)
600+
601+
// AlarmScheduler 确保可用
605602
if (!alarmManager.isAlarmSchedulerAvailable() && appContext != null) {
606603
alarmManager.initializeAlarmScheduler(appContext);
607604
}
608-
609-
Model.initAllModel(); //在所有服务启动前装模块配置
605+
606+
Model.initAllModel(); // 在所有服务启动前装模块配置
610607
if (service == null) {
611608
return false;
612609
}
610+
613611
if (force) {
614612
String userId = HookUtil.INSTANCE.getUserId(appLloadPackageParam.classLoader);
615613
if (userId == null) {
616-
Log.record(TAG, "initHandler:用户未登录");
617-
Toast.show("initHandler:用户未登录");
614+
Log.record(TAG, "initHandler: 用户未登录");
615+
Toast.show("用户未登录");
618616
return false;
619617
}
618+
620619
HookUtil.INSTANCE.hookUser(appLloadPackageParam);
621620
String startMsg = "芝麻粒-TK 开始初始化...";
622621
Log.record(TAG, startMsg);
623622
Log.record(TAG, "⚙️模块版本:" + modelVersion);
624623
Log.record(TAG, "📦应用版本:" + alipayVersion.getVersionString());
625624
Log.record(TAG, "📶网络类型:" + NetworkUtils.INSTANCE.getNetworkType());
626-
Config.load(userId); //加载配置
625+
626+
Config.load(userId); // 加载配置
627627
if (!Config.isLoaded()) {
628628
Log.record(TAG, "用户模块配置加载失败");
629629
Toast.show("用户模块配置加载失败");
630630
return false;
631631
}
632-
//闹钟权限申请
632+
633+
// 闹钟权限检查
633634
if (!PermissionUtil.checkAlarmPermissions()) {
634635
Log.record(TAG, "❌ 支付宝无闹钟权限");
635636
mainHandler.postDelayed(
@@ -641,7 +642,8 @@ private static synchronized Boolean initHandler(Boolean force) {
641642
2000);
642643
return false;
643644
}
644-
// 检查并请求后台运行权限
645+
646+
// 后台运行权限检查
645647
if (BaseModel.getBatteryPerm().getValue() && !init && !PermissionUtil.checkBatteryPermissions()) {
646648
Log.record(TAG, "支付宝无始终在后台运行权限");
647649
mainHandler.postDelayed(
@@ -652,27 +654,28 @@ private static synchronized Boolean initHandler(Boolean force) {
652654
},
653655
2000);
654656
}
657+
655658
Notify.start(service);
656-
// 获取 BaseModel 实例
659+
657660
BaseModel baseModel = Model.getModel(BaseModel.class);
658661
if (baseModel == null) {
659662
Log.error(TAG, "BaseModel 未找到 初始化失败");
660663
Notify.setStatusTextDisabled();
661664
return false;
662665
}
663-
// 检查 enableField 的值
666+
664667
if ((0 == baseModel.getEnableField().getValue())) {
665668
Log.record(TAG, "❌ 芝麻粒已禁用");
666669
Toast.show("❌ 芝麻粒已禁用");
667670
Notify.setStatusTextDisabled();
668671
return false;
669672
}
670-
// 保持唤醒锁,防止设备休眠
673+
671674
if (BaseModel.getStayAwake().getValue()) {
672675
try {
673676
PowerManager pm = (PowerManager) service.getSystemService(Context.POWER_SERVICE);
674677
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, service.getClass().getName());
675-
wakeLock.acquire(10*60*1000L /*10 minutes*/); // 确保唤醒锁在前台服务启动前
678+
wakeLock.acquire(10 * 60 * 1000L); // 10分钟
676679
} catch (Throwable t) {
677680
Log.record(TAG, "唤醒锁申请失败:");
678681
Log.printStackTrace(t);
@@ -690,22 +693,30 @@ private static synchronized Boolean initHandler(Boolean force) {
690693
rpcBridge.load();
691694
rpcVersion = rpcBridge.getVersion();
692695
}
696+
693697
if (BaseModel.getNewRpc().getValue() && BaseModel.getDebugMode().getValue()) {
694-
HookUtil.INSTANCE.hookRpcBridgeExtension(appLloadPackageParam, BaseModel.getSendHookData().getValue(), BaseModel.getSendHookDataUrl().getValue());
698+
HookUtil.INSTANCE.hookRpcBridgeExtension(
699+
appLloadPackageParam,
700+
BaseModel.getSendHookData().getValue(),
701+
BaseModel.getSendHookDataUrl().getValue()
702+
);
695703
HookUtil.INSTANCE.hookDefaultBridgeCallback(appLloadPackageParam);
696704
}
705+
697706
Model.bootAllModel(classLoader);
698707
Status.load(userId);
699708
DataCache.INSTANCE.load();
700709
DataStore.INSTANCE.init(Files.CONFIG_DIR);
701710
updateDay(userId);
711+
702712
String successMsg = "芝麻粒-TK 加载成功✨";
703713
Log.record(successMsg);
704714
Toast.show(successMsg);
705-
706715
}
716+
707717
offline = false;
708718
execHandler();
719+
init = true;
709720
return true;
710721
} catch (Throwable th) {
711722
Log.printStackTrace(TAG, "startHandler", th);
@@ -766,8 +777,6 @@ static void execHandler() {
766777
mainTask.startTask(false);
767778
}
768779

769-
770-
771780
/**
772781
* 检查长时间未执行的情况,如果超过阈值则自动重启
773782
* 特别针对0点后可能出现的执行中断情况
@@ -961,7 +970,6 @@ public static void reLogin() {
961970
});
962971
}
963972

964-
965973
static class AlipayBroadcastReceiver extends BroadcastReceiver {
966974
@Override
967975
public void onReceive(Context context, Intent intent) {

0 commit comments

Comments
 (0)