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:
1 parent
b7f87c958a
commit
db148c4a69
2 files changed
+99
-18
No files matched your search
@@ -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();
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.yama.client
|
||||
import android.accessibilityservice.AccessibilityService
|
||||
import android.accessibilityservice.GestureDescription
|
||||
import android.annotation.SuppressLint
|
||||
import android.app.KeyguardManager
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
@@ -21,6 +22,7 @@ import android.view.View
|
||||
import android.view.WindowManager
|
||||
import android.view.accessibility.AccessibilityEvent
|
||||
import android.view.accessibility.AccessibilityNodeInfo
|
||||
import java.util.Locale
|
||||
|
||||
/**
|
||||
* AccessibilityService that injects touch gestures and global key actions
|
||||
@@ -45,8 +47,31 @@ class ControlService : AccessibilityService() {
|
||||
@Volatile var instance: ControlService? = null
|
||||
|
||||
// ── Active foreground window ─────────────────────────────────────
|
||||
@JvmStatic
|
||||
@Volatile var activeWindow: String = "Android"
|
||||
// 服务端心跳读的是这个属性的 Java getter(C++ 按 getActiveWindow 查找)。
|
||||
// 锁屏时与其他平台对齐返回 "Locked: HH:MM:SS"(已锁时长),否则返回前台包名。
|
||||
@Volatile private var foregroundPkg: String = "Android"
|
||||
@Volatile private var lockedAtElapsed: Long = 0L
|
||||
|
||||
@get:JvmStatic
|
||||
val activeWindow: String
|
||||
get() {
|
||||
val svc = instance
|
||||
val now = SystemClock.elapsedRealtime()
|
||||
val locked = lockedAtElapsed != 0L || (svc?.isKeyguardLockedNow() == true)
|
||||
if (locked) {
|
||||
if (lockedAtElapsed == 0L) lockedAtElapsed = now
|
||||
return "Locked: " + formatElapsed(now - lockedAtElapsed)
|
||||
}
|
||||
lockedAtElapsed = 0L
|
||||
return foregroundPkg
|
||||
}
|
||||
|
||||
// 锁屏时长,格式与 Windows/macOS 的 "Locked: HH:MM:SS" 一致
|
||||
private fun formatElapsed(ms: Long): String {
|
||||
val total = (ms / 1000).coerceAtLeast(0)
|
||||
return String.format(Locale.US, "%02d:%02d:%02d",
|
||||
total / 3600, (total % 3600) / 60, total % 60)
|
||||
}
|
||||
|
||||
// ── 历史活动采样用的前台应用标题(C++ ActivityHistory 的 probe 每秒调一次)──
|
||||
// 辅助功能服务没启用时返回 null,让「历史活动」报告能说清是缺权限,而不是
|
||||
@@ -56,10 +81,11 @@ class ControlService : AccessibilityService() {
|
||||
@JvmStatic
|
||||
fun currentActivityTitle(): String? {
|
||||
val svc = instance ?: return null
|
||||
val pkg = activeWindow
|
||||
// 历史活动里锁屏仍按空闲处理(C++ 侧只认裸 "Locked"),不要带时长
|
||||
if (lockedAtElapsed != 0L || svc.isKeyguardLockedNow()) return "Locked"
|
||||
val pkg = foregroundPkg
|
||||
return when {
|
||||
pkg.isEmpty() -> null
|
||||
pkg == "Locked" -> pkg // 锁屏/息屏:C++ 侧当空闲处理,不记录
|
||||
pkg == "Android" -> svc.getString(R.string.activity_home)
|
||||
else -> synchronized(labelCache) {
|
||||
labelCache.getOrPut(pkg) { describeApp(svc, pkg) }
|
||||
@@ -177,12 +203,23 @@ class ControlService : AccessibilityService() {
|
||||
private val screenReceiver = object : BroadcastReceiver() {
|
||||
override fun onReceive(context: Context?, intent: Intent?) {
|
||||
when (intent?.action) {
|
||||
Intent.ACTION_SCREEN_OFF -> activeWindow = "Locked"
|
||||
Intent.ACTION_USER_PRESENT -> activeWindow = "Android"
|
||||
// 只记录起点;上报时由 activeWindow 的 getter 算 "Locked: HH:MM:SS"
|
||||
Intent.ACTION_SCREEN_OFF -> lockedAtElapsed = SystemClock.elapsedRealtime()
|
||||
Intent.ACTION_USER_PRESENT -> {
|
||||
lockedAtElapsed = 0L
|
||||
foregroundPkg = "Android"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 锁屏判断:KeyguardManager 在「屏幕亮着但未解锁」时也返回 true;
|
||||
// 少数机型息屏时它返回 false,所以调用方还会叠加 lockedAtElapsed。
|
||||
private fun isKeyguardLockedNow(): Boolean {
|
||||
val km = getSystemService(Context.KEYGUARD_SERVICE) as? KeyguardManager ?: return false
|
||||
return km.isKeyguardLocked
|
||||
}
|
||||
|
||||
// ── Main-thread handler ───────────────────────────────────────────────
|
||||
private val handler = Handler(Looper.getMainLooper())
|
||||
|
||||
@@ -238,7 +275,11 @@ class ControlService : AccessibilityService() {
|
||||
override fun onAccessibilityEvent(event: AccessibilityEvent?) {
|
||||
if (event?.eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
|
||||
val pkg = event.packageName?.toString()
|
||||
if (!pkg.isNullOrEmpty()) activeWindow = pkg
|
||||
if (!pkg.isNullOrEmpty()) {
|
||||
// 只更新前台包名;解锁由 USER_PRESENT 与 activeWindow 的 getter 负责,
|
||||
// 避免「息屏时 KeyguardManager 误报未锁」把锁定起点清掉
|
||||
foregroundPkg = pkg
|
||||
}
|
||||
}
|
||||
}
|
||||
override fun onInterrupt() {}
|
||||
|
||||
Reference in new issue
Block a user