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 | |
Louis Pullen-Freilich | ab19475 | 2020-07-21 22:21:22 +0100 | [diff] [blame] | 17 | package androidx.compose.ui.text.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 |
Seigo Nonaka | 9a92fd9 | 2020-06-19 15:58:02 -0700 | [diff] [blame] | 22 | import android.view.ViewTreeObserver |
Seigo Nonaka | 4b21159 | 2019-04-07 14:11:28 -0700 | [diff] [blame] | 23 | import android.view.inputmethod.EditorInfo |
| 24 | import android.view.inputmethod.InputConnection |
| 25 | import android.view.inputmethod.InputMethodManager |
George Mount | 842c8c1 | 2020-01-08 16:03:42 -0800 | [diff] [blame] | 26 | import androidx.ui.geometry.Rect |
Louis Pullen-Freilich | ab19475 | 2020-07-21 22:21:22 +0100 | [diff] [blame] | 27 | import androidx.compose.ui.text.TextRange |
Seigo Nonaka | dd60a5c | 2019-07-10 11:23:36 -0700 | [diff] [blame] | 28 | import kotlin.math.roundToInt |
Seigo Nonaka | 4b21159 | 2019-04-07 14:11:28 -0700 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Provide Android specific input service with the Operating System. |
| 32 | */ |
Siyamed Sinir | 9da613b | 2019-11-15 20:39:19 -0800 | [diff] [blame] | 33 | internal class TextInputServiceAndroid(val view: View) : PlatformTextInputService { |
Louis Pullen-Freilich | 5da28bd | 2019-10-15 17:05:07 +0100 | [diff] [blame] | 34 | /** True if the currently editable composable has connected */ |
Seigo Nonaka | 4b21159 | 2019-04-07 14:11:28 -0700 | [diff] [blame] | 35 | private var editorHasFocus = false |
| 36 | |
| 37 | /** |
Louis Pullen-Freilich | 5da28bd | 2019-10-15 17:05:07 +0100 | [diff] [blame] | 38 | * 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] | 39 | * session |
| 40 | */ |
Seigo Nonaka | 615f32d | 2019-06-18 15:33:38 -0700 | [diff] [blame] | 41 | private var onEditCommand: (List<EditOperation>) -> Unit = {} |
Seigo Nonaka | c04f629d | 2019-07-11 13:20:30 -0700 | [diff] [blame] | 42 | private var onImeActionPerformed: (ImeAction) -> Unit = {} |
Seigo Nonaka | 4b21159 | 2019-04-07 14:11:28 -0700 | [diff] [blame] | 43 | |
Siyamed Sinir | 01b35f7 | 2020-06-15 19:15:32 -0700 | [diff] [blame] | 44 | private var state = TextFieldValue(text = "", selection = TextRange.Zero) |
Seigo Nonaka | 6e6e2ee | 2019-07-01 17:18:41 -0700 | [diff] [blame] | 45 | private var keyboardType = KeyboardType.Text |
Seigo Nonaka | c04f629d | 2019-07-11 13:20:30 -0700 | [diff] [blame] | 46 | private var imeAction = ImeAction.Unspecified |
Seigo Nonaka | 4e45c46 | 2019-07-01 12:12:59 -0700 | [diff] [blame] | 47 | private var ic: RecordingInputConnection? = null |
| 48 | |
Seigo Nonaka | 9a92fd9 | 2020-06-19 15:58:02 -0700 | [diff] [blame] | 49 | private var focusedRect: android.graphics.Rect? = null |
| 50 | |
Seigo Nonaka | 4b21159 | 2019-04-07 14:11:28 -0700 | [diff] [blame] | 51 | /** |
| 52 | * The editable buffer used for BaseInputConnection. |
| 53 | */ |
Ralston Da Silva | dff6b62 | 2019-08-07 15:29:06 -0700 | [diff] [blame] | 54 | private lateinit var imm: InputMethodManager |
Seigo Nonaka | 4b21159 | 2019-04-07 14:11:28 -0700 | [diff] [blame] | 55 | |
Seigo Nonaka | 9a92fd9 | 2020-06-19 15:58:02 -0700 | [diff] [blame] | 56 | private val layoutListener = ViewTreeObserver.OnGlobalLayoutListener { |
| 57 | // focusedRect is null if there is not ongoing text input session. So safe to request |
| 58 | // latest focused rectangle whenever global layout has changed. |
| 59 | focusedRect?.let { view.requestRectangleOnScreen(it) } |
| 60 | } |
| 61 | |
| 62 | init { |
| 63 | view.addOnAttachStateChangeListener(object : View.OnAttachStateChangeListener { |
| 64 | override fun onViewDetachedFromWindow(v: View?) { |
| 65 | v?.rootView?.viewTreeObserver?.removeOnGlobalLayoutListener(layoutListener) |
| 66 | } |
| 67 | |
| 68 | override fun onViewAttachedToWindow(v: View?) { |
| 69 | v?.rootView?.viewTreeObserver?.addOnGlobalLayoutListener(layoutListener) |
| 70 | } |
| 71 | }) |
| 72 | } |
| 73 | |
Seigo Nonaka | 4b21159 | 2019-04-07 14:11:28 -0700 | [diff] [blame] | 74 | /** |
| 75 | * Creates new input connection. |
| 76 | */ |
| 77 | fun createInputConnection(outAttrs: EditorInfo): InputConnection? { |
| 78 | if (!editorHasFocus) { |
| 79 | return null |
| 80 | } |
Seigo Nonaka | c04f629d | 2019-07-11 13:20:30 -0700 | [diff] [blame] | 81 | fillEditorInfo(keyboardType, imeAction, outAttrs) |
Seigo Nonaka | 4b21159 | 2019-04-07 14:11:28 -0700 | [diff] [blame] | 82 | |
Seigo Nonaka | 4e45c46 | 2019-07-01 12:12:59 -0700 | [diff] [blame] | 83 | return RecordingInputConnection( |
| 84 | initState = state, |
| 85 | eventListener = object : InputEventListener { |
| 86 | override fun onEditOperations(editOps: List<EditOperation>) { |
| 87 | onEditCommand(editOps) |
| 88 | } |
Seigo Nonaka | c04f629d | 2019-07-11 13:20:30 -0700 | [diff] [blame] | 89 | |
| 90 | override fun onImeAction(imeAction: ImeAction) { |
| 91 | onImeActionPerformed(imeAction) |
| 92 | } |
Seigo Nonaka | 615f32d | 2019-06-18 15:33:38 -0700 | [diff] [blame] | 93 | } |
Seigo Nonaka | 4e45c46 | 2019-07-01 12:12:59 -0700 | [diff] [blame] | 94 | ).also { ic = it } |
Seigo Nonaka | 4b21159 | 2019-04-07 14:11:28 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Returns true if some editable component is focused. |
| 99 | */ |
| 100 | fun isEditorFocused(): Boolean = editorHasFocus |
| 101 | |
| 102 | override fun startInput( |
Siyamed Sinir | c2e5559 | 2020-06-09 09:37:23 -0700 | [diff] [blame] | 103 | value: TextFieldValue, |
Seigo Nonaka | 6e6e2ee | 2019-07-01 17:18:41 -0700 | [diff] [blame] | 104 | keyboardType: KeyboardType, |
Seigo Nonaka | c04f629d | 2019-07-11 13:20:30 -0700 | [diff] [blame] | 105 | imeAction: ImeAction, |
Seigo Nonaka | 615f32d | 2019-06-18 15:33:38 -0700 | [diff] [blame] | 106 | onEditCommand: (List<EditOperation>) -> Unit, |
Seigo Nonaka | c04f629d | 2019-07-11 13:20:30 -0700 | [diff] [blame] | 107 | onImeActionPerformed: (ImeAction) -> Unit |
Seigo Nonaka | 4b21159 | 2019-04-07 14:11:28 -0700 | [diff] [blame] | 108 | ) { |
Ralston Da Silva | dff6b62 | 2019-08-07 15:29:06 -0700 | [diff] [blame] | 109 | imm = view.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager |
Seigo Nonaka | 4b21159 | 2019-04-07 14:11:28 -0700 | [diff] [blame] | 110 | editorHasFocus = true |
Siyamed Sinir | c2e5559 | 2020-06-09 09:37:23 -0700 | [diff] [blame] | 111 | state = value |
Seigo Nonaka | 6e6e2ee | 2019-07-01 17:18:41 -0700 | [diff] [blame] | 112 | this.keyboardType = keyboardType |
Seigo Nonaka | c04f629d | 2019-07-11 13:20:30 -0700 | [diff] [blame] | 113 | this.imeAction = imeAction |
Seigo Nonaka | 615f32d | 2019-06-18 15:33:38 -0700 | [diff] [blame] | 114 | this.onEditCommand = onEditCommand |
Seigo Nonaka | c04f629d | 2019-07-11 13:20:30 -0700 | [diff] [blame] | 115 | this.onImeActionPerformed = onImeActionPerformed |
Seigo Nonaka | 4b21159 | 2019-04-07 14:11:28 -0700 | [diff] [blame] | 116 | |
| 117 | view.requestFocus() |
| 118 | view.post { |
| 119 | imm.restartInput(view) |
| 120 | imm.showSoftInput(view, 0) |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | override fun stopInput() { |
| 125 | editorHasFocus = false |
Seigo Nonaka | 615f32d | 2019-06-18 15:33:38 -0700 | [diff] [blame] | 126 | onEditCommand = {} |
Seigo Nonaka | c04f629d | 2019-07-11 13:20:30 -0700 | [diff] [blame] | 127 | onImeActionPerformed = {} |
Seigo Nonaka | 9a92fd9 | 2020-06-19 15:58:02 -0700 | [diff] [blame] | 128 | focusedRect = null |
Seigo Nonaka | 4b21159 | 2019-04-07 14:11:28 -0700 | [diff] [blame] | 129 | |
| 130 | imm.restartInput(view) |
Seigo Nonaka | 6425fde | 2020-02-27 16:20:33 -0800 | [diff] [blame] | 131 | editorHasFocus = false |
Seigo Nonaka | 4b21159 | 2019-04-07 14:11:28 -0700 | [diff] [blame] | 132 | } |
Seigo Nonaka | 97c9f0b | 2019-07-01 14:22:07 -0700 | [diff] [blame] | 133 | |
| 134 | override fun showSoftwareKeyboard() { |
| 135 | imm.showSoftInput(view, 0) |
| 136 | } |
Seigo Nonaka | 4e45c46 | 2019-07-01 12:12:59 -0700 | [diff] [blame] | 137 | |
Seigo Nonaka | 6425fde | 2020-02-27 16:20:33 -0800 | [diff] [blame] | 138 | override fun hideSoftwareKeyboard() { |
| 139 | imm.hideSoftInputFromWindow(view.windowToken, 0) |
| 140 | } |
| 141 | |
Siyamed Sinir | c2e5559 | 2020-06-09 09:37:23 -0700 | [diff] [blame] | 142 | override fun onStateUpdated(value: TextFieldValue) { |
| 143 | this.state = value |
Seigo Nonaka | 4e45c46 | 2019-07-01 12:12:59 -0700 | [diff] [blame] | 144 | ic?.updateInputState(this.state, imm, view) |
| 145 | } |
Seigo Nonaka | 6e6e2ee | 2019-07-01 17:18:41 -0700 | [diff] [blame] | 146 | |
Seigo Nonaka | dd60a5c | 2019-07-10 11:23:36 -0700 | [diff] [blame] | 147 | override fun notifyFocusedRect(rect: Rect) { |
Seigo Nonaka | 9a92fd9 | 2020-06-19 15:58:02 -0700 | [diff] [blame] | 148 | focusedRect = android.graphics.Rect( |
Seigo Nonaka | dd60a5c | 2019-07-10 11:23:36 -0700 | [diff] [blame] | 149 | rect.left.roundToInt(), |
| 150 | rect.top.roundToInt(), |
| 151 | rect.right.roundToInt(), |
Seigo Nonaka | 9a92fd9 | 2020-06-19 15:58:02 -0700 | [diff] [blame] | 152 | rect.bottom.roundToInt() |
| 153 | ) |
| 154 | |
| 155 | // Requesting rectangle too early after obtaining focus may bring view into wrong place |
| 156 | // probably due to transient IME inset change. We don't know the correct timing of calling |
| 157 | // requestRectangleOnScreen API, so try to call this API only after the IME is ready to |
| 158 | // use, i.e. InputConnection has created. |
| 159 | // Even if we miss all the timing of requesting rectangle during initial text field focus, |
| 160 | // focused rectangle will be requested when software keyboard has shown. |
| 161 | if (ic == null) { |
| 162 | view.requestRectangleOnScreen(focusedRect) |
| 163 | } |
Seigo Nonaka | dd60a5c | 2019-07-10 11:23:36 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Seigo Nonaka | 6e6e2ee | 2019-07-01 17:18:41 -0700 | [diff] [blame] | 166 | /** |
| 167 | * Fills necessary info of EditorInfo. |
| 168 | */ |
Seigo Nonaka | c04f629d | 2019-07-11 13:20:30 -0700 | [diff] [blame] | 169 | private fun fillEditorInfo( |
| 170 | keyboardType: KeyboardType, |
| 171 | imeAction: ImeAction, |
| 172 | outInfo: EditorInfo |
| 173 | ) { |
| 174 | outInfo.imeOptions = when (imeAction) { |
| 175 | ImeAction.Unspecified -> EditorInfo.IME_ACTION_UNSPECIFIED |
| 176 | ImeAction.NoAction -> EditorInfo.IME_ACTION_NONE |
| 177 | ImeAction.Go -> EditorInfo.IME_ACTION_GO |
| 178 | ImeAction.Next -> EditorInfo.IME_ACTION_NEXT |
| 179 | ImeAction.Previous -> EditorInfo.IME_ACTION_PREVIOUS |
| 180 | ImeAction.Search -> EditorInfo.IME_ACTION_SEARCH |
| 181 | ImeAction.Send -> EditorInfo.IME_ACTION_SEND |
| 182 | ImeAction.Done -> EditorInfo.IME_ACTION_DONE |
| 183 | else -> throw IllegalArgumentException("Unknown ImeAction: $imeAction") |
| 184 | } |
Seigo Nonaka | 6e6e2ee | 2019-07-01 17:18:41 -0700 | [diff] [blame] | 185 | when (keyboardType) { |
| 186 | KeyboardType.Text -> outInfo.inputType = InputType.TYPE_CLASS_TEXT |
Siyamed Sinir | 3a952ea | 2019-07-08 16:50:50 -0700 | [diff] [blame] | 187 | KeyboardType.Ascii -> { |
Seigo Nonaka | 6e6e2ee | 2019-07-01 17:18:41 -0700 | [diff] [blame] | 188 | outInfo.inputType = InputType.TYPE_CLASS_TEXT |
Seigo Nonaka | c04f629d | 2019-07-11 13:20:30 -0700 | [diff] [blame] | 189 | outInfo.imeOptions = outInfo.imeOptions or EditorInfo.IME_FLAG_FORCE_ASCII |
Seigo Nonaka | 6e6e2ee | 2019-07-01 17:18:41 -0700 | [diff] [blame] | 190 | } |
| 191 | KeyboardType.Number -> outInfo.inputType = InputType.TYPE_CLASS_NUMBER |
| 192 | KeyboardType.Phone -> outInfo.inputType = InputType.TYPE_CLASS_PHONE |
Siyamed Sinir | 3a952ea | 2019-07-08 16:50:50 -0700 | [diff] [blame] | 193 | KeyboardType.Uri -> |
Seigo Nonaka | 6e6e2ee | 2019-07-01 17:18:41 -0700 | [diff] [blame] | 194 | outInfo.inputType = InputType.TYPE_CLASS_TEXT or EditorInfo.TYPE_TEXT_VARIATION_URI |
| 195 | KeyboardType.Email -> |
| 196 | outInfo.inputType = |
| 197 | InputType.TYPE_CLASS_TEXT or EditorInfo.TYPE_TEXT_VARIATION_EMAIL_ADDRESS |
Seigo Nonaka | 813dc15 | 2019-07-15 17:15:05 -0700 | [diff] [blame] | 198 | KeyboardType.Password -> { |
| 199 | outInfo.inputType = |
| 200 | InputType.TYPE_CLASS_TEXT or EditorInfo.TYPE_TEXT_VARIATION_PASSWORD |
| 201 | } |
| 202 | KeyboardType.NumberPassword -> { |
| 203 | outInfo.inputType = |
| 204 | InputType.TYPE_CLASS_NUMBER or EditorInfo.TYPE_NUMBER_VARIATION_PASSWORD |
| 205 | } |
Seigo Nonaka | 6e6e2ee | 2019-07-01 17:18:41 -0700 | [diff] [blame] | 206 | else -> throw IllegalArgumentException("Unknown KeyboardType: $keyboardType") |
| 207 | } |
Seigo Nonaka | c04f629d | 2019-07-11 13:20:30 -0700 | [diff] [blame] | 208 | outInfo.imeOptions = |
| 209 | outInfo.imeOptions or outInfo.imeOptions or EditorInfo.IME_FLAG_NO_FULLSCREEN |
Seigo Nonaka | 6e6e2ee | 2019-07-01 17:18:41 -0700 | [diff] [blame] | 210 | } |
Seigo Nonaka | a7e05d7 | 2019-09-30 17:31:25 -0700 | [diff] [blame] | 211 | } |