RikkaApps / Shizuku-API

The API and the developer guide for Shizuku and Sui.
MIT License
950 stars 223 forks source link

Shizuku.addRequestPermissionResultListener not triggering? #98

Closed samolego closed 3 months ago

samolego commented 3 months ago

This is my code for Shizuku permission granting:

class ShizukuData {
    companion object {
        private const val SHIZUKU_CODE = 0xCA07A

        private val isSui: Boolean = Sui.init(packageName)
        private val TAG: String = ShizukuData::class.java.simpleName
        private var shizukuPermissionFuture = CompletableFuture<Boolean>()
        private var binderStatus = Shizuku.pingBinder()

        fun init() {
            Shizuku.addRequestPermissionResultListener { requestCode, grantResult ->
                if (requestCode == SHIZUKU_CODE) {
                    val granted = grantResult == PackageManager.PERMISSION_GRANTED
                    shizukuPermissionFuture.complete(granted)
                }
            }
            Shizuku.addBinderDeadListener { binderStatus = false }
            Shizuku.addBinderReceivedListener { binderStatus = true }
        }

        /**
         * Checks if the shizuku permission is granted. Call from main thread only!
         */
        fun checkShizukuPermission(): Boolean {
            return if (!binderStatus || Shizuku.isPreV11() || Shizuku.shouldShowRequestPermissionRationale()) {
                val shouldShow = try {
                    Shizuku.shouldShowRequestPermissionRationale()
                } catch (e: Exception) {
                    Log.e(TAG, "Error while checking shizuku permission", e)
                    false
                }
                Log.i(
                    TAG,
                    "Shizuku permission result: ping: ${Shizuku.pingBinder()}, preV11: ${Shizuku.isPreV11()}, shouldShowRequestPermissionRationale: $shouldShow"
                )
                false
            } else if (Shizuku.checkSelfPermission() == PackageManager.PERMISSION_GRANTED || isSui) {
                Log.i(
                    TAG,
                    "Shizuku permission result: ${Shizuku.checkSelfPermission()}, sui status: $isSui"
                )
                true
            } else {
                Log.i(TAG, "Requesting shizuku permission")
                Shizuku.requestPermission(SHIZUKU_CODE)

                val result = shizukuPermissionFuture.get()  // The process hangs here
                shizukuPermissionFuture = CompletableFuture<Boolean>()

                result
            }
        }
    }
}

I call it on button press:


onClick = {
                    // Check shizuku permission
                    val permission = ShizukuData.checkShizukuPermission()
                    coroutineScope.launch {
                        println("Shizuku permission: $permission")
                        ...
                    }
                }

The app proccess always hangs due to get call on CompletableFuture (which never gets completed, I tried putting some prints in addRequestPermissionResultListener and they don't get triggered as well. Do you know what I'm doing wrong?

Whole code is available at https://github.com/samolego/Canta