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