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