blob: 60333a3bf3f31edd24e90a81c8e4490cfc1239d2 [file] [log] [blame]
Jeff Brown46b9ac02010-04-22 18:58:52 -07001/*
2 * Copyright (C) 2010 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
Dianne Hackborna924dc0d2011-02-17 14:22:17 -080017package com.android.server.wm;
Jeff Brown46b9ac02010-04-22 18:58:52 -070018
19import com.android.internal.util.XmlUtils;
Jeff Brown89ef0722011-08-10 16:25:21 -070020import com.android.server.Watchdog;
Jeff Brown46b9ac02010-04-22 18:58:52 -070021
22import org.xmlpull.v1.XmlPullParser;
23
24import android.content.Context;
Jeff Brown349703e2010-06-22 01:27:15 -070025import android.content.pm.PackageManager;
Jeff Brown46b9ac02010-04-22 18:58:52 -070026import android.content.res.Configuration;
Jeff Brown1a84fd12011-06-02 01:26:32 -070027import android.database.ContentObserver;
Jeff Brown46b9ac02010-04-22 18:58:52 -070028import android.os.Environment;
Jeff Brown05dc66a2011-03-02 14:41:58 -080029import android.os.Looper;
30import android.os.MessageQueue;
Jeff Brownae9fc032010-08-18 15:51:08 -070031import android.os.SystemProperties;
Jeff Brown1a84fd12011-06-02 01:26:32 -070032import android.provider.Settings;
33import android.provider.Settings.SettingNotFoundException;
Jeff Brown46b9ac02010-04-22 18:58:52 -070034import android.util.Slog;
35import android.util.Xml;
36import android.view.InputChannel;
Jeff Brown8d608662010-08-30 03:02:23 -070037import android.view.InputDevice;
Jeff Brown6ec402b2010-07-28 15:48:59 -070038import android.view.InputEvent;
Jeff Brown1f245102010-11-18 20:53:46 -080039import android.view.KeyEvent;
Jeff Brown2352b972011-04-12 22:39:53 -070040import android.view.PointerIcon;
Jeff Brown46b9ac02010-04-22 18:58:52 -070041import android.view.Surface;
Jeff Browna4547672011-03-02 21:38:11 -080042import android.view.ViewConfiguration;
Jeff Brown83c09682010-12-23 17:50:18 -080043import android.view.WindowManager;
Jeff Brown0029c662011-03-30 02:25:18 -070044import android.view.WindowManagerPolicy;
Jeff Brown46b9ac02010-04-22 18:58:52 -070045
Jeff Brown46b9ac02010-04-22 18:58:52 -070046import java.io.File;
Jeff Brown46b9ac02010-04-22 18:58:52 -070047import java.io.FileNotFoundException;
48import java.io.FileReader;
49import java.io.IOException;
Jeff Brown46b9ac02010-04-22 18:58:52 -070050import java.io.PrintWriter;
51import java.util.ArrayList;
52
53/*
54 * Wraps the C++ InputManager and provides its callbacks.
Jeff Brown46b9ac02010-04-22 18:58:52 -070055 */
Jeff Brown89ef0722011-08-10 16:25:21 -070056public class InputManager implements Watchdog.Monitor {
Jeff Brown46b9ac02010-04-22 18:58:52 -070057 static final String TAG = "InputManager";
58
Jeff Brownb6997262010-10-08 22:31:17 -070059 private static final boolean DEBUG = false;
60
Jeff Brown46b9ac02010-04-22 18:58:52 -070061 private final Callbacks mCallbacks;
62 private final Context mContext;
63 private final WindowManagerService mWindowManagerService;
Jeff Brown1a84fd12011-06-02 01:26:32 -070064
Jeff Brown2352b972011-04-12 22:39:53 -070065 private static native void nativeInit(Context context,
66 Callbacks callbacks, MessageQueue messageQueue);
Jeff Brown46b9ac02010-04-22 18:58:52 -070067 private static native void nativeStart();
Jeff Brownbc68a592011-07-25 12:58:12 -070068 private static native void nativeSetDisplaySize(int displayId, int width, int height,
69 int externalWidth, int externalHeight);
Jeff Brown46b9ac02010-04-22 18:58:52 -070070 private static native void nativeSetDisplayOrientation(int displayId, int rotation);
71
Jeff Brown6d0fec22010-07-23 21:28:06 -070072 private static native int nativeGetScanCodeState(int deviceId, int sourceMask,
Jeff Brown46b9ac02010-04-22 18:58:52 -070073 int scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -070074 private static native int nativeGetKeyCodeState(int deviceId, int sourceMask,
Jeff Brown46b9ac02010-04-22 18:58:52 -070075 int keyCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -070076 private static native int nativeGetSwitchState(int deviceId, int sourceMask,
Jeff Brown46b9ac02010-04-22 18:58:52 -070077 int sw);
Jeff Brown6d0fec22010-07-23 21:28:06 -070078 private static native boolean nativeHasKeys(int deviceId, int sourceMask,
79 int[] keyCodes, boolean[] keyExists);
Jeff Browna41ca772010-08-11 14:46:32 -070080 private static native void nativeRegisterInputChannel(InputChannel inputChannel,
Jeff Brown928e0542011-01-10 11:17:36 -080081 InputWindowHandle inputWindowHandle, boolean monitor);
Jeff Brown46b9ac02010-04-22 18:58:52 -070082 private static native void nativeUnregisterInputChannel(InputChannel inputChannel);
Jeff Brown0029c662011-03-30 02:25:18 -070083 private static native void nativeSetInputFilterEnabled(boolean enable);
Jeff Brown6ec402b2010-07-28 15:48:59 -070084 private static native int nativeInjectInputEvent(InputEvent event,
Jeff Brown0029c662011-03-30 02:25:18 -070085 int injectorPid, int injectorUid, int syncMode, int timeoutMillis,
86 int policyFlags);
Jeff Brown9302c872011-07-13 22:51:29 -070087 private static native void nativeSetInputWindows(InputWindowHandle[] windowHandles);
Jeff Brown349703e2010-06-22 01:27:15 -070088 private static native void nativeSetInputDispatchMode(boolean enabled, boolean frozen);
Jeff Brown05dc66a2011-03-02 14:41:58 -080089 private static native void nativeSetSystemUiVisibility(int visibility);
Jeff Brown9302c872011-07-13 22:51:29 -070090 private static native void nativeSetFocusedApplication(InputApplicationHandle application);
Jeff Brown8d608662010-08-30 03:02:23 -070091 private static native InputDevice nativeGetInputDevice(int deviceId);
Jeff Brown57c59372010-09-21 18:22:55 -070092 private static native void nativeGetInputConfiguration(Configuration configuration);
Jeff Brown8d608662010-08-30 03:02:23 -070093 private static native int[] nativeGetInputDeviceIds();
Jeff Browne6504122010-09-27 14:52:15 -070094 private static native boolean nativeTransferTouchFocus(InputChannel fromChannel,
95 InputChannel toChannel);
Jeff Brown1a84fd12011-06-02 01:26:32 -070096 private static native void nativeSetPointerSpeed(int speed);
Jeff Browndaf4a122011-08-26 17:14:14 -070097 private static native void nativeSetShowTouches(boolean enabled);
Jeff Browne33348b2010-07-15 23:54:05 -070098 private static native String nativeDump();
Jeff Brown89ef0722011-08-10 16:25:21 -070099 private static native void nativeMonitor();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700100
Jeff Brown7fbdc842010-06-17 20:52:56 -0700101 // Input event injection constants defined in InputDispatcher.h.
102 static final int INPUT_EVENT_INJECTION_SUCCEEDED = 0;
103 static final int INPUT_EVENT_INJECTION_PERMISSION_DENIED = 1;
104 static final int INPUT_EVENT_INJECTION_FAILED = 2;
105 static final int INPUT_EVENT_INJECTION_TIMED_OUT = 3;
106
Jeff Brown6ec402b2010-07-28 15:48:59 -0700107 // Input event injection synchronization modes defined in InputDispatcher.h
108 static final int INPUT_EVENT_INJECTION_SYNC_NONE = 0;
109 static final int INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT = 1;
110 static final int INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_FINISH = 2;
111
Jeff Brown6d0fec22010-07-23 21:28:06 -0700112 // Key states (may be returned by queries about the current state of a
113 // particular key code, scan code or switch).
114
115 /** The key state is unknown or the requested key itself is not supported. */
116 public static final int KEY_STATE_UNKNOWN = -1;
117
118 /** The key is up. /*/
119 public static final int KEY_STATE_UP = 0;
120
121 /** The key is down. */
122 public static final int KEY_STATE_DOWN = 1;
123
124 /** The key is down but is a virtual key press that is being emulated by the system. */
125 public static final int KEY_STATE_VIRTUAL = 2;
126
Jeff Brown0029c662011-03-30 02:25:18 -0700127 // State for the currently installed input filter.
128 final Object mInputFilterLock = new Object();
129 InputFilter mInputFilter;
130 InputFilterHost mInputFilterHost;
131
Jeff Browne33348b2010-07-15 23:54:05 -0700132 public InputManager(Context context, WindowManagerService windowManagerService) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700133 this.mContext = context;
134 this.mWindowManagerService = windowManagerService;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700135 this.mCallbacks = new Callbacks();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800136
137 Looper looper = windowManagerService.mH.getLooper();
138
Jeff Brown46b9ac02010-04-22 18:58:52 -0700139 Slog.i(TAG, "Initializing input manager");
Jeff Brown2352b972011-04-12 22:39:53 -0700140 nativeInit(mContext, mCallbacks, looper.getQueue());
Jeff Brown89ef0722011-08-10 16:25:21 -0700141
142 // Add ourself to the Watchdog monitors.
143 Watchdog.getInstance().addMonitor(this);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700144 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700145
Jeff Brown46b9ac02010-04-22 18:58:52 -0700146 public void start() {
147 Slog.i(TAG, "Starting input manager");
148 nativeStart();
Jeff Brown1a84fd12011-06-02 01:26:32 -0700149
150 registerPointerSpeedSettingObserver();
Jeff Browndaf4a122011-08-26 17:14:14 -0700151 registerShowTouchesSettingObserver();
152
Jeff Brown1a84fd12011-06-02 01:26:32 -0700153 updatePointerSpeedFromSettings();
Jeff Browndaf4a122011-08-26 17:14:14 -0700154 updateShowTouchesFromSettings();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700155 }
156
Jeff Brownbc68a592011-07-25 12:58:12 -0700157 public void setDisplaySize(int displayId, int width, int height,
158 int externalWidth, int externalHeight) {
159 if (width <= 0 || height <= 0 || externalWidth <= 0 || externalHeight <= 0) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700160 throw new IllegalArgumentException("Invalid display id or dimensions.");
161 }
162
Jeff Brownb6997262010-10-08 22:31:17 -0700163 if (DEBUG) {
Jeff Brownbc68a592011-07-25 12:58:12 -0700164 Slog.d(TAG, "Setting display #" + displayId + " size to " + width + "x" + height
165 + " external size " + externalWidth + "x" + externalHeight);
Jeff Brownb6997262010-10-08 22:31:17 -0700166 }
Jeff Brownbc68a592011-07-25 12:58:12 -0700167 nativeSetDisplaySize(displayId, width, height, externalWidth, externalHeight);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700168 }
169
170 public void setDisplayOrientation(int displayId, int rotation) {
171 if (rotation < Surface.ROTATION_0 || rotation > Surface.ROTATION_270) {
172 throw new IllegalArgumentException("Invalid rotation.");
173 }
174
Jeff Brownb6997262010-10-08 22:31:17 -0700175 if (DEBUG) {
176 Slog.d(TAG, "Setting display #" + displayId + " orientation to " + rotation);
177 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700178 nativeSetDisplayOrientation(displayId, rotation);
179 }
180
181 public void getInputConfiguration(Configuration config) {
182 if (config == null) {
183 throw new IllegalArgumentException("config must not be null.");
184 }
185
Jeff Brown57c59372010-09-21 18:22:55 -0700186 nativeGetInputConfiguration(config);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700187 }
188
Jeff Brown6d0fec22010-07-23 21:28:06 -0700189 /**
190 * Gets the current state of a key or button by key code.
191 * @param deviceId The input device id, or -1 to consult all devices.
192 * @param sourceMask The input sources to consult, or {@link InputDevice#SOURCE_ANY} to
193 * consider all input sources. An input device is consulted if at least one of its
194 * non-class input source bits matches the specified source mask.
195 * @param keyCode The key code to check.
196 * @return The key state.
197 */
198 public int getKeyCodeState(int deviceId, int sourceMask, int keyCode) {
199 return nativeGetKeyCodeState(deviceId, sourceMask, keyCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700200 }
201
Jeff Brown6d0fec22010-07-23 21:28:06 -0700202 /**
203 * Gets the current state of a key or button by scan code.
204 * @param deviceId The input device id, or -1 to consult all devices.
205 * @param sourceMask The input sources to consult, or {@link InputDevice#SOURCE_ANY} to
206 * consider all input sources. An input device is consulted if at least one of its
207 * non-class input source bits matches the specified source mask.
208 * @param scanCode The scan code to check.
209 * @return The key state.
210 */
211 public int getScanCodeState(int deviceId, int sourceMask, int scanCode) {
212 return nativeGetScanCodeState(deviceId, sourceMask, scanCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700213 }
214
Jeff Brown6d0fec22010-07-23 21:28:06 -0700215 /**
216 * Gets the current state of a switch by switch code.
217 * @param deviceId The input device id, or -1 to consult all devices.
218 * @param sourceMask The input sources to consult, or {@link InputDevice#SOURCE_ANY} to
219 * consider all input sources. An input device is consulted if at least one of its
220 * non-class input source bits matches the specified source mask.
221 * @param switchCode The switch code to check.
222 * @return The switch state.
223 */
224 public int getSwitchState(int deviceId, int sourceMask, int switchCode) {
225 return nativeGetSwitchState(deviceId, sourceMask, switchCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700226 }
227
Jeff Brown6d0fec22010-07-23 21:28:06 -0700228 /**
229 * Determines whether the specified key codes are supported by a particular device.
230 * @param deviceId The input device id, or -1 to consult all devices.
231 * @param sourceMask The input sources to consult, or {@link InputDevice#SOURCE_ANY} to
232 * consider all input sources. An input device is consulted if at least one of its
233 * non-class input source bits matches the specified source mask.
234 * @param keyCodes The array of key codes to check.
235 * @param keyExists An array at least as large as keyCodes whose entries will be set
236 * to true or false based on the presence or absence of support for the corresponding
237 * key codes.
238 * @return True if the lookup was successful, false otherwise.
239 */
240 public boolean hasKeys(int deviceId, int sourceMask, int[] keyCodes, boolean[] keyExists) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700241 if (keyCodes == null) {
242 throw new IllegalArgumentException("keyCodes must not be null.");
243 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700244 if (keyExists == null || keyExists.length < keyCodes.length) {
245 throw new IllegalArgumentException("keyExists must not be null and must be at "
246 + "least as large as keyCodes.");
Jeff Brown46b9ac02010-04-22 18:58:52 -0700247 }
248
Jeff Brown6d0fec22010-07-23 21:28:06 -0700249 return nativeHasKeys(deviceId, sourceMask, keyCodes, keyExists);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700250 }
251
Jeff Browna41ca772010-08-11 14:46:32 -0700252 /**
253 * Creates an input channel that will receive all input from the input dispatcher.
254 * @param inputChannelName The input channel name.
255 * @return The input channel.
256 */
257 public InputChannel monitorInput(String inputChannelName) {
258 if (inputChannelName == null) {
259 throw new IllegalArgumentException("inputChannelName must not be null.");
260 }
261
262 InputChannel[] inputChannels = InputChannel.openInputChannelPair(inputChannelName);
Jeff Brown928e0542011-01-10 11:17:36 -0800263 nativeRegisterInputChannel(inputChannels[0], null, true);
Jeff Browna41ca772010-08-11 14:46:32 -0700264 inputChannels[0].dispose(); // don't need to retain the Java object reference
265 return inputChannels[1];
266 }
267
268 /**
269 * Registers an input channel so that it can be used as an input event target.
270 * @param inputChannel The input channel to register.
Jeff Brown928e0542011-01-10 11:17:36 -0800271 * @param inputWindowHandle The handle of the input window associated with the
272 * input channel, or null if none.
Jeff Browna41ca772010-08-11 14:46:32 -0700273 */
Jeff Brown928e0542011-01-10 11:17:36 -0800274 public void registerInputChannel(InputChannel inputChannel,
275 InputWindowHandle inputWindowHandle) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700276 if (inputChannel == null) {
277 throw new IllegalArgumentException("inputChannel must not be null.");
278 }
279
Jeff Brown928e0542011-01-10 11:17:36 -0800280 nativeRegisterInputChannel(inputChannel, inputWindowHandle, false);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700281 }
282
Jeff Browna41ca772010-08-11 14:46:32 -0700283 /**
284 * Unregisters an input channel.
285 * @param inputChannel The input channel to unregister.
286 */
Jeff Brown46b9ac02010-04-22 18:58:52 -0700287 public void unregisterInputChannel(InputChannel inputChannel) {
288 if (inputChannel == null) {
289 throw new IllegalArgumentException("inputChannel must not be null.");
290 }
291
292 nativeUnregisterInputChannel(inputChannel);
293 }
Jeff Brown0029c662011-03-30 02:25:18 -0700294
295 /**
296 * Sets an input filter that will receive all input events before they are dispatched.
297 * The input filter may then reinterpret input events or inject new ones.
298 *
299 * To ensure consistency, the input dispatcher automatically drops all events
300 * in progress whenever an input filter is installed or uninstalled. After an input
301 * filter is uninstalled, it can no longer send input events unless it is reinstalled.
302 * Any events it attempts to send after it has been uninstalled will be dropped.
303 *
304 * @param filter The input filter, or null to remove the current filter.
305 */
306 public void setInputFilter(InputFilter filter) {
307 synchronized (mInputFilterLock) {
308 final InputFilter oldFilter = mInputFilter;
309 if (oldFilter == filter) {
310 return; // nothing to do
311 }
312
313 if (oldFilter != null) {
314 mInputFilter = null;
315 mInputFilterHost.disconnectLocked();
316 mInputFilterHost = null;
317 oldFilter.uninstall();
318 }
319
320 if (filter != null) {
321 mInputFilter = filter;
322 mInputFilterHost = new InputFilterHost();
323 filter.install(mInputFilterHost);
324 }
325
326 nativeSetInputFilterEnabled(filter != null);
327 }
328 }
329
Jeff Brown46b9ac02010-04-22 18:58:52 -0700330 /**
Jeff Brown6ec402b2010-07-28 15:48:59 -0700331 * Injects an input event into the event system on behalf of an application.
332 * The synchronization mode determines whether the method blocks while waiting for
333 * input injection to proceed.
334 *
335 * {@link #INPUT_EVENT_INJECTION_SYNC_NONE} never blocks. Injection is asynchronous and
336 * is assumed always to be successful.
337 *
338 * {@link #INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT} waits for previous events to be
339 * dispatched so that the input dispatcher can determine whether input event injection will
340 * be permitted based on the current input focus. Does not wait for the input event to
341 * finish processing.
342 *
343 * {@link #INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_FINISH} waits for the input event to
344 * be completely processed.
345 *
Jeff Brown46b9ac02010-04-22 18:58:52 -0700346 * @param event The event to inject.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700347 * @param injectorPid The pid of the injecting application.
348 * @param injectorUid The uid of the injecting application.
Jeff Brown6ec402b2010-07-28 15:48:59 -0700349 * @param syncMode The synchronization mode.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700350 * @param timeoutMillis The injection timeout in milliseconds.
351 * @return One of the INPUT_EVENT_INJECTION_XXX constants.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700352 */
Jeff Brown6ec402b2010-07-28 15:48:59 -0700353 public int injectInputEvent(InputEvent event, int injectorPid, int injectorUid,
354 int syncMode, int timeoutMillis) {
Jeff Brown7fbdc842010-06-17 20:52:56 -0700355 if (event == null) {
356 throw new IllegalArgumentException("event must not be null");
357 }
358 if (injectorPid < 0 || injectorUid < 0) {
359 throw new IllegalArgumentException("injectorPid and injectorUid must not be negative.");
360 }
361 if (timeoutMillis <= 0) {
362 throw new IllegalArgumentException("timeoutMillis must be positive");
363 }
Jeff Brown6ec402b2010-07-28 15:48:59 -0700364
Jeff Brown0029c662011-03-30 02:25:18 -0700365 return nativeInjectInputEvent(event, injectorPid, injectorUid, syncMode, timeoutMillis,
366 WindowManagerPolicy.FLAG_DISABLE_KEY_REPEAT);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700367 }
Jeff Brown0029c662011-03-30 02:25:18 -0700368
Jeff Brown8d608662010-08-30 03:02:23 -0700369 /**
370 * Gets information about the input device with the specified id.
371 * @param id The device id.
372 * @return The input device or null if not found.
373 */
374 public InputDevice getInputDevice(int deviceId) {
375 return nativeGetInputDevice(deviceId);
376 }
377
378 /**
379 * Gets the ids of all input devices in the system.
380 * @return The input device ids.
381 */
382 public int[] getInputDeviceIds() {
383 return nativeGetInputDeviceIds();
384 }
385
Jeff Brown9302c872011-07-13 22:51:29 -0700386 public void setInputWindows(InputWindowHandle[] windowHandles) {
387 nativeSetInputWindows(windowHandles);
Jeff Brown349703e2010-06-22 01:27:15 -0700388 }
389
Jeff Brown9302c872011-07-13 22:51:29 -0700390 public void setFocusedApplication(InputApplicationHandle application) {
Jeff Brown349703e2010-06-22 01:27:15 -0700391 nativeSetFocusedApplication(application);
392 }
393
Jeff Brown349703e2010-06-22 01:27:15 -0700394 public void setInputDispatchMode(boolean enabled, boolean frozen) {
395 nativeSetInputDispatchMode(enabled, frozen);
396 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800397
398 public void setSystemUiVisibility(int visibility) {
399 nativeSetSystemUiVisibility(visibility);
400 }
401
Jeff Browne6504122010-09-27 14:52:15 -0700402 /**
403 * Atomically transfers touch focus from one window to another as identified by
404 * their input channels. It is possible for multiple windows to have
405 * touch focus if they support split touch dispatch
406 * {@link android.view.WindowManager.LayoutParams#FLAG_SPLIT_TOUCH} but this
407 * method only transfers touch focus of the specified window without affecting
408 * other windows that may also have touch focus at the same time.
409 * @param fromChannel The channel of a window that currently has touch focus.
410 * @param toChannel The channel of the window that should receive touch focus in
411 * place of the first.
412 * @return True if the transfer was successful. False if the window with the
413 * specified channel did not actually have touch focus at the time of the request.
414 */
415 public boolean transferTouchFocus(InputChannel fromChannel, InputChannel toChannel) {
416 if (fromChannel == null) {
417 throw new IllegalArgumentException("fromChannel must not be null.");
418 }
419 if (toChannel == null) {
420 throw new IllegalArgumentException("toChannel must not be null.");
421 }
422 return nativeTransferTouchFocus(fromChannel, toChannel);
423 }
Jeff Brown05dc66a2011-03-02 14:41:58 -0800424
Jeff Brown1a84fd12011-06-02 01:26:32 -0700425 /**
426 * Set the pointer speed.
427 * @param speed The pointer speed as a value between -7 (slowest) and 7 (fastest)
428 * where 0 is the default speed.
429 */
430 public void setPointerSpeed(int speed) {
431 speed = Math.min(Math.max(speed, -7), 7);
432 nativeSetPointerSpeed(speed);
433 }
434
435 public void updatePointerSpeedFromSettings() {
436 int speed = getPointerSpeedSetting(0);
437 setPointerSpeed(speed);
438 }
439
440 private void registerPointerSpeedSettingObserver() {
441 mContext.getContentResolver().registerContentObserver(
442 Settings.System.getUriFor(Settings.System.POINTER_SPEED), true,
443 new ContentObserver(mWindowManagerService.mH) {
444 @Override
445 public void onChange(boolean selfChange) {
446 updatePointerSpeedFromSettings();
447 }
448 });
449 }
450
451 private int getPointerSpeedSetting(int defaultValue) {
452 int speed = defaultValue;
453 try {
454 speed = Settings.System.getInt(mContext.getContentResolver(),
455 Settings.System.POINTER_SPEED);
456 } catch (SettingNotFoundException snfe) {
457 }
458 return speed;
459 }
460
Jeff Browndaf4a122011-08-26 17:14:14 -0700461 public void updateShowTouchesFromSettings() {
462 int setting = getShowTouchesSetting(0);
463 nativeSetShowTouches(setting != 0);
464 }
465
466 private void registerShowTouchesSettingObserver() {
467 mContext.getContentResolver().registerContentObserver(
468 Settings.System.getUriFor(Settings.System.SHOW_TOUCHES), true,
469 new ContentObserver(mWindowManagerService.mH) {
470 @Override
471 public void onChange(boolean selfChange) {
472 updateShowTouchesFromSettings();
473 }
474 });
475 }
476
477 private int getShowTouchesSetting(int defaultValue) {
478 int result = defaultValue;
479 try {
480 result = Settings.System.getInt(mContext.getContentResolver(),
481 Settings.System.SHOW_TOUCHES);
482 } catch (SettingNotFoundException snfe) {
483 }
484 return result;
485 }
486
Jeff Brown46b9ac02010-04-22 18:58:52 -0700487 public void dump(PrintWriter pw) {
Jeff Browne33348b2010-07-15 23:54:05 -0700488 String dumpStr = nativeDump();
489 if (dumpStr != null) {
490 pw.println(dumpStr);
491 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700492 }
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800493
Jeff Brown89ef0722011-08-10 16:25:21 -0700494 // Called by the heartbeat to ensure locks are not held indefnitely (for deadlock detection).
495 public void monitor() {
496 synchronized (mInputFilterLock) { }
497 nativeMonitor();
498 }
499
Jeff Brown0029c662011-03-30 02:25:18 -0700500 private final class InputFilterHost implements InputFilter.Host {
501 private boolean mDisconnected;
502
503 public void disconnectLocked() {
504 mDisconnected = true;
505 }
506
507 public void sendInputEvent(InputEvent event, int policyFlags) {
508 if (event == null) {
509 throw new IllegalArgumentException("event must not be null");
510 }
511
512 synchronized (mInputFilterLock) {
513 if (!mDisconnected) {
514 nativeInjectInputEvent(event, 0, 0, INPUT_EVENT_INJECTION_SYNC_NONE, 0,
515 policyFlags | WindowManagerPolicy.FLAG_FILTERED);
516 }
517 }
518 }
519 }
520
Jeff Brown46b9ac02010-04-22 18:58:52 -0700521 /*
522 * Callbacks from native.
523 */
Jeff Brown0029c662011-03-30 02:25:18 -0700524 private final class Callbacks {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700525 static final String TAG = "InputManager-Callbacks";
526
527 private static final boolean DEBUG_VIRTUAL_KEYS = false;
528 private static final String EXCLUDED_DEVICES_PATH = "etc/excluded-input-devices.xml";
Jeff Brown8d608662010-08-30 03:02:23 -0700529 private static final String CALIBRATION_DIR_PATH = "usr/idc/";
Jeff Brown46b9ac02010-04-22 18:58:52 -0700530
Jeff Brown46b9ac02010-04-22 18:58:52 -0700531 @SuppressWarnings("unused")
Jeff Brown57c59372010-09-21 18:22:55 -0700532 public void notifyConfigurationChanged(long whenNanos) {
Jeff Brownb09abc12011-01-13 21:08:27 -0800533 mWindowManagerService.mInputMonitor.notifyConfigurationChanged();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700534 }
535
536 @SuppressWarnings("unused")
537 public void notifyLidSwitchChanged(long whenNanos, boolean lidOpen) {
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700538 mWindowManagerService.mInputMonitor.notifyLidSwitchChanged(whenNanos, lidOpen);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700539 }
540
541 @SuppressWarnings("unused")
Jeff Brown928e0542011-01-10 11:17:36 -0800542 public void notifyInputChannelBroken(InputWindowHandle inputWindowHandle) {
543 mWindowManagerService.mInputMonitor.notifyInputChannelBroken(inputWindowHandle);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700544 }
Jeff Brown7fbdc842010-06-17 20:52:56 -0700545
546 @SuppressWarnings("unused")
Jeff Brown928e0542011-01-10 11:17:36 -0800547 public long notifyANR(InputApplicationHandle inputApplicationHandle,
548 InputWindowHandle inputWindowHandle) {
549 return mWindowManagerService.mInputMonitor.notifyANR(
550 inputApplicationHandle, inputWindowHandle);
Jeff Brown349703e2010-06-22 01:27:15 -0700551 }
Jeff Brown0029c662011-03-30 02:25:18 -0700552
553 @SuppressWarnings("unused")
554 final boolean filterInputEvent(InputEvent event, int policyFlags) {
555 synchronized (mInputFilterLock) {
556 if (mInputFilter != null) {
557 mInputFilter.filterInputEvent(event, policyFlags);
558 return false;
559 }
560 }
561 event.recycle();
562 return true;
563 }
564
Jeff Brown349703e2010-06-22 01:27:15 -0700565 @SuppressWarnings("unused")
Jeff Brown1f245102010-11-18 20:53:46 -0800566 public int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags, boolean isScreenOn) {
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700567 return mWindowManagerService.mInputMonitor.interceptKeyBeforeQueueing(
Jeff Brown1f245102010-11-18 20:53:46 -0800568 event, policyFlags, isScreenOn);
Jeff Brown349703e2010-06-22 01:27:15 -0700569 }
Jeff Brown56194eb2011-03-02 19:23:13 -0800570
571 @SuppressWarnings("unused")
572 public int interceptMotionBeforeQueueingWhenScreenOff(int policyFlags) {
573 return mWindowManagerService.mInputMonitor.interceptMotionBeforeQueueingWhenScreenOff(
574 policyFlags);
575 }
576
Jeff Brown349703e2010-06-22 01:27:15 -0700577 @SuppressWarnings("unused")
Jeff Brown928e0542011-01-10 11:17:36 -0800578 public boolean interceptKeyBeforeDispatching(InputWindowHandle focus,
Jeff Brown1f245102010-11-18 20:53:46 -0800579 KeyEvent event, int policyFlags) {
580 return mWindowManagerService.mInputMonitor.interceptKeyBeforeDispatching(
581 focus, event, policyFlags);
Jeff Brown349703e2010-06-22 01:27:15 -0700582 }
583
584 @SuppressWarnings("unused")
Jeff Brown928e0542011-01-10 11:17:36 -0800585 public KeyEvent dispatchUnhandledKey(InputWindowHandle focus,
Jeff Brown1f245102010-11-18 20:53:46 -0800586 KeyEvent event, int policyFlags) {
587 return mWindowManagerService.mInputMonitor.dispatchUnhandledKey(
588 focus, event, policyFlags);
Jeff Brown3915bb82010-11-05 15:02:16 -0700589 }
590
591 @SuppressWarnings("unused")
Jeff Brown349703e2010-06-22 01:27:15 -0700592 public boolean checkInjectEventsPermission(int injectorPid, int injectorUid) {
593 return mContext.checkPermission(
594 android.Manifest.permission.INJECT_EVENTS, injectorPid, injectorUid)
595 == PackageManager.PERMISSION_GRANTED;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700596 }
Jeff Brownfe508922011-01-18 15:10:10 -0800597
598 @SuppressWarnings("unused")
599 public int getVirtualKeyQuietTimeMillis() {
600 return mContext.getResources().getInteger(
601 com.android.internal.R.integer.config_virtualKeyQuietTimeMillis);
602 }
603
Jeff Brown46b9ac02010-04-22 18:58:52 -0700604 @SuppressWarnings("unused")
Jeff Brown46b9ac02010-04-22 18:58:52 -0700605 public String[] getExcludedDeviceNames() {
606 ArrayList<String> names = new ArrayList<String>();
607
608 // Read partner-provided list of excluded input devices
609 XmlPullParser parser = null;
610 // Environment.getRootDirectory() is a fancy way of saying ANDROID_ROOT or "/system".
611 File confFile = new File(Environment.getRootDirectory(), EXCLUDED_DEVICES_PATH);
612 FileReader confreader = null;
613 try {
614 confreader = new FileReader(confFile);
615 parser = Xml.newPullParser();
616 parser.setInput(confreader);
617 XmlUtils.beginDocument(parser, "devices");
618
619 while (true) {
620 XmlUtils.nextElement(parser);
621 if (!"device".equals(parser.getName())) {
622 break;
623 }
624 String name = parser.getAttributeValue(null, "name");
625 if (name != null) {
626 names.add(name);
627 }
628 }
629 } catch (FileNotFoundException e) {
630 // It's ok if the file does not exist.
631 } catch (Exception e) {
632 Slog.e(TAG, "Exception while parsing '" + confFile.getAbsolutePath() + "'", e);
633 } finally {
634 try { if (confreader != null) confreader.close(); } catch (IOException e) { }
635 }
636
637 return names.toArray(new String[names.size()]);
638 }
Jeff Browna4547672011-03-02 21:38:11 -0800639
640 @SuppressWarnings("unused")
641 public int getKeyRepeatTimeout() {
642 return ViewConfiguration.getKeyRepeatTimeout();
643 }
644
645 @SuppressWarnings("unused")
646 public int getKeyRepeatDelay() {
647 return ViewConfiguration.getKeyRepeatDelay();
648 }
649
Jeff Brownae9fc032010-08-18 15:51:08 -0700650 @SuppressWarnings("unused")
Jeff Brownbb3fcba0c2011-06-06 19:23:05 -0700651 public int getHoverTapTimeout() {
652 return ViewConfiguration.getHoverTapTimeout();
653 }
654
655 @SuppressWarnings("unused")
656 public int getHoverTapSlop() {
657 return ViewConfiguration.getHoverTapSlop();
Jeff Brown214eaf42011-05-26 19:17:02 -0700658 }
659
660 @SuppressWarnings("unused")
661 public int getDoubleTapTimeout() {
662 return ViewConfiguration.getDoubleTapTimeout();
663 }
664
665 @SuppressWarnings("unused")
666 public int getLongPressTimeout() {
667 return ViewConfiguration.getLongPressTimeout();
668 }
669
670 @SuppressWarnings("unused")
Jeff Brownae9fc032010-08-18 15:51:08 -0700671 public int getMaxEventsPerSecond() {
672 int result = 0;
673 try {
674 result = Integer.parseInt(SystemProperties.get("windowsmgr.max_events_per_sec"));
675 } catch (NumberFormatException e) {
676 }
677 if (result < 1) {
makarand.karvekar88dd6e62011-03-02 17:11:14 -0600678 result = 55;
Jeff Brownae9fc032010-08-18 15:51:08 -0700679 }
680 return result;
681 }
Jeff Brown83c09682010-12-23 17:50:18 -0800682
683 @SuppressWarnings("unused")
684 public int getPointerLayer() {
685 return mWindowManagerService.mPolicy.windowTypeToLayerLw(
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800686 WindowManager.LayoutParams.TYPE_POINTER)
Jeff Brown83c09682010-12-23 17:50:18 -0800687 * WindowManagerService.TYPE_LAYER_MULTIPLIER
688 + WindowManagerService.TYPE_LAYER_OFFSET;
689 }
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800690
691 @SuppressWarnings("unused")
692 public PointerIcon getPointerIcon() {
Jeff Brown2352b972011-04-12 22:39:53 -0700693 return PointerIcon.getDefaultIcon(mContext);
Jeff Brownb4ff35d2011-01-02 16:37:43 -0800694 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700695 }
696}