Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1 | /* |
| 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 Hackborn | a924dc0d | 2011-02-17 14:22:17 -0800 | [diff] [blame] | 17 | package com.android.server.wm; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 18 | |
| 19 | import com.android.internal.util.XmlUtils; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 20 | |
| 21 | import org.xmlpull.v1.XmlPullParser; |
| 22 | |
| 23 | import android.content.Context; |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 24 | import android.content.pm.PackageManager; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 25 | import android.content.res.Configuration; |
| 26 | import android.os.Environment; |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 27 | import android.os.Looper; |
| 28 | import android.os.MessageQueue; |
Jeff Brown | ae9fc03 | 2010-08-18 15:51:08 -0700 | [diff] [blame] | 29 | import android.os.SystemProperties; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 30 | import android.util.Slog; |
| 31 | import android.util.Xml; |
| 32 | import android.view.InputChannel; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 33 | import android.view.InputDevice; |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 34 | import android.view.InputEvent; |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 35 | import android.view.KeyEvent; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame^] | 36 | import android.view.PointerIcon; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 37 | import android.view.Surface; |
Jeff Brown | a454767 | 2011-03-02 21:38:11 -0800 | [diff] [blame] | 38 | import android.view.ViewConfiguration; |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 39 | import android.view.WindowManager; |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 40 | import android.view.WindowManagerPolicy; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 41 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 42 | import java.io.File; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 43 | import java.io.FileNotFoundException; |
| 44 | import java.io.FileReader; |
| 45 | import java.io.IOException; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 46 | import java.io.PrintWriter; |
| 47 | import java.util.ArrayList; |
| 48 | |
| 49 | /* |
| 50 | * Wraps the C++ InputManager and provides its callbacks. |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 51 | */ |
| 52 | public class InputManager { |
| 53 | static final String TAG = "InputManager"; |
| 54 | |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 55 | private static final boolean DEBUG = false; |
| 56 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 57 | private final Callbacks mCallbacks; |
| 58 | private final Context mContext; |
| 59 | private final WindowManagerService mWindowManagerService; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 60 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame^] | 61 | private static native void nativeInit(Context context, |
| 62 | Callbacks callbacks, MessageQueue messageQueue); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 63 | private static native void nativeStart(); |
| 64 | private static native void nativeSetDisplaySize(int displayId, int width, int height); |
| 65 | private static native void nativeSetDisplayOrientation(int displayId, int rotation); |
| 66 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 67 | private static native int nativeGetScanCodeState(int deviceId, int sourceMask, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 68 | int scanCode); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 69 | private static native int nativeGetKeyCodeState(int deviceId, int sourceMask, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 70 | int keyCode); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 71 | private static native int nativeGetSwitchState(int deviceId, int sourceMask, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 72 | int sw); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 73 | private static native boolean nativeHasKeys(int deviceId, int sourceMask, |
| 74 | int[] keyCodes, boolean[] keyExists); |
Jeff Brown | a41ca77 | 2010-08-11 14:46:32 -0700 | [diff] [blame] | 75 | private static native void nativeRegisterInputChannel(InputChannel inputChannel, |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 76 | InputWindowHandle inputWindowHandle, boolean monitor); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 77 | private static native void nativeUnregisterInputChannel(InputChannel inputChannel); |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 78 | private static native void nativeSetInputFilterEnabled(boolean enable); |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 79 | private static native int nativeInjectInputEvent(InputEvent event, |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 80 | int injectorPid, int injectorUid, int syncMode, int timeoutMillis, |
| 81 | int policyFlags); |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 82 | private static native void nativeSetInputWindows(InputWindow[] windows); |
| 83 | private static native void nativeSetInputDispatchMode(boolean enabled, boolean frozen); |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 84 | private static native void nativeSetSystemUiVisibility(int visibility); |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 85 | private static native void nativeSetFocusedApplication(InputApplication application); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 86 | private static native InputDevice nativeGetInputDevice(int deviceId); |
Jeff Brown | 57c5937 | 2010-09-21 18:22:55 -0700 | [diff] [blame] | 87 | private static native void nativeGetInputConfiguration(Configuration configuration); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 88 | private static native int[] nativeGetInputDeviceIds(); |
Jeff Brown | e650412 | 2010-09-27 14:52:15 -0700 | [diff] [blame] | 89 | private static native boolean nativeTransferTouchFocus(InputChannel fromChannel, |
| 90 | InputChannel toChannel); |
Jeff Brown | e33348b | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 91 | private static native String nativeDump(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 92 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 93 | // Input event injection constants defined in InputDispatcher.h. |
| 94 | static final int INPUT_EVENT_INJECTION_SUCCEEDED = 0; |
| 95 | static final int INPUT_EVENT_INJECTION_PERMISSION_DENIED = 1; |
| 96 | static final int INPUT_EVENT_INJECTION_FAILED = 2; |
| 97 | static final int INPUT_EVENT_INJECTION_TIMED_OUT = 3; |
| 98 | |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 99 | // Input event injection synchronization modes defined in InputDispatcher.h |
| 100 | static final int INPUT_EVENT_INJECTION_SYNC_NONE = 0; |
| 101 | static final int INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT = 1; |
| 102 | static final int INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_FINISH = 2; |
| 103 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 104 | // Key states (may be returned by queries about the current state of a |
| 105 | // particular key code, scan code or switch). |
| 106 | |
| 107 | /** The key state is unknown or the requested key itself is not supported. */ |
| 108 | public static final int KEY_STATE_UNKNOWN = -1; |
| 109 | |
| 110 | /** The key is up. /*/ |
| 111 | public static final int KEY_STATE_UP = 0; |
| 112 | |
| 113 | /** The key is down. */ |
| 114 | public static final int KEY_STATE_DOWN = 1; |
| 115 | |
| 116 | /** The key is down but is a virtual key press that is being emulated by the system. */ |
| 117 | public static final int KEY_STATE_VIRTUAL = 2; |
| 118 | |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 119 | // State for the currently installed input filter. |
| 120 | final Object mInputFilterLock = new Object(); |
| 121 | InputFilter mInputFilter; |
| 122 | InputFilterHost mInputFilterHost; |
| 123 | |
Jeff Brown | e33348b | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 124 | public InputManager(Context context, WindowManagerService windowManagerService) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 125 | this.mContext = context; |
| 126 | this.mWindowManagerService = windowManagerService; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 127 | this.mCallbacks = new Callbacks(); |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 128 | |
| 129 | Looper looper = windowManagerService.mH.getLooper(); |
| 130 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 131 | Slog.i(TAG, "Initializing input manager"); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame^] | 132 | nativeInit(mContext, mCallbacks, looper.getQueue()); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | public void start() { |
| 136 | Slog.i(TAG, "Starting input manager"); |
| 137 | nativeStart(); |
| 138 | } |
| 139 | |
| 140 | public void setDisplaySize(int displayId, int width, int height) { |
| 141 | if (width <= 0 || height <= 0) { |
| 142 | throw new IllegalArgumentException("Invalid display id or dimensions."); |
| 143 | } |
| 144 | |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 145 | if (DEBUG) { |
| 146 | Slog.d(TAG, "Setting display #" + displayId + " size to " + width + "x" + height); |
| 147 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 148 | nativeSetDisplaySize(displayId, width, height); |
| 149 | } |
| 150 | |
| 151 | public void setDisplayOrientation(int displayId, int rotation) { |
| 152 | if (rotation < Surface.ROTATION_0 || rotation > Surface.ROTATION_270) { |
| 153 | throw new IllegalArgumentException("Invalid rotation."); |
| 154 | } |
| 155 | |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 156 | if (DEBUG) { |
| 157 | Slog.d(TAG, "Setting display #" + displayId + " orientation to " + rotation); |
| 158 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 159 | nativeSetDisplayOrientation(displayId, rotation); |
| 160 | } |
| 161 | |
| 162 | public void getInputConfiguration(Configuration config) { |
| 163 | if (config == null) { |
| 164 | throw new IllegalArgumentException("config must not be null."); |
| 165 | } |
| 166 | |
Jeff Brown | 57c5937 | 2010-09-21 18:22:55 -0700 | [diff] [blame] | 167 | nativeGetInputConfiguration(config); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 170 | /** |
| 171 | * Gets the current state of a key or button by key code. |
| 172 | * @param deviceId The input device id, or -1 to consult all devices. |
| 173 | * @param sourceMask The input sources to consult, or {@link InputDevice#SOURCE_ANY} to |
| 174 | * consider all input sources. An input device is consulted if at least one of its |
| 175 | * non-class input source bits matches the specified source mask. |
| 176 | * @param keyCode The key code to check. |
| 177 | * @return The key state. |
| 178 | */ |
| 179 | public int getKeyCodeState(int deviceId, int sourceMask, int keyCode) { |
| 180 | return nativeGetKeyCodeState(deviceId, sourceMask, keyCode); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 183 | /** |
| 184 | * Gets the current state of a key or button by scan code. |
| 185 | * @param deviceId The input device id, or -1 to consult all devices. |
| 186 | * @param sourceMask The input sources to consult, or {@link InputDevice#SOURCE_ANY} to |
| 187 | * consider all input sources. An input device is consulted if at least one of its |
| 188 | * non-class input source bits matches the specified source mask. |
| 189 | * @param scanCode The scan code to check. |
| 190 | * @return The key state. |
| 191 | */ |
| 192 | public int getScanCodeState(int deviceId, int sourceMask, int scanCode) { |
| 193 | return nativeGetScanCodeState(deviceId, sourceMask, scanCode); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 196 | /** |
| 197 | * Gets the current state of a switch by switch code. |
| 198 | * @param deviceId The input device id, or -1 to consult all devices. |
| 199 | * @param sourceMask The input sources to consult, or {@link InputDevice#SOURCE_ANY} to |
| 200 | * consider all input sources. An input device is consulted if at least one of its |
| 201 | * non-class input source bits matches the specified source mask. |
| 202 | * @param switchCode The switch code to check. |
| 203 | * @return The switch state. |
| 204 | */ |
| 205 | public int getSwitchState(int deviceId, int sourceMask, int switchCode) { |
| 206 | return nativeGetSwitchState(deviceId, sourceMask, switchCode); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 209 | /** |
| 210 | * Determines whether the specified key codes are supported by a particular device. |
| 211 | * @param deviceId The input device id, or -1 to consult all devices. |
| 212 | * @param sourceMask The input sources to consult, or {@link InputDevice#SOURCE_ANY} to |
| 213 | * consider all input sources. An input device is consulted if at least one of its |
| 214 | * non-class input source bits matches the specified source mask. |
| 215 | * @param keyCodes The array of key codes to check. |
| 216 | * @param keyExists An array at least as large as keyCodes whose entries will be set |
| 217 | * to true or false based on the presence or absence of support for the corresponding |
| 218 | * key codes. |
| 219 | * @return True if the lookup was successful, false otherwise. |
| 220 | */ |
| 221 | public boolean hasKeys(int deviceId, int sourceMask, int[] keyCodes, boolean[] keyExists) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 222 | if (keyCodes == null) { |
| 223 | throw new IllegalArgumentException("keyCodes must not be null."); |
| 224 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 225 | if (keyExists == null || keyExists.length < keyCodes.length) { |
| 226 | throw new IllegalArgumentException("keyExists must not be null and must be at " |
| 227 | + "least as large as keyCodes."); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 230 | return nativeHasKeys(deviceId, sourceMask, keyCodes, keyExists); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Jeff Brown | a41ca77 | 2010-08-11 14:46:32 -0700 | [diff] [blame] | 233 | /** |
| 234 | * Creates an input channel that will receive all input from the input dispatcher. |
| 235 | * @param inputChannelName The input channel name. |
| 236 | * @return The input channel. |
| 237 | */ |
| 238 | public InputChannel monitorInput(String inputChannelName) { |
| 239 | if (inputChannelName == null) { |
| 240 | throw new IllegalArgumentException("inputChannelName must not be null."); |
| 241 | } |
| 242 | |
| 243 | InputChannel[] inputChannels = InputChannel.openInputChannelPair(inputChannelName); |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 244 | nativeRegisterInputChannel(inputChannels[0], null, true); |
Jeff Brown | a41ca77 | 2010-08-11 14:46:32 -0700 | [diff] [blame] | 245 | inputChannels[0].dispose(); // don't need to retain the Java object reference |
| 246 | return inputChannels[1]; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Registers an input channel so that it can be used as an input event target. |
| 251 | * @param inputChannel The input channel to register. |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 252 | * @param inputWindowHandle The handle of the input window associated with the |
| 253 | * input channel, or null if none. |
Jeff Brown | a41ca77 | 2010-08-11 14:46:32 -0700 | [diff] [blame] | 254 | */ |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 255 | public void registerInputChannel(InputChannel inputChannel, |
| 256 | InputWindowHandle inputWindowHandle) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 257 | if (inputChannel == null) { |
| 258 | throw new IllegalArgumentException("inputChannel must not be null."); |
| 259 | } |
| 260 | |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 261 | nativeRegisterInputChannel(inputChannel, inputWindowHandle, false); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Jeff Brown | a41ca77 | 2010-08-11 14:46:32 -0700 | [diff] [blame] | 264 | /** |
| 265 | * Unregisters an input channel. |
| 266 | * @param inputChannel The input channel to unregister. |
| 267 | */ |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 268 | public void unregisterInputChannel(InputChannel inputChannel) { |
| 269 | if (inputChannel == null) { |
| 270 | throw new IllegalArgumentException("inputChannel must not be null."); |
| 271 | } |
| 272 | |
| 273 | nativeUnregisterInputChannel(inputChannel); |
| 274 | } |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 275 | |
| 276 | /** |
| 277 | * Sets an input filter that will receive all input events before they are dispatched. |
| 278 | * The input filter may then reinterpret input events or inject new ones. |
| 279 | * |
| 280 | * To ensure consistency, the input dispatcher automatically drops all events |
| 281 | * in progress whenever an input filter is installed or uninstalled. After an input |
| 282 | * filter is uninstalled, it can no longer send input events unless it is reinstalled. |
| 283 | * Any events it attempts to send after it has been uninstalled will be dropped. |
| 284 | * |
| 285 | * @param filter The input filter, or null to remove the current filter. |
| 286 | */ |
| 287 | public void setInputFilter(InputFilter filter) { |
| 288 | synchronized (mInputFilterLock) { |
| 289 | final InputFilter oldFilter = mInputFilter; |
| 290 | if (oldFilter == filter) { |
| 291 | return; // nothing to do |
| 292 | } |
| 293 | |
| 294 | if (oldFilter != null) { |
| 295 | mInputFilter = null; |
| 296 | mInputFilterHost.disconnectLocked(); |
| 297 | mInputFilterHost = null; |
| 298 | oldFilter.uninstall(); |
| 299 | } |
| 300 | |
| 301 | if (filter != null) { |
| 302 | mInputFilter = filter; |
| 303 | mInputFilterHost = new InputFilterHost(); |
| 304 | filter.install(mInputFilterHost); |
| 305 | } |
| 306 | |
| 307 | nativeSetInputFilterEnabled(filter != null); |
| 308 | } |
| 309 | } |
| 310 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 311 | /** |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 312 | * Injects an input event into the event system on behalf of an application. |
| 313 | * The synchronization mode determines whether the method blocks while waiting for |
| 314 | * input injection to proceed. |
| 315 | * |
| 316 | * {@link #INPUT_EVENT_INJECTION_SYNC_NONE} never blocks. Injection is asynchronous and |
| 317 | * is assumed always to be successful. |
| 318 | * |
| 319 | * {@link #INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT} waits for previous events to be |
| 320 | * dispatched so that the input dispatcher can determine whether input event injection will |
| 321 | * be permitted based on the current input focus. Does not wait for the input event to |
| 322 | * finish processing. |
| 323 | * |
| 324 | * {@link #INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_FINISH} waits for the input event to |
| 325 | * be completely processed. |
| 326 | * |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 327 | * @param event The event to inject. |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 328 | * @param injectorPid The pid of the injecting application. |
| 329 | * @param injectorUid The uid of the injecting application. |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 330 | * @param syncMode The synchronization mode. |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 331 | * @param timeoutMillis The injection timeout in milliseconds. |
| 332 | * @return One of the INPUT_EVENT_INJECTION_XXX constants. |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 333 | */ |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 334 | public int injectInputEvent(InputEvent event, int injectorPid, int injectorUid, |
| 335 | int syncMode, int timeoutMillis) { |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 336 | if (event == null) { |
| 337 | throw new IllegalArgumentException("event must not be null"); |
| 338 | } |
| 339 | if (injectorPid < 0 || injectorUid < 0) { |
| 340 | throw new IllegalArgumentException("injectorPid and injectorUid must not be negative."); |
| 341 | } |
| 342 | if (timeoutMillis <= 0) { |
| 343 | throw new IllegalArgumentException("timeoutMillis must be positive"); |
| 344 | } |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 345 | |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 346 | return nativeInjectInputEvent(event, injectorPid, injectorUid, syncMode, timeoutMillis, |
| 347 | WindowManagerPolicy.FLAG_DISABLE_KEY_REPEAT); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 348 | } |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 349 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 350 | /** |
| 351 | * Gets information about the input device with the specified id. |
| 352 | * @param id The device id. |
| 353 | * @return The input device or null if not found. |
| 354 | */ |
| 355 | public InputDevice getInputDevice(int deviceId) { |
| 356 | return nativeGetInputDevice(deviceId); |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * Gets the ids of all input devices in the system. |
| 361 | * @return The input device ids. |
| 362 | */ |
| 363 | public int[] getInputDeviceIds() { |
| 364 | return nativeGetInputDeviceIds(); |
| 365 | } |
| 366 | |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 367 | public void setInputWindows(InputWindow[] windows) { |
| 368 | nativeSetInputWindows(windows); |
| 369 | } |
| 370 | |
| 371 | public void setFocusedApplication(InputApplication application) { |
| 372 | nativeSetFocusedApplication(application); |
| 373 | } |
| 374 | |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 375 | public void setInputDispatchMode(boolean enabled, boolean frozen) { |
| 376 | nativeSetInputDispatchMode(enabled, frozen); |
| 377 | } |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 378 | |
| 379 | public void setSystemUiVisibility(int visibility) { |
| 380 | nativeSetSystemUiVisibility(visibility); |
| 381 | } |
| 382 | |
Jeff Brown | e650412 | 2010-09-27 14:52:15 -0700 | [diff] [blame] | 383 | /** |
| 384 | * Atomically transfers touch focus from one window to another as identified by |
| 385 | * their input channels. It is possible for multiple windows to have |
| 386 | * touch focus if they support split touch dispatch |
| 387 | * {@link android.view.WindowManager.LayoutParams#FLAG_SPLIT_TOUCH} but this |
| 388 | * method only transfers touch focus of the specified window without affecting |
| 389 | * other windows that may also have touch focus at the same time. |
| 390 | * @param fromChannel The channel of a window that currently has touch focus. |
| 391 | * @param toChannel The channel of the window that should receive touch focus in |
| 392 | * place of the first. |
| 393 | * @return True if the transfer was successful. False if the window with the |
| 394 | * specified channel did not actually have touch focus at the time of the request. |
| 395 | */ |
| 396 | public boolean transferTouchFocus(InputChannel fromChannel, InputChannel toChannel) { |
| 397 | if (fromChannel == null) { |
| 398 | throw new IllegalArgumentException("fromChannel must not be null."); |
| 399 | } |
| 400 | if (toChannel == null) { |
| 401 | throw new IllegalArgumentException("toChannel must not be null."); |
| 402 | } |
| 403 | return nativeTransferTouchFocus(fromChannel, toChannel); |
| 404 | } |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 405 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 406 | public void dump(PrintWriter pw) { |
Jeff Brown | e33348b | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 407 | String dumpStr = nativeDump(); |
| 408 | if (dumpStr != null) { |
| 409 | pw.println(dumpStr); |
| 410 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 411 | } |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 412 | |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 413 | private final class InputFilterHost implements InputFilter.Host { |
| 414 | private boolean mDisconnected; |
| 415 | |
| 416 | public void disconnectLocked() { |
| 417 | mDisconnected = true; |
| 418 | } |
| 419 | |
| 420 | public void sendInputEvent(InputEvent event, int policyFlags) { |
| 421 | if (event == null) { |
| 422 | throw new IllegalArgumentException("event must not be null"); |
| 423 | } |
| 424 | |
| 425 | synchronized (mInputFilterLock) { |
| 426 | if (!mDisconnected) { |
| 427 | nativeInjectInputEvent(event, 0, 0, INPUT_EVENT_INJECTION_SYNC_NONE, 0, |
| 428 | policyFlags | WindowManagerPolicy.FLAG_FILTERED); |
| 429 | } |
| 430 | } |
| 431 | } |
| 432 | } |
| 433 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 434 | /* |
| 435 | * Callbacks from native. |
| 436 | */ |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 437 | private final class Callbacks { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 438 | static final String TAG = "InputManager-Callbacks"; |
| 439 | |
| 440 | private static final boolean DEBUG_VIRTUAL_KEYS = false; |
| 441 | private static final String EXCLUDED_DEVICES_PATH = "etc/excluded-input-devices.xml"; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 442 | private static final String CALIBRATION_DIR_PATH = "usr/idc/"; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 443 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 444 | @SuppressWarnings("unused") |
Jeff Brown | 57c5937 | 2010-09-21 18:22:55 -0700 | [diff] [blame] | 445 | public void notifyConfigurationChanged(long whenNanos) { |
Jeff Brown | b09abc1 | 2011-01-13 21:08:27 -0800 | [diff] [blame] | 446 | mWindowManagerService.mInputMonitor.notifyConfigurationChanged(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | @SuppressWarnings("unused") |
| 450 | public void notifyLidSwitchChanged(long whenNanos, boolean lidOpen) { |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 451 | mWindowManagerService.mInputMonitor.notifyLidSwitchChanged(whenNanos, lidOpen); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | @SuppressWarnings("unused") |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 455 | public void notifyInputChannelBroken(InputWindowHandle inputWindowHandle) { |
| 456 | mWindowManagerService.mInputMonitor.notifyInputChannelBroken(inputWindowHandle); |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 457 | } |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 458 | |
| 459 | @SuppressWarnings("unused") |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 460 | public long notifyANR(InputApplicationHandle inputApplicationHandle, |
| 461 | InputWindowHandle inputWindowHandle) { |
| 462 | return mWindowManagerService.mInputMonitor.notifyANR( |
| 463 | inputApplicationHandle, inputWindowHandle); |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 464 | } |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 465 | |
| 466 | @SuppressWarnings("unused") |
| 467 | final boolean filterInputEvent(InputEvent event, int policyFlags) { |
| 468 | synchronized (mInputFilterLock) { |
| 469 | if (mInputFilter != null) { |
| 470 | mInputFilter.filterInputEvent(event, policyFlags); |
| 471 | return false; |
| 472 | } |
| 473 | } |
| 474 | event.recycle(); |
| 475 | return true; |
| 476 | } |
| 477 | |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 478 | @SuppressWarnings("unused") |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 479 | public int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags, boolean isScreenOn) { |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 480 | return mWindowManagerService.mInputMonitor.interceptKeyBeforeQueueing( |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 481 | event, policyFlags, isScreenOn); |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 482 | } |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame] | 483 | |
| 484 | @SuppressWarnings("unused") |
| 485 | public int interceptMotionBeforeQueueingWhenScreenOff(int policyFlags) { |
| 486 | return mWindowManagerService.mInputMonitor.interceptMotionBeforeQueueingWhenScreenOff( |
| 487 | policyFlags); |
| 488 | } |
| 489 | |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 490 | @SuppressWarnings("unused") |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 491 | public boolean interceptKeyBeforeDispatching(InputWindowHandle focus, |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 492 | KeyEvent event, int policyFlags) { |
| 493 | return mWindowManagerService.mInputMonitor.interceptKeyBeforeDispatching( |
| 494 | focus, event, policyFlags); |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | @SuppressWarnings("unused") |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 498 | public KeyEvent dispatchUnhandledKey(InputWindowHandle focus, |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 499 | KeyEvent event, int policyFlags) { |
| 500 | return mWindowManagerService.mInputMonitor.dispatchUnhandledKey( |
| 501 | focus, event, policyFlags); |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | @SuppressWarnings("unused") |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 505 | public boolean checkInjectEventsPermission(int injectorPid, int injectorUid) { |
| 506 | return mContext.checkPermission( |
| 507 | android.Manifest.permission.INJECT_EVENTS, injectorPid, injectorUid) |
| 508 | == PackageManager.PERMISSION_GRANTED; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | @SuppressWarnings("unused") |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 512 | public boolean filterTouchEvents() { |
| 513 | return mContext.getResources().getBoolean( |
| 514 | com.android.internal.R.bool.config_filterTouchEvents); |
| 515 | } |
| 516 | |
| 517 | @SuppressWarnings("unused") |
| 518 | public boolean filterJumpyTouchEvents() { |
| 519 | return mContext.getResources().getBoolean( |
| 520 | com.android.internal.R.bool.config_filterJumpyTouchEvents); |
| 521 | } |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 522 | |
| 523 | @SuppressWarnings("unused") |
| 524 | public int getVirtualKeyQuietTimeMillis() { |
| 525 | return mContext.getResources().getInteger( |
| 526 | com.android.internal.R.integer.config_virtualKeyQuietTimeMillis); |
| 527 | } |
| 528 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 529 | @SuppressWarnings("unused") |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 530 | public String[] getExcludedDeviceNames() { |
| 531 | ArrayList<String> names = new ArrayList<String>(); |
| 532 | |
| 533 | // Read partner-provided list of excluded input devices |
| 534 | XmlPullParser parser = null; |
| 535 | // Environment.getRootDirectory() is a fancy way of saying ANDROID_ROOT or "/system". |
| 536 | File confFile = new File(Environment.getRootDirectory(), EXCLUDED_DEVICES_PATH); |
| 537 | FileReader confreader = null; |
| 538 | try { |
| 539 | confreader = new FileReader(confFile); |
| 540 | parser = Xml.newPullParser(); |
| 541 | parser.setInput(confreader); |
| 542 | XmlUtils.beginDocument(parser, "devices"); |
| 543 | |
| 544 | while (true) { |
| 545 | XmlUtils.nextElement(parser); |
| 546 | if (!"device".equals(parser.getName())) { |
| 547 | break; |
| 548 | } |
| 549 | String name = parser.getAttributeValue(null, "name"); |
| 550 | if (name != null) { |
| 551 | names.add(name); |
| 552 | } |
| 553 | } |
| 554 | } catch (FileNotFoundException e) { |
| 555 | // It's ok if the file does not exist. |
| 556 | } catch (Exception e) { |
| 557 | Slog.e(TAG, "Exception while parsing '" + confFile.getAbsolutePath() + "'", e); |
| 558 | } finally { |
| 559 | try { if (confreader != null) confreader.close(); } catch (IOException e) { } |
| 560 | } |
| 561 | |
| 562 | return names.toArray(new String[names.size()]); |
| 563 | } |
Jeff Brown | a454767 | 2011-03-02 21:38:11 -0800 | [diff] [blame] | 564 | |
| 565 | @SuppressWarnings("unused") |
| 566 | public int getKeyRepeatTimeout() { |
| 567 | return ViewConfiguration.getKeyRepeatTimeout(); |
| 568 | } |
| 569 | |
| 570 | @SuppressWarnings("unused") |
| 571 | public int getKeyRepeatDelay() { |
| 572 | return ViewConfiguration.getKeyRepeatDelay(); |
| 573 | } |
| 574 | |
Jeff Brown | ae9fc03 | 2010-08-18 15:51:08 -0700 | [diff] [blame] | 575 | @SuppressWarnings("unused") |
| 576 | public int getMaxEventsPerSecond() { |
| 577 | int result = 0; |
| 578 | try { |
| 579 | result = Integer.parseInt(SystemProperties.get("windowsmgr.max_events_per_sec")); |
| 580 | } catch (NumberFormatException e) { |
| 581 | } |
| 582 | if (result < 1) { |
makarand.karvekar | 88dd6e6 | 2011-03-02 17:11:14 -0600 | [diff] [blame] | 583 | result = 55; |
Jeff Brown | ae9fc03 | 2010-08-18 15:51:08 -0700 | [diff] [blame] | 584 | } |
| 585 | return result; |
| 586 | } |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 587 | |
| 588 | @SuppressWarnings("unused") |
| 589 | public int getPointerLayer() { |
| 590 | return mWindowManagerService.mPolicy.windowTypeToLayerLw( |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 591 | WindowManager.LayoutParams.TYPE_POINTER) |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 592 | * WindowManagerService.TYPE_LAYER_MULTIPLIER |
| 593 | + WindowManagerService.TYPE_LAYER_OFFSET; |
| 594 | } |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 595 | |
| 596 | @SuppressWarnings("unused") |
| 597 | public PointerIcon getPointerIcon() { |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame^] | 598 | return PointerIcon.getDefaultIcon(mContext); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 599 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 600 | } |
| 601 | } |