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