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