blob: bc946b5947da9d1b64aad5e1ca3cc13fd4a814d6 [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 Browncf39bdf2012-05-18 14:41:19 -070019import com.android.internal.R;
Jeff Brown46b9ac02010-04-22 18:58:52 -070020import com.android.internal.util.XmlUtils;
Jeff Brown89ef0722011-08-10 16:25:21 -070021import com.android.server.Watchdog;
Jeff Brown46b9ac02010-04-22 18:58:52 -070022
23import org.xmlpull.v1.XmlPullParser;
Jeff Browna3bc5652012-04-17 11:42:25 -070024import org.xmlpull.v1.XmlPullParserException;
25import org.xmlpull.v1.XmlSerializer;
Jeff Brown46b9ac02010-04-22 18:58:52 -070026
Jeff Browna3bc5652012-04-17 11:42:25 -070027import android.Manifest;
Jeff Browncf39bdf2012-05-18 14:41:19 -070028import android.app.Notification;
29import android.app.NotificationManager;
30import android.app.PendingIntent;
Jeff Brown5bbd4b42012-04-20 19:28:00 -070031import android.bluetooth.BluetoothAdapter;
32import android.bluetooth.BluetoothDevice;
Jeff Brown6ec6f792012-04-17 16:52:41 -070033import android.content.BroadcastReceiver;
Jeff Brown9f25b7f2012-04-10 14:30:49 -070034import android.content.ComponentName;
Jeff Brown46b9ac02010-04-22 18:58:52 -070035import android.content.Context;
Jeff Brown9f25b7f2012-04-10 14:30:49 -070036import android.content.Intent;
Jeff Brown6ec6f792012-04-17 16:52:41 -070037import android.content.IntentFilter;
Jeff Brown9f25b7f2012-04-10 14:30:49 -070038import android.content.pm.ActivityInfo;
Jeff Brown349703e2010-06-22 01:27:15 -070039import android.content.pm.PackageManager;
Jeff Brown9f25b7f2012-04-10 14:30:49 -070040import android.content.pm.ResolveInfo;
41import android.content.pm.PackageManager.NameNotFoundException;
Jeff Brown9f25b7f2012-04-10 14:30:49 -070042import android.content.res.Resources;
Jeff Brown6ec6f792012-04-17 16:52:41 -070043import android.content.res.Resources.NotFoundException;
Jeff Brown9f25b7f2012-04-10 14:30:49 -070044import android.content.res.TypedArray;
45import android.content.res.XmlResourceParser;
Jeff Brown1a84fd12011-06-02 01:26:32 -070046import android.database.ContentObserver;
Jeff Brown4532e612012-04-05 14:27:12 -070047import android.hardware.input.IInputManager;
Jeff Brownaf9e8d32012-04-12 17:32:48 -070048import android.hardware.input.IInputDevicesChangedListener;
Jeff Brownac143512012-04-05 18:57:33 -070049import android.hardware.input.InputManager;
Jeff Brown9f25b7f2012-04-10 14:30:49 -070050import android.hardware.input.KeyboardLayout;
Jeff Brown4532e612012-04-05 14:27:12 -070051import android.os.Binder;
Jeff Brown9f25b7f2012-04-10 14:30:49 -070052import android.os.Bundle;
Jeff Brown46b9ac02010-04-22 18:58:52 -070053import android.os.Environment;
Jeff Brown4532e612012-04-05 14:27:12 -070054import android.os.Handler;
Jeff Brownaf9e8d32012-04-12 17:32:48 -070055import android.os.IBinder;
56import android.os.Message;
Jeff Brown05dc66a2011-03-02 14:41:58 -080057import android.os.MessageQueue;
Jeff Brownac143512012-04-05 18:57:33 -070058import android.os.Process;
Jeff Brownaf9e8d32012-04-12 17:32:48 -070059import android.os.RemoteException;
Jeff Brown1a84fd12011-06-02 01:26:32 -070060import android.provider.Settings;
61import android.provider.Settings.SettingNotFoundException;
Jeff Brown5bbd4b42012-04-20 19:28:00 -070062import android.server.BluetoothService;
Jeff Brown9f25b7f2012-04-10 14:30:49 -070063import android.util.Log;
Jeff Brown46b9ac02010-04-22 18:58:52 -070064import android.util.Slog;
Jeff Brownaf9e8d32012-04-12 17:32:48 -070065import android.util.SparseArray;
Jeff Brown46b9ac02010-04-22 18:58:52 -070066import android.util.Xml;
67import android.view.InputChannel;
Jeff Brown8d608662010-08-30 03:02:23 -070068import android.view.InputDevice;
Jeff Brown6ec402b2010-07-28 15:48:59 -070069import android.view.InputEvent;
Jeff Brown1f245102010-11-18 20:53:46 -080070import android.view.KeyEvent;
Jeff Brown2352b972011-04-12 22:39:53 -070071import android.view.PointerIcon;
Jeff Brown46b9ac02010-04-22 18:58:52 -070072import android.view.Surface;
Jeff Browna4547672011-03-02 21:38:11 -080073import android.view.ViewConfiguration;
Jeff Brown0029c662011-03-30 02:25:18 -070074import android.view.WindowManagerPolicy;
Jeff Browncf39bdf2012-05-18 14:41:19 -070075import android.widget.Toast;
Jeff Brown46b9ac02010-04-22 18:58:52 -070076
Jeff Brown46b9ac02010-04-22 18:58:52 -070077import java.io.File;
Jeff Brown4532e612012-04-05 14:27:12 -070078import java.io.FileDescriptor;
Jeff Brown46b9ac02010-04-22 18:58:52 -070079import java.io.FileNotFoundException;
80import java.io.FileReader;
81import java.io.IOException;
Jeff Brown6ec6f792012-04-17 16:52:41 -070082import java.io.InputStreamReader;
Jeff Brown46b9ac02010-04-22 18:58:52 -070083import java.io.PrintWriter;
84import java.util.ArrayList;
Jeff Browncf39bdf2012-05-18 14:41:19 -070085import java.util.Collections;
Jeff Brown9f25b7f2012-04-10 14:30:49 -070086import java.util.HashMap;
Jeff Browncf39bdf2012-05-18 14:41:19 -070087import java.util.HashSet;
Jeff Browna3bc5652012-04-17 11:42:25 -070088
Jeff Brown6ec6f792012-04-17 16:52:41 -070089import libcore.io.Streams;
Jeff Browna3bc5652012-04-17 11:42:25 -070090import libcore.util.Objects;
Jeff Brown46b9ac02010-04-22 18:58:52 -070091
92/*
93 * Wraps the C++ InputManager and provides its callbacks.
Jeff Brown46b9ac02010-04-22 18:58:52 -070094 */
Jeff Brown4532e612012-04-05 14:27:12 -070095public class InputManagerService extends IInputManager.Stub implements Watchdog.Monitor {
Jeff Brown46b9ac02010-04-22 18:58:52 -070096 static final String TAG = "InputManager";
Jeff Brown6ec6f792012-04-17 16:52:41 -070097 static final boolean DEBUG = true;
Jeff Brownb6997262010-10-08 22:31:17 -070098
Jeff Brown4532e612012-04-05 14:27:12 -070099 private static final String EXCLUDED_DEVICES_PATH = "etc/excluded-input-devices.xml";
100
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700101 private static final int MSG_DELIVER_INPUT_DEVICES_CHANGED = 1;
Jeff Browncf39bdf2012-05-18 14:41:19 -0700102 private static final int MSG_SWITCH_KEYBOARD_LAYOUT = 2;
103 private static final int MSG_RELOAD_KEYBOARD_LAYOUTS = 3;
104 private static final int MSG_UPDATE_KEYBOARD_LAYOUTS = 4;
105 private static final int MSG_RELOAD_DEVICE_ALIASES = 5;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700106
Jeff Brown4532e612012-04-05 14:27:12 -0700107 // Pointer to native input manager service object.
108 private final int mPtr;
109
Jeff Brown46b9ac02010-04-22 18:58:52 -0700110 private final Context mContext;
Jeff Brown4532e612012-04-05 14:27:12 -0700111 private final Callbacks mCallbacks;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700112 private final InputManagerHandler mHandler;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700113 private boolean mSystemReady;
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700114 private BluetoothService mBluetoothService;
Jeff Browncf39bdf2012-05-18 14:41:19 -0700115 private NotificationManager mNotificationManager;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700116
Jeff Browna3bc5652012-04-17 11:42:25 -0700117 // Persistent data store. Must be locked each time during use.
118 private final PersistentDataStore mDataStore = new PersistentDataStore();
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700119
120 // List of currently registered input devices changed listeners by process id.
121 private Object mInputDevicesLock = new Object();
122 private boolean mInputDevicesChangedPending; // guarded by mInputDevicesLock
123 private InputDevice[] mInputDevices = new InputDevice[0];
124 private final SparseArray<InputDevicesChangedListenerRecord> mInputDevicesChangedListeners =
125 new SparseArray<InputDevicesChangedListenerRecord>(); // guarded by mInputDevicesLock
126 private final ArrayList<InputDevicesChangedListenerRecord>
127 mTempInputDevicesChangedListenersToNotify =
128 new ArrayList<InputDevicesChangedListenerRecord>(); // handler thread only
Jeff Browncf39bdf2012-05-18 14:41:19 -0700129 private final ArrayList<InputDevice>
130 mTempFullKeyboards = new ArrayList<InputDevice>(); // handler thread only
131 private boolean mKeyboardLayoutNotificationShown;
132 private PendingIntent mKeyboardLayoutIntent;
133 private Toast mSwitchedKeyboardLayoutToast;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700134
Jeff Browna47425a2012-04-13 04:09:27 -0700135 // State for vibrator tokens.
136 private Object mVibratorLock = new Object();
137 private HashMap<IBinder, VibratorToken> mVibratorTokens =
138 new HashMap<IBinder, VibratorToken>();
139 private int mNextVibratorTokenValue;
140
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700141 // State for the currently installed input filter.
142 final Object mInputFilterLock = new Object();
143 InputFilter mInputFilter; // guarded by mInputFilterLock
144 InputFilterHost mInputFilterHost; // guarded by mInputFilterLock
Jeff Brown1a84fd12011-06-02 01:26:32 -0700145
Jeff Brown4532e612012-04-05 14:27:12 -0700146 private static native int nativeInit(InputManagerService service,
147 Context context, MessageQueue messageQueue);
148 private static native void nativeStart(int ptr);
149 private static native void nativeSetDisplaySize(int ptr, int displayId,
150 int width, int height, int externalWidth, int externalHeight);
Jeff Brown93de7462012-05-02 16:29:42 -0700151 private static native void nativeSetDisplayOrientation(int ptr, int displayId,
152 int rotation, int externalRotation);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700153
Jeff Brown4532e612012-04-05 14:27:12 -0700154 private static native int nativeGetScanCodeState(int ptr,
155 int deviceId, int sourceMask, int scanCode);
156 private static native int nativeGetKeyCodeState(int ptr,
157 int deviceId, int sourceMask, int keyCode);
158 private static native int nativeGetSwitchState(int ptr,
159 int deviceId, int sourceMask, int sw);
160 private static native boolean nativeHasKeys(int ptr,
161 int deviceId, int sourceMask, int[] keyCodes, boolean[] keyExists);
162 private static native void nativeRegisterInputChannel(int ptr, InputChannel inputChannel,
Jeff Brown928e0542011-01-10 11:17:36 -0800163 InputWindowHandle inputWindowHandle, boolean monitor);
Jeff Brown4532e612012-04-05 14:27:12 -0700164 private static native void nativeUnregisterInputChannel(int ptr, InputChannel inputChannel);
165 private static native void nativeSetInputFilterEnabled(int ptr, boolean enable);
166 private static native int nativeInjectInputEvent(int ptr, InputEvent event,
Jeff Brown0029c662011-03-30 02:25:18 -0700167 int injectorPid, int injectorUid, int syncMode, int timeoutMillis,
168 int policyFlags);
Jeff Brown4532e612012-04-05 14:27:12 -0700169 private static native void nativeSetInputWindows(int ptr, InputWindowHandle[] windowHandles);
170 private static native void nativeSetInputDispatchMode(int ptr, boolean enabled, boolean frozen);
171 private static native void nativeSetSystemUiVisibility(int ptr, int visibility);
172 private static native void nativeSetFocusedApplication(int ptr,
173 InputApplicationHandle application);
Jeff Brown4532e612012-04-05 14:27:12 -0700174 private static native boolean nativeTransferTouchFocus(int ptr,
175 InputChannel fromChannel, InputChannel toChannel);
176 private static native void nativeSetPointerSpeed(int ptr, int speed);
177 private static native void nativeSetShowTouches(int ptr, boolean enabled);
Jeff Browna47425a2012-04-13 04:09:27 -0700178 private static native void nativeVibrate(int ptr, int deviceId, long[] pattern,
179 int repeat, int token);
180 private static native void nativeCancelVibrate(int ptr, int deviceId, int token);
Jeff Brown6ec6f792012-04-17 16:52:41 -0700181 private static native void nativeReloadKeyboardLayouts(int ptr);
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700182 private static native void nativeReloadDeviceAliases(int ptr);
Jeff Brown4532e612012-04-05 14:27:12 -0700183 private static native String nativeDump(int ptr);
184 private static native void nativeMonitor(int ptr);
Jeff Brown4532e612012-04-05 14:27:12 -0700185
Jeff Brownac143512012-04-05 18:57:33 -0700186 // Input event injection constants defined in InputDispatcher.h.
187 private static final int INPUT_EVENT_INJECTION_SUCCEEDED = 0;
188 private static final int INPUT_EVENT_INJECTION_PERMISSION_DENIED = 1;
189 private static final int INPUT_EVENT_INJECTION_FAILED = 2;
190 private static final int INPUT_EVENT_INJECTION_TIMED_OUT = 3;
191
192 // Maximum number of milliseconds to wait for input event injection.
193 private static final int INJECTION_TIMEOUT_MILLIS = 30 * 1000;
194
Jeff Brown6d0fec22010-07-23 21:28:06 -0700195 // Key states (may be returned by queries about the current state of a
196 // particular key code, scan code or switch).
197
198 /** The key state is unknown or the requested key itself is not supported. */
199 public static final int KEY_STATE_UNKNOWN = -1;
200
201 /** The key is up. /*/
202 public static final int KEY_STATE_UP = 0;
203
204 /** The key is down. */
205 public static final int KEY_STATE_DOWN = 1;
206
207 /** The key is down but is a virtual key press that is being emulated by the system. */
208 public static final int KEY_STATE_VIRTUAL = 2;
209
Jeff Brownc458ce92012-04-30 14:58:40 -0700210 /** Scan code: Mouse / trackball button. */
211 public static final int BTN_MOUSE = 0x110;
212
213 /** Switch code: Lid switch. When set, lid is shut. */
214 public static final int SW_LID = 0x00;
215
216 /** Switch code: Keypad slide. When set, keyboard is exposed. */
217 public static final int SW_KEYPAD_SLIDE = 0x0a;
218
Jeff Brown4532e612012-04-05 14:27:12 -0700219 public InputManagerService(Context context, Callbacks callbacks) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700220 this.mContext = context;
Jeff Brown4532e612012-04-05 14:27:12 -0700221 this.mCallbacks = callbacks;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700222 this.mHandler = new InputManagerHandler();
Jeff Brown05dc66a2011-03-02 14:41:58 -0800223
Jeff Brown46b9ac02010-04-22 18:58:52 -0700224 Slog.i(TAG, "Initializing input manager");
Jeff Brown4532e612012-04-05 14:27:12 -0700225 mPtr = nativeInit(this, mContext, mHandler.getLooper().getQueue());
Jeff Brown46b9ac02010-04-22 18:58:52 -0700226 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700227
Jeff Brown46b9ac02010-04-22 18:58:52 -0700228 public void start() {
229 Slog.i(TAG, "Starting input manager");
Jeff Brown4532e612012-04-05 14:27:12 -0700230 nativeStart(mPtr);
231
232 // Add ourself to the Watchdog monitors.
233 Watchdog.getInstance().addMonitor(this);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700234
235 registerPointerSpeedSettingObserver();
Jeff Browndaf4a122011-08-26 17:14:14 -0700236 registerShowTouchesSettingObserver();
237
Jeff Brown1a84fd12011-06-02 01:26:32 -0700238 updatePointerSpeedFromSettings();
Jeff Browndaf4a122011-08-26 17:14:14 -0700239 updateShowTouchesFromSettings();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700240 }
Jeff Brown6ec6f792012-04-17 16:52:41 -0700241
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700242 public void systemReady(BluetoothService bluetoothService) {
Jeff Brown6ec6f792012-04-17 16:52:41 -0700243 if (DEBUG) {
244 Slog.d(TAG, "System ready.");
245 }
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700246 mBluetoothService = bluetoothService;
Jeff Browncf39bdf2012-05-18 14:41:19 -0700247 mNotificationManager = (NotificationManager)mContext.getSystemService(
248 Context.NOTIFICATION_SERVICE);
Jeff Brown6ec6f792012-04-17 16:52:41 -0700249 mSystemReady = true;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700250
251 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
252 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
253 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
254 filter.addDataScheme("package");
255 mContext.registerReceiver(new BroadcastReceiver() {
256 @Override
257 public void onReceive(Context context, Intent intent) {
Jeff Browncf39bdf2012-05-18 14:41:19 -0700258 updateKeyboardLayouts();
Jeff Brown6ec6f792012-04-17 16:52:41 -0700259 }
260 }, filter, null, mHandler);
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700261
262 filter = new IntentFilter(BluetoothDevice.ACTION_ALIAS_CHANGED);
263 mContext.registerReceiver(new BroadcastReceiver() {
264 @Override
265 public void onReceive(Context context, Intent intent) {
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700266 reloadDeviceAliases();
267 }
268 }, filter, null, mHandler);
269
Jeff Browncf39bdf2012-05-18 14:41:19 -0700270 mHandler.sendEmptyMessage(MSG_RELOAD_DEVICE_ALIASES);
271 mHandler.sendEmptyMessage(MSG_UPDATE_KEYBOARD_LAYOUTS);
Jeff Brown6ec6f792012-04-17 16:52:41 -0700272 }
273
274 private void reloadKeyboardLayouts() {
Jeff Browncf39bdf2012-05-18 14:41:19 -0700275 if (DEBUG) {
276 Slog.d(TAG, "Reloading keyboard layouts.");
277 }
Jeff Brown6ec6f792012-04-17 16:52:41 -0700278 nativeReloadKeyboardLayouts(mPtr);
279 }
280
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700281 private void reloadDeviceAliases() {
Jeff Browncf39bdf2012-05-18 14:41:19 -0700282 if (DEBUG) {
283 Slog.d(TAG, "Reloading device names.");
284 }
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700285 nativeReloadDeviceAliases(mPtr);
286 }
287
Jeff Brownbc68a592011-07-25 12:58:12 -0700288 public void setDisplaySize(int displayId, int width, int height,
289 int externalWidth, int externalHeight) {
290 if (width <= 0 || height <= 0 || externalWidth <= 0 || externalHeight <= 0) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700291 throw new IllegalArgumentException("Invalid display id or dimensions.");
292 }
293
Jeff Brownb6997262010-10-08 22:31:17 -0700294 if (DEBUG) {
Jeff Brownbc68a592011-07-25 12:58:12 -0700295 Slog.d(TAG, "Setting display #" + displayId + " size to " + width + "x" + height
296 + " external size " + externalWidth + "x" + externalHeight);
Jeff Brownb6997262010-10-08 22:31:17 -0700297 }
Jeff Brown4532e612012-04-05 14:27:12 -0700298 nativeSetDisplaySize(mPtr, displayId, width, height, externalWidth, externalHeight);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700299 }
300
Jeff Brown93de7462012-05-02 16:29:42 -0700301 public void setDisplayOrientation(int displayId, int rotation, int externalRotation) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700302 if (rotation < Surface.ROTATION_0 || rotation > Surface.ROTATION_270) {
303 throw new IllegalArgumentException("Invalid rotation.");
304 }
305
Jeff Brownb6997262010-10-08 22:31:17 -0700306 if (DEBUG) {
Jeff Brown93de7462012-05-02 16:29:42 -0700307 Slog.d(TAG, "Setting display #" + displayId + " orientation to rotation " + rotation
308 + " external rotation " + externalRotation);
Jeff Brownb6997262010-10-08 22:31:17 -0700309 }
Jeff Brown93de7462012-05-02 16:29:42 -0700310 nativeSetDisplayOrientation(mPtr, displayId, rotation, externalRotation);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700311 }
Jeff Brownac143512012-04-05 18:57:33 -0700312
Jeff Brown6d0fec22010-07-23 21:28:06 -0700313 /**
314 * Gets the current state of a key or button by key code.
315 * @param deviceId The input device id, or -1 to consult all devices.
316 * @param sourceMask The input sources to consult, or {@link InputDevice#SOURCE_ANY} to
317 * consider all input sources. An input device is consulted if at least one of its
318 * non-class input source bits matches the specified source mask.
319 * @param keyCode The key code to check.
320 * @return The key state.
321 */
322 public int getKeyCodeState(int deviceId, int sourceMask, int keyCode) {
Jeff Brown4532e612012-04-05 14:27:12 -0700323 return nativeGetKeyCodeState(mPtr, deviceId, sourceMask, keyCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700324 }
325
Jeff Brown6d0fec22010-07-23 21:28:06 -0700326 /**
327 * Gets the current state of a key or button by scan code.
328 * @param deviceId The input device id, or -1 to consult all devices.
329 * @param sourceMask The input sources to consult, or {@link InputDevice#SOURCE_ANY} to
330 * consider all input sources. An input device is consulted if at least one of its
331 * non-class input source bits matches the specified source mask.
332 * @param scanCode The scan code to check.
333 * @return The key state.
334 */
335 public int getScanCodeState(int deviceId, int sourceMask, int scanCode) {
Jeff Brown4532e612012-04-05 14:27:12 -0700336 return nativeGetScanCodeState(mPtr, deviceId, sourceMask, scanCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700337 }
338
Jeff Brown6d0fec22010-07-23 21:28:06 -0700339 /**
340 * Gets the current state of a switch by switch code.
341 * @param deviceId The input device id, or -1 to consult all devices.
342 * @param sourceMask The input sources to consult, or {@link InputDevice#SOURCE_ANY} to
343 * consider all input sources. An input device is consulted if at least one of its
344 * non-class input source bits matches the specified source mask.
345 * @param switchCode The switch code to check.
346 * @return The switch state.
347 */
348 public int getSwitchState(int deviceId, int sourceMask, int switchCode) {
Jeff Brown4532e612012-04-05 14:27:12 -0700349 return nativeGetSwitchState(mPtr, deviceId, sourceMask, switchCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700350 }
351
Jeff Brown6d0fec22010-07-23 21:28:06 -0700352 /**
353 * Determines whether the specified key codes are supported by a particular device.
354 * @param deviceId The input device id, or -1 to consult all devices.
355 * @param sourceMask The input sources to consult, or {@link InputDevice#SOURCE_ANY} to
356 * consider all input sources. An input device is consulted if at least one of its
357 * non-class input source bits matches the specified source mask.
358 * @param keyCodes The array of key codes to check.
359 * @param keyExists An array at least as large as keyCodes whose entries will be set
360 * to true or false based on the presence or absence of support for the corresponding
361 * key codes.
362 * @return True if the lookup was successful, false otherwise.
363 */
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700364 @Override // Binder call
Jeff Brown6d0fec22010-07-23 21:28:06 -0700365 public boolean hasKeys(int deviceId, int sourceMask, int[] keyCodes, boolean[] keyExists) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700366 if (keyCodes == null) {
367 throw new IllegalArgumentException("keyCodes must not be null.");
368 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700369 if (keyExists == null || keyExists.length < keyCodes.length) {
370 throw new IllegalArgumentException("keyExists must not be null and must be at "
371 + "least as large as keyCodes.");
Jeff Brown46b9ac02010-04-22 18:58:52 -0700372 }
373
Jeff Brown4532e612012-04-05 14:27:12 -0700374 return nativeHasKeys(mPtr, deviceId, sourceMask, keyCodes, keyExists);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700375 }
376
Jeff Browna41ca772010-08-11 14:46:32 -0700377 /**
378 * Creates an input channel that will receive all input from the input dispatcher.
379 * @param inputChannelName The input channel name.
380 * @return The input channel.
381 */
382 public InputChannel monitorInput(String inputChannelName) {
383 if (inputChannelName == null) {
384 throw new IllegalArgumentException("inputChannelName must not be null.");
385 }
386
387 InputChannel[] inputChannels = InputChannel.openInputChannelPair(inputChannelName);
Jeff Brown4532e612012-04-05 14:27:12 -0700388 nativeRegisterInputChannel(mPtr, inputChannels[0], null, true);
Jeff Browna41ca772010-08-11 14:46:32 -0700389 inputChannels[0].dispose(); // don't need to retain the Java object reference
390 return inputChannels[1];
391 }
392
393 /**
394 * Registers an input channel so that it can be used as an input event target.
395 * @param inputChannel The input channel to register.
Jeff Brown928e0542011-01-10 11:17:36 -0800396 * @param inputWindowHandle The handle of the input window associated with the
397 * input channel, or null if none.
Jeff Browna41ca772010-08-11 14:46:32 -0700398 */
Jeff Brown928e0542011-01-10 11:17:36 -0800399 public void registerInputChannel(InputChannel inputChannel,
400 InputWindowHandle inputWindowHandle) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700401 if (inputChannel == null) {
402 throw new IllegalArgumentException("inputChannel must not be null.");
403 }
404
Jeff Brown4532e612012-04-05 14:27:12 -0700405 nativeRegisterInputChannel(mPtr, inputChannel, inputWindowHandle, false);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700406 }
407
Jeff Browna41ca772010-08-11 14:46:32 -0700408 /**
409 * Unregisters an input channel.
410 * @param inputChannel The input channel to unregister.
411 */
Jeff Brown46b9ac02010-04-22 18:58:52 -0700412 public void unregisterInputChannel(InputChannel inputChannel) {
413 if (inputChannel == null) {
414 throw new IllegalArgumentException("inputChannel must not be null.");
415 }
416
Jeff Brown4532e612012-04-05 14:27:12 -0700417 nativeUnregisterInputChannel(mPtr, inputChannel);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700418 }
Jeff Brown0029c662011-03-30 02:25:18 -0700419
420 /**
421 * Sets an input filter that will receive all input events before they are dispatched.
422 * The input filter may then reinterpret input events or inject new ones.
423 *
424 * To ensure consistency, the input dispatcher automatically drops all events
425 * in progress whenever an input filter is installed or uninstalled. After an input
426 * filter is uninstalled, it can no longer send input events unless it is reinstalled.
427 * Any events it attempts to send after it has been uninstalled will be dropped.
428 *
429 * @param filter The input filter, or null to remove the current filter.
430 */
431 public void setInputFilter(InputFilter filter) {
432 synchronized (mInputFilterLock) {
433 final InputFilter oldFilter = mInputFilter;
434 if (oldFilter == filter) {
435 return; // nothing to do
436 }
437
438 if (oldFilter != null) {
439 mInputFilter = null;
440 mInputFilterHost.disconnectLocked();
441 mInputFilterHost = null;
442 oldFilter.uninstall();
443 }
444
445 if (filter != null) {
446 mInputFilter = filter;
447 mInputFilterHost = new InputFilterHost();
448 filter.install(mInputFilterHost);
449 }
450
Jeff Brown4532e612012-04-05 14:27:12 -0700451 nativeSetInputFilterEnabled(mPtr, filter != null);
Jeff Brown0029c662011-03-30 02:25:18 -0700452 }
453 }
454
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700455 @Override // Binder call
Jeff Brownac143512012-04-05 18:57:33 -0700456 public boolean injectInputEvent(InputEvent event, int mode) {
Jeff Brown7fbdc842010-06-17 20:52:56 -0700457 if (event == null) {
458 throw new IllegalArgumentException("event must not be null");
459 }
Jeff Brownac143512012-04-05 18:57:33 -0700460 if (mode != InputManager.INJECT_INPUT_EVENT_MODE_ASYNC
461 && mode != InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH
462 && mode != InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_RESULT) {
463 throw new IllegalArgumentException("mode is invalid");
Jeff Brown7fbdc842010-06-17 20:52:56 -0700464 }
Jeff Brown6ec402b2010-07-28 15:48:59 -0700465
Jeff Brownac143512012-04-05 18:57:33 -0700466 final int pid = Binder.getCallingPid();
467 final int uid = Binder.getCallingUid();
468 final long ident = Binder.clearCallingIdentity();
469 final int result;
470 try {
471 result = nativeInjectInputEvent(mPtr, event, pid, uid, mode,
472 INJECTION_TIMEOUT_MILLIS, WindowManagerPolicy.FLAG_DISABLE_KEY_REPEAT);
473 } finally {
474 Binder.restoreCallingIdentity(ident);
475 }
476 switch (result) {
477 case INPUT_EVENT_INJECTION_PERMISSION_DENIED:
478 Slog.w(TAG, "Input event injection from pid " + pid + " permission denied.");
479 throw new SecurityException(
480 "Injecting to another application requires INJECT_EVENTS permission");
481 case INPUT_EVENT_INJECTION_SUCCEEDED:
482 return true;
483 case INPUT_EVENT_INJECTION_TIMED_OUT:
484 Slog.w(TAG, "Input event injection from pid " + pid + " timed out.");
485 return false;
486 case INPUT_EVENT_INJECTION_FAILED:
487 default:
488 Slog.w(TAG, "Input event injection from pid " + pid + " failed.");
489 return false;
490 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700491 }
Jeff Brown0029c662011-03-30 02:25:18 -0700492
Jeff Brown8d608662010-08-30 03:02:23 -0700493 /**
494 * Gets information about the input device with the specified id.
495 * @param id The device id.
496 * @return The input device or null if not found.
497 */
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700498 @Override // Binder call
Jeff Brown8d608662010-08-30 03:02:23 -0700499 public InputDevice getInputDevice(int deviceId) {
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700500 synchronized (mInputDevicesLock) {
501 final int count = mInputDevices.length;
502 for (int i = 0; i < count; i++) {
503 final InputDevice inputDevice = mInputDevices[i];
504 if (inputDevice.getId() == deviceId) {
505 return inputDevice;
506 }
507 }
508 }
509 return null;
Jeff Brown8d608662010-08-30 03:02:23 -0700510 }
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700511
Jeff Brown8d608662010-08-30 03:02:23 -0700512 /**
513 * Gets the ids of all input devices in the system.
514 * @return The input device ids.
515 */
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700516 @Override // Binder call
Jeff Brown8d608662010-08-30 03:02:23 -0700517 public int[] getInputDeviceIds() {
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700518 synchronized (mInputDevicesLock) {
519 final int count = mInputDevices.length;
520 int[] ids = new int[count];
521 for (int i = 0; i < count; i++) {
522 ids[i] = mInputDevices[i].getId();
523 }
524 return ids;
525 }
526 }
527
Jeff Browndaa37532012-05-01 15:54:03 -0700528 /**
529 * Gets all input devices in the system.
530 * @return The array of input devices.
531 */
532 public InputDevice[] getInputDevices() {
533 synchronized (mInputDevicesLock) {
534 return mInputDevices;
535 }
536 }
537
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700538 @Override // Binder call
539 public void registerInputDevicesChangedListener(IInputDevicesChangedListener listener) {
540 if (listener == null) {
541 throw new IllegalArgumentException("listener must not be null");
542 }
543
544 synchronized (mInputDevicesLock) {
545 int callingPid = Binder.getCallingPid();
546 if (mInputDevicesChangedListeners.get(callingPid) != null) {
547 throw new SecurityException("The calling process has already "
548 + "registered an InputDevicesChangedListener.");
549 }
550
551 InputDevicesChangedListenerRecord record =
552 new InputDevicesChangedListenerRecord(callingPid, listener);
553 try {
554 IBinder binder = listener.asBinder();
555 binder.linkToDeath(record, 0);
556 } catch (RemoteException ex) {
557 // give up
558 throw new RuntimeException(ex);
559 }
560
561 mInputDevicesChangedListeners.put(callingPid, record);
562 }
563 }
564
565 private void onInputDevicesChangedListenerDied(int pid) {
566 synchronized (mInputDevicesLock) {
567 mInputDevicesChangedListeners.remove(pid);
568 }
569 }
570
571 // Must be called on handler.
Jeff Browncf39bdf2012-05-18 14:41:19 -0700572 private void deliverInputDevicesChanged(InputDevice[] oldInputDevices) {
573 // Scan for changes.
574 int numFullKeyboardsAdded = 0;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700575 mTempInputDevicesChangedListenersToNotify.clear();
Jeff Browncf39bdf2012-05-18 14:41:19 -0700576 mTempFullKeyboards.clear();
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700577 final int numListeners;
578 final int[] deviceIdAndGeneration;
579 synchronized (mInputDevicesLock) {
580 if (!mInputDevicesChangedPending) {
581 return;
582 }
583 mInputDevicesChangedPending = false;
584
585 numListeners = mInputDevicesChangedListeners.size();
586 for (int i = 0; i < numListeners; i++) {
587 mTempInputDevicesChangedListenersToNotify.add(
588 mInputDevicesChangedListeners.valueAt(i));
589 }
590
591 final int numDevices = mInputDevices.length;
592 deviceIdAndGeneration = new int[numDevices * 2];
593 for (int i = 0; i < numDevices; i++) {
594 final InputDevice inputDevice = mInputDevices[i];
595 deviceIdAndGeneration[i * 2] = inputDevice.getId();
596 deviceIdAndGeneration[i * 2 + 1] = inputDevice.getGeneration();
Jeff Browncf39bdf2012-05-18 14:41:19 -0700597
598 if (isFullKeyboard(inputDevice)) {
599 if (!containsInputDeviceWithDescriptor(oldInputDevices,
600 inputDevice.getDescriptor())) {
601 mTempFullKeyboards.add(numFullKeyboardsAdded++, inputDevice);
602 } else {
603 mTempFullKeyboards.add(inputDevice);
604 }
605 }
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700606 }
607 }
608
Jeff Browncf39bdf2012-05-18 14:41:19 -0700609 // Notify listeners.
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700610 for (int i = 0; i < numListeners; i++) {
611 mTempInputDevicesChangedListenersToNotify.get(i).notifyInputDevicesChanged(
612 deviceIdAndGeneration);
613 }
Jeff Browncf39bdf2012-05-18 14:41:19 -0700614 mTempInputDevicesChangedListenersToNotify.clear();
615
616 // Check for missing keyboard layouts.
617 if (mNotificationManager != null) {
618 final int numFullKeyboards = mTempFullKeyboards.size();
619 boolean missingLayoutForExternalKeyboard = false;
620 boolean missingLayoutForExternalKeyboardAdded = false;
621 synchronized (mDataStore) {
622 for (int i = 0; i < numFullKeyboards; i++) {
623 final InputDevice inputDevice = mTempFullKeyboards.get(i);
624 if (mDataStore.getCurrentKeyboardLayout(inputDevice.getDescriptor()) == null) {
625 missingLayoutForExternalKeyboard = true;
626 if (i < numFullKeyboardsAdded) {
627 missingLayoutForExternalKeyboardAdded = true;
628 }
629 }
630 }
631 }
632 if (missingLayoutForExternalKeyboard) {
633 if (missingLayoutForExternalKeyboardAdded) {
634 showMissingKeyboardLayoutNotification();
635 }
636 } else if (mKeyboardLayoutNotificationShown) {
637 hideMissingKeyboardLayoutNotification();
638 }
639 }
640 mTempFullKeyboards.clear();
641 }
642
643 // Must be called on handler.
644 private void showMissingKeyboardLayoutNotification() {
645 if (!mKeyboardLayoutNotificationShown) {
646 if (mKeyboardLayoutIntent == null) {
647 final Intent intent = new Intent("android.settings.INPUT_METHOD_SETTINGS");
648 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
649 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
650 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
651 mKeyboardLayoutIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
652 }
653
654 Resources r = mContext.getResources();
655 Notification notification = new Notification.Builder(mContext)
656 .setContentTitle(r.getString(
657 R.string.select_keyboard_layout_notification_title))
658 .setContentText(r.getString(
659 R.string.select_keyboard_layout_notification_message))
660 .setContentIntent(mKeyboardLayoutIntent)
661 .setSmallIcon(R.drawable.ic_settings_language)
662 .setPriority(Notification.PRIORITY_LOW)
663 .build();
664 mNotificationManager.notify(R.string.select_keyboard_layout_notification_title,
665 notification);
666 mKeyboardLayoutNotificationShown = true;
667 }
668 }
669
670 // Must be called on handler.
671 private void hideMissingKeyboardLayoutNotification() {
672 if (mKeyboardLayoutNotificationShown) {
673 mKeyboardLayoutNotificationShown = false;
674 mNotificationManager.cancel(R.string.select_keyboard_layout_notification_title);
675 }
676 }
677
678 // Must be called on handler.
679 private void updateKeyboardLayouts() {
680 // Scan all input devices state for keyboard layouts that have been uninstalled.
681 final HashSet<String> availableKeyboardLayouts = new HashSet<String>();
682 visitAllKeyboardLayouts(new KeyboardLayoutVisitor() {
683 @Override
684 public void visitKeyboardLayout(Resources resources,
685 String descriptor, String label, String collection, int keyboardLayoutResId) {
686 availableKeyboardLayouts.add(descriptor);
687 }
688 });
689 synchronized (mDataStore) {
690 try {
691 mDataStore.removeUninstalledKeyboardLayouts(availableKeyboardLayouts);
692 } finally {
693 mDataStore.saveIfNeeded();
694 }
695 }
696
697 // Reload keyboard layouts.
698 reloadKeyboardLayouts();
699 }
700
701 private static boolean isFullKeyboard(InputDevice inputDevice) {
702 return !inputDevice.isVirtual()
703 && (inputDevice.getSources() & InputDevice.SOURCE_KEYBOARD) != 0
704 && inputDevice.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC;
705 }
706
707 private static boolean containsInputDeviceWithDescriptor(InputDevice[] inputDevices,
708 String descriptor) {
709 final int numDevices = inputDevices.length;
710 for (int i = 0; i < numDevices; i++) {
711 final InputDevice inputDevice = inputDevices[i];
712 if (inputDevice.getDescriptor().equals(descriptor)) {
713 return true;
714 }
715 }
716 return false;
Jeff Brown8d608662010-08-30 03:02:23 -0700717 }
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700718
719 @Override // Binder call
720 public KeyboardLayout[] getKeyboardLayouts() {
Jeff Brown6ec6f792012-04-17 16:52:41 -0700721 final ArrayList<KeyboardLayout> list = new ArrayList<KeyboardLayout>();
722 visitAllKeyboardLayouts(new KeyboardLayoutVisitor() {
723 @Override
724 public void visitKeyboardLayout(Resources resources,
Jeff Brownd9fec5d2012-05-17 16:01:54 -0700725 String descriptor, String label, String collection, int keyboardLayoutResId) {
726 list.add(new KeyboardLayout(descriptor, label, collection));
Jeff Brown6ec6f792012-04-17 16:52:41 -0700727 }
728 });
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700729 return list.toArray(new KeyboardLayout[list.size()]);
730 }
731
732 @Override // Binder call
733 public KeyboardLayout getKeyboardLayout(String keyboardLayoutDescriptor) {
734 if (keyboardLayoutDescriptor == null) {
735 throw new IllegalArgumentException("keyboardLayoutDescriptor must not be null");
736 }
737
Jeff Brown6ec6f792012-04-17 16:52:41 -0700738 final KeyboardLayout[] result = new KeyboardLayout[1];
739 visitKeyboardLayout(keyboardLayoutDescriptor, new KeyboardLayoutVisitor() {
740 @Override
741 public void visitKeyboardLayout(Resources resources,
Jeff Brownd9fec5d2012-05-17 16:01:54 -0700742 String descriptor, String label, String collection, int keyboardLayoutResId) {
743 result[0] = new KeyboardLayout(descriptor, label, collection);
Jeff Brown6ec6f792012-04-17 16:52:41 -0700744 }
745 });
746 if (result[0] == null) {
747 Log.w(TAG, "Could not get keyboard layout with descriptor '"
748 + keyboardLayoutDescriptor + "'.");
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700749 }
Jeff Brown6ec6f792012-04-17 16:52:41 -0700750 return result[0];
751 }
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700752
Jeff Brown6ec6f792012-04-17 16:52:41 -0700753 private void visitAllKeyboardLayouts(KeyboardLayoutVisitor visitor) {
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700754 final PackageManager pm = mContext.getPackageManager();
Jeff Brown6ec6f792012-04-17 16:52:41 -0700755 Intent intent = new Intent(InputManager.ACTION_QUERY_KEYBOARD_LAYOUTS);
756 for (ResolveInfo resolveInfo : pm.queryBroadcastReceivers(intent,
757 PackageManager.GET_META_DATA)) {
758 visitKeyboardLayoutsInPackage(pm, resolveInfo.activityInfo, null, visitor);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700759 }
760 }
761
Jeff Brown6ec6f792012-04-17 16:52:41 -0700762 private void visitKeyboardLayout(String keyboardLayoutDescriptor,
763 KeyboardLayoutVisitor visitor) {
764 KeyboardLayoutDescriptor d = KeyboardLayoutDescriptor.parse(keyboardLayoutDescriptor);
765 if (d != null) {
766 final PackageManager pm = mContext.getPackageManager();
767 try {
768 ActivityInfo receiver = pm.getReceiverInfo(
769 new ComponentName(d.packageName, d.receiverName),
770 PackageManager.GET_META_DATA);
771 visitKeyboardLayoutsInPackage(pm, receiver, d.keyboardLayoutName, visitor);
772 } catch (NameNotFoundException ex) {
773 }
774 }
775 }
776
777 private void visitKeyboardLayoutsInPackage(PackageManager pm, ActivityInfo receiver,
778 String keyboardName, KeyboardLayoutVisitor visitor) {
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700779 Bundle metaData = receiver.metaData;
780 if (metaData == null) {
Jeff Brown6ec6f792012-04-17 16:52:41 -0700781 return;
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700782 }
783
784 int configResId = metaData.getInt(InputManager.META_DATA_KEYBOARD_LAYOUTS);
785 if (configResId == 0) {
786 Log.w(TAG, "Missing meta-data '" + InputManager.META_DATA_KEYBOARD_LAYOUTS
787 + "' on receiver " + receiver.packageName + "/" + receiver.name);
Jeff Brown6ec6f792012-04-17 16:52:41 -0700788 return;
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700789 }
790
Jeff Brownd9fec5d2012-05-17 16:01:54 -0700791 CharSequence receiverLabel = receiver.loadLabel(pm);
792 String collection = receiverLabel != null ? receiverLabel.toString() : "";
793
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700794 try {
795 Resources resources = pm.getResourcesForApplication(receiver.applicationInfo);
796 XmlResourceParser parser = resources.getXml(configResId);
797 try {
798 XmlUtils.beginDocument(parser, "keyboard-layouts");
799
800 for (;;) {
801 XmlUtils.nextElement(parser);
802 String element = parser.getName();
803 if (element == null) {
804 break;
805 }
806 if (element.equals("keyboard-layout")) {
807 TypedArray a = resources.obtainAttributes(
808 parser, com.android.internal.R.styleable.KeyboardLayout);
809 try {
810 String name = a.getString(
811 com.android.internal.R.styleable.KeyboardLayout_name);
812 String label = a.getString(
813 com.android.internal.R.styleable.KeyboardLayout_label);
Jeff Brown2f095762012-05-10 21:29:33 -0700814 int keyboardLayoutResId = a.getResourceId(
815 com.android.internal.R.styleable.KeyboardLayout_keyboardLayout,
816 0);
817 if (name == null || label == null || keyboardLayoutResId == 0) {
818 Log.w(TAG, "Missing required 'name', 'label' or 'keyboardLayout' "
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700819 + "attributes in keyboard layout "
820 + "resource from receiver "
821 + receiver.packageName + "/" + receiver.name);
822 } else {
823 String descriptor = KeyboardLayoutDescriptor.format(
824 receiver.packageName, receiver.name, name);
Jeff Brown6ec6f792012-04-17 16:52:41 -0700825 if (keyboardName == null || name.equals(keyboardName)) {
826 visitor.visitKeyboardLayout(resources, descriptor,
Jeff Brownd9fec5d2012-05-17 16:01:54 -0700827 label, collection, keyboardLayoutResId);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700828 }
829 }
830 } finally {
831 a.recycle();
832 }
833 } else {
834 Log.w(TAG, "Skipping unrecognized element '" + element
835 + "' in keyboard layout resource from receiver "
836 + receiver.packageName + "/" + receiver.name);
837 }
838 }
839 } finally {
840 parser.close();
841 }
842 } catch (Exception ex) {
Jeff Brown6ec6f792012-04-17 16:52:41 -0700843 Log.w(TAG, "Could not parse keyboard layout resource from receiver "
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700844 + receiver.packageName + "/" + receiver.name, ex);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700845 }
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700846 }
847
848 @Override // Binder call
Jeff Browncf39bdf2012-05-18 14:41:19 -0700849 public String getCurrentKeyboardLayoutForInputDevice(String inputDeviceDescriptor) {
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700850 if (inputDeviceDescriptor == null) {
851 throw new IllegalArgumentException("inputDeviceDescriptor must not be null");
852 }
853
Jeff Browna3bc5652012-04-17 11:42:25 -0700854 synchronized (mDataStore) {
Jeff Browncf39bdf2012-05-18 14:41:19 -0700855 return mDataStore.getCurrentKeyboardLayout(inputDeviceDescriptor);
Jeff Browna3bc5652012-04-17 11:42:25 -0700856 }
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700857 }
858
859 @Override // Binder call
Jeff Browncf39bdf2012-05-18 14:41:19 -0700860 public void setCurrentKeyboardLayoutForInputDevice(String inputDeviceDescriptor,
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700861 String keyboardLayoutDescriptor) {
862 if (!checkCallingPermission(android.Manifest.permission.SET_KEYBOARD_LAYOUT,
Jeff Browncf39bdf2012-05-18 14:41:19 -0700863 "setCurrentKeyboardLayoutForInputDevice()")) {
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700864 throw new SecurityException("Requires SET_KEYBOARD_LAYOUT permission");
865 }
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700866 if (inputDeviceDescriptor == null) {
867 throw new IllegalArgumentException("inputDeviceDescriptor must not be null");
868 }
Jeff Browncf39bdf2012-05-18 14:41:19 -0700869 if (keyboardLayoutDescriptor == null) {
870 throw new IllegalArgumentException("keyboardLayoutDescriptor must not be null");
871 }
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700872
Jeff Browna3bc5652012-04-17 11:42:25 -0700873 synchronized (mDataStore) {
874 try {
Jeff Browncf39bdf2012-05-18 14:41:19 -0700875 if (mDataStore.setCurrentKeyboardLayout(
876 inputDeviceDescriptor, keyboardLayoutDescriptor)) {
877 mHandler.sendEmptyMessage(MSG_RELOAD_KEYBOARD_LAYOUTS);
878 }
Jeff Browna3bc5652012-04-17 11:42:25 -0700879 } finally {
880 mDataStore.saveIfNeeded();
881 }
882 }
Jeff Browncf39bdf2012-05-18 14:41:19 -0700883 }
Jeff Brown6ec6f792012-04-17 16:52:41 -0700884
Jeff Browncf39bdf2012-05-18 14:41:19 -0700885 @Override // Binder call
886 public String[] getKeyboardLayoutsForInputDevice(String inputDeviceDescriptor) {
887 if (inputDeviceDescriptor == null) {
888 throw new IllegalArgumentException("inputDeviceDescriptor must not be null");
889 }
890
891 synchronized (mDataStore) {
892 return mDataStore.getKeyboardLayouts(inputDeviceDescriptor);
893 }
894 }
895
896 @Override // Binder call
897 public void addKeyboardLayoutForInputDevice(String inputDeviceDescriptor,
898 String keyboardLayoutDescriptor) {
899 if (!checkCallingPermission(android.Manifest.permission.SET_KEYBOARD_LAYOUT,
900 "addKeyboardLayoutForInputDevice()")) {
901 throw new SecurityException("Requires SET_KEYBOARD_LAYOUT permission");
902 }
903 if (inputDeviceDescriptor == null) {
904 throw new IllegalArgumentException("inputDeviceDescriptor must not be null");
905 }
906 if (keyboardLayoutDescriptor == null) {
907 throw new IllegalArgumentException("keyboardLayoutDescriptor must not be null");
908 }
909
910 synchronized (mDataStore) {
911 try {
912 String oldLayout = mDataStore.getCurrentKeyboardLayout(inputDeviceDescriptor);
913 if (mDataStore.addKeyboardLayout(inputDeviceDescriptor, keyboardLayoutDescriptor)
914 && !Objects.equal(oldLayout,
915 mDataStore.getCurrentKeyboardLayout(inputDeviceDescriptor))) {
916 mHandler.sendEmptyMessage(MSG_RELOAD_KEYBOARD_LAYOUTS);
917 }
918 } finally {
919 mDataStore.saveIfNeeded();
Jeff Brown6ec6f792012-04-17 16:52:41 -0700920 }
Jeff Browncf39bdf2012-05-18 14:41:19 -0700921 }
922 }
923
924 @Override // Binder call
925 public void removeKeyboardLayoutForInputDevice(String inputDeviceDescriptor,
926 String keyboardLayoutDescriptor) {
927 if (!checkCallingPermission(android.Manifest.permission.SET_KEYBOARD_LAYOUT,
928 "removeKeyboardLayoutForInputDevice()")) {
929 throw new SecurityException("Requires SET_KEYBOARD_LAYOUT permission");
930 }
931 if (inputDeviceDescriptor == null) {
932 throw new IllegalArgumentException("inputDeviceDescriptor must not be null");
933 }
934 if (keyboardLayoutDescriptor == null) {
935 throw new IllegalArgumentException("keyboardLayoutDescriptor must not be null");
936 }
937
938 synchronized (mDataStore) {
939 try {
940 String oldLayout = mDataStore.getCurrentKeyboardLayout(inputDeviceDescriptor);
941 if (mDataStore.removeKeyboardLayout(inputDeviceDescriptor,
942 keyboardLayoutDescriptor)
943 && !Objects.equal(oldLayout,
944 mDataStore.getCurrentKeyboardLayout(inputDeviceDescriptor))) {
945 mHandler.sendEmptyMessage(MSG_RELOAD_KEYBOARD_LAYOUTS);
946 }
947 } finally {
948 mDataStore.saveIfNeeded();
949 }
950 }
951 }
952
953 public void switchKeyboardLayout(int deviceId, int direction) {
954 mHandler.obtainMessage(MSG_SWITCH_KEYBOARD_LAYOUT, deviceId, direction).sendToTarget();
955 }
956
957 // Must be called on handler.
958 private void handleSwitchKeyboardLayout(int deviceId, int direction) {
959 final InputDevice device = getInputDevice(deviceId);
960 final String inputDeviceDescriptor = device.getDescriptor();
961 if (device != null) {
962 final boolean changed;
963 final String keyboardLayoutDescriptor;
964 synchronized (mDataStore) {
965 try {
966 changed = mDataStore.switchKeyboardLayout(inputDeviceDescriptor, direction);
967 keyboardLayoutDescriptor = mDataStore.getCurrentKeyboardLayout(
968 inputDeviceDescriptor);
969 } finally {
970 mDataStore.saveIfNeeded();
971 }
972 }
973
974 if (changed) {
975 if (mSwitchedKeyboardLayoutToast != null) {
976 mSwitchedKeyboardLayoutToast.cancel();
977 mSwitchedKeyboardLayoutToast = null;
978 }
979 if (keyboardLayoutDescriptor != null) {
980 KeyboardLayout keyboardLayout = getKeyboardLayout(keyboardLayoutDescriptor);
981 if (keyboardLayout != null) {
982 mSwitchedKeyboardLayoutToast = Toast.makeText(
983 mContext, keyboardLayout.getLabel(), Toast.LENGTH_SHORT);
984 mSwitchedKeyboardLayoutToast.show();
985 }
986 }
987
988 reloadKeyboardLayouts();
989 }
Jeff Brown6ec6f792012-04-17 16:52:41 -0700990 }
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700991 }
992
Jeff Brown9302c872011-07-13 22:51:29 -0700993 public void setInputWindows(InputWindowHandle[] windowHandles) {
Jeff Brown4532e612012-04-05 14:27:12 -0700994 nativeSetInputWindows(mPtr, windowHandles);
Jeff Brown349703e2010-06-22 01:27:15 -0700995 }
996
Jeff Brown9302c872011-07-13 22:51:29 -0700997 public void setFocusedApplication(InputApplicationHandle application) {
Jeff Brown4532e612012-04-05 14:27:12 -0700998 nativeSetFocusedApplication(mPtr, application);
Jeff Brown349703e2010-06-22 01:27:15 -0700999 }
1000
Jeff Brown349703e2010-06-22 01:27:15 -07001001 public void setInputDispatchMode(boolean enabled, boolean frozen) {
Jeff Brown4532e612012-04-05 14:27:12 -07001002 nativeSetInputDispatchMode(mPtr, enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -07001003 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08001004
1005 public void setSystemUiVisibility(int visibility) {
Jeff Brown4532e612012-04-05 14:27:12 -07001006 nativeSetSystemUiVisibility(mPtr, visibility);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001007 }
1008
Jeff Browne6504122010-09-27 14:52:15 -07001009 /**
1010 * Atomically transfers touch focus from one window to another as identified by
1011 * their input channels. It is possible for multiple windows to have
1012 * touch focus if they support split touch dispatch
1013 * {@link android.view.WindowManager.LayoutParams#FLAG_SPLIT_TOUCH} but this
1014 * method only transfers touch focus of the specified window without affecting
1015 * other windows that may also have touch focus at the same time.
1016 * @param fromChannel The channel of a window that currently has touch focus.
1017 * @param toChannel The channel of the window that should receive touch focus in
1018 * place of the first.
1019 * @return True if the transfer was successful. False if the window with the
1020 * specified channel did not actually have touch focus at the time of the request.
1021 */
1022 public boolean transferTouchFocus(InputChannel fromChannel, InputChannel toChannel) {
1023 if (fromChannel == null) {
1024 throw new IllegalArgumentException("fromChannel must not be null.");
1025 }
1026 if (toChannel == null) {
1027 throw new IllegalArgumentException("toChannel must not be null.");
1028 }
Jeff Brown4532e612012-04-05 14:27:12 -07001029 return nativeTransferTouchFocus(mPtr, fromChannel, toChannel);
Jeff Browne6504122010-09-27 14:52:15 -07001030 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08001031
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001032 @Override // Binder call
Jeff Brownac143512012-04-05 18:57:33 -07001033 public void tryPointerSpeed(int speed) {
1034 if (!checkCallingPermission(android.Manifest.permission.SET_POINTER_SPEED,
1035 "tryPointerSpeed()")) {
1036 throw new SecurityException("Requires SET_POINTER_SPEED permission");
1037 }
1038
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001039 if (speed < InputManager.MIN_POINTER_SPEED || speed > InputManager.MAX_POINTER_SPEED) {
1040 throw new IllegalArgumentException("speed out of range");
1041 }
1042
Jeff Brownac143512012-04-05 18:57:33 -07001043 setPointerSpeedUnchecked(speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001044 }
1045
1046 public void updatePointerSpeedFromSettings() {
Jeff Brownac143512012-04-05 18:57:33 -07001047 int speed = getPointerSpeedSetting();
1048 setPointerSpeedUnchecked(speed);
1049 }
1050
1051 private void setPointerSpeedUnchecked(int speed) {
1052 speed = Math.min(Math.max(speed, InputManager.MIN_POINTER_SPEED),
1053 InputManager.MAX_POINTER_SPEED);
1054 nativeSetPointerSpeed(mPtr, speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001055 }
1056
1057 private void registerPointerSpeedSettingObserver() {
1058 mContext.getContentResolver().registerContentObserver(
1059 Settings.System.getUriFor(Settings.System.POINTER_SPEED), true,
Jeff Brown4532e612012-04-05 14:27:12 -07001060 new ContentObserver(mHandler) {
Jeff Brown1a84fd12011-06-02 01:26:32 -07001061 @Override
1062 public void onChange(boolean selfChange) {
1063 updatePointerSpeedFromSettings();
1064 }
1065 });
1066 }
1067
Jeff Brownac143512012-04-05 18:57:33 -07001068 private int getPointerSpeedSetting() {
1069 int speed = InputManager.DEFAULT_POINTER_SPEED;
Jeff Brown1a84fd12011-06-02 01:26:32 -07001070 try {
1071 speed = Settings.System.getInt(mContext.getContentResolver(),
1072 Settings.System.POINTER_SPEED);
1073 } catch (SettingNotFoundException snfe) {
1074 }
1075 return speed;
1076 }
1077
Jeff Browndaf4a122011-08-26 17:14:14 -07001078 public void updateShowTouchesFromSettings() {
1079 int setting = getShowTouchesSetting(0);
Jeff Brown4532e612012-04-05 14:27:12 -07001080 nativeSetShowTouches(mPtr, setting != 0);
Jeff Browndaf4a122011-08-26 17:14:14 -07001081 }
1082
1083 private void registerShowTouchesSettingObserver() {
1084 mContext.getContentResolver().registerContentObserver(
1085 Settings.System.getUriFor(Settings.System.SHOW_TOUCHES), true,
Jeff Brown4532e612012-04-05 14:27:12 -07001086 new ContentObserver(mHandler) {
Jeff Browndaf4a122011-08-26 17:14:14 -07001087 @Override
1088 public void onChange(boolean selfChange) {
1089 updateShowTouchesFromSettings();
1090 }
1091 });
1092 }
1093
1094 private int getShowTouchesSetting(int defaultValue) {
1095 int result = defaultValue;
1096 try {
1097 result = Settings.System.getInt(mContext.getContentResolver(),
1098 Settings.System.SHOW_TOUCHES);
1099 } catch (SettingNotFoundException snfe) {
1100 }
1101 return result;
1102 }
1103
Jeff Browna47425a2012-04-13 04:09:27 -07001104 // Binder call
1105 @Override
1106 public void vibrate(int deviceId, long[] pattern, int repeat, IBinder token) {
1107 if (repeat >= pattern.length) {
1108 throw new ArrayIndexOutOfBoundsException();
1109 }
1110
1111 VibratorToken v;
1112 synchronized (mVibratorLock) {
1113 v = mVibratorTokens.get(token);
1114 if (v == null) {
1115 v = new VibratorToken(deviceId, token, mNextVibratorTokenValue++);
1116 try {
1117 token.linkToDeath(v, 0);
1118 } catch (RemoteException ex) {
1119 // give up
1120 throw new RuntimeException(ex);
1121 }
1122 mVibratorTokens.put(token, v);
1123 }
1124 }
1125
1126 synchronized (v) {
1127 v.mVibrating = true;
1128 nativeVibrate(mPtr, deviceId, pattern, repeat, v.mTokenValue);
1129 }
1130 }
1131
1132 // Binder call
1133 @Override
1134 public void cancelVibrate(int deviceId, IBinder token) {
1135 VibratorToken v;
1136 synchronized (mVibratorLock) {
1137 v = mVibratorTokens.get(token);
1138 if (v == null || v.mDeviceId != deviceId) {
1139 return; // nothing to cancel
1140 }
1141 }
1142
1143 cancelVibrateIfNeeded(v);
1144 }
1145
1146 void onVibratorTokenDied(VibratorToken v) {
1147 synchronized (mVibratorLock) {
1148 mVibratorTokens.remove(v.mToken);
1149 }
1150
1151 cancelVibrateIfNeeded(v);
1152 }
1153
1154 private void cancelVibrateIfNeeded(VibratorToken v) {
1155 synchronized (v) {
1156 if (v.mVibrating) {
1157 nativeCancelVibrate(mPtr, v.mDeviceId, v.mTokenValue);
1158 v.mVibrating = false;
1159 }
1160 }
1161 }
1162
Jeff Brown4532e612012-04-05 14:27:12 -07001163 @Override
1164 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Browna3bc5652012-04-17 11:42:25 -07001165 if (mContext.checkCallingOrSelfPermission(Manifest.permission.DUMP)
Jeff Brown4532e612012-04-05 14:27:12 -07001166 != PackageManager.PERMISSION_GRANTED) {
1167 pw.println("Permission Denial: can't dump InputManager from from pid="
1168 + Binder.getCallingPid()
1169 + ", uid=" + Binder.getCallingUid());
1170 return;
1171 }
1172
1173 pw.println("INPUT MANAGER (dumpsys input)\n");
1174 String dumpStr = nativeDump(mPtr);
Jeff Browne33348b2010-07-15 23:54:05 -07001175 if (dumpStr != null) {
1176 pw.println(dumpStr);
1177 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001178 }
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001179
Jeff Brownac143512012-04-05 18:57:33 -07001180 private boolean checkCallingPermission(String permission, String func) {
1181 // Quick check: if the calling permission is me, it's all okay.
1182 if (Binder.getCallingPid() == Process.myPid()) {
1183 return true;
1184 }
1185
1186 if (mContext.checkCallingPermission(permission) == PackageManager.PERMISSION_GRANTED) {
1187 return true;
1188 }
1189 String msg = "Permission Denial: " + func + " from pid="
1190 + Binder.getCallingPid()
1191 + ", uid=" + Binder.getCallingUid()
1192 + " requires " + permission;
1193 Slog.w(TAG, msg);
1194 return false;
1195 }
1196
Jeff Brown4532e612012-04-05 14:27:12 -07001197 // Called by the heartbeat to ensure locks are not held indefinitely (for deadlock detection).
Jeff Brown89ef0722011-08-10 16:25:21 -07001198 public void monitor() {
1199 synchronized (mInputFilterLock) { }
Jeff Brown4532e612012-04-05 14:27:12 -07001200 nativeMonitor(mPtr);
Jeff Brown89ef0722011-08-10 16:25:21 -07001201 }
1202
Jeff Brown4532e612012-04-05 14:27:12 -07001203 // Native callback.
1204 private void notifyConfigurationChanged(long whenNanos) {
1205 mCallbacks.notifyConfigurationChanged();
1206 }
1207
1208 // Native callback.
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001209 private void notifyInputDevicesChanged(InputDevice[] inputDevices) {
1210 synchronized (mInputDevicesLock) {
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001211 if (!mInputDevicesChangedPending) {
1212 mInputDevicesChangedPending = true;
Jeff Browncf39bdf2012-05-18 14:41:19 -07001213 mHandler.obtainMessage(MSG_DELIVER_INPUT_DEVICES_CHANGED,
1214 mInputDevices).sendToTarget();
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001215 }
Jeff Browncf39bdf2012-05-18 14:41:19 -07001216
1217 mInputDevices = inputDevices;
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001218 }
1219 }
1220
1221 // Native callback.
Jeff Brown4532e612012-04-05 14:27:12 -07001222 private void notifyLidSwitchChanged(long whenNanos, boolean lidOpen) {
1223 mCallbacks.notifyLidSwitchChanged(whenNanos, lidOpen);
1224 }
1225
1226 // Native callback.
1227 private void notifyInputChannelBroken(InputWindowHandle inputWindowHandle) {
1228 mCallbacks.notifyInputChannelBroken(inputWindowHandle);
1229 }
1230
1231 // Native callback.
1232 private long notifyANR(InputApplicationHandle inputApplicationHandle,
1233 InputWindowHandle inputWindowHandle) {
1234 return mCallbacks.notifyANR(inputApplicationHandle, inputWindowHandle);
1235 }
1236
1237 // Native callback.
1238 final boolean filterInputEvent(InputEvent event, int policyFlags) {
1239 synchronized (mInputFilterLock) {
1240 if (mInputFilter != null) {
1241 mInputFilter.filterInputEvent(event, policyFlags);
1242 return false;
1243 }
1244 }
1245 event.recycle();
1246 return true;
1247 }
1248
1249 // Native callback.
1250 private int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags, boolean isScreenOn) {
1251 return mCallbacks.interceptKeyBeforeQueueing(
1252 event, policyFlags, isScreenOn);
1253 }
1254
1255 // Native callback.
1256 private int interceptMotionBeforeQueueingWhenScreenOff(int policyFlags) {
1257 return mCallbacks.interceptMotionBeforeQueueingWhenScreenOff(policyFlags);
1258 }
1259
1260 // Native callback.
1261 private long interceptKeyBeforeDispatching(InputWindowHandle focus,
1262 KeyEvent event, int policyFlags) {
1263 return mCallbacks.interceptKeyBeforeDispatching(focus, event, policyFlags);
1264 }
1265
1266 // Native callback.
1267 private KeyEvent dispatchUnhandledKey(InputWindowHandle focus,
1268 KeyEvent event, int policyFlags) {
1269 return mCallbacks.dispatchUnhandledKey(focus, event, policyFlags);
1270 }
1271
1272 // Native callback.
1273 private boolean checkInjectEventsPermission(int injectorPid, int injectorUid) {
1274 return mContext.checkPermission(android.Manifest.permission.INJECT_EVENTS,
1275 injectorPid, injectorUid) == PackageManager.PERMISSION_GRANTED;
1276 }
1277
1278 // Native callback.
1279 private int getVirtualKeyQuietTimeMillis() {
1280 return mContext.getResources().getInteger(
1281 com.android.internal.R.integer.config_virtualKeyQuietTimeMillis);
1282 }
1283
1284 // Native callback.
1285 private String[] getExcludedDeviceNames() {
1286 ArrayList<String> names = new ArrayList<String>();
1287
1288 // Read partner-provided list of excluded input devices
1289 XmlPullParser parser = null;
1290 // Environment.getRootDirectory() is a fancy way of saying ANDROID_ROOT or "/system".
1291 File confFile = new File(Environment.getRootDirectory(), EXCLUDED_DEVICES_PATH);
1292 FileReader confreader = null;
1293 try {
1294 confreader = new FileReader(confFile);
1295 parser = Xml.newPullParser();
1296 parser.setInput(confreader);
1297 XmlUtils.beginDocument(parser, "devices");
1298
1299 while (true) {
1300 XmlUtils.nextElement(parser);
1301 if (!"device".equals(parser.getName())) {
1302 break;
1303 }
1304 String name = parser.getAttributeValue(null, "name");
1305 if (name != null) {
1306 names.add(name);
1307 }
1308 }
1309 } catch (FileNotFoundException e) {
1310 // It's ok if the file does not exist.
1311 } catch (Exception e) {
1312 Slog.e(TAG, "Exception while parsing '" + confFile.getAbsolutePath() + "'", e);
1313 } finally {
1314 try { if (confreader != null) confreader.close(); } catch (IOException e) { }
1315 }
1316
1317 return names.toArray(new String[names.size()]);
1318 }
1319
1320 // Native callback.
1321 private int getKeyRepeatTimeout() {
1322 return ViewConfiguration.getKeyRepeatTimeout();
1323 }
1324
1325 // Native callback.
1326 private int getKeyRepeatDelay() {
1327 return ViewConfiguration.getKeyRepeatDelay();
1328 }
1329
1330 // Native callback.
1331 private int getHoverTapTimeout() {
1332 return ViewConfiguration.getHoverTapTimeout();
1333 }
1334
1335 // Native callback.
1336 private int getHoverTapSlop() {
1337 return ViewConfiguration.getHoverTapSlop();
1338 }
1339
1340 // Native callback.
1341 private int getDoubleTapTimeout() {
1342 return ViewConfiguration.getDoubleTapTimeout();
1343 }
1344
1345 // Native callback.
1346 private int getLongPressTimeout() {
1347 return ViewConfiguration.getLongPressTimeout();
1348 }
1349
1350 // Native callback.
1351 private int getPointerLayer() {
1352 return mCallbacks.getPointerLayer();
1353 }
1354
1355 // Native callback.
1356 private PointerIcon getPointerIcon() {
1357 return PointerIcon.getDefaultIcon(mContext);
1358 }
1359
Jeff Brown6ec6f792012-04-17 16:52:41 -07001360 // Native callback.
1361 private String[] getKeyboardLayoutOverlay(String inputDeviceDescriptor) {
1362 if (!mSystemReady) {
1363 return null;
1364 }
1365
Jeff Browncf39bdf2012-05-18 14:41:19 -07001366 String keyboardLayoutDescriptor = getCurrentKeyboardLayoutForInputDevice(
1367 inputDeviceDescriptor);
Jeff Brown6ec6f792012-04-17 16:52:41 -07001368 if (keyboardLayoutDescriptor == null) {
1369 return null;
1370 }
1371
1372 final String[] result = new String[2];
1373 visitKeyboardLayout(keyboardLayoutDescriptor, new KeyboardLayoutVisitor() {
1374 @Override
1375 public void visitKeyboardLayout(Resources resources,
Jeff Brownd9fec5d2012-05-17 16:01:54 -07001376 String descriptor, String label, String collection, int keyboardLayoutResId) {
Jeff Brown6ec6f792012-04-17 16:52:41 -07001377 try {
1378 result[0] = descriptor;
1379 result[1] = Streams.readFully(new InputStreamReader(
Jeff Brown2f095762012-05-10 21:29:33 -07001380 resources.openRawResource(keyboardLayoutResId)));
Jeff Brown6ec6f792012-04-17 16:52:41 -07001381 } catch (IOException ex) {
1382 } catch (NotFoundException ex) {
1383 }
1384 }
1385 });
1386 if (result[0] == null) {
1387 Log.w(TAG, "Could not get keyboard layout with descriptor '"
1388 + keyboardLayoutDescriptor + "'.");
1389 return null;
1390 }
1391 return result;
1392 }
1393
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001394 // Native callback.
1395 private String getDeviceAlias(String uniqueId) {
1396 if (mBluetoothService != null &&
1397 BluetoothAdapter.checkBluetoothAddress(uniqueId)) {
1398 return mBluetoothService.getRemoteAlias(uniqueId);
1399 }
1400 return null;
1401 }
1402
Jeff Brown6ec6f792012-04-17 16:52:41 -07001403
Jeff Brown4532e612012-04-05 14:27:12 -07001404 /**
1405 * Callback interface implemented by the Window Manager.
1406 */
1407 public interface Callbacks {
1408 public void notifyConfigurationChanged();
1409
1410 public void notifyLidSwitchChanged(long whenNanos, boolean lidOpen);
1411
1412 public void notifyInputChannelBroken(InputWindowHandle inputWindowHandle);
1413
1414 public long notifyANR(InputApplicationHandle inputApplicationHandle,
1415 InputWindowHandle inputWindowHandle);
1416
1417 public int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags, boolean isScreenOn);
1418
1419 public int interceptMotionBeforeQueueingWhenScreenOff(int policyFlags);
1420
1421 public long interceptKeyBeforeDispatching(InputWindowHandle focus,
1422 KeyEvent event, int policyFlags);
1423
1424 public KeyEvent dispatchUnhandledKey(InputWindowHandle focus,
1425 KeyEvent event, int policyFlags);
1426
1427 public int getPointerLayer();
1428 }
1429
1430 /**
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001431 * Private handler for the input manager.
1432 */
1433 private final class InputManagerHandler extends Handler {
1434 @Override
1435 public void handleMessage(Message msg) {
1436 switch (msg.what) {
1437 case MSG_DELIVER_INPUT_DEVICES_CHANGED:
Jeff Browncf39bdf2012-05-18 14:41:19 -07001438 deliverInputDevicesChanged((InputDevice[])msg.obj);
1439 break;
1440 case MSG_SWITCH_KEYBOARD_LAYOUT:
1441 handleSwitchKeyboardLayout(msg.arg1, msg.arg2);
1442 break;
1443 case MSG_RELOAD_KEYBOARD_LAYOUTS:
1444 reloadKeyboardLayouts();
1445 break;
1446 case MSG_UPDATE_KEYBOARD_LAYOUTS:
1447 updateKeyboardLayouts();
1448 break;
1449 case MSG_RELOAD_DEVICE_ALIASES:
1450 reloadDeviceAliases();
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001451 break;
1452 }
1453 }
1454 }
1455
1456 /**
Jeff Brown4532e612012-04-05 14:27:12 -07001457 * Hosting interface for input filters to call back into the input manager.
1458 */
Jeff Brown0029c662011-03-30 02:25:18 -07001459 private final class InputFilterHost implements InputFilter.Host {
1460 private boolean mDisconnected;
1461
1462 public void disconnectLocked() {
1463 mDisconnected = true;
1464 }
1465
1466 public void sendInputEvent(InputEvent event, int policyFlags) {
1467 if (event == null) {
1468 throw new IllegalArgumentException("event must not be null");
1469 }
1470
1471 synchronized (mInputFilterLock) {
1472 if (!mDisconnected) {
Jeff Brownac143512012-04-05 18:57:33 -07001473 nativeInjectInputEvent(mPtr, event, 0, 0,
1474 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC, 0,
Jeff Brown0029c662011-03-30 02:25:18 -07001475 policyFlags | WindowManagerPolicy.FLAG_FILTERED);
1476 }
1477 }
1478 }
1479 }
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001480
1481 private static final class KeyboardLayoutDescriptor {
1482 public String packageName;
1483 public String receiverName;
1484 public String keyboardLayoutName;
1485
1486 public static String format(String packageName,
1487 String receiverName, String keyboardName) {
1488 return packageName + "/" + receiverName + "/" + keyboardName;
1489 }
1490
1491 public static KeyboardLayoutDescriptor parse(String descriptor) {
1492 int pos = descriptor.indexOf('/');
1493 if (pos < 0 || pos + 1 == descriptor.length()) {
1494 return null;
1495 }
1496 int pos2 = descriptor.indexOf('/', pos + 1);
1497 if (pos2 < pos + 2 || pos2 + 1 == descriptor.length()) {
1498 return null;
1499 }
1500
1501 KeyboardLayoutDescriptor result = new KeyboardLayoutDescriptor();
1502 result.packageName = descriptor.substring(0, pos);
1503 result.receiverName = descriptor.substring(pos + 1, pos2);
1504 result.keyboardLayoutName = descriptor.substring(pos2 + 1);
1505 return result;
1506 }
1507 }
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001508
Jeff Brown6ec6f792012-04-17 16:52:41 -07001509 private interface KeyboardLayoutVisitor {
1510 void visitKeyboardLayout(Resources resources,
Jeff Brownd9fec5d2012-05-17 16:01:54 -07001511 String descriptor, String label, String collection, int keyboardLayoutResId);
Jeff Brown6ec6f792012-04-17 16:52:41 -07001512 }
1513
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001514 private final class InputDevicesChangedListenerRecord implements DeathRecipient {
1515 private final int mPid;
1516 private final IInputDevicesChangedListener mListener;
1517
1518 public InputDevicesChangedListenerRecord(int pid, IInputDevicesChangedListener listener) {
1519 mPid = pid;
1520 mListener = listener;
1521 }
1522
1523 @Override
1524 public void binderDied() {
1525 if (DEBUG) {
1526 Slog.d(TAG, "Input devices changed listener for pid " + mPid + " died.");
1527 }
1528 onInputDevicesChangedListenerDied(mPid);
1529 }
1530
1531 public void notifyInputDevicesChanged(int[] info) {
1532 try {
1533 mListener.onInputDevicesChanged(info);
1534 } catch (RemoteException ex) {
1535 Slog.w(TAG, "Failed to notify process "
1536 + mPid + " that input devices changed, assuming it died.", ex);
1537 binderDied();
1538 }
1539 }
1540 }
Jeff Browna47425a2012-04-13 04:09:27 -07001541
1542 private final class VibratorToken implements DeathRecipient {
1543 public final int mDeviceId;
1544 public final IBinder mToken;
1545 public final int mTokenValue;
1546
1547 public boolean mVibrating;
1548
1549 public VibratorToken(int deviceId, IBinder token, int tokenValue) {
1550 mDeviceId = deviceId;
1551 mToken = token;
1552 mTokenValue = tokenValue;
1553 }
1554
1555 @Override
1556 public void binderDied() {
1557 if (DEBUG) {
1558 Slog.d(TAG, "Vibrator token died.");
1559 }
1560 onVibratorTokenDied(this);
1561 }
1562 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001563}