Jetpack Compose: Keyboard input doesn't seem to work on physical Wear OS device

548 Views Asked by At

I'm working on a Baybayin (it's an ancient Filipino writing script) keyboard for our app and it's developed in Wear OS using Jetpack Compose. I tried the code here: https://stackoverflow.com/a/70294785/16028653. It works perfectly as intended on my virtual device and I was able to output Baybayin characters using a special font style for it. However, when I tried using it on my Samsung Galaxy Watch 4, it crashes after I input the text on the keyboard.

The error says android.text.SpannableString cannot be cast to java.lang.String, caused by this code:

rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) {
it.data?.let { data -\>
val results: Bundle = RemoteInput.getResultsFromIntent(data)
val ipAddress: CharSequence? = results.getCharSequence("ip_address")
label.value = ipAddress as String
}

Here's the whole code:

@Composable
fun Keyboard(){
    val contentModifier = Modifier
        .padding(top = 17.dp)
        .fillMaxWidth()
        .wrapContentSize(align = Alignment.Center)
    val label = remember { mutableStateOf("Start")}
    val launcher =
        rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) {
            it.data?.let { data ->
                val results: Bundle = RemoteInput.getResultsFromIntent(data)
                val ipAddress: CharSequence? = results.getCharSequence("ip_address")
                label.value = ipAddress as String
            }
        }
    Column() {
        Spacer(modifier = Modifier.height(20.dp))
        Chip(
            label = { Text(label.value, style = MaterialTheme.typography.body2)}, //baybayin font style
            onClick = {}
        )
        Chip(
            label = { Text("Search with specific IP") },
            onClick = {
                val intent: Intent = RemoteInputIntentHelper.createActionRemoteInputIntent();
                val remoteInputs: List<RemoteInput> = listOf(
                    RemoteInput.Builder("ip_address")
                        .setLabel("Manual IP Entry")
                        .wearableExtender {
                            setEmojisAllowed(false)
                            setInputActionType(EditorInfo.IME_ACTION_DONE)
                        }.build()
                )

                RemoteInputIntentHelper.putRemoteInputsExtra(intent, remoteInputs)

                launcher.launch(intent)
            }
        )
    }
}

I'm sure that the chip's label accepts string. I'm not sure where it became a spannable string. label.value expects it to be a string and I can't convert it to spannable. I also tried using Gboard with no success.

And here's the stack trace:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.twentytwoitten.ibaybay, PID: 16539
    java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=788413898, result=-1, data=Intent { clip={text/vnd.android.intent {...}} (has extras) }} to activity {com.twentytwoitten.ibaybay/modules.MainActivity}: java.lang.ClassCastException: android.text.SpannableString cannot be cast to java.lang.String
        at android.app.ActivityThread.deliverResults(ActivityThread.java:5051)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:5092)
        at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2073)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:246)
        at android.app.ActivityThread.main(ActivityThread.java:7690)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:593)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:995)
     Caused by: java.lang.ClassCastException: android.text.SpannableString cannot be cast to java.lang.String
        at com.twentytwoitten.ibaybay.modules.KeebKt$Keyboard$launcher$1$1.invoke(keeb.kt:47)
        at com.twentytwoitten.ibaybay.modules.KeebKt$Keyboard$launcher$1$1.invoke(keeb.kt:43)
        at androidx.activity.compose.ActivityResultRegistryKt$rememberLauncherForActivityResult$1$1.onActivityResult(ActivityResultRegistry.kt:105)
        at androidx.activity.result.ActivityResultRegistry.doDispatch(ActivityResultRegistry.java:392)
        at androidx.activity.result.ActivityResultRegistry.dispatchResult(ActivityResultRegistry.java:351)
        at androidx.activity.ComponentActivity.onActivityResult(ComponentActivity.java:638)
        at android.app.Activity.dispatchActivityResult(Activity.java:8364)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:5044)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:5092) 
        at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2073) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:246) 
        at android.app.ActivityThread.main(ActivityThread.java:7690) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:593) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:995) 
I/Process: Sending signal. PID: 16539 SIG: 9

I hope somebody can help me on this. I'm very close on to making this work. I'm also fairly new in using Jetpack Compose. I appreciate your help. Thanks.

0

There are 0 best solutions below