From 363ba0a60fbadf09f0cdb1c653cf100a8c54f15f Mon Sep 17 00:00:00 2001 From: gallonyin Date: Wed, 4 Jan 2023 17:04:52 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E4=BC=98=E5=8C=96=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E9=A6=96=E9=A1=B5=E5=92=8C=E5=9B=9E=E5=A4=8D=E9=80=9F=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/yameida/worktool/Constant.kt | 1 + .../yameida/worktool/service/GlobalMethod.kt | 31 ++-- .../worktool/service/WeworkLoopImpl.kt | 133 +++++++++++------- .../worktool/service/WeworkOperationImpl.kt | 3 +- .../worktool/utils/WebSocketManager.java | 2 +- 5 files changed, 97 insertions(+), 73 deletions(-) diff --git a/app/src/main/java/org/yameida/worktool/Constant.kt b/app/src/main/java/org/yameida/worktool/Constant.kt index d974739..4ae88b7 100644 --- a/app/src/main/java/org/yameida/worktool/Constant.kt +++ b/app/src/main/java/org/yameida/worktool/Constant.kt @@ -26,6 +26,7 @@ object Constant { set(value) { SPUtils.getInstance().put("robotId", value) } + //replyStrategy=replyAll+1 replyStrategy=0不回复 replyStrategy=1回复at replyStrategy=2回复所有 var replyStrategy: Int get() = SPUtils.getInstance().getInt("replyStrategy", 1) set(value) { diff --git a/app/src/main/java/org/yameida/worktool/service/GlobalMethod.kt b/app/src/main/java/org/yameida/worktool/service/GlobalMethod.kt index 07e2a00..e16c521 100644 --- a/app/src/main/java/org/yameida/worktool/service/GlobalMethod.kt +++ b/app/src/main/java/org/yameida/worktool/service/GlobalMethod.kt @@ -30,9 +30,8 @@ fun goHome() { */ fun goHomeTab(title: String): Boolean { var atHome = false - var find = false while (!atHome) { - val list = AccessibilityUtil.findAllOnceByText(getRoot(), "消息", exact = true) + val list = AccessibilityUtil.findAllOnceByText(getRoot(), title, exact = true) for (item in list) { val childCount = item.parent?.parent?.parent?.childCount if (childCount == 4 || childCount == 5) { @@ -41,19 +40,14 @@ fun goHomeTab(title: String): Boolean { val rect = Rect() item.getBoundsInScreen(rect) if (rect.left > ScreenUtils.getScreenWidth() / 2) { - goHomeTab("工作台") + return goHomeTab("工作台") && goHomeTab("消息") } } + if (!item.isSelected) { + AccessibilityUtil.performClick(item) + sleep(300) + } atHome = true - val tempList = AccessibilityUtil.findAllOnceByText(getRoot(), title, exact = true) - for (tempItem in tempList) { - val tempChildCount = tempItem.parent?.parent?.parent?.childCount - if (tempChildCount == 4 || tempChildCount == 5) { - AccessibilityUtil.performClick(tempItem) - sleep(300) - find = true - } - } } } if (!atHome) { @@ -68,7 +62,7 @@ fun goHomeTab(title: String): Boolean { } } LogUtils.v("进入首页-${title}页") - return find + return atHome } /** @@ -76,10 +70,15 @@ fun goHomeTab(title: String): Boolean { */ fun isAtHome(): Boolean { val list = AccessibilityUtil.findAllOnceByText(getRoot(), "消息", exact = true) - return list.count { + val item = list.firstOrNull { val childCount = it.parent?.parent?.parent?.childCount - childCount == 4 || childCount == 5 - } > 0 + (childCount == 4 || childCount == 5) + } ?: return false + if (!item.isSelected) { + AccessibilityUtil.performClick(item) + sleep(300) + } + return true } /** diff --git a/app/src/main/java/org/yameida/worktool/service/WeworkLoopImpl.kt b/app/src/main/java/org/yameida/worktool/service/WeworkLoopImpl.kt index edc581f..f0b3b10 100644 --- a/app/src/main/java/org/yameida/worktool/service/WeworkLoopImpl.kt +++ b/app/src/main/java/org/yameida/worktool/service/WeworkLoopImpl.kt @@ -26,18 +26,19 @@ object WeworkLoopImpl { mainLoopRunning = true try { while (mainLoopRunning) { - if (WeworkRoomUtil.getRoomType(false) != WeworkMessageBean.ROOM_TYPE_UNKNOWN - && getChatMessageList()) { + if (!isAtHome() && WeworkRoomUtil.getRoomType(false) != WeworkMessageBean.ROOM_TYPE_UNKNOWN) { + LogUtils.d("当前在房间: ") + getChatMessageList() + if (mainLoopRunning) { + goHome() + } + continue } if (!mainLoopRunning) break - goHomeTab("消息") + getChatroomList() if (!mainLoopRunning) break - if (getChatroomList()) { - } - if (!mainLoopRunning) break - if (getFriendRequest()) { - } - sleep(500) + getFriendRequest() + sleep(300) } } catch (e: Exception) { mainLoopRunning = false @@ -104,9 +105,8 @@ object WeworkLoopImpl { * @param needInfer 是否需要推断@me并等待回复 * @param timeout 在房间内等待回复的时长 */ - fun getChatMessageList(needInfer: Boolean = true, timeout: Long = 3000): Boolean { + fun getChatMessageList(needInfer: Boolean = true, timeout: Long = 5000): Boolean { if (Constant.autoReply == 0) return true - AccessibilityUtil.performScrollDown(getRoot(), 0) val roomType = WeworkRoomUtil.getRoomType() var titleList = WeworkRoomUtil.getRoomTitle() if (titleList.contains("对方正在输入…")) { @@ -139,28 +139,40 @@ object WeworkLoopImpl { ) //推测是否回复并在房间等待指令 if (needInfer) { - val lastMessage = messageList.lastOrNull { it.sender == 0 } - if (lastMessage != null) { - var tempContent = "" - for (itemMessage in lastMessage.itemMessageList) { - if (itemMessage.text.contains("@" + Constant.myName)) { - tempContent = itemMessage.text + val lastMessage = messageList.lastOrNull() + if (lastMessage != null && lastMessage.sender == 0) { + when (Constant.replyStrategy) { + 1 -> { + var tempContent = "" + for (itemMessage in lastMessage.itemMessageList) { + if (itemMessage.text.contains("@" + Constant.myName)) { + tempContent = itemMessage.text + } + } + if (roomType == WeworkMessageBean.ROOM_TYPE_EXTERNAL_CONTACT + || roomType == WeworkMessageBean.ROOM_TYPE_INTERNAL_CONTACT + || tempContent.isNotBlank() + ) { + LogUtils.v("推测需要回复: $tempContent") + val startTime = System.currentTimeMillis() + var currentTime = startTime + while (mainLoopRunning && currentTime - startTime < timeout) { + sleep(Constant.POP_WINDOW_INTERVAL / 5) + currentTime = System.currentTimeMillis() + } + return getChatMessageList(needInfer = false) + } } - } - if (roomType == WeworkMessageBean.ROOM_TYPE_EXTERNAL_CONTACT - || roomType == WeworkMessageBean.ROOM_TYPE_INTERNAL_CONTACT - || tempContent.isNotBlank() - ) { - LogUtils.v("推测需要回复: $tempContent") - val startTime = System.currentTimeMillis() - var currentTime = startTime - while (mainLoopRunning && currentTime - startTime < timeout) { - sleep(Constant.POP_WINDOW_INTERVAL / 5) - currentTime = System.currentTimeMillis() - } - if (mainLoopRunning) { + 2 -> { + val startTime = System.currentTimeMillis() + var currentTime = startTime + while (mainLoopRunning && currentTime - startTime < timeout) { + sleep(Constant.POP_WINDOW_INTERVAL / 5) + currentTime = System.currentTimeMillis() + } return getChatMessageList(needInfer = false) } + else -> return true } } } @@ -225,20 +237,53 @@ object WeworkLoopImpl { */ private fun getChatroomList(): Boolean { if (Constant.autoReply == 0) return true - if (!isAtHome()) return true + if (!isAtHome()) { goHome() } + if (logIndex++ % 30 == 0) { + LogUtils.d("读取首页聊天列表") + if (logIndex % 120 == 0) log("读取首页聊天列表") + } + + var hasNewMessage: AccessibilityNodeInfo? = null val list = AccessibilityUtil.findAllOnceByText(getRoot(), "消息", exact = true) for (item in list) { val childCount = item.parent?.parent?.parent?.childCount if (childCount == 4 || childCount == 5) { if (item.parent != null && item.parent.childCount > 1) { LogUtils.d("消息有红点") - AccessibilityUtil.clickByNode(WeworkController.weworkService, item) - sleep(100) - AccessibilityUtil.clickByNode(WeworkController.weworkService, item) + hasNewMessage = item } } } + val listview = AccessibilityUtil.findOneByClazz(getRoot(), Views.RecyclerView, Views.ListView, Views.ViewGroup) + if (listview != null && listview.childCount >= 2) { + if (hasNewMessage != null) { + if (checkUnreadChatRoom(listview)) { + //如果有红点 点击进入聊天页 + return true + } else { + AccessibilityUtil.clickByNode(WeworkController.weworkService, hasNewMessage) + sleep(Constant.POP_WINDOW_INTERVAL / 5) + AccessibilityUtil.clickByNode(WeworkController.weworkService, hasNewMessage) + sleep(Constant.POP_WINDOW_INTERVAL / 5) + //双击消息再试一次 + if (checkUnreadChatRoom(listview)) { + //如果有红点 点击进入聊天页 + return true + } + } + } else { + if (checkNoTipMessage(listview) == 1) { + //如果发现拉入群聊/修改群名/移出群聊 点击进入聊天页 + return true + } else { + LogUtils.v("未发现新消息或无提示消息") + } + } + } else { + LogUtils.e("读取聊天列表失败") + error("读取聊天列表失败") + } if (logIndex % 120 == 0) { //让企微切换页面使APP保持活跃 goHomeTab("通讯录") @@ -259,26 +304,6 @@ object WeworkLoopImpl { } } } - if (!isAtHome()) return true - if (logIndex++ % 30 == 0) { - LogUtils.i("读取首页聊天列表") - if (logIndex % 120 == 0) log("读取首页聊天列表") - } - val listview = AccessibilityUtil.findOneByClazz(getRoot(), Views.RecyclerView, Views.ListView, Views.ViewGroup) - if (listview != null && listview.childCount >= 2) { - if (checkUnreadChatRoom(listview)) { - //如果有红点 点击进入聊天页 - return true - } else if (checkNoTipMessage(listview) == 1) { - //如果发现拉入群聊/修改群名/移出群聊 点击进入聊天页 - return true - } else { - LogUtils.v("未发现新消息或无提示消息") - } - } else { - LogUtils.e("读取聊天列表失败") - error("读取聊天列表失败") - } return false } diff --git a/app/src/main/java/org/yameida/worktool/service/WeworkOperationImpl.kt b/app/src/main/java/org/yameida/worktool/service/WeworkOperationImpl.kt index feafb15..a910e5a 100644 --- a/app/src/main/java/org/yameida/worktool/service/WeworkOperationImpl.kt +++ b/app/src/main/java/org/yameida/worktool/service/WeworkOperationImpl.kt @@ -102,7 +102,6 @@ object WeworkOperationImpl { LogUtils.v("开始回复") if (sendChatMessage(receivedContent, reply = true)) { LogUtils.d("$title: 回复成功") - WeworkLoopImpl.getChatMessageList() uploadCommandResult(message, ExecCallbackBean.SUCCESS, "", startTime) return true } else { @@ -116,7 +115,6 @@ object WeworkOperationImpl { val text = if (originalContent.isNotEmpty()) "【$originalContent】\n$receivedContent" else receivedContent if (sendChatMessage(text, receivedName)) { LogUtils.d("$title: 直接发送答案成功") - WeworkLoopImpl.getChatMessageList() uploadCommandResult(message, ExecCallbackBean.SUCCESS, "", startTime) return true } else { @@ -1608,6 +1606,7 @@ object WeworkOperationImpl { LogUtils.d("发送消息: \n$content") log("发送消息: \n$content") AccessibilityUtil.performClick(sendButton) + WeworkLoopImpl.getChatMessageList() return true } else { LogUtils.e("未找到发送按钮") diff --git a/app/src/main/java/org/yameida/worktool/utils/WebSocketManager.java b/app/src/main/java/org/yameida/worktool/utils/WebSocketManager.java index e9e355c..8768793 100644 --- a/app/src/main/java/org/yameida/worktool/utils/WebSocketManager.java +++ b/app/src/main/java/org/yameida/worktool/utils/WebSocketManager.java @@ -157,7 +157,7 @@ public class WebSocketManager { //重连后刷新连接时间 lastConnectedTime = System.currentTimeMillis(); } - if (System.currentTimeMillis() - lastConnectedTime > heartBeatRate * 2000 && !FloatWindowHelper.INSTANCE.isPause()) { + if (System.currentTimeMillis() - lastConnectedTime > heartBeatRate * 3000 && !FloatWindowHelper.INSTANCE.isPause()) { ToastUtils.show("机器人运行中 请勿人工操作手机~"); } };