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