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