|
| 1 | +/* |
| 2 | + * Copyright 2024 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.android.horologist.composables |
| 18 | + |
| 19 | +import android.app.Activity.RESULT_OK |
| 20 | +import android.content.Context |
| 21 | +import android.content.Intent |
| 22 | +import androidx.activity.compose.rememberLauncherForActivityResult |
| 23 | +import androidx.activity.result.contract.ActivityResultContract |
| 24 | +import androidx.compose.foundation.clickable |
| 25 | +import androidx.compose.foundation.interaction.MutableInteractionSource |
| 26 | +import androidx.compose.foundation.text.BasicTextField |
| 27 | +import androidx.compose.foundation.text.KeyboardActions |
| 28 | +import androidx.compose.foundation.text.KeyboardOptions |
| 29 | +import androidx.compose.runtime.Composable |
| 30 | +import androidx.compose.ui.Modifier |
| 31 | +import androidx.compose.ui.graphics.Brush |
| 32 | +import androidx.compose.ui.graphics.Color |
| 33 | +import androidx.compose.ui.graphics.SolidColor |
| 34 | +import androidx.compose.ui.text.TextLayoutResult |
| 35 | +import androidx.compose.ui.text.TextStyle |
| 36 | +import androidx.compose.ui.text.input.TextFieldValue |
| 37 | +import androidx.compose.ui.text.input.VisualTransformation |
| 38 | + |
| 39 | +/** |
| 40 | + * This is a wrapper for [BasicTextField] that when clicked launches the keyboard. It cannot be |
| 41 | + * edited, but works as a helper to open the keyboard. |
| 42 | + */ |
| 43 | +@Composable |
| 44 | +public fun NonEditableBasicTextField( |
| 45 | + modifier: Modifier = Modifier, |
| 46 | + onTextChanged: (value: String) -> Unit, |
| 47 | + value: TextFieldValue, |
| 48 | + enabled: Boolean = true, |
| 49 | + readOnly: Boolean = false, |
| 50 | + textStyle: TextStyle = TextStyle.Default, |
| 51 | + keyboardOptions: KeyboardOptions = KeyboardOptions.Default, |
| 52 | + keyboardActions: KeyboardActions = KeyboardActions.Default, |
| 53 | + singleLine: Boolean = false, |
| 54 | + maxLines: Int = if (singleLine) 1 else Int.MAX_VALUE, |
| 55 | + minLines: Int = 1, |
| 56 | + visualTransformation: VisualTransformation = VisualTransformation.None, |
| 57 | + onTextLayout: (TextLayoutResult) -> Unit = {}, |
| 58 | + interactionSource: MutableInteractionSource? = null, |
| 59 | + cursorBrush: Brush = SolidColor(Color.Black), |
| 60 | + decorationBox: @Composable (innerTextField: @Composable () -> Unit) -> Unit = |
| 61 | + @Composable { innerTextField -> innerTextField() }, |
| 62 | +) { |
| 63 | + val keyboardLauncher = rememberLauncherForActivityResult( |
| 64 | + KeyBoardContract(), |
| 65 | + ) { |
| 66 | + if (it is KeyBoardContract.Result.TextChanged) { |
| 67 | + onTextChanged(it.text) |
| 68 | + } |
| 69 | + } |
| 70 | + val keyboardIntent = Intent("com.google.android.wearable.action.LAUNCH_KEYBOARD") |
| 71 | + |
| 72 | + BasicTextField( |
| 73 | + modifier = modifier.clickable { |
| 74 | + keyboardLauncher.launch(keyboardIntent) |
| 75 | + }, |
| 76 | + value = value, |
| 77 | + onValueChange = {}, |
| 78 | + enabled = enabled, |
| 79 | + readOnly = readOnly, |
| 80 | + textStyle = textStyle, |
| 81 | + keyboardOptions = keyboardOptions, |
| 82 | + keyboardActions = keyboardActions, |
| 83 | + singleLine = singleLine, |
| 84 | + maxLines = maxLines, |
| 85 | + minLines = minLines, |
| 86 | + visualTransformation = visualTransformation, |
| 87 | + onTextLayout = onTextLayout, |
| 88 | + interactionSource = interactionSource, |
| 89 | + cursorBrush = cursorBrush, |
| 90 | + decorationBox = decorationBox, |
| 91 | + ) |
| 92 | +} |
| 93 | + |
| 94 | +private class KeyBoardContract : ActivityResultContract<Intent, KeyBoardContract.Result>() { |
| 95 | + override fun createIntent(context: Context, input: Intent): Intent = input |
| 96 | + |
| 97 | + override fun parseResult(resultCode: Int, intent: Intent?): Result { |
| 98 | + println("resultCode: $resultCode") |
| 99 | + return if (resultCode == RESULT_OK) { |
| 100 | + val resultText = intent?.extras?.getString("result_text") |
| 101 | + if (!resultText.isNullOrBlank()) { |
| 102 | + Result.TextChanged(resultText) |
| 103 | + } else { |
| 104 | + Result.Empty |
| 105 | + } |
| 106 | + } else { |
| 107 | + Result.Empty |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + sealed class Result { |
| 112 | + data class TextChanged(val text: String) : Result() |
| 113 | + data object Empty : Result() |
| 114 | + } |
| 115 | +} |
0 commit comments