• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * Copyright (C) 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  *      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 @file:OptIn(ExperimentalComposeUiApi::class)
18 
19 package com.android.systemui.common.ui.compose
20 
21 import android.annotation.UserIdInt
22 import android.os.UserHandle
23 import android.view.inputmethod.EditorInfo
24 import android.view.inputmethod.InputConnection
25 import androidx.compose.runtime.Composable
26 import androidx.compose.ui.ExperimentalComposeUiApi
27 import androidx.compose.ui.platform.InterceptPlatformTextInput
28 import androidx.compose.ui.platform.PlatformTextInputMethodRequest
29 
30 /**
31  * Wrapper for input connection composables that need to be aware of the selected user to connect to
32  * the correct instance of on-device services like autofill, autocorrect, etc.
33  *
34  * Usage:
35  * ```
36  * @Composable
37  * fun YourFunction(viewModel: YourViewModel) {
38  *     val selectedUserId by viewModel.selectedUserId.collectAsStateWithLifecycle()
39  *
40  *     SelectedUserAwareInputConnection(selectedUserId) {
41  *         TextField(...)
42  *     }
43  * }
44  * ```
45  */
46 @Composable
47 fun SelectedUserAwareInputConnection(
48     @UserIdInt selectedUserId: Int,
49     content: @Composable () -> Unit,
50 ) {
51     InterceptPlatformTextInput(
52         interceptor = { request, nextHandler ->
53             // Create a new request to wrap the incoming one with some custom logic.
54             val modifiedRequest =
55                 object : PlatformTextInputMethodRequest {
56                     override fun createInputConnection(outAttributes: EditorInfo): InputConnection {
57                         val inputConnection = request.createInputConnection(outAttributes)
58                         // After the original request finishes initializing the EditorInfo we can
59                         // customize it. If we needed to we could also wrap the InputConnection
60                         // before
61                         // returning it.
62                         updateEditorInfo(outAttributes)
63                         return inputConnection
64                     }
65 
66                     fun updateEditorInfo(outAttributes: EditorInfo) {
67                         outAttributes.targetInputMethodUser = UserHandle.of(selectedUserId)
68                     }
69                 }
70 
71             // Send our wrapping request to the next handler, which could be the system or another
72             // interceptor up the tree.
73             nextHandler.startInputMethod(modifiedRequest)
74         }
75     ) {
76         content()
77     }
78 }
79