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 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 17 | package com.android.server.input; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 18 | |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 19 | import com.android.internal.R; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 20 | import com.android.internal.util.XmlUtils; |
Jeff Brown | 89ef072 | 2011-08-10 16:25:21 -0700 | [diff] [blame] | 21 | import com.android.server.Watchdog; |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 22 | import com.android.server.display.DisplayManagerService; |
| 23 | import com.android.server.display.DisplayViewport; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 24 | |
| 25 | import org.xmlpull.v1.XmlPullParser; |
| 26 | |
Jeff Brown | a3bc565 | 2012-04-17 11:42:25 -0700 | [diff] [blame] | 27 | import android.Manifest; |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 28 | import android.app.Notification; |
| 29 | import android.app.NotificationManager; |
| 30 | import android.app.PendingIntent; |
Jeff Brown | 5bbd4b4 | 2012-04-20 19:28:00 -0700 | [diff] [blame] | 31 | import android.bluetooth.BluetoothAdapter; |
| 32 | import android.bluetooth.BluetoothDevice; |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 33 | import android.content.BroadcastReceiver; |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 34 | import android.content.ComponentName; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 35 | import android.content.Context; |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 36 | import android.content.Intent; |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 37 | import android.content.IntentFilter; |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 38 | import android.content.pm.ActivityInfo; |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 39 | import android.content.pm.PackageManager; |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 40 | import android.content.pm.ResolveInfo; |
| 41 | import android.content.pm.PackageManager.NameNotFoundException; |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 42 | import android.content.res.Resources; |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 43 | import android.content.res.Resources.NotFoundException; |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 44 | import android.content.res.TypedArray; |
| 45 | import android.content.res.XmlResourceParser; |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 46 | import android.database.ContentObserver; |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 47 | import android.hardware.input.IInputDevicesChangedListener; |
Svetoslav Ganov | c9c9a48 | 2012-07-16 08:46:07 -0700 | [diff] [blame] | 48 | import android.hardware.input.IInputManager; |
Jeff Brown | ac14351 | 2012-04-05 18:57:33 -0700 | [diff] [blame] | 49 | import android.hardware.input.InputManager; |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 50 | import android.hardware.input.KeyboardLayout; |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 51 | import android.os.Binder; |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 52 | import android.os.Bundle; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 53 | import android.os.Environment; |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 54 | import android.os.Handler; |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 55 | import android.os.IBinder; |
Jeff Brown | a9d131c | 2012-09-20 16:48:17 -0700 | [diff] [blame] | 56 | import android.os.Looper; |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 57 | import android.os.Message; |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 58 | import android.os.MessageQueue; |
Jeff Brown | ac14351 | 2012-04-05 18:57:33 -0700 | [diff] [blame] | 59 | import android.os.Process; |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 60 | import android.os.RemoteException; |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame^] | 61 | import android.os.UserHandle; |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 62 | import android.provider.Settings; |
| 63 | import android.provider.Settings.SettingNotFoundException; |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 64 | import android.util.Log; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 65 | import android.util.Slog; |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 66 | import android.util.SparseArray; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 67 | import android.util.Xml; |
Svetoslav Ganov | c9c9a48 | 2012-07-16 08:46:07 -0700 | [diff] [blame] | 68 | import android.view.IInputFilter; |
| 69 | import android.view.IInputFilterHost; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 70 | import android.view.InputChannel; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 71 | import android.view.InputDevice; |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 72 | import android.view.InputEvent; |
Jeff Brown | 1f24510 | 2010-11-18 20:53:46 -0800 | [diff] [blame] | 73 | import android.view.KeyEvent; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 74 | import android.view.PointerIcon; |
Jeff Brown | a454767 | 2011-03-02 21:38:11 -0800 | [diff] [blame] | 75 | import android.view.ViewConfiguration; |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 76 | import android.view.WindowManagerPolicy; |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 77 | import android.widget.Toast; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 78 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 79 | import java.io.File; |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 80 | import java.io.FileDescriptor; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 81 | import java.io.FileNotFoundException; |
| 82 | import java.io.FileReader; |
| 83 | import java.io.IOException; |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 84 | import java.io.InputStreamReader; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 85 | import java.io.PrintWriter; |
| 86 | import java.util.ArrayList; |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 87 | import java.util.HashMap; |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 88 | import java.util.HashSet; |
Jeff Brown | a3bc565 | 2012-04-17 11:42:25 -0700 | [diff] [blame] | 89 | |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 90 | import libcore.io.Streams; |
Jeff Brown | a3bc565 | 2012-04-17 11:42:25 -0700 | [diff] [blame] | 91 | import libcore.util.Objects; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 92 | |
| 93 | /* |
| 94 | * Wraps the C++ InputManager and provides its callbacks. |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 95 | */ |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 96 | public class InputManagerService extends IInputManager.Stub |
| 97 | implements Watchdog.Monitor, DisplayManagerService.InputManagerFuncs { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 98 | static final String TAG = "InputManager"; |
Jeff Brown | 1b9ba57 | 2012-05-21 10:54:18 -0700 | [diff] [blame] | 99 | static final boolean DEBUG = false; |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 100 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 101 | private static final String EXCLUDED_DEVICES_PATH = "etc/excluded-input-devices.xml"; |
| 102 | |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 103 | private static final int MSG_DELIVER_INPUT_DEVICES_CHANGED = 1; |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 104 | private static final int MSG_SWITCH_KEYBOARD_LAYOUT = 2; |
| 105 | private static final int MSG_RELOAD_KEYBOARD_LAYOUTS = 3; |
| 106 | private static final int MSG_UPDATE_KEYBOARD_LAYOUTS = 4; |
| 107 | private static final int MSG_RELOAD_DEVICE_ALIASES = 5; |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 108 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 109 | // Pointer to native input manager service object. |
| 110 | private final int mPtr; |
| 111 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 112 | private final Context mContext; |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 113 | private final InputManagerHandler mHandler; |
Jeff Brown | a9d131c | 2012-09-20 16:48:17 -0700 | [diff] [blame] | 114 | |
| 115 | private WindowManagerCallbacks mWindowManagerCallbacks; |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 116 | private boolean mSystemReady; |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 117 | private NotificationManager mNotificationManager; |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 118 | |
Jeff Brown | a3bc565 | 2012-04-17 11:42:25 -0700 | [diff] [blame] | 119 | // Persistent data store. Must be locked each time during use. |
| 120 | private final PersistentDataStore mDataStore = new PersistentDataStore(); |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 121 | |
| 122 | // List of currently registered input devices changed listeners by process id. |
| 123 | private Object mInputDevicesLock = new Object(); |
| 124 | private boolean mInputDevicesChangedPending; // guarded by mInputDevicesLock |
| 125 | private InputDevice[] mInputDevices = new InputDevice[0]; |
| 126 | private final SparseArray<InputDevicesChangedListenerRecord> mInputDevicesChangedListeners = |
| 127 | new SparseArray<InputDevicesChangedListenerRecord>(); // guarded by mInputDevicesLock |
| 128 | private final ArrayList<InputDevicesChangedListenerRecord> |
| 129 | mTempInputDevicesChangedListenersToNotify = |
| 130 | new ArrayList<InputDevicesChangedListenerRecord>(); // handler thread only |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 131 | private final ArrayList<InputDevice> |
| 132 | mTempFullKeyboards = new ArrayList<InputDevice>(); // handler thread only |
| 133 | private boolean mKeyboardLayoutNotificationShown; |
| 134 | private PendingIntent mKeyboardLayoutIntent; |
| 135 | private Toast mSwitchedKeyboardLayoutToast; |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 136 | |
Jeff Brown | a47425a | 2012-04-13 04:09:27 -0700 | [diff] [blame] | 137 | // State for vibrator tokens. |
| 138 | private Object mVibratorLock = new Object(); |
| 139 | private HashMap<IBinder, VibratorToken> mVibratorTokens = |
| 140 | new HashMap<IBinder, VibratorToken>(); |
| 141 | private int mNextVibratorTokenValue; |
| 142 | |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 143 | // State for the currently installed input filter. |
| 144 | final Object mInputFilterLock = new Object(); |
Svetoslav Ganov | c9c9a48 | 2012-07-16 08:46:07 -0700 | [diff] [blame] | 145 | IInputFilter mInputFilter; // guarded by mInputFilterLock |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 146 | InputFilterHost mInputFilterHost; // guarded by mInputFilterLock |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 147 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 148 | private static native int nativeInit(InputManagerService service, |
| 149 | Context context, MessageQueue messageQueue); |
| 150 | private static native void nativeStart(int ptr); |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 151 | private static native void nativeSetDisplayViewport(int ptr, boolean external, |
| 152 | int displayId, int rotation, |
| 153 | int logicalLeft, int logicalTop, int logicalRight, int logicalBottom, |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 154 | int physicalLeft, int physicalTop, int physicalRight, int physicalBottom, |
| 155 | int deviceWidth, int deviceHeight); |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 156 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 157 | private static native int nativeGetScanCodeState(int ptr, |
| 158 | int deviceId, int sourceMask, int scanCode); |
| 159 | private static native int nativeGetKeyCodeState(int ptr, |
| 160 | int deviceId, int sourceMask, int keyCode); |
| 161 | private static native int nativeGetSwitchState(int ptr, |
| 162 | int deviceId, int sourceMask, int sw); |
| 163 | private static native boolean nativeHasKeys(int ptr, |
| 164 | int deviceId, int sourceMask, int[] keyCodes, boolean[] keyExists); |
| 165 | private static native void nativeRegisterInputChannel(int ptr, InputChannel inputChannel, |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 166 | InputWindowHandle inputWindowHandle, boolean monitor); |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 167 | private static native void nativeUnregisterInputChannel(int ptr, InputChannel inputChannel); |
| 168 | private static native void nativeSetInputFilterEnabled(int ptr, boolean enable); |
| 169 | private static native int nativeInjectInputEvent(int ptr, InputEvent event, |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 170 | int injectorPid, int injectorUid, int syncMode, int timeoutMillis, |
| 171 | int policyFlags); |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 172 | private static native void nativeSetInputWindows(int ptr, InputWindowHandle[] windowHandles); |
| 173 | private static native void nativeSetInputDispatchMode(int ptr, boolean enabled, boolean frozen); |
| 174 | private static native void nativeSetSystemUiVisibility(int ptr, int visibility); |
| 175 | private static native void nativeSetFocusedApplication(int ptr, |
| 176 | InputApplicationHandle application); |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 177 | private static native boolean nativeTransferTouchFocus(int ptr, |
| 178 | InputChannel fromChannel, InputChannel toChannel); |
| 179 | private static native void nativeSetPointerSpeed(int ptr, int speed); |
| 180 | private static native void nativeSetShowTouches(int ptr, boolean enabled); |
Jeff Brown | a47425a | 2012-04-13 04:09:27 -0700 | [diff] [blame] | 181 | private static native void nativeVibrate(int ptr, int deviceId, long[] pattern, |
| 182 | int repeat, int token); |
| 183 | private static native void nativeCancelVibrate(int ptr, int deviceId, int token); |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 184 | private static native void nativeReloadKeyboardLayouts(int ptr); |
Jeff Brown | 5bbd4b4 | 2012-04-20 19:28:00 -0700 | [diff] [blame] | 185 | private static native void nativeReloadDeviceAliases(int ptr); |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 186 | private static native String nativeDump(int ptr); |
| 187 | private static native void nativeMonitor(int ptr); |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 188 | |
Jeff Brown | ac14351 | 2012-04-05 18:57:33 -0700 | [diff] [blame] | 189 | // Input event injection constants defined in InputDispatcher.h. |
| 190 | private static final int INPUT_EVENT_INJECTION_SUCCEEDED = 0; |
| 191 | private static final int INPUT_EVENT_INJECTION_PERMISSION_DENIED = 1; |
| 192 | private static final int INPUT_EVENT_INJECTION_FAILED = 2; |
| 193 | private static final int INPUT_EVENT_INJECTION_TIMED_OUT = 3; |
| 194 | |
| 195 | // Maximum number of milliseconds to wait for input event injection. |
| 196 | private static final int INJECTION_TIMEOUT_MILLIS = 30 * 1000; |
| 197 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 198 | // Key states (may be returned by queries about the current state of a |
| 199 | // particular key code, scan code or switch). |
| 200 | |
| 201 | /** The key state is unknown or the requested key itself is not supported. */ |
| 202 | public static final int KEY_STATE_UNKNOWN = -1; |
| 203 | |
| 204 | /** The key is up. /*/ |
| 205 | public static final int KEY_STATE_UP = 0; |
| 206 | |
| 207 | /** The key is down. */ |
| 208 | public static final int KEY_STATE_DOWN = 1; |
| 209 | |
| 210 | /** The key is down but is a virtual key press that is being emulated by the system. */ |
| 211 | public static final int KEY_STATE_VIRTUAL = 2; |
| 212 | |
Jeff Brown | c458ce9 | 2012-04-30 14:58:40 -0700 | [diff] [blame] | 213 | /** Scan code: Mouse / trackball button. */ |
| 214 | public static final int BTN_MOUSE = 0x110; |
| 215 | |
| 216 | /** Switch code: Lid switch. When set, lid is shut. */ |
| 217 | public static final int SW_LID = 0x00; |
| 218 | |
| 219 | /** Switch code: Keypad slide. When set, keyboard is exposed. */ |
| 220 | public static final int SW_KEYPAD_SLIDE = 0x0a; |
| 221 | |
Jeff Brown | a9d131c | 2012-09-20 16:48:17 -0700 | [diff] [blame] | 222 | public InputManagerService(Context context, Handler handler) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 223 | this.mContext = context; |
Jeff Brown | a9d131c | 2012-09-20 16:48:17 -0700 | [diff] [blame] | 224 | this.mHandler = new InputManagerHandler(handler.getLooper()); |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 225 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 226 | Slog.i(TAG, "Initializing input manager"); |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 227 | mPtr = nativeInit(this, mContext, mHandler.getLooper().getQueue()); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 228 | } |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 229 | |
Jeff Brown | a9d131c | 2012-09-20 16:48:17 -0700 | [diff] [blame] | 230 | public void setWindowManagerCallbacks(WindowManagerCallbacks callbacks) { |
| 231 | mWindowManagerCallbacks = callbacks; |
| 232 | } |
| 233 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 234 | public void start() { |
| 235 | Slog.i(TAG, "Starting input manager"); |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 236 | nativeStart(mPtr); |
| 237 | |
| 238 | // Add ourself to the Watchdog monitors. |
| 239 | Watchdog.getInstance().addMonitor(this); |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 240 | |
| 241 | registerPointerSpeedSettingObserver(); |
Jeff Brown | daf4a12 | 2011-08-26 17:14:14 -0700 | [diff] [blame] | 242 | registerShowTouchesSettingObserver(); |
| 243 | |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 244 | updatePointerSpeedFromSettings(); |
Jeff Brown | daf4a12 | 2011-08-26 17:14:14 -0700 | [diff] [blame] | 245 | updateShowTouchesFromSettings(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 246 | } |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 247 | |
Matthew Xie | 9631314 | 2012-06-29 16:57:31 -0700 | [diff] [blame] | 248 | // TODO(BT) Pass in paramter for bluetooth system |
| 249 | public void systemReady() { |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 250 | if (DEBUG) { |
| 251 | Slog.d(TAG, "System ready."); |
| 252 | } |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 253 | mNotificationManager = (NotificationManager)mContext.getSystemService( |
| 254 | Context.NOTIFICATION_SERVICE); |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 255 | mSystemReady = true; |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 256 | |
| 257 | IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED); |
| 258 | filter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 259 | filter.addAction(Intent.ACTION_PACKAGE_CHANGED); |
| 260 | filter.addDataScheme("package"); |
| 261 | mContext.registerReceiver(new BroadcastReceiver() { |
| 262 | @Override |
| 263 | public void onReceive(Context context, Intent intent) { |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 264 | updateKeyboardLayouts(); |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 265 | } |
| 266 | }, filter, null, mHandler); |
Jeff Brown | 5bbd4b4 | 2012-04-20 19:28:00 -0700 | [diff] [blame] | 267 | |
| 268 | filter = new IntentFilter(BluetoothDevice.ACTION_ALIAS_CHANGED); |
| 269 | mContext.registerReceiver(new BroadcastReceiver() { |
| 270 | @Override |
| 271 | public void onReceive(Context context, Intent intent) { |
Jeff Brown | 5bbd4b4 | 2012-04-20 19:28:00 -0700 | [diff] [blame] | 272 | reloadDeviceAliases(); |
| 273 | } |
| 274 | }, filter, null, mHandler); |
| 275 | |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 276 | mHandler.sendEmptyMessage(MSG_RELOAD_DEVICE_ALIASES); |
| 277 | mHandler.sendEmptyMessage(MSG_UPDATE_KEYBOARD_LAYOUTS); |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | private void reloadKeyboardLayouts() { |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 281 | if (DEBUG) { |
| 282 | Slog.d(TAG, "Reloading keyboard layouts."); |
| 283 | } |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 284 | nativeReloadKeyboardLayouts(mPtr); |
| 285 | } |
| 286 | |
Jeff Brown | 5bbd4b4 | 2012-04-20 19:28:00 -0700 | [diff] [blame] | 287 | private void reloadDeviceAliases() { |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 288 | if (DEBUG) { |
| 289 | Slog.d(TAG, "Reloading device names."); |
| 290 | } |
Jeff Brown | 5bbd4b4 | 2012-04-20 19:28:00 -0700 | [diff] [blame] | 291 | nativeReloadDeviceAliases(mPtr); |
| 292 | } |
| 293 | |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 294 | @Override |
| 295 | public void setDisplayViewports(DisplayViewport defaultViewport, |
| 296 | DisplayViewport externalTouchViewport) { |
| 297 | if (defaultViewport.valid) { |
| 298 | setDisplayViewport(false, defaultViewport); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 299 | } |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 300 | |
| 301 | if (externalTouchViewport.valid) { |
| 302 | setDisplayViewport(true, externalTouchViewport); |
| 303 | } else if (defaultViewport.valid) { |
| 304 | setDisplayViewport(true, defaultViewport); |
Jeff Brown | b699726 | 2010-10-08 22:31:17 -0700 | [diff] [blame] | 305 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 306 | } |
Jeff Brown | 4ed8fe7 | 2012-08-30 18:18:29 -0700 | [diff] [blame] | 307 | |
Jeff Brown | d728bf5 | 2012-09-08 18:05:28 -0700 | [diff] [blame] | 308 | private void setDisplayViewport(boolean external, DisplayViewport viewport) { |
| 309 | nativeSetDisplayViewport(mPtr, external, |
| 310 | viewport.displayId, viewport.orientation, |
| 311 | viewport.logicalFrame.left, viewport.logicalFrame.top, |
| 312 | viewport.logicalFrame.right, viewport.logicalFrame.bottom, |
| 313 | viewport.physicalFrame.left, viewport.physicalFrame.top, |
Jeff Brown | 83d616a | 2012-09-09 20:33:43 -0700 | [diff] [blame] | 314 | viewport.physicalFrame.right, viewport.physicalFrame.bottom, |
| 315 | viewport.deviceWidth, viewport.deviceHeight); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 316 | } |
Jeff Brown | ac14351 | 2012-04-05 18:57:33 -0700 | [diff] [blame] | 317 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 318 | /** |
| 319 | * Gets the current state of a key or button by key code. |
| 320 | * @param deviceId The input device id, or -1 to consult all devices. |
| 321 | * @param sourceMask The input sources to consult, or {@link InputDevice#SOURCE_ANY} to |
| 322 | * consider all input sources. An input device is consulted if at least one of its |
| 323 | * non-class input source bits matches the specified source mask. |
| 324 | * @param keyCode The key code to check. |
| 325 | * @return The key state. |
| 326 | */ |
| 327 | public int getKeyCodeState(int deviceId, int sourceMask, int keyCode) { |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 328 | return nativeGetKeyCodeState(mPtr, deviceId, sourceMask, keyCode); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 331 | /** |
| 332 | * Gets the current state of a key or button by scan code. |
| 333 | * @param deviceId The input device id, or -1 to consult all devices. |
| 334 | * @param sourceMask The input sources to consult, or {@link InputDevice#SOURCE_ANY} to |
| 335 | * consider all input sources. An input device is consulted if at least one of its |
| 336 | * non-class input source bits matches the specified source mask. |
| 337 | * @param scanCode The scan code to check. |
| 338 | * @return The key state. |
| 339 | */ |
| 340 | public int getScanCodeState(int deviceId, int sourceMask, int scanCode) { |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 341 | return nativeGetScanCodeState(mPtr, deviceId, sourceMask, scanCode); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 344 | /** |
| 345 | * Gets the current state of a switch by switch code. |
| 346 | * @param deviceId The input device id, or -1 to consult all devices. |
| 347 | * @param sourceMask The input sources to consult, or {@link InputDevice#SOURCE_ANY} to |
| 348 | * consider all input sources. An input device is consulted if at least one of its |
| 349 | * non-class input source bits matches the specified source mask. |
| 350 | * @param switchCode The switch code to check. |
| 351 | * @return The switch state. |
| 352 | */ |
| 353 | public int getSwitchState(int deviceId, int sourceMask, int switchCode) { |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 354 | return nativeGetSwitchState(mPtr, deviceId, sourceMask, switchCode); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 355 | } |
| 356 | |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 357 | /** |
| 358 | * Determines whether the specified key codes are supported by a particular device. |
| 359 | * @param deviceId The input device id, or -1 to consult all devices. |
| 360 | * @param sourceMask The input sources to consult, or {@link InputDevice#SOURCE_ANY} to |
| 361 | * consider all input sources. An input device is consulted if at least one of its |
| 362 | * non-class input source bits matches the specified source mask. |
| 363 | * @param keyCodes The array of key codes to check. |
| 364 | * @param keyExists An array at least as large as keyCodes whose entries will be set |
| 365 | * to true or false based on the presence or absence of support for the corresponding |
| 366 | * key codes. |
| 367 | * @return True if the lookup was successful, false otherwise. |
| 368 | */ |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 369 | @Override // Binder call |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 370 | public boolean hasKeys(int deviceId, int sourceMask, int[] keyCodes, boolean[] keyExists) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 371 | if (keyCodes == null) { |
| 372 | throw new IllegalArgumentException("keyCodes must not be null."); |
| 373 | } |
Jeff Brown | 6d0fec2 | 2010-07-23 21:28:06 -0700 | [diff] [blame] | 374 | if (keyExists == null || keyExists.length < keyCodes.length) { |
| 375 | throw new IllegalArgumentException("keyExists must not be null and must be at " |
| 376 | + "least as large as keyCodes."); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 377 | } |
| 378 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 379 | return nativeHasKeys(mPtr, deviceId, sourceMask, keyCodes, keyExists); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Jeff Brown | a41ca77 | 2010-08-11 14:46:32 -0700 | [diff] [blame] | 382 | /** |
| 383 | * Creates an input channel that will receive all input from the input dispatcher. |
| 384 | * @param inputChannelName The input channel name. |
| 385 | * @return The input channel. |
| 386 | */ |
| 387 | public InputChannel monitorInput(String inputChannelName) { |
| 388 | if (inputChannelName == null) { |
| 389 | throw new IllegalArgumentException("inputChannelName must not be null."); |
| 390 | } |
| 391 | |
| 392 | InputChannel[] inputChannels = InputChannel.openInputChannelPair(inputChannelName); |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 393 | nativeRegisterInputChannel(mPtr, inputChannels[0], null, true); |
Jeff Brown | a41ca77 | 2010-08-11 14:46:32 -0700 | [diff] [blame] | 394 | inputChannels[0].dispose(); // don't need to retain the Java object reference |
| 395 | return inputChannels[1]; |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * Registers an input channel so that it can be used as an input event target. |
| 400 | * @param inputChannel The input channel to register. |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 401 | * @param inputWindowHandle The handle of the input window associated with the |
| 402 | * input channel, or null if none. |
Jeff Brown | a41ca77 | 2010-08-11 14:46:32 -0700 | [diff] [blame] | 403 | */ |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 404 | public void registerInputChannel(InputChannel inputChannel, |
| 405 | InputWindowHandle inputWindowHandle) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 406 | if (inputChannel == null) { |
| 407 | throw new IllegalArgumentException("inputChannel must not be null."); |
| 408 | } |
| 409 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 410 | nativeRegisterInputChannel(mPtr, inputChannel, inputWindowHandle, false); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Jeff Brown | a41ca77 | 2010-08-11 14:46:32 -0700 | [diff] [blame] | 413 | /** |
| 414 | * Unregisters an input channel. |
| 415 | * @param inputChannel The input channel to unregister. |
| 416 | */ |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 417 | public void unregisterInputChannel(InputChannel inputChannel) { |
| 418 | if (inputChannel == null) { |
| 419 | throw new IllegalArgumentException("inputChannel must not be null."); |
| 420 | } |
| 421 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 422 | nativeUnregisterInputChannel(mPtr, inputChannel); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 423 | } |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 424 | |
| 425 | /** |
| 426 | * Sets an input filter that will receive all input events before they are dispatched. |
| 427 | * The input filter may then reinterpret input events or inject new ones. |
| 428 | * |
| 429 | * To ensure consistency, the input dispatcher automatically drops all events |
| 430 | * in progress whenever an input filter is installed or uninstalled. After an input |
| 431 | * filter is uninstalled, it can no longer send input events unless it is reinstalled. |
| 432 | * Any events it attempts to send after it has been uninstalled will be dropped. |
| 433 | * |
| 434 | * @param filter The input filter, or null to remove the current filter. |
| 435 | */ |
Svetoslav Ganov | c9c9a48 | 2012-07-16 08:46:07 -0700 | [diff] [blame] | 436 | public void setInputFilter(IInputFilter filter) { |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 437 | synchronized (mInputFilterLock) { |
Svetoslav Ganov | c9c9a48 | 2012-07-16 08:46:07 -0700 | [diff] [blame] | 438 | final IInputFilter oldFilter = mInputFilter; |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 439 | if (oldFilter == filter) { |
| 440 | return; // nothing to do |
| 441 | } |
| 442 | |
| 443 | if (oldFilter != null) { |
| 444 | mInputFilter = null; |
| 445 | mInputFilterHost.disconnectLocked(); |
| 446 | mInputFilterHost = null; |
Svetoslav Ganov | c9c9a48 | 2012-07-16 08:46:07 -0700 | [diff] [blame] | 447 | try { |
| 448 | oldFilter.uninstall(); |
| 449 | } catch (RemoteException re) { |
| 450 | /* ignore */ |
| 451 | } |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | if (filter != null) { |
| 455 | mInputFilter = filter; |
| 456 | mInputFilterHost = new InputFilterHost(); |
Svetoslav Ganov | c9c9a48 | 2012-07-16 08:46:07 -0700 | [diff] [blame] | 457 | try { |
| 458 | filter.install(mInputFilterHost); |
| 459 | } catch (RemoteException re) { |
| 460 | /* ignore */ |
| 461 | } |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 462 | } |
| 463 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 464 | nativeSetInputFilterEnabled(mPtr, filter != null); |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 468 | @Override // Binder call |
Jeff Brown | ac14351 | 2012-04-05 18:57:33 -0700 | [diff] [blame] | 469 | public boolean injectInputEvent(InputEvent event, int mode) { |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 470 | if (event == null) { |
| 471 | throw new IllegalArgumentException("event must not be null"); |
| 472 | } |
Jeff Brown | ac14351 | 2012-04-05 18:57:33 -0700 | [diff] [blame] | 473 | if (mode != InputManager.INJECT_INPUT_EVENT_MODE_ASYNC |
| 474 | && mode != InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH |
| 475 | && mode != InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_RESULT) { |
| 476 | throw new IllegalArgumentException("mode is invalid"); |
Jeff Brown | 7fbdc84 | 2010-06-17 20:52:56 -0700 | [diff] [blame] | 477 | } |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 478 | |
Jeff Brown | ac14351 | 2012-04-05 18:57:33 -0700 | [diff] [blame] | 479 | final int pid = Binder.getCallingPid(); |
| 480 | final int uid = Binder.getCallingUid(); |
| 481 | final long ident = Binder.clearCallingIdentity(); |
| 482 | final int result; |
| 483 | try { |
| 484 | result = nativeInjectInputEvent(mPtr, event, pid, uid, mode, |
| 485 | INJECTION_TIMEOUT_MILLIS, WindowManagerPolicy.FLAG_DISABLE_KEY_REPEAT); |
| 486 | } finally { |
| 487 | Binder.restoreCallingIdentity(ident); |
| 488 | } |
| 489 | switch (result) { |
| 490 | case INPUT_EVENT_INJECTION_PERMISSION_DENIED: |
| 491 | Slog.w(TAG, "Input event injection from pid " + pid + " permission denied."); |
| 492 | throw new SecurityException( |
| 493 | "Injecting to another application requires INJECT_EVENTS permission"); |
| 494 | case INPUT_EVENT_INJECTION_SUCCEEDED: |
| 495 | return true; |
| 496 | case INPUT_EVENT_INJECTION_TIMED_OUT: |
| 497 | Slog.w(TAG, "Input event injection from pid " + pid + " timed out."); |
| 498 | return false; |
| 499 | case INPUT_EVENT_INJECTION_FAILED: |
| 500 | default: |
| 501 | Slog.w(TAG, "Input event injection from pid " + pid + " failed."); |
| 502 | return false; |
| 503 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 504 | } |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 505 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 506 | /** |
| 507 | * Gets information about the input device with the specified id. |
| 508 | * @param id The device id. |
| 509 | * @return The input device or null if not found. |
| 510 | */ |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 511 | @Override // Binder call |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 512 | public InputDevice getInputDevice(int deviceId) { |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 513 | synchronized (mInputDevicesLock) { |
| 514 | final int count = mInputDevices.length; |
| 515 | for (int i = 0; i < count; i++) { |
| 516 | final InputDevice inputDevice = mInputDevices[i]; |
| 517 | if (inputDevice.getId() == deviceId) { |
| 518 | return inputDevice; |
| 519 | } |
| 520 | } |
| 521 | } |
| 522 | return null; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 523 | } |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 524 | |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 525 | /** |
| 526 | * Gets the ids of all input devices in the system. |
| 527 | * @return The input device ids. |
| 528 | */ |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 529 | @Override // Binder call |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 530 | public int[] getInputDeviceIds() { |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 531 | synchronized (mInputDevicesLock) { |
| 532 | final int count = mInputDevices.length; |
| 533 | int[] ids = new int[count]; |
| 534 | for (int i = 0; i < count; i++) { |
| 535 | ids[i] = mInputDevices[i].getId(); |
| 536 | } |
| 537 | return ids; |
| 538 | } |
| 539 | } |
| 540 | |
Jeff Brown | daa3753 | 2012-05-01 15:54:03 -0700 | [diff] [blame] | 541 | /** |
| 542 | * Gets all input devices in the system. |
| 543 | * @return The array of input devices. |
| 544 | */ |
| 545 | public InputDevice[] getInputDevices() { |
| 546 | synchronized (mInputDevicesLock) { |
| 547 | return mInputDevices; |
| 548 | } |
| 549 | } |
| 550 | |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 551 | @Override // Binder call |
| 552 | public void registerInputDevicesChangedListener(IInputDevicesChangedListener listener) { |
| 553 | if (listener == null) { |
| 554 | throw new IllegalArgumentException("listener must not be null"); |
| 555 | } |
| 556 | |
| 557 | synchronized (mInputDevicesLock) { |
| 558 | int callingPid = Binder.getCallingPid(); |
| 559 | if (mInputDevicesChangedListeners.get(callingPid) != null) { |
| 560 | throw new SecurityException("The calling process has already " |
| 561 | + "registered an InputDevicesChangedListener."); |
| 562 | } |
| 563 | |
| 564 | InputDevicesChangedListenerRecord record = |
| 565 | new InputDevicesChangedListenerRecord(callingPid, listener); |
| 566 | try { |
| 567 | IBinder binder = listener.asBinder(); |
| 568 | binder.linkToDeath(record, 0); |
| 569 | } catch (RemoteException ex) { |
| 570 | // give up |
| 571 | throw new RuntimeException(ex); |
| 572 | } |
| 573 | |
| 574 | mInputDevicesChangedListeners.put(callingPid, record); |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | private void onInputDevicesChangedListenerDied(int pid) { |
| 579 | synchronized (mInputDevicesLock) { |
| 580 | mInputDevicesChangedListeners.remove(pid); |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | // Must be called on handler. |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 585 | private void deliverInputDevicesChanged(InputDevice[] oldInputDevices) { |
| 586 | // Scan for changes. |
| 587 | int numFullKeyboardsAdded = 0; |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 588 | mTempInputDevicesChangedListenersToNotify.clear(); |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 589 | mTempFullKeyboards.clear(); |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 590 | final int numListeners; |
| 591 | final int[] deviceIdAndGeneration; |
| 592 | synchronized (mInputDevicesLock) { |
| 593 | if (!mInputDevicesChangedPending) { |
| 594 | return; |
| 595 | } |
| 596 | mInputDevicesChangedPending = false; |
| 597 | |
| 598 | numListeners = mInputDevicesChangedListeners.size(); |
| 599 | for (int i = 0; i < numListeners; i++) { |
| 600 | mTempInputDevicesChangedListenersToNotify.add( |
| 601 | mInputDevicesChangedListeners.valueAt(i)); |
| 602 | } |
| 603 | |
| 604 | final int numDevices = mInputDevices.length; |
| 605 | deviceIdAndGeneration = new int[numDevices * 2]; |
| 606 | for (int i = 0; i < numDevices; i++) { |
| 607 | final InputDevice inputDevice = mInputDevices[i]; |
| 608 | deviceIdAndGeneration[i * 2] = inputDevice.getId(); |
| 609 | deviceIdAndGeneration[i * 2 + 1] = inputDevice.getGeneration(); |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 610 | |
Jeff Brown | 7e4ff4b | 2012-05-30 14:32:16 -0700 | [diff] [blame] | 611 | if (!inputDevice.isVirtual() && inputDevice.isFullKeyboard()) { |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 612 | if (!containsInputDeviceWithDescriptor(oldInputDevices, |
| 613 | inputDevice.getDescriptor())) { |
| 614 | mTempFullKeyboards.add(numFullKeyboardsAdded++, inputDevice); |
| 615 | } else { |
| 616 | mTempFullKeyboards.add(inputDevice); |
| 617 | } |
| 618 | } |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 619 | } |
| 620 | } |
| 621 | |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 622 | // Notify listeners. |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 623 | for (int i = 0; i < numListeners; i++) { |
| 624 | mTempInputDevicesChangedListenersToNotify.get(i).notifyInputDevicesChanged( |
| 625 | deviceIdAndGeneration); |
| 626 | } |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 627 | mTempInputDevicesChangedListenersToNotify.clear(); |
| 628 | |
| 629 | // Check for missing keyboard layouts. |
| 630 | if (mNotificationManager != null) { |
| 631 | final int numFullKeyboards = mTempFullKeyboards.size(); |
| 632 | boolean missingLayoutForExternalKeyboard = false; |
| 633 | boolean missingLayoutForExternalKeyboardAdded = false; |
| 634 | synchronized (mDataStore) { |
| 635 | for (int i = 0; i < numFullKeyboards; i++) { |
| 636 | final InputDevice inputDevice = mTempFullKeyboards.get(i); |
| 637 | if (mDataStore.getCurrentKeyboardLayout(inputDevice.getDescriptor()) == null) { |
| 638 | missingLayoutForExternalKeyboard = true; |
| 639 | if (i < numFullKeyboardsAdded) { |
| 640 | missingLayoutForExternalKeyboardAdded = true; |
| 641 | } |
| 642 | } |
| 643 | } |
| 644 | } |
| 645 | if (missingLayoutForExternalKeyboard) { |
| 646 | if (missingLayoutForExternalKeyboardAdded) { |
| 647 | showMissingKeyboardLayoutNotification(); |
| 648 | } |
| 649 | } else if (mKeyboardLayoutNotificationShown) { |
| 650 | hideMissingKeyboardLayoutNotification(); |
| 651 | } |
| 652 | } |
| 653 | mTempFullKeyboards.clear(); |
| 654 | } |
| 655 | |
| 656 | // Must be called on handler. |
| 657 | private void showMissingKeyboardLayoutNotification() { |
| 658 | if (!mKeyboardLayoutNotificationShown) { |
| 659 | if (mKeyboardLayoutIntent == null) { |
| 660 | final Intent intent = new Intent("android.settings.INPUT_METHOD_SETTINGS"); |
| 661 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
| 662 | | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED |
| 663 | | Intent.FLAG_ACTIVITY_CLEAR_TOP); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame^] | 664 | mKeyboardLayoutIntent = PendingIntent.getActivityAsUser(mContext, 0, |
| 665 | intent, 0, null, UserHandle.CURRENT); |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | Resources r = mContext.getResources(); |
| 669 | Notification notification = new Notification.Builder(mContext) |
| 670 | .setContentTitle(r.getString( |
| 671 | R.string.select_keyboard_layout_notification_title)) |
| 672 | .setContentText(r.getString( |
| 673 | R.string.select_keyboard_layout_notification_message)) |
| 674 | .setContentIntent(mKeyboardLayoutIntent) |
| 675 | .setSmallIcon(R.drawable.ic_settings_language) |
| 676 | .setPriority(Notification.PRIORITY_LOW) |
| 677 | .build(); |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame^] | 678 | mNotificationManager.notifyAsUser(null, |
| 679 | R.string.select_keyboard_layout_notification_title, |
| 680 | notification, UserHandle.ALL); |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 681 | mKeyboardLayoutNotificationShown = true; |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | // Must be called on handler. |
| 686 | private void hideMissingKeyboardLayoutNotification() { |
| 687 | if (mKeyboardLayoutNotificationShown) { |
| 688 | mKeyboardLayoutNotificationShown = false; |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame^] | 689 | mNotificationManager.cancelAsUser(null, |
| 690 | R.string.select_keyboard_layout_notification_title, |
| 691 | UserHandle.ALL); |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 692 | } |
| 693 | } |
| 694 | |
| 695 | // Must be called on handler. |
| 696 | private void updateKeyboardLayouts() { |
| 697 | // Scan all input devices state for keyboard layouts that have been uninstalled. |
| 698 | final HashSet<String> availableKeyboardLayouts = new HashSet<String>(); |
| 699 | visitAllKeyboardLayouts(new KeyboardLayoutVisitor() { |
| 700 | @Override |
| 701 | public void visitKeyboardLayout(Resources resources, |
| 702 | String descriptor, String label, String collection, int keyboardLayoutResId) { |
| 703 | availableKeyboardLayouts.add(descriptor); |
| 704 | } |
| 705 | }); |
| 706 | synchronized (mDataStore) { |
| 707 | try { |
| 708 | mDataStore.removeUninstalledKeyboardLayouts(availableKeyboardLayouts); |
| 709 | } finally { |
| 710 | mDataStore.saveIfNeeded(); |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | // Reload keyboard layouts. |
| 715 | reloadKeyboardLayouts(); |
| 716 | } |
| 717 | |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 718 | private static boolean containsInputDeviceWithDescriptor(InputDevice[] inputDevices, |
| 719 | String descriptor) { |
| 720 | final int numDevices = inputDevices.length; |
| 721 | for (int i = 0; i < numDevices; i++) { |
| 722 | final InputDevice inputDevice = inputDevices[i]; |
| 723 | if (inputDevice.getDescriptor().equals(descriptor)) { |
| 724 | return true; |
| 725 | } |
| 726 | } |
| 727 | return false; |
Jeff Brown | 8d60866 | 2010-08-30 03:02:23 -0700 | [diff] [blame] | 728 | } |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 729 | |
| 730 | @Override // Binder call |
| 731 | public KeyboardLayout[] getKeyboardLayouts() { |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 732 | final ArrayList<KeyboardLayout> list = new ArrayList<KeyboardLayout>(); |
| 733 | visitAllKeyboardLayouts(new KeyboardLayoutVisitor() { |
| 734 | @Override |
| 735 | public void visitKeyboardLayout(Resources resources, |
Jeff Brown | d9fec5d | 2012-05-17 16:01:54 -0700 | [diff] [blame] | 736 | String descriptor, String label, String collection, int keyboardLayoutResId) { |
| 737 | list.add(new KeyboardLayout(descriptor, label, collection)); |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 738 | } |
| 739 | }); |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 740 | return list.toArray(new KeyboardLayout[list.size()]); |
| 741 | } |
| 742 | |
| 743 | @Override // Binder call |
| 744 | public KeyboardLayout getKeyboardLayout(String keyboardLayoutDescriptor) { |
| 745 | if (keyboardLayoutDescriptor == null) { |
| 746 | throw new IllegalArgumentException("keyboardLayoutDescriptor must not be null"); |
| 747 | } |
| 748 | |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 749 | final KeyboardLayout[] result = new KeyboardLayout[1]; |
| 750 | visitKeyboardLayout(keyboardLayoutDescriptor, new KeyboardLayoutVisitor() { |
| 751 | @Override |
| 752 | public void visitKeyboardLayout(Resources resources, |
Jeff Brown | d9fec5d | 2012-05-17 16:01:54 -0700 | [diff] [blame] | 753 | String descriptor, String label, String collection, int keyboardLayoutResId) { |
| 754 | result[0] = new KeyboardLayout(descriptor, label, collection); |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 755 | } |
| 756 | }); |
| 757 | if (result[0] == null) { |
| 758 | Log.w(TAG, "Could not get keyboard layout with descriptor '" |
| 759 | + keyboardLayoutDescriptor + "'."); |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 760 | } |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 761 | return result[0]; |
| 762 | } |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 763 | |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 764 | private void visitAllKeyboardLayouts(KeyboardLayoutVisitor visitor) { |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 765 | final PackageManager pm = mContext.getPackageManager(); |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 766 | Intent intent = new Intent(InputManager.ACTION_QUERY_KEYBOARD_LAYOUTS); |
| 767 | for (ResolveInfo resolveInfo : pm.queryBroadcastReceivers(intent, |
| 768 | PackageManager.GET_META_DATA)) { |
| 769 | visitKeyboardLayoutsInPackage(pm, resolveInfo.activityInfo, null, visitor); |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 770 | } |
| 771 | } |
| 772 | |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 773 | private void visitKeyboardLayout(String keyboardLayoutDescriptor, |
| 774 | KeyboardLayoutVisitor visitor) { |
| 775 | KeyboardLayoutDescriptor d = KeyboardLayoutDescriptor.parse(keyboardLayoutDescriptor); |
| 776 | if (d != null) { |
| 777 | final PackageManager pm = mContext.getPackageManager(); |
| 778 | try { |
| 779 | ActivityInfo receiver = pm.getReceiverInfo( |
| 780 | new ComponentName(d.packageName, d.receiverName), |
| 781 | PackageManager.GET_META_DATA); |
| 782 | visitKeyboardLayoutsInPackage(pm, receiver, d.keyboardLayoutName, visitor); |
| 783 | } catch (NameNotFoundException ex) { |
| 784 | } |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | private void visitKeyboardLayoutsInPackage(PackageManager pm, ActivityInfo receiver, |
| 789 | String keyboardName, KeyboardLayoutVisitor visitor) { |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 790 | Bundle metaData = receiver.metaData; |
| 791 | if (metaData == null) { |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 792 | return; |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | int configResId = metaData.getInt(InputManager.META_DATA_KEYBOARD_LAYOUTS); |
| 796 | if (configResId == 0) { |
| 797 | Log.w(TAG, "Missing meta-data '" + InputManager.META_DATA_KEYBOARD_LAYOUTS |
| 798 | + "' on receiver " + receiver.packageName + "/" + receiver.name); |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 799 | return; |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 800 | } |
| 801 | |
Jeff Brown | d9fec5d | 2012-05-17 16:01:54 -0700 | [diff] [blame] | 802 | CharSequence receiverLabel = receiver.loadLabel(pm); |
| 803 | String collection = receiverLabel != null ? receiverLabel.toString() : ""; |
| 804 | |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 805 | try { |
| 806 | Resources resources = pm.getResourcesForApplication(receiver.applicationInfo); |
| 807 | XmlResourceParser parser = resources.getXml(configResId); |
| 808 | try { |
| 809 | XmlUtils.beginDocument(parser, "keyboard-layouts"); |
| 810 | |
| 811 | for (;;) { |
| 812 | XmlUtils.nextElement(parser); |
| 813 | String element = parser.getName(); |
| 814 | if (element == null) { |
| 815 | break; |
| 816 | } |
| 817 | if (element.equals("keyboard-layout")) { |
| 818 | TypedArray a = resources.obtainAttributes( |
| 819 | parser, com.android.internal.R.styleable.KeyboardLayout); |
| 820 | try { |
| 821 | String name = a.getString( |
| 822 | com.android.internal.R.styleable.KeyboardLayout_name); |
| 823 | String label = a.getString( |
| 824 | com.android.internal.R.styleable.KeyboardLayout_label); |
Jeff Brown | 2f09576 | 2012-05-10 21:29:33 -0700 | [diff] [blame] | 825 | int keyboardLayoutResId = a.getResourceId( |
| 826 | com.android.internal.R.styleable.KeyboardLayout_keyboardLayout, |
| 827 | 0); |
| 828 | if (name == null || label == null || keyboardLayoutResId == 0) { |
| 829 | Log.w(TAG, "Missing required 'name', 'label' or 'keyboardLayout' " |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 830 | + "attributes in keyboard layout " |
| 831 | + "resource from receiver " |
| 832 | + receiver.packageName + "/" + receiver.name); |
| 833 | } else { |
| 834 | String descriptor = KeyboardLayoutDescriptor.format( |
| 835 | receiver.packageName, receiver.name, name); |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 836 | if (keyboardName == null || name.equals(keyboardName)) { |
| 837 | visitor.visitKeyboardLayout(resources, descriptor, |
Jeff Brown | d9fec5d | 2012-05-17 16:01:54 -0700 | [diff] [blame] | 838 | label, collection, keyboardLayoutResId); |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 839 | } |
| 840 | } |
| 841 | } finally { |
| 842 | a.recycle(); |
| 843 | } |
| 844 | } else { |
| 845 | Log.w(TAG, "Skipping unrecognized element '" + element |
| 846 | + "' in keyboard layout resource from receiver " |
| 847 | + receiver.packageName + "/" + receiver.name); |
| 848 | } |
| 849 | } |
| 850 | } finally { |
| 851 | parser.close(); |
| 852 | } |
| 853 | } catch (Exception ex) { |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 854 | Log.w(TAG, "Could not parse keyboard layout resource from receiver " |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 855 | + receiver.packageName + "/" + receiver.name, ex); |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 856 | } |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | @Override // Binder call |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 860 | public String getCurrentKeyboardLayoutForInputDevice(String inputDeviceDescriptor) { |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 861 | if (inputDeviceDescriptor == null) { |
| 862 | throw new IllegalArgumentException("inputDeviceDescriptor must not be null"); |
| 863 | } |
| 864 | |
Jeff Brown | a3bc565 | 2012-04-17 11:42:25 -0700 | [diff] [blame] | 865 | synchronized (mDataStore) { |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 866 | return mDataStore.getCurrentKeyboardLayout(inputDeviceDescriptor); |
Jeff Brown | a3bc565 | 2012-04-17 11:42:25 -0700 | [diff] [blame] | 867 | } |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | @Override // Binder call |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 871 | public void setCurrentKeyboardLayoutForInputDevice(String inputDeviceDescriptor, |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 872 | String keyboardLayoutDescriptor) { |
| 873 | if (!checkCallingPermission(android.Manifest.permission.SET_KEYBOARD_LAYOUT, |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 874 | "setCurrentKeyboardLayoutForInputDevice()")) { |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 875 | throw new SecurityException("Requires SET_KEYBOARD_LAYOUT permission"); |
| 876 | } |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 877 | if (inputDeviceDescriptor == null) { |
| 878 | throw new IllegalArgumentException("inputDeviceDescriptor must not be null"); |
| 879 | } |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 880 | if (keyboardLayoutDescriptor == null) { |
| 881 | throw new IllegalArgumentException("keyboardLayoutDescriptor must not be null"); |
| 882 | } |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 883 | |
Jeff Brown | a3bc565 | 2012-04-17 11:42:25 -0700 | [diff] [blame] | 884 | synchronized (mDataStore) { |
| 885 | try { |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 886 | if (mDataStore.setCurrentKeyboardLayout( |
| 887 | inputDeviceDescriptor, keyboardLayoutDescriptor)) { |
| 888 | mHandler.sendEmptyMessage(MSG_RELOAD_KEYBOARD_LAYOUTS); |
| 889 | } |
Jeff Brown | a3bc565 | 2012-04-17 11:42:25 -0700 | [diff] [blame] | 890 | } finally { |
| 891 | mDataStore.saveIfNeeded(); |
| 892 | } |
| 893 | } |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 894 | } |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 895 | |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 896 | @Override // Binder call |
| 897 | public String[] getKeyboardLayoutsForInputDevice(String inputDeviceDescriptor) { |
| 898 | if (inputDeviceDescriptor == null) { |
| 899 | throw new IllegalArgumentException("inputDeviceDescriptor must not be null"); |
| 900 | } |
| 901 | |
| 902 | synchronized (mDataStore) { |
| 903 | return mDataStore.getKeyboardLayouts(inputDeviceDescriptor); |
| 904 | } |
| 905 | } |
| 906 | |
| 907 | @Override // Binder call |
| 908 | public void addKeyboardLayoutForInputDevice(String inputDeviceDescriptor, |
| 909 | String keyboardLayoutDescriptor) { |
| 910 | if (!checkCallingPermission(android.Manifest.permission.SET_KEYBOARD_LAYOUT, |
| 911 | "addKeyboardLayoutForInputDevice()")) { |
| 912 | throw new SecurityException("Requires SET_KEYBOARD_LAYOUT permission"); |
| 913 | } |
| 914 | if (inputDeviceDescriptor == null) { |
| 915 | throw new IllegalArgumentException("inputDeviceDescriptor must not be null"); |
| 916 | } |
| 917 | if (keyboardLayoutDescriptor == null) { |
| 918 | throw new IllegalArgumentException("keyboardLayoutDescriptor must not be null"); |
| 919 | } |
| 920 | |
| 921 | synchronized (mDataStore) { |
| 922 | try { |
| 923 | String oldLayout = mDataStore.getCurrentKeyboardLayout(inputDeviceDescriptor); |
| 924 | if (mDataStore.addKeyboardLayout(inputDeviceDescriptor, keyboardLayoutDescriptor) |
| 925 | && !Objects.equal(oldLayout, |
| 926 | mDataStore.getCurrentKeyboardLayout(inputDeviceDescriptor))) { |
| 927 | mHandler.sendEmptyMessage(MSG_RELOAD_KEYBOARD_LAYOUTS); |
| 928 | } |
| 929 | } finally { |
| 930 | mDataStore.saveIfNeeded(); |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 931 | } |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 932 | } |
| 933 | } |
| 934 | |
| 935 | @Override // Binder call |
| 936 | public void removeKeyboardLayoutForInputDevice(String inputDeviceDescriptor, |
| 937 | String keyboardLayoutDescriptor) { |
| 938 | if (!checkCallingPermission(android.Manifest.permission.SET_KEYBOARD_LAYOUT, |
| 939 | "removeKeyboardLayoutForInputDevice()")) { |
| 940 | throw new SecurityException("Requires SET_KEYBOARD_LAYOUT permission"); |
| 941 | } |
| 942 | if (inputDeviceDescriptor == null) { |
| 943 | throw new IllegalArgumentException("inputDeviceDescriptor must not be null"); |
| 944 | } |
| 945 | if (keyboardLayoutDescriptor == null) { |
| 946 | throw new IllegalArgumentException("keyboardLayoutDescriptor must not be null"); |
| 947 | } |
| 948 | |
| 949 | synchronized (mDataStore) { |
| 950 | try { |
| 951 | String oldLayout = mDataStore.getCurrentKeyboardLayout(inputDeviceDescriptor); |
| 952 | if (mDataStore.removeKeyboardLayout(inputDeviceDescriptor, |
| 953 | keyboardLayoutDescriptor) |
| 954 | && !Objects.equal(oldLayout, |
| 955 | mDataStore.getCurrentKeyboardLayout(inputDeviceDescriptor))) { |
| 956 | mHandler.sendEmptyMessage(MSG_RELOAD_KEYBOARD_LAYOUTS); |
| 957 | } |
| 958 | } finally { |
| 959 | mDataStore.saveIfNeeded(); |
| 960 | } |
| 961 | } |
| 962 | } |
| 963 | |
| 964 | public void switchKeyboardLayout(int deviceId, int direction) { |
| 965 | mHandler.obtainMessage(MSG_SWITCH_KEYBOARD_LAYOUT, deviceId, direction).sendToTarget(); |
| 966 | } |
| 967 | |
| 968 | // Must be called on handler. |
| 969 | private void handleSwitchKeyboardLayout(int deviceId, int direction) { |
| 970 | final InputDevice device = getInputDevice(deviceId); |
| 971 | final String inputDeviceDescriptor = device.getDescriptor(); |
| 972 | if (device != null) { |
| 973 | final boolean changed; |
| 974 | final String keyboardLayoutDescriptor; |
| 975 | synchronized (mDataStore) { |
| 976 | try { |
| 977 | changed = mDataStore.switchKeyboardLayout(inputDeviceDescriptor, direction); |
| 978 | keyboardLayoutDescriptor = mDataStore.getCurrentKeyboardLayout( |
| 979 | inputDeviceDescriptor); |
| 980 | } finally { |
| 981 | mDataStore.saveIfNeeded(); |
| 982 | } |
| 983 | } |
| 984 | |
| 985 | if (changed) { |
| 986 | if (mSwitchedKeyboardLayoutToast != null) { |
| 987 | mSwitchedKeyboardLayoutToast.cancel(); |
| 988 | mSwitchedKeyboardLayoutToast = null; |
| 989 | } |
| 990 | if (keyboardLayoutDescriptor != null) { |
| 991 | KeyboardLayout keyboardLayout = getKeyboardLayout(keyboardLayoutDescriptor); |
| 992 | if (keyboardLayout != null) { |
| 993 | mSwitchedKeyboardLayoutToast = Toast.makeText( |
| 994 | mContext, keyboardLayout.getLabel(), Toast.LENGTH_SHORT); |
| 995 | mSwitchedKeyboardLayoutToast.show(); |
| 996 | } |
| 997 | } |
| 998 | |
| 999 | reloadKeyboardLayouts(); |
| 1000 | } |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 1001 | } |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 1002 | } |
| 1003 | |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1004 | public void setInputWindows(InputWindowHandle[] windowHandles) { |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1005 | nativeSetInputWindows(mPtr, windowHandles); |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 1006 | } |
| 1007 | |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 1008 | public void setFocusedApplication(InputApplicationHandle application) { |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1009 | nativeSetFocusedApplication(mPtr, application); |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 1010 | } |
| 1011 | |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 1012 | public void setInputDispatchMode(boolean enabled, boolean frozen) { |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1013 | nativeSetInputDispatchMode(mPtr, enabled, frozen); |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 1014 | } |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 1015 | |
| 1016 | public void setSystemUiVisibility(int visibility) { |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1017 | nativeSetSystemUiVisibility(mPtr, visibility); |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 1018 | } |
| 1019 | |
Jeff Brown | e650412 | 2010-09-27 14:52:15 -0700 | [diff] [blame] | 1020 | /** |
| 1021 | * Atomically transfers touch focus from one window to another as identified by |
| 1022 | * their input channels. It is possible for multiple windows to have |
| 1023 | * touch focus if they support split touch dispatch |
| 1024 | * {@link android.view.WindowManager.LayoutParams#FLAG_SPLIT_TOUCH} but this |
| 1025 | * method only transfers touch focus of the specified window without affecting |
| 1026 | * other windows that may also have touch focus at the same time. |
| 1027 | * @param fromChannel The channel of a window that currently has touch focus. |
| 1028 | * @param toChannel The channel of the window that should receive touch focus in |
| 1029 | * place of the first. |
| 1030 | * @return True if the transfer was successful. False if the window with the |
| 1031 | * specified channel did not actually have touch focus at the time of the request. |
| 1032 | */ |
| 1033 | public boolean transferTouchFocus(InputChannel fromChannel, InputChannel toChannel) { |
| 1034 | if (fromChannel == null) { |
| 1035 | throw new IllegalArgumentException("fromChannel must not be null."); |
| 1036 | } |
| 1037 | if (toChannel == null) { |
| 1038 | throw new IllegalArgumentException("toChannel must not be null."); |
| 1039 | } |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1040 | return nativeTransferTouchFocus(mPtr, fromChannel, toChannel); |
Jeff Brown | e650412 | 2010-09-27 14:52:15 -0700 | [diff] [blame] | 1041 | } |
Jeff Brown | 05dc66a | 2011-03-02 14:41:58 -0800 | [diff] [blame] | 1042 | |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 1043 | @Override // Binder call |
Jeff Brown | ac14351 | 2012-04-05 18:57:33 -0700 | [diff] [blame] | 1044 | public void tryPointerSpeed(int speed) { |
| 1045 | if (!checkCallingPermission(android.Manifest.permission.SET_POINTER_SPEED, |
| 1046 | "tryPointerSpeed()")) { |
| 1047 | throw new SecurityException("Requires SET_POINTER_SPEED permission"); |
| 1048 | } |
| 1049 | |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 1050 | if (speed < InputManager.MIN_POINTER_SPEED || speed > InputManager.MAX_POINTER_SPEED) { |
| 1051 | throw new IllegalArgumentException("speed out of range"); |
| 1052 | } |
| 1053 | |
Jeff Brown | ac14351 | 2012-04-05 18:57:33 -0700 | [diff] [blame] | 1054 | setPointerSpeedUnchecked(speed); |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | public void updatePointerSpeedFromSettings() { |
Jeff Brown | ac14351 | 2012-04-05 18:57:33 -0700 | [diff] [blame] | 1058 | int speed = getPointerSpeedSetting(); |
| 1059 | setPointerSpeedUnchecked(speed); |
| 1060 | } |
| 1061 | |
| 1062 | private void setPointerSpeedUnchecked(int speed) { |
| 1063 | speed = Math.min(Math.max(speed, InputManager.MIN_POINTER_SPEED), |
| 1064 | InputManager.MAX_POINTER_SPEED); |
| 1065 | nativeSetPointerSpeed(mPtr, speed); |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 1066 | } |
| 1067 | |
| 1068 | private void registerPointerSpeedSettingObserver() { |
| 1069 | mContext.getContentResolver().registerContentObserver( |
| 1070 | Settings.System.getUriFor(Settings.System.POINTER_SPEED), true, |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1071 | new ContentObserver(mHandler) { |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 1072 | @Override |
| 1073 | public void onChange(boolean selfChange) { |
| 1074 | updatePointerSpeedFromSettings(); |
| 1075 | } |
| 1076 | }); |
| 1077 | } |
| 1078 | |
Jeff Brown | ac14351 | 2012-04-05 18:57:33 -0700 | [diff] [blame] | 1079 | private int getPointerSpeedSetting() { |
| 1080 | int speed = InputManager.DEFAULT_POINTER_SPEED; |
Jeff Brown | 1a84fd1 | 2011-06-02 01:26:32 -0700 | [diff] [blame] | 1081 | try { |
| 1082 | speed = Settings.System.getInt(mContext.getContentResolver(), |
| 1083 | Settings.System.POINTER_SPEED); |
| 1084 | } catch (SettingNotFoundException snfe) { |
| 1085 | } |
| 1086 | return speed; |
| 1087 | } |
| 1088 | |
Jeff Brown | daf4a12 | 2011-08-26 17:14:14 -0700 | [diff] [blame] | 1089 | public void updateShowTouchesFromSettings() { |
| 1090 | int setting = getShowTouchesSetting(0); |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1091 | nativeSetShowTouches(mPtr, setting != 0); |
Jeff Brown | daf4a12 | 2011-08-26 17:14:14 -0700 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | private void registerShowTouchesSettingObserver() { |
| 1095 | mContext.getContentResolver().registerContentObserver( |
| 1096 | Settings.System.getUriFor(Settings.System.SHOW_TOUCHES), true, |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1097 | new ContentObserver(mHandler) { |
Jeff Brown | daf4a12 | 2011-08-26 17:14:14 -0700 | [diff] [blame] | 1098 | @Override |
| 1099 | public void onChange(boolean selfChange) { |
| 1100 | updateShowTouchesFromSettings(); |
| 1101 | } |
| 1102 | }); |
| 1103 | } |
| 1104 | |
| 1105 | private int getShowTouchesSetting(int defaultValue) { |
| 1106 | int result = defaultValue; |
| 1107 | try { |
| 1108 | result = Settings.System.getInt(mContext.getContentResolver(), |
| 1109 | Settings.System.SHOW_TOUCHES); |
| 1110 | } catch (SettingNotFoundException snfe) { |
| 1111 | } |
| 1112 | return result; |
| 1113 | } |
| 1114 | |
Jeff Brown | a47425a | 2012-04-13 04:09:27 -0700 | [diff] [blame] | 1115 | // Binder call |
| 1116 | @Override |
| 1117 | public void vibrate(int deviceId, long[] pattern, int repeat, IBinder token) { |
| 1118 | if (repeat >= pattern.length) { |
| 1119 | throw new ArrayIndexOutOfBoundsException(); |
| 1120 | } |
| 1121 | |
| 1122 | VibratorToken v; |
| 1123 | synchronized (mVibratorLock) { |
| 1124 | v = mVibratorTokens.get(token); |
| 1125 | if (v == null) { |
| 1126 | v = new VibratorToken(deviceId, token, mNextVibratorTokenValue++); |
| 1127 | try { |
| 1128 | token.linkToDeath(v, 0); |
| 1129 | } catch (RemoteException ex) { |
| 1130 | // give up |
| 1131 | throw new RuntimeException(ex); |
| 1132 | } |
| 1133 | mVibratorTokens.put(token, v); |
| 1134 | } |
| 1135 | } |
| 1136 | |
| 1137 | synchronized (v) { |
| 1138 | v.mVibrating = true; |
| 1139 | nativeVibrate(mPtr, deviceId, pattern, repeat, v.mTokenValue); |
| 1140 | } |
| 1141 | } |
| 1142 | |
| 1143 | // Binder call |
| 1144 | @Override |
| 1145 | public void cancelVibrate(int deviceId, IBinder token) { |
| 1146 | VibratorToken v; |
| 1147 | synchronized (mVibratorLock) { |
| 1148 | v = mVibratorTokens.get(token); |
| 1149 | if (v == null || v.mDeviceId != deviceId) { |
| 1150 | return; // nothing to cancel |
| 1151 | } |
| 1152 | } |
| 1153 | |
| 1154 | cancelVibrateIfNeeded(v); |
| 1155 | } |
| 1156 | |
| 1157 | void onVibratorTokenDied(VibratorToken v) { |
| 1158 | synchronized (mVibratorLock) { |
| 1159 | mVibratorTokens.remove(v.mToken); |
| 1160 | } |
| 1161 | |
| 1162 | cancelVibrateIfNeeded(v); |
| 1163 | } |
| 1164 | |
| 1165 | private void cancelVibrateIfNeeded(VibratorToken v) { |
| 1166 | synchronized (v) { |
| 1167 | if (v.mVibrating) { |
| 1168 | nativeCancelVibrate(mPtr, v.mDeviceId, v.mTokenValue); |
| 1169 | v.mVibrating = false; |
| 1170 | } |
| 1171 | } |
| 1172 | } |
| 1173 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1174 | @Override |
| 1175 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Brown | a3bc565 | 2012-04-17 11:42:25 -0700 | [diff] [blame] | 1176 | if (mContext.checkCallingOrSelfPermission(Manifest.permission.DUMP) |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1177 | != PackageManager.PERMISSION_GRANTED) { |
| 1178 | pw.println("Permission Denial: can't dump InputManager from from pid=" |
| 1179 | + Binder.getCallingPid() |
| 1180 | + ", uid=" + Binder.getCallingUid()); |
| 1181 | return; |
| 1182 | } |
| 1183 | |
| 1184 | pw.println("INPUT MANAGER (dumpsys input)\n"); |
| 1185 | String dumpStr = nativeDump(mPtr); |
Jeff Brown | e33348b | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 1186 | if (dumpStr != null) { |
| 1187 | pw.println(dumpStr); |
| 1188 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1189 | } |
Jeff Brown | b4ff35d | 2011-01-02 16:37:43 -0800 | [diff] [blame] | 1190 | |
Jeff Brown | ac14351 | 2012-04-05 18:57:33 -0700 | [diff] [blame] | 1191 | private boolean checkCallingPermission(String permission, String func) { |
| 1192 | // Quick check: if the calling permission is me, it's all okay. |
| 1193 | if (Binder.getCallingPid() == Process.myPid()) { |
| 1194 | return true; |
| 1195 | } |
| 1196 | |
| 1197 | if (mContext.checkCallingPermission(permission) == PackageManager.PERMISSION_GRANTED) { |
| 1198 | return true; |
| 1199 | } |
| 1200 | String msg = "Permission Denial: " + func + " from pid=" |
| 1201 | + Binder.getCallingPid() |
| 1202 | + ", uid=" + Binder.getCallingUid() |
| 1203 | + " requires " + permission; |
| 1204 | Slog.w(TAG, msg); |
| 1205 | return false; |
| 1206 | } |
| 1207 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1208 | // Called by the heartbeat to ensure locks are not held indefinitely (for deadlock detection). |
Jeff Brown | 89ef072 | 2011-08-10 16:25:21 -0700 | [diff] [blame] | 1209 | public void monitor() { |
| 1210 | synchronized (mInputFilterLock) { } |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1211 | nativeMonitor(mPtr); |
Jeff Brown | 89ef072 | 2011-08-10 16:25:21 -0700 | [diff] [blame] | 1212 | } |
| 1213 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1214 | // Native callback. |
| 1215 | private void notifyConfigurationChanged(long whenNanos) { |
Jeff Brown | a9d131c | 2012-09-20 16:48:17 -0700 | [diff] [blame] | 1216 | mWindowManagerCallbacks.notifyConfigurationChanged(); |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1217 | } |
| 1218 | |
| 1219 | // Native callback. |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 1220 | private void notifyInputDevicesChanged(InputDevice[] inputDevices) { |
| 1221 | synchronized (mInputDevicesLock) { |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 1222 | if (!mInputDevicesChangedPending) { |
| 1223 | mInputDevicesChangedPending = true; |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 1224 | mHandler.obtainMessage(MSG_DELIVER_INPUT_DEVICES_CHANGED, |
| 1225 | mInputDevices).sendToTarget(); |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 1226 | } |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 1227 | |
| 1228 | mInputDevices = inputDevices; |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 1229 | } |
| 1230 | } |
| 1231 | |
| 1232 | // Native callback. |
Jeff Brown | 5338428 | 2012-08-20 20:16:01 -0700 | [diff] [blame] | 1233 | private void notifySwitch(long whenNanos, int switchCode, int switchValue) { |
| 1234 | switch (switchCode) { |
| 1235 | case SW_LID: |
Jeff Brown | a9d131c | 2012-09-20 16:48:17 -0700 | [diff] [blame] | 1236 | mWindowManagerCallbacks.notifyLidSwitchChanged(whenNanos, switchValue == 0); |
Jeff Brown | 5338428 | 2012-08-20 20:16:01 -0700 | [diff] [blame] | 1237 | break; |
| 1238 | } |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1239 | } |
| 1240 | |
| 1241 | // Native callback. |
| 1242 | private void notifyInputChannelBroken(InputWindowHandle inputWindowHandle) { |
Jeff Brown | a9d131c | 2012-09-20 16:48:17 -0700 | [diff] [blame] | 1243 | mWindowManagerCallbacks.notifyInputChannelBroken(inputWindowHandle); |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | // Native callback. |
| 1247 | private long notifyANR(InputApplicationHandle inputApplicationHandle, |
| 1248 | InputWindowHandle inputWindowHandle) { |
Jeff Brown | a9d131c | 2012-09-20 16:48:17 -0700 | [diff] [blame] | 1249 | return mWindowManagerCallbacks.notifyANR(inputApplicationHandle, inputWindowHandle); |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1250 | } |
| 1251 | |
| 1252 | // Native callback. |
| 1253 | final boolean filterInputEvent(InputEvent event, int policyFlags) { |
| 1254 | synchronized (mInputFilterLock) { |
| 1255 | if (mInputFilter != null) { |
Svetoslav Ganov | c9c9a48 | 2012-07-16 08:46:07 -0700 | [diff] [blame] | 1256 | try { |
| 1257 | mInputFilter.filterInputEvent(event, policyFlags); |
| 1258 | } catch (RemoteException e) { |
| 1259 | /* ignore */ |
| 1260 | } |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1261 | return false; |
| 1262 | } |
| 1263 | } |
| 1264 | event.recycle(); |
| 1265 | return true; |
| 1266 | } |
| 1267 | |
| 1268 | // Native callback. |
| 1269 | private int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags, boolean isScreenOn) { |
Jeff Brown | a9d131c | 2012-09-20 16:48:17 -0700 | [diff] [blame] | 1270 | return mWindowManagerCallbacks.interceptKeyBeforeQueueing( |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1271 | event, policyFlags, isScreenOn); |
| 1272 | } |
| 1273 | |
| 1274 | // Native callback. |
| 1275 | private int interceptMotionBeforeQueueingWhenScreenOff(int policyFlags) { |
Jeff Brown | a9d131c | 2012-09-20 16:48:17 -0700 | [diff] [blame] | 1276 | return mWindowManagerCallbacks.interceptMotionBeforeQueueingWhenScreenOff(policyFlags); |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1277 | } |
| 1278 | |
| 1279 | // Native callback. |
| 1280 | private long interceptKeyBeforeDispatching(InputWindowHandle focus, |
| 1281 | KeyEvent event, int policyFlags) { |
Jeff Brown | a9d131c | 2012-09-20 16:48:17 -0700 | [diff] [blame] | 1282 | return mWindowManagerCallbacks.interceptKeyBeforeDispatching(focus, event, policyFlags); |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1283 | } |
| 1284 | |
| 1285 | // Native callback. |
| 1286 | private KeyEvent dispatchUnhandledKey(InputWindowHandle focus, |
| 1287 | KeyEvent event, int policyFlags) { |
Jeff Brown | a9d131c | 2012-09-20 16:48:17 -0700 | [diff] [blame] | 1288 | return mWindowManagerCallbacks.dispatchUnhandledKey(focus, event, policyFlags); |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1289 | } |
| 1290 | |
| 1291 | // Native callback. |
| 1292 | private boolean checkInjectEventsPermission(int injectorPid, int injectorUid) { |
| 1293 | return mContext.checkPermission(android.Manifest.permission.INJECT_EVENTS, |
| 1294 | injectorPid, injectorUid) == PackageManager.PERMISSION_GRANTED; |
| 1295 | } |
| 1296 | |
| 1297 | // Native callback. |
| 1298 | private int getVirtualKeyQuietTimeMillis() { |
| 1299 | return mContext.getResources().getInteger( |
| 1300 | com.android.internal.R.integer.config_virtualKeyQuietTimeMillis); |
| 1301 | } |
| 1302 | |
| 1303 | // Native callback. |
| 1304 | private String[] getExcludedDeviceNames() { |
| 1305 | ArrayList<String> names = new ArrayList<String>(); |
| 1306 | |
| 1307 | // Read partner-provided list of excluded input devices |
| 1308 | XmlPullParser parser = null; |
| 1309 | // Environment.getRootDirectory() is a fancy way of saying ANDROID_ROOT or "/system". |
| 1310 | File confFile = new File(Environment.getRootDirectory(), EXCLUDED_DEVICES_PATH); |
| 1311 | FileReader confreader = null; |
| 1312 | try { |
| 1313 | confreader = new FileReader(confFile); |
| 1314 | parser = Xml.newPullParser(); |
| 1315 | parser.setInput(confreader); |
| 1316 | XmlUtils.beginDocument(parser, "devices"); |
| 1317 | |
| 1318 | while (true) { |
| 1319 | XmlUtils.nextElement(parser); |
| 1320 | if (!"device".equals(parser.getName())) { |
| 1321 | break; |
| 1322 | } |
| 1323 | String name = parser.getAttributeValue(null, "name"); |
| 1324 | if (name != null) { |
| 1325 | names.add(name); |
| 1326 | } |
| 1327 | } |
| 1328 | } catch (FileNotFoundException e) { |
| 1329 | // It's ok if the file does not exist. |
| 1330 | } catch (Exception e) { |
| 1331 | Slog.e(TAG, "Exception while parsing '" + confFile.getAbsolutePath() + "'", e); |
| 1332 | } finally { |
| 1333 | try { if (confreader != null) confreader.close(); } catch (IOException e) { } |
| 1334 | } |
| 1335 | |
| 1336 | return names.toArray(new String[names.size()]); |
| 1337 | } |
| 1338 | |
| 1339 | // Native callback. |
| 1340 | private int getKeyRepeatTimeout() { |
| 1341 | return ViewConfiguration.getKeyRepeatTimeout(); |
| 1342 | } |
| 1343 | |
| 1344 | // Native callback. |
| 1345 | private int getKeyRepeatDelay() { |
| 1346 | return ViewConfiguration.getKeyRepeatDelay(); |
| 1347 | } |
| 1348 | |
| 1349 | // Native callback. |
| 1350 | private int getHoverTapTimeout() { |
| 1351 | return ViewConfiguration.getHoverTapTimeout(); |
| 1352 | } |
| 1353 | |
| 1354 | // Native callback. |
| 1355 | private int getHoverTapSlop() { |
| 1356 | return ViewConfiguration.getHoverTapSlop(); |
| 1357 | } |
| 1358 | |
| 1359 | // Native callback. |
| 1360 | private int getDoubleTapTimeout() { |
| 1361 | return ViewConfiguration.getDoubleTapTimeout(); |
| 1362 | } |
| 1363 | |
| 1364 | // Native callback. |
| 1365 | private int getLongPressTimeout() { |
| 1366 | return ViewConfiguration.getLongPressTimeout(); |
| 1367 | } |
| 1368 | |
| 1369 | // Native callback. |
| 1370 | private int getPointerLayer() { |
Jeff Brown | a9d131c | 2012-09-20 16:48:17 -0700 | [diff] [blame] | 1371 | return mWindowManagerCallbacks.getPointerLayer(); |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1372 | } |
| 1373 | |
| 1374 | // Native callback. |
| 1375 | private PointerIcon getPointerIcon() { |
| 1376 | return PointerIcon.getDefaultIcon(mContext); |
| 1377 | } |
| 1378 | |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 1379 | // Native callback. |
| 1380 | private String[] getKeyboardLayoutOverlay(String inputDeviceDescriptor) { |
| 1381 | if (!mSystemReady) { |
| 1382 | return null; |
| 1383 | } |
| 1384 | |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 1385 | String keyboardLayoutDescriptor = getCurrentKeyboardLayoutForInputDevice( |
| 1386 | inputDeviceDescriptor); |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 1387 | if (keyboardLayoutDescriptor == null) { |
| 1388 | return null; |
| 1389 | } |
| 1390 | |
| 1391 | final String[] result = new String[2]; |
| 1392 | visitKeyboardLayout(keyboardLayoutDescriptor, new KeyboardLayoutVisitor() { |
| 1393 | @Override |
| 1394 | public void visitKeyboardLayout(Resources resources, |
Jeff Brown | d9fec5d | 2012-05-17 16:01:54 -0700 | [diff] [blame] | 1395 | String descriptor, String label, String collection, int keyboardLayoutResId) { |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 1396 | try { |
| 1397 | result[0] = descriptor; |
| 1398 | result[1] = Streams.readFully(new InputStreamReader( |
Jeff Brown | 2f09576 | 2012-05-10 21:29:33 -0700 | [diff] [blame] | 1399 | resources.openRawResource(keyboardLayoutResId))); |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 1400 | } catch (IOException ex) { |
| 1401 | } catch (NotFoundException ex) { |
| 1402 | } |
| 1403 | } |
| 1404 | }); |
| 1405 | if (result[0] == null) { |
| 1406 | Log.w(TAG, "Could not get keyboard layout with descriptor '" |
| 1407 | + keyboardLayoutDescriptor + "'."); |
| 1408 | return null; |
| 1409 | } |
| 1410 | return result; |
| 1411 | } |
| 1412 | |
Jeff Brown | 5bbd4b4 | 2012-04-20 19:28:00 -0700 | [diff] [blame] | 1413 | // Native callback. |
| 1414 | private String getDeviceAlias(String uniqueId) { |
Matthew Xie | 9631314 | 2012-06-29 16:57:31 -0700 | [diff] [blame] | 1415 | if (BluetoothAdapter.checkBluetoothAddress(uniqueId)) { |
| 1416 | // TODO(BT) mBluetoothService.getRemoteAlias(uniqueId) |
| 1417 | return null; |
Jeff Brown | 5bbd4b4 | 2012-04-20 19:28:00 -0700 | [diff] [blame] | 1418 | } |
| 1419 | return null; |
| 1420 | } |
| 1421 | |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 1422 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1423 | /** |
| 1424 | * Callback interface implemented by the Window Manager. |
| 1425 | */ |
Jeff Brown | a9d131c | 2012-09-20 16:48:17 -0700 | [diff] [blame] | 1426 | public interface WindowManagerCallbacks { |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1427 | public void notifyConfigurationChanged(); |
| 1428 | |
| 1429 | public void notifyLidSwitchChanged(long whenNanos, boolean lidOpen); |
| 1430 | |
| 1431 | public void notifyInputChannelBroken(InputWindowHandle inputWindowHandle); |
| 1432 | |
| 1433 | public long notifyANR(InputApplicationHandle inputApplicationHandle, |
| 1434 | InputWindowHandle inputWindowHandle); |
| 1435 | |
| 1436 | public int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags, boolean isScreenOn); |
| 1437 | |
| 1438 | public int interceptMotionBeforeQueueingWhenScreenOff(int policyFlags); |
| 1439 | |
| 1440 | public long interceptKeyBeforeDispatching(InputWindowHandle focus, |
| 1441 | KeyEvent event, int policyFlags); |
| 1442 | |
| 1443 | public KeyEvent dispatchUnhandledKey(InputWindowHandle focus, |
| 1444 | KeyEvent event, int policyFlags); |
| 1445 | |
| 1446 | public int getPointerLayer(); |
| 1447 | } |
| 1448 | |
| 1449 | /** |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 1450 | * Private handler for the input manager. |
| 1451 | */ |
| 1452 | private final class InputManagerHandler extends Handler { |
Jeff Brown | a9d131c | 2012-09-20 16:48:17 -0700 | [diff] [blame] | 1453 | public InputManagerHandler(Looper looper) { |
| 1454 | super(looper, null, true /*async*/); |
Jeff Brown | a2910d0 | 2012-08-25 12:29:46 -0700 | [diff] [blame] | 1455 | } |
| 1456 | |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 1457 | @Override |
| 1458 | public void handleMessage(Message msg) { |
| 1459 | switch (msg.what) { |
| 1460 | case MSG_DELIVER_INPUT_DEVICES_CHANGED: |
Jeff Brown | cf39bdf | 2012-05-18 14:41:19 -0700 | [diff] [blame] | 1461 | deliverInputDevicesChanged((InputDevice[])msg.obj); |
| 1462 | break; |
| 1463 | case MSG_SWITCH_KEYBOARD_LAYOUT: |
| 1464 | handleSwitchKeyboardLayout(msg.arg1, msg.arg2); |
| 1465 | break; |
| 1466 | case MSG_RELOAD_KEYBOARD_LAYOUTS: |
| 1467 | reloadKeyboardLayouts(); |
| 1468 | break; |
| 1469 | case MSG_UPDATE_KEYBOARD_LAYOUTS: |
| 1470 | updateKeyboardLayouts(); |
| 1471 | break; |
| 1472 | case MSG_RELOAD_DEVICE_ALIASES: |
| 1473 | reloadDeviceAliases(); |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 1474 | break; |
| 1475 | } |
| 1476 | } |
| 1477 | } |
| 1478 | |
| 1479 | /** |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 1480 | * Hosting interface for input filters to call back into the input manager. |
| 1481 | */ |
Svetoslav Ganov | c9c9a48 | 2012-07-16 08:46:07 -0700 | [diff] [blame] | 1482 | private final class InputFilterHost extends IInputFilterHost.Stub { |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 1483 | private boolean mDisconnected; |
| 1484 | |
| 1485 | public void disconnectLocked() { |
| 1486 | mDisconnected = true; |
| 1487 | } |
| 1488 | |
| 1489 | public void sendInputEvent(InputEvent event, int policyFlags) { |
| 1490 | if (event == null) { |
| 1491 | throw new IllegalArgumentException("event must not be null"); |
| 1492 | } |
| 1493 | |
| 1494 | synchronized (mInputFilterLock) { |
| 1495 | if (!mDisconnected) { |
Jeff Brown | ac14351 | 2012-04-05 18:57:33 -0700 | [diff] [blame] | 1496 | nativeInjectInputEvent(mPtr, event, 0, 0, |
| 1497 | InputManager.INJECT_INPUT_EVENT_MODE_ASYNC, 0, |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 1498 | policyFlags | WindowManagerPolicy.FLAG_FILTERED); |
| 1499 | } |
| 1500 | } |
| 1501 | } |
| 1502 | } |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 1503 | |
| 1504 | private static final class KeyboardLayoutDescriptor { |
| 1505 | public String packageName; |
| 1506 | public String receiverName; |
| 1507 | public String keyboardLayoutName; |
| 1508 | |
| 1509 | public static String format(String packageName, |
| 1510 | String receiverName, String keyboardName) { |
| 1511 | return packageName + "/" + receiverName + "/" + keyboardName; |
| 1512 | } |
| 1513 | |
| 1514 | public static KeyboardLayoutDescriptor parse(String descriptor) { |
| 1515 | int pos = descriptor.indexOf('/'); |
| 1516 | if (pos < 0 || pos + 1 == descriptor.length()) { |
| 1517 | return null; |
| 1518 | } |
| 1519 | int pos2 = descriptor.indexOf('/', pos + 1); |
| 1520 | if (pos2 < pos + 2 || pos2 + 1 == descriptor.length()) { |
| 1521 | return null; |
| 1522 | } |
| 1523 | |
| 1524 | KeyboardLayoutDescriptor result = new KeyboardLayoutDescriptor(); |
| 1525 | result.packageName = descriptor.substring(0, pos); |
| 1526 | result.receiverName = descriptor.substring(pos + 1, pos2); |
| 1527 | result.keyboardLayoutName = descriptor.substring(pos2 + 1); |
| 1528 | return result; |
| 1529 | } |
| 1530 | } |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 1531 | |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 1532 | private interface KeyboardLayoutVisitor { |
| 1533 | void visitKeyboardLayout(Resources resources, |
Jeff Brown | d9fec5d | 2012-05-17 16:01:54 -0700 | [diff] [blame] | 1534 | String descriptor, String label, String collection, int keyboardLayoutResId); |
Jeff Brown | 6ec6f79 | 2012-04-17 16:52:41 -0700 | [diff] [blame] | 1535 | } |
| 1536 | |
Jeff Brown | af9e8d3 | 2012-04-12 17:32:48 -0700 | [diff] [blame] | 1537 | private final class InputDevicesChangedListenerRecord implements DeathRecipient { |
| 1538 | private final int mPid; |
| 1539 | private final IInputDevicesChangedListener mListener; |
| 1540 | |
| 1541 | public InputDevicesChangedListenerRecord(int pid, IInputDevicesChangedListener listener) { |
| 1542 | mPid = pid; |
| 1543 | mListener = listener; |
| 1544 | } |
| 1545 | |
| 1546 | @Override |
| 1547 | public void binderDied() { |
| 1548 | if (DEBUG) { |
| 1549 | Slog.d(TAG, "Input devices changed listener for pid " + mPid + " died."); |
| 1550 | } |
| 1551 | onInputDevicesChangedListenerDied(mPid); |
| 1552 | } |
| 1553 | |
| 1554 | public void notifyInputDevicesChanged(int[] info) { |
| 1555 | try { |
| 1556 | mListener.onInputDevicesChanged(info); |
| 1557 | } catch (RemoteException ex) { |
| 1558 | Slog.w(TAG, "Failed to notify process " |
| 1559 | + mPid + " that input devices changed, assuming it died.", ex); |
| 1560 | binderDied(); |
| 1561 | } |
| 1562 | } |
| 1563 | } |
Jeff Brown | a47425a | 2012-04-13 04:09:27 -0700 | [diff] [blame] | 1564 | |
| 1565 | private final class VibratorToken implements DeathRecipient { |
| 1566 | public final int mDeviceId; |
| 1567 | public final IBinder mToken; |
| 1568 | public final int mTokenValue; |
| 1569 | |
| 1570 | public boolean mVibrating; |
| 1571 | |
| 1572 | public VibratorToken(int deviceId, IBinder token, int tokenValue) { |
| 1573 | mDeviceId = deviceId; |
| 1574 | mToken = token; |
| 1575 | mTokenValue = tokenValue; |
| 1576 | } |
| 1577 | |
| 1578 | @Override |
| 1579 | public void binderDied() { |
| 1580 | if (DEBUG) { |
| 1581 | Slog.d(TAG, "Vibrator token died."); |
| 1582 | } |
| 1583 | onVibratorTokenDied(this); |
| 1584 | } |
| 1585 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1586 | } |