blob: 5a136729291ac03ac2cbcac3f0664ddf19aa341a [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
Jeff Brownca9bc702014-02-11 14:32:56 -080019import android.view.Display;
Michael Wright39e5e942015-08-19 22:52:47 +010020import com.android.internal.os.SomeArgs;
Jeff Browncf39bdf2012-05-18 14:41:19 -070021import com.android.internal.R;
Jeff Brown46b9ac02010-04-22 18:58:52 -070022import com.android.internal.util.XmlUtils;
Jeff Brown4ccb8232014-01-16 22:16:42 -080023import com.android.server.DisplayThread;
24import com.android.server.LocalServices;
Jeff Brown89ef0722011-08-10 16:25:21 -070025import com.android.server.Watchdog;
Jeff Brown46b9ac02010-04-22 18:58:52 -070026
27import org.xmlpull.v1.XmlPullParser;
28
Jeff Browna3bc5652012-04-17 11:42:25 -070029import android.Manifest;
Jeff Browncf39bdf2012-05-18 14:41:19 -070030import android.app.Notification;
31import android.app.NotificationManager;
32import android.app.PendingIntent;
Jeff Brown5bbd4b42012-04-20 19:28:00 -070033import android.bluetooth.BluetoothAdapter;
34import android.bluetooth.BluetoothDevice;
Jeff Brown6ec6f792012-04-17 16:52:41 -070035import android.content.BroadcastReceiver;
Jeff Brown9f25b7f2012-04-10 14:30:49 -070036import android.content.ComponentName;
Jeff Brown46b9ac02010-04-22 18:58:52 -070037import android.content.Context;
Jeff Brown9f25b7f2012-04-10 14:30:49 -070038import android.content.Intent;
Jeff Brown6ec6f792012-04-17 16:52:41 -070039import android.content.IntentFilter;
Jeff Brown9f25b7f2012-04-10 14:30:49 -070040import android.content.pm.ActivityInfo;
Michael Wright8ebac232014-09-18 18:29:49 -070041import android.content.pm.ApplicationInfo;
Jeff Brown349703e2010-06-22 01:27:15 -070042import android.content.pm.PackageManager;
Jeff Brown9f25b7f2012-04-10 14:30:49 -070043import android.content.pm.ResolveInfo;
44import android.content.pm.PackageManager.NameNotFoundException;
Jeff Brown9f25b7f2012-04-10 14:30:49 -070045import android.content.res.Resources;
Jeff Brown6ec6f792012-04-17 16:52:41 -070046import android.content.res.Resources.NotFoundException;
Jeff Brown9f25b7f2012-04-10 14:30:49 -070047import android.content.res.TypedArray;
48import android.content.res.XmlResourceParser;
Jeff Brown1a84fd12011-06-02 01:26:32 -070049import android.database.ContentObserver;
Jeff Brown4ccb8232014-01-16 22:16:42 -080050import android.hardware.display.DisplayViewport;
Jeff Brownaf9e8d32012-04-12 17:32:48 -070051import android.hardware.input.IInputDevicesChangedListener;
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -070052import android.hardware.input.IInputManager;
RoboErikfb290df2013-12-16 11:27:55 -080053import android.hardware.input.InputDeviceIdentifier;
Jeff Brownac143512012-04-05 18:57:33 -070054import android.hardware.input.InputManager;
Jeff Brown4ccb8232014-01-16 22:16:42 -080055import android.hardware.input.InputManagerInternal;
Michael Wright39e5e942015-08-19 22:52:47 +010056import android.hardware.input.ITabletModeChangedListener;
Jeff Brown9f25b7f2012-04-10 14:30:49 -070057import android.hardware.input.KeyboardLayout;
Jason Gerecked6396d62014-01-27 18:30:37 -080058import android.hardware.input.TouchCalibration;
Jeff Brown4532e612012-04-05 14:27:12 -070059import android.os.Binder;
Jeff Brown9f25b7f2012-04-10 14:30:49 -070060import android.os.Bundle;
Jeff Brown46b9ac02010-04-22 18:58:52 -070061import android.os.Environment;
Jeff Brown4532e612012-04-05 14:27:12 -070062import android.os.Handler;
Jeff Brownaf9e8d32012-04-12 17:32:48 -070063import android.os.IBinder;
Jeff Browna9d131c2012-09-20 16:48:17 -070064import android.os.Looper;
Jeff Brownaf9e8d32012-04-12 17:32:48 -070065import android.os.Message;
Jeff Brown05dc66a2011-03-02 14:41:58 -080066import android.os.MessageQueue;
Jeff Brownac143512012-04-05 18:57:33 -070067import android.os.Process;
Jeff Brownaf9e8d32012-04-12 17:32:48 -070068import android.os.RemoteException;
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -070069import android.os.UserHandle;
Jeff Brown1a84fd12011-06-02 01:26:32 -070070import android.provider.Settings;
71import android.provider.Settings.SettingNotFoundException;
Jeff Brown9f25b7f2012-04-10 14:30:49 -070072import android.util.Log;
Jeff Brown46b9ac02010-04-22 18:58:52 -070073import android.util.Slog;
Jeff Brownaf9e8d32012-04-12 17:32:48 -070074import android.util.SparseArray;
Jeff Brown46b9ac02010-04-22 18:58:52 -070075import android.util.Xml;
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -070076import android.view.IInputFilter;
77import android.view.IInputFilterHost;
Jeff Brown46b9ac02010-04-22 18:58:52 -070078import android.view.InputChannel;
Jeff Brown8d608662010-08-30 03:02:23 -070079import android.view.InputDevice;
Jeff Brown6ec402b2010-07-28 15:48:59 -070080import android.view.InputEvent;
Jeff Brown1f245102010-11-18 20:53:46 -080081import android.view.KeyEvent;
Jeff Brown2352b972011-04-12 22:39:53 -070082import android.view.PointerIcon;
Jason Gerecked5220742014-03-10 09:47:59 -070083import android.view.Surface;
Jeff Browna4547672011-03-02 21:38:11 -080084import android.view.ViewConfiguration;
Jeff Brown0029c662011-03-30 02:25:18 -070085import android.view.WindowManagerPolicy;
Jeff Browncf39bdf2012-05-18 14:41:19 -070086import android.widget.Toast;
Jeff Brown46b9ac02010-04-22 18:58:52 -070087
Jeff Brown46b9ac02010-04-22 18:58:52 -070088import java.io.File;
Jeff Brown4532e612012-04-05 14:27:12 -070089import java.io.FileDescriptor;
Jeff Brown46b9ac02010-04-22 18:58:52 -070090import java.io.FileNotFoundException;
91import java.io.FileReader;
92import java.io.IOException;
Jeff Brown6ec6f792012-04-17 16:52:41 -070093import java.io.InputStreamReader;
Jeff Brown46b9ac02010-04-22 18:58:52 -070094import java.io.PrintWriter;
95import java.util.ArrayList;
Jeff Brown9f25b7f2012-04-10 14:30:49 -070096import java.util.HashMap;
Jeff Browncf39bdf2012-05-18 14:41:19 -070097import java.util.HashSet;
Michael Wright39e5e942015-08-19 22:52:47 +010098import java.util.List;
Jeff Browna3bc5652012-04-17 11:42:25 -070099
Jeff Brown6ec6f792012-04-17 16:52:41 -0700100import libcore.io.Streams;
Jeff Browna3bc5652012-04-17 11:42:25 -0700101import libcore.util.Objects;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700102
103/*
104 * Wraps the C++ InputManager and provides its callbacks.
Jeff Brown46b9ac02010-04-22 18:58:52 -0700105 */
Jeff Brownd728bf52012-09-08 18:05:28 -0700106public class InputManagerService extends IInputManager.Stub
Jeff Brown4ccb8232014-01-16 22:16:42 -0800107 implements Watchdog.Monitor {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700108 static final String TAG = "InputManager";
Jeff Brown1b9ba572012-05-21 10:54:18 -0700109 static final boolean DEBUG = false;
Jeff Brownb6997262010-10-08 22:31:17 -0700110
Jeff Brown4532e612012-04-05 14:27:12 -0700111 private static final String EXCLUDED_DEVICES_PATH = "etc/excluded-input-devices.xml";
112
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700113 private static final int MSG_DELIVER_INPUT_DEVICES_CHANGED = 1;
Jeff Browncf39bdf2012-05-18 14:41:19 -0700114 private static final int MSG_SWITCH_KEYBOARD_LAYOUT = 2;
115 private static final int MSG_RELOAD_KEYBOARD_LAYOUTS = 3;
116 private static final int MSG_UPDATE_KEYBOARD_LAYOUTS = 4;
117 private static final int MSG_RELOAD_DEVICE_ALIASES = 5;
Michael Wright39e5e942015-08-19 22:52:47 +0100118 private static final int MSG_DELIVER_TABLET_MODE_CHANGED = 6;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700119
Jeff Brown4532e612012-04-05 14:27:12 -0700120 // Pointer to native input manager service object.
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000121 private final long mPtr;
Jeff Brown4532e612012-04-05 14:27:12 -0700122
Jeff Brown46b9ac02010-04-22 18:58:52 -0700123 private final Context mContext;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700124 private final InputManagerHandler mHandler;
Jeff Browna9d131c2012-09-20 16:48:17 -0700125
126 private WindowManagerCallbacks mWindowManagerCallbacks;
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700127 private WiredAccessoryCallbacks mWiredAccessoryCallbacks;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700128 private boolean mSystemReady;
Jeff Browncf39bdf2012-05-18 14:41:19 -0700129 private NotificationManager mNotificationManager;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700130
Michael Wright39e5e942015-08-19 22:52:47 +0100131 private final Object mTabletModeLock = new Object();
132 // List of currently registered tablet mode changed listeners by process id
133 private final SparseArray<TabletModeChangedListenerRecord> mTabletModeChangedListeners =
134 new SparseArray<>(); // guarded by mTabletModeLock
135 private final List<TabletModeChangedListenerRecord> mTempTabletModeChangedListenersToNotify =
136 new ArrayList<>();
137
Jeff Browna3bc5652012-04-17 11:42:25 -0700138 // Persistent data store. Must be locked each time during use.
139 private final PersistentDataStore mDataStore = new PersistentDataStore();
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700140
141 // List of currently registered input devices changed listeners by process id.
142 private Object mInputDevicesLock = new Object();
143 private boolean mInputDevicesChangedPending; // guarded by mInputDevicesLock
144 private InputDevice[] mInputDevices = new InputDevice[0];
145 private final SparseArray<InputDevicesChangedListenerRecord> mInputDevicesChangedListeners =
146 new SparseArray<InputDevicesChangedListenerRecord>(); // guarded by mInputDevicesLock
147 private final ArrayList<InputDevicesChangedListenerRecord>
148 mTempInputDevicesChangedListenersToNotify =
149 new ArrayList<InputDevicesChangedListenerRecord>(); // handler thread only
Jeff Browncf39bdf2012-05-18 14:41:19 -0700150 private final ArrayList<InputDevice>
151 mTempFullKeyboards = new ArrayList<InputDevice>(); // handler thread only
152 private boolean mKeyboardLayoutNotificationShown;
153 private PendingIntent mKeyboardLayoutIntent;
154 private Toast mSwitchedKeyboardLayoutToast;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700155
Jeff Browna47425a2012-04-13 04:09:27 -0700156 // State for vibrator tokens.
157 private Object mVibratorLock = new Object();
158 private HashMap<IBinder, VibratorToken> mVibratorTokens =
159 new HashMap<IBinder, VibratorToken>();
160 private int mNextVibratorTokenValue;
161
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700162 // State for the currently installed input filter.
163 final Object mInputFilterLock = new Object();
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -0700164 IInputFilter mInputFilter; // guarded by mInputFilterLock
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700165 InputFilterHost mInputFilterHost; // guarded by mInputFilterLock
Jeff Brown1a84fd12011-06-02 01:26:32 -0700166
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000167 private static native long nativeInit(InputManagerService service,
Jeff Brown4532e612012-04-05 14:27:12 -0700168 Context context, MessageQueue messageQueue);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000169 private static native void nativeStart(long ptr);
170 private static native void nativeSetDisplayViewport(long ptr, boolean external,
Jeff Brownd728bf52012-09-08 18:05:28 -0700171 int displayId, int rotation,
172 int logicalLeft, int logicalTop, int logicalRight, int logicalBottom,
Jeff Brown83d616a2012-09-09 20:33:43 -0700173 int physicalLeft, int physicalTop, int physicalRight, int physicalBottom,
174 int deviceWidth, int deviceHeight);
Jeff Brownd728bf52012-09-08 18:05:28 -0700175
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000176 private static native int nativeGetScanCodeState(long ptr,
Jeff Brown4532e612012-04-05 14:27:12 -0700177 int deviceId, int sourceMask, int scanCode);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000178 private static native int nativeGetKeyCodeState(long ptr,
Jeff Brown4532e612012-04-05 14:27:12 -0700179 int deviceId, int sourceMask, int keyCode);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000180 private static native int nativeGetSwitchState(long ptr,
Jeff Brown4532e612012-04-05 14:27:12 -0700181 int deviceId, int sourceMask, int sw);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000182 private static native boolean nativeHasKeys(long ptr,
Jeff Brown4532e612012-04-05 14:27:12 -0700183 int deviceId, int sourceMask, int[] keyCodes, boolean[] keyExists);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000184 private static native void nativeRegisterInputChannel(long ptr, InputChannel inputChannel,
Jeff Brown928e0542011-01-10 11:17:36 -0800185 InputWindowHandle inputWindowHandle, boolean monitor);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000186 private static native void nativeUnregisterInputChannel(long ptr, InputChannel inputChannel);
187 private static native void nativeSetInputFilterEnabled(long ptr, boolean enable);
Jeff Brownca9bc702014-02-11 14:32:56 -0800188 private static native int nativeInjectInputEvent(long ptr, InputEvent event, int displayId,
Jeff Brown0029c662011-03-30 02:25:18 -0700189 int injectorPid, int injectorUid, int syncMode, int timeoutMillis,
190 int policyFlags);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000191 private static native void nativeSetInputWindows(long ptr, InputWindowHandle[] windowHandles);
192 private static native void nativeSetInputDispatchMode(long ptr, boolean enabled, boolean frozen);
193 private static native void nativeSetSystemUiVisibility(long ptr, int visibility);
194 private static native void nativeSetFocusedApplication(long ptr,
Jeff Brown4532e612012-04-05 14:27:12 -0700195 InputApplicationHandle application);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000196 private static native boolean nativeTransferTouchFocus(long ptr,
Jeff Brown4532e612012-04-05 14:27:12 -0700197 InputChannel fromChannel, InputChannel toChannel);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000198 private static native void nativeSetPointerSpeed(long ptr, int speed);
199 private static native void nativeSetShowTouches(long ptr, boolean enabled);
Jeff Brown037c33e2014-04-09 00:31:55 -0700200 private static native void nativeSetInteractive(long ptr, boolean interactive);
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800201 private static native void nativeReloadCalibration(long ptr);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000202 private static native void nativeVibrate(long ptr, int deviceId, long[] pattern,
Jeff Browna47425a2012-04-13 04:09:27 -0700203 int repeat, int token);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000204 private static native void nativeCancelVibrate(long ptr, int deviceId, int token);
205 private static native void nativeReloadKeyboardLayouts(long ptr);
206 private static native void nativeReloadDeviceAliases(long ptr);
207 private static native String nativeDump(long ptr);
208 private static native void nativeMonitor(long ptr);
Jeff Brown4532e612012-04-05 14:27:12 -0700209
Jeff Brownac143512012-04-05 18:57:33 -0700210 // Input event injection constants defined in InputDispatcher.h.
211 private static final int INPUT_EVENT_INJECTION_SUCCEEDED = 0;
212 private static final int INPUT_EVENT_INJECTION_PERMISSION_DENIED = 1;
213 private static final int INPUT_EVENT_INJECTION_FAILED = 2;
214 private static final int INPUT_EVENT_INJECTION_TIMED_OUT = 3;
215
216 // Maximum number of milliseconds to wait for input event injection.
217 private static final int INJECTION_TIMEOUT_MILLIS = 30 * 1000;
218
Jeff Brown6d0fec22010-07-23 21:28:06 -0700219 // Key states (may be returned by queries about the current state of a
220 // particular key code, scan code or switch).
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700221
Jeff Brown6d0fec22010-07-23 21:28:06 -0700222 /** The key state is unknown or the requested key itself is not supported. */
223 public static final int KEY_STATE_UNKNOWN = -1;
224
225 /** The key is up. /*/
226 public static final int KEY_STATE_UP = 0;
227
228 /** The key is down. */
229 public static final int KEY_STATE_DOWN = 1;
230
231 /** The key is down but is a virtual key press that is being emulated by the system. */
232 public static final int KEY_STATE_VIRTUAL = 2;
233
Jeff Brownc458ce92012-04-30 14:58:40 -0700234 /** Scan code: Mouse / trackball button. */
235 public static final int BTN_MOUSE = 0x110;
236
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700237 // Switch code values must match bionic/libc/kernel/common/linux/input.h
Jeff Brownc458ce92012-04-30 14:58:40 -0700238 /** Switch code: Lid switch. When set, lid is shut. */
239 public static final int SW_LID = 0x00;
240
Michael Wright39e5e942015-08-19 22:52:47 +0100241 /** Switch code: Tablet mode switch.
242 * When set, the device is in tablet mode (i.e. no keyboard is connected).
243 */
244 public static final int SW_TABLET_MODE = 0x01;
245
Jeff Brownc458ce92012-04-30 14:58:40 -0700246 /** Switch code: Keypad slide. When set, keyboard is exposed. */
247 public static final int SW_KEYPAD_SLIDE = 0x0a;
248
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700249 /** Switch code: Headphone. When set, headphone is inserted. */
250 public static final int SW_HEADPHONE_INSERT = 0x02;
251
252 /** Switch code: Microphone. When set, microphone is inserted. */
253 public static final int SW_MICROPHONE_INSERT = 0x04;
254
Jon Eklund43cc8bb2014-07-28 16:07:24 -0500255 /** Switch code: Line out. When set, Line out (hi-Z) is inserted. */
256 public static final int SW_LINEOUT_INSERT = 0x06;
257
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700258 /** Switch code: Headphone/Microphone Jack. When set, something is inserted. */
259 public static final int SW_JACK_PHYSICAL_INSERT = 0x07;
260
Michael Wright3818c922014-09-02 13:59:07 -0700261 /** Switch code: Camera lens cover. When set the lens is covered. */
262 public static final int SW_CAMERA_LENS_COVER = 0x09;
263
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700264 public static final int SW_LID_BIT = 1 << SW_LID;
Michael Wright39e5e942015-08-19 22:52:47 +0100265 public static final int SW_TABLET_MODE_BIT = 1 << SW_TABLET_MODE;
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700266 public static final int SW_KEYPAD_SLIDE_BIT = 1 << SW_KEYPAD_SLIDE;
267 public static final int SW_HEADPHONE_INSERT_BIT = 1 << SW_HEADPHONE_INSERT;
268 public static final int SW_MICROPHONE_INSERT_BIT = 1 << SW_MICROPHONE_INSERT;
Jon Eklund43cc8bb2014-07-28 16:07:24 -0500269 public static final int SW_LINEOUT_INSERT_BIT = 1 << SW_LINEOUT_INSERT;
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700270 public static final int SW_JACK_PHYSICAL_INSERT_BIT = 1 << SW_JACK_PHYSICAL_INSERT;
271 public static final int SW_JACK_BITS =
Jon Eklund43cc8bb2014-07-28 16:07:24 -0500272 SW_HEADPHONE_INSERT_BIT | SW_MICROPHONE_INSERT_BIT | SW_JACK_PHYSICAL_INSERT_BIT | SW_LINEOUT_INSERT_BIT;
Michael Wright3818c922014-09-02 13:59:07 -0700273 public static final int SW_CAMERA_LENS_COVER_BIT = 1 << SW_CAMERA_LENS_COVER;
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700274
275 /** Whether to use the dev/input/event or uevent subsystem for the audio jack. */
276 final boolean mUseDevInputEventForAudioJack;
277
Jeff Brown4ccb8232014-01-16 22:16:42 -0800278 public InputManagerService(Context context) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700279 this.mContext = context;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800280 this.mHandler = new InputManagerHandler(DisplayThread.get().getLooper());
Jeff Brown05dc66a2011-03-02 14:41:58 -0800281
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700282 mUseDevInputEventForAudioJack =
283 context.getResources().getBoolean(R.bool.config_useDevInputEventForAudioJack);
284 Slog.i(TAG, "Initializing input manager, mUseDevInputEventForAudioJack="
285 + mUseDevInputEventForAudioJack);
Jeff Brown4532e612012-04-05 14:27:12 -0700286 mPtr = nativeInit(this, mContext, mHandler.getLooper().getQueue());
Jeff Brown4ccb8232014-01-16 22:16:42 -0800287
288 LocalServices.addService(InputManagerInternal.class, new LocalService());
Jeff Brown46b9ac02010-04-22 18:58:52 -0700289 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700290
Jeff Browna9d131c2012-09-20 16:48:17 -0700291 public void setWindowManagerCallbacks(WindowManagerCallbacks callbacks) {
292 mWindowManagerCallbacks = callbacks;
293 }
294
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700295 public void setWiredAccessoryCallbacks(WiredAccessoryCallbacks callbacks) {
296 mWiredAccessoryCallbacks = callbacks;
297 }
298
Jeff Brown46b9ac02010-04-22 18:58:52 -0700299 public void start() {
300 Slog.i(TAG, "Starting input manager");
Jeff Brown4532e612012-04-05 14:27:12 -0700301 nativeStart(mPtr);
302
303 // Add ourself to the Watchdog monitors.
304 Watchdog.getInstance().addMonitor(this);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700305
306 registerPointerSpeedSettingObserver();
Jeff Browndaf4a122011-08-26 17:14:14 -0700307 registerShowTouchesSettingObserver();
308
Jeff Brownd4935962012-09-25 13:27:20 -0700309 mContext.registerReceiver(new BroadcastReceiver() {
310 @Override
311 public void onReceive(Context context, Intent intent) {
312 updatePointerSpeedFromSettings();
313 updateShowTouchesFromSettings();
314 }
315 }, new IntentFilter(Intent.ACTION_USER_SWITCHED), null, mHandler);
316
Jeff Brown1a84fd12011-06-02 01:26:32 -0700317 updatePointerSpeedFromSettings();
Jeff Browndaf4a122011-08-26 17:14:14 -0700318 updateShowTouchesFromSettings();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700319 }
Jeff Brown6ec6f792012-04-17 16:52:41 -0700320
Matthew Xie96313142012-06-29 16:57:31 -0700321 // TODO(BT) Pass in paramter for bluetooth system
Svetoslav Ganova0027152013-06-25 14:59:53 -0700322 public void systemRunning() {
Jeff Brown6ec6f792012-04-17 16:52:41 -0700323 if (DEBUG) {
324 Slog.d(TAG, "System ready.");
325 }
Jeff Browncf39bdf2012-05-18 14:41:19 -0700326 mNotificationManager = (NotificationManager)mContext.getSystemService(
327 Context.NOTIFICATION_SERVICE);
Jeff Brown6ec6f792012-04-17 16:52:41 -0700328 mSystemReady = true;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700329
330 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
331 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
332 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
Jeff Brown69b07162013-11-07 00:30:16 -0800333 filter.addAction(Intent.ACTION_PACKAGE_REPLACED);
Jeff Brown6ec6f792012-04-17 16:52:41 -0700334 filter.addDataScheme("package");
335 mContext.registerReceiver(new BroadcastReceiver() {
336 @Override
337 public void onReceive(Context context, Intent intent) {
Jeff Browncf39bdf2012-05-18 14:41:19 -0700338 updateKeyboardLayouts();
Jeff Brown6ec6f792012-04-17 16:52:41 -0700339 }
340 }, filter, null, mHandler);
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700341
342 filter = new IntentFilter(BluetoothDevice.ACTION_ALIAS_CHANGED);
343 mContext.registerReceiver(new BroadcastReceiver() {
344 @Override
345 public void onReceive(Context context, Intent intent) {
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700346 reloadDeviceAliases();
347 }
348 }, filter, null, mHandler);
349
Jeff Browncf39bdf2012-05-18 14:41:19 -0700350 mHandler.sendEmptyMessage(MSG_RELOAD_DEVICE_ALIASES);
351 mHandler.sendEmptyMessage(MSG_UPDATE_KEYBOARD_LAYOUTS);
Eric Laurent4a5eeb92014-05-06 10:49:04 -0700352
353 if (mWiredAccessoryCallbacks != null) {
354 mWiredAccessoryCallbacks.systemReady();
355 }
Jeff Brown6ec6f792012-04-17 16:52:41 -0700356 }
357
358 private void reloadKeyboardLayouts() {
Jeff Browncf39bdf2012-05-18 14:41:19 -0700359 if (DEBUG) {
360 Slog.d(TAG, "Reloading keyboard layouts.");
361 }
Jeff Brown6ec6f792012-04-17 16:52:41 -0700362 nativeReloadKeyboardLayouts(mPtr);
363 }
364
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700365 private void reloadDeviceAliases() {
Jeff Browncf39bdf2012-05-18 14:41:19 -0700366 if (DEBUG) {
367 Slog.d(TAG, "Reloading device names.");
368 }
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700369 nativeReloadDeviceAliases(mPtr);
370 }
371
Jeff Brown4ccb8232014-01-16 22:16:42 -0800372 private void setDisplayViewportsInternal(DisplayViewport defaultViewport,
Jeff Brownd728bf52012-09-08 18:05:28 -0700373 DisplayViewport externalTouchViewport) {
374 if (defaultViewport.valid) {
375 setDisplayViewport(false, defaultViewport);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700376 }
Jeff Brownd728bf52012-09-08 18:05:28 -0700377
378 if (externalTouchViewport.valid) {
379 setDisplayViewport(true, externalTouchViewport);
380 } else if (defaultViewport.valid) {
381 setDisplayViewport(true, defaultViewport);
Jeff Brownb6997262010-10-08 22:31:17 -0700382 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700383 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700384
Jeff Brownd728bf52012-09-08 18:05:28 -0700385 private void setDisplayViewport(boolean external, DisplayViewport viewport) {
386 nativeSetDisplayViewport(mPtr, external,
387 viewport.displayId, viewport.orientation,
388 viewport.logicalFrame.left, viewport.logicalFrame.top,
389 viewport.logicalFrame.right, viewport.logicalFrame.bottom,
390 viewport.physicalFrame.left, viewport.physicalFrame.top,
Jeff Brown83d616a2012-09-09 20:33:43 -0700391 viewport.physicalFrame.right, viewport.physicalFrame.bottom,
392 viewport.deviceWidth, viewport.deviceHeight);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700393 }
Jeff Brownac143512012-04-05 18:57:33 -0700394
Jeff Brown6d0fec22010-07-23 21:28:06 -0700395 /**
396 * Gets the current state of a key or button by key code.
397 * @param deviceId The input device id, or -1 to consult all devices.
398 * @param sourceMask The input sources to consult, or {@link InputDevice#SOURCE_ANY} to
399 * consider all input sources. An input device is consulted if at least one of its
400 * non-class input source bits matches the specified source mask.
401 * @param keyCode The key code to check.
402 * @return The key state.
403 */
404 public int getKeyCodeState(int deviceId, int sourceMask, int keyCode) {
Jeff Brown4532e612012-04-05 14:27:12 -0700405 return nativeGetKeyCodeState(mPtr, deviceId, sourceMask, keyCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700406 }
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700407
Jeff Brown6d0fec22010-07-23 21:28:06 -0700408 /**
409 * Gets the current state of a key or button by scan code.
410 * @param deviceId The input device id, or -1 to consult all devices.
411 * @param sourceMask The input sources to consult, or {@link InputDevice#SOURCE_ANY} to
412 * consider all input sources. An input device is consulted if at least one of its
413 * non-class input source bits matches the specified source mask.
414 * @param scanCode The scan code to check.
415 * @return The key state.
416 */
417 public int getScanCodeState(int deviceId, int sourceMask, int scanCode) {
Jeff Brown4532e612012-04-05 14:27:12 -0700418 return nativeGetScanCodeState(mPtr, deviceId, sourceMask, scanCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700419 }
RoboErikfb290df2013-12-16 11:27:55 -0800420
Jeff Brown6d0fec22010-07-23 21:28:06 -0700421 /**
422 * Gets the current state of a switch by switch code.
423 * @param deviceId The input device id, or -1 to consult all devices.
424 * @param sourceMask The input sources to consult, or {@link InputDevice#SOURCE_ANY} to
425 * consider all input sources. An input device is consulted if at least one of its
426 * non-class input source bits matches the specified source mask.
427 * @param switchCode The switch code to check.
428 * @return The switch state.
429 */
430 public int getSwitchState(int deviceId, int sourceMask, int switchCode) {
Jeff Brown4532e612012-04-05 14:27:12 -0700431 return nativeGetSwitchState(mPtr, deviceId, sourceMask, switchCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700432 }
433
Jeff Brown6d0fec22010-07-23 21:28:06 -0700434 /**
435 * Determines whether the specified key codes are supported by a particular device.
436 * @param deviceId The input device id, or -1 to consult all devices.
437 * @param sourceMask The input sources to consult, or {@link InputDevice#SOURCE_ANY} to
438 * consider all input sources. An input device is consulted if at least one of its
439 * non-class input source bits matches the specified source mask.
440 * @param keyCodes The array of key codes to check.
441 * @param keyExists An array at least as large as keyCodes whose entries will be set
442 * to true or false based on the presence or absence of support for the corresponding
443 * key codes.
444 * @return True if the lookup was successful, false otherwise.
445 */
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700446 @Override // Binder call
Jeff Brown6d0fec22010-07-23 21:28:06 -0700447 public boolean hasKeys(int deviceId, int sourceMask, int[] keyCodes, boolean[] keyExists) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700448 if (keyCodes == null) {
449 throw new IllegalArgumentException("keyCodes must not be null.");
450 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700451 if (keyExists == null || keyExists.length < keyCodes.length) {
452 throw new IllegalArgumentException("keyExists must not be null and must be at "
453 + "least as large as keyCodes.");
Jeff Brown46b9ac02010-04-22 18:58:52 -0700454 }
RoboErikfb290df2013-12-16 11:27:55 -0800455
Jeff Brown4532e612012-04-05 14:27:12 -0700456 return nativeHasKeys(mPtr, deviceId, sourceMask, keyCodes, keyExists);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700457 }
RoboErikfb290df2013-12-16 11:27:55 -0800458
Jeff Browna41ca772010-08-11 14:46:32 -0700459 /**
460 * Creates an input channel that will receive all input from the input dispatcher.
461 * @param inputChannelName The input channel name.
462 * @return The input channel.
463 */
464 public InputChannel monitorInput(String inputChannelName) {
465 if (inputChannelName == null) {
466 throw new IllegalArgumentException("inputChannelName must not be null.");
467 }
RoboErikfb290df2013-12-16 11:27:55 -0800468
Jeff Browna41ca772010-08-11 14:46:32 -0700469 InputChannel[] inputChannels = InputChannel.openInputChannelPair(inputChannelName);
Jeff Brown4532e612012-04-05 14:27:12 -0700470 nativeRegisterInputChannel(mPtr, inputChannels[0], null, true);
Jeff Browna41ca772010-08-11 14:46:32 -0700471 inputChannels[0].dispose(); // don't need to retain the Java object reference
472 return inputChannels[1];
473 }
474
475 /**
476 * Registers an input channel so that it can be used as an input event target.
477 * @param inputChannel The input channel to register.
Jeff Brown928e0542011-01-10 11:17:36 -0800478 * @param inputWindowHandle The handle of the input window associated with the
479 * input channel, or null if none.
Jeff Browna41ca772010-08-11 14:46:32 -0700480 */
Jeff Brown928e0542011-01-10 11:17:36 -0800481 public void registerInputChannel(InputChannel inputChannel,
482 InputWindowHandle inputWindowHandle) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700483 if (inputChannel == null) {
484 throw new IllegalArgumentException("inputChannel must not be null.");
485 }
RoboErikfb290df2013-12-16 11:27:55 -0800486
Jeff Brown4532e612012-04-05 14:27:12 -0700487 nativeRegisterInputChannel(mPtr, inputChannel, inputWindowHandle, false);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700488 }
RoboErikfb290df2013-12-16 11:27:55 -0800489
Jeff Browna41ca772010-08-11 14:46:32 -0700490 /**
491 * Unregisters an input channel.
492 * @param inputChannel The input channel to unregister.
493 */
Jeff Brown46b9ac02010-04-22 18:58:52 -0700494 public void unregisterInputChannel(InputChannel inputChannel) {
495 if (inputChannel == null) {
496 throw new IllegalArgumentException("inputChannel must not be null.");
497 }
RoboErikfb290df2013-12-16 11:27:55 -0800498
Jeff Brown4532e612012-04-05 14:27:12 -0700499 nativeUnregisterInputChannel(mPtr, inputChannel);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700500 }
Jeff Brown0029c662011-03-30 02:25:18 -0700501
502 /**
503 * Sets an input filter that will receive all input events before they are dispatched.
504 * The input filter may then reinterpret input events or inject new ones.
505 *
506 * To ensure consistency, the input dispatcher automatically drops all events
507 * in progress whenever an input filter is installed or uninstalled. After an input
508 * filter is uninstalled, it can no longer send input events unless it is reinstalled.
509 * Any events it attempts to send after it has been uninstalled will be dropped.
510 *
511 * @param filter The input filter, or null to remove the current filter.
512 */
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -0700513 public void setInputFilter(IInputFilter filter) {
Jeff Brown0029c662011-03-30 02:25:18 -0700514 synchronized (mInputFilterLock) {
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -0700515 final IInputFilter oldFilter = mInputFilter;
Jeff Brown0029c662011-03-30 02:25:18 -0700516 if (oldFilter == filter) {
517 return; // nothing to do
518 }
519
520 if (oldFilter != null) {
521 mInputFilter = null;
522 mInputFilterHost.disconnectLocked();
523 mInputFilterHost = null;
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -0700524 try {
525 oldFilter.uninstall();
526 } catch (RemoteException re) {
527 /* ignore */
528 }
Jeff Brown0029c662011-03-30 02:25:18 -0700529 }
530
531 if (filter != null) {
532 mInputFilter = filter;
533 mInputFilterHost = new InputFilterHost();
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -0700534 try {
535 filter.install(mInputFilterHost);
536 } catch (RemoteException re) {
537 /* ignore */
538 }
Jeff Brown0029c662011-03-30 02:25:18 -0700539 }
540
Jeff Brown4532e612012-04-05 14:27:12 -0700541 nativeSetInputFilterEnabled(mPtr, filter != null);
Jeff Brown0029c662011-03-30 02:25:18 -0700542 }
543 }
544
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700545 @Override // Binder call
Jeff Brownac143512012-04-05 18:57:33 -0700546 public boolean injectInputEvent(InputEvent event, int mode) {
Jeff Brownca9bc702014-02-11 14:32:56 -0800547 return injectInputEventInternal(event, Display.DEFAULT_DISPLAY, mode);
548 }
549
550 private boolean injectInputEventInternal(InputEvent event, int displayId, int mode) {
Jeff Brown7fbdc842010-06-17 20:52:56 -0700551 if (event == null) {
552 throw new IllegalArgumentException("event must not be null");
553 }
Jeff Brownac143512012-04-05 18:57:33 -0700554 if (mode != InputManager.INJECT_INPUT_EVENT_MODE_ASYNC
555 && mode != InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH
556 && mode != InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_RESULT) {
557 throw new IllegalArgumentException("mode is invalid");
Jeff Brown7fbdc842010-06-17 20:52:56 -0700558 }
Jeff Brown6ec402b2010-07-28 15:48:59 -0700559
Jeff Brownac143512012-04-05 18:57:33 -0700560 final int pid = Binder.getCallingPid();
561 final int uid = Binder.getCallingUid();
562 final long ident = Binder.clearCallingIdentity();
563 final int result;
564 try {
Jeff Brownca9bc702014-02-11 14:32:56 -0800565 result = nativeInjectInputEvent(mPtr, event, displayId, pid, uid, mode,
Jeff Brownac143512012-04-05 18:57:33 -0700566 INJECTION_TIMEOUT_MILLIS, WindowManagerPolicy.FLAG_DISABLE_KEY_REPEAT);
567 } finally {
568 Binder.restoreCallingIdentity(ident);
569 }
570 switch (result) {
571 case INPUT_EVENT_INJECTION_PERMISSION_DENIED:
572 Slog.w(TAG, "Input event injection from pid " + pid + " permission denied.");
573 throw new SecurityException(
574 "Injecting to another application requires INJECT_EVENTS permission");
575 case INPUT_EVENT_INJECTION_SUCCEEDED:
576 return true;
577 case INPUT_EVENT_INJECTION_TIMED_OUT:
578 Slog.w(TAG, "Input event injection from pid " + pid + " timed out.");
579 return false;
580 case INPUT_EVENT_INJECTION_FAILED:
581 default:
582 Slog.w(TAG, "Input event injection from pid " + pid + " failed.");
583 return false;
584 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700585 }
Jeff Brown0029c662011-03-30 02:25:18 -0700586
Jeff Brown8d608662010-08-30 03:02:23 -0700587 /**
588 * Gets information about the input device with the specified id.
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700589 * @param deviceId The device id.
Jeff Brown8d608662010-08-30 03:02:23 -0700590 * @return The input device or null if not found.
591 */
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700592 @Override // Binder call
Jeff Brown8d608662010-08-30 03:02:23 -0700593 public InputDevice getInputDevice(int deviceId) {
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700594 synchronized (mInputDevicesLock) {
595 final int count = mInputDevices.length;
596 for (int i = 0; i < count; i++) {
597 final InputDevice inputDevice = mInputDevices[i];
598 if (inputDevice.getId() == deviceId) {
599 return inputDevice;
600 }
601 }
602 }
603 return null;
Jeff Brown8d608662010-08-30 03:02:23 -0700604 }
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700605
Jeff Brown8d608662010-08-30 03:02:23 -0700606 /**
607 * Gets the ids of all input devices in the system.
608 * @return The input device ids.
609 */
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700610 @Override // Binder call
Jeff Brown8d608662010-08-30 03:02:23 -0700611 public int[] getInputDeviceIds() {
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700612 synchronized (mInputDevicesLock) {
613 final int count = mInputDevices.length;
614 int[] ids = new int[count];
615 for (int i = 0; i < count; i++) {
616 ids[i] = mInputDevices[i].getId();
617 }
618 return ids;
619 }
620 }
621
Jeff Browndaa37532012-05-01 15:54:03 -0700622 /**
623 * Gets all input devices in the system.
624 * @return The array of input devices.
625 */
626 public InputDevice[] getInputDevices() {
627 synchronized (mInputDevicesLock) {
628 return mInputDevices;
629 }
630 }
631
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700632 @Override // Binder call
633 public void registerInputDevicesChangedListener(IInputDevicesChangedListener listener) {
634 if (listener == null) {
635 throw new IllegalArgumentException("listener must not be null");
636 }
637
638 synchronized (mInputDevicesLock) {
639 int callingPid = Binder.getCallingPid();
640 if (mInputDevicesChangedListeners.get(callingPid) != null) {
641 throw new SecurityException("The calling process has already "
642 + "registered an InputDevicesChangedListener.");
643 }
644
645 InputDevicesChangedListenerRecord record =
646 new InputDevicesChangedListenerRecord(callingPid, listener);
647 try {
648 IBinder binder = listener.asBinder();
649 binder.linkToDeath(record, 0);
650 } catch (RemoteException ex) {
651 // give up
652 throw new RuntimeException(ex);
653 }
654
655 mInputDevicesChangedListeners.put(callingPid, record);
656 }
657 }
658
659 private void onInputDevicesChangedListenerDied(int pid) {
660 synchronized (mInputDevicesLock) {
661 mInputDevicesChangedListeners.remove(pid);
662 }
663 }
664
665 // Must be called on handler.
Jeff Browncf39bdf2012-05-18 14:41:19 -0700666 private void deliverInputDevicesChanged(InputDevice[] oldInputDevices) {
667 // Scan for changes.
668 int numFullKeyboardsAdded = 0;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700669 mTempInputDevicesChangedListenersToNotify.clear();
Jeff Browncf39bdf2012-05-18 14:41:19 -0700670 mTempFullKeyboards.clear();
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700671 final int numListeners;
672 final int[] deviceIdAndGeneration;
673 synchronized (mInputDevicesLock) {
674 if (!mInputDevicesChangedPending) {
675 return;
676 }
677 mInputDevicesChangedPending = false;
678
679 numListeners = mInputDevicesChangedListeners.size();
680 for (int i = 0; i < numListeners; i++) {
681 mTempInputDevicesChangedListenersToNotify.add(
682 mInputDevicesChangedListeners.valueAt(i));
683 }
684
685 final int numDevices = mInputDevices.length;
686 deviceIdAndGeneration = new int[numDevices * 2];
687 for (int i = 0; i < numDevices; i++) {
688 final InputDevice inputDevice = mInputDevices[i];
689 deviceIdAndGeneration[i * 2] = inputDevice.getId();
690 deviceIdAndGeneration[i * 2 + 1] = inputDevice.getGeneration();
Jeff Browncf39bdf2012-05-18 14:41:19 -0700691
Jeff Brown7e4ff4b2012-05-30 14:32:16 -0700692 if (!inputDevice.isVirtual() && inputDevice.isFullKeyboard()) {
Jeff Browncf39bdf2012-05-18 14:41:19 -0700693 if (!containsInputDeviceWithDescriptor(oldInputDevices,
694 inputDevice.getDescriptor())) {
695 mTempFullKeyboards.add(numFullKeyboardsAdded++, inputDevice);
696 } else {
697 mTempFullKeyboards.add(inputDevice);
698 }
699 }
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700700 }
701 }
702
Jeff Browncf39bdf2012-05-18 14:41:19 -0700703 // Notify listeners.
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700704 for (int i = 0; i < numListeners; i++) {
705 mTempInputDevicesChangedListenersToNotify.get(i).notifyInputDevicesChanged(
706 deviceIdAndGeneration);
707 }
Jeff Browncf39bdf2012-05-18 14:41:19 -0700708 mTempInputDevicesChangedListenersToNotify.clear();
709
710 // Check for missing keyboard layouts.
711 if (mNotificationManager != null) {
712 final int numFullKeyboards = mTempFullKeyboards.size();
713 boolean missingLayoutForExternalKeyboard = false;
714 boolean missingLayoutForExternalKeyboardAdded = false;
Michael Wrightc93fbd12014-09-22 20:07:59 -0700715 boolean multipleMissingLayoutsForExternalKeyboardsAdded = false;
716 InputDevice keyboardMissingLayout = null;
Jeff Browncf39bdf2012-05-18 14:41:19 -0700717 synchronized (mDataStore) {
718 for (int i = 0; i < numFullKeyboards; i++) {
719 final InputDevice inputDevice = mTempFullKeyboards.get(i);
Michael Wright86aaca62014-09-08 18:54:45 -0700720 final String layout =
721 getCurrentKeyboardLayoutForInputDevice(inputDevice.getIdentifier());
722 if (layout == null) {
Jeff Browncf39bdf2012-05-18 14:41:19 -0700723 missingLayoutForExternalKeyboard = true;
724 if (i < numFullKeyboardsAdded) {
725 missingLayoutForExternalKeyboardAdded = true;
Michael Wrightc93fbd12014-09-22 20:07:59 -0700726 if (keyboardMissingLayout == null) {
727 keyboardMissingLayout = inputDevice;
728 } else {
729 multipleMissingLayoutsForExternalKeyboardsAdded = true;
730 }
Jeff Browncf39bdf2012-05-18 14:41:19 -0700731 }
732 }
733 }
734 }
735 if (missingLayoutForExternalKeyboard) {
736 if (missingLayoutForExternalKeyboardAdded) {
Michael Wrightc93fbd12014-09-22 20:07:59 -0700737 if (multipleMissingLayoutsForExternalKeyboardsAdded) {
738 // We have more than one keyboard missing a layout, so drop the
739 // user at the generic input methods page so they can pick which
740 // one to set.
741 showMissingKeyboardLayoutNotification(null);
742 } else {
743 showMissingKeyboardLayoutNotification(keyboardMissingLayout);
744 }
Jeff Browncf39bdf2012-05-18 14:41:19 -0700745 }
746 } else if (mKeyboardLayoutNotificationShown) {
747 hideMissingKeyboardLayoutNotification();
748 }
749 }
750 mTempFullKeyboards.clear();
751 }
752
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800753 @Override // Binder call & native callback
Jason Gerecked5220742014-03-10 09:47:59 -0700754 public TouchCalibration getTouchCalibrationForInputDevice(String inputDeviceDescriptor,
755 int surfaceRotation) {
Jason Gerecked6396d62014-01-27 18:30:37 -0800756 if (inputDeviceDescriptor == null) {
757 throw new IllegalArgumentException("inputDeviceDescriptor must not be null");
758 }
759
760 synchronized (mDataStore) {
Jason Gerecked5220742014-03-10 09:47:59 -0700761 return mDataStore.getTouchCalibration(inputDeviceDescriptor, surfaceRotation);
Jason Gerecked6396d62014-01-27 18:30:37 -0800762 }
763 }
764
765 @Override // Binder call
Jason Gerecked5220742014-03-10 09:47:59 -0700766 public void setTouchCalibrationForInputDevice(String inputDeviceDescriptor, int surfaceRotation,
Jason Gerecked6396d62014-01-27 18:30:37 -0800767 TouchCalibration calibration) {
768 if (!checkCallingPermission(android.Manifest.permission.SET_INPUT_CALIBRATION,
769 "setTouchCalibrationForInputDevice()")) {
770 throw new SecurityException("Requires SET_INPUT_CALIBRATION permission");
771 }
772 if (inputDeviceDescriptor == null) {
773 throw new IllegalArgumentException("inputDeviceDescriptor must not be null");
774 }
775 if (calibration == null) {
776 throw new IllegalArgumentException("calibration must not be null");
777 }
Jason Gerecked5220742014-03-10 09:47:59 -0700778 if (surfaceRotation < Surface.ROTATION_0 || surfaceRotation > Surface.ROTATION_270) {
779 throw new IllegalArgumentException("surfaceRotation value out of bounds");
780 }
Jason Gerecked6396d62014-01-27 18:30:37 -0800781
782 synchronized (mDataStore) {
783 try {
Jason Gerecked5220742014-03-10 09:47:59 -0700784 if (mDataStore.setTouchCalibration(inputDeviceDescriptor, surfaceRotation,
785 calibration)) {
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800786 nativeReloadCalibration(mPtr);
787 }
Jason Gerecked6396d62014-01-27 18:30:37 -0800788 } finally {
789 mDataStore.saveIfNeeded();
790 }
791 }
792 }
793
Michael Wright39e5e942015-08-19 22:52:47 +0100794 @Override // Binder call
795 public void registerTabletModeChangedListener(ITabletModeChangedListener listener) {
796 if (!checkCallingPermission(android.Manifest.permission.TABLET_MODE_LISTENER,
797 "registerTabletModeChangedListener()")) {
798 throw new SecurityException("Requires TABLET_MODE_LISTENER permission");
799 }
800 if (listener == null) {
801 throw new IllegalArgumentException("listener must not be null");
802 }
803
804 synchronized (mTabletModeLock) {
805 final int callingPid = Binder.getCallingPid();
806 if (mTabletModeChangedListeners.get(callingPid) != null) {
807 throw new IllegalStateException("The calling process has already registered "
808 + "a TabletModeChangedListener.");
809 }
810 TabletModeChangedListenerRecord record =
811 new TabletModeChangedListenerRecord(callingPid, listener);
812 try {
813 IBinder binder = listener.asBinder();
814 binder.linkToDeath(record, 0);
815 } catch (RemoteException ex) {
816 throw new RuntimeException(ex);
817 }
818 mTabletModeChangedListeners.put(callingPid, record);
819 }
820 }
821
822 private void onTabletModeChangedListenerDied(int pid) {
823 synchronized (mTabletModeLock) {
824 mTabletModeChangedListeners.remove(pid);
825 }
826 }
827
828 // Must be called on handler
829 private void deliverTabletModeChanged(long whenNanos, boolean inTabletMode) {
830 mTempTabletModeChangedListenersToNotify.clear();
831 final int numListeners;
832 synchronized (mTabletModeLock) {
833 numListeners = mTabletModeChangedListeners.size();
834 for (int i = 0; i < numListeners; i++) {
835 mTempTabletModeChangedListenersToNotify.add(
836 mTabletModeChangedListeners.valueAt(i));
837 }
838 }
839 for (int i = 0; i < numListeners; i++) {
840 mTempTabletModeChangedListenersToNotify.get(i).notifyTabletModeChanged(
841 whenNanos, inTabletMode);
842 }
843 }
844
Jeff Browncf39bdf2012-05-18 14:41:19 -0700845 // Must be called on handler.
Michael Wrightc93fbd12014-09-22 20:07:59 -0700846 private void showMissingKeyboardLayoutNotification(InputDevice device) {
Jeff Browncf39bdf2012-05-18 14:41:19 -0700847 if (!mKeyboardLayoutNotificationShown) {
Michael Wrightc93fbd12014-09-22 20:07:59 -0700848 final Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
849 if (device != null) {
850 intent.putExtra(Settings.EXTRA_INPUT_DEVICE_IDENTIFIER, device.getIdentifier());
Jeff Browncf39bdf2012-05-18 14:41:19 -0700851 }
Michael Wrightc93fbd12014-09-22 20:07:59 -0700852 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
853 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
854 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
855 final PendingIntent keyboardLayoutIntent = PendingIntent.getActivityAsUser(mContext, 0,
856 intent, 0, null, UserHandle.CURRENT);
Jeff Browncf39bdf2012-05-18 14:41:19 -0700857
858 Resources r = mContext.getResources();
859 Notification notification = new Notification.Builder(mContext)
860 .setContentTitle(r.getString(
861 R.string.select_keyboard_layout_notification_title))
862 .setContentText(r.getString(
863 R.string.select_keyboard_layout_notification_message))
Michael Wrightc93fbd12014-09-22 20:07:59 -0700864 .setContentIntent(keyboardLayoutIntent)
Jeff Browncf39bdf2012-05-18 14:41:19 -0700865 .setSmallIcon(R.drawable.ic_settings_language)
866 .setPriority(Notification.PRIORITY_LOW)
Alan Viverette4a357cd2015-03-18 18:37:18 -0700867 .setColor(mContext.getColor(
Selim Cinek255dd042014-08-19 22:29:02 +0200868 com.android.internal.R.color.system_notification_accent_color))
Jeff Browncf39bdf2012-05-18 14:41:19 -0700869 .build();
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700870 mNotificationManager.notifyAsUser(null,
871 R.string.select_keyboard_layout_notification_title,
872 notification, UserHandle.ALL);
Jeff Browncf39bdf2012-05-18 14:41:19 -0700873 mKeyboardLayoutNotificationShown = true;
874 }
875 }
876
877 // Must be called on handler.
878 private void hideMissingKeyboardLayoutNotification() {
879 if (mKeyboardLayoutNotificationShown) {
880 mKeyboardLayoutNotificationShown = false;
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700881 mNotificationManager.cancelAsUser(null,
882 R.string.select_keyboard_layout_notification_title,
883 UserHandle.ALL);
Jeff Browncf39bdf2012-05-18 14:41:19 -0700884 }
885 }
886
887 // Must be called on handler.
888 private void updateKeyboardLayouts() {
889 // Scan all input devices state for keyboard layouts that have been uninstalled.
890 final HashSet<String> availableKeyboardLayouts = new HashSet<String>();
891 visitAllKeyboardLayouts(new KeyboardLayoutVisitor() {
892 @Override
Michael Wright8ebac232014-09-18 18:29:49 -0700893 public void visitKeyboardLayout(Resources resources, String descriptor, String label,
894 String collection, int keyboardLayoutResId, int priority) {
Jeff Browncf39bdf2012-05-18 14:41:19 -0700895 availableKeyboardLayouts.add(descriptor);
896 }
897 });
898 synchronized (mDataStore) {
899 try {
900 mDataStore.removeUninstalledKeyboardLayouts(availableKeyboardLayouts);
901 } finally {
902 mDataStore.saveIfNeeded();
903 }
904 }
905
906 // Reload keyboard layouts.
907 reloadKeyboardLayouts();
908 }
909
Jeff Browncf39bdf2012-05-18 14:41:19 -0700910 private static boolean containsInputDeviceWithDescriptor(InputDevice[] inputDevices,
911 String descriptor) {
912 final int numDevices = inputDevices.length;
913 for (int i = 0; i < numDevices; i++) {
914 final InputDevice inputDevice = inputDevices[i];
915 if (inputDevice.getDescriptor().equals(descriptor)) {
916 return true;
917 }
918 }
919 return false;
Jeff Brown8d608662010-08-30 03:02:23 -0700920 }
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700921
922 @Override // Binder call
923 public KeyboardLayout[] getKeyboardLayouts() {
Jeff Brown6ec6f792012-04-17 16:52:41 -0700924 final ArrayList<KeyboardLayout> list = new ArrayList<KeyboardLayout>();
925 visitAllKeyboardLayouts(new KeyboardLayoutVisitor() {
926 @Override
Michael Wright8ebac232014-09-18 18:29:49 -0700927 public void visitKeyboardLayout(Resources resources, String descriptor, String label,
928 String collection, int keyboardLayoutResId, int priority) {
929 list.add(new KeyboardLayout(descriptor, label, collection, priority));
Jeff Brown6ec6f792012-04-17 16:52:41 -0700930 }
931 });
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700932 return list.toArray(new KeyboardLayout[list.size()]);
933 }
934
935 @Override // Binder call
936 public KeyboardLayout getKeyboardLayout(String keyboardLayoutDescriptor) {
937 if (keyboardLayoutDescriptor == null) {
938 throw new IllegalArgumentException("keyboardLayoutDescriptor must not be null");
939 }
940
Jeff Brown6ec6f792012-04-17 16:52:41 -0700941 final KeyboardLayout[] result = new KeyboardLayout[1];
942 visitKeyboardLayout(keyboardLayoutDescriptor, new KeyboardLayoutVisitor() {
943 @Override
Michael Wright8ebac232014-09-18 18:29:49 -0700944 public void visitKeyboardLayout(Resources resources, String descriptor,
945 String label, String collection, int keyboardLayoutResId, int priority) {
946 result[0] = new KeyboardLayout(descriptor, label, collection, priority);
Jeff Brown6ec6f792012-04-17 16:52:41 -0700947 }
948 });
949 if (result[0] == null) {
950 Log.w(TAG, "Could not get keyboard layout with descriptor '"
951 + keyboardLayoutDescriptor + "'.");
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700952 }
Jeff Brown6ec6f792012-04-17 16:52:41 -0700953 return result[0];
954 }
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700955
Jeff Brown6ec6f792012-04-17 16:52:41 -0700956 private void visitAllKeyboardLayouts(KeyboardLayoutVisitor visitor) {
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700957 final PackageManager pm = mContext.getPackageManager();
Jeff Brown6ec6f792012-04-17 16:52:41 -0700958 Intent intent = new Intent(InputManager.ACTION_QUERY_KEYBOARD_LAYOUTS);
959 for (ResolveInfo resolveInfo : pm.queryBroadcastReceivers(intent,
960 PackageManager.GET_META_DATA)) {
Michael Wright8ebac232014-09-18 18:29:49 -0700961 final ActivityInfo activityInfo = resolveInfo.activityInfo;
962 final int priority = resolveInfo.priority;
963 visitKeyboardLayoutsInPackage(pm, activityInfo, null, priority, visitor);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700964 }
965 }
966
Jeff Brown6ec6f792012-04-17 16:52:41 -0700967 private void visitKeyboardLayout(String keyboardLayoutDescriptor,
968 KeyboardLayoutVisitor visitor) {
969 KeyboardLayoutDescriptor d = KeyboardLayoutDescriptor.parse(keyboardLayoutDescriptor);
970 if (d != null) {
971 final PackageManager pm = mContext.getPackageManager();
972 try {
973 ActivityInfo receiver = pm.getReceiverInfo(
974 new ComponentName(d.packageName, d.receiverName),
975 PackageManager.GET_META_DATA);
Michael Wright8ebac232014-09-18 18:29:49 -0700976 visitKeyboardLayoutsInPackage(pm, receiver, d.keyboardLayoutName, 0, visitor);
Jeff Brown6ec6f792012-04-17 16:52:41 -0700977 } catch (NameNotFoundException ex) {
978 }
979 }
980 }
981
982 private void visitKeyboardLayoutsInPackage(PackageManager pm, ActivityInfo receiver,
Michael Wright8ebac232014-09-18 18:29:49 -0700983 String keyboardName, int requestedPriority, KeyboardLayoutVisitor visitor) {
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700984 Bundle metaData = receiver.metaData;
985 if (metaData == null) {
Jeff Brown6ec6f792012-04-17 16:52:41 -0700986 return;
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700987 }
988
989 int configResId = metaData.getInt(InputManager.META_DATA_KEYBOARD_LAYOUTS);
990 if (configResId == 0) {
991 Log.w(TAG, "Missing meta-data '" + InputManager.META_DATA_KEYBOARD_LAYOUTS
992 + "' on receiver " + receiver.packageName + "/" + receiver.name);
Jeff Brown6ec6f792012-04-17 16:52:41 -0700993 return;
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700994 }
995
Jeff Brownd9fec5d2012-05-17 16:01:54 -0700996 CharSequence receiverLabel = receiver.loadLabel(pm);
997 String collection = receiverLabel != null ? receiverLabel.toString() : "";
Michael Wright8ebac232014-09-18 18:29:49 -0700998 int priority;
999 if ((receiver.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
1000 priority = requestedPriority;
1001 } else {
1002 priority = 0;
1003 }
Jeff Brownd9fec5d2012-05-17 16:01:54 -07001004
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001005 try {
1006 Resources resources = pm.getResourcesForApplication(receiver.applicationInfo);
1007 XmlResourceParser parser = resources.getXml(configResId);
1008 try {
1009 XmlUtils.beginDocument(parser, "keyboard-layouts");
1010
1011 for (;;) {
1012 XmlUtils.nextElement(parser);
1013 String element = parser.getName();
1014 if (element == null) {
1015 break;
1016 }
1017 if (element.equals("keyboard-layout")) {
1018 TypedArray a = resources.obtainAttributes(
1019 parser, com.android.internal.R.styleable.KeyboardLayout);
1020 try {
1021 String name = a.getString(
1022 com.android.internal.R.styleable.KeyboardLayout_name);
1023 String label = a.getString(
1024 com.android.internal.R.styleable.KeyboardLayout_label);
Jeff Brown2f095762012-05-10 21:29:33 -07001025 int keyboardLayoutResId = a.getResourceId(
1026 com.android.internal.R.styleable.KeyboardLayout_keyboardLayout,
1027 0);
1028 if (name == null || label == null || keyboardLayoutResId == 0) {
1029 Log.w(TAG, "Missing required 'name', 'label' or 'keyboardLayout' "
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001030 + "attributes in keyboard layout "
1031 + "resource from receiver "
1032 + receiver.packageName + "/" + receiver.name);
1033 } else {
1034 String descriptor = KeyboardLayoutDescriptor.format(
1035 receiver.packageName, receiver.name, name);
Jeff Brown6ec6f792012-04-17 16:52:41 -07001036 if (keyboardName == null || name.equals(keyboardName)) {
1037 visitor.visitKeyboardLayout(resources, descriptor,
Michael Wright8ebac232014-09-18 18:29:49 -07001038 label, collection, keyboardLayoutResId, priority);
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001039 }
1040 }
1041 } finally {
1042 a.recycle();
1043 }
1044 } else {
1045 Log.w(TAG, "Skipping unrecognized element '" + element
1046 + "' in keyboard layout resource from receiver "
1047 + receiver.packageName + "/" + receiver.name);
1048 }
1049 }
1050 } finally {
1051 parser.close();
1052 }
1053 } catch (Exception ex) {
Jeff Brown6ec6f792012-04-17 16:52:41 -07001054 Log.w(TAG, "Could not parse keyboard layout resource from receiver "
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001055 + receiver.packageName + "/" + receiver.name, ex);
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001056 }
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001057 }
1058
RoboErikfb290df2013-12-16 11:27:55 -08001059 /**
1060 * Builds a layout descriptor for the vendor/product. This returns the
1061 * descriptor for ids that aren't useful (such as the default 0, 0).
1062 */
1063 private String getLayoutDescriptor(InputDeviceIdentifier identifier) {
1064 if (identifier == null || identifier.getDescriptor() == null) {
1065 throw new IllegalArgumentException("identifier and descriptor must not be null");
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001066 }
1067
RoboErikfb290df2013-12-16 11:27:55 -08001068 if (identifier.getVendorId() == 0 && identifier.getProductId() == 0) {
1069 return identifier.getDescriptor();
1070 }
1071 StringBuilder bob = new StringBuilder();
1072 bob.append("vendor:").append(identifier.getVendorId());
1073 bob.append(",product:").append(identifier.getProductId());
1074 return bob.toString();
1075 }
1076
1077 @Override // Binder call
1078 public String getCurrentKeyboardLayoutForInputDevice(InputDeviceIdentifier identifier) {
1079
1080 String key = getLayoutDescriptor(identifier);
Jeff Browna3bc5652012-04-17 11:42:25 -07001081 synchronized (mDataStore) {
RoboErikfb290df2013-12-16 11:27:55 -08001082 String layout = null;
1083 // try loading it using the layout descriptor if we have it
1084 layout = mDataStore.getCurrentKeyboardLayout(key);
1085 if (layout == null && !key.equals(identifier.getDescriptor())) {
1086 // if it doesn't exist fall back to the device descriptor
1087 layout = mDataStore.getCurrentKeyboardLayout(identifier.getDescriptor());
1088 }
1089 if (DEBUG) {
1090 Slog.d(TAG, "Loaded keyboard layout id for " + key + " and got "
1091 + layout);
1092 }
1093 return layout;
Jeff Browna3bc5652012-04-17 11:42:25 -07001094 }
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001095 }
1096
1097 @Override // Binder call
RoboErikfb290df2013-12-16 11:27:55 -08001098 public void setCurrentKeyboardLayoutForInputDevice(InputDeviceIdentifier identifier,
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001099 String keyboardLayoutDescriptor) {
1100 if (!checkCallingPermission(android.Manifest.permission.SET_KEYBOARD_LAYOUT,
Jeff Browncf39bdf2012-05-18 14:41:19 -07001101 "setCurrentKeyboardLayoutForInputDevice()")) {
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001102 throw new SecurityException("Requires SET_KEYBOARD_LAYOUT permission");
1103 }
Jeff Browncf39bdf2012-05-18 14:41:19 -07001104 if (keyboardLayoutDescriptor == null) {
1105 throw new IllegalArgumentException("keyboardLayoutDescriptor must not be null");
1106 }
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001107
RoboErikfb290df2013-12-16 11:27:55 -08001108 String key = getLayoutDescriptor(identifier);
Jeff Browna3bc5652012-04-17 11:42:25 -07001109 synchronized (mDataStore) {
1110 try {
RoboErikfb290df2013-12-16 11:27:55 -08001111 if (mDataStore.setCurrentKeyboardLayout(key, keyboardLayoutDescriptor)) {
1112 if (DEBUG) {
1113 Slog.d(TAG, "Saved keyboard layout using " + key);
1114 }
Jeff Browncf39bdf2012-05-18 14:41:19 -07001115 mHandler.sendEmptyMessage(MSG_RELOAD_KEYBOARD_LAYOUTS);
1116 }
Jeff Browna3bc5652012-04-17 11:42:25 -07001117 } finally {
1118 mDataStore.saveIfNeeded();
1119 }
1120 }
Jeff Browncf39bdf2012-05-18 14:41:19 -07001121 }
Jeff Brown6ec6f792012-04-17 16:52:41 -07001122
Jeff Browncf39bdf2012-05-18 14:41:19 -07001123 @Override // Binder call
RoboErikfb290df2013-12-16 11:27:55 -08001124 public String[] getKeyboardLayoutsForInputDevice(InputDeviceIdentifier identifier) {
1125 String key = getLayoutDescriptor(identifier);
Jeff Browncf39bdf2012-05-18 14:41:19 -07001126 synchronized (mDataStore) {
RoboErikfb290df2013-12-16 11:27:55 -08001127 String[] layouts = mDataStore.getKeyboardLayouts(key);
1128 if ((layouts == null || layouts.length == 0)
1129 && !key.equals(identifier.getDescriptor())) {
1130 layouts = mDataStore.getKeyboardLayouts(identifier.getDescriptor());
1131 }
1132 return layouts;
Jeff Browncf39bdf2012-05-18 14:41:19 -07001133 }
1134 }
1135
1136 @Override // Binder call
RoboErikfb290df2013-12-16 11:27:55 -08001137 public void addKeyboardLayoutForInputDevice(InputDeviceIdentifier identifier,
Jeff Browncf39bdf2012-05-18 14:41:19 -07001138 String keyboardLayoutDescriptor) {
1139 if (!checkCallingPermission(android.Manifest.permission.SET_KEYBOARD_LAYOUT,
1140 "addKeyboardLayoutForInputDevice()")) {
1141 throw new SecurityException("Requires SET_KEYBOARD_LAYOUT permission");
1142 }
Jeff Browncf39bdf2012-05-18 14:41:19 -07001143 if (keyboardLayoutDescriptor == null) {
1144 throw new IllegalArgumentException("keyboardLayoutDescriptor must not be null");
1145 }
1146
RoboErikfb290df2013-12-16 11:27:55 -08001147 String key = getLayoutDescriptor(identifier);
Jeff Browncf39bdf2012-05-18 14:41:19 -07001148 synchronized (mDataStore) {
1149 try {
RoboErikfb290df2013-12-16 11:27:55 -08001150 String oldLayout = mDataStore.getCurrentKeyboardLayout(key);
1151 if (oldLayout == null && !key.equals(identifier.getDescriptor())) {
1152 oldLayout = mDataStore.getCurrentKeyboardLayout(identifier.getDescriptor());
1153 }
1154 if (mDataStore.addKeyboardLayout(key, keyboardLayoutDescriptor)
Jeff Browncf39bdf2012-05-18 14:41:19 -07001155 && !Objects.equal(oldLayout,
RoboErikfb290df2013-12-16 11:27:55 -08001156 mDataStore.getCurrentKeyboardLayout(key))) {
Jeff Browncf39bdf2012-05-18 14:41:19 -07001157 mHandler.sendEmptyMessage(MSG_RELOAD_KEYBOARD_LAYOUTS);
1158 }
1159 } finally {
1160 mDataStore.saveIfNeeded();
Jeff Brown6ec6f792012-04-17 16:52:41 -07001161 }
Jeff Browncf39bdf2012-05-18 14:41:19 -07001162 }
1163 }
1164
1165 @Override // Binder call
RoboErikfb290df2013-12-16 11:27:55 -08001166 public void removeKeyboardLayoutForInputDevice(InputDeviceIdentifier identifier,
Jeff Browncf39bdf2012-05-18 14:41:19 -07001167 String keyboardLayoutDescriptor) {
1168 if (!checkCallingPermission(android.Manifest.permission.SET_KEYBOARD_LAYOUT,
1169 "removeKeyboardLayoutForInputDevice()")) {
1170 throw new SecurityException("Requires SET_KEYBOARD_LAYOUT permission");
1171 }
Jeff Browncf39bdf2012-05-18 14:41:19 -07001172 if (keyboardLayoutDescriptor == null) {
1173 throw new IllegalArgumentException("keyboardLayoutDescriptor must not be null");
1174 }
1175
RoboErikfb290df2013-12-16 11:27:55 -08001176 String key = getLayoutDescriptor(identifier);
Jeff Browncf39bdf2012-05-18 14:41:19 -07001177 synchronized (mDataStore) {
1178 try {
RoboErikfb290df2013-12-16 11:27:55 -08001179 String oldLayout = mDataStore.getCurrentKeyboardLayout(key);
1180 if (oldLayout == null && !key.equals(identifier.getDescriptor())) {
1181 oldLayout = mDataStore.getCurrentKeyboardLayout(identifier.getDescriptor());
1182 }
1183 boolean removed = mDataStore.removeKeyboardLayout(key, keyboardLayoutDescriptor);
1184 if (!key.equals(identifier.getDescriptor())) {
1185 // We need to remove from both places to ensure it is gone
1186 removed |= mDataStore.removeKeyboardLayout(identifier.getDescriptor(),
1187 keyboardLayoutDescriptor);
1188 }
1189 if (removed && !Objects.equal(oldLayout,
1190 mDataStore.getCurrentKeyboardLayout(key))) {
Jeff Browncf39bdf2012-05-18 14:41:19 -07001191 mHandler.sendEmptyMessage(MSG_RELOAD_KEYBOARD_LAYOUTS);
1192 }
1193 } finally {
1194 mDataStore.saveIfNeeded();
1195 }
1196 }
1197 }
1198
1199 public void switchKeyboardLayout(int deviceId, int direction) {
1200 mHandler.obtainMessage(MSG_SWITCH_KEYBOARD_LAYOUT, deviceId, direction).sendToTarget();
1201 }
1202
1203 // Must be called on handler.
1204 private void handleSwitchKeyboardLayout(int deviceId, int direction) {
1205 final InputDevice device = getInputDevice(deviceId);
Jeff Browncf39bdf2012-05-18 14:41:19 -07001206 if (device != null) {
1207 final boolean changed;
1208 final String keyboardLayoutDescriptor;
RoboErikfb290df2013-12-16 11:27:55 -08001209
1210 String key = getLayoutDescriptor(device.getIdentifier());
Jeff Browncf39bdf2012-05-18 14:41:19 -07001211 synchronized (mDataStore) {
1212 try {
RoboErikfb290df2013-12-16 11:27:55 -08001213 changed = mDataStore.switchKeyboardLayout(key, direction);
Jeff Browncf39bdf2012-05-18 14:41:19 -07001214 keyboardLayoutDescriptor = mDataStore.getCurrentKeyboardLayout(
RoboErikfb290df2013-12-16 11:27:55 -08001215 key);
Jeff Browncf39bdf2012-05-18 14:41:19 -07001216 } finally {
1217 mDataStore.saveIfNeeded();
1218 }
1219 }
1220
1221 if (changed) {
1222 if (mSwitchedKeyboardLayoutToast != null) {
1223 mSwitchedKeyboardLayoutToast.cancel();
1224 mSwitchedKeyboardLayoutToast = null;
1225 }
1226 if (keyboardLayoutDescriptor != null) {
1227 KeyboardLayout keyboardLayout = getKeyboardLayout(keyboardLayoutDescriptor);
1228 if (keyboardLayout != null) {
1229 mSwitchedKeyboardLayoutToast = Toast.makeText(
1230 mContext, keyboardLayout.getLabel(), Toast.LENGTH_SHORT);
1231 mSwitchedKeyboardLayoutToast.show();
1232 }
1233 }
1234
1235 reloadKeyboardLayouts();
1236 }
Jeff Brown6ec6f792012-04-17 16:52:41 -07001237 }
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001238 }
1239
Jeff Brown9302c872011-07-13 22:51:29 -07001240 public void setInputWindows(InputWindowHandle[] windowHandles) {
Jeff Brown4532e612012-04-05 14:27:12 -07001241 nativeSetInputWindows(mPtr, windowHandles);
Jeff Brown349703e2010-06-22 01:27:15 -07001242 }
RoboErikfb290df2013-12-16 11:27:55 -08001243
Jeff Brown9302c872011-07-13 22:51:29 -07001244 public void setFocusedApplication(InputApplicationHandle application) {
Jeff Brown4532e612012-04-05 14:27:12 -07001245 nativeSetFocusedApplication(mPtr, application);
Jeff Brown349703e2010-06-22 01:27:15 -07001246 }
RoboErikfb290df2013-12-16 11:27:55 -08001247
Jeff Brown349703e2010-06-22 01:27:15 -07001248 public void setInputDispatchMode(boolean enabled, boolean frozen) {
Jeff Brown4532e612012-04-05 14:27:12 -07001249 nativeSetInputDispatchMode(mPtr, enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -07001250 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08001251
1252 public void setSystemUiVisibility(int visibility) {
Jeff Brown4532e612012-04-05 14:27:12 -07001253 nativeSetSystemUiVisibility(mPtr, visibility);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001254 }
1255
Jeff Browne6504122010-09-27 14:52:15 -07001256 /**
1257 * Atomically transfers touch focus from one window to another as identified by
1258 * their input channels. It is possible for multiple windows to have
1259 * touch focus if they support split touch dispatch
1260 * {@link android.view.WindowManager.LayoutParams#FLAG_SPLIT_TOUCH} but this
1261 * method only transfers touch focus of the specified window without affecting
1262 * other windows that may also have touch focus at the same time.
1263 * @param fromChannel The channel of a window that currently has touch focus.
1264 * @param toChannel The channel of the window that should receive touch focus in
1265 * place of the first.
1266 * @return True if the transfer was successful. False if the window with the
1267 * specified channel did not actually have touch focus at the time of the request.
1268 */
1269 public boolean transferTouchFocus(InputChannel fromChannel, InputChannel toChannel) {
1270 if (fromChannel == null) {
1271 throw new IllegalArgumentException("fromChannel must not be null.");
1272 }
1273 if (toChannel == null) {
1274 throw new IllegalArgumentException("toChannel must not be null.");
1275 }
Jeff Brown4532e612012-04-05 14:27:12 -07001276 return nativeTransferTouchFocus(mPtr, fromChannel, toChannel);
Jeff Browne6504122010-09-27 14:52:15 -07001277 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08001278
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001279 @Override // Binder call
Jeff Brownac143512012-04-05 18:57:33 -07001280 public void tryPointerSpeed(int speed) {
1281 if (!checkCallingPermission(android.Manifest.permission.SET_POINTER_SPEED,
1282 "tryPointerSpeed()")) {
1283 throw new SecurityException("Requires SET_POINTER_SPEED permission");
1284 }
1285
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001286 if (speed < InputManager.MIN_POINTER_SPEED || speed > InputManager.MAX_POINTER_SPEED) {
1287 throw new IllegalArgumentException("speed out of range");
1288 }
1289
Jeff Brownac143512012-04-05 18:57:33 -07001290 setPointerSpeedUnchecked(speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001291 }
1292
1293 public void updatePointerSpeedFromSettings() {
Jeff Brownac143512012-04-05 18:57:33 -07001294 int speed = getPointerSpeedSetting();
1295 setPointerSpeedUnchecked(speed);
1296 }
1297
1298 private void setPointerSpeedUnchecked(int speed) {
1299 speed = Math.min(Math.max(speed, InputManager.MIN_POINTER_SPEED),
1300 InputManager.MAX_POINTER_SPEED);
1301 nativeSetPointerSpeed(mPtr, speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001302 }
1303
1304 private void registerPointerSpeedSettingObserver() {
1305 mContext.getContentResolver().registerContentObserver(
1306 Settings.System.getUriFor(Settings.System.POINTER_SPEED), true,
Jeff Brown4532e612012-04-05 14:27:12 -07001307 new ContentObserver(mHandler) {
Jeff Brown1a84fd12011-06-02 01:26:32 -07001308 @Override
1309 public void onChange(boolean selfChange) {
1310 updatePointerSpeedFromSettings();
1311 }
Jeff Brownd4935962012-09-25 13:27:20 -07001312 }, UserHandle.USER_ALL);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001313 }
1314
Jeff Brownac143512012-04-05 18:57:33 -07001315 private int getPointerSpeedSetting() {
1316 int speed = InputManager.DEFAULT_POINTER_SPEED;
Jeff Brown1a84fd12011-06-02 01:26:32 -07001317 try {
Jeff Brownd4935962012-09-25 13:27:20 -07001318 speed = Settings.System.getIntForUser(mContext.getContentResolver(),
1319 Settings.System.POINTER_SPEED, UserHandle.USER_CURRENT);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001320 } catch (SettingNotFoundException snfe) {
1321 }
1322 return speed;
1323 }
1324
Jeff Browndaf4a122011-08-26 17:14:14 -07001325 public void updateShowTouchesFromSettings() {
1326 int setting = getShowTouchesSetting(0);
Jeff Brown4532e612012-04-05 14:27:12 -07001327 nativeSetShowTouches(mPtr, setting != 0);
Jeff Browndaf4a122011-08-26 17:14:14 -07001328 }
1329
1330 private void registerShowTouchesSettingObserver() {
1331 mContext.getContentResolver().registerContentObserver(
1332 Settings.System.getUriFor(Settings.System.SHOW_TOUCHES), true,
Jeff Brown4532e612012-04-05 14:27:12 -07001333 new ContentObserver(mHandler) {
Jeff Browndaf4a122011-08-26 17:14:14 -07001334 @Override
1335 public void onChange(boolean selfChange) {
1336 updateShowTouchesFromSettings();
1337 }
Jeff Brownd4935962012-09-25 13:27:20 -07001338 }, UserHandle.USER_ALL);
Jeff Browndaf4a122011-08-26 17:14:14 -07001339 }
1340
1341 private int getShowTouchesSetting(int defaultValue) {
1342 int result = defaultValue;
1343 try {
Jeff Brownd4935962012-09-25 13:27:20 -07001344 result = Settings.System.getIntForUser(mContext.getContentResolver(),
1345 Settings.System.SHOW_TOUCHES, UserHandle.USER_CURRENT);
Jeff Browndaf4a122011-08-26 17:14:14 -07001346 } catch (SettingNotFoundException snfe) {
1347 }
1348 return result;
1349 }
1350
Jeff Browna47425a2012-04-13 04:09:27 -07001351 // Binder call
1352 @Override
1353 public void vibrate(int deviceId, long[] pattern, int repeat, IBinder token) {
1354 if (repeat >= pattern.length) {
1355 throw new ArrayIndexOutOfBoundsException();
1356 }
1357
1358 VibratorToken v;
1359 synchronized (mVibratorLock) {
1360 v = mVibratorTokens.get(token);
1361 if (v == null) {
1362 v = new VibratorToken(deviceId, token, mNextVibratorTokenValue++);
1363 try {
1364 token.linkToDeath(v, 0);
1365 } catch (RemoteException ex) {
1366 // give up
1367 throw new RuntimeException(ex);
1368 }
1369 mVibratorTokens.put(token, v);
1370 }
1371 }
1372
1373 synchronized (v) {
1374 v.mVibrating = true;
1375 nativeVibrate(mPtr, deviceId, pattern, repeat, v.mTokenValue);
1376 }
1377 }
1378
1379 // Binder call
1380 @Override
1381 public void cancelVibrate(int deviceId, IBinder token) {
1382 VibratorToken v;
1383 synchronized (mVibratorLock) {
1384 v = mVibratorTokens.get(token);
1385 if (v == null || v.mDeviceId != deviceId) {
1386 return; // nothing to cancel
1387 }
1388 }
1389
1390 cancelVibrateIfNeeded(v);
1391 }
1392
1393 void onVibratorTokenDied(VibratorToken v) {
1394 synchronized (mVibratorLock) {
1395 mVibratorTokens.remove(v.mToken);
1396 }
1397
1398 cancelVibrateIfNeeded(v);
1399 }
1400
1401 private void cancelVibrateIfNeeded(VibratorToken v) {
1402 synchronized (v) {
1403 if (v.mVibrating) {
1404 nativeCancelVibrate(mPtr, v.mDeviceId, v.mTokenValue);
1405 v.mVibrating = false;
1406 }
1407 }
1408 }
1409
Jeff Brown4532e612012-04-05 14:27:12 -07001410 @Override
1411 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Browna3bc5652012-04-17 11:42:25 -07001412 if (mContext.checkCallingOrSelfPermission(Manifest.permission.DUMP)
Jeff Brown4532e612012-04-05 14:27:12 -07001413 != PackageManager.PERMISSION_GRANTED) {
1414 pw.println("Permission Denial: can't dump InputManager from from pid="
1415 + Binder.getCallingPid()
1416 + ", uid=" + Binder.getCallingUid());
1417 return;
1418 }
1419
1420 pw.println("INPUT MANAGER (dumpsys input)\n");
1421 String dumpStr = nativeDump(mPtr);
Jeff Browne33348b2010-07-15 23:54:05 -07001422 if (dumpStr != null) {
1423 pw.println(dumpStr);
1424 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001425 }
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001426
Jeff Brownac143512012-04-05 18:57:33 -07001427 private boolean checkCallingPermission(String permission, String func) {
1428 // Quick check: if the calling permission is me, it's all okay.
1429 if (Binder.getCallingPid() == Process.myPid()) {
1430 return true;
1431 }
1432
1433 if (mContext.checkCallingPermission(permission) == PackageManager.PERMISSION_GRANTED) {
1434 return true;
1435 }
1436 String msg = "Permission Denial: " + func + " from pid="
1437 + Binder.getCallingPid()
1438 + ", uid=" + Binder.getCallingUid()
1439 + " requires " + permission;
1440 Slog.w(TAG, msg);
1441 return false;
1442 }
1443
Jeff Brown4532e612012-04-05 14:27:12 -07001444 // Called by the heartbeat to ensure locks are not held indefinitely (for deadlock detection).
Craig Mautner2f39e9f2012-09-21 11:39:54 -07001445 @Override
Jeff Brown89ef0722011-08-10 16:25:21 -07001446 public void monitor() {
1447 synchronized (mInputFilterLock) { }
Jeff Brown4532e612012-04-05 14:27:12 -07001448 nativeMonitor(mPtr);
Jeff Brown89ef0722011-08-10 16:25:21 -07001449 }
1450
Jeff Brown4532e612012-04-05 14:27:12 -07001451 // Native callback.
1452 private void notifyConfigurationChanged(long whenNanos) {
Jeff Browna9d131c2012-09-20 16:48:17 -07001453 mWindowManagerCallbacks.notifyConfigurationChanged();
Jeff Brown4532e612012-04-05 14:27:12 -07001454 }
1455
1456 // Native callback.
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001457 private void notifyInputDevicesChanged(InputDevice[] inputDevices) {
1458 synchronized (mInputDevicesLock) {
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001459 if (!mInputDevicesChangedPending) {
1460 mInputDevicesChangedPending = true;
Jeff Browncf39bdf2012-05-18 14:41:19 -07001461 mHandler.obtainMessage(MSG_DELIVER_INPUT_DEVICES_CHANGED,
1462 mInputDevices).sendToTarget();
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001463 }
Jeff Browncf39bdf2012-05-18 14:41:19 -07001464
1465 mInputDevices = inputDevices;
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001466 }
1467 }
1468
1469 // Native callback.
Jeff Brownbcc046a2012-09-27 20:46:43 -07001470 private void notifySwitch(long whenNanos, int switchValues, int switchMask) {
1471 if (DEBUG) {
1472 Slog.d(TAG, "notifySwitch: values=" + Integer.toHexString(switchValues)
1473 + ", mask=" + Integer.toHexString(switchMask));
1474 }
1475
Craig Mautner2f39e9f2012-09-21 11:39:54 -07001476 if ((switchMask & SW_LID_BIT) != 0) {
1477 final boolean lidOpen = ((switchValues & SW_LID_BIT) == 0);
Jeff Brownbcc046a2012-09-27 20:46:43 -07001478 mWindowManagerCallbacks.notifyLidSwitchChanged(whenNanos, lidOpen);
Jeff Brown53384282012-08-20 20:16:01 -07001479 }
Craig Mautner2f39e9f2012-09-21 11:39:54 -07001480
Michael Wright3818c922014-09-02 13:59:07 -07001481 if ((switchMask & SW_CAMERA_LENS_COVER_BIT) != 0) {
Michael Wright9e10d252014-09-13 19:41:20 -07001482 final boolean lensCovered = ((switchValues & SW_CAMERA_LENS_COVER_BIT) != 0);
Michael Wright3818c922014-09-02 13:59:07 -07001483 mWindowManagerCallbacks.notifyCameraLensCoverSwitchChanged(whenNanos, lensCovered);
1484 }
1485
Craig Mautner2f39e9f2012-09-21 11:39:54 -07001486 if (mUseDevInputEventForAudioJack && (switchMask & SW_JACK_BITS) != 0) {
1487 mWiredAccessoryCallbacks.notifyWiredAccessoryChanged(whenNanos, switchValues,
1488 switchMask);
1489 }
Michael Wright39e5e942015-08-19 22:52:47 +01001490
1491 if ((switchMask & SW_TABLET_MODE) != 0) {
1492 SomeArgs args = SomeArgs.obtain();
1493 args.argi1 = (int) (whenNanos & 0xFFFFFFFF);
1494 args.argi2 = (int) (whenNanos >> 32);
1495 args.arg1 = Boolean.valueOf((switchValues & SW_TABLET_MODE_BIT) != 0);
1496 mHandler.obtainMessage(MSG_DELIVER_TABLET_MODE_CHANGED,
1497 args).sendToTarget();
1498 }
Jeff Brown4532e612012-04-05 14:27:12 -07001499 }
1500
1501 // Native callback.
1502 private void notifyInputChannelBroken(InputWindowHandle inputWindowHandle) {
Jeff Browna9d131c2012-09-20 16:48:17 -07001503 mWindowManagerCallbacks.notifyInputChannelBroken(inputWindowHandle);
Jeff Brown4532e612012-04-05 14:27:12 -07001504 }
1505
1506 // Native callback.
1507 private long notifyANR(InputApplicationHandle inputApplicationHandle,
Jeff Brownbd181bb2013-09-10 16:44:24 -07001508 InputWindowHandle inputWindowHandle, String reason) {
1509 return mWindowManagerCallbacks.notifyANR(
1510 inputApplicationHandle, inputWindowHandle, reason);
Jeff Brown4532e612012-04-05 14:27:12 -07001511 }
1512
1513 // Native callback.
1514 final boolean filterInputEvent(InputEvent event, int policyFlags) {
1515 synchronized (mInputFilterLock) {
1516 if (mInputFilter != null) {
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07001517 try {
1518 mInputFilter.filterInputEvent(event, policyFlags);
1519 } catch (RemoteException e) {
1520 /* ignore */
1521 }
Jeff Brown4532e612012-04-05 14:27:12 -07001522 return false;
1523 }
1524 }
1525 event.recycle();
1526 return true;
1527 }
1528
1529 // Native callback.
Jeff Brown037c33e2014-04-09 00:31:55 -07001530 private int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags) {
1531 return mWindowManagerCallbacks.interceptKeyBeforeQueueing(event, policyFlags);
Jeff Brown4532e612012-04-05 14:27:12 -07001532 }
1533
1534 // Native callback.
Michael Wright70af00a2014-09-03 19:30:20 -07001535 private int interceptMotionBeforeQueueingNonInteractive(long whenNanos, int policyFlags) {
1536 return mWindowManagerCallbacks.interceptMotionBeforeQueueingNonInteractive(
Jeff Brown26875502014-01-30 21:47:47 -08001537 whenNanos, policyFlags);
Jeff Brown4532e612012-04-05 14:27:12 -07001538 }
1539
1540 // Native callback.
1541 private long interceptKeyBeforeDispatching(InputWindowHandle focus,
1542 KeyEvent event, int policyFlags) {
Jeff Browna9d131c2012-09-20 16:48:17 -07001543 return mWindowManagerCallbacks.interceptKeyBeforeDispatching(focus, event, policyFlags);
Jeff Brown4532e612012-04-05 14:27:12 -07001544 }
1545
1546 // Native callback.
1547 private KeyEvent dispatchUnhandledKey(InputWindowHandle focus,
1548 KeyEvent event, int policyFlags) {
Jeff Browna9d131c2012-09-20 16:48:17 -07001549 return mWindowManagerCallbacks.dispatchUnhandledKey(focus, event, policyFlags);
Jeff Brown4532e612012-04-05 14:27:12 -07001550 }
1551
1552 // Native callback.
1553 private boolean checkInjectEventsPermission(int injectorPid, int injectorUid) {
1554 return mContext.checkPermission(android.Manifest.permission.INJECT_EVENTS,
1555 injectorPid, injectorUid) == PackageManager.PERMISSION_GRANTED;
1556 }
1557
1558 // Native callback.
1559 private int getVirtualKeyQuietTimeMillis() {
1560 return mContext.getResources().getInteger(
1561 com.android.internal.R.integer.config_virtualKeyQuietTimeMillis);
1562 }
1563
1564 // Native callback.
1565 private String[] getExcludedDeviceNames() {
1566 ArrayList<String> names = new ArrayList<String>();
1567
1568 // Read partner-provided list of excluded input devices
1569 XmlPullParser parser = null;
1570 // Environment.getRootDirectory() is a fancy way of saying ANDROID_ROOT or "/system".
1571 File confFile = new File(Environment.getRootDirectory(), EXCLUDED_DEVICES_PATH);
1572 FileReader confreader = null;
1573 try {
1574 confreader = new FileReader(confFile);
1575 parser = Xml.newPullParser();
1576 parser.setInput(confreader);
1577 XmlUtils.beginDocument(parser, "devices");
1578
1579 while (true) {
1580 XmlUtils.nextElement(parser);
1581 if (!"device".equals(parser.getName())) {
1582 break;
1583 }
1584 String name = parser.getAttributeValue(null, "name");
1585 if (name != null) {
1586 names.add(name);
1587 }
1588 }
1589 } catch (FileNotFoundException e) {
1590 // It's ok if the file does not exist.
1591 } catch (Exception e) {
1592 Slog.e(TAG, "Exception while parsing '" + confFile.getAbsolutePath() + "'", e);
1593 } finally {
1594 try { if (confreader != null) confreader.close(); } catch (IOException e) { }
1595 }
1596
1597 return names.toArray(new String[names.size()]);
1598 }
1599
1600 // Native callback.
1601 private int getKeyRepeatTimeout() {
1602 return ViewConfiguration.getKeyRepeatTimeout();
1603 }
1604
1605 // Native callback.
1606 private int getKeyRepeatDelay() {
1607 return ViewConfiguration.getKeyRepeatDelay();
1608 }
1609
1610 // Native callback.
1611 private int getHoverTapTimeout() {
1612 return ViewConfiguration.getHoverTapTimeout();
1613 }
1614
1615 // Native callback.
1616 private int getHoverTapSlop() {
1617 return ViewConfiguration.getHoverTapSlop();
1618 }
1619
1620 // Native callback.
1621 private int getDoubleTapTimeout() {
1622 return ViewConfiguration.getDoubleTapTimeout();
1623 }
1624
1625 // Native callback.
1626 private int getLongPressTimeout() {
1627 return ViewConfiguration.getLongPressTimeout();
1628 }
1629
1630 // Native callback.
1631 private int getPointerLayer() {
Jeff Browna9d131c2012-09-20 16:48:17 -07001632 return mWindowManagerCallbacks.getPointerLayer();
Jeff Brown4532e612012-04-05 14:27:12 -07001633 }
1634
1635 // Native callback.
1636 private PointerIcon getPointerIcon() {
1637 return PointerIcon.getDefaultIcon(mContext);
1638 }
1639
Jeff Brown6ec6f792012-04-17 16:52:41 -07001640 // Native callback.
RoboErikfb290df2013-12-16 11:27:55 -08001641 private String[] getKeyboardLayoutOverlay(InputDeviceIdentifier identifier) {
Jeff Brown6ec6f792012-04-17 16:52:41 -07001642 if (!mSystemReady) {
1643 return null;
1644 }
1645
RoboErikfb290df2013-12-16 11:27:55 -08001646 String keyboardLayoutDescriptor = getCurrentKeyboardLayoutForInputDevice(identifier);
Jeff Brown6ec6f792012-04-17 16:52:41 -07001647 if (keyboardLayoutDescriptor == null) {
1648 return null;
1649 }
1650
1651 final String[] result = new String[2];
1652 visitKeyboardLayout(keyboardLayoutDescriptor, new KeyboardLayoutVisitor() {
1653 @Override
Michael Wright8ebac232014-09-18 18:29:49 -07001654 public void visitKeyboardLayout(Resources resources, String descriptor, String label,
1655 String collection, int keyboardLayoutResId, int priority) {
Jeff Brown6ec6f792012-04-17 16:52:41 -07001656 try {
1657 result[0] = descriptor;
1658 result[1] = Streams.readFully(new InputStreamReader(
Jeff Brown2f095762012-05-10 21:29:33 -07001659 resources.openRawResource(keyboardLayoutResId)));
Jeff Brown6ec6f792012-04-17 16:52:41 -07001660 } catch (IOException ex) {
1661 } catch (NotFoundException ex) {
1662 }
1663 }
1664 });
1665 if (result[0] == null) {
1666 Log.w(TAG, "Could not get keyboard layout with descriptor '"
1667 + keyboardLayoutDescriptor + "'.");
1668 return null;
1669 }
1670 return result;
1671 }
1672
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001673 // Native callback.
1674 private String getDeviceAlias(String uniqueId) {
Matthew Xie96313142012-06-29 16:57:31 -07001675 if (BluetoothAdapter.checkBluetoothAddress(uniqueId)) {
1676 // TODO(BT) mBluetoothService.getRemoteAlias(uniqueId)
1677 return null;
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001678 }
1679 return null;
1680 }
1681
Jeff Brown4532e612012-04-05 14:27:12 -07001682 /**
1683 * Callback interface implemented by the Window Manager.
1684 */
Jeff Browna9d131c2012-09-20 16:48:17 -07001685 public interface WindowManagerCallbacks {
Jeff Brown4532e612012-04-05 14:27:12 -07001686 public void notifyConfigurationChanged();
1687
1688 public void notifyLidSwitchChanged(long whenNanos, boolean lidOpen);
1689
Michael Wright3818c922014-09-02 13:59:07 -07001690 public void notifyCameraLensCoverSwitchChanged(long whenNanos, boolean lensCovered);
1691
Jeff Brown4532e612012-04-05 14:27:12 -07001692 public void notifyInputChannelBroken(InputWindowHandle inputWindowHandle);
1693
1694 public long notifyANR(InputApplicationHandle inputApplicationHandle,
Jeff Brownbd181bb2013-09-10 16:44:24 -07001695 InputWindowHandle inputWindowHandle, String reason);
Jeff Brown4532e612012-04-05 14:27:12 -07001696
Jeff Brown037c33e2014-04-09 00:31:55 -07001697 public int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags);
Jeff Brown4532e612012-04-05 14:27:12 -07001698
Michael Wright70af00a2014-09-03 19:30:20 -07001699 public int interceptMotionBeforeQueueingNonInteractive(long whenNanos, int policyFlags);
Jeff Brown4532e612012-04-05 14:27:12 -07001700
1701 public long interceptKeyBeforeDispatching(InputWindowHandle focus,
1702 KeyEvent event, int policyFlags);
1703
1704 public KeyEvent dispatchUnhandledKey(InputWindowHandle focus,
1705 KeyEvent event, int policyFlags);
1706
1707 public int getPointerLayer();
1708 }
1709
1710 /**
Craig Mautner2f39e9f2012-09-21 11:39:54 -07001711 * Callback interface implemented by WiredAccessoryObserver.
1712 */
1713 public interface WiredAccessoryCallbacks {
1714 public void notifyWiredAccessoryChanged(long whenNanos, int switchValues, int switchMask);
Eric Laurent4a5eeb92014-05-06 10:49:04 -07001715 public void systemReady();
Craig Mautner2f39e9f2012-09-21 11:39:54 -07001716 }
1717
1718 /**
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001719 * Private handler for the input manager.
1720 */
1721 private final class InputManagerHandler extends Handler {
Jeff Browna9d131c2012-09-20 16:48:17 -07001722 public InputManagerHandler(Looper looper) {
1723 super(looper, null, true /*async*/);
Jeff Browna2910d02012-08-25 12:29:46 -07001724 }
1725
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001726 @Override
1727 public void handleMessage(Message msg) {
1728 switch (msg.what) {
1729 case MSG_DELIVER_INPUT_DEVICES_CHANGED:
Jeff Browncf39bdf2012-05-18 14:41:19 -07001730 deliverInputDevicesChanged((InputDevice[])msg.obj);
1731 break;
1732 case MSG_SWITCH_KEYBOARD_LAYOUT:
1733 handleSwitchKeyboardLayout(msg.arg1, msg.arg2);
1734 break;
1735 case MSG_RELOAD_KEYBOARD_LAYOUTS:
1736 reloadKeyboardLayouts();
1737 break;
1738 case MSG_UPDATE_KEYBOARD_LAYOUTS:
1739 updateKeyboardLayouts();
1740 break;
1741 case MSG_RELOAD_DEVICE_ALIASES:
1742 reloadDeviceAliases();
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001743 break;
Michael Wright39e5e942015-08-19 22:52:47 +01001744 case MSG_DELIVER_TABLET_MODE_CHANGED:
1745 SomeArgs args = (SomeArgs) msg.obj;
1746 long whenNanos = (args.argi1 & 0xFFFFFFFFl) | ((long) args.argi2 << 32);
1747 boolean inTabletMode = (boolean) args.arg1;
1748 deliverTabletModeChanged(whenNanos, inTabletMode);
1749 break;
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001750 }
1751 }
1752 }
1753
1754 /**
Jeff Brown4532e612012-04-05 14:27:12 -07001755 * Hosting interface for input filters to call back into the input manager.
1756 */
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07001757 private final class InputFilterHost extends IInputFilterHost.Stub {
Jeff Brown0029c662011-03-30 02:25:18 -07001758 private boolean mDisconnected;
1759
1760 public void disconnectLocked() {
1761 mDisconnected = true;
1762 }
1763
Craig Mautner2f39e9f2012-09-21 11:39:54 -07001764 @Override
Jeff Brown0029c662011-03-30 02:25:18 -07001765 public void sendInputEvent(InputEvent event, int policyFlags) {
1766 if (event == null) {
1767 throw new IllegalArgumentException("event must not be null");
1768 }
1769
1770 synchronized (mInputFilterLock) {
1771 if (!mDisconnected) {
Jeff Brownca9bc702014-02-11 14:32:56 -08001772 nativeInjectInputEvent(mPtr, event, Display.DEFAULT_DISPLAY, 0, 0,
Jeff Brownac143512012-04-05 18:57:33 -07001773 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC, 0,
Jeff Brown0029c662011-03-30 02:25:18 -07001774 policyFlags | WindowManagerPolicy.FLAG_FILTERED);
1775 }
1776 }
1777 }
1778 }
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001779
1780 private static final class KeyboardLayoutDescriptor {
1781 public String packageName;
1782 public String receiverName;
1783 public String keyboardLayoutName;
1784
1785 public static String format(String packageName,
1786 String receiverName, String keyboardName) {
1787 return packageName + "/" + receiverName + "/" + keyboardName;
1788 }
1789
1790 public static KeyboardLayoutDescriptor parse(String descriptor) {
1791 int pos = descriptor.indexOf('/');
1792 if (pos < 0 || pos + 1 == descriptor.length()) {
1793 return null;
1794 }
1795 int pos2 = descriptor.indexOf('/', pos + 1);
1796 if (pos2 < pos + 2 || pos2 + 1 == descriptor.length()) {
1797 return null;
1798 }
1799
1800 KeyboardLayoutDescriptor result = new KeyboardLayoutDescriptor();
1801 result.packageName = descriptor.substring(0, pos);
1802 result.receiverName = descriptor.substring(pos + 1, pos2);
1803 result.keyboardLayoutName = descriptor.substring(pos2 + 1);
1804 return result;
1805 }
1806 }
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001807
Jeff Brown6ec6f792012-04-17 16:52:41 -07001808 private interface KeyboardLayoutVisitor {
Michael Wright8ebac232014-09-18 18:29:49 -07001809 void visitKeyboardLayout(Resources resources, String descriptor, String label,
1810 String collection, int keyboardLayoutResId, int priority);
Jeff Brown6ec6f792012-04-17 16:52:41 -07001811 }
1812
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001813 private final class InputDevicesChangedListenerRecord implements DeathRecipient {
1814 private final int mPid;
1815 private final IInputDevicesChangedListener mListener;
1816
1817 public InputDevicesChangedListenerRecord(int pid, IInputDevicesChangedListener listener) {
1818 mPid = pid;
1819 mListener = listener;
1820 }
1821
1822 @Override
1823 public void binderDied() {
1824 if (DEBUG) {
1825 Slog.d(TAG, "Input devices changed listener for pid " + mPid + " died.");
1826 }
1827 onInputDevicesChangedListenerDied(mPid);
1828 }
1829
1830 public void notifyInputDevicesChanged(int[] info) {
1831 try {
1832 mListener.onInputDevicesChanged(info);
1833 } catch (RemoteException ex) {
1834 Slog.w(TAG, "Failed to notify process "
1835 + mPid + " that input devices changed, assuming it died.", ex);
1836 binderDied();
1837 }
1838 }
1839 }
Jeff Browna47425a2012-04-13 04:09:27 -07001840
Michael Wright39e5e942015-08-19 22:52:47 +01001841 private final class TabletModeChangedListenerRecord implements DeathRecipient {
1842 private final int mPid;
1843 private final ITabletModeChangedListener mListener;
1844
1845 public TabletModeChangedListenerRecord(int pid, ITabletModeChangedListener listener) {
1846 mPid = pid;
1847 mListener = listener;
1848 }
1849
1850 @Override
1851 public void binderDied() {
1852 if (DEBUG) {
1853 Slog.d(TAG, "Tablet mode changed listener for pid " + mPid + " died.");
1854 }
1855 onTabletModeChangedListenerDied(mPid);
1856 }
1857
1858 public void notifyTabletModeChanged(long whenNanos, boolean inTabletMode) {
1859 try {
1860 mListener.onTabletModeChanged(whenNanos, inTabletMode);
1861 } catch (RemoteException ex) {
1862 Slog.w(TAG, "Failed to notify process " + mPid +
1863 " that tablet mode changed, assuming it died.", ex);
1864 binderDied();
1865 }
1866 }
1867 }
1868
Jeff Browna47425a2012-04-13 04:09:27 -07001869 private final class VibratorToken implements DeathRecipient {
1870 public final int mDeviceId;
1871 public final IBinder mToken;
1872 public final int mTokenValue;
1873
1874 public boolean mVibrating;
1875
1876 public VibratorToken(int deviceId, IBinder token, int tokenValue) {
1877 mDeviceId = deviceId;
1878 mToken = token;
1879 mTokenValue = tokenValue;
1880 }
1881
1882 @Override
1883 public void binderDied() {
1884 if (DEBUG) {
1885 Slog.d(TAG, "Vibrator token died.");
1886 }
1887 onVibratorTokenDied(this);
1888 }
1889 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001890
1891 private final class LocalService extends InputManagerInternal {
1892 @Override
1893 public void setDisplayViewports(
1894 DisplayViewport defaultViewport, DisplayViewport externalTouchViewport) {
1895 setDisplayViewportsInternal(defaultViewport, externalTouchViewport);
1896 }
Jeff Brownca9bc702014-02-11 14:32:56 -08001897
1898 @Override
1899 public boolean injectInputEvent(InputEvent event, int displayId, int mode) {
1900 return injectInputEventInternal(event, displayId, mode);
1901 }
Jeff Brown037c33e2014-04-09 00:31:55 -07001902
1903 @Override
1904 public void setInteractive(boolean interactive) {
1905 nativeSetInteractive(mPtr, interactive);
1906 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001907 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001908}