Seigo Nonaka | 4b21159 | 2019-04-07 14:11:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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 | * http://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 androidx.ui.core.input |
| 18 | |
| 19 | import android.content.Context |
Seigo Nonaka | 4b21159 | 2019-04-07 14:11:28 -0700 | [diff] [blame] | 20 | import android.text.InputType |
| 21 | import android.view.View |
| 22 | import android.view.inputmethod.EditorInfo |
| 23 | import android.view.inputmethod.InputConnection |
| 24 | import android.view.inputmethod.InputMethodManager |
Seigo Nonaka | 4e45c46 | 2019-07-01 12:12:59 -0700 | [diff] [blame] | 25 | import androidx.ui.core.TextRange |
Seigo Nonaka | 615f32d | 2019-06-18 15:33:38 -0700 | [diff] [blame] | 26 | import androidx.ui.input.EditOperation |
Seigo Nonaka | 4b21159 | 2019-04-07 14:11:28 -0700 | [diff] [blame] | 27 | import androidx.ui.input.EditorState |
Seigo Nonaka | 615f32d | 2019-06-18 15:33:38 -0700 | [diff] [blame] | 28 | import androidx.ui.input.InputEventListener |
Seigo Nonaka | 6e6e2ee | 2019-07-01 17:18:41 -0700 | [diff] [blame^] | 29 | import androidx.ui.input.KeyboardType |
Seigo Nonaka | 4b21159 | 2019-04-07 14:11:28 -0700 | [diff] [blame] | 30 | import androidx.ui.input.TextInputService |
| 31 | |
| 32 | /** |
| 33 | * Provide Android specific input service with the Operating System. |
| 34 | */ |
| 35 | internal class TextInputServiceAndroid(val view: View) : TextInputService { |
| 36 | /** True if the currently editable widget has connected */ |
| 37 | private var editorHasFocus = false |
| 38 | |
| 39 | /** |
| 40 | * The following three observers are set when the editable widget has initiated the input |
| 41 | * session |
| 42 | */ |
Seigo Nonaka | 615f32d | 2019-06-18 15:33:38 -0700 | [diff] [blame] | 43 | private var onEditCommand: (List<EditOperation>) -> Unit = {} |
Seigo Nonaka | 4b21159 | 2019-04-07 14:11:28 -0700 | [diff] [blame] | 44 | private var onEditorActionPerformed: (Any) -> Unit = {} |
Seigo Nonaka | 4b21159 | 2019-04-07 14:11:28 -0700 | [diff] [blame] | 45 | |
Seigo Nonaka | 4e45c46 | 2019-07-01 12:12:59 -0700 | [diff] [blame] | 46 | private var state = InputState(text = "", selection = TextRange(0, 0)) |
Seigo Nonaka | 6e6e2ee | 2019-07-01 17:18:41 -0700 | [diff] [blame^] | 47 | private var keyboardType = KeyboardType.Text |
Seigo Nonaka | 4e45c46 | 2019-07-01 12:12:59 -0700 | [diff] [blame] | 48 | private var ic: RecordingInputConnection? = null |
| 49 | |
Seigo Nonaka | 4b21159 | 2019-04-07 14:11:28 -0700 | [diff] [blame] | 50 | /** |
| 51 | * The editable buffer used for BaseInputConnection. |
| 52 | */ |
Seigo Nonaka | 4b21159 | 2019-04-07 14:11:28 -0700 | [diff] [blame] | 53 | private val imm = |
| 54 | view.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager |
| 55 | |
| 56 | /** |
| 57 | * Creates new input connection. |
| 58 | */ |
| 59 | fun createInputConnection(outAttrs: EditorInfo): InputConnection? { |
| 60 | if (!editorHasFocus) { |
| 61 | return null |
| 62 | } |
Seigo Nonaka | 6e6e2ee | 2019-07-01 17:18:41 -0700 | [diff] [blame^] | 63 | fillEditorInfo(keyboardType, outAttrs) |
Seigo Nonaka | 4b21159 | 2019-04-07 14:11:28 -0700 | [diff] [blame] | 64 | |
Seigo Nonaka | 4e45c46 | 2019-07-01 12:12:59 -0700 | [diff] [blame] | 65 | return RecordingInputConnection( |
| 66 | initState = state, |
| 67 | eventListener = object : InputEventListener { |
| 68 | override fun onEditOperations(editOps: List<EditOperation>) { |
| 69 | onEditCommand(editOps) |
| 70 | } |
Seigo Nonaka | 615f32d | 2019-06-18 15:33:38 -0700 | [diff] [blame] | 71 | } |
Seigo Nonaka | 4e45c46 | 2019-07-01 12:12:59 -0700 | [diff] [blame] | 72 | ).also { ic = it } |
Seigo Nonaka | 4b21159 | 2019-04-07 14:11:28 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Returns true if some editable component is focused. |
| 77 | */ |
| 78 | fun isEditorFocused(): Boolean = editorHasFocus |
| 79 | |
| 80 | override fun startInput( |
| 81 | initState: EditorState, |
Seigo Nonaka | 6e6e2ee | 2019-07-01 17:18:41 -0700 | [diff] [blame^] | 82 | keyboardType: KeyboardType, |
Seigo Nonaka | 615f32d | 2019-06-18 15:33:38 -0700 | [diff] [blame] | 83 | onEditCommand: (List<EditOperation>) -> Unit, |
Seigo Nonaka | 5eb38c0 | 2019-06-27 18:11:59 -0700 | [diff] [blame] | 84 | onEditorActionPerformed: (Any) -> Unit |
Seigo Nonaka | 4b21159 | 2019-04-07 14:11:28 -0700 | [diff] [blame] | 85 | ) { |
| 86 | editorHasFocus = true |
Seigo Nonaka | 4e45c46 | 2019-07-01 12:12:59 -0700 | [diff] [blame] | 87 | state = initState.toInputState() |
Seigo Nonaka | 6e6e2ee | 2019-07-01 17:18:41 -0700 | [diff] [blame^] | 88 | this.keyboardType = keyboardType |
Seigo Nonaka | 615f32d | 2019-06-18 15:33:38 -0700 | [diff] [blame] | 89 | this.onEditCommand = onEditCommand |
Seigo Nonaka | 4b21159 | 2019-04-07 14:11:28 -0700 | [diff] [blame] | 90 | this.onEditorActionPerformed = onEditorActionPerformed |
Seigo Nonaka | 4b21159 | 2019-04-07 14:11:28 -0700 | [diff] [blame] | 91 | |
| 92 | view.requestFocus() |
| 93 | view.post { |
| 94 | imm.restartInput(view) |
| 95 | imm.showSoftInput(view, 0) |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | override fun stopInput() { |
| 100 | editorHasFocus = false |
Seigo Nonaka | 615f32d | 2019-06-18 15:33:38 -0700 | [diff] [blame] | 101 | onEditCommand = {} |
Seigo Nonaka | 4b21159 | 2019-04-07 14:11:28 -0700 | [diff] [blame] | 102 | onEditorActionPerformed = {} |
Seigo Nonaka | 4b21159 | 2019-04-07 14:11:28 -0700 | [diff] [blame] | 103 | |
| 104 | imm.restartInput(view) |
| 105 | } |
Seigo Nonaka | 97c9f0b | 2019-07-01 14:22:07 -0700 | [diff] [blame] | 106 | |
| 107 | override fun showSoftwareKeyboard() { |
| 108 | imm.showSoftInput(view, 0) |
| 109 | } |
Seigo Nonaka | 4e45c46 | 2019-07-01 12:12:59 -0700 | [diff] [blame] | 110 | |
| 111 | override fun onStateUpdated(state: EditorState) { |
| 112 | this.state = state.toInputState() |
| 113 | ic?.updateInputState(this.state, imm, view) |
| 114 | } |
Seigo Nonaka | 6e6e2ee | 2019-07-01 17:18:41 -0700 | [diff] [blame^] | 115 | |
| 116 | /** |
| 117 | * Fills necessary info of EditorInfo. |
| 118 | */ |
| 119 | private fun fillEditorInfo(keyboardType: KeyboardType, outInfo: EditorInfo) { |
| 120 | when (keyboardType) { |
| 121 | KeyboardType.Text -> outInfo.inputType = InputType.TYPE_CLASS_TEXT |
| 122 | KeyboardType.ASCII -> { |
| 123 | outInfo.inputType = InputType.TYPE_CLASS_TEXT |
| 124 | outInfo.imeOptions = EditorInfo.IME_FLAG_FORCE_ASCII |
| 125 | } |
| 126 | KeyboardType.Number -> outInfo.inputType = InputType.TYPE_CLASS_NUMBER |
| 127 | KeyboardType.Phone -> outInfo.inputType = InputType.TYPE_CLASS_PHONE |
| 128 | KeyboardType.URI -> |
| 129 | outInfo.inputType = InputType.TYPE_CLASS_TEXT or EditorInfo.TYPE_TEXT_VARIATION_URI |
| 130 | KeyboardType.Email -> |
| 131 | outInfo.inputType = |
| 132 | InputType.TYPE_CLASS_TEXT or EditorInfo.TYPE_TEXT_VARIATION_EMAIL_ADDRESS |
| 133 | else -> throw IllegalArgumentException("Unknown KeyboardType: $keyboardType") |
| 134 | } |
| 135 | outInfo.imeOptions = outInfo.imeOptions or EditorInfo.IME_FLAG_NO_FULLSCREEN |
| 136 | } |
Seigo Nonaka | 4e45c46 | 2019-07-01 12:12:59 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | private fun EditorState.toInputState(): InputState = |
| 140 | InputState( |
| 141 | text = text, // TODO(nona): call toString once AnnotatedString is in use. |
| 142 | selection = selection, |
| 143 | composition = composition) |