xiongzhu 1 ano atrás
pai
commit
20176c1c4b

+ 2 - 0
app/src/main/java/com/example/modifier/MainActivity.kt

@@ -14,6 +14,7 @@ import androidx.navigation.fragment.NavHostFragment
 import androidx.navigation.ui.NavigationUI.setupWithNavController
 import com.example.modifier.databinding.ActivityMainBinding
 import com.example.modifier.service.ModifierService
+import com.example.modifier.utils.ROOT_ACCESS
 import com.example.modifier.utils.enableAccessibility
 import com.example.modifier.utils.enableOverlay
 import com.example.modifier.utils.getIPAddress
@@ -44,6 +45,7 @@ class MainActivity : AppCompatActivity() {
         setupWithNavController(mBinding.nav, controller)
         CoroutineScope(Dispatchers.IO).launch {
             if (hasRootAccess()) {
+                ROOT_ACCESS = true
                 if (!enableAccessibility()) {
                     val intent = Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS)
                     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)

+ 9 - 8
app/src/main/java/com/example/modifier/repo/GmsgStateRepo.kt

@@ -6,6 +6,8 @@ import com.example.modifier.baseTag
 import com.example.modifier.enums.RcsConfigureState
 import com.example.modifier.model.TaskExecutionResult
 import com.example.modifier.model.TaskItem
+import com.example.modifier.utils.ADB
+import com.example.modifier.utils.ROOT_ACCESS
 import kotlinx.coroutines.CoroutineScope
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.cancel
@@ -29,17 +31,15 @@ class GmsgStateRepo private constructor() {
         val instance by lazy(LazyThreadSafetyMode.SYNCHRONIZED) { GmsgStateRepo() }
     }
 
-    init {
-        CoroutineScope(Dispatchers.IO).launch {
-            startLogging()
-        }
-    }
-
-    private suspend fun startLogging() {
+    suspend fun startLogging() {
         try {
             val logsCache = CircularFifoQueue<String>(128)
             val p = withContext(Dispatchers.IO) {
-                Runtime.getRuntime().exec("su")
+                if (!ROOT_ACCESS) {
+                    ADB.createProcess(ADB.adbPath, "-s", "localhost:5555", "shell")
+                } else {
+                    Runtime.getRuntime().exec("su")
+                }
             }
             p.outputStream.bufferedWriter().use { writer ->
                 writer.write("logcat -c")
@@ -53,6 +53,7 @@ class GmsgStateRepo private constructor() {
                 .bufferedReader()
                 .useLines { lines ->
                     lines.forEach { line ->
+                        Log.d(TAG, "MessagingLogging: $line")
                         if (line.contains("destState=CheckPreconditionsState")) {
                             rcsConfigureState.emit(RcsConfigureState.NOT_CONFIGURED)
                         } else if (line.contains("destState=ReadyState")) {

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

@@ -8,6 +8,8 @@ import android.graphics.PixelFormat
 import android.net.Uri
 import android.os.Build
 import android.provider.Settings
+import android.text.SpannableString
+import android.text.SpannableStringBuilder
 import android.util.DisplayMetrics
 import android.util.Log
 import android.view.Gravity
@@ -37,6 +39,7 @@ import com.example.modifier.repo.AppStateRepo
 import com.example.modifier.repo.BackupRepository
 import com.example.modifier.repo.GmsgStateRepo
 import com.example.modifier.repo.SpoofedSimInfoRepo
+import com.example.modifier.utils.ROOT_ACCESS
 import com.example.modifier.utils.checkPif
 import com.example.modifier.utils.clearConv
 import com.example.modifier.utils.findFirstByDesc
@@ -228,7 +231,16 @@ class ModifierService : AccessibilityService() {
                 }
                 false
             }
-            if (!hasRoot) {
+            if (hasRoot) {
+                ROOT_ACCESS = true
+            } else {
+                if (!ADB.connect()) {
+                    binding.tvLog.text = "ADB connection failed"
+                    return@launch
+                }
+            }
+            launch {
+                gmsgStateRepo.startLogging()
             }
             if (isRebooted()) {
                 delay(2.minutes)

+ 15 - 21
app/src/main/java/com/example/modifier/utils/ADB.kt

@@ -11,7 +11,7 @@ import kotlinx.coroutines.withContext
 class ADB {
     companion object {
         const val TAG = "ADB"
-        private val adbPath by lazy { "${getContext().applicationInfo.nativeLibraryDir}/libadb.so" }
+        val adbPath by lazy { "${getContext().applicationInfo.nativeLibraryDir}/libadb.so" }
         private val homeDir by lazy { getContext().filesDir }
         private val cacheDir by lazy { getContext().cacheDir }
 
@@ -46,16 +46,7 @@ class ADB {
                 var output = ""
                 var error = ""
                 val process = withContext(Dispatchers.IO) {
-                    ProcessBuilder(fullCommand)
-                        .directory(homeDir)
-                        .apply {
-//                    redirectErrorStream(true)
-                            environment().apply {
-                                put("HOME", homeDir.path)
-                                put("TMPDIR", cacheDir.path)
-                            }
-                        }
-                        .start()
+                    createProcess(*fullCommand.toTypedArray())
                 }
                 coroutineScope {
                     launch {
@@ -94,16 +85,7 @@ class ADB {
                 var output = ""
                 var error = ""
                 val process = withContext(Dispatchers.IO) {
-                    ProcessBuilder(listOf(adbPath, "-s", "localhost:5555", "shell"))
-                        .directory(homeDir)
-                        .apply {
-//                    redirectErrorStream(true)
-                            environment().apply {
-                                put("HOME", homeDir.path)
-                                put("TMPDIR", cacheDir.path)
-                            }
-                        }
-                        .start()
+                    createProcess(adbPath, "-s", "localhost:5555", "shell")
                 }
                 process.outputStream.bufferedWriter().use { writer ->
                     commands.forEach { command ->
@@ -140,5 +122,17 @@ class ADB {
             }
             return AdbResult()
         }
+
+        fun createProcess(vararg command: String): Process {
+            return ProcessBuilder(*command)
+                .directory(homeDir)
+                .apply {
+//                    redirectErrorStream(true)
+                    environment().apply {
+                        put("HOME", homeDir.path)
+                        put("TMPDIR", cacheDir.path)
+                    }
+                }.start()
+        }
     }
 }

+ 4 - 0
app/src/main/java/com/example/modifier/utils/Root.kt

@@ -9,6 +9,10 @@ import kotlinx.coroutines.withContext
 
 const val shellTag = "$baseTag/shell"
 suspend fun shellRun(vararg commands: String): Pair<String, String> {
+    if (!ROOT_ACCESS) {
+        val res = ADB.adbShell(*commands)
+        return Pair(res.output, res.error)
+    }
     var output = ""
     var error = ""
     Log.i(shellTag, "shellRun: \t${commands.joinToString("\n\t\t\t")}")

+ 31 - 7
app/src/main/java/com/example/modifier/utils/System.kt

@@ -28,7 +28,9 @@ import io.ktor.client.call.body
 import io.ktor.client.plugins.resources.get
 import io.ktor.client.request.head
 import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.coroutineScope
 import kotlinx.coroutines.delay
+import kotlinx.coroutines.launch
 import kotlinx.coroutines.withContext
 import org.apache.commons.io.FileUtils
 import org.json.JSONObject
@@ -43,7 +45,7 @@ import kotlin.system.exitProcess
 
 
 const val systemTag = "$baseTag/System"
-const val ROOTED_PHONE = false
+var ROOT_ACCESS = false
 val uniqueId: String
     @SuppressLint("HardwareIds")
     get() {
@@ -77,7 +79,29 @@ fun getContext(): Context {
 suspend fun hasRootAccess(): Boolean {
     var rootAccess = false
     try {
-        val (output, _) = shellRun("echo \"imrooted\"")
+        val p = withContext(Dispatchers.IO) {
+            ProcessBuilder("su", "-M", "-c", "echo", "imrooted").start()
+        }
+        var output = ""
+        coroutineScope {
+            launch {
+                p.inputStream.bufferedReader().useLines { line ->
+                    line.forEach {
+                        output += it + "\n"
+                    }
+                }
+            }
+            launch {
+                p.errorStream.bufferedReader().useLines { line ->
+                    line.forEach {
+                        Log.e(shellTag, "shellRunErr: $it")
+                    }
+                }
+            }
+        }
+        withContext(Dispatchers.IO) {
+            p.waitFor()
+        }
         if (output.contains("imrooted")) {
             rootAccess = true
         }
@@ -157,11 +181,11 @@ suspend fun optimize() {
         "pm grant ${BuildConfig.APPLICATION_ID} android.permission.POST_NOTIFICATIONS",
     )
 //    if (Build.MODEL.startsWith("SM-F707") || Build.MODEL.startsWith("SM-F711")) {
-        shellRun(
-            "settings put global window_animation_scale 1",
-            "settings put global transition_animation_scale 1",
-            "settings put global animator_duration_scale 1"
-        )
+    shellRun(
+        "settings put global window_animation_scale 1",
+        "settings put global transition_animation_scale 1",
+        "settings put global animator_duration_scale 1"
+    )
 //    }
 }