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