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