1 /*
2  * 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 package com.android.server.inputmethod;
18 
19 import static android.server.inputmethod.InputMethodManagerServiceProto.CUR_FOCUSED_WINDOW_NAME;
20 import static android.server.inputmethod.InputMethodManagerServiceProto.CUR_FOCUSED_WINDOW_SOFT_INPUT_MODE;
21 import static android.view.WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED;
22 
23 import android.annotation.Nullable;
24 import android.annotation.UserIdInt;
25 import android.os.IBinder;
26 import android.os.UserHandle;
27 import android.util.Printer;
28 import android.util.proto.ProtoOutputStream;
29 import android.view.WindowManager;
30 import android.view.WindowManager.LayoutParams.SoftInputModeFlags;
31 import android.view.inputmethod.EditorInfo;
32 
33 import com.android.internal.inputmethod.InputMethodDebug;
34 import com.android.server.wm.WindowManagerInternal;
35 
36 /**
37  * Stores information related to one active IME client on one display.
38  */
39 final class ImeBindingState {
40 
41     @UserIdInt
42     final int mUserId;
43 
44     /**
45      * The last window token that we confirmed to be focused.  This is always updated upon
46      * reports from the input method client. If the window state is already changed before the
47      * report is handled, this field just keeps the last value.
48      */
49     @Nullable
50     final IBinder mFocusedWindow;
51 
52     /**
53      * {@link WindowManager.LayoutParams#softInputMode} of {@link #mFocusedWindow}.
54      *
55      * @see #mFocusedWindow
56      */
57     @SoftInputModeFlags
58     final int mFocusedWindowSoftInputMode;
59 
60     /**
61      * The client by which {@link #mFocusedWindow} was reported. This gets updated whenever
62      * an
63      * IME-focusable window gained focus (without necessarily starting an input connection),
64      * while {@link InputMethodManagerService#mClient} only gets updated when we actually start an
65      * input connection.
66      *
67      * @see #mFocusedWindow
68      */
69     @Nullable
70     final ClientState mFocusedWindowClient;
71 
72     /**
73      * The editor info by which {@link #mFocusedWindow} was reported. This differs from
74      * {@link InputMethodManagerService#mCurEditorInfo} the same way {@link #mFocusedWindowClient}
75      * differs from {@link InputMethodManagerService#mCurClient}.
76      *
77      * @see #mFocusedWindow
78      */
79     @Nullable
80     final EditorInfo mFocusedWindowEditorInfo;
81 
dumpDebug(ProtoOutputStream proto, WindowManagerInternal windowManagerInternal)82     void dumpDebug(ProtoOutputStream proto, WindowManagerInternal windowManagerInternal) {
83         proto.write(CUR_FOCUSED_WINDOW_NAME,
84                 windowManagerInternal.getWindowName(mFocusedWindow));
85         proto.write(CUR_FOCUSED_WINDOW_SOFT_INPUT_MODE,
86                 InputMethodDebug.softInputModeToString(mFocusedWindowSoftInputMode));
87     }
88 
dump(String prefix, Printer p)89     void dump(String prefix, Printer p) {
90         p.println(prefix + "mFocusedWindow()=" + mFocusedWindow);
91         p.println(prefix + "softInputMode=" + InputMethodDebug.softInputModeToString(
92                 mFocusedWindowSoftInputMode));
93         p.println(prefix + "mFocusedWindowClient=" + mFocusedWindowClient);
94     }
95 
newEmptyState()96     static ImeBindingState newEmptyState() {
97         return new ImeBindingState(
98                 /*userId=*/ UserHandle.USER_NULL,
99                 /*focusedWindow=*/ null,
100                 /*focusedWindowSoftInputMode=*/ SOFT_INPUT_STATE_UNSPECIFIED,
101                 /*focusedWindowClient=*/ null,
102                 /*focusedWindowEditorInfo=*/ null
103         );
104     }
105 
ImeBindingState(@serIdInt int userId, @Nullable IBinder focusedWindow, @SoftInputModeFlags int focusedWindowSoftInputMode, @Nullable ClientState focusedWindowClient, @Nullable EditorInfo focusedWindowEditorInfo)106     ImeBindingState(@UserIdInt int userId,
107             @Nullable IBinder focusedWindow,
108             @SoftInputModeFlags int focusedWindowSoftInputMode,
109             @Nullable ClientState focusedWindowClient,
110             @Nullable EditorInfo focusedWindowEditorInfo) {
111         mUserId = userId;
112         mFocusedWindow = focusedWindow;
113         mFocusedWindowSoftInputMode = focusedWindowSoftInputMode;
114         mFocusedWindowClient = focusedWindowClient;
115         mFocusedWindowEditorInfo = focusedWindowEditorInfo;
116     }
117 }
118