fix: 修复启动时三个应用层报错问题
- MediaProjectionManager初始化提前到onCreate开头,避免多个return路径跳过初始化导致后续null - InputController剪贴板访问添加SecurityException防护,非前台时安全降级而非崩溃 - HuaweiAuthorizationHandler剪贴板访问添加异常捕获,防止ClipboardService拒绝访问 - SocketIO连接错误区分瞬态错误(xhr poll/timeout)和持久错误,瞬态错误降级为WARN - SocketIO connect方法添加URL格式验证和空值检查
This commit is contained in:
@@ -23,6 +23,23 @@ class InputController(private val service: AccessibilityRemoteService) {
|
||||
private val context: Context = service
|
||||
private val clipboardManager = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
|
||||
|
||||
/**
|
||||
* Safe clipboard write - checks if the service can access clipboard
|
||||
* Android 10+ restricts clipboard access to foreground apps only
|
||||
*/
|
||||
private fun safeSetClipboard(clipData: ClipData): Boolean {
|
||||
return try {
|
||||
clipboardManager.setPrimaryClip(clipData)
|
||||
true
|
||||
} catch (e: SecurityException) {
|
||||
Log.w(TAG, "Clipboard access denied (app not in foreground): ${e.message}")
|
||||
false
|
||||
} catch (e: Exception) {
|
||||
Log.w(TAG, "Clipboard operation failed: ${e.message}")
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 输入文本 - 智能输入策略
|
||||
*/
|
||||
@@ -118,11 +135,14 @@ class InputController(private val service: AccessibilityRemoteService) {
|
||||
*/
|
||||
private fun tryClipboardPaste(text: String): Boolean {
|
||||
return try {
|
||||
// 1. 将文本复制到剪贴板
|
||||
// 1. Safe clipboard write
|
||||
val clipData = ClipData.newPlainText("remote_input", text)
|
||||
clipboardManager.setPrimaryClip(clipData)
|
||||
if (!safeSetClipboard(clipData)) {
|
||||
Log.w(TAG, "Clipboard paste skipped: clipboard access denied")
|
||||
return false
|
||||
}
|
||||
|
||||
// 2. 模拟粘贴操作
|
||||
// 2. Perform paste action
|
||||
val focusedNode = findFocusedInputNode()
|
||||
focusedNode?.let { node ->
|
||||
val result = node.performAction(AccessibilityNodeInfo.ACTION_PASTE)
|
||||
@@ -131,7 +151,7 @@ class InputController(private val service: AccessibilityRemoteService) {
|
||||
} ?: false
|
||||
|
||||
} catch (e: Exception) {
|
||||
Log.w(TAG, "剪贴板粘贴失败", e)
|
||||
Log.w(TAG, "Clipboard paste failed", e)
|
||||
false
|
||||
}
|
||||
}
|
||||
@@ -189,29 +209,30 @@ class InputController(private val service: AccessibilityRemoteService) {
|
||||
return try {
|
||||
val focusedNode = findFocusedInputNode()
|
||||
focusedNode?.let { node ->
|
||||
// 获取当前文本
|
||||
val currentText = node.text?.toString() ?: ""
|
||||
// 追加新字符
|
||||
val newText = currentText + char
|
||||
|
||||
// 将新文本复制到剪贴板
|
||||
// Safe clipboard write
|
||||
val clipData = ClipData.newPlainText("remote_append", newText)
|
||||
clipboardManager.setPrimaryClip(clipData)
|
||||
if (!safeSetClipboard(clipData)) {
|
||||
Log.w(TAG, "Clipboard append skipped: clipboard access denied")
|
||||
node.recycle()
|
||||
return false
|
||||
}
|
||||
|
||||
// 先清空再粘贴
|
||||
// Clear then paste
|
||||
val clearBundle = Bundle().apply {
|
||||
putCharSequence(AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE, "")
|
||||
}
|
||||
node.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT, clearBundle)
|
||||
|
||||
// 粘贴新内容
|
||||
val result = node.performAction(AccessibilityNodeInfo.ACTION_PASTE)
|
||||
node.recycle()
|
||||
result
|
||||
} ?: false
|
||||
|
||||
} catch (e: Exception) {
|
||||
Log.w(TAG, "剪贴板追加失败", e)
|
||||
Log.w(TAG, "Clipboard append failed", e)
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user