Fix(android): humanize lock status and transfer messages

1) 锁屏时活动窗口与 Windows/macOS 对齐:由裸 "Locked" 改为
   "Locked: HH:MM:SS"(已锁时长)。起点取 ACTION_SCREEN_OFF,解锁由
   ACTION_USER_PRESENT 与 KeyguardManager 双重判定;历史活动仍上报裸
   "Locked" 当空闲处理,不记录。

2) 转移主机的提示/日志不再出现大整数:到期时刻格式化为
   "YYYY-MM-DD HH:MM:SS +ZZZZ",剩余有效期格式化为 "2h 05m"/"15m 03s"。
   原 "Host is already transferred until 1763..., ignored" 现在是
   "Host is already transferred until 2026-11-17 21:15:12 +0100
   (2h 00m left), ignored"。

验证:arm64/armeabi 编译通过;模拟器开无障碍后锁屏,服务端 list_hosts
收到 "Locked: 00:00:06",解锁后恢复 "Android";格式化函数单独跑过
2h/15m/42s/负数用例。

Model: deepseek-flash
Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
yuanyuanxiangandClaude Code committed 2026-11-16 21:41:02 +01:00
1 parent b7f87c958a
commit db148c4a69
2 files changed
+99 -18

No files matched your search

+51 -11
View File
@@ -11,6 +11,7 @@
#include <cstring>
#include <cinttypes>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <unistd.h>
#include <fcntl.h>
@@ -2045,6 +2046,37 @@ static int g_transferFailStreak = 0; // 仅连接线程访问
static const int kTransferRevertAttempts = 3;
static const long long kTransferRevertGraceSec = 60;
// 转移主机的日志与回给主控的提示里,时间/时长都转成可读文本:
// "1763... " → "2026-11-17 02:35:12 +0100"
// 7205 → "2h 00m"
static std::string FormatUnixTimeHuman(long long epochSec)
{
const time_t t = (time_t)epochSec;
struct tm tmv;
if (!localtime_r(&t, &tmv)) return std::to_string(epochSec);
char buf[32] = { 0 };
if (strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tmv) == 0)
return std::to_string(epochSec);
const long off = tmv.tm_gmtoff;
char zone[16] = { 0 };
snprintf(zone, sizeof(zone), "%c%02ld%02ld",
off < 0 ? '-' : '+', labs(off) / 3600, (labs(off) % 3600) / 60);
return std::string(buf) + " " + zone;
}
static std::string FormatDurationHuman(long long seconds)
{
if (seconds < 0) seconds = 0;
const long long h = seconds / 3600;
const long long m = (seconds % 3600) / 60;
const long long s = seconds % 60;
char buf[48] = { 0 };
if (h > 0) snprintf(buf, sizeof(buf), "%lldh %02lldm", h, m);
else if (m > 0) snprintf(buf, sizeof(buf), "%lldm %02llds", m, s);
else snprintf(buf, sizeof(buf), "%llds", s);
return buf;
}
static std::string TransferRecordPath()
{
return g_filesDir.empty() ? std::string() : g_filesDir + "/yama_transfer";
@@ -2110,14 +2142,15 @@ static void LoadTransferRecord()
}
const long long now = (long long)time(nullptr);
if (!t.Active(now)) {
LOGI("TRANSFER: record expired at %lld, removed", t.validTo);
LOGI("TRANSFER: record expired at %s, removed", FormatUnixTimeHuman(t.validTo).c_str());
ClearTransferRecord();
return;
}
g_transferValidTo.store(t.validTo, std::memory_order_relaxed);
g_transferAppliedAt.store(now, std::memory_order_relaxed);
ApplyEndpointToTarget(t.ep);
LOGI("TRANSFER: active, target %s:%d, %llds left", t.ep.ip.c_str(), t.ep.port, t.validTo - now);
LOGI("TRANSFER: active, target %s:%d, %s left", t.ep.ip.c_str(), t.ep.port,
FormatDurationHuman(t.validTo - now).c_str());
}
// ------------------------------------------------------------------ DataProcess
@@ -2457,18 +2490,23 @@ int DataProcess(void* /*user*/, PBYTE szBuffer, ULONG ulLength)
// 所以操作员必然填了一个值,日志里把它记下来免得事后对不上。
if (toOriginal) {
LOGI("cmd: ASSIGN back to original master %s:%d, ending transfer early "
"(requested validity %llds ignored)",
t.ep.ip.c_str(), t.ep.port, t.validTo - now);
"(requested validity %s ignored)",
t.ep.ip.c_str(), t.ep.port,
FormatDurationHuman(t.validTo - now).c_str());
g_transferEndEarly.store(true, std::memory_order_relaxed);
g_transferValidTo.store(now, std::memory_order_relaxed);
sendClientMsg("Transfer host",
"Transfer ended early, returning to original master (validity ignored)");
break;
}
LOGI("cmd: ASSIGN ignored, already assigned until %lld", active);
char msg[128];
const std::string until = FormatUnixTimeHuman(active);
const std::string remain = FormatDurationHuman(active - now);
LOGI("cmd: ASSIGN ignored, already assigned until %s (%s left)",
until.c_str(), remain.c_str());
char msg[192];
snprintf(msg, sizeof(msg),
"Host is already transferred until %lld, ignored", active);
"Host is already transferred until %s (%s left), ignored",
until.c_str(), remain.c_str());
sendClientMsg("Transfer host", msg);
break;
}
@@ -2486,7 +2524,8 @@ int DataProcess(void* /*user*/, PBYTE szBuffer, ULONG ulLength)
// 先挡住后续重复下发,再落盘:落盘失败也不至于被第二条命令覆盖。
g_transferValidTo.store(t.validTo, std::memory_order_relaxed);
SaveTransferRecord(t);
LOGI("cmd: ASSIGN → %s:%d for %llds", t.ep.ip.c_str(), t.ep.port, t.validTo - now);
LOGI("cmd: ASSIGN → %s:%d for %s", t.ep.ip.c_str(), t.ep.port,
FormatDurationHuman(t.validTo - now).c_str());
PostStatus("transfer: switching master");
break;
}
@@ -2624,7 +2663,8 @@ static void ConnectionThread(uint64_t gen)
const long long tried = (long long)time(nullptr)
- g_transferAppliedAt.load(std::memory_order_relaxed);
if (g_transferFailStreak >= kTransferRevertAttempts && tried >= kTransferRevertGraceSec) {
LOGI("TRANSFER: unreachable for %llds (%d tries), reverting", tried, g_transferFailStreak);
LOGI("TRANSFER: unreachable for %s (%d tries), reverting",
FormatDurationHuman(tried).c_str(), g_transferFailStreak);
g_transferValidTo.store(0, std::memory_order_relaxed);
g_transferFailStreak = 0;
ClearTransferRecord();
@@ -2714,8 +2754,8 @@ static void ConnectionThread(uint64_t gen)
const long long vt = g_transferValidTo.load(std::memory_order_relaxed);
if (TransferExpired()) {
const bool early = g_transferEndEarly.exchange(false, std::memory_order_relaxed);
LOGI("TRANSFER: %s at %lld, restoring original master",
early ? "ended early" : "expired", vt);
LOGI("TRANSFER: %s at %s, restoring original master",
early ? "ended early" : "expired", FormatUnixTimeHuman(vt).c_str());
g_transferValidTo.store(0, std::memory_order_relaxed);
ClearTransferRecord();
RestoreOrigTarget();