Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reboot without java.io.EOFException #7

Open
olankens opened this issue Dec 9, 2024 · 0 comments
Open

Reboot without java.io.EOFException #7

olankens opened this issue Dec 9, 2024 · 0 comments

Comments

@olankens
Copy link

olankens commented Dec 9, 2024

When I run reboot command to a target Android device, it throws java.io.EOFException.
Is this an intentional behavior?

Here is my Device class which uses Kadb:

class Device(
    private val address: String,
    private val port: Int = 5555,
    private val code: String? = null,
    private val context: Context,
) {
    private lateinit var manager: Kadb

    suspend fun runAttach() = withContext(IO) {
        runKeygen()
        // if (code != null) Kadb.pair(address, port, code)
        manager = Kadb.create(address, port, connectTimeout = 5000, socketTimeout = 5000).also {
            val command = "connect $address:$port"
            it.shell(command)
        }
    }

    suspend fun runKeygen() = withContext(IO) {
        val sharing = context.getSharedPreferences("kadb", Context.MODE_PRIVATE)
        val cert = sharing.getString("cert", null)
        val key = sharing.getString("key", null)
        if (cert.isNullOrEmpty() || key.isNullOrEmpty()) {
            val pair = KadbCert.get()
            sharing.edit()
                .putString("cert", pair.first.decodeToString())
                .putString("key", pair.second.decodeToString()).apply()
            return@withContext
        }
        try {
            KadbCert.set(cert.toByteArray(), key.toByteArray())
        } catch (e: Exception) {
            if (e is CertificateExpiredException || e is CertificateNotYetValidException) {
                val pair = KadbCert.get()
                sharing.edit().putString("cert", pair.first.decodeToString())
                    .putString("key", pair.second.decodeToString()).apply()
            }
            throw e
        }
    }

    suspend fun runReboot() = withContext(IO)  {
        manager.shell("reboot") // This is where the error is thrown
        // runCatching { manager.shell("reboot") } // Bypassing is working
        delay(4000)
        while (true) {
            try {
                runAttach()
                break
            } catch (e: Exception) {
                delay(2000)
            }
        }
        delay(8000)
    }
}

Here is my ViewModel:

fun onButtonClicked() = viewModelScope.launch {
    loading.value = true
    val device = Device(address.value, context = context)
    try {
        device.runAttach()
        device.runReboot()
        // TODO: Do other things
    } catch (e: Exception) {
        content.value = e.message.toString()
    }
    loading.value = false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant