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