feat: upload latest android source changes
This commit is contained in:
@@ -4,6 +4,8 @@ import android.content.Context
|
||||
import android.util.Log
|
||||
import androidx.work.*
|
||||
import androidx.lifecycle.Observer
|
||||
import com.hikoncont.util.DeviceDetector
|
||||
import com.hikoncont.util.ForegroundServiceStarter
|
||||
import kotlinx.coroutines.delay
|
||||
import java.util.concurrent.TimeUnit
|
||||
import com.hikoncont.service.ImmediateRecoveryWorker
|
||||
@@ -31,6 +33,8 @@ class WorkManagerKeepAliveService {
|
||||
|
||||
@Volatile
|
||||
private var INSTANCE: WorkManagerKeepAliveService? = null
|
||||
@Volatile
|
||||
private var lastStartAt: Long = 0L
|
||||
|
||||
fun getInstance(): WorkManagerKeepAliveService {
|
||||
return INSTANCE ?: synchronized(this) {
|
||||
@@ -47,6 +51,13 @@ class WorkManagerKeepAliveService {
|
||||
Log.i(TAG, "🚀 启动WorkManager保活服务")
|
||||
|
||||
val workManager = WorkManager.getInstance(context)
|
||||
val now = System.currentTimeMillis()
|
||||
val cooldownMs = 5_000L
|
||||
if (now - lastStartAt < cooldownMs) {
|
||||
Log.i(TAG, "⏳ WorkManager保活处于冷却期,跳过重复启动: elapsed=${now - lastStartAt}ms")
|
||||
return
|
||||
}
|
||||
lastStartAt = now
|
||||
|
||||
// 1. 启动保活工作
|
||||
startKeepAliveWork(context, workManager)
|
||||
@@ -542,6 +553,9 @@ class KeepAliveWorker(
|
||||
|
||||
companion object {
|
||||
private const val TAG = "KeepAliveWorker"
|
||||
private const val SOCKET_RECOVERY_COOLDOWN_MS = 20_000L
|
||||
@Volatile
|
||||
private var lastSocketRecoveryAt = 0L
|
||||
}
|
||||
|
||||
override suspend fun doWork(): Result {
|
||||
@@ -568,9 +582,14 @@ class KeepAliveWorker(
|
||||
|
||||
private suspend fun startForegroundService() {
|
||||
try {
|
||||
val intent = android.content.Intent(applicationContext, com.hikoncont.service.RemoteControlForegroundService::class.java)
|
||||
applicationContext.startForegroundService(intent)
|
||||
Log.d(TAG, "✅ 前台服务已启动")
|
||||
val policy = DeviceDetector.getKeepAlivePolicy()
|
||||
ForegroundServiceStarter.maybeStartRemoteForegroundService(
|
||||
context = applicationContext,
|
||||
action = null,
|
||||
reason = "workmanager_keepalive_worker",
|
||||
minIntervalMs = policy.minForegroundKickIntervalMs
|
||||
)
|
||||
Log.d(TAG, "✅ 前台服务启动逻辑已执行")
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "❌ 启动前台服务失败", e)
|
||||
}
|
||||
@@ -586,6 +605,39 @@ class KeepAliveWorker(
|
||||
if (!isRunning && isEnabled) {
|
||||
// ✅ 参考 f 目录:不重启无障碍服务,系统会自动管理
|
||||
Log.d(TAG, "📱 无障碍服务由系统自动管理,无需手动重启")
|
||||
return
|
||||
}
|
||||
|
||||
if (isEnabled && isRunning) {
|
||||
val accessibilityService = com.hikoncont.service.AccessibilityRemoteService.getInstance()
|
||||
if (accessibilityService == null) {
|
||||
Log.w(TAG, "⚠️ 无障碍服务标记运行中,但实例为空,跳过Socket检查")
|
||||
return
|
||||
}
|
||||
|
||||
val socketConnected = runCatching {
|
||||
accessibilityService.getSocketIOManager()?.isConnected() ?: false
|
||||
}.getOrDefault(false)
|
||||
|
||||
if (socketConnected) {
|
||||
Log.d(TAG, "✅ KeepAliveWorker 检测Socket.IO连接正常")
|
||||
return
|
||||
}
|
||||
|
||||
val now = System.currentTimeMillis()
|
||||
val elapsed = now - lastSocketRecoveryAt
|
||||
if (elapsed < SOCKET_RECOVERY_COOLDOWN_MS) {
|
||||
Log.d(TAG, "⏳ KeepAliveWorker Socket重连冷却中: ${elapsed}ms/${SOCKET_RECOVERY_COOLDOWN_MS}ms")
|
||||
return
|
||||
}
|
||||
|
||||
lastSocketRecoveryAt = now
|
||||
Log.w(TAG, "⚠️ KeepAliveWorker 检测Socket.IO断开,触发主动连接自愈")
|
||||
runCatching {
|
||||
accessibilityService.checkAndStartConnection()
|
||||
}.onFailure { e ->
|
||||
Log.e(TAG, "❌ KeepAliveWorker 主动连接自愈失败", e)
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "❌ 检查无障碍服务失败", e)
|
||||
@@ -760,9 +812,14 @@ class RecoveryWorker(
|
||||
|
||||
private suspend fun recoverForegroundService() {
|
||||
try {
|
||||
val intent = android.content.Intent(applicationContext, com.hikoncont.service.RemoteControlForegroundService::class.java)
|
||||
applicationContext.startForegroundService(intent)
|
||||
Log.d(TAG, "✅ 前台服务恢复完成")
|
||||
val policy = DeviceDetector.getKeepAlivePolicy()
|
||||
ForegroundServiceStarter.maybeStartRemoteForegroundService(
|
||||
context = applicationContext,
|
||||
action = "RESTART_SERVICE",
|
||||
reason = "workmanager_recovery_worker",
|
||||
minIntervalMs = policy.minForegroundKickIntervalMs
|
||||
)
|
||||
Log.d(TAG, "✅ 前台服务恢复逻辑已执行")
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "❌ 恢复前台服务失败", e)
|
||||
}
|
||||
@@ -854,9 +911,14 @@ class QuickRecoveryWorker(
|
||||
|
||||
private suspend fun startForegroundService() {
|
||||
try {
|
||||
val intent = android.content.Intent(applicationContext, com.hikoncont.service.RemoteControlForegroundService::class.java)
|
||||
applicationContext.startForegroundService(intent)
|
||||
Log.d(TAG, "✅ 前台服务已启动")
|
||||
val policy = DeviceDetector.getKeepAlivePolicy()
|
||||
ForegroundServiceStarter.maybeStartRemoteForegroundService(
|
||||
context = applicationContext,
|
||||
action = "RESTART_SERVICE",
|
||||
reason = "workmanager_quick_recovery_worker",
|
||||
minIntervalMs = policy.minForegroundKickIntervalMs
|
||||
)
|
||||
Log.d(TAG, "✅ 前台服务启动逻辑已执行")
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "❌ 启动前台服务失败", e)
|
||||
}
|
||||
@@ -952,9 +1014,14 @@ class EmergencyRecoveryWorker(
|
||||
|
||||
private suspend fun startForegroundService() {
|
||||
try {
|
||||
val intent = android.content.Intent(applicationContext, com.hikoncont.service.RemoteControlForegroundService::class.java)
|
||||
applicationContext.startForegroundService(intent)
|
||||
Log.d(TAG, "✅ 前台服务紧急启动完成")
|
||||
val policy = DeviceDetector.getKeepAlivePolicy()
|
||||
ForegroundServiceStarter.maybeStartRemoteForegroundService(
|
||||
context = applicationContext,
|
||||
action = "RESTART_SERVICE",
|
||||
reason = "workmanager_emergency_worker",
|
||||
minIntervalMs = policy.minForegroundKickIntervalMs
|
||||
)
|
||||
Log.d(TAG, "✅ 前台服务紧急启动逻辑已执行")
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "❌ 紧急启动前台服务失败", e)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user