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