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 | dd60a5c | 2019-07-10 11:23:36 -0700 | [diff] [blame] | 25 | import androidx.ui.engine.geometry.Rect |
Seigo Nonaka | 615f32d | 2019-06-18 15:33:38 -0700 | [diff] [blame] | 26 | import androidx.ui.input.EditOperation |
Seigo Nonaka | 92bc31a | 2019-07-30 15:40:52 -0700 | [diff] [blame] | 27 | import androidx.ui.input.EditorModel |
Seigo Nonaka | c04f629d | 2019-07-11 13:20:30 -0700 | [diff] [blame] | 28 | import androidx.ui.input.ImeAction |
Seigo Nonaka | 615f32d | 2019-06-18 15:33:38 -0700 | [diff] [blame] | 29 | import androidx.ui.input.InputEventListener |
Seigo Nonaka | 6e6e2ee | 2019-07-01 17:18:41 -0700 | [diff] [blame] | 30 | import androidx.ui.input.KeyboardType |
Seigo Nonaka | 4b21159 | 2019-04-07 14:11:28 -0700 | [diff] [blame] | 31 | import androidx.ui.input.TextInputService |
Seigo Nonaka | dd60a5c | 2019-07-10 11:23:36 -0700 | [diff] [blame] | 32 | import androidx.ui.text.TextRange |
| 33 | import kotlin.math.roundToInt |
Seigo Nonaka | 4b21159 | 2019-04-07 14:11:28 -0700 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Provide Android specific input service with the Operating System. |
| 37 | */ |
| 38 | internal class TextInputServiceAndroid(val view: View) : TextInputService { |
| 39 | /** True if the currently editable widget has connected */ |
| 40 | private var editorHasFocus = false |
| 41 | |
| 42 | /** |
| 43 | * The following three observers are set when the editable widget has initiated the input |
| 44 | * session |
| 45 | */ |
Seigo Nonaka | 615f32d | 2019-06-18 15:33:38 -0700 | [diff] [blame] | 46 | private var onEditCommand: (List<EditOperation>) -> Unit = {} |
Seigo Nonaka | c04f629d | 2019-07-11 13:20:30 -0700 | [diff] [blame] | 47 | private var onImeActionPerformed: (ImeAction) -> Unit = {} |
Seigo Nonaka | 4b21159 | 2019-04-07 14:11:28 -0700 | [diff] [blame] | 48 | |
Seigo Nonaka | 4e45c46 | 2019-07-01 12:12:59 -0700 | [diff] [blame] | 49 | private var state = InputState(text = "", selection = TextRange(0, 0)) |
Seigo Nonaka | 6e6e2ee | 2019-07-01 17:18:41 -0700 | [diff] [blame] | 50 | private var keyboardType = KeyboardType.Text |
Seigo Nonaka | c04f629d | 2019-07-11 13:20:30 -0700 | [diff] [blame] | 51 | private var imeAction = ImeAction.Unspecified |
Seigo Nonaka | 4e45c46 | 2019-07-01 12:12:59 -0700 | [diff] [blame] | 52 | private var ic: RecordingInputConnection? = null |
| 53 | |
Seigo Nonaka | 4b21159 | 2019-04-07 14:11:28 -0700 | [diff] [blame] | 54 | /** |
| 55 | * The editable buffer used for BaseInputConnection. |
| 56 | */ |
Ralston Da Silva | dff6b62 | 2019-08-07 15:29:06 -0700 | [diff] [blame^] | 57 | private lateinit var imm: InputMethodManager |
Seigo Nonaka | 4b21159 | 2019-04-07 14:11:28 -0700 | [diff] [blame] | 58 | |
| 59 | /** |
| 60 | * Creates new input connection. |
| 61 | */ |
| 62 | fun createInputConnection(outAttrs: EditorInfo): InputConnection? { |
| 63 | if (!editorHasFocus) { |
| 64 | return null |
| 65 | } |
Seigo Nonaka | c04f629d | 2019-07-11 13:20:30 -0700 | [diff] [blame] | 66 | fillEditorInfo(keyboardType, imeAction, outAttrs) |
Seigo Nonaka | 4b21159 | 2019-04-07 14:11:28 -0700 | [diff] [blame] | 67 | |
Seigo Nonaka | 4e45c46 | 2019-07-01 12:12:59 -0700 | [diff] [blame] | 68 | return RecordingInputConnection( |
| 69 | initState = state, |
| 70 | eventListener = object : InputEventListener { |
| 71 | override fun onEditOperations(editOps: List<EditOperation>) { |
| 72 | onEditCommand(editOps) |
| 73 | } |
Seigo Nonaka | c04f629d | 2019-07-11 13:20:30 -0700 | [diff] [blame] | 74 | |
| 75 | override fun onImeAction(imeAction: ImeAction) { |
| 76 | onImeActionPerformed(imeAction) |
| 77 | } |
Seigo Nonaka | 615f32d | 2019-06-18 15:33:38 -0700 | [diff] [blame] | 78 | } |
Seigo Nonaka | 4e45c46 | 2019-07-01 12:12:59 -0700 | [diff] [blame] | 79 | ).also { ic = it } |
Seigo Nonaka | 4b21159 | 2019-04-07 14:11:28 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Returns true if some editable component is focused. |
| 84 | */ |
| 85 | fun isEditorFocused(): Boolean = editorHasFocus |
| 86 | |
| 87 | override fun startInput( |
Seigo Nonaka | 92bc31a | 2019-07-30 15:40:52 -0700 | [diff] [blame] | 88 | initModel: EditorModel, |
Seigo Nonaka | 6e6e2ee | 2019-07-01 17:18:41 -0700 | [diff] [blame] | 89 | keyboardType: KeyboardType, |
Seigo Nonaka | c04f629d | 2019-07-11 13:20:30 -0700 | [diff] [blame] | 90 | imeAction: ImeAction, |
Seigo Nonaka | 615f32d | 2019-06-18 15:33:38 -0700 | [diff] [blame] | 91 | onEditCommand: (List<EditOperation>) -> Unit, |
Seigo Nonaka | c04f629d | 2019-07-11 13:20:30 -0700 | [diff] [blame] | 92 | onImeActionPerformed: (ImeAction) -> Unit |
Seigo Nonaka | 4b21159 | 2019-04-07 14:11:28 -0700 | [diff] [blame] | 93 | ) { |
Ralston Da Silva | dff6b62 | 2019-08-07 15:29:06 -0700 | [diff] [blame^] | 94 | imm = view.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager |
Seigo Nonaka | 4b21159 | 2019-04-07 14:11:28 -0700 | [diff] [blame] | 95 | editorHasFocus = true |
Seigo Nonaka | 92bc31a | 2019-07-30 15:40:52 -0700 | [diff] [blame] | 96 | state = initModel.toInputState() |
Seigo Nonaka | 6e6e2ee | 2019-07-01 17:18:41 -0700 | [diff] [blame] | 97 | this.keyboardType = keyboardType |
Seigo Nonaka | c04f629d | 2019-07-11 13:20:30 -0700 | [diff] [blame] | 98 | this.imeAction = imeAction |
Seigo Nonaka | 615f32d | 2019-06-18 15:33:38 -0700 | [diff] [blame] | 99 | this.onEditCommand = onEditCommand |
Seigo Nonaka | c04f629d | 2019-07-11 13:20:30 -0700 | [diff] [blame] | 100 | this.onImeActionPerformed = onImeActionPerformed |
Seigo Nonaka | 4b21159 | 2019-04-07 14:11:28 -0700 | [diff] [blame] | 101 | |
| 102 | view.requestFocus() |
| 103 | view.post { |
| 104 | imm.restartInput(view) |
| 105 | imm.showSoftInput(view, 0) |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | override fun stopInput() { |
| 110 | editorHasFocus = false |
Seigo Nonaka | 615f32d | 2019-06-18 15:33:38 -0700 | [diff] [blame] | 111 | onEditCommand = {} |
Seigo Nonaka | c04f629d | 2019-07-11 13:20:30 -0700 | [diff] [blame] | 112 | onImeActionPerformed = {} |
Seigo Nonaka | 4b21159 | 2019-04-07 14:11:28 -0700 | [diff] [blame] | 113 | |
| 114 | imm.restartInput(view) |
| 115 | } |
Seigo Nonaka | 97c9f0b | 2019-07-01 14:22:07 -0700 | [diff] [blame] | 116 | |
| 117 | override fun showSoftwareKeyboard() { |
| 118 | imm.showSoftInput(view, 0) |
| 119 | } |
Seigo Nonaka | 4e45c46 | 2019-07-01 12:12:59 -0700 | [diff] [blame] | 120 | |
Seigo Nonaka | 92bc31a | 2019-07-30 15:40:52 -0700 | [diff] [blame] | 121 | override fun onStateUpdated(model: EditorModel) { |
| 122 | this.state = model.toInputState() |
Seigo Nonaka | 4e45c46 | 2019-07-01 12:12:59 -0700 | [diff] [blame] | 123 | ic?.updateInputState(this.state, imm, view) |
| 124 | } |
Seigo Nonaka | 6e6e2ee | 2019-07-01 17:18:41 -0700 | [diff] [blame] | 125 | |
Seigo Nonaka | dd60a5c | 2019-07-10 11:23:36 -0700 | [diff] [blame] | 126 | override fun notifyFocusedRect(rect: Rect) { |
| 127 | view.requestRectangleOnScreen(android.graphics.Rect( |
| 128 | rect.left.roundToInt(), |
| 129 | rect.top.roundToInt(), |
| 130 | rect.right.roundToInt(), |
| 131 | rect.bottom.roundToInt())) |
| 132 | } |
| 133 | |
Seigo Nonaka | 6e6e2ee | 2019-07-01 17:18:41 -0700 | [diff] [blame] | 134 | /** |
| 135 | * Fills necessary info of EditorInfo. |
| 136 | */ |
Seigo Nonaka | c04f629d | 2019-07-11 13:20:30 -0700 | [diff] [blame] | 137 | private fun fillEditorInfo( |
| 138 | keyboardType: KeyboardType, |
| 139 | imeAction: ImeAction, |
| 140 | outInfo: EditorInfo |
| 141 | ) { |
| 142 | outInfo.imeOptions = when (imeAction) { |
| 143 | ImeAction.Unspecified -> EditorInfo.IME_ACTION_UNSPECIFIED |
| 144 | ImeAction.NoAction -> EditorInfo.IME_ACTION_NONE |
| 145 | ImeAction.Go -> EditorInfo.IME_ACTION_GO |
| 146 | ImeAction.Next -> EditorInfo.IME_ACTION_NEXT |
| 147 | ImeAction.Previous -> EditorInfo.IME_ACTION_PREVIOUS |
| 148 | ImeAction.Search -> EditorInfo.IME_ACTION_SEARCH |
| 149 | ImeAction.Send -> EditorInfo.IME_ACTION_SEND |
| 150 | ImeAction.Done -> EditorInfo.IME_ACTION_DONE |
| 151 | else -> throw IllegalArgumentException("Unknown ImeAction: $imeAction") |
| 152 | } |
Seigo Nonaka | 6e6e2ee | 2019-07-01 17:18:41 -0700 | [diff] [blame] | 153 | when (keyboardType) { |
| 154 | KeyboardType.Text -> outInfo.inputType = InputType.TYPE_CLASS_TEXT |
Siyamed Sinir | 3a952ea | 2019-07-08 16:50:50 -0700 | [diff] [blame] | 155 | KeyboardType.Ascii -> { |
Seigo Nonaka | 6e6e2ee | 2019-07-01 17:18:41 -0700 | [diff] [blame] | 156 | outInfo.inputType = InputType.TYPE_CLASS_TEXT |
Seigo Nonaka | c04f629d | 2019-07-11 13:20:30 -0700 | [diff] [blame] | 157 | outInfo.imeOptions = outInfo.imeOptions or EditorInfo.IME_FLAG_FORCE_ASCII |
Seigo Nonaka | 6e6e2ee | 2019-07-01 17:18:41 -0700 | [diff] [blame] | 158 | } |
| 159 | KeyboardType.Number -> outInfo.inputType = InputType.TYPE_CLASS_NUMBER |
| 160 | KeyboardType.Phone -> outInfo.inputType = InputType.TYPE_CLASS_PHONE |
Siyamed Sinir | 3a952ea | 2019-07-08 16:50:50 -0700 | [diff] [blame] | 161 | KeyboardType.Uri -> |
Seigo Nonaka | 6e6e2ee | 2019-07-01 17:18:41 -0700 | [diff] [blame] | 162 | outInfo.inputType = InputType.TYPE_CLASS_TEXT or EditorInfo.TYPE_TEXT_VARIATION_URI |
| 163 | KeyboardType.Email -> |
| 164 | outInfo.inputType = |
| 165 | InputType.TYPE_CLASS_TEXT or EditorInfo.TYPE_TEXT_VARIATION_EMAIL_ADDRESS |
Seigo Nonaka | 813dc15 | 2019-07-15 17:15:05 -0700 | [diff] [blame] | 166 | KeyboardType.Password -> { |
| 167 | outInfo.inputType = |
| 168 | InputType.TYPE_CLASS_TEXT or EditorInfo.TYPE_TEXT_VARIATION_PASSWORD |
| 169 | } |
| 170 | KeyboardType.NumberPassword -> { |
| 171 | outInfo.inputType = |
| 172 | InputType.TYPE_CLASS_NUMBER or EditorInfo.TYPE_NUMBER_VARIATION_PASSWORD |
| 173 | } |
Seigo Nonaka | 6e6e2ee | 2019-07-01 17:18:41 -0700 | [diff] [blame] | 174 | else -> throw IllegalArgumentException("Unknown KeyboardType: $keyboardType") |
| 175 | } |
Seigo Nonaka | c04f629d | 2019-07-11 13:20:30 -0700 | [diff] [blame] | 176 | outInfo.imeOptions = |
| 177 | outInfo.imeOptions or outInfo.imeOptions or EditorInfo.IME_FLAG_NO_FULLSCREEN |
Seigo Nonaka | 6e6e2ee | 2019-07-01 17:18:41 -0700 | [diff] [blame] | 178 | } |
Seigo Nonaka | 4e45c46 | 2019-07-01 12:12:59 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Seigo Nonaka | 92bc31a | 2019-07-30 15:40:52 -0700 | [diff] [blame] | 181 | private fun EditorModel.toInputState(): InputState = |
Seigo Nonaka | 4e45c46 | 2019-07-01 12:12:59 -0700 | [diff] [blame] | 182 | InputState( |
| 183 | text = text, // TODO(nona): call toString once AnnotatedString is in use. |
| 184 | selection = selection, |
| 185 | composition = composition) |