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