Debian Codegraph @

core/java/android/inputmethodservice/SoftInputWindow.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2007 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 android.inputmethodservice;
18
19 import static android.inputmethodservice.SoftInputWindowProto.BOUNDS;
20 import static android.inputmethodservice.SoftInputWindowProto.WINDOW_STATE;
21
22 import static java.lang.annotation.RetentionPolicy.SOURCE;
23
24 import android.annotation.IntDef;
25 import android.app.Dialog;
26 import android.graphics.Rect;
27 import android.os.Debug;
28 import android.os.IBinder;
29 import android.util.Log;
30 import android.util.proto.ProtoOutputStream;
31 import android.view.KeyEvent;
32 import android.view.MotionEvent;
33 import android.view.View;
34 import android.view.WindowManager;
35
36 import java.lang.annotation.Retention;
37
38 /**
39 * A {@link SoftInputWindow} is a {@link Dialog} that is intended to be used for a top-level input
40 * method window. It will be displayed along the edge of the screen, moving the application user
41 * interface away from it so that the focused item is always visible.
42 */
43 final class SoftInputWindow extends Dialog {
44 private static final boolean DEBUG = false;
45 private static final String TAG = "SoftInputWindow";
46
47 private final KeyEvent.DispatcherState mDispatcherState;
48 private final Rect mBounds = new Rect();
49 private final InputMethodService mService;
50
51 @Retention(SOURCE)
52 @IntDef(value = {WindowState.TOKEN_PENDING, WindowState.TOKEN_SET,
53 WindowState.SHOWN_AT_LEAST_ONCE, WindowState.REJECTED_AT_LEAST_ONCE,
54 WindowState.DESTROYED})
55 private @interface WindowState {
56 /**
57 * The window token is not set yet.
58 */
59 int TOKEN_PENDING = 0;
60 /**
61 * The window token was set, but the window is not shown yet.
62 */
63 int TOKEN_SET = 1;
64 /**
65 * The window was shown at least once.
66 */
67 int SHOWN_AT_LEAST_ONCE = 2;
68 /**
69 * {@link android.view.WindowManager.BadTokenException} was sent when calling
70 * {@link Dialog#show()} at least once.
71 */
72 int REJECTED_AT_LEAST_ONCE = 3;
73 /**
74 * The window is considered destroyed. Any incoming request should be ignored.
75 */
76 int DESTROYED = 4;
77 }
78
79 @WindowState
80 private int mWindowState = WindowState.TOKEN_PENDING;
81
82 /**
83 * Set {@link IBinder} window token to the window.
84 *
85 * <p>This method can be called only once.</p>
86 * @param token {@link IBinder} token to be associated with the window.
87 */
88 void setToken(IBinder token) {
89 switch (mWindowState) {
90 case WindowState.TOKEN_PENDING:
91 // Normal scenario. Nothing to worry about.
92 WindowManager.LayoutParams lp = getWindow().getAttributes();
93 lp.token = token;
94 getWindow().setAttributes(lp);
95 updateWindowState(WindowState.TOKEN_SET);
96
97 // As soon as we have a token, make sure the window is added (but not shown) by
98 // setting visibility to INVISIBLE and calling show() on Dialog. Note that
99 // WindowInsetsController.OnControllableInsetsChangedListener relies on the window
100 // being added to function.
101 getWindow().getDecorView().setVisibility(View.INVISIBLE);
102 show();
103 return;
104 case WindowState.TOKEN_SET:
105 case WindowState.SHOWN_AT_LEAST_ONCE:
106 case WindowState.REJECTED_AT_LEAST_ONCE:
107 throw new IllegalStateException("setToken can be called only once");
108 case WindowState.DESTROYED:
109 // Just ignore. Since there are multiple event queues from the token is issued
110 // in the system server to the timing when it arrives here, it can be delivered
111 // after the is already destroyed. No one should be blamed because of such an
112 // unfortunate but possible scenario.
113 Log.i(TAG, "Ignoring setToken() because window is already destroyed.");
114 return;
115 default:
116 throw new IllegalStateException("Unexpected state=" + mWindowState);
117 }
118 }
119
120 /**
121 * Create a SoftInputWindow that uses a custom style.
122 *
123 * @param service The {@link InputMethodService} in which the DockWindow should run. In
124 * particular, it uses the window manager and theme from this context
125 * to present its UI.
126 * @param theme A style resource describing the theme to use for the window.
127 * See <a href="{@docRoot}reference/available-resources.html#stylesandthemes">Style
128 * and Theme Resources</a> for more information about defining and
129 * using styles. This theme is applied on top of the current theme in
130 * <var>context</var>. If 0, the default dialog theme will be used.
131 */
132 SoftInputWindow(InputMethodService service, int theme,
133 KeyEvent.DispatcherState dispatcherState) {
134 super(service, theme);
135 mService = service;
136 mDispatcherState = dispatcherState;
137 }
138
139 @Override
140 public void onWindowFocusChanged(boolean hasFocus) {
141 super.onWindowFocusChanged(hasFocus);
142 mDispatcherState.reset();
143 }
144
145 @Override
146 public boolean dispatchTouchEvent(MotionEvent ev) {
147 getWindow().getDecorView().getHitRect(mBounds);
148
149 if (ev.isWithinBoundsNoHistory(mBounds.left, mBounds.top,
150 mBounds.right - 1, mBounds.bottom - 1)) {
151 return super.dispatchTouchEvent(ev);
152 } else {
153 MotionEvent temp = ev.clampNoHistory(mBounds.left, mBounds.top,
154 mBounds.right - 1, mBounds.bottom - 1);
155 boolean handled = super.dispatchTouchEvent(temp);
156 temp.recycle();
157 return handled;
158 }
159 }
160
161 @Override
162 public void show() {
163 switch (mWindowState) {
164 case WindowState.TOKEN_PENDING:
165 throw new IllegalStateException("Window token is not set yet.");
166 case WindowState.TOKEN_SET:
167 case WindowState.SHOWN_AT_LEAST_ONCE:
168 // Normal scenario. Nothing to worry about.
169 try {
170 super.show();
171 updateWindowState(WindowState.SHOWN_AT_LEAST_ONCE);
172 } catch (WindowManager.BadTokenException e) {
173 // Just ignore this exception. Since show() can be requested from other
174 // components such as the system and there could be multiple event queues before
175 // the request finally arrives here, the system may have already invalidated the
176 // window token attached to our window. In such a scenario, receiving
177 // BadTokenException here is an expected behavior. We just ignore it and update
178 // the state so that we do not touch this window later.
179 Log.i(TAG, "Probably the IME window token is already invalidated."
180 + " show() does nothing.");
181 updateWindowState(WindowState.REJECTED_AT_LEAST_ONCE);
182 }
183 return;
184 case WindowState.REJECTED_AT_LEAST_ONCE:
185 // Just ignore. In general we cannot completely avoid this kind of race condition.
186 Log.i(TAG, "Not trying to call show() because it was already rejected once.");
187 return;
188 case WindowState.DESTROYED:
189 // Just ignore. In general we cannot completely avoid this kind of race condition.
190 Log.i(TAG, "Ignoring show() because the window is already destroyed.");
191 return;
192 default:
193 throw new IllegalStateException("Unexpected state=" + mWindowState);
194 }
195 }
196
197 void dismissForDestroyIfNecessary() {
198 switch (mWindowState) {
199 case WindowState.TOKEN_PENDING:
200 case WindowState.TOKEN_SET:
201 // nothing to do because the window has never been shown.
202 updateWindowState(WindowState.DESTROYED);
203 return;
204 case WindowState.SHOWN_AT_LEAST_ONCE:
205 // Disable exit animation for the current IME window
206 // to avoid the race condition between the exit and enter animations
207 // when the current IME is being switched to another one.
208 try {
209 getWindow().setWindowAnimations(0);
210 dismiss();
211 } catch (WindowManager.BadTokenException e) {
212 // Just ignore this exception. Since show() can be requested from other
213 // components such as the system and there could be multiple event queues before
214 // the request finally arrives here, the system may have already invalidated the
215 // window token attached to our window. In such a scenario, receiving
216 // BadTokenException here is an expected behavior. We just ignore it and update
217 // the state so that we do not touch this window later.
218 Log.i(TAG, "Probably the IME window token is already invalidated. "
219 + "No need to dismiss it.");
220 }
221 // Either way, consider that the window is destroyed.
222 updateWindowState(WindowState.DESTROYED);
223 return;
224 case WindowState.REJECTED_AT_LEAST_ONCE:
225 // Just ignore. In general we cannot completely avoid this kind of race condition.
226 Log.i(TAG,
227 "Not trying to dismiss the window because it is most likely unnecessary.");
228 // Anyway, consider that the window is destroyed.
229 updateWindowState(WindowState.DESTROYED);
230 return;
231 case WindowState.DESTROYED:
232 throw new IllegalStateException(
233 "dismissForDestroyIfNecessary can be called only once");
234 default:
235 throw new IllegalStateException("Unexpected state=" + mWindowState);
236 }
237 }
238
239 private void updateWindowState(@WindowState int newState) {
240 if (DEBUG) {
241 if (mWindowState != newState) {
242 Log.d(TAG, "WindowState: " + stateToString(mWindowState) + " -> "
243 + stateToString(newState) + " @ " + Debug.getCaller());
244 }
245 }
246 mWindowState = newState;
247 }
248
249 private static String stateToString(@WindowState int state) {
250 switch (state) {
251 case WindowState.TOKEN_PENDING:
252 return "TOKEN_PENDING";
253 case WindowState.TOKEN_SET:
254 return "TOKEN_SET";
255 case WindowState.SHOWN_AT_LEAST_ONCE:
256 return "SHOWN_AT_LEAST_ONCE";
257 case WindowState.REJECTED_AT_LEAST_ONCE:
258 return "REJECTED_AT_LEAST_ONCE";
259 case WindowState.DESTROYED:
260 return "DESTROYED";
261 default:
262 throw new IllegalStateException("Unknown state=" + state);
263 }
264 }
265
266 void dumpDebug(ProtoOutputStream proto, long fieldId) {
267 final long token = proto.start(fieldId);
268 mBounds.dumpDebug(proto, BOUNDS);
269 proto.write(WINDOW_STATE, mWindowState);
270 proto.end(token);
271 }
272 }