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