xiongzhu 1 жил өмнө
parent
commit
5b6d9b8b5d

+ 1 - 1
app/src/main/java/com/example/modifier/service/ModifierService.kt

@@ -368,7 +368,7 @@ class ModifierService : AccessibilityService() {
                     }
 
                     R.id.check_availability -> {
-                        taskRunner.checkRcsAvailability()
+                        taskRunner.checkRcsA10y()
                     }
 
                     R.id.toggle_on -> {

+ 46 - 67
app/src/main/java/com/example/modifier/service/TaskRunner.kt

@@ -50,8 +50,6 @@ import com.example.modifier.utils.resetAll
 import com.example.modifier.utils.shellRun
 import com.example.modifier.utils.smsIntent
 import com.example.modifier.utils.spoofSmsIntent
-import com.example.modifier.utils.stopClash
-import com.example.modifier.utils.uniqueId
 import io.ktor.client.HttpClient
 import io.ktor.client.call.body
 import io.ktor.client.engine.okhttp.OkHttp
@@ -118,10 +116,7 @@ class TaskRunner(
         Log.i(tag, "Sending SMS to $to: $body")
         context.startActivity(smsIntent(to, body))
         try {
-            Log.i(
-                tag,
-                "Command executed successfully, waiting for app to open..."
-            )
+            Log.i(tag, "Command executed successfully, waiting for app to open...")
             delay(1000)
             var success = false
             var traverseResult = TraverseResult()
@@ -195,9 +190,9 @@ class TaskRunner(
             currentTaskId = taskAction.data.taskId
             requestMode = if (taskAction.data.config.useBackup) 2 else 1
 
-            if (taskAction.data.config.checkConnection) {
+            if (taskAction.data.config.checkConnection && appState.value.executedNum == 0) {
                 appStateRepository.updateRuntimeFlags(checkingConnection = true)
-                if (!checkRcsAvailability()) {
+                if (!checkRcsA10y()) {
                     onError(Exception("RCS not available"))
                     requestNumber()
                     appStateRepository.updateRuntimeFlags(checkingConnection = false)
@@ -242,6 +237,14 @@ class TaskRunner(
                 delay(2000)
             }
             appStateRepository.updateRuntimeFlags(running = false)
+            if (taskAction.data.config.checkConnection && success.isEmpty()) {
+                appStateRepository.updateRuntimeFlags(checkingConnection = true)
+                if (!checkRcsA10y()) {
+                    onError(Exception("RCS not available"))
+                    requestNumber()
+                }
+                appStateRepository.updateRuntimeFlags(checkingConnection = false)
+            }
         } catch (e: Exception) {
             Log.e(tag, "runTaskError: ${e.message}", e)
             onError(e)
@@ -276,10 +279,7 @@ class TaskRunner(
                             it == RcsConfigureState.WAITING_FOR_TOS
                         }
                         if (switchAppear != true) {
-                            Log.e(
-                                tag,
-                                "RCS not entered default on state, retrying..."
-                            )
+                            Log.e(tag, "RCS not entered default on state, retrying...")
                             continue
                         }
                     }
@@ -340,10 +340,7 @@ class TaskRunner(
                             it == RcsConfigureState.WAITING_FOR_DEFAULT_ON
                         }
                         if (switchAppear != true) {
-                            Log.e(
-                                tag,
-                                "RCS not entered default on state, retrying..."
-                            )
+                            Log.e(tag, "RCS not entered default on state, retrying...")
                             continue
                         }
                     }
@@ -701,60 +698,42 @@ class TaskRunner(
         false
     }
 
-    suspend fun checkRcsAvailability(): Boolean {
-        val availability = run checkAvailability@{
-            val rcsConnected = checkRcsConnectivity()
-            if (!rcsConnected) {
-                return@checkAvailability false
-            }
-
-            val checkRcsAvailabilityNumbers = mutableListOf<String>()
-            try {
-                val config = ktorClient(appPreferences.value.server).get(
-                    SysConfigApi.Id(
-                        SysConfigApi(),
-                        "check_availability_numbers"
-                    )
-                )
-                    .body<SysConfigResponse>()
-                Log.i(tag, "sysConfig response: $config")
-                checkRcsAvailabilityNumbers.addAll(
-                    config.value.split(",").map { it.trim() })
-            } catch (exception: Exception) {
-                Log.e(
-                    tag,
-                    "sysConfig Error: ${exception.message}",
-                    exception
-                )
-            }
-
-            if (checkRcsAvailabilityNumbers.isEmpty()) {
-                Log.e(tag, "checkRcsAvailabilityNumbers is empty")
-                return@checkAvailability true
-            }
-
-            checkRcsAvailabilityNumbers.forEach {
-                context.startActivity(smsIntent(it, ""))
-                val s = withTimeoutOrNull(5.seconds) {
-                    while (true) {
-                        val traverseResult = TraverseResult()
-                        screenInspector.traverseNode(traverseResult)
-                        if (traverseResult.isRcsCapable) {
-                            return@withTimeoutOrNull true
-                        } else {
-                            Log.i(
-                                tag,
-                                "checkRcsAvailability: RCS not detected"
-                            )
+    suspend fun checkRcsA10y(): Boolean {
+        val availability = run checkA10y@{
+            repeat(3) {
+                val rcsConnected = checkRcsConnectivity()
+                if (rcsConnected) {
+                    val checkRcsA10yNumbers = mutableListOf<String>()
+                    try {
+                        val config = ktorClient(appPreferences.value.server)
+                            .get(SysConfigApi.Id(SysConfigApi(), "check_availability_numbers"))
+                            .body<SysConfigResponse>()
+                        Log.i(tag, "sysConfig response: $config")
+                        checkRcsA10yNumbers.addAll(config.value.split(",").map { it.trim() })
+                    } catch (exception: Exception) {
+                        Log.e(tag, "sysConfig Error: ${exception.message}", exception)
+                    }
+
+                    if (checkRcsA10yNumbers.isEmpty()) {
+                        Log.e(tag, "checkRcsA10yNumbers is empty")
+                        return@checkA10y true
+                    }
+
+                    checkRcsA10yNumbers.forEach {
+                        context.startActivity(smsIntent(it, ""))
+                        repeat(10000 / 200) {
+                            delay(200)
+                            val traverseResult = TraverseResult()
+                            screenInspector.traverseNode(traverseResult)
+                            if (traverseResult.isRcsCapable) {
+                                return@checkA10y true
+                            } else {
+                                Log.i(tag, "checkRcsA10y: RCS not detected")
+                            }
                         }
-                        delay(200)
                     }
                 }
-                if (s == true) {
-                    Log.i(tag, "checkRcsAvailability: $it success")
-                    delay(1000)
-                    return@checkAvailability true
-                }
+                shellRun(PACKAGE_MESSAGING.kill(), CMD_MESSAGING_APP, "sleep 5")
             }
             false
         }