blob: ab2b825bf95c3a2ae80cf4d64341f125fa856ee3 [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);
Jun Mukai1db53972015-09-11 18:08:31 -0700209 private static native void nativeSetPointerIconShape(long ptr, int iconId);
Jun Mukai19a56012015-11-24 11:25:52 -0800210 private static native void nativeReloadPointerIcons(long ptr);
Jeff Brown4532e612012-04-05 14:27:12 -0700211
Jeff Brownac143512012-04-05 18:57:33 -0700212 // Input event injection constants defined in InputDispatcher.h.
213 private static final int INPUT_EVENT_INJECTION_SUCCEEDED = 0;
214 private static final int INPUT_EVENT_INJECTION_PERMISSION_DENIED = 1;
215 private static final int INPUT_EVENT_INJECTION_FAILED = 2;
216 private static final int INPUT_EVENT_INJECTION_TIMED_OUT = 3;
217
218 // Maximum number of milliseconds to wait for input event injection.
219 private static final int INJECTION_TIMEOUT_MILLIS = 30 * 1000;
220
Jeff Brown6d0fec22010-07-23 21:28:06 -0700221 // Key states (may be returned by queries about the current state of a
222 // particular key code, scan code or switch).
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700223
Jeff Brown6d0fec22010-07-23 21:28:06 -0700224 /** The key state is unknown or the requested key itself is not supported. */
225 public static final int KEY_STATE_UNKNOWN = -1;
226
227 /** The key is up. /*/
228 public static final int KEY_STATE_UP = 0;
229
230 /** The key is down. */
231 public static final int KEY_STATE_DOWN = 1;
232
233 /** The key is down but is a virtual key press that is being emulated by the system. */
234 public static final int KEY_STATE_VIRTUAL = 2;
235
Jeff Brownc458ce92012-04-30 14:58:40 -0700236 /** Scan code: Mouse / trackball button. */
237 public static final int BTN_MOUSE = 0x110;
238
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700239 // Switch code values must match bionic/libc/kernel/common/linux/input.h
Jeff Brownc458ce92012-04-30 14:58:40 -0700240 /** Switch code: Lid switch. When set, lid is shut. */
241 public static final int SW_LID = 0x00;
242
Michael Wright39e5e942015-08-19 22:52:47 +0100243 /** Switch code: Tablet mode switch.
244 * When set, the device is in tablet mode (i.e. no keyboard is connected).
245 */
246 public static final int SW_TABLET_MODE = 0x01;
247
Jeff Brownc458ce92012-04-30 14:58:40 -0700248 /** Switch code: Keypad slide. When set, keyboard is exposed. */
249 public static final int SW_KEYPAD_SLIDE = 0x0a;
250
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700251 /** Switch code: Headphone. When set, headphone is inserted. */
252 public static final int SW_HEADPHONE_INSERT = 0x02;
253
254 /** Switch code: Microphone. When set, microphone is inserted. */
255 public static final int SW_MICROPHONE_INSERT = 0x04;
256
Jon Eklund43cc8bb2014-07-28 16:07:24 -0500257 /** Switch code: Line out. When set, Line out (hi-Z) is inserted. */
258 public static final int SW_LINEOUT_INSERT = 0x06;
259
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700260 /** Switch code: Headphone/Microphone Jack. When set, something is inserted. */
261 public static final int SW_JACK_PHYSICAL_INSERT = 0x07;
262
Michael Wright3818c922014-09-02 13:59:07 -0700263 /** Switch code: Camera lens cover. When set the lens is covered. */
264 public static final int SW_CAMERA_LENS_COVER = 0x09;
265
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700266 public static final int SW_LID_BIT = 1 << SW_LID;
Michael Wright39e5e942015-08-19 22:52:47 +0100267 public static final int SW_TABLET_MODE_BIT = 1 << SW_TABLET_MODE;
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700268 public static final int SW_KEYPAD_SLIDE_BIT = 1 << SW_KEYPAD_SLIDE;
269 public static final int SW_HEADPHONE_INSERT_BIT = 1 << SW_HEADPHONE_INSERT;
270 public static final int SW_MICROPHONE_INSERT_BIT = 1 << SW_MICROPHONE_INSERT;
Jon Eklund43cc8bb2014-07-28 16:07:24 -0500271 public static final int SW_LINEOUT_INSERT_BIT = 1 << SW_LINEOUT_INSERT;
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700272 public static final int SW_JACK_PHYSICAL_INSERT_BIT = 1 << SW_JACK_PHYSICAL_INSERT;
273 public static final int SW_JACK_BITS =
Jon Eklund43cc8bb2014-07-28 16:07:24 -0500274 SW_HEADPHONE_INSERT_BIT | SW_MICROPHONE_INSERT_BIT | SW_JACK_PHYSICAL_INSERT_BIT | SW_LINEOUT_INSERT_BIT;
Michael Wright3818c922014-09-02 13:59:07 -0700275 public static final int SW_CAMERA_LENS_COVER_BIT = 1 << SW_CAMERA_LENS_COVER;
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700276
277 /** Whether to use the dev/input/event or uevent subsystem for the audio jack. */
278 final boolean mUseDevInputEventForAudioJack;
279
Jeff Brown4ccb8232014-01-16 22:16:42 -0800280 public InputManagerService(Context context) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700281 this.mContext = context;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800282 this.mHandler = new InputManagerHandler(DisplayThread.get().getLooper());
Jeff Brown05dc66a2011-03-02 14:41:58 -0800283
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700284 mUseDevInputEventForAudioJack =
285 context.getResources().getBoolean(R.bool.config_useDevInputEventForAudioJack);
286 Slog.i(TAG, "Initializing input manager, mUseDevInputEventForAudioJack="
287 + mUseDevInputEventForAudioJack);
Jeff Brown4532e612012-04-05 14:27:12 -0700288 mPtr = nativeInit(this, mContext, mHandler.getLooper().getQueue());
Jeff Brown4ccb8232014-01-16 22:16:42 -0800289
290 LocalServices.addService(InputManagerInternal.class, new LocalService());
Jeff Brown46b9ac02010-04-22 18:58:52 -0700291 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700292
Jeff Browna9d131c2012-09-20 16:48:17 -0700293 public void setWindowManagerCallbacks(WindowManagerCallbacks callbacks) {
294 mWindowManagerCallbacks = callbacks;
295 }
296
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700297 public void setWiredAccessoryCallbacks(WiredAccessoryCallbacks callbacks) {
298 mWiredAccessoryCallbacks = callbacks;
299 }
300
Jeff Brown46b9ac02010-04-22 18:58:52 -0700301 public void start() {
302 Slog.i(TAG, "Starting input manager");
Jeff Brown4532e612012-04-05 14:27:12 -0700303 nativeStart(mPtr);
304
305 // Add ourself to the Watchdog monitors.
306 Watchdog.getInstance().addMonitor(this);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700307
308 registerPointerSpeedSettingObserver();
Jeff Browndaf4a122011-08-26 17:14:14 -0700309 registerShowTouchesSettingObserver();
Jun Mukai19a56012015-11-24 11:25:52 -0800310 registerAccessibilityLargePointerSettingObserver();
Jeff Browndaf4a122011-08-26 17:14:14 -0700311
Jeff Brownd4935962012-09-25 13:27:20 -0700312 mContext.registerReceiver(new BroadcastReceiver() {
313 @Override
314 public void onReceive(Context context, Intent intent) {
315 updatePointerSpeedFromSettings();
316 updateShowTouchesFromSettings();
Jun Mukai19a56012015-11-24 11:25:52 -0800317 nativeReloadPointerIcons(mPtr);
Jeff Brownd4935962012-09-25 13:27:20 -0700318 }
319 }, new IntentFilter(Intent.ACTION_USER_SWITCHED), null, mHandler);
320
Jeff Brown1a84fd12011-06-02 01:26:32 -0700321 updatePointerSpeedFromSettings();
Jeff Browndaf4a122011-08-26 17:14:14 -0700322 updateShowTouchesFromSettings();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700323 }
Jeff Brown6ec6f792012-04-17 16:52:41 -0700324
Matthew Xie96313142012-06-29 16:57:31 -0700325 // TODO(BT) Pass in paramter for bluetooth system
Svetoslav Ganova0027152013-06-25 14:59:53 -0700326 public void systemRunning() {
Jeff Brown6ec6f792012-04-17 16:52:41 -0700327 if (DEBUG) {
328 Slog.d(TAG, "System ready.");
329 }
Jeff Browncf39bdf2012-05-18 14:41:19 -0700330 mNotificationManager = (NotificationManager)mContext.getSystemService(
331 Context.NOTIFICATION_SERVICE);
Jeff Brown6ec6f792012-04-17 16:52:41 -0700332 mSystemReady = true;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700333
334 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
335 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
336 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
Jeff Brown69b07162013-11-07 00:30:16 -0800337 filter.addAction(Intent.ACTION_PACKAGE_REPLACED);
Jeff Brown6ec6f792012-04-17 16:52:41 -0700338 filter.addDataScheme("package");
339 mContext.registerReceiver(new BroadcastReceiver() {
340 @Override
341 public void onReceive(Context context, Intent intent) {
Jeff Browncf39bdf2012-05-18 14:41:19 -0700342 updateKeyboardLayouts();
Jeff Brown6ec6f792012-04-17 16:52:41 -0700343 }
344 }, filter, null, mHandler);
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700345
346 filter = new IntentFilter(BluetoothDevice.ACTION_ALIAS_CHANGED);
347 mContext.registerReceiver(new BroadcastReceiver() {
348 @Override
349 public void onReceive(Context context, Intent intent) {
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700350 reloadDeviceAliases();
351 }
352 }, filter, null, mHandler);
353
Jeff Browncf39bdf2012-05-18 14:41:19 -0700354 mHandler.sendEmptyMessage(MSG_RELOAD_DEVICE_ALIASES);
355 mHandler.sendEmptyMessage(MSG_UPDATE_KEYBOARD_LAYOUTS);
Eric Laurent4a5eeb92014-05-06 10:49:04 -0700356
357 if (mWiredAccessoryCallbacks != null) {
358 mWiredAccessoryCallbacks.systemReady();
359 }
Jeff Brown6ec6f792012-04-17 16:52:41 -0700360 }
361
362 private void reloadKeyboardLayouts() {
Jeff Browncf39bdf2012-05-18 14:41:19 -0700363 if (DEBUG) {
364 Slog.d(TAG, "Reloading keyboard layouts.");
365 }
Jeff Brown6ec6f792012-04-17 16:52:41 -0700366 nativeReloadKeyboardLayouts(mPtr);
367 }
368
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700369 private void reloadDeviceAliases() {
Jeff Browncf39bdf2012-05-18 14:41:19 -0700370 if (DEBUG) {
371 Slog.d(TAG, "Reloading device names.");
372 }
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700373 nativeReloadDeviceAliases(mPtr);
374 }
375
Jeff Brown4ccb8232014-01-16 22:16:42 -0800376 private void setDisplayViewportsInternal(DisplayViewport defaultViewport,
Jeff Brownd728bf52012-09-08 18:05:28 -0700377 DisplayViewport externalTouchViewport) {
378 if (defaultViewport.valid) {
379 setDisplayViewport(false, defaultViewport);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700380 }
Jeff Brownd728bf52012-09-08 18:05:28 -0700381
382 if (externalTouchViewport.valid) {
383 setDisplayViewport(true, externalTouchViewport);
384 } else if (defaultViewport.valid) {
385 setDisplayViewport(true, defaultViewport);
Jeff Brownb6997262010-10-08 22:31:17 -0700386 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700387 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700388
Jeff Brownd728bf52012-09-08 18:05:28 -0700389 private void setDisplayViewport(boolean external, DisplayViewport viewport) {
390 nativeSetDisplayViewport(mPtr, external,
391 viewport.displayId, viewport.orientation,
392 viewport.logicalFrame.left, viewport.logicalFrame.top,
393 viewport.logicalFrame.right, viewport.logicalFrame.bottom,
394 viewport.physicalFrame.left, viewport.physicalFrame.top,
Jeff Brown83d616a2012-09-09 20:33:43 -0700395 viewport.physicalFrame.right, viewport.physicalFrame.bottom,
396 viewport.deviceWidth, viewport.deviceHeight);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700397 }
Jeff Brownac143512012-04-05 18:57:33 -0700398
Jeff Brown6d0fec22010-07-23 21:28:06 -0700399 /**
400 * Gets the current state of a key or button by key code.
401 * @param deviceId The input device id, or -1 to consult all devices.
402 * @param sourceMask The input sources to consult, or {@link InputDevice#SOURCE_ANY} to
403 * consider all input sources. An input device is consulted if at least one of its
404 * non-class input source bits matches the specified source mask.
405 * @param keyCode The key code to check.
406 * @return The key state.
407 */
408 public int getKeyCodeState(int deviceId, int sourceMask, int keyCode) {
Jeff Brown4532e612012-04-05 14:27:12 -0700409 return nativeGetKeyCodeState(mPtr, deviceId, sourceMask, keyCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700410 }
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700411
Jeff Brown6d0fec22010-07-23 21:28:06 -0700412 /**
413 * Gets the current state of a key or button by scan code.
414 * @param deviceId The input device id, or -1 to consult all devices.
415 * @param sourceMask The input sources to consult, or {@link InputDevice#SOURCE_ANY} to
416 * consider all input sources. An input device is consulted if at least one of its
417 * non-class input source bits matches the specified source mask.
418 * @param scanCode The scan code to check.
419 * @return The key state.
420 */
421 public int getScanCodeState(int deviceId, int sourceMask, int scanCode) {
Jeff Brown4532e612012-04-05 14:27:12 -0700422 return nativeGetScanCodeState(mPtr, deviceId, sourceMask, scanCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700423 }
RoboErikfb290df2013-12-16 11:27:55 -0800424
Jeff Brown6d0fec22010-07-23 21:28:06 -0700425 /**
426 * Gets the current state of a switch by switch code.
427 * @param deviceId The input device id, or -1 to consult all devices.
428 * @param sourceMask The input sources to consult, or {@link InputDevice#SOURCE_ANY} to
429 * consider all input sources. An input device is consulted if at least one of its
430 * non-class input source bits matches the specified source mask.
431 * @param switchCode The switch code to check.
432 * @return The switch state.
433 */
434 public int getSwitchState(int deviceId, int sourceMask, int switchCode) {
Jeff Brown4532e612012-04-05 14:27:12 -0700435 return nativeGetSwitchState(mPtr, deviceId, sourceMask, switchCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700436 }
437
Jeff Brown6d0fec22010-07-23 21:28:06 -0700438 /**
439 * Determines whether the specified key codes are supported by a particular device.
440 * @param deviceId The input device id, or -1 to consult all devices.
441 * @param sourceMask The input sources to consult, or {@link InputDevice#SOURCE_ANY} to
442 * consider all input sources. An input device is consulted if at least one of its
443 * non-class input source bits matches the specified source mask.
444 * @param keyCodes The array of key codes to check.
445 * @param keyExists An array at least as large as keyCodes whose entries will be set
446 * to true or false based on the presence or absence of support for the corresponding
447 * key codes.
448 * @return True if the lookup was successful, false otherwise.
449 */
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700450 @Override // Binder call
Jeff Brown6d0fec22010-07-23 21:28:06 -0700451 public boolean hasKeys(int deviceId, int sourceMask, int[] keyCodes, boolean[] keyExists) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700452 if (keyCodes == null) {
453 throw new IllegalArgumentException("keyCodes must not be null.");
454 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700455 if (keyExists == null || keyExists.length < keyCodes.length) {
456 throw new IllegalArgumentException("keyExists must not be null and must be at "
457 + "least as large as keyCodes.");
Jeff Brown46b9ac02010-04-22 18:58:52 -0700458 }
RoboErikfb290df2013-12-16 11:27:55 -0800459
Jeff Brown4532e612012-04-05 14:27:12 -0700460 return nativeHasKeys(mPtr, deviceId, sourceMask, keyCodes, keyExists);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700461 }
RoboErikfb290df2013-12-16 11:27:55 -0800462
Jeff Browna41ca772010-08-11 14:46:32 -0700463 /**
464 * Creates an input channel that will receive all input from the input dispatcher.
465 * @param inputChannelName The input channel name.
466 * @return The input channel.
467 */
468 public InputChannel monitorInput(String inputChannelName) {
469 if (inputChannelName == null) {
470 throw new IllegalArgumentException("inputChannelName must not be null.");
471 }
RoboErikfb290df2013-12-16 11:27:55 -0800472
Jeff Browna41ca772010-08-11 14:46:32 -0700473 InputChannel[] inputChannels = InputChannel.openInputChannelPair(inputChannelName);
Jeff Brown4532e612012-04-05 14:27:12 -0700474 nativeRegisterInputChannel(mPtr, inputChannels[0], null, true);
Jeff Browna41ca772010-08-11 14:46:32 -0700475 inputChannels[0].dispose(); // don't need to retain the Java object reference
476 return inputChannels[1];
477 }
478
479 /**
480 * Registers an input channel so that it can be used as an input event target.
481 * @param inputChannel The input channel to register.
Jeff Brown928e0542011-01-10 11:17:36 -0800482 * @param inputWindowHandle The handle of the input window associated with the
483 * input channel, or null if none.
Jeff Browna41ca772010-08-11 14:46:32 -0700484 */
Jeff Brown928e0542011-01-10 11:17:36 -0800485 public void registerInputChannel(InputChannel inputChannel,
486 InputWindowHandle inputWindowHandle) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700487 if (inputChannel == null) {
488 throw new IllegalArgumentException("inputChannel must not be null.");
489 }
RoboErikfb290df2013-12-16 11:27:55 -0800490
Jeff Brown4532e612012-04-05 14:27:12 -0700491 nativeRegisterInputChannel(mPtr, inputChannel, inputWindowHandle, false);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700492 }
RoboErikfb290df2013-12-16 11:27:55 -0800493
Jeff Browna41ca772010-08-11 14:46:32 -0700494 /**
495 * Unregisters an input channel.
496 * @param inputChannel The input channel to unregister.
497 */
Jeff Brown46b9ac02010-04-22 18:58:52 -0700498 public void unregisterInputChannel(InputChannel inputChannel) {
499 if (inputChannel == null) {
500 throw new IllegalArgumentException("inputChannel must not be null.");
501 }
RoboErikfb290df2013-12-16 11:27:55 -0800502
Jeff Brown4532e612012-04-05 14:27:12 -0700503 nativeUnregisterInputChannel(mPtr, inputChannel);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700504 }
Jeff Brown0029c662011-03-30 02:25:18 -0700505
506 /**
507 * Sets an input filter that will receive all input events before they are dispatched.
508 * The input filter may then reinterpret input events or inject new ones.
509 *
510 * To ensure consistency, the input dispatcher automatically drops all events
511 * in progress whenever an input filter is installed or uninstalled. After an input
512 * filter is uninstalled, it can no longer send input events unless it is reinstalled.
513 * Any events it attempts to send after it has been uninstalled will be dropped.
514 *
515 * @param filter The input filter, or null to remove the current filter.
516 */
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -0700517 public void setInputFilter(IInputFilter filter) {
Jeff Brown0029c662011-03-30 02:25:18 -0700518 synchronized (mInputFilterLock) {
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -0700519 final IInputFilter oldFilter = mInputFilter;
Jeff Brown0029c662011-03-30 02:25:18 -0700520 if (oldFilter == filter) {
521 return; // nothing to do
522 }
523
524 if (oldFilter != null) {
525 mInputFilter = null;
526 mInputFilterHost.disconnectLocked();
527 mInputFilterHost = null;
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -0700528 try {
529 oldFilter.uninstall();
530 } catch (RemoteException re) {
531 /* ignore */
532 }
Jeff Brown0029c662011-03-30 02:25:18 -0700533 }
534
535 if (filter != null) {
536 mInputFilter = filter;
537 mInputFilterHost = new InputFilterHost();
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -0700538 try {
539 filter.install(mInputFilterHost);
540 } catch (RemoteException re) {
541 /* ignore */
542 }
Jeff Brown0029c662011-03-30 02:25:18 -0700543 }
544
Jeff Brown4532e612012-04-05 14:27:12 -0700545 nativeSetInputFilterEnabled(mPtr, filter != null);
Jeff Brown0029c662011-03-30 02:25:18 -0700546 }
547 }
548
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700549 @Override // Binder call
Jeff Brownac143512012-04-05 18:57:33 -0700550 public boolean injectInputEvent(InputEvent event, int mode) {
Jeff Brownca9bc702014-02-11 14:32:56 -0800551 return injectInputEventInternal(event, Display.DEFAULT_DISPLAY, mode);
552 }
553
554 private boolean injectInputEventInternal(InputEvent event, int displayId, int mode) {
Jeff Brown7fbdc842010-06-17 20:52:56 -0700555 if (event == null) {
556 throw new IllegalArgumentException("event must not be null");
557 }
Jeff Brownac143512012-04-05 18:57:33 -0700558 if (mode != InputManager.INJECT_INPUT_EVENT_MODE_ASYNC
559 && mode != InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH
560 && mode != InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_RESULT) {
561 throw new IllegalArgumentException("mode is invalid");
Jeff Brown7fbdc842010-06-17 20:52:56 -0700562 }
Jeff Brown6ec402b2010-07-28 15:48:59 -0700563
Jeff Brownac143512012-04-05 18:57:33 -0700564 final int pid = Binder.getCallingPid();
565 final int uid = Binder.getCallingUid();
566 final long ident = Binder.clearCallingIdentity();
567 final int result;
568 try {
Jeff Brownca9bc702014-02-11 14:32:56 -0800569 result = nativeInjectInputEvent(mPtr, event, displayId, pid, uid, mode,
Jeff Brownac143512012-04-05 18:57:33 -0700570 INJECTION_TIMEOUT_MILLIS, WindowManagerPolicy.FLAG_DISABLE_KEY_REPEAT);
571 } finally {
572 Binder.restoreCallingIdentity(ident);
573 }
574 switch (result) {
575 case INPUT_EVENT_INJECTION_PERMISSION_DENIED:
576 Slog.w(TAG, "Input event injection from pid " + pid + " permission denied.");
577 throw new SecurityException(
578 "Injecting to another application requires INJECT_EVENTS permission");
579 case INPUT_EVENT_INJECTION_SUCCEEDED:
580 return true;
581 case INPUT_EVENT_INJECTION_TIMED_OUT:
582 Slog.w(TAG, "Input event injection from pid " + pid + " timed out.");
583 return false;
584 case INPUT_EVENT_INJECTION_FAILED:
585 default:
586 Slog.w(TAG, "Input event injection from pid " + pid + " failed.");
587 return false;
588 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700589 }
Jeff Brown0029c662011-03-30 02:25:18 -0700590
Jeff Brown8d608662010-08-30 03:02:23 -0700591 /**
592 * Gets information about the input device with the specified id.
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700593 * @param deviceId The device id.
Jeff Brown8d608662010-08-30 03:02:23 -0700594 * @return The input device or null if not found.
595 */
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700596 @Override // Binder call
Jeff Brown8d608662010-08-30 03:02:23 -0700597 public InputDevice getInputDevice(int deviceId) {
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700598 synchronized (mInputDevicesLock) {
599 final int count = mInputDevices.length;
600 for (int i = 0; i < count; i++) {
601 final InputDevice inputDevice = mInputDevices[i];
602 if (inputDevice.getId() == deviceId) {
603 return inputDevice;
604 }
605 }
606 }
607 return null;
Jeff Brown8d608662010-08-30 03:02:23 -0700608 }
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700609
Jeff Brown8d608662010-08-30 03:02:23 -0700610 /**
611 * Gets the ids of all input devices in the system.
612 * @return The input device ids.
613 */
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700614 @Override // Binder call
Jeff Brown8d608662010-08-30 03:02:23 -0700615 public int[] getInputDeviceIds() {
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700616 synchronized (mInputDevicesLock) {
617 final int count = mInputDevices.length;
618 int[] ids = new int[count];
619 for (int i = 0; i < count; i++) {
620 ids[i] = mInputDevices[i].getId();
621 }
622 return ids;
623 }
624 }
625
Jeff Browndaa37532012-05-01 15:54:03 -0700626 /**
627 * Gets all input devices in the system.
628 * @return The array of input devices.
629 */
630 public InputDevice[] getInputDevices() {
631 synchronized (mInputDevicesLock) {
632 return mInputDevices;
633 }
634 }
635
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700636 @Override // Binder call
637 public void registerInputDevicesChangedListener(IInputDevicesChangedListener listener) {
638 if (listener == null) {
639 throw new IllegalArgumentException("listener must not be null");
640 }
641
642 synchronized (mInputDevicesLock) {
643 int callingPid = Binder.getCallingPid();
644 if (mInputDevicesChangedListeners.get(callingPid) != null) {
645 throw new SecurityException("The calling process has already "
646 + "registered an InputDevicesChangedListener.");
647 }
648
649 InputDevicesChangedListenerRecord record =
650 new InputDevicesChangedListenerRecord(callingPid, listener);
651 try {
652 IBinder binder = listener.asBinder();
653 binder.linkToDeath(record, 0);
654 } catch (RemoteException ex) {
655 // give up
656 throw new RuntimeException(ex);
657 }
658
659 mInputDevicesChangedListeners.put(callingPid, record);
660 }
661 }
662
663 private void onInputDevicesChangedListenerDied(int pid) {
664 synchronized (mInputDevicesLock) {
665 mInputDevicesChangedListeners.remove(pid);
666 }
667 }
668
669 // Must be called on handler.
Jeff Browncf39bdf2012-05-18 14:41:19 -0700670 private void deliverInputDevicesChanged(InputDevice[] oldInputDevices) {
671 // Scan for changes.
672 int numFullKeyboardsAdded = 0;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700673 mTempInputDevicesChangedListenersToNotify.clear();
Jeff Browncf39bdf2012-05-18 14:41:19 -0700674 mTempFullKeyboards.clear();
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700675 final int numListeners;
676 final int[] deviceIdAndGeneration;
677 synchronized (mInputDevicesLock) {
678 if (!mInputDevicesChangedPending) {
679 return;
680 }
681 mInputDevicesChangedPending = false;
682
683 numListeners = mInputDevicesChangedListeners.size();
684 for (int i = 0; i < numListeners; i++) {
685 mTempInputDevicesChangedListenersToNotify.add(
686 mInputDevicesChangedListeners.valueAt(i));
687 }
688
689 final int numDevices = mInputDevices.length;
690 deviceIdAndGeneration = new int[numDevices * 2];
691 for (int i = 0; i < numDevices; i++) {
692 final InputDevice inputDevice = mInputDevices[i];
693 deviceIdAndGeneration[i * 2] = inputDevice.getId();
694 deviceIdAndGeneration[i * 2 + 1] = inputDevice.getGeneration();
Jeff Browncf39bdf2012-05-18 14:41:19 -0700695
Jeff Brown7e4ff4b2012-05-30 14:32:16 -0700696 if (!inputDevice.isVirtual() && inputDevice.isFullKeyboard()) {
Jeff Browncf39bdf2012-05-18 14:41:19 -0700697 if (!containsInputDeviceWithDescriptor(oldInputDevices,
698 inputDevice.getDescriptor())) {
699 mTempFullKeyboards.add(numFullKeyboardsAdded++, inputDevice);
700 } else {
701 mTempFullKeyboards.add(inputDevice);
702 }
703 }
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700704 }
705 }
706
Jeff Browncf39bdf2012-05-18 14:41:19 -0700707 // Notify listeners.
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700708 for (int i = 0; i < numListeners; i++) {
709 mTempInputDevicesChangedListenersToNotify.get(i).notifyInputDevicesChanged(
710 deviceIdAndGeneration);
711 }
Jeff Browncf39bdf2012-05-18 14:41:19 -0700712 mTempInputDevicesChangedListenersToNotify.clear();
713
714 // Check for missing keyboard layouts.
715 if (mNotificationManager != null) {
716 final int numFullKeyboards = mTempFullKeyboards.size();
717 boolean missingLayoutForExternalKeyboard = false;
718 boolean missingLayoutForExternalKeyboardAdded = false;
Michael Wrightc93fbd12014-09-22 20:07:59 -0700719 boolean multipleMissingLayoutsForExternalKeyboardsAdded = false;
720 InputDevice keyboardMissingLayout = null;
Jeff Browncf39bdf2012-05-18 14:41:19 -0700721 synchronized (mDataStore) {
722 for (int i = 0; i < numFullKeyboards; i++) {
723 final InputDevice inputDevice = mTempFullKeyboards.get(i);
Michael Wright86aaca62014-09-08 18:54:45 -0700724 final String layout =
725 getCurrentKeyboardLayoutForInputDevice(inputDevice.getIdentifier());
726 if (layout == null) {
Jeff Browncf39bdf2012-05-18 14:41:19 -0700727 missingLayoutForExternalKeyboard = true;
728 if (i < numFullKeyboardsAdded) {
729 missingLayoutForExternalKeyboardAdded = true;
Michael Wrightc93fbd12014-09-22 20:07:59 -0700730 if (keyboardMissingLayout == null) {
731 keyboardMissingLayout = inputDevice;
732 } else {
733 multipleMissingLayoutsForExternalKeyboardsAdded = true;
734 }
Jeff Browncf39bdf2012-05-18 14:41:19 -0700735 }
736 }
737 }
738 }
739 if (missingLayoutForExternalKeyboard) {
740 if (missingLayoutForExternalKeyboardAdded) {
Michael Wrightc93fbd12014-09-22 20:07:59 -0700741 if (multipleMissingLayoutsForExternalKeyboardsAdded) {
742 // We have more than one keyboard missing a layout, so drop the
743 // user at the generic input methods page so they can pick which
744 // one to set.
745 showMissingKeyboardLayoutNotification(null);
746 } else {
747 showMissingKeyboardLayoutNotification(keyboardMissingLayout);
748 }
Jeff Browncf39bdf2012-05-18 14:41:19 -0700749 }
750 } else if (mKeyboardLayoutNotificationShown) {
751 hideMissingKeyboardLayoutNotification();
752 }
753 }
754 mTempFullKeyboards.clear();
755 }
756
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800757 @Override // Binder call & native callback
Jason Gerecked5220742014-03-10 09:47:59 -0700758 public TouchCalibration getTouchCalibrationForInputDevice(String inputDeviceDescriptor,
759 int surfaceRotation) {
Jason Gerecked6396d62014-01-27 18:30:37 -0800760 if (inputDeviceDescriptor == null) {
761 throw new IllegalArgumentException("inputDeviceDescriptor must not be null");
762 }
763
764 synchronized (mDataStore) {
Jason Gerecked5220742014-03-10 09:47:59 -0700765 return mDataStore.getTouchCalibration(inputDeviceDescriptor, surfaceRotation);
Jason Gerecked6396d62014-01-27 18:30:37 -0800766 }
767 }
768
769 @Override // Binder call
Jason Gerecked5220742014-03-10 09:47:59 -0700770 public void setTouchCalibrationForInputDevice(String inputDeviceDescriptor, int surfaceRotation,
Jason Gerecked6396d62014-01-27 18:30:37 -0800771 TouchCalibration calibration) {
772 if (!checkCallingPermission(android.Manifest.permission.SET_INPUT_CALIBRATION,
773 "setTouchCalibrationForInputDevice()")) {
774 throw new SecurityException("Requires SET_INPUT_CALIBRATION permission");
775 }
776 if (inputDeviceDescriptor == null) {
777 throw new IllegalArgumentException("inputDeviceDescriptor must not be null");
778 }
779 if (calibration == null) {
780 throw new IllegalArgumentException("calibration must not be null");
781 }
Jason Gerecked5220742014-03-10 09:47:59 -0700782 if (surfaceRotation < Surface.ROTATION_0 || surfaceRotation > Surface.ROTATION_270) {
783 throw new IllegalArgumentException("surfaceRotation value out of bounds");
784 }
Jason Gerecked6396d62014-01-27 18:30:37 -0800785
786 synchronized (mDataStore) {
787 try {
Jason Gerecked5220742014-03-10 09:47:59 -0700788 if (mDataStore.setTouchCalibration(inputDeviceDescriptor, surfaceRotation,
789 calibration)) {
Jason Gerecke857aa7b2014-01-27 18:34:20 -0800790 nativeReloadCalibration(mPtr);
791 }
Jason Gerecked6396d62014-01-27 18:30:37 -0800792 } finally {
793 mDataStore.saveIfNeeded();
794 }
795 }
796 }
797
Michael Wright39e5e942015-08-19 22:52:47 +0100798 @Override // Binder call
Michael Wright9209c9c2015-09-03 17:57:01 +0100799 public int isInTabletMode() {
800 if (!checkCallingPermission(android.Manifest.permission.TABLET_MODE,
801 "isInTabletMode()")) {
802 throw new SecurityException("Requires TABLET_MODE permission");
803 }
804 return getSwitchState(-1, InputDevice.SOURCE_ANY, SW_TABLET_MODE);
805 }
806
807 @Override // Binder call
Michael Wright39e5e942015-08-19 22:52:47 +0100808 public void registerTabletModeChangedListener(ITabletModeChangedListener listener) {
Michael Wright9209c9c2015-09-03 17:57:01 +0100809 if (!checkCallingPermission(android.Manifest.permission.TABLET_MODE,
Michael Wright39e5e942015-08-19 22:52:47 +0100810 "registerTabletModeChangedListener()")) {
811 throw new SecurityException("Requires TABLET_MODE_LISTENER permission");
812 }
813 if (listener == null) {
814 throw new IllegalArgumentException("listener must not be null");
815 }
816
817 synchronized (mTabletModeLock) {
818 final int callingPid = Binder.getCallingPid();
819 if (mTabletModeChangedListeners.get(callingPid) != null) {
820 throw new IllegalStateException("The calling process has already registered "
821 + "a TabletModeChangedListener.");
822 }
823 TabletModeChangedListenerRecord record =
824 new TabletModeChangedListenerRecord(callingPid, listener);
825 try {
826 IBinder binder = listener.asBinder();
827 binder.linkToDeath(record, 0);
828 } catch (RemoteException ex) {
829 throw new RuntimeException(ex);
830 }
831 mTabletModeChangedListeners.put(callingPid, record);
832 }
833 }
834
835 private void onTabletModeChangedListenerDied(int pid) {
836 synchronized (mTabletModeLock) {
837 mTabletModeChangedListeners.remove(pid);
838 }
839 }
840
841 // Must be called on handler
842 private void deliverTabletModeChanged(long whenNanos, boolean inTabletMode) {
843 mTempTabletModeChangedListenersToNotify.clear();
844 final int numListeners;
845 synchronized (mTabletModeLock) {
846 numListeners = mTabletModeChangedListeners.size();
847 for (int i = 0; i < numListeners; i++) {
848 mTempTabletModeChangedListenersToNotify.add(
849 mTabletModeChangedListeners.valueAt(i));
850 }
851 }
852 for (int i = 0; i < numListeners; i++) {
853 mTempTabletModeChangedListenersToNotify.get(i).notifyTabletModeChanged(
854 whenNanos, inTabletMode);
855 }
856 }
857
Jeff Browncf39bdf2012-05-18 14:41:19 -0700858 // Must be called on handler.
Michael Wrightc93fbd12014-09-22 20:07:59 -0700859 private void showMissingKeyboardLayoutNotification(InputDevice device) {
Jeff Browncf39bdf2012-05-18 14:41:19 -0700860 if (!mKeyboardLayoutNotificationShown) {
Michael Wrightc93fbd12014-09-22 20:07:59 -0700861 final Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
862 if (device != null) {
863 intent.putExtra(Settings.EXTRA_INPUT_DEVICE_IDENTIFIER, device.getIdentifier());
Jeff Browncf39bdf2012-05-18 14:41:19 -0700864 }
Michael Wrightc93fbd12014-09-22 20:07:59 -0700865 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
866 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
867 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
868 final PendingIntent keyboardLayoutIntent = PendingIntent.getActivityAsUser(mContext, 0,
869 intent, 0, null, UserHandle.CURRENT);
Jeff Browncf39bdf2012-05-18 14:41:19 -0700870
871 Resources r = mContext.getResources();
872 Notification notification = new Notification.Builder(mContext)
873 .setContentTitle(r.getString(
874 R.string.select_keyboard_layout_notification_title))
875 .setContentText(r.getString(
876 R.string.select_keyboard_layout_notification_message))
Michael Wrightc93fbd12014-09-22 20:07:59 -0700877 .setContentIntent(keyboardLayoutIntent)
Jeff Browncf39bdf2012-05-18 14:41:19 -0700878 .setSmallIcon(R.drawable.ic_settings_language)
879 .setPriority(Notification.PRIORITY_LOW)
Alan Viverette4a357cd2015-03-18 18:37:18 -0700880 .setColor(mContext.getColor(
Selim Cinek255dd042014-08-19 22:29:02 +0200881 com.android.internal.R.color.system_notification_accent_color))
Jeff Browncf39bdf2012-05-18 14:41:19 -0700882 .build();
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700883 mNotificationManager.notifyAsUser(null,
884 R.string.select_keyboard_layout_notification_title,
885 notification, UserHandle.ALL);
Jeff Browncf39bdf2012-05-18 14:41:19 -0700886 mKeyboardLayoutNotificationShown = true;
887 }
888 }
889
890 // Must be called on handler.
891 private void hideMissingKeyboardLayoutNotification() {
892 if (mKeyboardLayoutNotificationShown) {
893 mKeyboardLayoutNotificationShown = false;
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700894 mNotificationManager.cancelAsUser(null,
895 R.string.select_keyboard_layout_notification_title,
896 UserHandle.ALL);
Jeff Browncf39bdf2012-05-18 14:41:19 -0700897 }
898 }
899
900 // Must be called on handler.
901 private void updateKeyboardLayouts() {
902 // Scan all input devices state for keyboard layouts that have been uninstalled.
903 final HashSet<String> availableKeyboardLayouts = new HashSet<String>();
904 visitAllKeyboardLayouts(new KeyboardLayoutVisitor() {
905 @Override
Michael Wright8ebac232014-09-18 18:29:49 -0700906 public void visitKeyboardLayout(Resources resources, String descriptor, String label,
907 String collection, int keyboardLayoutResId, int priority) {
Jeff Browncf39bdf2012-05-18 14:41:19 -0700908 availableKeyboardLayouts.add(descriptor);
909 }
910 });
911 synchronized (mDataStore) {
912 try {
913 mDataStore.removeUninstalledKeyboardLayouts(availableKeyboardLayouts);
914 } finally {
915 mDataStore.saveIfNeeded();
916 }
917 }
918
919 // Reload keyboard layouts.
920 reloadKeyboardLayouts();
921 }
922
Jeff Browncf39bdf2012-05-18 14:41:19 -0700923 private static boolean containsInputDeviceWithDescriptor(InputDevice[] inputDevices,
924 String descriptor) {
925 final int numDevices = inputDevices.length;
926 for (int i = 0; i < numDevices; i++) {
927 final InputDevice inputDevice = inputDevices[i];
928 if (inputDevice.getDescriptor().equals(descriptor)) {
929 return true;
930 }
931 }
932 return false;
Jeff Brown8d608662010-08-30 03:02:23 -0700933 }
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700934
935 @Override // Binder call
936 public KeyboardLayout[] getKeyboardLayouts() {
Jeff Brown6ec6f792012-04-17 16:52:41 -0700937 final ArrayList<KeyboardLayout> list = new ArrayList<KeyboardLayout>();
938 visitAllKeyboardLayouts(new KeyboardLayoutVisitor() {
939 @Override
Michael Wright8ebac232014-09-18 18:29:49 -0700940 public void visitKeyboardLayout(Resources resources, String descriptor, String label,
941 String collection, int keyboardLayoutResId, int priority) {
942 list.add(new KeyboardLayout(descriptor, label, collection, priority));
Jeff Brown6ec6f792012-04-17 16:52:41 -0700943 }
944 });
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700945 return list.toArray(new KeyboardLayout[list.size()]);
946 }
947
948 @Override // Binder call
949 public KeyboardLayout getKeyboardLayout(String keyboardLayoutDescriptor) {
950 if (keyboardLayoutDescriptor == null) {
951 throw new IllegalArgumentException("keyboardLayoutDescriptor must not be null");
952 }
953
Jeff Brown6ec6f792012-04-17 16:52:41 -0700954 final KeyboardLayout[] result = new KeyboardLayout[1];
955 visitKeyboardLayout(keyboardLayoutDescriptor, new KeyboardLayoutVisitor() {
956 @Override
Michael Wright8ebac232014-09-18 18:29:49 -0700957 public void visitKeyboardLayout(Resources resources, String descriptor,
958 String label, String collection, int keyboardLayoutResId, int priority) {
959 result[0] = new KeyboardLayout(descriptor, label, collection, priority);
Jeff Brown6ec6f792012-04-17 16:52:41 -0700960 }
961 });
962 if (result[0] == null) {
963 Log.w(TAG, "Could not get keyboard layout with descriptor '"
964 + keyboardLayoutDescriptor + "'.");
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700965 }
Jeff Brown6ec6f792012-04-17 16:52:41 -0700966 return result[0];
967 }
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700968
Jeff Brown6ec6f792012-04-17 16:52:41 -0700969 private void visitAllKeyboardLayouts(KeyboardLayoutVisitor visitor) {
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700970 final PackageManager pm = mContext.getPackageManager();
Jeff Brown6ec6f792012-04-17 16:52:41 -0700971 Intent intent = new Intent(InputManager.ACTION_QUERY_KEYBOARD_LAYOUTS);
972 for (ResolveInfo resolveInfo : pm.queryBroadcastReceivers(intent,
973 PackageManager.GET_META_DATA)) {
Michael Wright8ebac232014-09-18 18:29:49 -0700974 final ActivityInfo activityInfo = resolveInfo.activityInfo;
975 final int priority = resolveInfo.priority;
976 visitKeyboardLayoutsInPackage(pm, activityInfo, null, priority, visitor);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700977 }
978 }
979
Jeff Brown6ec6f792012-04-17 16:52:41 -0700980 private void visitKeyboardLayout(String keyboardLayoutDescriptor,
981 KeyboardLayoutVisitor visitor) {
982 KeyboardLayoutDescriptor d = KeyboardLayoutDescriptor.parse(keyboardLayoutDescriptor);
983 if (d != null) {
984 final PackageManager pm = mContext.getPackageManager();
985 try {
986 ActivityInfo receiver = pm.getReceiverInfo(
987 new ComponentName(d.packageName, d.receiverName),
988 PackageManager.GET_META_DATA);
Michael Wright8ebac232014-09-18 18:29:49 -0700989 visitKeyboardLayoutsInPackage(pm, receiver, d.keyboardLayoutName, 0, visitor);
Jeff Brown6ec6f792012-04-17 16:52:41 -0700990 } catch (NameNotFoundException ex) {
991 }
992 }
993 }
994
995 private void visitKeyboardLayoutsInPackage(PackageManager pm, ActivityInfo receiver,
Michael Wright8ebac232014-09-18 18:29:49 -0700996 String keyboardName, int requestedPriority, KeyboardLayoutVisitor visitor) {
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700997 Bundle metaData = receiver.metaData;
998 if (metaData == null) {
Jeff Brown6ec6f792012-04-17 16:52:41 -0700999 return;
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001000 }
1001
1002 int configResId = metaData.getInt(InputManager.META_DATA_KEYBOARD_LAYOUTS);
1003 if (configResId == 0) {
1004 Log.w(TAG, "Missing meta-data '" + InputManager.META_DATA_KEYBOARD_LAYOUTS
1005 + "' on receiver " + receiver.packageName + "/" + receiver.name);
Jeff Brown6ec6f792012-04-17 16:52:41 -07001006 return;
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001007 }
1008
Jeff Brownd9fec5d2012-05-17 16:01:54 -07001009 CharSequence receiverLabel = receiver.loadLabel(pm);
1010 String collection = receiverLabel != null ? receiverLabel.toString() : "";
Michael Wright8ebac232014-09-18 18:29:49 -07001011 int priority;
1012 if ((receiver.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
1013 priority = requestedPriority;
1014 } else {
1015 priority = 0;
1016 }
Jeff Brownd9fec5d2012-05-17 16:01:54 -07001017
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001018 try {
1019 Resources resources = pm.getResourcesForApplication(receiver.applicationInfo);
1020 XmlResourceParser parser = resources.getXml(configResId);
1021 try {
1022 XmlUtils.beginDocument(parser, "keyboard-layouts");
1023
1024 for (;;) {
1025 XmlUtils.nextElement(parser);
1026 String element = parser.getName();
1027 if (element == null) {
1028 break;
1029 }
1030 if (element.equals("keyboard-layout")) {
1031 TypedArray a = resources.obtainAttributes(
1032 parser, com.android.internal.R.styleable.KeyboardLayout);
1033 try {
1034 String name = a.getString(
1035 com.android.internal.R.styleable.KeyboardLayout_name);
1036 String label = a.getString(
1037 com.android.internal.R.styleable.KeyboardLayout_label);
Jeff Brown2f095762012-05-10 21:29:33 -07001038 int keyboardLayoutResId = a.getResourceId(
1039 com.android.internal.R.styleable.KeyboardLayout_keyboardLayout,
1040 0);
1041 if (name == null || label == null || keyboardLayoutResId == 0) {
1042 Log.w(TAG, "Missing required 'name', 'label' or 'keyboardLayout' "
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001043 + "attributes in keyboard layout "
1044 + "resource from receiver "
1045 + receiver.packageName + "/" + receiver.name);
1046 } else {
1047 String descriptor = KeyboardLayoutDescriptor.format(
1048 receiver.packageName, receiver.name, name);
Jeff Brown6ec6f792012-04-17 16:52:41 -07001049 if (keyboardName == null || name.equals(keyboardName)) {
1050 visitor.visitKeyboardLayout(resources, descriptor,
Michael Wright8ebac232014-09-18 18:29:49 -07001051 label, collection, keyboardLayoutResId, priority);
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001052 }
1053 }
1054 } finally {
1055 a.recycle();
1056 }
1057 } else {
1058 Log.w(TAG, "Skipping unrecognized element '" + element
1059 + "' in keyboard layout resource from receiver "
1060 + receiver.packageName + "/" + receiver.name);
1061 }
1062 }
1063 } finally {
1064 parser.close();
1065 }
1066 } catch (Exception ex) {
Jeff Brown6ec6f792012-04-17 16:52:41 -07001067 Log.w(TAG, "Could not parse keyboard layout resource from receiver "
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001068 + receiver.packageName + "/" + receiver.name, ex);
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001069 }
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001070 }
1071
RoboErikfb290df2013-12-16 11:27:55 -08001072 /**
1073 * Builds a layout descriptor for the vendor/product. This returns the
1074 * descriptor for ids that aren't useful (such as the default 0, 0).
1075 */
1076 private String getLayoutDescriptor(InputDeviceIdentifier identifier) {
1077 if (identifier == null || identifier.getDescriptor() == null) {
1078 throw new IllegalArgumentException("identifier and descriptor must not be null");
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001079 }
1080
RoboErikfb290df2013-12-16 11:27:55 -08001081 if (identifier.getVendorId() == 0 && identifier.getProductId() == 0) {
1082 return identifier.getDescriptor();
1083 }
1084 StringBuilder bob = new StringBuilder();
1085 bob.append("vendor:").append(identifier.getVendorId());
1086 bob.append(",product:").append(identifier.getProductId());
1087 return bob.toString();
1088 }
1089
1090 @Override // Binder call
1091 public String getCurrentKeyboardLayoutForInputDevice(InputDeviceIdentifier identifier) {
1092
1093 String key = getLayoutDescriptor(identifier);
Jeff Browna3bc5652012-04-17 11:42:25 -07001094 synchronized (mDataStore) {
RoboErikfb290df2013-12-16 11:27:55 -08001095 String layout = null;
1096 // try loading it using the layout descriptor if we have it
1097 layout = mDataStore.getCurrentKeyboardLayout(key);
1098 if (layout == null && !key.equals(identifier.getDescriptor())) {
1099 // if it doesn't exist fall back to the device descriptor
1100 layout = mDataStore.getCurrentKeyboardLayout(identifier.getDescriptor());
1101 }
1102 if (DEBUG) {
1103 Slog.d(TAG, "Loaded keyboard layout id for " + key + " and got "
1104 + layout);
1105 }
1106 return layout;
Jeff Browna3bc5652012-04-17 11:42:25 -07001107 }
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001108 }
1109
1110 @Override // Binder call
RoboErikfb290df2013-12-16 11:27:55 -08001111 public void setCurrentKeyboardLayoutForInputDevice(InputDeviceIdentifier identifier,
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001112 String keyboardLayoutDescriptor) {
1113 if (!checkCallingPermission(android.Manifest.permission.SET_KEYBOARD_LAYOUT,
Jeff Browncf39bdf2012-05-18 14:41:19 -07001114 "setCurrentKeyboardLayoutForInputDevice()")) {
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001115 throw new SecurityException("Requires SET_KEYBOARD_LAYOUT permission");
1116 }
Jeff Browncf39bdf2012-05-18 14:41:19 -07001117 if (keyboardLayoutDescriptor == null) {
1118 throw new IllegalArgumentException("keyboardLayoutDescriptor must not be null");
1119 }
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001120
RoboErikfb290df2013-12-16 11:27:55 -08001121 String key = getLayoutDescriptor(identifier);
Jeff Browna3bc5652012-04-17 11:42:25 -07001122 synchronized (mDataStore) {
1123 try {
RoboErikfb290df2013-12-16 11:27:55 -08001124 if (mDataStore.setCurrentKeyboardLayout(key, keyboardLayoutDescriptor)) {
1125 if (DEBUG) {
1126 Slog.d(TAG, "Saved keyboard layout using " + key);
1127 }
Jeff Browncf39bdf2012-05-18 14:41:19 -07001128 mHandler.sendEmptyMessage(MSG_RELOAD_KEYBOARD_LAYOUTS);
1129 }
Jeff Browna3bc5652012-04-17 11:42:25 -07001130 } finally {
1131 mDataStore.saveIfNeeded();
1132 }
1133 }
Jeff Browncf39bdf2012-05-18 14:41:19 -07001134 }
Jeff Brown6ec6f792012-04-17 16:52:41 -07001135
Jeff Browncf39bdf2012-05-18 14:41:19 -07001136 @Override // Binder call
RoboErikfb290df2013-12-16 11:27:55 -08001137 public String[] getKeyboardLayoutsForInputDevice(InputDeviceIdentifier identifier) {
1138 String key = getLayoutDescriptor(identifier);
Jeff Browncf39bdf2012-05-18 14:41:19 -07001139 synchronized (mDataStore) {
RoboErikfb290df2013-12-16 11:27:55 -08001140 String[] layouts = mDataStore.getKeyboardLayouts(key);
1141 if ((layouts == null || layouts.length == 0)
1142 && !key.equals(identifier.getDescriptor())) {
1143 layouts = mDataStore.getKeyboardLayouts(identifier.getDescriptor());
1144 }
1145 return layouts;
Jeff Browncf39bdf2012-05-18 14:41:19 -07001146 }
1147 }
1148
1149 @Override // Binder call
RoboErikfb290df2013-12-16 11:27:55 -08001150 public void addKeyboardLayoutForInputDevice(InputDeviceIdentifier identifier,
Jeff Browncf39bdf2012-05-18 14:41:19 -07001151 String keyboardLayoutDescriptor) {
1152 if (!checkCallingPermission(android.Manifest.permission.SET_KEYBOARD_LAYOUT,
1153 "addKeyboardLayoutForInputDevice()")) {
1154 throw new SecurityException("Requires SET_KEYBOARD_LAYOUT permission");
1155 }
Jeff Browncf39bdf2012-05-18 14:41:19 -07001156 if (keyboardLayoutDescriptor == null) {
1157 throw new IllegalArgumentException("keyboardLayoutDescriptor must not be null");
1158 }
1159
RoboErikfb290df2013-12-16 11:27:55 -08001160 String key = getLayoutDescriptor(identifier);
Jeff Browncf39bdf2012-05-18 14:41:19 -07001161 synchronized (mDataStore) {
1162 try {
RoboErikfb290df2013-12-16 11:27:55 -08001163 String oldLayout = mDataStore.getCurrentKeyboardLayout(key);
1164 if (oldLayout == null && !key.equals(identifier.getDescriptor())) {
1165 oldLayout = mDataStore.getCurrentKeyboardLayout(identifier.getDescriptor());
1166 }
1167 if (mDataStore.addKeyboardLayout(key, keyboardLayoutDescriptor)
Jeff Browncf39bdf2012-05-18 14:41:19 -07001168 && !Objects.equal(oldLayout,
RoboErikfb290df2013-12-16 11:27:55 -08001169 mDataStore.getCurrentKeyboardLayout(key))) {
Jeff Browncf39bdf2012-05-18 14:41:19 -07001170 mHandler.sendEmptyMessage(MSG_RELOAD_KEYBOARD_LAYOUTS);
1171 }
1172 } finally {
1173 mDataStore.saveIfNeeded();
Jeff Brown6ec6f792012-04-17 16:52:41 -07001174 }
Jeff Browncf39bdf2012-05-18 14:41:19 -07001175 }
1176 }
1177
1178 @Override // Binder call
RoboErikfb290df2013-12-16 11:27:55 -08001179 public void removeKeyboardLayoutForInputDevice(InputDeviceIdentifier identifier,
Jeff Browncf39bdf2012-05-18 14:41:19 -07001180 String keyboardLayoutDescriptor) {
1181 if (!checkCallingPermission(android.Manifest.permission.SET_KEYBOARD_LAYOUT,
1182 "removeKeyboardLayoutForInputDevice()")) {
1183 throw new SecurityException("Requires SET_KEYBOARD_LAYOUT permission");
1184 }
Jeff Browncf39bdf2012-05-18 14:41:19 -07001185 if (keyboardLayoutDescriptor == null) {
1186 throw new IllegalArgumentException("keyboardLayoutDescriptor must not be null");
1187 }
1188
RoboErikfb290df2013-12-16 11:27:55 -08001189 String key = getLayoutDescriptor(identifier);
Jeff Browncf39bdf2012-05-18 14:41:19 -07001190 synchronized (mDataStore) {
1191 try {
RoboErikfb290df2013-12-16 11:27:55 -08001192 String oldLayout = mDataStore.getCurrentKeyboardLayout(key);
1193 if (oldLayout == null && !key.equals(identifier.getDescriptor())) {
1194 oldLayout = mDataStore.getCurrentKeyboardLayout(identifier.getDescriptor());
1195 }
1196 boolean removed = mDataStore.removeKeyboardLayout(key, keyboardLayoutDescriptor);
1197 if (!key.equals(identifier.getDescriptor())) {
1198 // We need to remove from both places to ensure it is gone
1199 removed |= mDataStore.removeKeyboardLayout(identifier.getDescriptor(),
1200 keyboardLayoutDescriptor);
1201 }
1202 if (removed && !Objects.equal(oldLayout,
1203 mDataStore.getCurrentKeyboardLayout(key))) {
Jeff Browncf39bdf2012-05-18 14:41:19 -07001204 mHandler.sendEmptyMessage(MSG_RELOAD_KEYBOARD_LAYOUTS);
1205 }
1206 } finally {
1207 mDataStore.saveIfNeeded();
1208 }
1209 }
1210 }
1211
1212 public void switchKeyboardLayout(int deviceId, int direction) {
1213 mHandler.obtainMessage(MSG_SWITCH_KEYBOARD_LAYOUT, deviceId, direction).sendToTarget();
1214 }
1215
1216 // Must be called on handler.
1217 private void handleSwitchKeyboardLayout(int deviceId, int direction) {
1218 final InputDevice device = getInputDevice(deviceId);
Jeff Browncf39bdf2012-05-18 14:41:19 -07001219 if (device != null) {
1220 final boolean changed;
1221 final String keyboardLayoutDescriptor;
RoboErikfb290df2013-12-16 11:27:55 -08001222
1223 String key = getLayoutDescriptor(device.getIdentifier());
Jeff Browncf39bdf2012-05-18 14:41:19 -07001224 synchronized (mDataStore) {
1225 try {
RoboErikfb290df2013-12-16 11:27:55 -08001226 changed = mDataStore.switchKeyboardLayout(key, direction);
Jeff Browncf39bdf2012-05-18 14:41:19 -07001227 keyboardLayoutDescriptor = mDataStore.getCurrentKeyboardLayout(
RoboErikfb290df2013-12-16 11:27:55 -08001228 key);
Jeff Browncf39bdf2012-05-18 14:41:19 -07001229 } finally {
1230 mDataStore.saveIfNeeded();
1231 }
1232 }
1233
1234 if (changed) {
1235 if (mSwitchedKeyboardLayoutToast != null) {
1236 mSwitchedKeyboardLayoutToast.cancel();
1237 mSwitchedKeyboardLayoutToast = null;
1238 }
1239 if (keyboardLayoutDescriptor != null) {
1240 KeyboardLayout keyboardLayout = getKeyboardLayout(keyboardLayoutDescriptor);
1241 if (keyboardLayout != null) {
1242 mSwitchedKeyboardLayoutToast = Toast.makeText(
1243 mContext, keyboardLayout.getLabel(), Toast.LENGTH_SHORT);
1244 mSwitchedKeyboardLayoutToast.show();
1245 }
1246 }
1247
1248 reloadKeyboardLayouts();
1249 }
Jeff Brown6ec6f792012-04-17 16:52:41 -07001250 }
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001251 }
1252
Jeff Brown9302c872011-07-13 22:51:29 -07001253 public void setInputWindows(InputWindowHandle[] windowHandles) {
Jeff Brown4532e612012-04-05 14:27:12 -07001254 nativeSetInputWindows(mPtr, windowHandles);
Jeff Brown349703e2010-06-22 01:27:15 -07001255 }
RoboErikfb290df2013-12-16 11:27:55 -08001256
Jeff Brown9302c872011-07-13 22:51:29 -07001257 public void setFocusedApplication(InputApplicationHandle application) {
Jeff Brown4532e612012-04-05 14:27:12 -07001258 nativeSetFocusedApplication(mPtr, application);
Jeff Brown349703e2010-06-22 01:27:15 -07001259 }
RoboErikfb290df2013-12-16 11:27:55 -08001260
Jeff Brown349703e2010-06-22 01:27:15 -07001261 public void setInputDispatchMode(boolean enabled, boolean frozen) {
Jeff Brown4532e612012-04-05 14:27:12 -07001262 nativeSetInputDispatchMode(mPtr, enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -07001263 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08001264
1265 public void setSystemUiVisibility(int visibility) {
Jeff Brown4532e612012-04-05 14:27:12 -07001266 nativeSetSystemUiVisibility(mPtr, visibility);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001267 }
1268
Jeff Browne6504122010-09-27 14:52:15 -07001269 /**
1270 * Atomically transfers touch focus from one window to another as identified by
1271 * their input channels. It is possible for multiple windows to have
1272 * touch focus if they support split touch dispatch
1273 * {@link android.view.WindowManager.LayoutParams#FLAG_SPLIT_TOUCH} but this
1274 * method only transfers touch focus of the specified window without affecting
1275 * other windows that may also have touch focus at the same time.
1276 * @param fromChannel The channel of a window that currently has touch focus.
1277 * @param toChannel The channel of the window that should receive touch focus in
1278 * place of the first.
1279 * @return True if the transfer was successful. False if the window with the
1280 * specified channel did not actually have touch focus at the time of the request.
1281 */
1282 public boolean transferTouchFocus(InputChannel fromChannel, InputChannel toChannel) {
1283 if (fromChannel == null) {
1284 throw new IllegalArgumentException("fromChannel must not be null.");
1285 }
1286 if (toChannel == null) {
1287 throw new IllegalArgumentException("toChannel must not be null.");
1288 }
Jeff Brown4532e612012-04-05 14:27:12 -07001289 return nativeTransferTouchFocus(mPtr, fromChannel, toChannel);
Jeff Browne6504122010-09-27 14:52:15 -07001290 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08001291
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001292 @Override // Binder call
Jeff Brownac143512012-04-05 18:57:33 -07001293 public void tryPointerSpeed(int speed) {
1294 if (!checkCallingPermission(android.Manifest.permission.SET_POINTER_SPEED,
1295 "tryPointerSpeed()")) {
1296 throw new SecurityException("Requires SET_POINTER_SPEED permission");
1297 }
1298
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001299 if (speed < InputManager.MIN_POINTER_SPEED || speed > InputManager.MAX_POINTER_SPEED) {
1300 throw new IllegalArgumentException("speed out of range");
1301 }
1302
Jeff Brownac143512012-04-05 18:57:33 -07001303 setPointerSpeedUnchecked(speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001304 }
1305
1306 public void updatePointerSpeedFromSettings() {
Jeff Brownac143512012-04-05 18:57:33 -07001307 int speed = getPointerSpeedSetting();
1308 setPointerSpeedUnchecked(speed);
1309 }
1310
1311 private void setPointerSpeedUnchecked(int speed) {
1312 speed = Math.min(Math.max(speed, InputManager.MIN_POINTER_SPEED),
1313 InputManager.MAX_POINTER_SPEED);
1314 nativeSetPointerSpeed(mPtr, speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001315 }
1316
1317 private void registerPointerSpeedSettingObserver() {
1318 mContext.getContentResolver().registerContentObserver(
1319 Settings.System.getUriFor(Settings.System.POINTER_SPEED), true,
Jeff Brown4532e612012-04-05 14:27:12 -07001320 new ContentObserver(mHandler) {
Jeff Brown1a84fd12011-06-02 01:26:32 -07001321 @Override
1322 public void onChange(boolean selfChange) {
1323 updatePointerSpeedFromSettings();
1324 }
Jeff Brownd4935962012-09-25 13:27:20 -07001325 }, UserHandle.USER_ALL);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001326 }
1327
Jeff Brownac143512012-04-05 18:57:33 -07001328 private int getPointerSpeedSetting() {
1329 int speed = InputManager.DEFAULT_POINTER_SPEED;
Jeff Brown1a84fd12011-06-02 01:26:32 -07001330 try {
Jeff Brownd4935962012-09-25 13:27:20 -07001331 speed = Settings.System.getIntForUser(mContext.getContentResolver(),
1332 Settings.System.POINTER_SPEED, UserHandle.USER_CURRENT);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001333 } catch (SettingNotFoundException snfe) {
1334 }
1335 return speed;
1336 }
1337
Jeff Browndaf4a122011-08-26 17:14:14 -07001338 public void updateShowTouchesFromSettings() {
1339 int setting = getShowTouchesSetting(0);
Jeff Brown4532e612012-04-05 14:27:12 -07001340 nativeSetShowTouches(mPtr, setting != 0);
Jeff Browndaf4a122011-08-26 17:14:14 -07001341 }
1342
1343 private void registerShowTouchesSettingObserver() {
1344 mContext.getContentResolver().registerContentObserver(
1345 Settings.System.getUriFor(Settings.System.SHOW_TOUCHES), true,
Jeff Brown4532e612012-04-05 14:27:12 -07001346 new ContentObserver(mHandler) {
Jeff Browndaf4a122011-08-26 17:14:14 -07001347 @Override
1348 public void onChange(boolean selfChange) {
1349 updateShowTouchesFromSettings();
1350 }
Jeff Brownd4935962012-09-25 13:27:20 -07001351 }, UserHandle.USER_ALL);
Jeff Browndaf4a122011-08-26 17:14:14 -07001352 }
1353
Jun Mukai19a56012015-11-24 11:25:52 -08001354 private void registerAccessibilityLargePointerSettingObserver() {
1355 mContext.getContentResolver().registerContentObserver(
1356 Settings.Secure.getUriFor(Settings.Secure.ACCESSIBILITY_LARGE_POINTER_ICON), true,
1357 new ContentObserver(mHandler) {
1358 @Override
1359 public void onChange(boolean selfChange) {
1360 nativeReloadPointerIcons(mPtr);
1361 }
1362 }, UserHandle.USER_ALL);
1363 }
1364
Jeff Browndaf4a122011-08-26 17:14:14 -07001365 private int getShowTouchesSetting(int defaultValue) {
1366 int result = defaultValue;
1367 try {
Jeff Brownd4935962012-09-25 13:27:20 -07001368 result = Settings.System.getIntForUser(mContext.getContentResolver(),
1369 Settings.System.SHOW_TOUCHES, UserHandle.USER_CURRENT);
Jeff Browndaf4a122011-08-26 17:14:14 -07001370 } catch (SettingNotFoundException snfe) {
1371 }
1372 return result;
1373 }
1374
Jeff Browna47425a2012-04-13 04:09:27 -07001375 // Binder call
1376 @Override
1377 public void vibrate(int deviceId, long[] pattern, int repeat, IBinder token) {
1378 if (repeat >= pattern.length) {
1379 throw new ArrayIndexOutOfBoundsException();
1380 }
1381
1382 VibratorToken v;
1383 synchronized (mVibratorLock) {
1384 v = mVibratorTokens.get(token);
1385 if (v == null) {
1386 v = new VibratorToken(deviceId, token, mNextVibratorTokenValue++);
1387 try {
1388 token.linkToDeath(v, 0);
1389 } catch (RemoteException ex) {
1390 // give up
1391 throw new RuntimeException(ex);
1392 }
1393 mVibratorTokens.put(token, v);
1394 }
1395 }
1396
1397 synchronized (v) {
1398 v.mVibrating = true;
1399 nativeVibrate(mPtr, deviceId, pattern, repeat, v.mTokenValue);
1400 }
1401 }
1402
1403 // Binder call
1404 @Override
1405 public void cancelVibrate(int deviceId, IBinder token) {
1406 VibratorToken v;
1407 synchronized (mVibratorLock) {
1408 v = mVibratorTokens.get(token);
1409 if (v == null || v.mDeviceId != deviceId) {
1410 return; // nothing to cancel
1411 }
1412 }
1413
1414 cancelVibrateIfNeeded(v);
1415 }
1416
1417 void onVibratorTokenDied(VibratorToken v) {
1418 synchronized (mVibratorLock) {
1419 mVibratorTokens.remove(v.mToken);
1420 }
1421
1422 cancelVibrateIfNeeded(v);
1423 }
1424
1425 private void cancelVibrateIfNeeded(VibratorToken v) {
1426 synchronized (v) {
1427 if (v.mVibrating) {
1428 nativeCancelVibrate(mPtr, v.mDeviceId, v.mTokenValue);
1429 v.mVibrating = false;
1430 }
1431 }
1432 }
1433
Jun Mukai19a56012015-11-24 11:25:52 -08001434 // Binder call
1435 @Override
1436 public void setPointerIconShape(int iconId) {
1437 nativeSetPointerIconShape(mPtr, iconId);
1438 }
Jun Mukai1db53972015-09-11 18:08:31 -07001439
Jeff Brown4532e612012-04-05 14:27:12 -07001440 @Override
1441 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Browna3bc5652012-04-17 11:42:25 -07001442 if (mContext.checkCallingOrSelfPermission(Manifest.permission.DUMP)
Jeff Brown4532e612012-04-05 14:27:12 -07001443 != PackageManager.PERMISSION_GRANTED) {
1444 pw.println("Permission Denial: can't dump InputManager from from pid="
1445 + Binder.getCallingPid()
1446 + ", uid=" + Binder.getCallingUid());
1447 return;
1448 }
1449
1450 pw.println("INPUT MANAGER (dumpsys input)\n");
1451 String dumpStr = nativeDump(mPtr);
Jeff Browne33348b2010-07-15 23:54:05 -07001452 if (dumpStr != null) {
1453 pw.println(dumpStr);
1454 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001455 }
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001456
Jeff Brownac143512012-04-05 18:57:33 -07001457 private boolean checkCallingPermission(String permission, String func) {
1458 // Quick check: if the calling permission is me, it's all okay.
1459 if (Binder.getCallingPid() == Process.myPid()) {
1460 return true;
1461 }
1462
1463 if (mContext.checkCallingPermission(permission) == PackageManager.PERMISSION_GRANTED) {
1464 return true;
1465 }
1466 String msg = "Permission Denial: " + func + " from pid="
1467 + Binder.getCallingPid()
1468 + ", uid=" + Binder.getCallingUid()
1469 + " requires " + permission;
1470 Slog.w(TAG, msg);
1471 return false;
1472 }
1473
Jeff Brown4532e612012-04-05 14:27:12 -07001474 // Called by the heartbeat to ensure locks are not held indefinitely (for deadlock detection).
Craig Mautner2f39e9f2012-09-21 11:39:54 -07001475 @Override
Jeff Brown89ef0722011-08-10 16:25:21 -07001476 public void monitor() {
1477 synchronized (mInputFilterLock) { }
Jeff Brown4532e612012-04-05 14:27:12 -07001478 nativeMonitor(mPtr);
Jeff Brown89ef0722011-08-10 16:25:21 -07001479 }
1480
Jeff Brown4532e612012-04-05 14:27:12 -07001481 // Native callback.
1482 private void notifyConfigurationChanged(long whenNanos) {
Jeff Browna9d131c2012-09-20 16:48:17 -07001483 mWindowManagerCallbacks.notifyConfigurationChanged();
Jeff Brown4532e612012-04-05 14:27:12 -07001484 }
1485
1486 // Native callback.
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001487 private void notifyInputDevicesChanged(InputDevice[] inputDevices) {
1488 synchronized (mInputDevicesLock) {
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001489 if (!mInputDevicesChangedPending) {
1490 mInputDevicesChangedPending = true;
Jeff Browncf39bdf2012-05-18 14:41:19 -07001491 mHandler.obtainMessage(MSG_DELIVER_INPUT_DEVICES_CHANGED,
1492 mInputDevices).sendToTarget();
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001493 }
Jeff Browncf39bdf2012-05-18 14:41:19 -07001494
1495 mInputDevices = inputDevices;
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001496 }
1497 }
1498
1499 // Native callback.
Jeff Brownbcc046a2012-09-27 20:46:43 -07001500 private void notifySwitch(long whenNanos, int switchValues, int switchMask) {
1501 if (DEBUG) {
1502 Slog.d(TAG, "notifySwitch: values=" + Integer.toHexString(switchValues)
1503 + ", mask=" + Integer.toHexString(switchMask));
1504 }
1505
Craig Mautner2f39e9f2012-09-21 11:39:54 -07001506 if ((switchMask & SW_LID_BIT) != 0) {
1507 final boolean lidOpen = ((switchValues & SW_LID_BIT) == 0);
Jeff Brownbcc046a2012-09-27 20:46:43 -07001508 mWindowManagerCallbacks.notifyLidSwitchChanged(whenNanos, lidOpen);
Jeff Brown53384282012-08-20 20:16:01 -07001509 }
Craig Mautner2f39e9f2012-09-21 11:39:54 -07001510
Michael Wright3818c922014-09-02 13:59:07 -07001511 if ((switchMask & SW_CAMERA_LENS_COVER_BIT) != 0) {
Michael Wright9e10d252014-09-13 19:41:20 -07001512 final boolean lensCovered = ((switchValues & SW_CAMERA_LENS_COVER_BIT) != 0);
Michael Wright3818c922014-09-02 13:59:07 -07001513 mWindowManagerCallbacks.notifyCameraLensCoverSwitchChanged(whenNanos, lensCovered);
1514 }
1515
Craig Mautner2f39e9f2012-09-21 11:39:54 -07001516 if (mUseDevInputEventForAudioJack && (switchMask & SW_JACK_BITS) != 0) {
1517 mWiredAccessoryCallbacks.notifyWiredAccessoryChanged(whenNanos, switchValues,
1518 switchMask);
1519 }
Michael Wright39e5e942015-08-19 22:52:47 +01001520
Michael Wright9209c9c2015-09-03 17:57:01 +01001521 if ((switchMask & SW_TABLET_MODE_BIT) != 0) {
Michael Wright39e5e942015-08-19 22:52:47 +01001522 SomeArgs args = SomeArgs.obtain();
1523 args.argi1 = (int) (whenNanos & 0xFFFFFFFF);
1524 args.argi2 = (int) (whenNanos >> 32);
1525 args.arg1 = Boolean.valueOf((switchValues & SW_TABLET_MODE_BIT) != 0);
1526 mHandler.obtainMessage(MSG_DELIVER_TABLET_MODE_CHANGED,
1527 args).sendToTarget();
1528 }
Jeff Brown4532e612012-04-05 14:27:12 -07001529 }
1530
1531 // Native callback.
1532 private void notifyInputChannelBroken(InputWindowHandle inputWindowHandle) {
Jeff Browna9d131c2012-09-20 16:48:17 -07001533 mWindowManagerCallbacks.notifyInputChannelBroken(inputWindowHandle);
Jeff Brown4532e612012-04-05 14:27:12 -07001534 }
1535
1536 // Native callback.
1537 private long notifyANR(InputApplicationHandle inputApplicationHandle,
Jeff Brownbd181bb2013-09-10 16:44:24 -07001538 InputWindowHandle inputWindowHandle, String reason) {
1539 return mWindowManagerCallbacks.notifyANR(
1540 inputApplicationHandle, inputWindowHandle, reason);
Jeff Brown4532e612012-04-05 14:27:12 -07001541 }
1542
1543 // Native callback.
1544 final boolean filterInputEvent(InputEvent event, int policyFlags) {
1545 synchronized (mInputFilterLock) {
1546 if (mInputFilter != null) {
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07001547 try {
1548 mInputFilter.filterInputEvent(event, policyFlags);
1549 } catch (RemoteException e) {
1550 /* ignore */
1551 }
Jeff Brown4532e612012-04-05 14:27:12 -07001552 return false;
1553 }
1554 }
1555 event.recycle();
1556 return true;
1557 }
1558
1559 // Native callback.
Jeff Brown037c33e2014-04-09 00:31:55 -07001560 private int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags) {
1561 return mWindowManagerCallbacks.interceptKeyBeforeQueueing(event, policyFlags);
Jeff Brown4532e612012-04-05 14:27:12 -07001562 }
1563
1564 // Native callback.
Michael Wright70af00a2014-09-03 19:30:20 -07001565 private int interceptMotionBeforeQueueingNonInteractive(long whenNanos, int policyFlags) {
1566 return mWindowManagerCallbacks.interceptMotionBeforeQueueingNonInteractive(
Jeff Brown26875502014-01-30 21:47:47 -08001567 whenNanos, policyFlags);
Jeff Brown4532e612012-04-05 14:27:12 -07001568 }
1569
1570 // Native callback.
1571 private long interceptKeyBeforeDispatching(InputWindowHandle focus,
1572 KeyEvent event, int policyFlags) {
Jeff Browna9d131c2012-09-20 16:48:17 -07001573 return mWindowManagerCallbacks.interceptKeyBeforeDispatching(focus, event, policyFlags);
Jeff Brown4532e612012-04-05 14:27:12 -07001574 }
1575
1576 // Native callback.
1577 private KeyEvent dispatchUnhandledKey(InputWindowHandle focus,
1578 KeyEvent event, int policyFlags) {
Jeff Browna9d131c2012-09-20 16:48:17 -07001579 return mWindowManagerCallbacks.dispatchUnhandledKey(focus, event, policyFlags);
Jeff Brown4532e612012-04-05 14:27:12 -07001580 }
1581
1582 // Native callback.
1583 private boolean checkInjectEventsPermission(int injectorPid, int injectorUid) {
1584 return mContext.checkPermission(android.Manifest.permission.INJECT_EVENTS,
1585 injectorPid, injectorUid) == PackageManager.PERMISSION_GRANTED;
1586 }
1587
1588 // Native callback.
1589 private int getVirtualKeyQuietTimeMillis() {
1590 return mContext.getResources().getInteger(
1591 com.android.internal.R.integer.config_virtualKeyQuietTimeMillis);
1592 }
1593
1594 // Native callback.
1595 private String[] getExcludedDeviceNames() {
1596 ArrayList<String> names = new ArrayList<String>();
1597
1598 // Read partner-provided list of excluded input devices
1599 XmlPullParser parser = null;
1600 // Environment.getRootDirectory() is a fancy way of saying ANDROID_ROOT or "/system".
1601 File confFile = new File(Environment.getRootDirectory(), EXCLUDED_DEVICES_PATH);
1602 FileReader confreader = null;
1603 try {
1604 confreader = new FileReader(confFile);
1605 parser = Xml.newPullParser();
1606 parser.setInput(confreader);
1607 XmlUtils.beginDocument(parser, "devices");
1608
1609 while (true) {
1610 XmlUtils.nextElement(parser);
1611 if (!"device".equals(parser.getName())) {
1612 break;
1613 }
1614 String name = parser.getAttributeValue(null, "name");
1615 if (name != null) {
1616 names.add(name);
1617 }
1618 }
1619 } catch (FileNotFoundException e) {
1620 // It's ok if the file does not exist.
1621 } catch (Exception e) {
1622 Slog.e(TAG, "Exception while parsing '" + confFile.getAbsolutePath() + "'", e);
1623 } finally {
1624 try { if (confreader != null) confreader.close(); } catch (IOException e) { }
1625 }
1626
1627 return names.toArray(new String[names.size()]);
1628 }
1629
1630 // Native callback.
1631 private int getKeyRepeatTimeout() {
1632 return ViewConfiguration.getKeyRepeatTimeout();
1633 }
1634
1635 // Native callback.
1636 private int getKeyRepeatDelay() {
1637 return ViewConfiguration.getKeyRepeatDelay();
1638 }
1639
1640 // Native callback.
1641 private int getHoverTapTimeout() {
1642 return ViewConfiguration.getHoverTapTimeout();
1643 }
1644
1645 // Native callback.
1646 private int getHoverTapSlop() {
1647 return ViewConfiguration.getHoverTapSlop();
1648 }
1649
1650 // Native callback.
1651 private int getDoubleTapTimeout() {
1652 return ViewConfiguration.getDoubleTapTimeout();
1653 }
1654
1655 // Native callback.
1656 private int getLongPressTimeout() {
1657 return ViewConfiguration.getLongPressTimeout();
1658 }
1659
1660 // Native callback.
1661 private int getPointerLayer() {
Jeff Browna9d131c2012-09-20 16:48:17 -07001662 return mWindowManagerCallbacks.getPointerLayer();
Jeff Brown4532e612012-04-05 14:27:12 -07001663 }
1664
1665 // Native callback.
1666 private PointerIcon getPointerIcon() {
1667 return PointerIcon.getDefaultIcon(mContext);
1668 }
1669
Jeff Brown6ec6f792012-04-17 16:52:41 -07001670 // Native callback.
RoboErikfb290df2013-12-16 11:27:55 -08001671 private String[] getKeyboardLayoutOverlay(InputDeviceIdentifier identifier) {
Jeff Brown6ec6f792012-04-17 16:52:41 -07001672 if (!mSystemReady) {
1673 return null;
1674 }
1675
RoboErikfb290df2013-12-16 11:27:55 -08001676 String keyboardLayoutDescriptor = getCurrentKeyboardLayoutForInputDevice(identifier);
Jeff Brown6ec6f792012-04-17 16:52:41 -07001677 if (keyboardLayoutDescriptor == null) {
1678 return null;
1679 }
1680
1681 final String[] result = new String[2];
1682 visitKeyboardLayout(keyboardLayoutDescriptor, new KeyboardLayoutVisitor() {
1683 @Override
Michael Wright8ebac232014-09-18 18:29:49 -07001684 public void visitKeyboardLayout(Resources resources, String descriptor, String label,
1685 String collection, int keyboardLayoutResId, int priority) {
Jeff Brown6ec6f792012-04-17 16:52:41 -07001686 try {
1687 result[0] = descriptor;
1688 result[1] = Streams.readFully(new InputStreamReader(
Jeff Brown2f095762012-05-10 21:29:33 -07001689 resources.openRawResource(keyboardLayoutResId)));
Jeff Brown6ec6f792012-04-17 16:52:41 -07001690 } catch (IOException ex) {
1691 } catch (NotFoundException ex) {
1692 }
1693 }
1694 });
1695 if (result[0] == null) {
1696 Log.w(TAG, "Could not get keyboard layout with descriptor '"
1697 + keyboardLayoutDescriptor + "'.");
1698 return null;
1699 }
1700 return result;
1701 }
1702
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001703 // Native callback.
1704 private String getDeviceAlias(String uniqueId) {
Matthew Xie96313142012-06-29 16:57:31 -07001705 if (BluetoothAdapter.checkBluetoothAddress(uniqueId)) {
1706 // TODO(BT) mBluetoothService.getRemoteAlias(uniqueId)
1707 return null;
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001708 }
1709 return null;
1710 }
1711
Jeff Brown4532e612012-04-05 14:27:12 -07001712 /**
1713 * Callback interface implemented by the Window Manager.
1714 */
Jeff Browna9d131c2012-09-20 16:48:17 -07001715 public interface WindowManagerCallbacks {
Jeff Brown4532e612012-04-05 14:27:12 -07001716 public void notifyConfigurationChanged();
1717
1718 public void notifyLidSwitchChanged(long whenNanos, boolean lidOpen);
1719
Michael Wright3818c922014-09-02 13:59:07 -07001720 public void notifyCameraLensCoverSwitchChanged(long whenNanos, boolean lensCovered);
1721
Jeff Brown4532e612012-04-05 14:27:12 -07001722 public void notifyInputChannelBroken(InputWindowHandle inputWindowHandle);
1723
1724 public long notifyANR(InputApplicationHandle inputApplicationHandle,
Jeff Brownbd181bb2013-09-10 16:44:24 -07001725 InputWindowHandle inputWindowHandle, String reason);
Jeff Brown4532e612012-04-05 14:27:12 -07001726
Jeff Brown037c33e2014-04-09 00:31:55 -07001727 public int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags);
Jeff Brown4532e612012-04-05 14:27:12 -07001728
Michael Wright70af00a2014-09-03 19:30:20 -07001729 public int interceptMotionBeforeQueueingNonInteractive(long whenNanos, int policyFlags);
Jeff Brown4532e612012-04-05 14:27:12 -07001730
1731 public long interceptKeyBeforeDispatching(InputWindowHandle focus,
1732 KeyEvent event, int policyFlags);
1733
1734 public KeyEvent dispatchUnhandledKey(InputWindowHandle focus,
1735 KeyEvent event, int policyFlags);
1736
1737 public int getPointerLayer();
1738 }
1739
1740 /**
Craig Mautner2f39e9f2012-09-21 11:39:54 -07001741 * Callback interface implemented by WiredAccessoryObserver.
1742 */
1743 public interface WiredAccessoryCallbacks {
1744 public void notifyWiredAccessoryChanged(long whenNanos, int switchValues, int switchMask);
Eric Laurent4a5eeb92014-05-06 10:49:04 -07001745 public void systemReady();
Craig Mautner2f39e9f2012-09-21 11:39:54 -07001746 }
1747
1748 /**
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001749 * Private handler for the input manager.
1750 */
1751 private final class InputManagerHandler extends Handler {
Jeff Browna9d131c2012-09-20 16:48:17 -07001752 public InputManagerHandler(Looper looper) {
1753 super(looper, null, true /*async*/);
Jeff Browna2910d02012-08-25 12:29:46 -07001754 }
1755
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001756 @Override
1757 public void handleMessage(Message msg) {
1758 switch (msg.what) {
1759 case MSG_DELIVER_INPUT_DEVICES_CHANGED:
Jeff Browncf39bdf2012-05-18 14:41:19 -07001760 deliverInputDevicesChanged((InputDevice[])msg.obj);
1761 break;
1762 case MSG_SWITCH_KEYBOARD_LAYOUT:
1763 handleSwitchKeyboardLayout(msg.arg1, msg.arg2);
1764 break;
1765 case MSG_RELOAD_KEYBOARD_LAYOUTS:
1766 reloadKeyboardLayouts();
1767 break;
1768 case MSG_UPDATE_KEYBOARD_LAYOUTS:
1769 updateKeyboardLayouts();
1770 break;
1771 case MSG_RELOAD_DEVICE_ALIASES:
1772 reloadDeviceAliases();
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001773 break;
Michael Wright39e5e942015-08-19 22:52:47 +01001774 case MSG_DELIVER_TABLET_MODE_CHANGED:
1775 SomeArgs args = (SomeArgs) msg.obj;
1776 long whenNanos = (args.argi1 & 0xFFFFFFFFl) | ((long) args.argi2 << 32);
1777 boolean inTabletMode = (boolean) args.arg1;
1778 deliverTabletModeChanged(whenNanos, inTabletMode);
1779 break;
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001780 }
1781 }
1782 }
1783
1784 /**
Jeff Brown4532e612012-04-05 14:27:12 -07001785 * Hosting interface for input filters to call back into the input manager.
1786 */
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07001787 private final class InputFilterHost extends IInputFilterHost.Stub {
Jeff Brown0029c662011-03-30 02:25:18 -07001788 private boolean mDisconnected;
1789
1790 public void disconnectLocked() {
1791 mDisconnected = true;
1792 }
1793
Craig Mautner2f39e9f2012-09-21 11:39:54 -07001794 @Override
Jeff Brown0029c662011-03-30 02:25:18 -07001795 public void sendInputEvent(InputEvent event, int policyFlags) {
1796 if (event == null) {
1797 throw new IllegalArgumentException("event must not be null");
1798 }
1799
1800 synchronized (mInputFilterLock) {
1801 if (!mDisconnected) {
Jeff Brownca9bc702014-02-11 14:32:56 -08001802 nativeInjectInputEvent(mPtr, event, Display.DEFAULT_DISPLAY, 0, 0,
Jeff Brownac143512012-04-05 18:57:33 -07001803 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC, 0,
Jeff Brown0029c662011-03-30 02:25:18 -07001804 policyFlags | WindowManagerPolicy.FLAG_FILTERED);
1805 }
1806 }
1807 }
1808 }
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001809
1810 private static final class KeyboardLayoutDescriptor {
1811 public String packageName;
1812 public String receiverName;
1813 public String keyboardLayoutName;
1814
1815 public static String format(String packageName,
1816 String receiverName, String keyboardName) {
1817 return packageName + "/" + receiverName + "/" + keyboardName;
1818 }
1819
1820 public static KeyboardLayoutDescriptor parse(String descriptor) {
1821 int pos = descriptor.indexOf('/');
1822 if (pos < 0 || pos + 1 == descriptor.length()) {
1823 return null;
1824 }
1825 int pos2 = descriptor.indexOf('/', pos + 1);
1826 if (pos2 < pos + 2 || pos2 + 1 == descriptor.length()) {
1827 return null;
1828 }
1829
1830 KeyboardLayoutDescriptor result = new KeyboardLayoutDescriptor();
1831 result.packageName = descriptor.substring(0, pos);
1832 result.receiverName = descriptor.substring(pos + 1, pos2);
1833 result.keyboardLayoutName = descriptor.substring(pos2 + 1);
1834 return result;
1835 }
1836 }
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001837
Jeff Brown6ec6f792012-04-17 16:52:41 -07001838 private interface KeyboardLayoutVisitor {
Michael Wright8ebac232014-09-18 18:29:49 -07001839 void visitKeyboardLayout(Resources resources, String descriptor, String label,
1840 String collection, int keyboardLayoutResId, int priority);
Jeff Brown6ec6f792012-04-17 16:52:41 -07001841 }
1842
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001843 private final class InputDevicesChangedListenerRecord implements DeathRecipient {
1844 private final int mPid;
1845 private final IInputDevicesChangedListener mListener;
1846
1847 public InputDevicesChangedListenerRecord(int pid, IInputDevicesChangedListener listener) {
1848 mPid = pid;
1849 mListener = listener;
1850 }
1851
1852 @Override
1853 public void binderDied() {
1854 if (DEBUG) {
1855 Slog.d(TAG, "Input devices changed listener for pid " + mPid + " died.");
1856 }
1857 onInputDevicesChangedListenerDied(mPid);
1858 }
1859
1860 public void notifyInputDevicesChanged(int[] info) {
1861 try {
1862 mListener.onInputDevicesChanged(info);
1863 } catch (RemoteException ex) {
1864 Slog.w(TAG, "Failed to notify process "
1865 + mPid + " that input devices changed, assuming it died.", ex);
1866 binderDied();
1867 }
1868 }
1869 }
Jeff Browna47425a2012-04-13 04:09:27 -07001870
Michael Wright39e5e942015-08-19 22:52:47 +01001871 private final class TabletModeChangedListenerRecord implements DeathRecipient {
1872 private final int mPid;
1873 private final ITabletModeChangedListener mListener;
1874
1875 public TabletModeChangedListenerRecord(int pid, ITabletModeChangedListener listener) {
1876 mPid = pid;
1877 mListener = listener;
1878 }
1879
1880 @Override
1881 public void binderDied() {
1882 if (DEBUG) {
1883 Slog.d(TAG, "Tablet mode changed listener for pid " + mPid + " died.");
1884 }
1885 onTabletModeChangedListenerDied(mPid);
1886 }
1887
1888 public void notifyTabletModeChanged(long whenNanos, boolean inTabletMode) {
1889 try {
1890 mListener.onTabletModeChanged(whenNanos, inTabletMode);
1891 } catch (RemoteException ex) {
1892 Slog.w(TAG, "Failed to notify process " + mPid +
1893 " that tablet mode changed, assuming it died.", ex);
1894 binderDied();
1895 }
1896 }
1897 }
1898
Jeff Browna47425a2012-04-13 04:09:27 -07001899 private final class VibratorToken implements DeathRecipient {
1900 public final int mDeviceId;
1901 public final IBinder mToken;
1902 public final int mTokenValue;
1903
1904 public boolean mVibrating;
1905
1906 public VibratorToken(int deviceId, IBinder token, int tokenValue) {
1907 mDeviceId = deviceId;
1908 mToken = token;
1909 mTokenValue = tokenValue;
1910 }
1911
1912 @Override
1913 public void binderDied() {
1914 if (DEBUG) {
1915 Slog.d(TAG, "Vibrator token died.");
1916 }
1917 onVibratorTokenDied(this);
1918 }
1919 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001920
1921 private final class LocalService extends InputManagerInternal {
1922 @Override
1923 public void setDisplayViewports(
1924 DisplayViewport defaultViewport, DisplayViewport externalTouchViewport) {
1925 setDisplayViewportsInternal(defaultViewport, externalTouchViewport);
1926 }
Jeff Brownca9bc702014-02-11 14:32:56 -08001927
1928 @Override
1929 public boolean injectInputEvent(InputEvent event, int displayId, int mode) {
1930 return injectInputEventInternal(event, displayId, mode);
1931 }
Jeff Brown037c33e2014-04-09 00:31:55 -07001932
1933 @Override
1934 public void setInteractive(boolean interactive) {
1935 nativeSetInteractive(mPtr, interactive);
1936 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001937 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001938}