Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Dianne Hackborn | a924dc0d | 2011-02-17 14:22:17 -0800 | [diff] [blame] | 17 | package com.android.server.wm; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 18 | |
| 19 | import com.android.internal.util.XmlUtils; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 20 | |
| 21 | import org.xmlpull.v1.XmlPullParser; |
| 22 | |
| 23 | import android.content.Context; |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 24 | import android.content.pm.PackageManager; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 25 | import android.content.res.Configuration; |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 26 | import android.content.res.Resources; |
| 27 | import android.content.res.TypedArray; |
| 28 | import android.content.res.XmlResourceParser; |
| 29 | import android.graphics.Bitmap; |
| 30 | import android.graphics.drawable.BitmapDrawable; |
| 31 | import android.graphics.drawable.Drawable; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 32 | import android.os.Environment; |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 33 | import android.os.Looper; |
| 34 | import android.os.MessageQueue; |
Jeff Brown | ae9fc03 | 2010-08-18 15:51:08 -0700 | [diff] [blame] | 35 | import android.os.SystemProperties; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 36 | import android.util.Slog; |
| 37 | import android.util.Xml; |
| 38 | import android.view.InputChannel; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 39 | import android.view.InputDevice; |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 40 | import android.view.InputEvent; |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 41 | import android.view.KeyEvent; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 42 | import android.view.Surface; |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 43 | import android.view.WindowManager; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 44 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 45 | import java.io.File; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 46 | import java.io.FileNotFoundException; |
| 47 | import java.io.FileReader; |
| 48 | import java.io.IOException; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 49 | import java.io.PrintWriter; |
| 50 | import java.util.ArrayList; |
| 51 | |
| 52 | /* |
| 53 | * Wraps the C++ InputManager and provides its callbacks. |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 54 | */ |
| 55 | public class InputManager { |
| 56 | static final String TAG = "InputManager"; |
| 57 | |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 58 | private static final boolean DEBUG = false; |
| 59 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 60 | private final Callbacks mCallbacks; |
| 61 | private final Context mContext; |
| 62 | private final WindowManagerService mWindowManagerService; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 63 | |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 64 | private static native void nativeInit(Callbacks callbacks, MessageQueue messageQueue); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 65 | private static native void nativeStart(); |
| 66 | private static native void nativeSetDisplaySize(int displayId, int width, int height); |
| 67 | private static native void nativeSetDisplayOrientation(int displayId, int rotation); |
| 68 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 69 | private static native int nativeGetScanCodeState(int deviceId, int sourceMask, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 70 | int scanCode); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 71 | private static native int nativeGetKeyCodeState(int deviceId, int sourceMask, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 72 | int keyCode); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 73 | private static native int nativeGetSwitchState(int deviceId, int sourceMask, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 74 | int sw); |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 75 | private static native boolean nativeHasKeys(int deviceId, int sourceMask, |
| 76 | int[] keyCodes, boolean[] keyExists); |
Jeff Brown | a41ca77 | 2010-08-11 14:46:32 -0700 | [diff] [blame] | 77 | private static native void nativeRegisterInputChannel(InputChannel inputChannel, |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 78 | InputWindowHandle inputWindowHandle, boolean monitor); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 79 | private static native void nativeUnregisterInputChannel(InputChannel inputChannel); |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 80 | private static native int nativeInjectInputEvent(InputEvent event, |
| 81 | int injectorPid, int injectorUid, int syncMode, int timeoutMillis); |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 82 | private static native void nativeSetInputWindows(InputWindow[] windows); |
| 83 | private static native void nativeSetInputDispatchMode(boolean enabled, boolean frozen); |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 84 | private static native void nativeSetSystemUiVisibility(int visibility); |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 85 | private static native void nativeSetFocusedApplication(InputApplication application); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 86 | private static native InputDevice nativeGetInputDevice(int deviceId); |
Jeff Brown | 57c5937 | 2010-09-21 18:22:55 -0700 | [diff] [blame] | 87 | private static native void nativeGetInputConfiguration(Configuration configuration); |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 88 | private static native int[] nativeGetInputDeviceIds(); |
Jeff Brown | e650412 | 2010-09-27 14:52:15 -0700 | [diff] [blame] | 89 | private static native boolean nativeTransferTouchFocus(InputChannel fromChannel, |
| 90 | InputChannel toChannel); |
Jeff Brown | e33348b | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 91 | private static native String nativeDump(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 92 | |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 93 | // Input event injection constants defined in InputDispatcher.h. |
| 94 | static final int INPUT_EVENT_INJECTION_SUCCEEDED = 0; |
| 95 | static final int INPUT_EVENT_INJECTION_PERMISSION_DENIED = 1; |
| 96 | static final int INPUT_EVENT_INJECTION_FAILED = 2; |
| 97 | static final int INPUT_EVENT_INJECTION_TIMED_OUT = 3; |
| 98 | |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 99 | // Input event injection synchronization modes defined in InputDispatcher.h |
| 100 | static final int INPUT_EVENT_INJECTION_SYNC_NONE = 0; |
| 101 | static final int INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT = 1; |
| 102 | static final int INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_FINISH = 2; |
| 103 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 104 | // Key states (may be returned by queries about the current state of a |
| 105 | // particular key code, scan code or switch). |
| 106 | |
| 107 | /** The key state is unknown or the requested key itself is not supported. */ |
| 108 | public static final int KEY_STATE_UNKNOWN = -1; |
| 109 | |
| 110 | /** The key is up. /*/ |
| 111 | public static final int KEY_STATE_UP = 0; |
| 112 | |
| 113 | /** The key is down. */ |
| 114 | public static final int KEY_STATE_DOWN = 1; |
| 115 | |
| 116 | /** The key is down but is a virtual key press that is being emulated by the system. */ |
| 117 | public static final int KEY_STATE_VIRTUAL = 2; |
| 118 | |
Jeff Brown | e33348b | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 119 | public InputManager(Context context, WindowManagerService windowManagerService) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 120 | this.mContext = context; |
| 121 | this.mWindowManagerService = windowManagerService; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 122 | this.mCallbacks = new Callbacks(); |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 123 | |
| 124 | Looper looper = windowManagerService.mH.getLooper(); |
| 125 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 126 | Slog.i(TAG, "Initializing input manager"); |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 127 | nativeInit(mCallbacks, looper.getQueue()); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | public void start() { |
| 131 | Slog.i(TAG, "Starting input manager"); |
| 132 | nativeStart(); |
| 133 | } |
| 134 | |
| 135 | public void setDisplaySize(int displayId, int width, int height) { |
| 136 | if (width <= 0 || height <= 0) { |
| 137 | throw new IllegalArgumentException("Invalid display id or dimensions."); |
| 138 | } |
| 139 | |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 140 | if (DEBUG) { |
| 141 | Slog.d(TAG, "Setting display #" + displayId + " size to " + width + "x" + height); |
| 142 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 143 | nativeSetDisplaySize(displayId, width, height); |
| 144 | } |
| 145 | |
| 146 | public void setDisplayOrientation(int displayId, int rotation) { |
| 147 | if (rotation < Surface.ROTATION_0 || rotation > Surface.ROTATION_270) { |
| 148 | throw new IllegalArgumentException("Invalid rotation."); |
| 149 | } |
| 150 | |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 151 | if (DEBUG) { |
| 152 | Slog.d(TAG, "Setting display #" + displayId + " orientation to " + rotation); |
| 153 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 154 | nativeSetDisplayOrientation(displayId, rotation); |
| 155 | } |
| 156 | |
| 157 | public void getInputConfiguration(Configuration config) { |
| 158 | if (config == null) { |
| 159 | throw new IllegalArgumentException("config must not be null."); |
| 160 | } |
| 161 | |
Jeff Brown | 57c5937 | 2010-09-21 18:22:55 -0700 | [diff] [blame] | 162 | nativeGetInputConfiguration(config); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 165 | /** |
| 166 | * Gets the current state of a key or button by key code. |
| 167 | * @param deviceId The input device id, or -1 to consult all devices. |
| 168 | * @param sourceMask The input sources to consult, or {@link InputDevice#SOURCE_ANY} to |
| 169 | * consider all input sources. An input device is consulted if at least one of its |
| 170 | * non-class input source bits matches the specified source mask. |
| 171 | * @param keyCode The key code to check. |
| 172 | * @return The key state. |
| 173 | */ |
| 174 | public int getKeyCodeState(int deviceId, int sourceMask, int keyCode) { |
| 175 | return nativeGetKeyCodeState(deviceId, sourceMask, keyCode); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 178 | /** |
| 179 | * Gets the current state of a key or button by scan code. |
| 180 | * @param deviceId The input device id, or -1 to consult all devices. |
| 181 | * @param sourceMask The input sources to consult, or {@link InputDevice#SOURCE_ANY} to |
| 182 | * consider all input sources. An input device is consulted if at least one of its |
| 183 | * non-class input source bits matches the specified source mask. |
| 184 | * @param scanCode The scan code to check. |
| 185 | * @return The key state. |
| 186 | */ |
| 187 | public int getScanCodeState(int deviceId, int sourceMask, int scanCode) { |
| 188 | return nativeGetScanCodeState(deviceId, sourceMask, scanCode); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 191 | /** |
| 192 | * Gets the current state of a switch by switch code. |
| 193 | * @param deviceId The input device id, or -1 to consult all devices. |
| 194 | * @param sourceMask The input sources to consult, or {@link InputDevice#SOURCE_ANY} to |
| 195 | * consider all input sources. An input device is consulted if at least one of its |
| 196 | * non-class input source bits matches the specified source mask. |
| 197 | * @param switchCode The switch code to check. |
| 198 | * @return The switch state. |
| 199 | */ |
| 200 | public int getSwitchState(int deviceId, int sourceMask, int switchCode) { |
| 201 | return nativeGetSwitchState(deviceId, sourceMask, switchCode); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 204 | /** |
| 205 | * Determines whether the specified key codes are supported by a particular device. |
| 206 | * @param deviceId The input device id, or -1 to consult all devices. |
| 207 | * @param sourceMask The input sources to consult, or {@link InputDevice#SOURCE_ANY} to |
| 208 | * consider all input sources. An input device is consulted if at least one of its |
| 209 | * non-class input source bits matches the specified source mask. |
| 210 | * @param keyCodes The array of key codes to check. |
| 211 | * @param keyExists An array at least as large as keyCodes whose entries will be set |
| 212 | * to true or false based on the presence or absence of support for the corresponding |
| 213 | * key codes. |
| 214 | * @return True if the lookup was successful, false otherwise. |
| 215 | */ |
| 216 | public boolean hasKeys(int deviceId, int sourceMask, int[] keyCodes, boolean[] keyExists) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 217 | if (keyCodes == null) { |
| 218 | throw new IllegalArgumentException("keyCodes must not be null."); |
| 219 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 220 | if (keyExists == null || keyExists.length < keyCodes.length) { |
| 221 | throw new IllegalArgumentException("keyExists must not be null and must be at " |
| 222 | + "least as large as keyCodes."); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 225 | return nativeHasKeys(deviceId, sourceMask, keyCodes, keyExists); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Jeff Brown | a41ca77 | 2010-08-11 14:46:32 -0700 | [diff] [blame] | 228 | /** |
| 229 | * Creates an input channel that will receive all input from the input dispatcher. |
| 230 | * @param inputChannelName The input channel name. |
| 231 | * @return The input channel. |
| 232 | */ |
| 233 | public InputChannel monitorInput(String inputChannelName) { |
| 234 | if (inputChannelName == null) { |
| 235 | throw new IllegalArgumentException("inputChannelName must not be null."); |
| 236 | } |
| 237 | |
| 238 | InputChannel[] inputChannels = InputChannel.openInputChannelPair(inputChannelName); |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 239 | nativeRegisterInputChannel(inputChannels[0], null, true); |
Jeff Brown | a41ca77 | 2010-08-11 14:46:32 -0700 | [diff] [blame] | 240 | inputChannels[0].dispose(); // don't need to retain the Java object reference |
| 241 | return inputChannels[1]; |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Registers an input channel so that it can be used as an input event target. |
| 246 | * @param inputChannel The input channel to register. |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 247 | * @param inputWindowHandle The handle of the input window associated with the |
| 248 | * input channel, or null if none. |
Jeff Brown | a41ca77 | 2010-08-11 14:46:32 -0700 | [diff] [blame] | 249 | */ |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 250 | public void registerInputChannel(InputChannel inputChannel, |
| 251 | InputWindowHandle inputWindowHandle) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 252 | if (inputChannel == null) { |
| 253 | throw new IllegalArgumentException("inputChannel must not be null."); |
| 254 | } |
| 255 | |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 256 | nativeRegisterInputChannel(inputChannel, inputWindowHandle, false); |
Jeff Brown | 46b9ac0 | 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 | 46b9ac0 | 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 | 46b9ac0 | 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 | 46b9ac0 | 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 | 46b9ac0 | 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 | 46b9ac0 | 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 | |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 334 | public void setInputDispatchMode(boolean enabled, boolean frozen) { |
| 335 | nativeSetInputDispatchMode(enabled, frozen); |
| 336 | } |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 337 | |
| 338 | public void setSystemUiVisibility(int visibility) { |
| 339 | nativeSetSystemUiVisibility(visibility); |
| 340 | } |
| 341 | |
Jeff Brown | e650412 | 2010-09-27 14:52:15 -0700 | [diff] [blame] | 342 | /** |
| 343 | * Atomically transfers touch focus from one window to another as identified by |
| 344 | * their input channels. It is possible for multiple windows to have |
| 345 | * touch focus if they support split touch dispatch |
| 346 | * {@link android.view.WindowManager.LayoutParams#FLAG_SPLIT_TOUCH} but this |
| 347 | * method only transfers touch focus of the specified window without affecting |
| 348 | * other windows that may also have touch focus at the same time. |
| 349 | * @param fromChannel The channel of a window that currently has touch focus. |
| 350 | * @param toChannel The channel of the window that should receive touch focus in |
| 351 | * place of the first. |
| 352 | * @return True if the transfer was successful. False if the window with the |
| 353 | * specified channel did not actually have touch focus at the time of the request. |
| 354 | */ |
| 355 | public boolean transferTouchFocus(InputChannel fromChannel, InputChannel toChannel) { |
| 356 | if (fromChannel == null) { |
| 357 | throw new IllegalArgumentException("fromChannel must not be null."); |
| 358 | } |
| 359 | if (toChannel == null) { |
| 360 | throw new IllegalArgumentException("toChannel must not be null."); |
| 361 | } |
| 362 | return nativeTransferTouchFocus(fromChannel, toChannel); |
| 363 | } |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 364 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 365 | public void dump(PrintWriter pw) { |
Jeff Brown | e33348b | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 366 | String dumpStr = nativeDump(); |
| 367 | if (dumpStr != null) { |
| 368 | pw.println(dumpStr); |
| 369 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 370 | } |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 371 | |
| 372 | private static final class PointerIcon { |
| 373 | public Bitmap bitmap; |
| 374 | public float hotSpotX; |
| 375 | public float hotSpotY; |
| 376 | |
| 377 | public static PointerIcon load(Resources resources, int resourceId) { |
| 378 | PointerIcon icon = new PointerIcon(); |
| 379 | |
| 380 | XmlResourceParser parser = resources.getXml(resourceId); |
| 381 | final int bitmapRes; |
| 382 | try { |
| 383 | XmlUtils.beginDocument(parser, "pointer-icon"); |
| 384 | |
| 385 | TypedArray a = resources.obtainAttributes( |
| 386 | parser, com.android.internal.R.styleable.PointerIcon); |
| 387 | bitmapRes = a.getResourceId(com.android.internal.R.styleable.PointerIcon_bitmap, 0); |
| 388 | icon.hotSpotX = a.getFloat(com.android.internal.R.styleable.PointerIcon_hotSpotX, 0); |
| 389 | icon.hotSpotY = a.getFloat(com.android.internal.R.styleable.PointerIcon_hotSpotY, 0); |
| 390 | a.recycle(); |
| 391 | } catch (Exception ex) { |
| 392 | Slog.e(TAG, "Exception parsing pointer icon resource.", ex); |
| 393 | return null; |
| 394 | } finally { |
| 395 | parser.close(); |
| 396 | } |
| 397 | |
| 398 | if (bitmapRes == 0) { |
| 399 | Slog.e(TAG, "<pointer-icon> is missing bitmap attribute"); |
| 400 | return null; |
| 401 | } |
| 402 | |
| 403 | Drawable drawable = resources.getDrawable(bitmapRes); |
| 404 | if (!(drawable instanceof BitmapDrawable)) { |
| 405 | Slog.e(TAG, "<pointer-icon> bitmap attribute must refer to a bitmap drawable"); |
| 406 | return null; |
| 407 | } |
| 408 | |
| 409 | icon.bitmap = ((BitmapDrawable)drawable).getBitmap(); |
| 410 | return icon; |
| 411 | } |
| 412 | } |
| 413 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 414 | /* |
| 415 | * Callbacks from native. |
| 416 | */ |
| 417 | private class Callbacks { |
| 418 | static final String TAG = "InputManager-Callbacks"; |
| 419 | |
| 420 | private static final boolean DEBUG_VIRTUAL_KEYS = false; |
| 421 | private static final String EXCLUDED_DEVICES_PATH = "etc/excluded-input-devices.xml"; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 422 | private static final String CALIBRATION_DIR_PATH = "usr/idc/"; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 423 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 424 | @SuppressWarnings("unused") |
Jeff Brown | 57c5937 | 2010-09-21 18:22:55 -0700 | [diff] [blame] | 425 | public void notifyConfigurationChanged(long whenNanos) { |
Jeff Brown | b09abc1 | 2011-01-13 21:08:27 -0800 | [diff] [blame] | 426 | mWindowManagerService.mInputMonitor.notifyConfigurationChanged(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | @SuppressWarnings("unused") |
| 430 | public void notifyLidSwitchChanged(long whenNanos, boolean lidOpen) { |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 431 | mWindowManagerService.mInputMonitor.notifyLidSwitchChanged(whenNanos, lidOpen); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | @SuppressWarnings("unused") |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 435 | public void notifyInputChannelBroken(InputWindowHandle inputWindowHandle) { |
| 436 | mWindowManagerService.mInputMonitor.notifyInputChannelBroken(inputWindowHandle); |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 437 | } |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 438 | |
| 439 | @SuppressWarnings("unused") |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 440 | public long notifyANR(InputApplicationHandle inputApplicationHandle, |
| 441 | InputWindowHandle inputWindowHandle) { |
| 442 | return mWindowManagerService.mInputMonitor.notifyANR( |
| 443 | inputApplicationHandle, inputWindowHandle); |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | @SuppressWarnings("unused") |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 447 | public int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags, boolean isScreenOn) { |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 448 | return mWindowManagerService.mInputMonitor.interceptKeyBeforeQueueing( |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 449 | event, policyFlags, isScreenOn); |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 450 | } |
Jeff Brown | 56194eb | 2011-03-02 19:23:13 -0800 | [diff] [blame^] | 451 | |
| 452 | @SuppressWarnings("unused") |
| 453 | public int interceptMotionBeforeQueueingWhenScreenOff(int policyFlags) { |
| 454 | return mWindowManagerService.mInputMonitor.interceptMotionBeforeQueueingWhenScreenOff( |
| 455 | policyFlags); |
| 456 | } |
| 457 | |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 458 | @SuppressWarnings("unused") |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 459 | public boolean interceptKeyBeforeDispatching(InputWindowHandle focus, |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 460 | KeyEvent event, int policyFlags) { |
| 461 | return mWindowManagerService.mInputMonitor.interceptKeyBeforeDispatching( |
| 462 | focus, event, policyFlags); |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | @SuppressWarnings("unused") |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 466 | public KeyEvent dispatchUnhandledKey(InputWindowHandle focus, |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 467 | KeyEvent event, int policyFlags) { |
| 468 | return mWindowManagerService.mInputMonitor.dispatchUnhandledKey( |
| 469 | focus, event, policyFlags); |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | @SuppressWarnings("unused") |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 473 | public boolean checkInjectEventsPermission(int injectorPid, int injectorUid) { |
| 474 | return mContext.checkPermission( |
| 475 | android.Manifest.permission.INJECT_EVENTS, injectorPid, injectorUid) |
| 476 | == PackageManager.PERMISSION_GRANTED; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | @SuppressWarnings("unused") |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 480 | public boolean filterTouchEvents() { |
| 481 | return mContext.getResources().getBoolean( |
| 482 | com.android.internal.R.bool.config_filterTouchEvents); |
| 483 | } |
| 484 | |
| 485 | @SuppressWarnings("unused") |
| 486 | public boolean filterJumpyTouchEvents() { |
| 487 | return mContext.getResources().getBoolean( |
| 488 | com.android.internal.R.bool.config_filterJumpyTouchEvents); |
| 489 | } |
Jeff Brown | fe50892 | 2011-01-18 15:10:10 -0800 | [diff] [blame] | 490 | |
| 491 | @SuppressWarnings("unused") |
| 492 | public int getVirtualKeyQuietTimeMillis() { |
| 493 | return mContext.getResources().getInteger( |
| 494 | com.android.internal.R.integer.config_virtualKeyQuietTimeMillis); |
| 495 | } |
| 496 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 497 | @SuppressWarnings("unused") |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 498 | public String[] getExcludedDeviceNames() { |
| 499 | ArrayList<String> names = new ArrayList<String>(); |
| 500 | |
| 501 | // Read partner-provided list of excluded input devices |
| 502 | XmlPullParser parser = null; |
| 503 | // Environment.getRootDirectory() is a fancy way of saying ANDROID_ROOT or "/system". |
| 504 | File confFile = new File(Environment.getRootDirectory(), EXCLUDED_DEVICES_PATH); |
| 505 | FileReader confreader = null; |
| 506 | try { |
| 507 | confreader = new FileReader(confFile); |
| 508 | parser = Xml.newPullParser(); |
| 509 | parser.setInput(confreader); |
| 510 | XmlUtils.beginDocument(parser, "devices"); |
| 511 | |
| 512 | while (true) { |
| 513 | XmlUtils.nextElement(parser); |
| 514 | if (!"device".equals(parser.getName())) { |
| 515 | break; |
| 516 | } |
| 517 | String name = parser.getAttributeValue(null, "name"); |
| 518 | if (name != null) { |
| 519 | names.add(name); |
| 520 | } |
| 521 | } |
| 522 | } catch (FileNotFoundException e) { |
| 523 | // It's ok if the file does not exist. |
| 524 | } catch (Exception e) { |
| 525 | Slog.e(TAG, "Exception while parsing '" + confFile.getAbsolutePath() + "'", e); |
| 526 | } finally { |
| 527 | try { if (confreader != null) confreader.close(); } catch (IOException e) { } |
| 528 | } |
| 529 | |
| 530 | return names.toArray(new String[names.size()]); |
| 531 | } |
Jeff Brown | ae9fc03 | 2010-08-18 15:51:08 -0700 | [diff] [blame] | 532 | |
| 533 | @SuppressWarnings("unused") |
| 534 | public int getMaxEventsPerSecond() { |
| 535 | int result = 0; |
| 536 | try { |
| 537 | result = Integer.parseInt(SystemProperties.get("windowsmgr.max_events_per_sec")); |
| 538 | } catch (NumberFormatException e) { |
| 539 | } |
| 540 | if (result < 1) { |
Jeff Brown | 3d8c9bd | 2010-08-18 17:48:53 -0700 | [diff] [blame] | 541 | result = 60; |
Jeff Brown | ae9fc03 | 2010-08-18 15:51:08 -0700 | [diff] [blame] | 542 | } |
| 543 | return result; |
| 544 | } |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 545 | |
| 546 | @SuppressWarnings("unused") |
| 547 | public int getPointerLayer() { |
| 548 | return mWindowManagerService.mPolicy.windowTypeToLayerLw( |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 549 | WindowManager.LayoutParams.TYPE_POINTER) |
Jeff Brown | 83c0968 | 2010-12-23 17:50:18 -0800 | [diff] [blame] | 550 | * WindowManagerService.TYPE_LAYER_MULTIPLIER |
| 551 | + WindowManagerService.TYPE_LAYER_OFFSET; |
| 552 | } |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 553 | |
| 554 | @SuppressWarnings("unused") |
| 555 | public PointerIcon getPointerIcon() { |
| 556 | return PointerIcon.load(mContext.getResources(), |
Jeff Brown | 909733c | 2011-01-13 21:51:44 -0800 | [diff] [blame] | 557 | com.android.internal.R.drawable.pointer_arrow_icon); |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 558 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 559 | } |
| 560 | } |