| Jeff Brown | 46b9ac0a | 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 | |
| 17 | package com.android.server; |
| 18 | |
| 19 | import com.android.internal.util.XmlUtils; |
| Jeff Brown | 46b9ac0a | 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 | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 25 | import android.content.res.Configuration; |
| 26 | import android.os.Environment; |
| Jeff Brown | ae9fc03 | 2010-08-18 15:51:08 -0700 | [diff] [blame] | 27 | import android.os.SystemProperties; |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 28 | import android.util.Slog; |
| 29 | import android.util.Xml; |
| 30 | import android.view.InputChannel; |
| Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 31 | import android.view.InputDevice; |
| Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 32 | import android.view.InputEvent; |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 33 | import android.view.Surface; |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 34 | |
| 35 | import java.io.BufferedReader; |
| 36 | import java.io.File; |
| 37 | import java.io.FileInputStream; |
| 38 | import java.io.FileNotFoundException; |
| 39 | import java.io.FileReader; |
| 40 | import java.io.IOException; |
| 41 | import java.io.InputStreamReader; |
| 42 | import java.io.PrintWriter; |
| 43 | import java.util.ArrayList; |
| Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 44 | import java.util.Properties; |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 45 | |
| 46 | /* |
| 47 | * Wraps the C++ InputManager and provides its callbacks. |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 48 | */ |
| 49 | public class InputManager { |
| 50 | static final String TAG = "InputManager"; |
| 51 | |
| Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 52 | private static final boolean DEBUG = false; |
| 53 | |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 54 | private final Callbacks mCallbacks; |
| 55 | private final Context mContext; |
| 56 | private final WindowManagerService mWindowManagerService; |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 57 | |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 58 | private static native void nativeInit(Callbacks callbacks); |
| 59 | private static native void nativeStart(); |
| 60 | private static native void nativeSetDisplaySize(int displayId, int width, int height); |
| 61 | private static native void nativeSetDisplayOrientation(int displayId, int rotation); |
| 62 | |
| Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 63 | private static native int nativeGetScanCodeState(int deviceId, int sourceMask, |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 64 | int scanCode); |
| Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 65 | private static native int nativeGetKeyCodeState(int deviceId, int sourceMask, |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 66 | int keyCode); |
| Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 67 | private static native int nativeGetSwitchState(int deviceId, int sourceMask, |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 68 | int sw); |
| Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 69 | private static native boolean nativeHasKeys(int deviceId, int sourceMask, |
| 70 | int[] keyCodes, boolean[] keyExists); |
| Jeff Brown | a41ca77 | 2010-08-11 14:46:32 -0700 | [diff] [blame] | 71 | private static native void nativeRegisterInputChannel(InputChannel inputChannel, |
| 72 | boolean monitor); |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 73 | private static native void nativeUnregisterInputChannel(InputChannel inputChannel); |
| Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 74 | private static native int nativeInjectInputEvent(InputEvent event, |
| 75 | int injectorPid, int injectorUid, int syncMode, int timeoutMillis); |
| Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 76 | private static native void nativeSetInputWindows(InputWindow[] windows); |
| 77 | private static native void nativeSetInputDispatchMode(boolean enabled, boolean frozen); |
| 78 | private static native void nativeSetFocusedApplication(InputApplication application); |
| Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 79 | private static native InputDevice nativeGetInputDevice(int deviceId); |
| Jeff Brown | 57c5937 | 2010-09-21 18:22:55 -0700 | [diff] [blame] | 80 | private static native void nativeGetInputConfiguration(Configuration configuration); |
| Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 81 | private static native int[] nativeGetInputDeviceIds(); |
| Jeff Brown | e33348b | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 82 | private static native String nativeDump(); |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 83 | |
| Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 84 | // Input event injection constants defined in InputDispatcher.h. |
| 85 | static final int INPUT_EVENT_INJECTION_SUCCEEDED = 0; |
| 86 | static final int INPUT_EVENT_INJECTION_PERMISSION_DENIED = 1; |
| 87 | static final int INPUT_EVENT_INJECTION_FAILED = 2; |
| 88 | static final int INPUT_EVENT_INJECTION_TIMED_OUT = 3; |
| 89 | |
| Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 90 | // Input event injection synchronization modes defined in InputDispatcher.h |
| 91 | static final int INPUT_EVENT_INJECTION_SYNC_NONE = 0; |
| 92 | static final int INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT = 1; |
| 93 | static final int INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_FINISH = 2; |
| 94 | |
| Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 95 | // Key states (may be returned by queries about the current state of a |
| 96 | // particular key code, scan code or switch). |
| 97 | |
| 98 | /** The key state is unknown or the requested key itself is not supported. */ |
| 99 | public static final int KEY_STATE_UNKNOWN = -1; |
| 100 | |
| 101 | /** The key is up. /*/ |
| 102 | public static final int KEY_STATE_UP = 0; |
| 103 | |
| 104 | /** The key is down. */ |
| 105 | public static final int KEY_STATE_DOWN = 1; |
| 106 | |
| 107 | /** The key is down but is a virtual key press that is being emulated by the system. */ |
| 108 | public static final int KEY_STATE_VIRTUAL = 2; |
| 109 | |
| Jeff Brown | e33348b | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 110 | public InputManager(Context context, WindowManagerService windowManagerService) { |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 111 | this.mContext = context; |
| 112 | this.mWindowManagerService = windowManagerService; |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 113 | |
| 114 | this.mCallbacks = new Callbacks(); |
| 115 | |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 116 | init(); |
| 117 | } |
| 118 | |
| 119 | private void init() { |
| 120 | Slog.i(TAG, "Initializing input manager"); |
| 121 | nativeInit(mCallbacks); |
| 122 | } |
| 123 | |
| 124 | public void start() { |
| 125 | Slog.i(TAG, "Starting input manager"); |
| 126 | nativeStart(); |
| 127 | } |
| 128 | |
| 129 | public void setDisplaySize(int displayId, int width, int height) { |
| 130 | if (width <= 0 || height <= 0) { |
| 131 | throw new IllegalArgumentException("Invalid display id or dimensions."); |
| 132 | } |
| 133 | |
| Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 134 | if (DEBUG) { |
| 135 | Slog.d(TAG, "Setting display #" + displayId + " size to " + width + "x" + height); |
| 136 | } |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 137 | nativeSetDisplaySize(displayId, width, height); |
| 138 | } |
| 139 | |
| 140 | public void setDisplayOrientation(int displayId, int rotation) { |
| 141 | if (rotation < Surface.ROTATION_0 || rotation > Surface.ROTATION_270) { |
| 142 | throw new IllegalArgumentException("Invalid rotation."); |
| 143 | } |
| 144 | |
| Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 145 | if (DEBUG) { |
| 146 | Slog.d(TAG, "Setting display #" + displayId + " orientation to " + rotation); |
| 147 | } |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 148 | nativeSetDisplayOrientation(displayId, rotation); |
| 149 | } |
| 150 | |
| 151 | public void getInputConfiguration(Configuration config) { |
| 152 | if (config == null) { |
| 153 | throw new IllegalArgumentException("config must not be null."); |
| 154 | } |
| 155 | |
| Jeff Brown | 57c5937 | 2010-09-21 18:22:55 -0700 | [diff] [blame] | 156 | nativeGetInputConfiguration(config); |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 159 | /** |
| 160 | * Gets the current state of a key or button by key code. |
| 161 | * @param deviceId The input device id, or -1 to consult all devices. |
| 162 | * @param sourceMask The input sources to consult, or {@link InputDevice#SOURCE_ANY} to |
| 163 | * consider all input sources. An input device is consulted if at least one of its |
| 164 | * non-class input source bits matches the specified source mask. |
| 165 | * @param keyCode The key code to check. |
| 166 | * @return The key state. |
| 167 | */ |
| 168 | public int getKeyCodeState(int deviceId, int sourceMask, int keyCode) { |
| 169 | return nativeGetKeyCodeState(deviceId, sourceMask, keyCode); |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 172 | /** |
| 173 | * Gets the current state of a key or button by scan code. |
| 174 | * @param deviceId The input device id, or -1 to consult all devices. |
| 175 | * @param sourceMask The input sources to consult, or {@link InputDevice#SOURCE_ANY} to |
| 176 | * consider all input sources. An input device is consulted if at least one of its |
| 177 | * non-class input source bits matches the specified source mask. |
| 178 | * @param scanCode The scan code to check. |
| 179 | * @return The key state. |
| 180 | */ |
| 181 | public int getScanCodeState(int deviceId, int sourceMask, int scanCode) { |
| 182 | return nativeGetScanCodeState(deviceId, sourceMask, scanCode); |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 185 | /** |
| 186 | * Gets the current state of a switch by switch code. |
| 187 | * @param deviceId The input device id, or -1 to consult all devices. |
| 188 | * @param sourceMask The input sources to consult, or {@link InputDevice#SOURCE_ANY} to |
| 189 | * consider all input sources. An input device is consulted if at least one of its |
| 190 | * non-class input source bits matches the specified source mask. |
| 191 | * @param switchCode The switch code to check. |
| 192 | * @return The switch state. |
| 193 | */ |
| 194 | public int getSwitchState(int deviceId, int sourceMask, int switchCode) { |
| 195 | return nativeGetSwitchState(deviceId, sourceMask, switchCode); |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 198 | /** |
| 199 | * Determines whether the specified key codes are supported by a particular device. |
| 200 | * @param deviceId The input device id, or -1 to consult all devices. |
| 201 | * @param sourceMask The input sources to consult, or {@link InputDevice#SOURCE_ANY} to |
| 202 | * consider all input sources. An input device is consulted if at least one of its |
| 203 | * non-class input source bits matches the specified source mask. |
| 204 | * @param keyCodes The array of key codes to check. |
| 205 | * @param keyExists An array at least as large as keyCodes whose entries will be set |
| 206 | * to true or false based on the presence or absence of support for the corresponding |
| 207 | * key codes. |
| 208 | * @return True if the lookup was successful, false otherwise. |
| 209 | */ |
| 210 | public boolean hasKeys(int deviceId, int sourceMask, int[] keyCodes, boolean[] keyExists) { |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 211 | if (keyCodes == null) { |
| 212 | throw new IllegalArgumentException("keyCodes must not be null."); |
| 213 | } |
| Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 214 | if (keyExists == null || keyExists.length < keyCodes.length) { |
| 215 | throw new IllegalArgumentException("keyExists must not be null and must be at " |
| 216 | + "least as large as keyCodes."); |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 219 | return nativeHasKeys(deviceId, sourceMask, keyCodes, keyExists); |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| Jeff Brown | a41ca77 | 2010-08-11 14:46:32 -0700 | [diff] [blame] | 222 | /** |
| 223 | * Creates an input channel that will receive all input from the input dispatcher. |
| 224 | * @param inputChannelName The input channel name. |
| 225 | * @return The input channel. |
| 226 | */ |
| 227 | public InputChannel monitorInput(String inputChannelName) { |
| 228 | if (inputChannelName == null) { |
| 229 | throw new IllegalArgumentException("inputChannelName must not be null."); |
| 230 | } |
| 231 | |
| 232 | InputChannel[] inputChannels = InputChannel.openInputChannelPair(inputChannelName); |
| 233 | nativeRegisterInputChannel(inputChannels[0], true); |
| 234 | inputChannels[0].dispose(); // don't need to retain the Java object reference |
| 235 | return inputChannels[1]; |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Registers an input channel so that it can be used as an input event target. |
| 240 | * @param inputChannel The input channel to register. |
| 241 | */ |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 242 | public void registerInputChannel(InputChannel inputChannel) { |
| 243 | if (inputChannel == null) { |
| 244 | throw new IllegalArgumentException("inputChannel must not be null."); |
| 245 | } |
| 246 | |
| Jeff Brown | a41ca77 | 2010-08-11 14:46:32 -0700 | [diff] [blame] | 247 | nativeRegisterInputChannel(inputChannel, false); |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| Jeff Brown | a41ca77 | 2010-08-11 14:46:32 -0700 | [diff] [blame] | 250 | /** |
| 251 | * Unregisters an input channel. |
| 252 | * @param inputChannel The input channel to unregister. |
| 253 | */ |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 254 | public void unregisterInputChannel(InputChannel inputChannel) { |
| 255 | if (inputChannel == null) { |
| 256 | throw new IllegalArgumentException("inputChannel must not be null."); |
| 257 | } |
| 258 | |
| 259 | nativeUnregisterInputChannel(inputChannel); |
| 260 | } |
| 261 | |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 262 | /** |
| Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 263 | * Injects an input event into the event system on behalf of an application. |
| 264 | * The synchronization mode determines whether the method blocks while waiting for |
| 265 | * input injection to proceed. |
| 266 | * |
| 267 | * {@link #INPUT_EVENT_INJECTION_SYNC_NONE} never blocks. Injection is asynchronous and |
| 268 | * is assumed always to be successful. |
| 269 | * |
| 270 | * {@link #INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT} waits for previous events to be |
| 271 | * dispatched so that the input dispatcher can determine whether input event injection will |
| 272 | * be permitted based on the current input focus. Does not wait for the input event to |
| 273 | * finish processing. |
| 274 | * |
| 275 | * {@link #INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_FINISH} waits for the input event to |
| 276 | * be completely processed. |
| 277 | * |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 278 | * @param event The event to inject. |
| Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 279 | * @param injectorPid The pid of the injecting application. |
| 280 | * @param injectorUid The uid of the injecting application. |
| Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 281 | * @param syncMode The synchronization mode. |
| Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 282 | * @param timeoutMillis The injection timeout in milliseconds. |
| 283 | * @return One of the INPUT_EVENT_INJECTION_XXX constants. |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 284 | */ |
| Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 285 | public int injectInputEvent(InputEvent event, int injectorPid, int injectorUid, |
| 286 | int syncMode, int timeoutMillis) { |
| Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 287 | if (event == null) { |
| 288 | throw new IllegalArgumentException("event must not be null"); |
| 289 | } |
| 290 | if (injectorPid < 0 || injectorUid < 0) { |
| 291 | throw new IllegalArgumentException("injectorPid and injectorUid must not be negative."); |
| 292 | } |
| 293 | if (timeoutMillis <= 0) { |
| 294 | throw new IllegalArgumentException("timeoutMillis must be positive"); |
| 295 | } |
| Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 296 | |
| 297 | return nativeInjectInputEvent(event, injectorPid, injectorUid, syncMode, timeoutMillis); |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 300 | /** |
| 301 | * Gets information about the input device with the specified id. |
| 302 | * @param id The device id. |
| 303 | * @return The input device or null if not found. |
| 304 | */ |
| 305 | public InputDevice getInputDevice(int deviceId) { |
| 306 | return nativeGetInputDevice(deviceId); |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * Gets the ids of all input devices in the system. |
| 311 | * @return The input device ids. |
| 312 | */ |
| 313 | public int[] getInputDeviceIds() { |
| 314 | return nativeGetInputDeviceIds(); |
| 315 | } |
| 316 | |
| Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 317 | public void setInputWindows(InputWindow[] windows) { |
| 318 | nativeSetInputWindows(windows); |
| 319 | } |
| 320 | |
| 321 | public void setFocusedApplication(InputApplication application) { |
| 322 | nativeSetFocusedApplication(application); |
| 323 | } |
| 324 | |
| Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 325 | public void setInputDispatchMode(boolean enabled, boolean frozen) { |
| 326 | nativeSetInputDispatchMode(enabled, frozen); |
| 327 | } |
| 328 | |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 329 | public void dump(PrintWriter pw) { |
| Jeff Brown | e33348b | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 330 | String dumpStr = nativeDump(); |
| 331 | if (dumpStr != null) { |
| 332 | pw.println(dumpStr); |
| 333 | } |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | private static final class VirtualKeyDefinition { |
| 337 | public int scanCode; |
| 338 | |
| 339 | // configured position data, specified in display coords |
| 340 | public int centerX; |
| 341 | public int centerY; |
| 342 | public int width; |
| 343 | public int height; |
| 344 | } |
| 345 | |
| Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 346 | private static final class InputDeviceCalibration { |
| 347 | public String[] keys; |
| 348 | public String[] values; |
| 349 | } |
| 350 | |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 351 | /* |
| 352 | * Callbacks from native. |
| 353 | */ |
| 354 | private class Callbacks { |
| 355 | static final String TAG = "InputManager-Callbacks"; |
| 356 | |
| 357 | private static final boolean DEBUG_VIRTUAL_KEYS = false; |
| 358 | private static final String EXCLUDED_DEVICES_PATH = "etc/excluded-input-devices.xml"; |
| Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 359 | private static final String CALIBRATION_DIR_PATH = "usr/idc/"; |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 360 | |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 361 | @SuppressWarnings("unused") |
| Jeff Brown | 57c5937 | 2010-09-21 18:22:55 -0700 | [diff] [blame] | 362 | public void notifyConfigurationChanged(long whenNanos) { |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 363 | mWindowManagerService.sendNewConfiguration(); |
| 364 | } |
| 365 | |
| 366 | @SuppressWarnings("unused") |
| 367 | public void notifyLidSwitchChanged(long whenNanos, boolean lidOpen) { |
| Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 368 | mWindowManagerService.mInputMonitor.notifyLidSwitchChanged(whenNanos, lidOpen); |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | @SuppressWarnings("unused") |
| Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 372 | public void notifyInputChannelBroken(InputChannel inputChannel) { |
| Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 373 | mWindowManagerService.mInputMonitor.notifyInputChannelBroken(inputChannel); |
| Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 374 | } |
| Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 375 | |
| 376 | @SuppressWarnings("unused") |
| Jeff Brown | 519e024 | 2010-09-15 15:18:56 -0700 | [diff] [blame] | 377 | public long notifyANR(Object token, InputChannel inputChannel) { |
| 378 | return mWindowManagerService.mInputMonitor.notifyANR(token, inputChannel); |
| Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | @SuppressWarnings("unused") |
| Jeff Brown | eb9f7a0 | 2010-10-29 21:50:21 -0700 | [diff] [blame] | 382 | public int interceptKeyBeforeQueueing(long whenNanos, int action, int flags, |
| 383 | int keyCode, int scanCode, int policyFlags, boolean isScreenOn) { |
| Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 384 | return mWindowManagerService.mInputMonitor.interceptKeyBeforeQueueing( |
| Jeff Brown | eb9f7a0 | 2010-10-29 21:50:21 -0700 | [diff] [blame] | 385 | whenNanos, action, flags, keyCode, scanCode, policyFlags, isScreenOn); |
| Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | @SuppressWarnings("unused") |
| Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 389 | public boolean interceptKeyBeforeDispatching(InputChannel focus, int action, |
| Jeff Brown | eb9f7a0 | 2010-10-29 21:50:21 -0700 | [diff] [blame] | 390 | int flags, int keyCode, int scanCode, int metaState, int repeatCount, |
| 391 | int policyFlags) { |
| Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 392 | return mWindowManagerService.mInputMonitor.interceptKeyBeforeDispatching(focus, |
| Jeff Brown | eb9f7a0 | 2010-10-29 21:50:21 -0700 | [diff] [blame] | 393 | action, flags, keyCode, scanCode, metaState, repeatCount, policyFlags); |
| Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | @SuppressWarnings("unused") |
| 397 | public boolean checkInjectEventsPermission(int injectorPid, int injectorUid) { |
| 398 | return mContext.checkPermission( |
| 399 | android.Manifest.permission.INJECT_EVENTS, injectorPid, injectorUid) |
| 400 | == PackageManager.PERMISSION_GRANTED; |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | @SuppressWarnings("unused") |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 404 | public boolean filterTouchEvents() { |
| 405 | return mContext.getResources().getBoolean( |
| 406 | com.android.internal.R.bool.config_filterTouchEvents); |
| 407 | } |
| 408 | |
| 409 | @SuppressWarnings("unused") |
| 410 | public boolean filterJumpyTouchEvents() { |
| 411 | return mContext.getResources().getBoolean( |
| 412 | com.android.internal.R.bool.config_filterJumpyTouchEvents); |
| 413 | } |
| Jeff Brown | 6f71d0f | 2010-10-24 14:39:33 -0700 | [diff] [blame] | 414 | |
| 415 | @SuppressWarnings("unused") |
| 416 | public int getVirtualKeyQuietTimeMillis() { |
| 417 | return mContext.getResources().getInteger( |
| 418 | com.android.internal.R.integer.config_virtualKeyQuietTimeMillis); |
| 419 | } |
| 420 | |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 421 | @SuppressWarnings("unused") |
| 422 | public VirtualKeyDefinition[] getVirtualKeyDefinitions(String deviceName) { |
| 423 | ArrayList<VirtualKeyDefinition> keys = new ArrayList<VirtualKeyDefinition>(); |
| 424 | |
| 425 | try { |
| 426 | FileInputStream fis = new FileInputStream( |
| 427 | "/sys/board_properties/virtualkeys." + deviceName); |
| 428 | InputStreamReader isr = new InputStreamReader(fis); |
| 429 | BufferedReader br = new BufferedReader(isr, 2048); |
| 430 | String str = br.readLine(); |
| 431 | if (str != null) { |
| 432 | String[] it = str.split(":"); |
| 433 | if (DEBUG_VIRTUAL_KEYS) Slog.v(TAG, "***** VIRTUAL KEYS: " + it); |
| 434 | final int N = it.length-6; |
| 435 | for (int i=0; i<=N; i+=6) { |
| 436 | if (!"0x01".equals(it[i])) { |
| Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 437 | Slog.w(TAG, "Unknown virtual key type at elem #" |
| 438 | + i + ": " + it[i] + " for device " + deviceName); |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 439 | continue; |
| 440 | } |
| 441 | try { |
| 442 | VirtualKeyDefinition key = new VirtualKeyDefinition(); |
| 443 | key.scanCode = Integer.parseInt(it[i+1]); |
| 444 | key.centerX = Integer.parseInt(it[i+2]); |
| 445 | key.centerY = Integer.parseInt(it[i+3]); |
| 446 | key.width = Integer.parseInt(it[i+4]); |
| 447 | key.height = Integer.parseInt(it[i+5]); |
| 448 | if (DEBUG_VIRTUAL_KEYS) Slog.v(TAG, "Virtual key " |
| 449 | + key.scanCode + ": center=" + key.centerX + "," |
| 450 | + key.centerY + " size=" + key.width + "x" |
| 451 | + key.height); |
| 452 | keys.add(key); |
| 453 | } catch (NumberFormatException e) { |
| Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 454 | Slog.w(TAG, "Bad number in virtual key definition at region " |
| 455 | + i + " in: " + str + " for device " + deviceName, e); |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 456 | } |
| 457 | } |
| 458 | } |
| 459 | br.close(); |
| 460 | } catch (FileNotFoundException e) { |
| Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 461 | Slog.i(TAG, "No virtual keys found for device " + deviceName + "."); |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 462 | } catch (IOException e) { |
| Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 463 | Slog.w(TAG, "Error reading virtual keys for device " + deviceName + ".", e); |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | return keys.toArray(new VirtualKeyDefinition[keys.size()]); |
| 467 | } |
| 468 | |
| 469 | @SuppressWarnings("unused") |
| Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 470 | public InputDeviceCalibration getInputDeviceCalibration(String deviceName) { |
| 471 | // Calibration is specified as a sequence of colon-delimited key value pairs. |
| 472 | Properties properties = new Properties(); |
| 473 | File calibrationFile = new File(Environment.getRootDirectory(), |
| 474 | CALIBRATION_DIR_PATH + deviceName + ".idc"); |
| 475 | if (calibrationFile.exists()) { |
| 476 | try { |
| 477 | properties.load(new FileInputStream(calibrationFile)); |
| 478 | } catch (IOException ex) { |
| 479 | Slog.w(TAG, "Error reading input device calibration properties for device " |
| 480 | + deviceName + " from " + calibrationFile + ".", ex); |
| 481 | } |
| 482 | } else { |
| 483 | Slog.i(TAG, "No input device calibration properties found for device " |
| 484 | + deviceName + "."); |
| 485 | return null; |
| 486 | } |
| 487 | |
| 488 | InputDeviceCalibration calibration = new InputDeviceCalibration(); |
| 489 | calibration.keys = properties.keySet().toArray(new String[properties.size()]); |
| 490 | calibration.values = properties.values().toArray(new String[properties.size()]); |
| 491 | return calibration; |
| 492 | } |
| 493 | |
| 494 | @SuppressWarnings("unused") |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 495 | public String[] getExcludedDeviceNames() { |
| 496 | ArrayList<String> names = new ArrayList<String>(); |
| 497 | |
| 498 | // Read partner-provided list of excluded input devices |
| 499 | XmlPullParser parser = null; |
| 500 | // Environment.getRootDirectory() is a fancy way of saying ANDROID_ROOT or "/system". |
| 501 | File confFile = new File(Environment.getRootDirectory(), EXCLUDED_DEVICES_PATH); |
| 502 | FileReader confreader = null; |
| 503 | try { |
| 504 | confreader = new FileReader(confFile); |
| 505 | parser = Xml.newPullParser(); |
| 506 | parser.setInput(confreader); |
| 507 | XmlUtils.beginDocument(parser, "devices"); |
| 508 | |
| 509 | while (true) { |
| 510 | XmlUtils.nextElement(parser); |
| 511 | if (!"device".equals(parser.getName())) { |
| 512 | break; |
| 513 | } |
| 514 | String name = parser.getAttributeValue(null, "name"); |
| 515 | if (name != null) { |
| 516 | names.add(name); |
| 517 | } |
| 518 | } |
| 519 | } catch (FileNotFoundException e) { |
| 520 | // It's ok if the file does not exist. |
| 521 | } catch (Exception e) { |
| 522 | Slog.e(TAG, "Exception while parsing '" + confFile.getAbsolutePath() + "'", e); |
| 523 | } finally { |
| 524 | try { if (confreader != null) confreader.close(); } catch (IOException e) { } |
| 525 | } |
| 526 | |
| 527 | return names.toArray(new String[names.size()]); |
| 528 | } |
| Jeff Brown | ae9fc03 | 2010-08-18 15:51:08 -0700 | [diff] [blame] | 529 | |
| 530 | @SuppressWarnings("unused") |
| 531 | public int getMaxEventsPerSecond() { |
| 532 | int result = 0; |
| 533 | try { |
| 534 | result = Integer.parseInt(SystemProperties.get("windowsmgr.max_events_per_sec")); |
| 535 | } catch (NumberFormatException e) { |
| 536 | } |
| 537 | if (result < 1) { |
| Jeff Brown | 3d8c9bd | 2010-08-18 17:48:53 -0700 | [diff] [blame] | 538 | result = 60; |
| Jeff Brown | ae9fc03 | 2010-08-18 15:51:08 -0700 | [diff] [blame] | 539 | } |
| 540 | return result; |
| 541 | } |
| Jeff Brown | 46b9ac0a | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 542 | } |
| 543 | } |