blob: a951d470735a1e784a3561e92643f67afc013ad5 [file] [log] [blame]
Jeff Brown46b9ac02010-04-22 18:58:52 -07001/*
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 Brown4532e612012-04-05 14:27:12 -070017package com.android.server.input;
Jeff Brown46b9ac02010-04-22 18:58:52 -070018
Yohei Yukawa5660fad2016-01-27 22:04:09 -080019import android.annotation.NonNull;
Yohei Yukawab097b822015-12-01 10:43:08 -080020import android.annotation.Nullable;
Yohei Yukawa23cbe852016-05-17 16:42:58 -070021import android.os.LocaleList;
Dianne Hackborn354736e2016-08-22 17:00:05 -070022import android.os.ShellCallback;
Adrian Roos99182342016-06-15 15:30:46 -070023import android.util.Log;
Jeff Brownca9bc702014-02-11 14:32:56 -080024import android.view.Display;
Michael Wrightd5f7ed92016-01-19 11:23:51 -080025import com.android.internal.inputmethod.InputMethodSubtypeHandle;
Chris Wren282cfef2017-03-27 15:01:44 -040026import com.android.internal.messages.nano.SystemMessageProto.SystemMessage;
Geoffrey Pitschaf759c52017-02-15 09:35:38 -050027import com.android.internal.notification.SystemNotificationChannels;
Michael Wright39e5e942015-08-19 22:52:47 +010028import com.android.internal.os.SomeArgs;
Jeff Browncf39bdf2012-05-18 14:41:19 -070029import com.android.internal.R;
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -060030import com.android.internal.util.DumpUtils;
Michael Wrightb004b512017-01-18 18:09:29 +000031import com.android.internal.util.Preconditions;
Jeff Brown46b9ac02010-04-22 18:58:52 -070032import com.android.internal.util.XmlUtils;
Jeff Brown4ccb8232014-01-16 22:16:42 -080033import com.android.server.DisplayThread;
34import com.android.server.LocalServices;
Jeff Brown89ef0722011-08-10 16:25:21 -070035import com.android.server.Watchdog;
Adrian Roose99bc052017-11-20 17:55:31 +010036import com.android.server.policy.WindowManagerPolicy;
Jeff Brown46b9ac02010-04-22 18:58:52 -070037
38import org.xmlpull.v1.XmlPullParser;
39
Jeff Browna3bc5652012-04-17 11:42:25 -070040import android.Manifest;
Andrii Kulianed76e742017-06-26 14:57:02 -070041import android.app.IInputForwarder;
Jeff Browncf39bdf2012-05-18 14:41:19 -070042import android.app.Notification;
43import android.app.NotificationManager;
44import android.app.PendingIntent;
Jeff Brown5bbd4b42012-04-20 19:28:00 -070045import android.bluetooth.BluetoothAdapter;
46import android.bluetooth.BluetoothDevice;
Jeff Brown6ec6f792012-04-17 16:52:41 -070047import android.content.BroadcastReceiver;
Jeff Brown9f25b7f2012-04-10 14:30:49 -070048import android.content.ComponentName;
Jeff Brown46b9ac02010-04-22 18:58:52 -070049import android.content.Context;
Jeff Brown9f25b7f2012-04-10 14:30:49 -070050import android.content.Intent;
Jeff Brown6ec6f792012-04-17 16:52:41 -070051import android.content.IntentFilter;
Jeff Brown9f25b7f2012-04-10 14:30:49 -070052import android.content.pm.ActivityInfo;
Michael Wright8ebac232014-09-18 18:29:49 -070053import android.content.pm.ApplicationInfo;
Jeff Brown349703e2010-06-22 01:27:15 -070054import android.content.pm.PackageManager;
Jeff Brown9f25b7f2012-04-10 14:30:49 -070055import android.content.pm.ResolveInfo;
56import android.content.pm.PackageManager.NameNotFoundException;
Jeff Brown9f25b7f2012-04-10 14:30:49 -070057import android.content.res.Resources;
Jeff Brown6ec6f792012-04-17 16:52:41 -070058import android.content.res.Resources.NotFoundException;
Jeff Brown9f25b7f2012-04-10 14:30:49 -070059import android.content.res.TypedArray;
60import android.content.res.XmlResourceParser;
Jeff Brown1a84fd12011-06-02 01:26:32 -070061import android.database.ContentObserver;
Andrii Kulianed76e742017-06-26 14:57:02 -070062import android.hardware.display.DisplayManager;
Jeff Brown4ccb8232014-01-16 22:16:42 -080063import android.hardware.display.DisplayViewport;
Jeff Brownaf9e8d32012-04-12 17:32:48 -070064import android.hardware.input.IInputDevicesChangedListener;
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -070065import android.hardware.input.IInputManager;
RoboErikfb290df2013-12-16 11:27:55 -080066import android.hardware.input.InputDeviceIdentifier;
Jeff Brownac143512012-04-05 18:57:33 -070067import android.hardware.input.InputManager;
Jeff Brown4ccb8232014-01-16 22:16:42 -080068import android.hardware.input.InputManagerInternal;
Michael Wright39e5e942015-08-19 22:52:47 +010069import android.hardware.input.ITabletModeChangedListener;
Jeff Brown9f25b7f2012-04-10 14:30:49 -070070import android.hardware.input.KeyboardLayout;
Jason Gerecked6396d62014-01-27 18:30:37 -080071import android.hardware.input.TouchCalibration;
Jeff Brown4532e612012-04-05 14:27:12 -070072import android.os.Binder;
Jeff Brown9f25b7f2012-04-10 14:30:49 -070073import android.os.Bundle;
Jeff Brown46b9ac02010-04-22 18:58:52 -070074import android.os.Environment;
Jeff Brown4532e612012-04-05 14:27:12 -070075import android.os.Handler;
Jeff Brownaf9e8d32012-04-12 17:32:48 -070076import android.os.IBinder;
Jeff Browna9d131c2012-09-20 16:48:17 -070077import android.os.Looper;
Jeff Brownaf9e8d32012-04-12 17:32:48 -070078import android.os.Message;
Jeff Brown05dc66a2011-03-02 14:41:58 -080079import android.os.MessageQueue;
Jeff Brownac143512012-04-05 18:57:33 -070080import android.os.Process;
Jeff Brownaf9e8d32012-04-12 17:32:48 -070081import android.os.RemoteException;
Michael Wrightd5f7ed92016-01-19 11:23:51 -080082import android.os.ResultReceiver;
83import android.os.ShellCommand;
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -070084import android.os.UserHandle;
Jeff Brown1a84fd12011-06-02 01:26:32 -070085import android.provider.Settings;
86import android.provider.Settings.SettingNotFoundException;
Michael Wright07483422015-10-30 16:20:13 +000087import android.text.TextUtils;
Jeff Brown46b9ac02010-04-22 18:58:52 -070088import android.util.Slog;
Jeff Brownaf9e8d32012-04-12 17:32:48 -070089import android.util.SparseArray;
Jeff Brown46b9ac02010-04-22 18:58:52 -070090import android.util.Xml;
Andrii Kulianed76e742017-06-26 14:57:02 -070091import android.view.Display;
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -070092import android.view.IInputFilter;
93import android.view.IInputFilterHost;
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -080094import android.view.IWindow;
Jeff Brown46b9ac02010-04-22 18:58:52 -070095import android.view.InputChannel;
Jeff Brown8d608662010-08-30 03:02:23 -070096import android.view.InputDevice;
Jeff Brown6ec402b2010-07-28 15:48:59 -070097import android.view.InputEvent;
Jeff Brown1f245102010-11-18 20:53:46 -080098import android.view.KeyEvent;
Jeff Brown2352b972011-04-12 22:39:53 -070099import android.view.PointerIcon;
Jason Gerecked5220742014-03-10 09:47:59 -0700100import android.view.Surface;
Jeff Browna4547672011-03-02 21:38:11 -0800101import android.view.ViewConfiguration;
Yohei Yukawab097b822015-12-01 10:43:08 -0800102import android.view.inputmethod.InputMethodInfo;
103import android.view.inputmethod.InputMethodSubtype;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700104
Jeff Brown46b9ac02010-04-22 18:58:52 -0700105import java.io.File;
Jeff Brown4532e612012-04-05 14:27:12 -0700106import java.io.FileDescriptor;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700107import java.io.FileNotFoundException;
108import java.io.FileReader;
Adrian Roos99182342016-06-15 15:30:46 -0700109import java.io.FileWriter;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700110import java.io.IOException;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700111import java.io.InputStreamReader;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700112import java.io.PrintWriter;
113import java.util.ArrayList;
Michael Wright07483422015-10-30 16:20:13 +0000114import java.util.Collections;
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700115import java.util.HashMap;
Jeff Browncf39bdf2012-05-18 14:41:19 -0700116import java.util.HashSet;
Michael Wright39e5e942015-08-19 22:52:47 +0100117import java.util.List;
Michael Wright07483422015-10-30 16:20:13 +0000118import java.util.Locale;
Narayan Kamath607223f2018-02-19 14:09:02 +0000119import java.util.Objects;
Jeff Browna3bc5652012-04-17 11:42:25 -0700120
Adrian Roos99182342016-06-15 15:30:46 -0700121import libcore.io.IoUtils;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700122import libcore.io.Streams;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700123
124/*
125 * Wraps the C++ InputManager and provides its callbacks.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700126 */
Jeff Brownd728bf52012-09-08 18:05:28 -0700127public class InputManagerService extends IInputManager.Stub
Jeff Brown4ccb8232014-01-16 22:16:42 -0800128 implements Watchdog.Monitor {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700129 static final String TAG = "InputManager";
Jeff Brown1b9ba572012-05-21 10:54:18 -0700130 static final boolean DEBUG = false;
Jeff Brownb6997262010-10-08 22:31:17 -0700131
Jeff Brown4532e612012-04-05 14:27:12 -0700132 private static final String EXCLUDED_DEVICES_PATH = "etc/excluded-input-devices.xml";
133
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700134 private static final int MSG_DELIVER_INPUT_DEVICES_CHANGED = 1;
Jeff Browncf39bdf2012-05-18 14:41:19 -0700135 private static final int MSG_SWITCH_KEYBOARD_LAYOUT = 2;
136 private static final int MSG_RELOAD_KEYBOARD_LAYOUTS = 3;
137 private static final int MSG_UPDATE_KEYBOARD_LAYOUTS = 4;
138 private static final int MSG_RELOAD_DEVICE_ALIASES = 5;
Michael Wright39e5e942015-08-19 22:52:47 +0100139 private static final int MSG_DELIVER_TABLET_MODE_CHANGED = 6;
Yohei Yukawab097b822015-12-01 10:43:08 -0800140 private static final int MSG_INPUT_METHOD_SUBTYPE_CHANGED = 7;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700141
Jeff Brown4532e612012-04-05 14:27:12 -0700142 // Pointer to native input manager service object.
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000143 private final long mPtr;
Jeff Brown4532e612012-04-05 14:27:12 -0700144
Jeff Brown46b9ac02010-04-22 18:58:52 -0700145 private final Context mContext;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700146 private final InputManagerHandler mHandler;
Jeff Browna9d131c2012-09-20 16:48:17 -0700147
Adrian Roos99182342016-06-15 15:30:46 -0700148 private final File mDoubleTouchGestureEnableFile;
149
Jeff Browna9d131c2012-09-20 16:48:17 -0700150 private WindowManagerCallbacks mWindowManagerCallbacks;
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700151 private WiredAccessoryCallbacks mWiredAccessoryCallbacks;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700152 private boolean mSystemReady;
Jeff Browncf39bdf2012-05-18 14:41:19 -0700153 private NotificationManager mNotificationManager;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700154
Michael Wright39e5e942015-08-19 22:52:47 +0100155 private final Object mTabletModeLock = new Object();
156 // List of currently registered tablet mode changed listeners by process id
157 private final SparseArray<TabletModeChangedListenerRecord> mTabletModeChangedListeners =
158 new SparseArray<>(); // guarded by mTabletModeLock
159 private final List<TabletModeChangedListenerRecord> mTempTabletModeChangedListenersToNotify =
160 new ArrayList<>();
161
Jeff Browna3bc5652012-04-17 11:42:25 -0700162 // Persistent data store. Must be locked each time during use.
163 private final PersistentDataStore mDataStore = new PersistentDataStore();
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700164
165 // List of currently registered input devices changed listeners by process id.
166 private Object mInputDevicesLock = new Object();
167 private boolean mInputDevicesChangedPending; // guarded by mInputDevicesLock
168 private InputDevice[] mInputDevices = new InputDevice[0];
169 private final SparseArray<InputDevicesChangedListenerRecord> mInputDevicesChangedListeners =
170 new SparseArray<InputDevicesChangedListenerRecord>(); // guarded by mInputDevicesLock
171 private final ArrayList<InputDevicesChangedListenerRecord>
172 mTempInputDevicesChangedListenersToNotify =
173 new ArrayList<InputDevicesChangedListenerRecord>(); // handler thread only
Jeff Browncf39bdf2012-05-18 14:41:19 -0700174 private final ArrayList<InputDevice>
175 mTempFullKeyboards = new ArrayList<InputDevice>(); // handler thread only
176 private boolean mKeyboardLayoutNotificationShown;
Michael Wrightd5f7ed92016-01-19 11:23:51 -0800177 private InputMethodSubtypeHandle mCurrentImeHandle;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700178
Jeff Browna47425a2012-04-13 04:09:27 -0700179 // State for vibrator tokens.
180 private Object mVibratorLock = new Object();
181 private HashMap<IBinder, VibratorToken> mVibratorTokens =
182 new HashMap<IBinder, VibratorToken>();
183 private int mNextVibratorTokenValue;
184
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700185 // State for the currently installed input filter.
186 final Object mInputFilterLock = new Object();
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -0700187 IInputFilter mInputFilter; // guarded by mInputFilterLock
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700188 InputFilterHost mInputFilterHost; // guarded by mInputFilterLock
Jeff Brown1a84fd12011-06-02 01:26:32 -0700189
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800190 private IWindow mFocusedWindow;
191 private boolean mFocusedWindowHasCapture;
192
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000193 private static native long nativeInit(InputManagerService service,
Jeff Brown4532e612012-04-05 14:27:12 -0700194 Context context, MessageQueue messageQueue);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000195 private static native void nativeStart(long ptr);
Santos Cordonee8931e2017-04-05 10:31:15 -0700196 private static native void nativeSetVirtualDisplayViewports(long ptr,
197 DisplayViewport[] viewports);
198 private static native void nativeSetDisplayViewport(long ptr, int viewportType,
Jeff Brownd728bf52012-09-08 18:05:28 -0700199 int displayId, int rotation,
200 int logicalLeft, int logicalTop, int logicalRight, int logicalBottom,
Jeff Brown83d616a2012-09-09 20:33:43 -0700201 int physicalLeft, int physicalTop, int physicalRight, int physicalBottom,
Santos Cordonee8931e2017-04-05 10:31:15 -0700202 int deviceWidth, int deviceHeight, String uniqueId);
Jeff Brownd728bf52012-09-08 18:05:28 -0700203
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000204 private static native int nativeGetScanCodeState(long ptr,
Jeff Brown4532e612012-04-05 14:27:12 -0700205 int deviceId, int sourceMask, int scanCode);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000206 private static native int nativeGetKeyCodeState(long ptr,
Jeff Brown4532e612012-04-05 14:27:12 -0700207 int deviceId, int sourceMask, int keyCode);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000208 private static native int nativeGetSwitchState(long ptr,
Jeff Brown4532e612012-04-05 14:27:12 -0700209 int deviceId, int sourceMask, int sw);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000210 private static native boolean nativeHasKeys(long ptr,
Jeff Brown4532e612012-04-05 14:27:12 -0700211 int deviceId, int sourceMask, int[] keyCodes, boolean[] keyExists);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000212 private static native void nativeRegisterInputChannel(long ptr, InputChannel inputChannel,
Jeff Brown928e0542011-01-10 11:17:36 -0800213 InputWindowHandle inputWindowHandle, boolean monitor);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000214 private static native void nativeUnregisterInputChannel(long ptr, InputChannel inputChannel);
215 private static native void nativeSetInputFilterEnabled(long ptr, boolean enable);
Jeff Brownca9bc702014-02-11 14:32:56 -0800216 private static native int nativeInjectInputEvent(long ptr, InputEvent event, int displayId,
Jeff Brown0029c662011-03-30 02:25:18 -0700217 int injectorPid, int injectorUid, int syncMode, int timeoutMillis,
218 int policyFlags);
Andrii Kulian112d0562016-03-08 10:44:22 -0800219 private static native void nativeToggleCapsLock(long ptr, int deviceId);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000220 private static native void nativeSetInputWindows(long ptr, InputWindowHandle[] windowHandles);
221 private static native void nativeSetInputDispatchMode(long ptr, boolean enabled, boolean frozen);
222 private static native void nativeSetSystemUiVisibility(long ptr, int visibility);
223 private static native void nativeSetFocusedApplication(long ptr,
Jeff Brown4532e612012-04-05 14:27:12 -0700224 InputApplicationHandle application);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000225 private static native boolean nativeTransferTouchFocus(long ptr,
Jeff Brown4532e612012-04-05 14:27:12 -0700226 InputChannel fromChannel, InputChannel toChannel);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000227 private static native void nativeSetPointerSpeed(long ptr, int speed);
228 private static native void nativeSetShowTouches(long ptr, boolean enabled);
Jeff Brown037c33e2014-04-09 00:31:55 -0700229 private static native void nativeSetInteractive(long ptr, boolean interactive);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800230 private static native void nativeReloadCalibration(long ptr);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000231 private static native void nativeVibrate(long ptr, int deviceId, long[] pattern,
Jeff Browna47425a2012-04-13 04:09:27 -0700232 int repeat, int token);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000233 private static native void nativeCancelVibrate(long ptr, int deviceId, int token);
234 private static native void nativeReloadKeyboardLayouts(long ptr);
235 private static native void nativeReloadDeviceAliases(long ptr);
236 private static native String nativeDump(long ptr);
237 private static native void nativeMonitor(long ptr);
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700238 private static native boolean nativeIsInputDeviceEnabled(long ptr, int deviceId);
239 private static native void nativeEnableInputDevice(long ptr, int deviceId);
240 private static native void nativeDisableInputDevice(long ptr, int deviceId);
Michael Wrightf9d9ce772016-05-13 17:44:16 +0100241 private static native void nativeSetPointerIconType(long ptr, int iconId);
Jun Mukai19a56012015-11-24 11:25:52 -0800242 private static native void nativeReloadPointerIcons(long ptr);
Jun Mukaid4eaef72015-10-30 15:54:33 -0700243 private static native void nativeSetCustomPointerIcon(long ptr, PointerIcon icon);
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -0800244 private static native void nativeSetPointerCapture(long ptr, boolean detached);
Jeff Brown4532e612012-04-05 14:27:12 -0700245
Jeff Brownac143512012-04-05 18:57:33 -0700246 // Input event injection constants defined in InputDispatcher.h.
247 private static final int INPUT_EVENT_INJECTION_SUCCEEDED = 0;
248 private static final int INPUT_EVENT_INJECTION_PERMISSION_DENIED = 1;
249 private static final int INPUT_EVENT_INJECTION_FAILED = 2;
250 private static final int INPUT_EVENT_INJECTION_TIMED_OUT = 3;
251
252 // Maximum number of milliseconds to wait for input event injection.
253 private static final int INJECTION_TIMEOUT_MILLIS = 30 * 1000;
254
Jeff Brown6d0fec22010-07-23 21:28:06 -0700255 // Key states (may be returned by queries about the current state of a
256 // particular key code, scan code or switch).
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700257
Jeff Brown6d0fec22010-07-23 21:28:06 -0700258 /** The key state is unknown or the requested key itself is not supported. */
259 public static final int KEY_STATE_UNKNOWN = -1;
260
261 /** The key is up. /*/
262 public static final int KEY_STATE_UP = 0;
263
264 /** The key is down. */
265 public static final int KEY_STATE_DOWN = 1;
266
267 /** The key is down but is a virtual key press that is being emulated by the system. */
268 public static final int KEY_STATE_VIRTUAL = 2;
269
Jeff Brownc458ce92012-04-30 14:58:40 -0700270 /** Scan code: Mouse / trackball button. */
271 public static final int BTN_MOUSE = 0x110;
272
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700273 // Switch code values must match bionic/libc/kernel/common/linux/input.h
Jeff Brownc458ce92012-04-30 14:58:40 -0700274 /** Switch code: Lid switch. When set, lid is shut. */
275 public static final int SW_LID = 0x00;
276
Michael Wright39e5e942015-08-19 22:52:47 +0100277 /** Switch code: Tablet mode switch.
278 * When set, the device is in tablet mode (i.e. no keyboard is connected).
279 */
280 public static final int SW_TABLET_MODE = 0x01;
281
Jeff Brownc458ce92012-04-30 14:58:40 -0700282 /** Switch code: Keypad slide. When set, keyboard is exposed. */
283 public static final int SW_KEYPAD_SLIDE = 0x0a;
284
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700285 /** Switch code: Headphone. When set, headphone is inserted. */
286 public static final int SW_HEADPHONE_INSERT = 0x02;
287
288 /** Switch code: Microphone. When set, microphone is inserted. */
289 public static final int SW_MICROPHONE_INSERT = 0x04;
290
Jon Eklund43cc8bb2014-07-28 16:07:24 -0500291 /** Switch code: Line out. When set, Line out (hi-Z) is inserted. */
292 public static final int SW_LINEOUT_INSERT = 0x06;
293
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700294 /** Switch code: Headphone/Microphone Jack. When set, something is inserted. */
295 public static final int SW_JACK_PHYSICAL_INSERT = 0x07;
296
Michael Wright3818c922014-09-02 13:59:07 -0700297 /** Switch code: Camera lens cover. When set the lens is covered. */
298 public static final int SW_CAMERA_LENS_COVER = 0x09;
299
Santos Cordonee8931e2017-04-05 10:31:15 -0700300 // Viewport constants defined in InputReader.h.
301 public static final int VIEWPORT_DEFAULT = 1;
302 public static final int VIEWPORT_EXTERNAL = 2;
303 public static final int VIEWPORT_VIRTUAL = 3;
304
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700305 public static final int SW_LID_BIT = 1 << SW_LID;
Michael Wright39e5e942015-08-19 22:52:47 +0100306 public static final int SW_TABLET_MODE_BIT = 1 << SW_TABLET_MODE;
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700307 public static final int SW_KEYPAD_SLIDE_BIT = 1 << SW_KEYPAD_SLIDE;
308 public static final int SW_HEADPHONE_INSERT_BIT = 1 << SW_HEADPHONE_INSERT;
309 public static final int SW_MICROPHONE_INSERT_BIT = 1 << SW_MICROPHONE_INSERT;
Jon Eklund43cc8bb2014-07-28 16:07:24 -0500310 public static final int SW_LINEOUT_INSERT_BIT = 1 << SW_LINEOUT_INSERT;
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700311 public static final int SW_JACK_PHYSICAL_INSERT_BIT = 1 << SW_JACK_PHYSICAL_INSERT;
312 public static final int SW_JACK_BITS =
Jon Eklund43cc8bb2014-07-28 16:07:24 -0500313 SW_HEADPHONE_INSERT_BIT | SW_MICROPHONE_INSERT_BIT | SW_JACK_PHYSICAL_INSERT_BIT | SW_LINEOUT_INSERT_BIT;
Michael Wright3818c922014-09-02 13:59:07 -0700314 public static final int SW_CAMERA_LENS_COVER_BIT = 1 << SW_CAMERA_LENS_COVER;
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700315
316 /** Whether to use the dev/input/event or uevent subsystem for the audio jack. */
317 final boolean mUseDevInputEventForAudioJack;
318
Jeff Brown4ccb8232014-01-16 22:16:42 -0800319 public InputManagerService(Context context) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700320 this.mContext = context;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800321 this.mHandler = new InputManagerHandler(DisplayThread.get().getLooper());
Jeff Brown05dc66a2011-03-02 14:41:58 -0800322
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700323 mUseDevInputEventForAudioJack =
324 context.getResources().getBoolean(R.bool.config_useDevInputEventForAudioJack);
325 Slog.i(TAG, "Initializing input manager, mUseDevInputEventForAudioJack="
326 + mUseDevInputEventForAudioJack);
Jeff Brown4532e612012-04-05 14:27:12 -0700327 mPtr = nativeInit(this, mContext, mHandler.getLooper().getQueue());
Jeff Brown4ccb8232014-01-16 22:16:42 -0800328
Adrian Roos99182342016-06-15 15:30:46 -0700329 String doubleTouchGestureEnablePath = context.getResources().getString(
330 R.string.config_doubleTouchGestureEnableFile);
331 mDoubleTouchGestureEnableFile = TextUtils.isEmpty(doubleTouchGestureEnablePath) ? null :
332 new File(doubleTouchGestureEnablePath);
333
Jeff Brown4ccb8232014-01-16 22:16:42 -0800334 LocalServices.addService(InputManagerInternal.class, new LocalService());
Jeff Brown46b9ac02010-04-22 18:58:52 -0700335 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700336
Jeff Browna9d131c2012-09-20 16:48:17 -0700337 public void setWindowManagerCallbacks(WindowManagerCallbacks callbacks) {
338 mWindowManagerCallbacks = callbacks;
339 }
340
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700341 public void setWiredAccessoryCallbacks(WiredAccessoryCallbacks callbacks) {
342 mWiredAccessoryCallbacks = callbacks;
343 }
344
Jeff Brown46b9ac02010-04-22 18:58:52 -0700345 public void start() {
346 Slog.i(TAG, "Starting input manager");
Jeff Brown4532e612012-04-05 14:27:12 -0700347 nativeStart(mPtr);
348
349 // Add ourself to the Watchdog monitors.
350 Watchdog.getInstance().addMonitor(this);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700351
352 registerPointerSpeedSettingObserver();
Jeff Browndaf4a122011-08-26 17:14:14 -0700353 registerShowTouchesSettingObserver();
Jun Mukai19a56012015-11-24 11:25:52 -0800354 registerAccessibilityLargePointerSettingObserver();
Jeff Browndaf4a122011-08-26 17:14:14 -0700355
Jeff Brownd4935962012-09-25 13:27:20 -0700356 mContext.registerReceiver(new BroadcastReceiver() {
357 @Override
358 public void onReceive(Context context, Intent intent) {
359 updatePointerSpeedFromSettings();
360 updateShowTouchesFromSettings();
Jun Mukaie4e75da2015-12-15 16:19:20 -0800361 updateAccessibilityLargePointerFromSettings();
Jeff Brownd4935962012-09-25 13:27:20 -0700362 }
363 }, new IntentFilter(Intent.ACTION_USER_SWITCHED), null, mHandler);
364
Jeff Brown1a84fd12011-06-02 01:26:32 -0700365 updatePointerSpeedFromSettings();
Jeff Browndaf4a122011-08-26 17:14:14 -0700366 updateShowTouchesFromSettings();
Jun Mukaie4e75da2015-12-15 16:19:20 -0800367 updateAccessibilityLargePointerFromSettings();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700368 }
Jeff Brown6ec6f792012-04-17 16:52:41 -0700369
Matthew Xie96313142012-06-29 16:57:31 -0700370 // TODO(BT) Pass in paramter for bluetooth system
Svetoslav Ganova0027152013-06-25 14:59:53 -0700371 public void systemRunning() {
Jeff Brown6ec6f792012-04-17 16:52:41 -0700372 if (DEBUG) {
373 Slog.d(TAG, "System ready.");
374 }
Jeff Browncf39bdf2012-05-18 14:41:19 -0700375 mNotificationManager = (NotificationManager)mContext.getSystemService(
376 Context.NOTIFICATION_SERVICE);
Jeff Brown6ec6f792012-04-17 16:52:41 -0700377 mSystemReady = true;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700378
379 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
380 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
381 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
Jeff Brown69b07162013-11-07 00:30:16 -0800382 filter.addAction(Intent.ACTION_PACKAGE_REPLACED);
Jeff Brown6ec6f792012-04-17 16:52:41 -0700383 filter.addDataScheme("package");
384 mContext.registerReceiver(new BroadcastReceiver() {
385 @Override
386 public void onReceive(Context context, Intent intent) {
Jeff Browncf39bdf2012-05-18 14:41:19 -0700387 updateKeyboardLayouts();
Jeff Brown6ec6f792012-04-17 16:52:41 -0700388 }
389 }, filter, null, mHandler);
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700390
391 filter = new IntentFilter(BluetoothDevice.ACTION_ALIAS_CHANGED);
392 mContext.registerReceiver(new BroadcastReceiver() {
393 @Override
394 public void onReceive(Context context, Intent intent) {
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700395 reloadDeviceAliases();
396 }
397 }, filter, null, mHandler);
398
Jeff Browncf39bdf2012-05-18 14:41:19 -0700399 mHandler.sendEmptyMessage(MSG_RELOAD_DEVICE_ALIASES);
400 mHandler.sendEmptyMessage(MSG_UPDATE_KEYBOARD_LAYOUTS);
Eric Laurent4a5eeb92014-05-06 10:49:04 -0700401
402 if (mWiredAccessoryCallbacks != null) {
403 mWiredAccessoryCallbacks.systemReady();
404 }
Jeff Brown6ec6f792012-04-17 16:52:41 -0700405 }
406
407 private void reloadKeyboardLayouts() {
Jeff Browncf39bdf2012-05-18 14:41:19 -0700408 if (DEBUG) {
409 Slog.d(TAG, "Reloading keyboard layouts.");
410 }
Jeff Brown6ec6f792012-04-17 16:52:41 -0700411 nativeReloadKeyboardLayouts(mPtr);
412 }
413
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700414 private void reloadDeviceAliases() {
Jeff Browncf39bdf2012-05-18 14:41:19 -0700415 if (DEBUG) {
416 Slog.d(TAG, "Reloading device names.");
417 }
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700418 nativeReloadDeviceAliases(mPtr);
419 }
420
Jeff Brown4ccb8232014-01-16 22:16:42 -0800421 private void setDisplayViewportsInternal(DisplayViewport defaultViewport,
Santos Cordonee8931e2017-04-05 10:31:15 -0700422 DisplayViewport externalTouchViewport,
423 List<DisplayViewport> virtualTouchViewports) {
Jeff Brownd728bf52012-09-08 18:05:28 -0700424 if (defaultViewport.valid) {
Santos Cordonee8931e2017-04-05 10:31:15 -0700425 setDisplayViewport(VIEWPORT_DEFAULT, defaultViewport);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700426 }
Jeff Brownd728bf52012-09-08 18:05:28 -0700427
428 if (externalTouchViewport.valid) {
Santos Cordonee8931e2017-04-05 10:31:15 -0700429 setDisplayViewport(VIEWPORT_EXTERNAL, externalTouchViewport);
Jeff Brownd728bf52012-09-08 18:05:28 -0700430 } else if (defaultViewport.valid) {
Santos Cordonee8931e2017-04-05 10:31:15 -0700431 setDisplayViewport(VIEWPORT_EXTERNAL, defaultViewport);
Jeff Brownb6997262010-10-08 22:31:17 -0700432 }
Santos Cordonee8931e2017-04-05 10:31:15 -0700433
434 nativeSetVirtualDisplayViewports(mPtr,
435 virtualTouchViewports.toArray(new DisplayViewport[0]));
Jeff Brown46b9ac02010-04-22 18:58:52 -0700436 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700437
Santos Cordonee8931e2017-04-05 10:31:15 -0700438 private void setDisplayViewport(int viewportType, DisplayViewport viewport) {
439 nativeSetDisplayViewport(mPtr, viewportType,
Jeff Brownd728bf52012-09-08 18:05:28 -0700440 viewport.displayId, viewport.orientation,
441 viewport.logicalFrame.left, viewport.logicalFrame.top,
442 viewport.logicalFrame.right, viewport.logicalFrame.bottom,
443 viewport.physicalFrame.left, viewport.physicalFrame.top,
Jeff Brown83d616a2012-09-09 20:33:43 -0700444 viewport.physicalFrame.right, viewport.physicalFrame.bottom,
Santos Cordonee8931e2017-04-05 10:31:15 -0700445 viewport.deviceWidth, viewport.deviceHeight, viewport.uniqueId);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700446 }
Jeff Brownac143512012-04-05 18:57:33 -0700447
Jeff Brown6d0fec22010-07-23 21:28:06 -0700448 /**
449 * Gets the current state of a key or button by key code.
450 * @param deviceId The input device id, or -1 to consult all devices.
451 * @param sourceMask The input sources to consult, or {@link InputDevice#SOURCE_ANY} to
452 * consider all input sources. An input device is consulted if at least one of its
453 * non-class input source bits matches the specified source mask.
454 * @param keyCode The key code to check.
455 * @return The key state.
456 */
457 public int getKeyCodeState(int deviceId, int sourceMask, int keyCode) {
Jeff Brown4532e612012-04-05 14:27:12 -0700458 return nativeGetKeyCodeState(mPtr, deviceId, sourceMask, keyCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700459 }
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700460
Jeff Brown6d0fec22010-07-23 21:28:06 -0700461 /**
462 * Gets the current state of a key or button by scan code.
463 * @param deviceId The input device id, or -1 to consult all devices.
464 * @param sourceMask The input sources to consult, or {@link InputDevice#SOURCE_ANY} to
465 * consider all input sources. An input device is consulted if at least one of its
466 * non-class input source bits matches the specified source mask.
467 * @param scanCode The scan code to check.
468 * @return The key state.
469 */
470 public int getScanCodeState(int deviceId, int sourceMask, int scanCode) {
Jeff Brown4532e612012-04-05 14:27:12 -0700471 return nativeGetScanCodeState(mPtr, deviceId, sourceMask, scanCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700472 }
RoboErikfb290df2013-12-16 11:27:55 -0800473
Jeff Brown6d0fec22010-07-23 21:28:06 -0700474 /**
475 * Gets the current state of a switch by switch code.
476 * @param deviceId The input device id, or -1 to consult all devices.
477 * @param sourceMask The input sources to consult, or {@link InputDevice#SOURCE_ANY} to
478 * consider all input sources. An input device is consulted if at least one of its
479 * non-class input source bits matches the specified source mask.
480 * @param switchCode The switch code to check.
481 * @return The switch state.
482 */
483 public int getSwitchState(int deviceId, int sourceMask, int switchCode) {
Jeff Brown4532e612012-04-05 14:27:12 -0700484 return nativeGetSwitchState(mPtr, deviceId, sourceMask, switchCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700485 }
486
Jeff Brown6d0fec22010-07-23 21:28:06 -0700487 /**
488 * Determines whether the specified key codes are supported by a particular device.
489 * @param deviceId The input device id, or -1 to consult all devices.
490 * @param sourceMask The input sources to consult, or {@link InputDevice#SOURCE_ANY} to
491 * consider all input sources. An input device is consulted if at least one of its
492 * non-class input source bits matches the specified source mask.
493 * @param keyCodes The array of key codes to check.
494 * @param keyExists An array at least as large as keyCodes whose entries will be set
495 * to true or false based on the presence or absence of support for the corresponding
496 * key codes.
497 * @return True if the lookup was successful, false otherwise.
498 */
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700499 @Override // Binder call
Jeff Brown6d0fec22010-07-23 21:28:06 -0700500 public boolean hasKeys(int deviceId, int sourceMask, int[] keyCodes, boolean[] keyExists) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700501 if (keyCodes == null) {
502 throw new IllegalArgumentException("keyCodes must not be null.");
503 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700504 if (keyExists == null || keyExists.length < keyCodes.length) {
505 throw new IllegalArgumentException("keyExists must not be null and must be at "
506 + "least as large as keyCodes.");
Jeff Brown46b9ac02010-04-22 18:58:52 -0700507 }
RoboErikfb290df2013-12-16 11:27:55 -0800508
Jeff Brown4532e612012-04-05 14:27:12 -0700509 return nativeHasKeys(mPtr, deviceId, sourceMask, keyCodes, keyExists);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700510 }
RoboErikfb290df2013-12-16 11:27:55 -0800511
Jeff Browna41ca772010-08-11 14:46:32 -0700512 /**
513 * Creates an input channel that will receive all input from the input dispatcher.
514 * @param inputChannelName The input channel name.
515 * @return The input channel.
516 */
517 public InputChannel monitorInput(String inputChannelName) {
518 if (inputChannelName == null) {
519 throw new IllegalArgumentException("inputChannelName must not be null.");
520 }
RoboErikfb290df2013-12-16 11:27:55 -0800521
Jeff Browna41ca772010-08-11 14:46:32 -0700522 InputChannel[] inputChannels = InputChannel.openInputChannelPair(inputChannelName);
Jeff Brown4532e612012-04-05 14:27:12 -0700523 nativeRegisterInputChannel(mPtr, inputChannels[0], null, true);
Jeff Browna41ca772010-08-11 14:46:32 -0700524 inputChannels[0].dispose(); // don't need to retain the Java object reference
525 return inputChannels[1];
526 }
527
528 /**
529 * Registers an input channel so that it can be used as an input event target.
530 * @param inputChannel The input channel to register.
Jeff Brown928e0542011-01-10 11:17:36 -0800531 * @param inputWindowHandle The handle of the input window associated with the
532 * input channel, or null if none.
Jeff Browna41ca772010-08-11 14:46:32 -0700533 */
Jeff Brown928e0542011-01-10 11:17:36 -0800534 public void registerInputChannel(InputChannel inputChannel,
535 InputWindowHandle inputWindowHandle) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700536 if (inputChannel == null) {
537 throw new IllegalArgumentException("inputChannel must not be null.");
538 }
RoboErikfb290df2013-12-16 11:27:55 -0800539
Jeff Brown4532e612012-04-05 14:27:12 -0700540 nativeRegisterInputChannel(mPtr, inputChannel, inputWindowHandle, false);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700541 }
RoboErikfb290df2013-12-16 11:27:55 -0800542
Jeff Browna41ca772010-08-11 14:46:32 -0700543 /**
544 * Unregisters an input channel.
545 * @param inputChannel The input channel to unregister.
546 */
Jeff Brown46b9ac02010-04-22 18:58:52 -0700547 public void unregisterInputChannel(InputChannel inputChannel) {
548 if (inputChannel == null) {
549 throw new IllegalArgumentException("inputChannel must not be null.");
550 }
RoboErikfb290df2013-12-16 11:27:55 -0800551
Jeff Brown4532e612012-04-05 14:27:12 -0700552 nativeUnregisterInputChannel(mPtr, inputChannel);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700553 }
Jeff Brown0029c662011-03-30 02:25:18 -0700554
555 /**
556 * Sets an input filter that will receive all input events before they are dispatched.
557 * The input filter may then reinterpret input events or inject new ones.
558 *
559 * To ensure consistency, the input dispatcher automatically drops all events
560 * in progress whenever an input filter is installed or uninstalled. After an input
561 * filter is uninstalled, it can no longer send input events unless it is reinstalled.
562 * Any events it attempts to send after it has been uninstalled will be dropped.
563 *
564 * @param filter The input filter, or null to remove the current filter.
565 */
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -0700566 public void setInputFilter(IInputFilter filter) {
Jeff Brown0029c662011-03-30 02:25:18 -0700567 synchronized (mInputFilterLock) {
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -0700568 final IInputFilter oldFilter = mInputFilter;
Jeff Brown0029c662011-03-30 02:25:18 -0700569 if (oldFilter == filter) {
570 return; // nothing to do
571 }
572
573 if (oldFilter != null) {
574 mInputFilter = null;
575 mInputFilterHost.disconnectLocked();
576 mInputFilterHost = null;
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -0700577 try {
578 oldFilter.uninstall();
579 } catch (RemoteException re) {
580 /* ignore */
581 }
Jeff Brown0029c662011-03-30 02:25:18 -0700582 }
583
584 if (filter != null) {
585 mInputFilter = filter;
586 mInputFilterHost = new InputFilterHost();
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -0700587 try {
588 filter.install(mInputFilterHost);
589 } catch (RemoteException re) {
590 /* ignore */
591 }
Jeff Brown0029c662011-03-30 02:25:18 -0700592 }
593
Jeff Brown4532e612012-04-05 14:27:12 -0700594 nativeSetInputFilterEnabled(mPtr, filter != null);
Jeff Brown0029c662011-03-30 02:25:18 -0700595 }
596 }
597
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700598 @Override // Binder call
Jeff Brownac143512012-04-05 18:57:33 -0700599 public boolean injectInputEvent(InputEvent event, int mode) {
Jeff Brownca9bc702014-02-11 14:32:56 -0800600 return injectInputEventInternal(event, Display.DEFAULT_DISPLAY, mode);
601 }
602
603 private boolean injectInputEventInternal(InputEvent event, int displayId, int mode) {
Jeff Brown7fbdc842010-06-17 20:52:56 -0700604 if (event == null) {
605 throw new IllegalArgumentException("event must not be null");
606 }
Jeff Brownac143512012-04-05 18:57:33 -0700607 if (mode != InputManager.INJECT_INPUT_EVENT_MODE_ASYNC
608 && mode != InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH
609 && mode != InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_RESULT) {
610 throw new IllegalArgumentException("mode is invalid");
Jeff Brown7fbdc842010-06-17 20:52:56 -0700611 }
Jeff Brown6ec402b2010-07-28 15:48:59 -0700612
Jeff Brownac143512012-04-05 18:57:33 -0700613 final int pid = Binder.getCallingPid();
614 final int uid = Binder.getCallingUid();
615 final long ident = Binder.clearCallingIdentity();
616 final int result;
617 try {
Jeff Brownca9bc702014-02-11 14:32:56 -0800618 result = nativeInjectInputEvent(mPtr, event, displayId, pid, uid, mode,
Jeff Brownac143512012-04-05 18:57:33 -0700619 INJECTION_TIMEOUT_MILLIS, WindowManagerPolicy.FLAG_DISABLE_KEY_REPEAT);
620 } finally {
621 Binder.restoreCallingIdentity(ident);
622 }
623 switch (result) {
624 case INPUT_EVENT_INJECTION_PERMISSION_DENIED:
625 Slog.w(TAG, "Input event injection from pid " + pid + " permission denied.");
626 throw new SecurityException(
627 "Injecting to another application requires INJECT_EVENTS permission");
628 case INPUT_EVENT_INJECTION_SUCCEEDED:
629 return true;
630 case INPUT_EVENT_INJECTION_TIMED_OUT:
631 Slog.w(TAG, "Input event injection from pid " + pid + " timed out.");
632 return false;
633 case INPUT_EVENT_INJECTION_FAILED:
634 default:
635 Slog.w(TAG, "Input event injection from pid " + pid + " failed.");
636 return false;
637 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700638 }
Jeff Brown0029c662011-03-30 02:25:18 -0700639
Jeff Brown8d608662010-08-30 03:02:23 -0700640 /**
641 * Gets information about the input device with the specified id.
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700642 * @param deviceId The device id.
Jeff Brown8d608662010-08-30 03:02:23 -0700643 * @return The input device or null if not found.
644 */
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700645 @Override // Binder call
Jeff Brown8d608662010-08-30 03:02:23 -0700646 public InputDevice getInputDevice(int deviceId) {
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700647 synchronized (mInputDevicesLock) {
648 final int count = mInputDevices.length;
649 for (int i = 0; i < count; i++) {
650 final InputDevice inputDevice = mInputDevices[i];
651 if (inputDevice.getId() == deviceId) {
652 return inputDevice;
653 }
654 }
655 }
656 return null;
Jeff Brown8d608662010-08-30 03:02:23 -0700657 }
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700658
Siarhei Vishniakoua7f99b52017-03-21 17:39:40 -0700659 // Binder call
660 @Override
661 public boolean isInputDeviceEnabled(int deviceId) {
662 return nativeIsInputDeviceEnabled(mPtr, deviceId);
663 }
664
665 // Binder call
666 @Override
667 public void enableInputDevice(int deviceId) {
668 if (!checkCallingPermission(android.Manifest.permission.DISABLE_INPUT_DEVICE,
669 "enableInputDevice()")) {
670 throw new SecurityException("Requires DISABLE_INPUT_DEVICE permission");
671 }
672 nativeEnableInputDevice(mPtr, deviceId);
673 }
674
675 // Binder call
676 @Override
677 public void disableInputDevice(int deviceId) {
678 if (!checkCallingPermission(android.Manifest.permission.DISABLE_INPUT_DEVICE,
679 "disableInputDevice()")) {
680 throw new SecurityException("Requires DISABLE_INPUT_DEVICE permission");
681 }
682 nativeDisableInputDevice(mPtr, deviceId);
683 }
684
Jeff Brown8d608662010-08-30 03:02:23 -0700685 /**
686 * Gets the ids of all input devices in the system.
687 * @return The input device ids.
688 */
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700689 @Override // Binder call
Jeff Brown8d608662010-08-30 03:02:23 -0700690 public int[] getInputDeviceIds() {
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700691 synchronized (mInputDevicesLock) {
692 final int count = mInputDevices.length;
693 int[] ids = new int[count];
694 for (int i = 0; i < count; i++) {
695 ids[i] = mInputDevices[i].getId();
696 }
697 return ids;
698 }
699 }
700
Jeff Browndaa37532012-05-01 15:54:03 -0700701 /**
702 * Gets all input devices in the system.
703 * @return The array of input devices.
704 */
705 public InputDevice[] getInputDevices() {
706 synchronized (mInputDevicesLock) {
707 return mInputDevices;
708 }
709 }
710
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700711 @Override // Binder call
712 public void registerInputDevicesChangedListener(IInputDevicesChangedListener listener) {
713 if (listener == null) {
714 throw new IllegalArgumentException("listener must not be null");
715 }
716
717 synchronized (mInputDevicesLock) {
718 int callingPid = Binder.getCallingPid();
719 if (mInputDevicesChangedListeners.get(callingPid) != null) {
720 throw new SecurityException("The calling process has already "
721 + "registered an InputDevicesChangedListener.");
722 }
723
724 InputDevicesChangedListenerRecord record =
725 new InputDevicesChangedListenerRecord(callingPid, listener);
726 try {
727 IBinder binder = listener.asBinder();
728 binder.linkToDeath(record, 0);
729 } catch (RemoteException ex) {
730 // give up
731 throw new RuntimeException(ex);
732 }
733
734 mInputDevicesChangedListeners.put(callingPid, record);
735 }
736 }
737
738 private void onInputDevicesChangedListenerDied(int pid) {
739 synchronized (mInputDevicesLock) {
740 mInputDevicesChangedListeners.remove(pid);
741 }
742 }
743
744 // Must be called on handler.
Jeff Browncf39bdf2012-05-18 14:41:19 -0700745 private void deliverInputDevicesChanged(InputDevice[] oldInputDevices) {
746 // Scan for changes.
747 int numFullKeyboardsAdded = 0;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700748 mTempInputDevicesChangedListenersToNotify.clear();
Jeff Browncf39bdf2012-05-18 14:41:19 -0700749 mTempFullKeyboards.clear();
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700750 final int numListeners;
751 final int[] deviceIdAndGeneration;
752 synchronized (mInputDevicesLock) {
753 if (!mInputDevicesChangedPending) {
754 return;
755 }
756 mInputDevicesChangedPending = false;
757
758 numListeners = mInputDevicesChangedListeners.size();
759 for (int i = 0; i < numListeners; i++) {
760 mTempInputDevicesChangedListenersToNotify.add(
761 mInputDevicesChangedListeners.valueAt(i));
762 }
763
764 final int numDevices = mInputDevices.length;
765 deviceIdAndGeneration = new int[numDevices * 2];
766 for (int i = 0; i < numDevices; i++) {
767 final InputDevice inputDevice = mInputDevices[i];
768 deviceIdAndGeneration[i * 2] = inputDevice.getId();
769 deviceIdAndGeneration[i * 2 + 1] = inputDevice.getGeneration();
Jeff Browncf39bdf2012-05-18 14:41:19 -0700770
Jeff Brown7e4ff4b2012-05-30 14:32:16 -0700771 if (!inputDevice.isVirtual() && inputDevice.isFullKeyboard()) {
Jeff Browncf39bdf2012-05-18 14:41:19 -0700772 if (!containsInputDeviceWithDescriptor(oldInputDevices,
773 inputDevice.getDescriptor())) {
774 mTempFullKeyboards.add(numFullKeyboardsAdded++, inputDevice);
775 } else {
776 mTempFullKeyboards.add(inputDevice);
777 }
778 }
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700779 }
780 }
781
Jeff Browncf39bdf2012-05-18 14:41:19 -0700782 // Notify listeners.
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700783 for (int i = 0; i < numListeners; i++) {
784 mTempInputDevicesChangedListenersToNotify.get(i).notifyInputDevicesChanged(
785 deviceIdAndGeneration);
786 }
Jeff Browncf39bdf2012-05-18 14:41:19 -0700787 mTempInputDevicesChangedListenersToNotify.clear();
788
789 // Check for missing keyboard layouts.
Michael Wright07483422015-10-30 16:20:13 +0000790 List<InputDevice> keyboardsMissingLayout = new ArrayList<>();
791 final int numFullKeyboards = mTempFullKeyboards.size();
792 synchronized (mDataStore) {
793 for (int i = 0; i < numFullKeyboards; i++) {
794 final InputDevice inputDevice = mTempFullKeyboards.get(i);
795 String layout =
796 getCurrentKeyboardLayoutForInputDevice(inputDevice.getIdentifier());
797 if (layout == null) {
798 layout = getDefaultKeyboardLayout(inputDevice);
799 if (layout != null) {
800 setCurrentKeyboardLayoutForInputDevice(
801 inputDevice.getIdentifier(), layout);
Jeff Browncf39bdf2012-05-18 14:41:19 -0700802 }
803 }
Michael Wright07483422015-10-30 16:20:13 +0000804 if (layout == null) {
805 keyboardsMissingLayout.add(inputDevice);
806 }
Jeff Browncf39bdf2012-05-18 14:41:19 -0700807 }
Michael Wright07483422015-10-30 16:20:13 +0000808 }
809
810 if (mNotificationManager != null) {
811 if (!keyboardsMissingLayout.isEmpty()) {
812 if (keyboardsMissingLayout.size() > 1) {
813 // We have more than one keyboard missing a layout, so drop the
814 // user at the generic input methods page so they can pick which
815 // one to set.
816 showMissingKeyboardLayoutNotification(null);
817 } else {
818 showMissingKeyboardLayoutNotification(keyboardsMissingLayout.get(0));
Jeff Browncf39bdf2012-05-18 14:41:19 -0700819 }
820 } else if (mKeyboardLayoutNotificationShown) {
821 hideMissingKeyboardLayoutNotification();
822 }
823 }
824 mTempFullKeyboards.clear();
825 }
826
Michael Wright07483422015-10-30 16:20:13 +0000827 private String getDefaultKeyboardLayout(final InputDevice d) {
828 final Locale systemLocale = mContext.getResources().getConfiguration().locale;
829 // If our locale doesn't have a language for some reason, then we don't really have a
830 // reasonable default.
831 if (TextUtils.isEmpty(systemLocale.getLanguage())) {
832 return null;
833 }
834 final List<KeyboardLayout> layouts = new ArrayList<>();
835 visitAllKeyboardLayouts(new KeyboardLayoutVisitor() {
836 @Override
837 public void visitKeyboardLayout(Resources resources,
838 int keyboardLayoutResId, KeyboardLayout layout) {
839 // Only select a default when we know the layout is appropriate. For now, this
840 // means its a custom layout for a specific keyboard.
841 if (layout.getVendorId() != d.getVendorId()
842 || layout.getProductId() != d.getProductId()) {
843 return;
844 }
Yohei Yukawa5660fad2016-01-27 22:04:09 -0800845 final LocaleList locales = layout.getLocales();
846 final int numLocales = locales.size();
847 for (int localeIndex = 0; localeIndex < numLocales; ++localeIndex) {
848 if (isCompatibleLocale(systemLocale, locales.get(localeIndex))) {
Michael Wright07483422015-10-30 16:20:13 +0000849 layouts.add(layout);
850 break;
851 }
852 }
853 }
854 });
855
856 if (layouts.isEmpty()) {
857 return null;
858 }
859
860 // First sort so that ones with higher priority are listed at the top
861 Collections.sort(layouts);
862 // Next we want to try to find an exact match of language, country and variant.
863 final int N = layouts.size();
864 for (int i = 0; i < N; i++) {
865 KeyboardLayout layout = layouts.get(i);
Yohei Yukawa5660fad2016-01-27 22:04:09 -0800866 final LocaleList locales = layout.getLocales();
867 final int numLocales = locales.size();
868 for (int localeIndex = 0; localeIndex < numLocales; ++localeIndex) {
869 final Locale locale = locales.get(localeIndex);
870 if (locale.getCountry().equals(systemLocale.getCountry())
871 && locale.getVariant().equals(systemLocale.getVariant())) {
Michael Wright07483422015-10-30 16:20:13 +0000872 return layout.getDescriptor();
873 }
874 }
875 }
876 // Then try an exact match of language and country
877 for (int i = 0; i < N; i++) {
878 KeyboardLayout layout = layouts.get(i);
Yohei Yukawa5660fad2016-01-27 22:04:09 -0800879 final LocaleList locales = layout.getLocales();
880 final int numLocales = locales.size();
881 for (int localeIndex = 0; localeIndex < numLocales; ++localeIndex) {
882 final Locale locale = locales.get(localeIndex);
883 if (locale.getCountry().equals(systemLocale.getCountry())) {
Michael Wright07483422015-10-30 16:20:13 +0000884 return layout.getDescriptor();
885 }
886 }
887 }
888
889 // Give up and just use the highest priority layout with matching language
890 return layouts.get(0).getDescriptor();
891 }
892
893 private static boolean isCompatibleLocale(Locale systemLocale, Locale keyboardLocale) {
894 // Different languages are never compatible
895 if (!systemLocale.getLanguage().equals(keyboardLocale.getLanguage())) {
896 return false;
897 }
898 // If both the system and the keyboard layout have a country specifier, they must be equal.
899 if (!TextUtils.isEmpty(systemLocale.getCountry())
900 && !TextUtils.isEmpty(keyboardLocale.getCountry())
901 && !systemLocale.getCountry().equals(keyboardLocale.getCountry())) {
902 return false;
903 }
904 return true;
905 }
906
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800907 @Override // Binder call & native callback
Jason Gerecked5220742014-03-10 09:47:59 -0700908 public TouchCalibration getTouchCalibrationForInputDevice(String inputDeviceDescriptor,
909 int surfaceRotation) {
Jason Gerecked6396d62014-01-27 18:30:37 -0800910 if (inputDeviceDescriptor == null) {
911 throw new IllegalArgumentException("inputDeviceDescriptor must not be null");
912 }
913
914 synchronized (mDataStore) {
Jason Gerecked5220742014-03-10 09:47:59 -0700915 return mDataStore.getTouchCalibration(inputDeviceDescriptor, surfaceRotation);
Jason Gerecked6396d62014-01-27 18:30:37 -0800916 }
917 }
918
919 @Override // Binder call
Jason Gerecked5220742014-03-10 09:47:59 -0700920 public void setTouchCalibrationForInputDevice(String inputDeviceDescriptor, int surfaceRotation,
Jason Gerecked6396d62014-01-27 18:30:37 -0800921 TouchCalibration calibration) {
922 if (!checkCallingPermission(android.Manifest.permission.SET_INPUT_CALIBRATION,
923 "setTouchCalibrationForInputDevice()")) {
924 throw new SecurityException("Requires SET_INPUT_CALIBRATION permission");
925 }
926 if (inputDeviceDescriptor == null) {
927 throw new IllegalArgumentException("inputDeviceDescriptor must not be null");
928 }
929 if (calibration == null) {
930 throw new IllegalArgumentException("calibration must not be null");
931 }
Jason Gerecked5220742014-03-10 09:47:59 -0700932 if (surfaceRotation < Surface.ROTATION_0 || surfaceRotation > Surface.ROTATION_270) {
933 throw new IllegalArgumentException("surfaceRotation value out of bounds");
934 }
Jason Gerecked6396d62014-01-27 18:30:37 -0800935
936 synchronized (mDataStore) {
937 try {
Jason Gerecked5220742014-03-10 09:47:59 -0700938 if (mDataStore.setTouchCalibration(inputDeviceDescriptor, surfaceRotation,
939 calibration)) {
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800940 nativeReloadCalibration(mPtr);
941 }
Jason Gerecked6396d62014-01-27 18:30:37 -0800942 } finally {
943 mDataStore.saveIfNeeded();
944 }
945 }
946 }
947
Michael Wright39e5e942015-08-19 22:52:47 +0100948 @Override // Binder call
Michael Wright9209c9c2015-09-03 17:57:01 +0100949 public int isInTabletMode() {
950 if (!checkCallingPermission(android.Manifest.permission.TABLET_MODE,
951 "isInTabletMode()")) {
952 throw new SecurityException("Requires TABLET_MODE permission");
953 }
954 return getSwitchState(-1, InputDevice.SOURCE_ANY, SW_TABLET_MODE);
955 }
956
957 @Override // Binder call
Michael Wright39e5e942015-08-19 22:52:47 +0100958 public void registerTabletModeChangedListener(ITabletModeChangedListener listener) {
Michael Wright9209c9c2015-09-03 17:57:01 +0100959 if (!checkCallingPermission(android.Manifest.permission.TABLET_MODE,
Michael Wright39e5e942015-08-19 22:52:47 +0100960 "registerTabletModeChangedListener()")) {
961 throw new SecurityException("Requires TABLET_MODE_LISTENER permission");
962 }
963 if (listener == null) {
964 throw new IllegalArgumentException("listener must not be null");
965 }
966
967 synchronized (mTabletModeLock) {
968 final int callingPid = Binder.getCallingPid();
969 if (mTabletModeChangedListeners.get(callingPid) != null) {
970 throw new IllegalStateException("The calling process has already registered "
971 + "a TabletModeChangedListener.");
972 }
973 TabletModeChangedListenerRecord record =
974 new TabletModeChangedListenerRecord(callingPid, listener);
975 try {
976 IBinder binder = listener.asBinder();
977 binder.linkToDeath(record, 0);
978 } catch (RemoteException ex) {
979 throw new RuntimeException(ex);
980 }
981 mTabletModeChangedListeners.put(callingPid, record);
982 }
983 }
984
985 private void onTabletModeChangedListenerDied(int pid) {
986 synchronized (mTabletModeLock) {
987 mTabletModeChangedListeners.remove(pid);
988 }
989 }
990
991 // Must be called on handler
992 private void deliverTabletModeChanged(long whenNanos, boolean inTabletMode) {
993 mTempTabletModeChangedListenersToNotify.clear();
994 final int numListeners;
995 synchronized (mTabletModeLock) {
996 numListeners = mTabletModeChangedListeners.size();
997 for (int i = 0; i < numListeners; i++) {
998 mTempTabletModeChangedListenersToNotify.add(
999 mTabletModeChangedListeners.valueAt(i));
1000 }
1001 }
1002 for (int i = 0; i < numListeners; i++) {
1003 mTempTabletModeChangedListenersToNotify.get(i).notifyTabletModeChanged(
1004 whenNanos, inTabletMode);
1005 }
1006 }
1007
Jeff Browncf39bdf2012-05-18 14:41:19 -07001008 // Must be called on handler.
Michael Wrightc93fbd12014-09-22 20:07:59 -07001009 private void showMissingKeyboardLayoutNotification(InputDevice device) {
Jeff Browncf39bdf2012-05-18 14:41:19 -07001010 if (!mKeyboardLayoutNotificationShown) {
Yohei Yukawa2bff4902016-03-30 01:06:37 -07001011 final Intent intent = new Intent(Settings.ACTION_HARD_KEYBOARD_SETTINGS);
Michael Wrightc93fbd12014-09-22 20:07:59 -07001012 if (device != null) {
1013 intent.putExtra(Settings.EXTRA_INPUT_DEVICE_IDENTIFIER, device.getIdentifier());
Jeff Browncf39bdf2012-05-18 14:41:19 -07001014 }
Michael Wrightc93fbd12014-09-22 20:07:59 -07001015 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1016 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
1017 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
1018 final PendingIntent keyboardLayoutIntent = PendingIntent.getActivityAsUser(mContext, 0,
1019 intent, 0, null, UserHandle.CURRENT);
Jeff Browncf39bdf2012-05-18 14:41:19 -07001020
1021 Resources r = mContext.getResources();
Geoffrey Pitschaf759c52017-02-15 09:35:38 -05001022 Notification notification =
1023 new Notification.Builder(mContext, SystemNotificationChannels.PHYSICAL_KEYBOARD)
1024 .setContentTitle(r.getString(
1025 R.string.select_keyboard_layout_notification_title))
1026 .setContentText(r.getString(
1027 R.string.select_keyboard_layout_notification_message))
1028 .setContentIntent(keyboardLayoutIntent)
1029 .setSmallIcon(R.drawable.ic_settings_language)
1030 .setColor(mContext.getColor(
1031 com.android.internal.R.color.system_notification_accent_color))
1032 .build();
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -07001033 mNotificationManager.notifyAsUser(null,
Chris Wren282cfef2017-03-27 15:01:44 -04001034 SystemMessage.NOTE_SELECT_KEYBOARD_LAYOUT,
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -07001035 notification, UserHandle.ALL);
Jeff Browncf39bdf2012-05-18 14:41:19 -07001036 mKeyboardLayoutNotificationShown = true;
1037 }
1038 }
1039
1040 // Must be called on handler.
1041 private void hideMissingKeyboardLayoutNotification() {
1042 if (mKeyboardLayoutNotificationShown) {
1043 mKeyboardLayoutNotificationShown = false;
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -07001044 mNotificationManager.cancelAsUser(null,
Chris Wren282cfef2017-03-27 15:01:44 -04001045 SystemMessage.NOTE_SELECT_KEYBOARD_LAYOUT,
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -07001046 UserHandle.ALL);
Jeff Browncf39bdf2012-05-18 14:41:19 -07001047 }
1048 }
1049
1050 // Must be called on handler.
1051 private void updateKeyboardLayouts() {
1052 // Scan all input devices state for keyboard layouts that have been uninstalled.
1053 final HashSet<String> availableKeyboardLayouts = new HashSet<String>();
1054 visitAllKeyboardLayouts(new KeyboardLayoutVisitor() {
1055 @Override
Michael Wright07483422015-10-30 16:20:13 +00001056 public void visitKeyboardLayout(Resources resources,
1057 int keyboardLayoutResId, KeyboardLayout layout) {
1058 availableKeyboardLayouts.add(layout.getDescriptor());
Jeff Browncf39bdf2012-05-18 14:41:19 -07001059 }
1060 });
1061 synchronized (mDataStore) {
1062 try {
1063 mDataStore.removeUninstalledKeyboardLayouts(availableKeyboardLayouts);
1064 } finally {
1065 mDataStore.saveIfNeeded();
1066 }
1067 }
1068
1069 // Reload keyboard layouts.
1070 reloadKeyboardLayouts();
1071 }
1072
Jeff Browncf39bdf2012-05-18 14:41:19 -07001073 private static boolean containsInputDeviceWithDescriptor(InputDevice[] inputDevices,
1074 String descriptor) {
1075 final int numDevices = inputDevices.length;
1076 for (int i = 0; i < numDevices; i++) {
1077 final InputDevice inputDevice = inputDevices[i];
1078 if (inputDevice.getDescriptor().equals(descriptor)) {
1079 return true;
1080 }
1081 }
1082 return false;
Jeff Brown8d608662010-08-30 03:02:23 -07001083 }
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001084
1085 @Override // Binder call
1086 public KeyboardLayout[] getKeyboardLayouts() {
Jeff Brown6ec6f792012-04-17 16:52:41 -07001087 final ArrayList<KeyboardLayout> list = new ArrayList<KeyboardLayout>();
1088 visitAllKeyboardLayouts(new KeyboardLayoutVisitor() {
1089 @Override
Michael Wright07483422015-10-30 16:20:13 +00001090 public void visitKeyboardLayout(Resources resources,
1091 int keyboardLayoutResId, KeyboardLayout layout) {
1092 list.add(layout);
1093 }
1094 });
1095 return list.toArray(new KeyboardLayout[list.size()]);
1096 }
1097
Michael Wrightb0e804a2016-01-04 16:48:53 -05001098 @Override // Binder call
Michael Wright07483422015-10-30 16:20:13 +00001099 public KeyboardLayout[] getKeyboardLayoutsForInputDevice(
1100 final InputDeviceIdentifier identifier) {
Michael Wrightb0e804a2016-01-04 16:48:53 -05001101 final String[] enabledLayoutDescriptors =
1102 getEnabledKeyboardLayoutsForInputDevice(identifier);
1103 final ArrayList<KeyboardLayout> enabledLayouts =
1104 new ArrayList<KeyboardLayout>(enabledLayoutDescriptors.length);
1105 final ArrayList<KeyboardLayout> potentialLayouts = new ArrayList<KeyboardLayout>();
Michael Wright07483422015-10-30 16:20:13 +00001106 visitAllKeyboardLayouts(new KeyboardLayoutVisitor() {
1107 boolean mHasSeenDeviceSpecificLayout;
1108
1109 @Override
1110 public void visitKeyboardLayout(Resources resources,
1111 int keyboardLayoutResId, KeyboardLayout layout) {
Michael Wrightb0e804a2016-01-04 16:48:53 -05001112 // First check if it's enabled. If the keyboard layout is enabled then we always
1113 // want to return it as a possible layout for the device.
1114 for (String s : enabledLayoutDescriptors) {
1115 if (s != null && s.equals(layout.getDescriptor())) {
1116 enabledLayouts.add(layout);
1117 return;
1118 }
1119 }
1120 // Next find any potential layouts that aren't yet enabled for the device. For
1121 // devices that have special layouts we assume there's a reason that the generic
1122 // layouts don't work for them so we don't want to return them since it's likely
1123 // to result in a poor user experience.
Michael Wright07483422015-10-30 16:20:13 +00001124 if (layout.getVendorId() == identifier.getVendorId()
1125 && layout.getProductId() == identifier.getProductId()) {
1126 if (!mHasSeenDeviceSpecificLayout) {
1127 mHasSeenDeviceSpecificLayout = true;
Michael Wrightb0e804a2016-01-04 16:48:53 -05001128 potentialLayouts.clear();
Michael Wright07483422015-10-30 16:20:13 +00001129 }
Michael Wrightb0e804a2016-01-04 16:48:53 -05001130 potentialLayouts.add(layout);
Michael Wright07483422015-10-30 16:20:13 +00001131 } else if (layout.getVendorId() == -1 && layout.getProductId() == -1
1132 && !mHasSeenDeviceSpecificLayout) {
Michael Wrightb0e804a2016-01-04 16:48:53 -05001133 potentialLayouts.add(layout);
Michael Wright07483422015-10-30 16:20:13 +00001134 }
Jeff Brown6ec6f792012-04-17 16:52:41 -07001135 }
1136 });
Michael Wrightb0e804a2016-01-04 16:48:53 -05001137 final int enabledLayoutSize = enabledLayouts.size();
1138 final int potentialLayoutSize = potentialLayouts.size();
1139 KeyboardLayout[] layouts = new KeyboardLayout[enabledLayoutSize + potentialLayoutSize];
1140 enabledLayouts.toArray(layouts);
1141 for (int i = 0; i < potentialLayoutSize; i++) {
1142 layouts[enabledLayoutSize + i] = potentialLayouts.get(i);
1143 }
1144 return layouts;
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001145 }
1146
1147 @Override // Binder call
1148 public KeyboardLayout getKeyboardLayout(String keyboardLayoutDescriptor) {
1149 if (keyboardLayoutDescriptor == null) {
1150 throw new IllegalArgumentException("keyboardLayoutDescriptor must not be null");
1151 }
1152
Jeff Brown6ec6f792012-04-17 16:52:41 -07001153 final KeyboardLayout[] result = new KeyboardLayout[1];
1154 visitKeyboardLayout(keyboardLayoutDescriptor, new KeyboardLayoutVisitor() {
1155 @Override
Michael Wright07483422015-10-30 16:20:13 +00001156 public void visitKeyboardLayout(Resources resources,
1157 int keyboardLayoutResId, KeyboardLayout layout) {
1158 result[0] = layout;
Jeff Brown6ec6f792012-04-17 16:52:41 -07001159 }
1160 });
1161 if (result[0] == null) {
Michael Wright07483422015-10-30 16:20:13 +00001162 Slog.w(TAG, "Could not get keyboard layout with descriptor '"
Jeff Brown6ec6f792012-04-17 16:52:41 -07001163 + keyboardLayoutDescriptor + "'.");
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001164 }
Jeff Brown6ec6f792012-04-17 16:52:41 -07001165 return result[0];
1166 }
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001167
Jeff Brown6ec6f792012-04-17 16:52:41 -07001168 private void visitAllKeyboardLayouts(KeyboardLayoutVisitor visitor) {
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001169 final PackageManager pm = mContext.getPackageManager();
Jeff Brown6ec6f792012-04-17 16:52:41 -07001170 Intent intent = new Intent(InputManager.ACTION_QUERY_KEYBOARD_LAYOUTS);
1171 for (ResolveInfo resolveInfo : pm.queryBroadcastReceivers(intent,
Jeff Sharkey8a372a02016-03-16 16:25:45 -06001172 PackageManager.GET_META_DATA | PackageManager.MATCH_DIRECT_BOOT_AWARE
1173 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE)) {
Michael Wright8ebac232014-09-18 18:29:49 -07001174 final ActivityInfo activityInfo = resolveInfo.activityInfo;
1175 final int priority = resolveInfo.priority;
1176 visitKeyboardLayoutsInPackage(pm, activityInfo, null, priority, visitor);
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001177 }
1178 }
1179
Jeff Brown6ec6f792012-04-17 16:52:41 -07001180 private void visitKeyboardLayout(String keyboardLayoutDescriptor,
1181 KeyboardLayoutVisitor visitor) {
1182 KeyboardLayoutDescriptor d = KeyboardLayoutDescriptor.parse(keyboardLayoutDescriptor);
1183 if (d != null) {
1184 final PackageManager pm = mContext.getPackageManager();
1185 try {
1186 ActivityInfo receiver = pm.getReceiverInfo(
1187 new ComponentName(d.packageName, d.receiverName),
Jeff Sharkey5217cac2015-12-20 15:34:01 -07001188 PackageManager.GET_META_DATA
Jeff Sharkey8a372a02016-03-16 16:25:45 -06001189 | PackageManager.MATCH_DIRECT_BOOT_AWARE
1190 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE);
Michael Wright8ebac232014-09-18 18:29:49 -07001191 visitKeyboardLayoutsInPackage(pm, receiver, d.keyboardLayoutName, 0, visitor);
Jeff Brown6ec6f792012-04-17 16:52:41 -07001192 } catch (NameNotFoundException ex) {
1193 }
1194 }
1195 }
1196
1197 private void visitKeyboardLayoutsInPackage(PackageManager pm, ActivityInfo receiver,
Michael Wright8ebac232014-09-18 18:29:49 -07001198 String keyboardName, int requestedPriority, KeyboardLayoutVisitor visitor) {
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001199 Bundle metaData = receiver.metaData;
1200 if (metaData == null) {
Jeff Brown6ec6f792012-04-17 16:52:41 -07001201 return;
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001202 }
1203
1204 int configResId = metaData.getInt(InputManager.META_DATA_KEYBOARD_LAYOUTS);
1205 if (configResId == 0) {
Michael Wright07483422015-10-30 16:20:13 +00001206 Slog.w(TAG, "Missing meta-data '" + InputManager.META_DATA_KEYBOARD_LAYOUTS
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001207 + "' on receiver " + receiver.packageName + "/" + receiver.name);
Jeff Brown6ec6f792012-04-17 16:52:41 -07001208 return;
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001209 }
1210
Jeff Brownd9fec5d2012-05-17 16:01:54 -07001211 CharSequence receiverLabel = receiver.loadLabel(pm);
1212 String collection = receiverLabel != null ? receiverLabel.toString() : "";
Michael Wright8ebac232014-09-18 18:29:49 -07001213 int priority;
1214 if ((receiver.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
1215 priority = requestedPriority;
1216 } else {
1217 priority = 0;
1218 }
Jeff Brownd9fec5d2012-05-17 16:01:54 -07001219
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001220 try {
1221 Resources resources = pm.getResourcesForApplication(receiver.applicationInfo);
1222 XmlResourceParser parser = resources.getXml(configResId);
1223 try {
1224 XmlUtils.beginDocument(parser, "keyboard-layouts");
1225
1226 for (;;) {
1227 XmlUtils.nextElement(parser);
1228 String element = parser.getName();
1229 if (element == null) {
1230 break;
1231 }
1232 if (element.equals("keyboard-layout")) {
1233 TypedArray a = resources.obtainAttributes(
1234 parser, com.android.internal.R.styleable.KeyboardLayout);
1235 try {
1236 String name = a.getString(
1237 com.android.internal.R.styleable.KeyboardLayout_name);
1238 String label = a.getString(
1239 com.android.internal.R.styleable.KeyboardLayout_label);
Jeff Brown2f095762012-05-10 21:29:33 -07001240 int keyboardLayoutResId = a.getResourceId(
1241 com.android.internal.R.styleable.KeyboardLayout_keyboardLayout,
1242 0);
Michael Wright07483422015-10-30 16:20:13 +00001243 String languageTags = a.getString(
1244 com.android.internal.R.styleable.KeyboardLayout_locale);
Yohei Yukawa5660fad2016-01-27 22:04:09 -08001245 LocaleList locales = getLocalesFromLanguageTags(languageTags);
Michael Wright07483422015-10-30 16:20:13 +00001246 int vid = a.getInt(
1247 com.android.internal.R.styleable.KeyboardLayout_vendorId, -1);
1248 int pid = a.getInt(
1249 com.android.internal.R.styleable.KeyboardLayout_productId, -1);
1250
Jeff Brown2f095762012-05-10 21:29:33 -07001251 if (name == null || label == null || keyboardLayoutResId == 0) {
Michael Wright07483422015-10-30 16:20:13 +00001252 Slog.w(TAG, "Missing required 'name', 'label' or 'keyboardLayout' "
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001253 + "attributes in keyboard layout "
1254 + "resource from receiver "
1255 + receiver.packageName + "/" + receiver.name);
1256 } else {
1257 String descriptor = KeyboardLayoutDescriptor.format(
1258 receiver.packageName, receiver.name, name);
Jeff Brown6ec6f792012-04-17 16:52:41 -07001259 if (keyboardName == null || name.equals(keyboardName)) {
Michael Wright07483422015-10-30 16:20:13 +00001260 KeyboardLayout layout = new KeyboardLayout(
1261 descriptor, label, collection, priority,
1262 locales, vid, pid);
1263 visitor.visitKeyboardLayout(
1264 resources, keyboardLayoutResId, layout);
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001265 }
1266 }
1267 } finally {
1268 a.recycle();
1269 }
1270 } else {
Michael Wright07483422015-10-30 16:20:13 +00001271 Slog.w(TAG, "Skipping unrecognized element '" + element
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001272 + "' in keyboard layout resource from receiver "
1273 + receiver.packageName + "/" + receiver.name);
1274 }
1275 }
1276 } finally {
1277 parser.close();
1278 }
1279 } catch (Exception ex) {
Michael Wright07483422015-10-30 16:20:13 +00001280 Slog.w(TAG, "Could not parse keyboard layout resource from receiver "
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001281 + receiver.packageName + "/" + receiver.name, ex);
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001282 }
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001283 }
1284
Yohei Yukawa5660fad2016-01-27 22:04:09 -08001285 @NonNull
1286 private static LocaleList getLocalesFromLanguageTags(String languageTags) {
Michael Wright07483422015-10-30 16:20:13 +00001287 if (TextUtils.isEmpty(languageTags)) {
Yohei Yukawa5660fad2016-01-27 22:04:09 -08001288 return LocaleList.getEmptyLocaleList();
Michael Wright07483422015-10-30 16:20:13 +00001289 }
Yohei Yukawa5660fad2016-01-27 22:04:09 -08001290 return LocaleList.forLanguageTags(languageTags.replace('|', ','));
Michael Wright07483422015-10-30 16:20:13 +00001291 }
1292
RoboErikfb290df2013-12-16 11:27:55 -08001293 /**
1294 * Builds a layout descriptor for the vendor/product. This returns the
1295 * descriptor for ids that aren't useful (such as the default 0, 0).
1296 */
1297 private String getLayoutDescriptor(InputDeviceIdentifier identifier) {
1298 if (identifier == null || identifier.getDescriptor() == null) {
1299 throw new IllegalArgumentException("identifier and descriptor must not be null");
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001300 }
1301
RoboErikfb290df2013-12-16 11:27:55 -08001302 if (identifier.getVendorId() == 0 && identifier.getProductId() == 0) {
1303 return identifier.getDescriptor();
1304 }
1305 StringBuilder bob = new StringBuilder();
1306 bob.append("vendor:").append(identifier.getVendorId());
1307 bob.append(",product:").append(identifier.getProductId());
1308 return bob.toString();
1309 }
1310
1311 @Override // Binder call
1312 public String getCurrentKeyboardLayoutForInputDevice(InputDeviceIdentifier identifier) {
1313
1314 String key = getLayoutDescriptor(identifier);
Jeff Browna3bc5652012-04-17 11:42:25 -07001315 synchronized (mDataStore) {
RoboErikfb290df2013-12-16 11:27:55 -08001316 String layout = null;
1317 // try loading it using the layout descriptor if we have it
1318 layout = mDataStore.getCurrentKeyboardLayout(key);
1319 if (layout == null && !key.equals(identifier.getDescriptor())) {
1320 // if it doesn't exist fall back to the device descriptor
1321 layout = mDataStore.getCurrentKeyboardLayout(identifier.getDescriptor());
1322 }
1323 if (DEBUG) {
1324 Slog.d(TAG, "Loaded keyboard layout id for " + key + " and got "
1325 + layout);
1326 }
1327 return layout;
Jeff Browna3bc5652012-04-17 11:42:25 -07001328 }
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001329 }
1330
1331 @Override // Binder call
RoboErikfb290df2013-12-16 11:27:55 -08001332 public void setCurrentKeyboardLayoutForInputDevice(InputDeviceIdentifier identifier,
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001333 String keyboardLayoutDescriptor) {
1334 if (!checkCallingPermission(android.Manifest.permission.SET_KEYBOARD_LAYOUT,
Jeff Browncf39bdf2012-05-18 14:41:19 -07001335 "setCurrentKeyboardLayoutForInputDevice()")) {
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001336 throw new SecurityException("Requires SET_KEYBOARD_LAYOUT permission");
1337 }
Jeff Browncf39bdf2012-05-18 14:41:19 -07001338 if (keyboardLayoutDescriptor == null) {
1339 throw new IllegalArgumentException("keyboardLayoutDescriptor must not be null");
1340 }
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001341
RoboErikfb290df2013-12-16 11:27:55 -08001342 String key = getLayoutDescriptor(identifier);
Jeff Browna3bc5652012-04-17 11:42:25 -07001343 synchronized (mDataStore) {
1344 try {
RoboErikfb290df2013-12-16 11:27:55 -08001345 if (mDataStore.setCurrentKeyboardLayout(key, keyboardLayoutDescriptor)) {
1346 if (DEBUG) {
1347 Slog.d(TAG, "Saved keyboard layout using " + key);
1348 }
Jeff Browncf39bdf2012-05-18 14:41:19 -07001349 mHandler.sendEmptyMessage(MSG_RELOAD_KEYBOARD_LAYOUTS);
1350 }
Jeff Browna3bc5652012-04-17 11:42:25 -07001351 } finally {
1352 mDataStore.saveIfNeeded();
1353 }
1354 }
Jeff Browncf39bdf2012-05-18 14:41:19 -07001355 }
Jeff Brown6ec6f792012-04-17 16:52:41 -07001356
Jeff Browncf39bdf2012-05-18 14:41:19 -07001357 @Override // Binder call
Michael Wright07483422015-10-30 16:20:13 +00001358 public String[] getEnabledKeyboardLayoutsForInputDevice(InputDeviceIdentifier identifier) {
RoboErikfb290df2013-12-16 11:27:55 -08001359 String key = getLayoutDescriptor(identifier);
Jeff Browncf39bdf2012-05-18 14:41:19 -07001360 synchronized (mDataStore) {
RoboErikfb290df2013-12-16 11:27:55 -08001361 String[] layouts = mDataStore.getKeyboardLayouts(key);
1362 if ((layouts == null || layouts.length == 0)
1363 && !key.equals(identifier.getDescriptor())) {
1364 layouts = mDataStore.getKeyboardLayouts(identifier.getDescriptor());
1365 }
1366 return layouts;
Jeff Browncf39bdf2012-05-18 14:41:19 -07001367 }
1368 }
1369
1370 @Override // Binder call
Michael Wrightd5f7ed92016-01-19 11:23:51 -08001371 @Nullable
1372 public KeyboardLayout getKeyboardLayoutForInputDevice(InputDeviceIdentifier identifier,
1373 InputMethodInfo imeInfo, InputMethodSubtype imeSubtype) {
1374 InputMethodSubtypeHandle handle = new InputMethodSubtypeHandle(imeInfo, imeSubtype);
1375 String key = getLayoutDescriptor(identifier);
1376 final String keyboardLayoutDescriptor;
1377 synchronized (mDataStore) {
1378 keyboardLayoutDescriptor = mDataStore.getKeyboardLayout(key, handle);
1379 }
1380
1381 if (keyboardLayoutDescriptor == null) {
1382 return null;
1383 }
1384
1385 final KeyboardLayout[] result = new KeyboardLayout[1];
1386 visitKeyboardLayout(keyboardLayoutDescriptor, new KeyboardLayoutVisitor() {
1387 @Override
1388 public void visitKeyboardLayout(Resources resources,
1389 int keyboardLayoutResId, KeyboardLayout layout) {
1390 result[0] = layout;
1391 }
1392 });
1393 if (result[0] == null) {
1394 Slog.w(TAG, "Could not get keyboard layout with descriptor '"
1395 + keyboardLayoutDescriptor + "'.");
1396 }
1397 return result[0];
1398 }
1399
1400 @Override
1401 public void setKeyboardLayoutForInputDevice(InputDeviceIdentifier identifier,
1402 InputMethodInfo imeInfo, InputMethodSubtype imeSubtype,
1403 String keyboardLayoutDescriptor) {
1404 if (!checkCallingPermission(android.Manifest.permission.SET_KEYBOARD_LAYOUT,
1405 "setKeyboardLayoutForInputDevice()")) {
1406 throw new SecurityException("Requires SET_KEYBOARD_LAYOUT permission");
1407 }
1408 if (keyboardLayoutDescriptor == null) {
1409 throw new IllegalArgumentException("keyboardLayoutDescriptor must not be null");
1410 }
Yohei Yukawa46ac35d2016-04-20 16:59:45 -07001411 if (imeInfo == null) {
1412 throw new IllegalArgumentException("imeInfo must not be null");
Michael Wrightd5f7ed92016-01-19 11:23:51 -08001413 }
1414 InputMethodSubtypeHandle handle = new InputMethodSubtypeHandle(imeInfo, imeSubtype);
1415 setKeyboardLayoutForInputDeviceInner(identifier, handle, keyboardLayoutDescriptor);
1416 }
1417
1418 private void setKeyboardLayoutForInputDeviceInner(InputDeviceIdentifier identifier,
1419 InputMethodSubtypeHandle imeHandle, String keyboardLayoutDescriptor) {
1420 String key = getLayoutDescriptor(identifier);
1421 synchronized (mDataStore) {
1422 try {
1423 if (mDataStore.setKeyboardLayout(key, imeHandle, keyboardLayoutDescriptor)) {
1424 if (DEBUG) {
1425 Slog.d(TAG, "Set keyboard layout " + keyboardLayoutDescriptor +
1426 " for subtype " + imeHandle + " and device " + identifier +
1427 " using key " + key);
1428 }
1429 if (imeHandle.equals(mCurrentImeHandle)) {
1430 if (DEBUG) {
1431 Slog.d(TAG, "Layout for current subtype changed, switching layout");
1432 }
1433 SomeArgs args = SomeArgs.obtain();
1434 args.arg1 = identifier;
1435 args.arg2 = imeHandle;
1436 mHandler.obtainMessage(MSG_SWITCH_KEYBOARD_LAYOUT, args).sendToTarget();
1437 }
1438 mHandler.sendEmptyMessage(MSG_RELOAD_KEYBOARD_LAYOUTS);
1439 }
1440 } finally {
1441 mDataStore.saveIfNeeded();
1442 }
1443 }
1444 }
1445
1446 @Override // Binder call
RoboErikfb290df2013-12-16 11:27:55 -08001447 public void addKeyboardLayoutForInputDevice(InputDeviceIdentifier identifier,
Jeff Browncf39bdf2012-05-18 14:41:19 -07001448 String keyboardLayoutDescriptor) {
1449 if (!checkCallingPermission(android.Manifest.permission.SET_KEYBOARD_LAYOUT,
1450 "addKeyboardLayoutForInputDevice()")) {
1451 throw new SecurityException("Requires SET_KEYBOARD_LAYOUT permission");
1452 }
Jeff Browncf39bdf2012-05-18 14:41:19 -07001453 if (keyboardLayoutDescriptor == null) {
1454 throw new IllegalArgumentException("keyboardLayoutDescriptor must not be null");
1455 }
1456
RoboErikfb290df2013-12-16 11:27:55 -08001457 String key = getLayoutDescriptor(identifier);
Jeff Browncf39bdf2012-05-18 14:41:19 -07001458 synchronized (mDataStore) {
1459 try {
RoboErikfb290df2013-12-16 11:27:55 -08001460 String oldLayout = mDataStore.getCurrentKeyboardLayout(key);
1461 if (oldLayout == null && !key.equals(identifier.getDescriptor())) {
1462 oldLayout = mDataStore.getCurrentKeyboardLayout(identifier.getDescriptor());
1463 }
1464 if (mDataStore.addKeyboardLayout(key, keyboardLayoutDescriptor)
Narayan Kamath607223f2018-02-19 14:09:02 +00001465 && !Objects.equals(oldLayout, mDataStore.getCurrentKeyboardLayout(key))) {
Jeff Browncf39bdf2012-05-18 14:41:19 -07001466 mHandler.sendEmptyMessage(MSG_RELOAD_KEYBOARD_LAYOUTS);
1467 }
1468 } finally {
1469 mDataStore.saveIfNeeded();
Jeff Brown6ec6f792012-04-17 16:52:41 -07001470 }
Jeff Browncf39bdf2012-05-18 14:41:19 -07001471 }
1472 }
1473
1474 @Override // Binder call
RoboErikfb290df2013-12-16 11:27:55 -08001475 public void removeKeyboardLayoutForInputDevice(InputDeviceIdentifier identifier,
Jeff Browncf39bdf2012-05-18 14:41:19 -07001476 String keyboardLayoutDescriptor) {
1477 if (!checkCallingPermission(android.Manifest.permission.SET_KEYBOARD_LAYOUT,
1478 "removeKeyboardLayoutForInputDevice()")) {
1479 throw new SecurityException("Requires SET_KEYBOARD_LAYOUT permission");
1480 }
Jeff Browncf39bdf2012-05-18 14:41:19 -07001481 if (keyboardLayoutDescriptor == null) {
1482 throw new IllegalArgumentException("keyboardLayoutDescriptor must not be null");
1483 }
1484
RoboErikfb290df2013-12-16 11:27:55 -08001485 String key = getLayoutDescriptor(identifier);
Jeff Browncf39bdf2012-05-18 14:41:19 -07001486 synchronized (mDataStore) {
1487 try {
RoboErikfb290df2013-12-16 11:27:55 -08001488 String oldLayout = mDataStore.getCurrentKeyboardLayout(key);
1489 if (oldLayout == null && !key.equals(identifier.getDescriptor())) {
1490 oldLayout = mDataStore.getCurrentKeyboardLayout(identifier.getDescriptor());
1491 }
1492 boolean removed = mDataStore.removeKeyboardLayout(key, keyboardLayoutDescriptor);
1493 if (!key.equals(identifier.getDescriptor())) {
1494 // We need to remove from both places to ensure it is gone
1495 removed |= mDataStore.removeKeyboardLayout(identifier.getDescriptor(),
1496 keyboardLayoutDescriptor);
1497 }
Narayan Kamath607223f2018-02-19 14:09:02 +00001498 if (removed && !Objects.equals(oldLayout,
RoboErikfb290df2013-12-16 11:27:55 -08001499 mDataStore.getCurrentKeyboardLayout(key))) {
Jeff Browncf39bdf2012-05-18 14:41:19 -07001500 mHandler.sendEmptyMessage(MSG_RELOAD_KEYBOARD_LAYOUTS);
1501 }
1502 } finally {
1503 mDataStore.saveIfNeeded();
1504 }
1505 }
1506 }
1507
Yohei Yukawab097b822015-12-01 10:43:08 -08001508 // Must be called on handler.
1509 private void handleSwitchInputMethodSubtype(int userId,
1510 @Nullable InputMethodInfo inputMethodInfo, @Nullable InputMethodSubtype subtype) {
1511 if (DEBUG) {
1512 Slog.i(TAG, "InputMethodSubtype changed: userId=" + userId
1513 + " ime=" + inputMethodInfo + " subtype=" + subtype);
1514 }
Michael Wrightd5f7ed92016-01-19 11:23:51 -08001515 if (inputMethodInfo == null) {
1516 Slog.d(TAG, "No InputMethod is running, ignoring change");
1517 return;
1518 }
1519 if (subtype != null && !"keyboard".equals(subtype.getMode())) {
1520 Slog.d(TAG, "InputMethodSubtype changed to non-keyboard subtype, ignoring change");
1521 return;
1522 }
1523 InputMethodSubtypeHandle handle = new InputMethodSubtypeHandle(inputMethodInfo, subtype);
1524 if (!handle.equals(mCurrentImeHandle)) {
1525 mCurrentImeHandle = handle;
1526 handleSwitchKeyboardLayout(null, handle);
1527 }
Jeff Browncf39bdf2012-05-18 14:41:19 -07001528 }
1529
1530 // Must be called on handler.
Michael Wrightd5f7ed92016-01-19 11:23:51 -08001531 private void handleSwitchKeyboardLayout(@Nullable InputDeviceIdentifier identifier,
1532 InputMethodSubtypeHandle handle) {
1533 synchronized (mInputDevicesLock) {
1534 for (InputDevice device : mInputDevices) {
1535 if (identifier != null && !device.getIdentifier().equals(identifier) ||
1536 !device.isFullKeyboard()) {
1537 continue;
Jeff Browncf39bdf2012-05-18 14:41:19 -07001538 }
Michael Wrightd5f7ed92016-01-19 11:23:51 -08001539 String key = getLayoutDescriptor(device.getIdentifier());
1540 boolean changed = false;
1541 synchronized (mDataStore) {
1542 try {
1543 if (mDataStore.switchKeyboardLayout(key, handle)) {
1544 changed = true;
1545 }
1546 } finally {
1547 mDataStore.saveIfNeeded();
Jeff Browncf39bdf2012-05-18 14:41:19 -07001548 }
1549 }
Michael Wrightd5f7ed92016-01-19 11:23:51 -08001550 if (changed) {
1551 reloadKeyboardLayouts();
1552 }
Jeff Browncf39bdf2012-05-18 14:41:19 -07001553 }
Jeff Brown6ec6f792012-04-17 16:52:41 -07001554 }
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001555 }
1556
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -08001557 public void setInputWindows(InputWindowHandle[] windowHandles,
1558 InputWindowHandle focusedWindowHandle) {
1559 final IWindow newFocusedWindow =
1560 focusedWindowHandle != null ? focusedWindowHandle.clientWindow : null;
1561 if (mFocusedWindow != newFocusedWindow) {
1562 mFocusedWindow = newFocusedWindow;
1563 if (mFocusedWindowHasCapture) {
1564 setPointerCapture(false);
1565 }
1566 }
Jeff Brown4532e612012-04-05 14:27:12 -07001567 nativeSetInputWindows(mPtr, windowHandles);
Jeff Brown349703e2010-06-22 01:27:15 -07001568 }
RoboErikfb290df2013-12-16 11:27:55 -08001569
Jeff Brown9302c872011-07-13 22:51:29 -07001570 public void setFocusedApplication(InputApplicationHandle application) {
Jeff Brown4532e612012-04-05 14:27:12 -07001571 nativeSetFocusedApplication(mPtr, application);
Jeff Brown349703e2010-06-22 01:27:15 -07001572 }
RoboErikfb290df2013-12-16 11:27:55 -08001573
Vladislav Kaznacheev3787de12016-12-21 10:36:35 -08001574 @Override
1575 public void requestPointerCapture(IBinder windowToken, boolean enabled) {
1576 if (mFocusedWindow == null || mFocusedWindow.asBinder() != windowToken) {
1577 Slog.e(TAG, "requestPointerCapture called for a window that has no focus: "
1578 + windowToken);
1579 return;
1580 }
1581 if (mFocusedWindowHasCapture == enabled) {
1582 Slog.i(TAG, "requestPointerCapture: already " + (enabled ? "enabled" : "disabled"));
1583 return;
1584 }
1585 setPointerCapture(enabled);
1586 try {
1587 mFocusedWindow.dispatchPointerCaptureChanged(enabled);
1588 } catch (RemoteException ex) {
1589 /* ignore */
1590 }
1591 }
1592
1593 private void setPointerCapture(boolean enabled) {
1594 mFocusedWindowHasCapture = enabled;
1595 nativeSetPointerCapture(mPtr, enabled);
1596 }
1597
Jeff Brown349703e2010-06-22 01:27:15 -07001598 public void setInputDispatchMode(boolean enabled, boolean frozen) {
Jeff Brown4532e612012-04-05 14:27:12 -07001599 nativeSetInputDispatchMode(mPtr, enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -07001600 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08001601
1602 public void setSystemUiVisibility(int visibility) {
Jeff Brown4532e612012-04-05 14:27:12 -07001603 nativeSetSystemUiVisibility(mPtr, visibility);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001604 }
1605
Jeff Browne6504122010-09-27 14:52:15 -07001606 /**
1607 * Atomically transfers touch focus from one window to another as identified by
1608 * their input channels. It is possible for multiple windows to have
1609 * touch focus if they support split touch dispatch
1610 * {@link android.view.WindowManager.LayoutParams#FLAG_SPLIT_TOUCH} but this
1611 * method only transfers touch focus of the specified window without affecting
1612 * other windows that may also have touch focus at the same time.
1613 * @param fromChannel The channel of a window that currently has touch focus.
1614 * @param toChannel The channel of the window that should receive touch focus in
1615 * place of the first.
1616 * @return True if the transfer was successful. False if the window with the
1617 * specified channel did not actually have touch focus at the time of the request.
1618 */
1619 public boolean transferTouchFocus(InputChannel fromChannel, InputChannel toChannel) {
1620 if (fromChannel == null) {
1621 throw new IllegalArgumentException("fromChannel must not be null.");
1622 }
1623 if (toChannel == null) {
1624 throw new IllegalArgumentException("toChannel must not be null.");
1625 }
Jeff Brown4532e612012-04-05 14:27:12 -07001626 return nativeTransferTouchFocus(mPtr, fromChannel, toChannel);
Jeff Browne6504122010-09-27 14:52:15 -07001627 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08001628
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001629 @Override // Binder call
Jeff Brownac143512012-04-05 18:57:33 -07001630 public void tryPointerSpeed(int speed) {
1631 if (!checkCallingPermission(android.Manifest.permission.SET_POINTER_SPEED,
1632 "tryPointerSpeed()")) {
1633 throw new SecurityException("Requires SET_POINTER_SPEED permission");
1634 }
1635
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001636 if (speed < InputManager.MIN_POINTER_SPEED || speed > InputManager.MAX_POINTER_SPEED) {
1637 throw new IllegalArgumentException("speed out of range");
1638 }
1639
Jeff Brownac143512012-04-05 18:57:33 -07001640 setPointerSpeedUnchecked(speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001641 }
1642
1643 public void updatePointerSpeedFromSettings() {
Jeff Brownac143512012-04-05 18:57:33 -07001644 int speed = getPointerSpeedSetting();
1645 setPointerSpeedUnchecked(speed);
1646 }
1647
1648 private void setPointerSpeedUnchecked(int speed) {
1649 speed = Math.min(Math.max(speed, InputManager.MIN_POINTER_SPEED),
1650 InputManager.MAX_POINTER_SPEED);
1651 nativeSetPointerSpeed(mPtr, speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001652 }
1653
1654 private void registerPointerSpeedSettingObserver() {
1655 mContext.getContentResolver().registerContentObserver(
1656 Settings.System.getUriFor(Settings.System.POINTER_SPEED), true,
Jeff Brown4532e612012-04-05 14:27:12 -07001657 new ContentObserver(mHandler) {
Jeff Brown1a84fd12011-06-02 01:26:32 -07001658 @Override
1659 public void onChange(boolean selfChange) {
1660 updatePointerSpeedFromSettings();
1661 }
Jeff Brownd4935962012-09-25 13:27:20 -07001662 }, UserHandle.USER_ALL);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001663 }
1664
Jeff Brownac143512012-04-05 18:57:33 -07001665 private int getPointerSpeedSetting() {
1666 int speed = InputManager.DEFAULT_POINTER_SPEED;
Jeff Brown1a84fd12011-06-02 01:26:32 -07001667 try {
Jeff Brownd4935962012-09-25 13:27:20 -07001668 speed = Settings.System.getIntForUser(mContext.getContentResolver(),
1669 Settings.System.POINTER_SPEED, UserHandle.USER_CURRENT);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001670 } catch (SettingNotFoundException snfe) {
1671 }
1672 return speed;
1673 }
1674
Jeff Browndaf4a122011-08-26 17:14:14 -07001675 public void updateShowTouchesFromSettings() {
1676 int setting = getShowTouchesSetting(0);
Jeff Brown4532e612012-04-05 14:27:12 -07001677 nativeSetShowTouches(mPtr, setting != 0);
Jeff Browndaf4a122011-08-26 17:14:14 -07001678 }
1679
1680 private void registerShowTouchesSettingObserver() {
1681 mContext.getContentResolver().registerContentObserver(
1682 Settings.System.getUriFor(Settings.System.SHOW_TOUCHES), true,
Jeff Brown4532e612012-04-05 14:27:12 -07001683 new ContentObserver(mHandler) {
Jeff Browndaf4a122011-08-26 17:14:14 -07001684 @Override
1685 public void onChange(boolean selfChange) {
1686 updateShowTouchesFromSettings();
1687 }
Jeff Brownd4935962012-09-25 13:27:20 -07001688 }, UserHandle.USER_ALL);
Jeff Browndaf4a122011-08-26 17:14:14 -07001689 }
1690
Jun Mukaie4e75da2015-12-15 16:19:20 -08001691 public void updateAccessibilityLargePointerFromSettings() {
1692 final int accessibilityConfig = Settings.Secure.getIntForUser(
1693 mContext.getContentResolver(), Settings.Secure.ACCESSIBILITY_LARGE_POINTER_ICON,
1694 0, UserHandle.USER_CURRENT);
Jun Mukai1f3dbff2015-12-16 14:41:25 -08001695 PointerIcon.setUseLargeIcons(accessibilityConfig == 1);
Jun Mukaie4e75da2015-12-15 16:19:20 -08001696 nativeReloadPointerIcons(mPtr);
1697 }
1698
Jun Mukai19a56012015-11-24 11:25:52 -08001699 private void registerAccessibilityLargePointerSettingObserver() {
1700 mContext.getContentResolver().registerContentObserver(
1701 Settings.Secure.getUriFor(Settings.Secure.ACCESSIBILITY_LARGE_POINTER_ICON), true,
1702 new ContentObserver(mHandler) {
1703 @Override
1704 public void onChange(boolean selfChange) {
Jun Mukaie4e75da2015-12-15 16:19:20 -08001705 updateAccessibilityLargePointerFromSettings();
Jun Mukai19a56012015-11-24 11:25:52 -08001706 }
1707 }, UserHandle.USER_ALL);
1708 }
1709
Jeff Browndaf4a122011-08-26 17:14:14 -07001710 private int getShowTouchesSetting(int defaultValue) {
1711 int result = defaultValue;
1712 try {
Jeff Brownd4935962012-09-25 13:27:20 -07001713 result = Settings.System.getIntForUser(mContext.getContentResolver(),
1714 Settings.System.SHOW_TOUCHES, UserHandle.USER_CURRENT);
Jeff Browndaf4a122011-08-26 17:14:14 -07001715 } catch (SettingNotFoundException snfe) {
1716 }
1717 return result;
1718 }
1719
Jeff Browna47425a2012-04-13 04:09:27 -07001720 // Binder call
1721 @Override
1722 public void vibrate(int deviceId, long[] pattern, int repeat, IBinder token) {
1723 if (repeat >= pattern.length) {
1724 throw new ArrayIndexOutOfBoundsException();
1725 }
1726
1727 VibratorToken v;
1728 synchronized (mVibratorLock) {
1729 v = mVibratorTokens.get(token);
1730 if (v == null) {
1731 v = new VibratorToken(deviceId, token, mNextVibratorTokenValue++);
1732 try {
1733 token.linkToDeath(v, 0);
1734 } catch (RemoteException ex) {
1735 // give up
1736 throw new RuntimeException(ex);
1737 }
1738 mVibratorTokens.put(token, v);
1739 }
1740 }
1741
1742 synchronized (v) {
1743 v.mVibrating = true;
1744 nativeVibrate(mPtr, deviceId, pattern, repeat, v.mTokenValue);
1745 }
1746 }
1747
1748 // Binder call
1749 @Override
1750 public void cancelVibrate(int deviceId, IBinder token) {
1751 VibratorToken v;
1752 synchronized (mVibratorLock) {
1753 v = mVibratorTokens.get(token);
1754 if (v == null || v.mDeviceId != deviceId) {
1755 return; // nothing to cancel
1756 }
1757 }
1758
1759 cancelVibrateIfNeeded(v);
1760 }
1761
1762 void onVibratorTokenDied(VibratorToken v) {
1763 synchronized (mVibratorLock) {
1764 mVibratorTokens.remove(v.mToken);
1765 }
1766
1767 cancelVibrateIfNeeded(v);
1768 }
1769
1770 private void cancelVibrateIfNeeded(VibratorToken v) {
1771 synchronized (v) {
1772 if (v.mVibrating) {
1773 nativeCancelVibrate(mPtr, v.mDeviceId, v.mTokenValue);
1774 v.mVibrating = false;
1775 }
1776 }
1777 }
1778
Jun Mukai19a56012015-11-24 11:25:52 -08001779 // Binder call
1780 @Override
Michael Wrightf9d9ce772016-05-13 17:44:16 +01001781 public void setPointerIconType(int iconId) {
1782 nativeSetPointerIconType(mPtr, iconId);
Jun Mukai19a56012015-11-24 11:25:52 -08001783 }
Jun Mukai1db53972015-09-11 18:08:31 -07001784
Jun Mukaid4eaef72015-10-30 15:54:33 -07001785 // Binder call
1786 @Override
1787 public void setCustomPointerIcon(PointerIcon icon) {
Michael Wrightb004b512017-01-18 18:09:29 +00001788 Preconditions.checkNotNull(icon);
Jun Mukaid4eaef72015-10-30 15:54:33 -07001789 nativeSetCustomPointerIcon(mPtr, icon);
1790 }
1791
Jeff Brown4532e612012-04-05 14:27:12 -07001792 @Override
Michael Wrightd5f7ed92016-01-19 11:23:51 -08001793 public void dump(FileDescriptor fd, final PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -06001794 if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;
Jeff Brown4532e612012-04-05 14:27:12 -07001795
1796 pw.println("INPUT MANAGER (dumpsys input)\n");
1797 String dumpStr = nativeDump(mPtr);
Jeff Browne33348b2010-07-15 23:54:05 -07001798 if (dumpStr != null) {
1799 pw.println(dumpStr);
1800 }
Michael Wrightd5f7ed92016-01-19 11:23:51 -08001801 pw.println(" Keyboard Layouts:");
1802 visitAllKeyboardLayouts(new KeyboardLayoutVisitor() {
1803 @Override
1804 public void visitKeyboardLayout(Resources resources,
1805 int keyboardLayoutResId, KeyboardLayout layout) {
1806 pw.println(" \"" + layout + "\": " + layout.getDescriptor());
1807 }
1808 });
1809 pw.println();
1810 synchronized(mDataStore) {
1811 mDataStore.dump(pw, " ");
1812 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001813 }
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001814
Michael Wrightd5f7ed92016-01-19 11:23:51 -08001815 @Override
1816 public void onShellCommand(FileDescriptor in, FileDescriptor out,
Dianne Hackborn354736e2016-08-22 17:00:05 -07001817 FileDescriptor err, String[] args, ShellCallback callback,
1818 ResultReceiver resultReceiver) {
1819 (new Shell()).exec(this, in, out, err, args, callback, resultReceiver);
Michael Wrightd5f7ed92016-01-19 11:23:51 -08001820 }
1821
1822 public int onShellCommand(Shell shell, String cmd) {
1823 if (TextUtils.isEmpty(cmd)) {
1824 shell.onHelp();
1825 return 1;
1826 }
1827 if (cmd.equals("setlayout")) {
1828 if (!checkCallingPermission(android.Manifest.permission.SET_KEYBOARD_LAYOUT,
1829 "onShellCommand()")) {
1830 throw new SecurityException("Requires SET_KEYBOARD_LAYOUT permission");
1831 }
1832 InputMethodSubtypeHandle handle = new InputMethodSubtypeHandle(
1833 shell.getNextArgRequired(), Integer.parseInt(shell.getNextArgRequired()));
1834 String descriptor = shell.getNextArgRequired();
1835 int vid = Integer.decode(shell.getNextArgRequired());
1836 int pid = Integer.decode(shell.getNextArgRequired());
1837 InputDeviceIdentifier id = new InputDeviceIdentifier(descriptor, vid, pid);
1838 setKeyboardLayoutForInputDeviceInner(id, handle, shell.getNextArgRequired());
1839 }
1840 return 0;
1841 }
1842
1843
Jeff Brownac143512012-04-05 18:57:33 -07001844 private boolean checkCallingPermission(String permission, String func) {
1845 // Quick check: if the calling permission is me, it's all okay.
1846 if (Binder.getCallingPid() == Process.myPid()) {
1847 return true;
1848 }
1849
1850 if (mContext.checkCallingPermission(permission) == PackageManager.PERMISSION_GRANTED) {
1851 return true;
1852 }
1853 String msg = "Permission Denial: " + func + " from pid="
1854 + Binder.getCallingPid()
1855 + ", uid=" + Binder.getCallingUid()
1856 + " requires " + permission;
1857 Slog.w(TAG, msg);
1858 return false;
1859 }
1860
Jeff Brown4532e612012-04-05 14:27:12 -07001861 // Called by the heartbeat to ensure locks are not held indefinitely (for deadlock detection).
Craig Mautner2f39e9f2012-09-21 11:39:54 -07001862 @Override
Jeff Brown89ef0722011-08-10 16:25:21 -07001863 public void monitor() {
1864 synchronized (mInputFilterLock) { }
Jeff Brown4532e612012-04-05 14:27:12 -07001865 nativeMonitor(mPtr);
Jeff Brown89ef0722011-08-10 16:25:21 -07001866 }
1867
Andrii Kulianed76e742017-06-26 14:57:02 -07001868 // Binder call
1869 @Override
1870 public IInputForwarder createInputForwarder(int displayId) throws RemoteException {
1871 if (!checkCallingPermission(android.Manifest.permission.INJECT_EVENTS,
1872 "createInputForwarder()")) {
1873 throw new SecurityException("Requires INJECT_EVENTS permission");
1874 }
1875 final DisplayManager displayManager = mContext.getSystemService(DisplayManager.class);
1876 final Display display = displayManager.getDisplay(displayId);
1877 if (display == null) {
1878 throw new IllegalArgumentException(
1879 "Can't create input forwarder for non-existent displayId: " + displayId);
1880 }
1881 final int callingUid = Binder.getCallingUid();
1882 final int displayOwnerUid = display.getOwnerUid();
1883 if (callingUid != displayOwnerUid) {
1884 throw new SecurityException(
1885 "Only owner of the display can forward input events to it.");
1886 }
1887
1888 return new InputForwarder(displayId);
1889 }
1890
Jeff Brown4532e612012-04-05 14:27:12 -07001891 // Native callback.
1892 private void notifyConfigurationChanged(long whenNanos) {
Jeff Browna9d131c2012-09-20 16:48:17 -07001893 mWindowManagerCallbacks.notifyConfigurationChanged();
Jeff Brown4532e612012-04-05 14:27:12 -07001894 }
1895
1896 // Native callback.
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001897 private void notifyInputDevicesChanged(InputDevice[] inputDevices) {
1898 synchronized (mInputDevicesLock) {
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001899 if (!mInputDevicesChangedPending) {
1900 mInputDevicesChangedPending = true;
Jeff Browncf39bdf2012-05-18 14:41:19 -07001901 mHandler.obtainMessage(MSG_DELIVER_INPUT_DEVICES_CHANGED,
1902 mInputDevices).sendToTarget();
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001903 }
Jeff Browncf39bdf2012-05-18 14:41:19 -07001904
1905 mInputDevices = inputDevices;
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001906 }
1907 }
1908
1909 // Native callback.
Jeff Brownbcc046a2012-09-27 20:46:43 -07001910 private void notifySwitch(long whenNanos, int switchValues, int switchMask) {
1911 if (DEBUG) {
1912 Slog.d(TAG, "notifySwitch: values=" + Integer.toHexString(switchValues)
1913 + ", mask=" + Integer.toHexString(switchMask));
1914 }
1915
Craig Mautner2f39e9f2012-09-21 11:39:54 -07001916 if ((switchMask & SW_LID_BIT) != 0) {
1917 final boolean lidOpen = ((switchValues & SW_LID_BIT) == 0);
Jeff Brownbcc046a2012-09-27 20:46:43 -07001918 mWindowManagerCallbacks.notifyLidSwitchChanged(whenNanos, lidOpen);
Jeff Brown53384282012-08-20 20:16:01 -07001919 }
Craig Mautner2f39e9f2012-09-21 11:39:54 -07001920
Michael Wright3818c922014-09-02 13:59:07 -07001921 if ((switchMask & SW_CAMERA_LENS_COVER_BIT) != 0) {
Michael Wright9e10d252014-09-13 19:41:20 -07001922 final boolean lensCovered = ((switchValues & SW_CAMERA_LENS_COVER_BIT) != 0);
Michael Wright3818c922014-09-02 13:59:07 -07001923 mWindowManagerCallbacks.notifyCameraLensCoverSwitchChanged(whenNanos, lensCovered);
1924 }
1925
Craig Mautner2f39e9f2012-09-21 11:39:54 -07001926 if (mUseDevInputEventForAudioJack && (switchMask & SW_JACK_BITS) != 0) {
1927 mWiredAccessoryCallbacks.notifyWiredAccessoryChanged(whenNanos, switchValues,
1928 switchMask);
1929 }
Michael Wright39e5e942015-08-19 22:52:47 +01001930
Michael Wright9209c9c2015-09-03 17:57:01 +01001931 if ((switchMask & SW_TABLET_MODE_BIT) != 0) {
Michael Wright39e5e942015-08-19 22:52:47 +01001932 SomeArgs args = SomeArgs.obtain();
1933 args.argi1 = (int) (whenNanos & 0xFFFFFFFF);
1934 args.argi2 = (int) (whenNanos >> 32);
1935 args.arg1 = Boolean.valueOf((switchValues & SW_TABLET_MODE_BIT) != 0);
1936 mHandler.obtainMessage(MSG_DELIVER_TABLET_MODE_CHANGED,
1937 args).sendToTarget();
1938 }
Jeff Brown4532e612012-04-05 14:27:12 -07001939 }
1940
1941 // Native callback.
1942 private void notifyInputChannelBroken(InputWindowHandle inputWindowHandle) {
Jeff Browna9d131c2012-09-20 16:48:17 -07001943 mWindowManagerCallbacks.notifyInputChannelBroken(inputWindowHandle);
Jeff Brown4532e612012-04-05 14:27:12 -07001944 }
1945
1946 // Native callback.
1947 private long notifyANR(InputApplicationHandle inputApplicationHandle,
Jeff Brownbd181bb2013-09-10 16:44:24 -07001948 InputWindowHandle inputWindowHandle, String reason) {
1949 return mWindowManagerCallbacks.notifyANR(
1950 inputApplicationHandle, inputWindowHandle, reason);
Jeff Brown4532e612012-04-05 14:27:12 -07001951 }
1952
1953 // Native callback.
1954 final boolean filterInputEvent(InputEvent event, int policyFlags) {
1955 synchronized (mInputFilterLock) {
1956 if (mInputFilter != null) {
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07001957 try {
1958 mInputFilter.filterInputEvent(event, policyFlags);
1959 } catch (RemoteException e) {
1960 /* ignore */
1961 }
Jeff Brown4532e612012-04-05 14:27:12 -07001962 return false;
1963 }
1964 }
1965 event.recycle();
1966 return true;
1967 }
1968
1969 // Native callback.
Jeff Brown037c33e2014-04-09 00:31:55 -07001970 private int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags) {
1971 return mWindowManagerCallbacks.interceptKeyBeforeQueueing(event, policyFlags);
Jeff Brown4532e612012-04-05 14:27:12 -07001972 }
1973
1974 // Native callback.
Michael Wright70af00a2014-09-03 19:30:20 -07001975 private int interceptMotionBeforeQueueingNonInteractive(long whenNanos, int policyFlags) {
1976 return mWindowManagerCallbacks.interceptMotionBeforeQueueingNonInteractive(
Jeff Brown26875502014-01-30 21:47:47 -08001977 whenNanos, policyFlags);
Jeff Brown4532e612012-04-05 14:27:12 -07001978 }
1979
1980 // Native callback.
1981 private long interceptKeyBeforeDispatching(InputWindowHandle focus,
1982 KeyEvent event, int policyFlags) {
Jeff Browna9d131c2012-09-20 16:48:17 -07001983 return mWindowManagerCallbacks.interceptKeyBeforeDispatching(focus, event, policyFlags);
Jeff Brown4532e612012-04-05 14:27:12 -07001984 }
1985
1986 // Native callback.
1987 private KeyEvent dispatchUnhandledKey(InputWindowHandle focus,
1988 KeyEvent event, int policyFlags) {
Jeff Browna9d131c2012-09-20 16:48:17 -07001989 return mWindowManagerCallbacks.dispatchUnhandledKey(focus, event, policyFlags);
Jeff Brown4532e612012-04-05 14:27:12 -07001990 }
1991
1992 // Native callback.
1993 private boolean checkInjectEventsPermission(int injectorPid, int injectorUid) {
1994 return mContext.checkPermission(android.Manifest.permission.INJECT_EVENTS,
1995 injectorPid, injectorUid) == PackageManager.PERMISSION_GRANTED;
1996 }
1997
1998 // Native callback.
1999 private int getVirtualKeyQuietTimeMillis() {
2000 return mContext.getResources().getInteger(
2001 com.android.internal.R.integer.config_virtualKeyQuietTimeMillis);
2002 }
2003
2004 // Native callback.
2005 private String[] getExcludedDeviceNames() {
2006 ArrayList<String> names = new ArrayList<String>();
2007
2008 // Read partner-provided list of excluded input devices
2009 XmlPullParser parser = null;
2010 // Environment.getRootDirectory() is a fancy way of saying ANDROID_ROOT or "/system".
2011 File confFile = new File(Environment.getRootDirectory(), EXCLUDED_DEVICES_PATH);
2012 FileReader confreader = null;
2013 try {
2014 confreader = new FileReader(confFile);
2015 parser = Xml.newPullParser();
2016 parser.setInput(confreader);
2017 XmlUtils.beginDocument(parser, "devices");
2018
2019 while (true) {
2020 XmlUtils.nextElement(parser);
2021 if (!"device".equals(parser.getName())) {
2022 break;
2023 }
2024 String name = parser.getAttributeValue(null, "name");
2025 if (name != null) {
2026 names.add(name);
2027 }
2028 }
2029 } catch (FileNotFoundException e) {
2030 // It's ok if the file does not exist.
2031 } catch (Exception e) {
2032 Slog.e(TAG, "Exception while parsing '" + confFile.getAbsolutePath() + "'", e);
2033 } finally {
2034 try { if (confreader != null) confreader.close(); } catch (IOException e) { }
2035 }
2036
2037 return names.toArray(new String[names.size()]);
2038 }
2039
2040 // Native callback.
2041 private int getKeyRepeatTimeout() {
2042 return ViewConfiguration.getKeyRepeatTimeout();
2043 }
2044
2045 // Native callback.
2046 private int getKeyRepeatDelay() {
2047 return ViewConfiguration.getKeyRepeatDelay();
2048 }
2049
2050 // Native callback.
2051 private int getHoverTapTimeout() {
2052 return ViewConfiguration.getHoverTapTimeout();
2053 }
2054
2055 // Native callback.
2056 private int getHoverTapSlop() {
2057 return ViewConfiguration.getHoverTapSlop();
2058 }
2059
2060 // Native callback.
2061 private int getDoubleTapTimeout() {
2062 return ViewConfiguration.getDoubleTapTimeout();
2063 }
2064
2065 // Native callback.
2066 private int getLongPressTimeout() {
2067 return ViewConfiguration.getLongPressTimeout();
2068 }
2069
2070 // Native callback.
2071 private int getPointerLayer() {
Jeff Browna9d131c2012-09-20 16:48:17 -07002072 return mWindowManagerCallbacks.getPointerLayer();
Jeff Brown4532e612012-04-05 14:27:12 -07002073 }
2074
2075 // Native callback.
2076 private PointerIcon getPointerIcon() {
2077 return PointerIcon.getDefaultIcon(mContext);
2078 }
2079
Jeff Brown6ec6f792012-04-17 16:52:41 -07002080 // Native callback.
RoboErikfb290df2013-12-16 11:27:55 -08002081 private String[] getKeyboardLayoutOverlay(InputDeviceIdentifier identifier) {
Jeff Brown6ec6f792012-04-17 16:52:41 -07002082 if (!mSystemReady) {
2083 return null;
2084 }
2085
RoboErikfb290df2013-12-16 11:27:55 -08002086 String keyboardLayoutDescriptor = getCurrentKeyboardLayoutForInputDevice(identifier);
Jeff Brown6ec6f792012-04-17 16:52:41 -07002087 if (keyboardLayoutDescriptor == null) {
2088 return null;
2089 }
2090
2091 final String[] result = new String[2];
2092 visitKeyboardLayout(keyboardLayoutDescriptor, new KeyboardLayoutVisitor() {
2093 @Override
Michael Wright07483422015-10-30 16:20:13 +00002094 public void visitKeyboardLayout(Resources resources,
2095 int keyboardLayoutResId, KeyboardLayout layout) {
Jeff Brown6ec6f792012-04-17 16:52:41 -07002096 try {
Michael Wright07483422015-10-30 16:20:13 +00002097 result[0] = layout.getDescriptor();
Jeff Brown6ec6f792012-04-17 16:52:41 -07002098 result[1] = Streams.readFully(new InputStreamReader(
Jeff Brown2f095762012-05-10 21:29:33 -07002099 resources.openRawResource(keyboardLayoutResId)));
Jeff Brown6ec6f792012-04-17 16:52:41 -07002100 } catch (IOException ex) {
2101 } catch (NotFoundException ex) {
2102 }
2103 }
2104 });
2105 if (result[0] == null) {
Michael Wright07483422015-10-30 16:20:13 +00002106 Slog.w(TAG, "Could not get keyboard layout with descriptor '"
Jeff Brown6ec6f792012-04-17 16:52:41 -07002107 + keyboardLayoutDescriptor + "'.");
2108 return null;
2109 }
2110 return result;
2111 }
2112
Jeff Brown5bbd4b42012-04-20 19:28:00 -07002113 // Native callback.
2114 private String getDeviceAlias(String uniqueId) {
Matthew Xie96313142012-06-29 16:57:31 -07002115 if (BluetoothAdapter.checkBluetoothAddress(uniqueId)) {
2116 // TODO(BT) mBluetoothService.getRemoteAlias(uniqueId)
2117 return null;
Jeff Brown5bbd4b42012-04-20 19:28:00 -07002118 }
2119 return null;
2120 }
2121
Jeff Brown4532e612012-04-05 14:27:12 -07002122 /**
2123 * Callback interface implemented by the Window Manager.
2124 */
Jeff Browna9d131c2012-09-20 16:48:17 -07002125 public interface WindowManagerCallbacks {
Jeff Brown4532e612012-04-05 14:27:12 -07002126 public void notifyConfigurationChanged();
2127
2128 public void notifyLidSwitchChanged(long whenNanos, boolean lidOpen);
2129
Michael Wright3818c922014-09-02 13:59:07 -07002130 public void notifyCameraLensCoverSwitchChanged(long whenNanos, boolean lensCovered);
2131
Jeff Brown4532e612012-04-05 14:27:12 -07002132 public void notifyInputChannelBroken(InputWindowHandle inputWindowHandle);
2133
2134 public long notifyANR(InputApplicationHandle inputApplicationHandle,
Jeff Brownbd181bb2013-09-10 16:44:24 -07002135 InputWindowHandle inputWindowHandle, String reason);
Jeff Brown4532e612012-04-05 14:27:12 -07002136
Jeff Brown037c33e2014-04-09 00:31:55 -07002137 public int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags);
Jeff Brown4532e612012-04-05 14:27:12 -07002138
Michael Wright70af00a2014-09-03 19:30:20 -07002139 public int interceptMotionBeforeQueueingNonInteractive(long whenNanos, int policyFlags);
Jeff Brown4532e612012-04-05 14:27:12 -07002140
2141 public long interceptKeyBeforeDispatching(InputWindowHandle focus,
2142 KeyEvent event, int policyFlags);
2143
2144 public KeyEvent dispatchUnhandledKey(InputWindowHandle focus,
2145 KeyEvent event, int policyFlags);
2146
2147 public int getPointerLayer();
2148 }
2149
2150 /**
Craig Mautner2f39e9f2012-09-21 11:39:54 -07002151 * Callback interface implemented by WiredAccessoryObserver.
2152 */
2153 public interface WiredAccessoryCallbacks {
2154 public void notifyWiredAccessoryChanged(long whenNanos, int switchValues, int switchMask);
Eric Laurent4a5eeb92014-05-06 10:49:04 -07002155 public void systemReady();
Craig Mautner2f39e9f2012-09-21 11:39:54 -07002156 }
2157
2158 /**
Jeff Brownaf9e8d32012-04-12 17:32:48 -07002159 * Private handler for the input manager.
2160 */
2161 private final class InputManagerHandler extends Handler {
Jeff Browna9d131c2012-09-20 16:48:17 -07002162 public InputManagerHandler(Looper looper) {
2163 super(looper, null, true /*async*/);
Jeff Browna2910d02012-08-25 12:29:46 -07002164 }
2165
Jeff Brownaf9e8d32012-04-12 17:32:48 -07002166 @Override
2167 public void handleMessage(Message msg) {
2168 switch (msg.what) {
2169 case MSG_DELIVER_INPUT_DEVICES_CHANGED:
Jeff Browncf39bdf2012-05-18 14:41:19 -07002170 deliverInputDevicesChanged((InputDevice[])msg.obj);
2171 break;
Michael Wrightd5f7ed92016-01-19 11:23:51 -08002172 case MSG_SWITCH_KEYBOARD_LAYOUT: {
2173 SomeArgs args = (SomeArgs)msg.obj;
2174 handleSwitchKeyboardLayout((InputDeviceIdentifier)args.arg1,
2175 (InputMethodSubtypeHandle)args.arg2);
Jeff Browncf39bdf2012-05-18 14:41:19 -07002176 break;
Michael Wrightd5f7ed92016-01-19 11:23:51 -08002177 }
Jeff Browncf39bdf2012-05-18 14:41:19 -07002178 case MSG_RELOAD_KEYBOARD_LAYOUTS:
2179 reloadKeyboardLayouts();
2180 break;
2181 case MSG_UPDATE_KEYBOARD_LAYOUTS:
2182 updateKeyboardLayouts();
2183 break;
2184 case MSG_RELOAD_DEVICE_ALIASES:
2185 reloadDeviceAliases();
Jeff Brownaf9e8d32012-04-12 17:32:48 -07002186 break;
Yohei Yukawab097b822015-12-01 10:43:08 -08002187 case MSG_DELIVER_TABLET_MODE_CHANGED: {
Michael Wright39e5e942015-08-19 22:52:47 +01002188 SomeArgs args = (SomeArgs) msg.obj;
2189 long whenNanos = (args.argi1 & 0xFFFFFFFFl) | ((long) args.argi2 << 32);
2190 boolean inTabletMode = (boolean) args.arg1;
2191 deliverTabletModeChanged(whenNanos, inTabletMode);
2192 break;
Yohei Yukawab097b822015-12-01 10:43:08 -08002193 }
2194 case MSG_INPUT_METHOD_SUBTYPE_CHANGED: {
2195 final int userId = msg.arg1;
2196 final SomeArgs args = (SomeArgs) msg.obj;
2197 final InputMethodInfo inputMethodInfo = (InputMethodInfo) args.arg1;
2198 final InputMethodSubtype subtype = (InputMethodSubtype) args.arg2;
2199 args.recycle();
2200 handleSwitchInputMethodSubtype(userId, inputMethodInfo, subtype);
2201 break;
2202 }
Jeff Brownaf9e8d32012-04-12 17:32:48 -07002203 }
2204 }
2205 }
2206
2207 /**
Jeff Brown4532e612012-04-05 14:27:12 -07002208 * Hosting interface for input filters to call back into the input manager.
2209 */
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07002210 private final class InputFilterHost extends IInputFilterHost.Stub {
Jeff Brown0029c662011-03-30 02:25:18 -07002211 private boolean mDisconnected;
2212
2213 public void disconnectLocked() {
2214 mDisconnected = true;
2215 }
2216
Craig Mautner2f39e9f2012-09-21 11:39:54 -07002217 @Override
Jeff Brown0029c662011-03-30 02:25:18 -07002218 public void sendInputEvent(InputEvent event, int policyFlags) {
2219 if (event == null) {
2220 throw new IllegalArgumentException("event must not be null");
2221 }
2222
2223 synchronized (mInputFilterLock) {
2224 if (!mDisconnected) {
Jeff Brownca9bc702014-02-11 14:32:56 -08002225 nativeInjectInputEvent(mPtr, event, Display.DEFAULT_DISPLAY, 0, 0,
Jeff Brownac143512012-04-05 18:57:33 -07002226 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC, 0,
Jeff Brown0029c662011-03-30 02:25:18 -07002227 policyFlags | WindowManagerPolicy.FLAG_FILTERED);
2228 }
2229 }
2230 }
2231 }
Jeff Brown9f25b7f2012-04-10 14:30:49 -07002232
2233 private static final class KeyboardLayoutDescriptor {
2234 public String packageName;
2235 public String receiverName;
2236 public String keyboardLayoutName;
2237
2238 public static String format(String packageName,
2239 String receiverName, String keyboardName) {
2240 return packageName + "/" + receiverName + "/" + keyboardName;
2241 }
2242
2243 public static KeyboardLayoutDescriptor parse(String descriptor) {
2244 int pos = descriptor.indexOf('/');
2245 if (pos < 0 || pos + 1 == descriptor.length()) {
2246 return null;
2247 }
2248 int pos2 = descriptor.indexOf('/', pos + 1);
2249 if (pos2 < pos + 2 || pos2 + 1 == descriptor.length()) {
2250 return null;
2251 }
2252
2253 KeyboardLayoutDescriptor result = new KeyboardLayoutDescriptor();
2254 result.packageName = descriptor.substring(0, pos);
2255 result.receiverName = descriptor.substring(pos + 1, pos2);
2256 result.keyboardLayoutName = descriptor.substring(pos2 + 1);
2257 return result;
2258 }
2259 }
Jeff Brownaf9e8d32012-04-12 17:32:48 -07002260
Jeff Brown6ec6f792012-04-17 16:52:41 -07002261 private interface KeyboardLayoutVisitor {
Michael Wright07483422015-10-30 16:20:13 +00002262 void visitKeyboardLayout(Resources resources,
2263 int keyboardLayoutResId, KeyboardLayout layout);
Jeff Brown6ec6f792012-04-17 16:52:41 -07002264 }
2265
Jeff Brownaf9e8d32012-04-12 17:32:48 -07002266 private final class InputDevicesChangedListenerRecord implements DeathRecipient {
2267 private final int mPid;
2268 private final IInputDevicesChangedListener mListener;
2269
2270 public InputDevicesChangedListenerRecord(int pid, IInputDevicesChangedListener listener) {
2271 mPid = pid;
2272 mListener = listener;
2273 }
2274
2275 @Override
2276 public void binderDied() {
2277 if (DEBUG) {
2278 Slog.d(TAG, "Input devices changed listener for pid " + mPid + " died.");
2279 }
2280 onInputDevicesChangedListenerDied(mPid);
2281 }
2282
2283 public void notifyInputDevicesChanged(int[] info) {
2284 try {
2285 mListener.onInputDevicesChanged(info);
2286 } catch (RemoteException ex) {
2287 Slog.w(TAG, "Failed to notify process "
2288 + mPid + " that input devices changed, assuming it died.", ex);
2289 binderDied();
2290 }
2291 }
2292 }
Jeff Browna47425a2012-04-13 04:09:27 -07002293
Michael Wright39e5e942015-08-19 22:52:47 +01002294 private final class TabletModeChangedListenerRecord implements DeathRecipient {
2295 private final int mPid;
2296 private final ITabletModeChangedListener mListener;
2297
2298 public TabletModeChangedListenerRecord(int pid, ITabletModeChangedListener listener) {
2299 mPid = pid;
2300 mListener = listener;
2301 }
2302
2303 @Override
2304 public void binderDied() {
2305 if (DEBUG) {
2306 Slog.d(TAG, "Tablet mode changed listener for pid " + mPid + " died.");
2307 }
2308 onTabletModeChangedListenerDied(mPid);
2309 }
2310
2311 public void notifyTabletModeChanged(long whenNanos, boolean inTabletMode) {
2312 try {
2313 mListener.onTabletModeChanged(whenNanos, inTabletMode);
2314 } catch (RemoteException ex) {
2315 Slog.w(TAG, "Failed to notify process " + mPid +
2316 " that tablet mode changed, assuming it died.", ex);
2317 binderDied();
2318 }
2319 }
2320 }
2321
Jeff Browna47425a2012-04-13 04:09:27 -07002322 private final class VibratorToken implements DeathRecipient {
2323 public final int mDeviceId;
2324 public final IBinder mToken;
2325 public final int mTokenValue;
2326
2327 public boolean mVibrating;
2328
2329 public VibratorToken(int deviceId, IBinder token, int tokenValue) {
2330 mDeviceId = deviceId;
2331 mToken = token;
2332 mTokenValue = tokenValue;
2333 }
2334
2335 @Override
2336 public void binderDied() {
2337 if (DEBUG) {
2338 Slog.d(TAG, "Vibrator token died.");
2339 }
2340 onVibratorTokenDied(this);
2341 }
2342 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08002343
Michael Wrightd5f7ed92016-01-19 11:23:51 -08002344 private class Shell extends ShellCommand {
2345 @Override
2346 public int onCommand(String cmd) {
2347 return onShellCommand(this, cmd);
2348 }
2349
2350 @Override
2351 public void onHelp() {
2352 final PrintWriter pw = getOutPrintWriter();
2353 pw.println("Input manager commands:");
2354 pw.println(" help");
2355 pw.println(" Print this help text.");
2356 pw.println("");
2357 pw.println(" setlayout IME_ID IME_SUPTYPE_HASH_CODE"
2358 + " DEVICE_DESCRIPTOR VENDOR_ID PRODUCT_ID KEYBOARD_DESCRIPTOR");
2359 pw.println(" Sets a keyboard layout for a given IME subtype and input device pair");
2360 }
2361 }
2362
Jeff Brown4ccb8232014-01-16 22:16:42 -08002363 private final class LocalService extends InputManagerInternal {
2364 @Override
Santos Cordonee8931e2017-04-05 10:31:15 -07002365 public void setDisplayViewports(DisplayViewport defaultViewport,
2366 DisplayViewport externalTouchViewport,
2367 List<DisplayViewport> virtualTouchViewports) {
2368 setDisplayViewportsInternal(defaultViewport, externalTouchViewport,
2369 virtualTouchViewports);
Jeff Brown4ccb8232014-01-16 22:16:42 -08002370 }
Jeff Brownca9bc702014-02-11 14:32:56 -08002371
2372 @Override
2373 public boolean injectInputEvent(InputEvent event, int displayId, int mode) {
2374 return injectInputEventInternal(event, displayId, mode);
2375 }
Jeff Brown037c33e2014-04-09 00:31:55 -07002376
2377 @Override
2378 public void setInteractive(boolean interactive) {
2379 nativeSetInteractive(mPtr, interactive);
2380 }
Yohei Yukawab097b822015-12-01 10:43:08 -08002381
2382 @Override
2383 public void onInputMethodSubtypeChanged(int userId,
2384 @Nullable InputMethodInfo inputMethodInfo, @Nullable InputMethodSubtype subtype) {
2385 final SomeArgs someArgs = SomeArgs.obtain();
2386 someArgs.arg1 = inputMethodInfo;
2387 someArgs.arg2 = subtype;
2388 mHandler.obtainMessage(MSG_INPUT_METHOD_SUBTYPE_CHANGED, userId, 0, someArgs)
2389 .sendToTarget();
2390 }
Andrii Kulian112d0562016-03-08 10:44:22 -08002391
2392 @Override
2393 public void toggleCapsLock(int deviceId) {
2394 nativeToggleCapsLock(mPtr, deviceId);
2395 }
Adrian Roos99182342016-06-15 15:30:46 -07002396
2397 @Override
2398 public void setPulseGestureEnabled(boolean enabled) {
2399 if (mDoubleTouchGestureEnableFile != null) {
2400 FileWriter writer = null;
2401 try {
2402 writer = new FileWriter(mDoubleTouchGestureEnableFile);
2403 writer.write(enabled ? "1" : "0");
2404 } catch (IOException e) {
2405 Log.wtf(TAG, "Unable to setPulseGestureEnabled", e);
2406 } finally {
2407 IoUtils.closeQuietly(writer);
2408 }
2409 }
2410 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08002411 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002412}