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