1
1
package be.mygod.librootkotlinx.demo
2
2
3
3
import android.os.Bundle
4
+ import android.os.Process
5
+ import android.text.method.ScrollingMovementMethod
4
6
import android.widget.TextView
5
7
import androidx.activity.ComponentActivity
6
8
import androidx.lifecycle.lifecycleScope
7
- import be.mygod.librootkotlinx.ParcelableInt
8
9
import be.mygod.librootkotlinx.ParcelableString
9
10
import be.mygod.librootkotlinx.RootCommand
10
11
import be.mygod.librootkotlinx.RootCommandChannel
11
12
import kotlinx.coroutines.CoroutineScope
13
+ import kotlinx.coroutines.Dispatchers
12
14
import kotlinx.coroutines.channels.produce
13
15
import kotlinx.coroutines.channels.toList
14
16
import kotlinx.coroutines.launch
17
+ import kotlinx.coroutines.withContext
15
18
import kotlinx.parcelize.Parcelize
16
19
17
20
class MainActivity : ComponentActivity () {
18
21
@Parcelize
19
- class GetRoot : RootCommand <ParcelableInt > {
20
- override suspend fun execute () = ParcelableInt (android.os.Process .myUid())
22
+ class SimpleTest : RootCommand <ParcelableString > {
23
+ override suspend fun execute () = ParcelableString (" uid: ${Process .myUid()} \n " + withContext(Dispatchers .IO ) {
24
+ // try to execute a restricted subprocess command
25
+ val process = ProcessBuilder (" /system/bin/iptables" , " -L" , " INPUT" ).start()
26
+ var output = process.inputStream.reader().readText()
27
+ when (val exit = process.waitFor()) {
28
+ 0 -> { }
29
+ else -> output + = " Process exited with $exit " .toByteArray()
30
+ }
31
+ output
32
+ })
21
33
}
22
34
23
35
@Parcelize
@@ -31,13 +43,14 @@ class MainActivity : ComponentActivity() {
31
43
override fun onCreate (savedInstanceState : Bundle ? ) {
32
44
super .onCreate(savedInstanceState)
33
45
setContentView(R .layout.activity_main)
46
+ val text = findViewById<TextView >(android.R .id.text1)
47
+ text.movementMethod = ScrollingMovementMethod ()
34
48
lifecycleScope.launch {
35
- findViewById< TextView >(android. R .id.text1) .text = try {
36
- val result = App .rootManager.use {
37
- it.execute(GetRoot ()).value.toString() + " \n " +
38
- it.create( ChannelDemo (), lifecycleScope).toList() .joinToString { it.value }
49
+ text .text = try {
50
+ App .rootManager.use {
51
+ it.execute(SimpleTest ()).value + ' \n ' + it.create( ChannelDemo (), lifecycleScope).toList()
52
+ .joinToString { it.value }
39
53
}
40
- " Got result from root: $result "
41
54
} catch (e: Exception ) {
42
55
e.printStackTrace()
43
56
e.stackTraceToString()
0 commit comments