blob: fa18149112bd3f21df8eb7266b0145459bbd5af1 [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 Brown4ccb8232014-01-16 22:16:42 -080021import com.android.server.DisplayThread;
22import com.android.server.LocalServices;
Jeff Brown89ef0722011-08-10 16:25:21 -070023import com.android.server.Watchdog;
Jeff Brown46b9ac02010-04-22 18:58:52 -070024
25import org.xmlpull.v1.XmlPullParser;
26
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 Brown4ccb8232014-01-16 22:16:42 -080047import android.hardware.display.DisplayViewport;
Jeff Brownaf9e8d32012-04-12 17:32:48 -070048import android.hardware.input.IInputDevicesChangedListener;
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -070049import android.hardware.input.IInputManager;
Jeff Brownac143512012-04-05 18:57:33 -070050import android.hardware.input.InputManager;
Jeff Brown4ccb8232014-01-16 22:16:42 -080051import android.hardware.input.InputManagerInternal;
Jeff Brown9f25b7f2012-04-10 14:30:49 -070052import android.hardware.input.KeyboardLayout;
Jeff Brown4532e612012-04-05 14:27:12 -070053import android.os.Binder;
Jeff Brown9f25b7f2012-04-10 14:30:49 -070054import android.os.Bundle;
Jeff Brown46b9ac02010-04-22 18:58:52 -070055import android.os.Environment;
Jeff Brown4532e612012-04-05 14:27:12 -070056import android.os.Handler;
Jeff Brownaf9e8d32012-04-12 17:32:48 -070057import android.os.IBinder;
Jeff Browna9d131c2012-09-20 16:48:17 -070058import android.os.Looper;
Jeff Brownaf9e8d32012-04-12 17:32:48 -070059import android.os.Message;
Jeff Brown05dc66a2011-03-02 14:41:58 -080060import android.os.MessageQueue;
Jeff Brownac143512012-04-05 18:57:33 -070061import android.os.Process;
Jeff Brownaf9e8d32012-04-12 17:32:48 -070062import android.os.RemoteException;
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -070063import android.os.UserHandle;
Jeff Brown1a84fd12011-06-02 01:26:32 -070064import android.provider.Settings;
65import android.provider.Settings.SettingNotFoundException;
Jeff Brown9f25b7f2012-04-10 14:30:49 -070066import android.util.Log;
Jeff Brown46b9ac02010-04-22 18:58:52 -070067import android.util.Slog;
Jeff Brownaf9e8d32012-04-12 17:32:48 -070068import android.util.SparseArray;
Jeff Brown46b9ac02010-04-22 18:58:52 -070069import android.util.Xml;
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -070070import android.view.IInputFilter;
71import android.view.IInputFilterHost;
Jeff Brown46b9ac02010-04-22 18:58:52 -070072import android.view.InputChannel;
Jeff Brown8d608662010-08-30 03:02:23 -070073import android.view.InputDevice;
Jeff Brown6ec402b2010-07-28 15:48:59 -070074import android.view.InputEvent;
Jeff Brown1f245102010-11-18 20:53:46 -080075import android.view.KeyEvent;
Jeff Brown2352b972011-04-12 22:39:53 -070076import android.view.PointerIcon;
Jeff Browna4547672011-03-02 21:38:11 -080077import android.view.ViewConfiguration;
Jeff Brown0029c662011-03-30 02:25:18 -070078import android.view.WindowManagerPolicy;
Jeff Browncf39bdf2012-05-18 14:41:19 -070079import android.widget.Toast;
Jeff Brown46b9ac02010-04-22 18:58:52 -070080
Jeff Brown46b9ac02010-04-22 18:58:52 -070081import java.io.File;
Jeff Brown4532e612012-04-05 14:27:12 -070082import java.io.FileDescriptor;
Jeff Brown46b9ac02010-04-22 18:58:52 -070083import java.io.FileNotFoundException;
84import java.io.FileReader;
85import java.io.IOException;
Jeff Brown6ec6f792012-04-17 16:52:41 -070086import java.io.InputStreamReader;
Jeff Brown46b9ac02010-04-22 18:58:52 -070087import java.io.PrintWriter;
88import java.util.ArrayList;
Jeff Brown9f25b7f2012-04-10 14:30:49 -070089import java.util.HashMap;
Jeff Browncf39bdf2012-05-18 14:41:19 -070090import java.util.HashSet;
Jeff Browna3bc5652012-04-17 11:42:25 -070091
Jeff Brown6ec6f792012-04-17 16:52:41 -070092import libcore.io.Streams;
Jeff Browna3bc5652012-04-17 11:42:25 -070093import libcore.util.Objects;
Jeff Brown46b9ac02010-04-22 18:58:52 -070094
95/*
96 * Wraps the C++ InputManager and provides its callbacks.
Jeff Brown46b9ac02010-04-22 18:58:52 -070097 */
Jeff Brownd728bf52012-09-08 18:05:28 -070098public class InputManagerService extends IInputManager.Stub
Jeff Brown4ccb8232014-01-16 22:16:42 -080099 implements Watchdog.Monitor {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700100 static final String TAG = "InputManager";
Jeff Brown1b9ba572012-05-21 10:54:18 -0700101 static final boolean DEBUG = false;
Jeff Brownb6997262010-10-08 22:31:17 -0700102
Jeff Brown4532e612012-04-05 14:27:12 -0700103 private static final String EXCLUDED_DEVICES_PATH = "etc/excluded-input-devices.xml";
104
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700105 private static final int MSG_DELIVER_INPUT_DEVICES_CHANGED = 1;
Jeff Browncf39bdf2012-05-18 14:41:19 -0700106 private static final int MSG_SWITCH_KEYBOARD_LAYOUT = 2;
107 private static final int MSG_RELOAD_KEYBOARD_LAYOUTS = 3;
108 private static final int MSG_UPDATE_KEYBOARD_LAYOUTS = 4;
109 private static final int MSG_RELOAD_DEVICE_ALIASES = 5;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700110
Jeff Brown4532e612012-04-05 14:27:12 -0700111 // Pointer to native input manager service object.
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000112 private final long mPtr;
Jeff Brown4532e612012-04-05 14:27:12 -0700113
Jeff Brown46b9ac02010-04-22 18:58:52 -0700114 private final Context mContext;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700115 private final InputManagerHandler mHandler;
Jeff Browna9d131c2012-09-20 16:48:17 -0700116
117 private WindowManagerCallbacks mWindowManagerCallbacks;
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700118 private WiredAccessoryCallbacks mWiredAccessoryCallbacks;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700119 private boolean mSystemReady;
Jeff Browncf39bdf2012-05-18 14:41:19 -0700120 private NotificationManager mNotificationManager;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700121
Jeff Browna3bc5652012-04-17 11:42:25 -0700122 // Persistent data store. Must be locked each time during use.
123 private final PersistentDataStore mDataStore = new PersistentDataStore();
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700124
125 // List of currently registered input devices changed listeners by process id.
126 private Object mInputDevicesLock = new Object();
127 private boolean mInputDevicesChangedPending; // guarded by mInputDevicesLock
128 private InputDevice[] mInputDevices = new InputDevice[0];
129 private final SparseArray<InputDevicesChangedListenerRecord> mInputDevicesChangedListeners =
130 new SparseArray<InputDevicesChangedListenerRecord>(); // guarded by mInputDevicesLock
131 private final ArrayList<InputDevicesChangedListenerRecord>
132 mTempInputDevicesChangedListenersToNotify =
133 new ArrayList<InputDevicesChangedListenerRecord>(); // handler thread only
Jeff Browncf39bdf2012-05-18 14:41:19 -0700134 private final ArrayList<InputDevice>
135 mTempFullKeyboards = new ArrayList<InputDevice>(); // handler thread only
136 private boolean mKeyboardLayoutNotificationShown;
137 private PendingIntent mKeyboardLayoutIntent;
138 private Toast mSwitchedKeyboardLayoutToast;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700139
Jeff Browna47425a2012-04-13 04:09:27 -0700140 // State for vibrator tokens.
141 private Object mVibratorLock = new Object();
142 private HashMap<IBinder, VibratorToken> mVibratorTokens =
143 new HashMap<IBinder, VibratorToken>();
144 private int mNextVibratorTokenValue;
145
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700146 // State for the currently installed input filter.
147 final Object mInputFilterLock = new Object();
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -0700148 IInputFilter mInputFilter; // guarded by mInputFilterLock
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700149 InputFilterHost mInputFilterHost; // guarded by mInputFilterLock
Jeff Brown1a84fd12011-06-02 01:26:32 -0700150
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000151 private static native long nativeInit(InputManagerService service,
Jeff Brown4532e612012-04-05 14:27:12 -0700152 Context context, MessageQueue messageQueue);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000153 private static native void nativeStart(long ptr);
154 private static native void nativeSetDisplayViewport(long ptr, boolean external,
Jeff Brownd728bf52012-09-08 18:05:28 -0700155 int displayId, int rotation,
156 int logicalLeft, int logicalTop, int logicalRight, int logicalBottom,
Jeff Brown83d616a2012-09-09 20:33:43 -0700157 int physicalLeft, int physicalTop, int physicalRight, int physicalBottom,
158 int deviceWidth, int deviceHeight);
Jeff Brownd728bf52012-09-08 18:05:28 -0700159
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000160 private static native int nativeGetScanCodeState(long ptr,
Jeff Brown4532e612012-04-05 14:27:12 -0700161 int deviceId, int sourceMask, int scanCode);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000162 private static native int nativeGetKeyCodeState(long ptr,
Jeff Brown4532e612012-04-05 14:27:12 -0700163 int deviceId, int sourceMask, int keyCode);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000164 private static native int nativeGetSwitchState(long ptr,
Jeff Brown4532e612012-04-05 14:27:12 -0700165 int deviceId, int sourceMask, int sw);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000166 private static native boolean nativeHasKeys(long ptr,
Jeff Brown4532e612012-04-05 14:27:12 -0700167 int deviceId, int sourceMask, int[] keyCodes, boolean[] keyExists);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000168 private static native void nativeRegisterInputChannel(long ptr, InputChannel inputChannel,
Jeff Brown928e0542011-01-10 11:17:36 -0800169 InputWindowHandle inputWindowHandle, boolean monitor);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000170 private static native void nativeUnregisterInputChannel(long ptr, InputChannel inputChannel);
171 private static native void nativeSetInputFilterEnabled(long ptr, boolean enable);
172 private static native int nativeInjectInputEvent(long ptr, InputEvent event,
Jeff Brown0029c662011-03-30 02:25:18 -0700173 int injectorPid, int injectorUid, int syncMode, int timeoutMillis,
174 int policyFlags);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000175 private static native void nativeSetInputWindows(long ptr, InputWindowHandle[] windowHandles);
176 private static native void nativeSetInputDispatchMode(long ptr, boolean enabled, boolean frozen);
177 private static native void nativeSetSystemUiVisibility(long ptr, int visibility);
178 private static native void nativeSetFocusedApplication(long ptr,
Jeff Brown4532e612012-04-05 14:27:12 -0700179 InputApplicationHandle application);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000180 private static native boolean nativeTransferTouchFocus(long ptr,
Jeff Brown4532e612012-04-05 14:27:12 -0700181 InputChannel fromChannel, InputChannel toChannel);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000182 private static native void nativeSetPointerSpeed(long ptr, int speed);
183 private static native void nativeSetShowTouches(long ptr, boolean enabled);
184 private static native void nativeVibrate(long ptr, int deviceId, long[] pattern,
Jeff Browna47425a2012-04-13 04:09:27 -0700185 int repeat, int token);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000186 private static native void nativeCancelVibrate(long ptr, int deviceId, int token);
187 private static native void nativeReloadKeyboardLayouts(long ptr);
188 private static native void nativeReloadDeviceAliases(long ptr);
189 private static native String nativeDump(long ptr);
190 private static native void nativeMonitor(long ptr);
Jeff Brown4532e612012-04-05 14:27:12 -0700191
Jeff Brownac143512012-04-05 18:57:33 -0700192 // Input event injection constants defined in InputDispatcher.h.
193 private static final int INPUT_EVENT_INJECTION_SUCCEEDED = 0;
194 private static final int INPUT_EVENT_INJECTION_PERMISSION_DENIED = 1;
195 private static final int INPUT_EVENT_INJECTION_FAILED = 2;
196 private static final int INPUT_EVENT_INJECTION_TIMED_OUT = 3;
197
198 // Maximum number of milliseconds to wait for input event injection.
199 private static final int INJECTION_TIMEOUT_MILLIS = 30 * 1000;
200
Jeff Brown6d0fec22010-07-23 21:28:06 -0700201 // Key states (may be returned by queries about the current state of a
202 // particular key code, scan code or switch).
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700203
Jeff Brown6d0fec22010-07-23 21:28:06 -0700204 /** The key state is unknown or the requested key itself is not supported. */
205 public static final int KEY_STATE_UNKNOWN = -1;
206
207 /** The key is up. /*/
208 public static final int KEY_STATE_UP = 0;
209
210 /** The key is down. */
211 public static final int KEY_STATE_DOWN = 1;
212
213 /** The key is down but is a virtual key press that is being emulated by the system. */
214 public static final int KEY_STATE_VIRTUAL = 2;
215
Jeff Brownc458ce92012-04-30 14:58:40 -0700216 /** Scan code: Mouse / trackball button. */
217 public static final int BTN_MOUSE = 0x110;
218
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700219 // Switch code values must match bionic/libc/kernel/common/linux/input.h
Jeff Brownc458ce92012-04-30 14:58:40 -0700220 /** Switch code: Lid switch. When set, lid is shut. */
221 public static final int SW_LID = 0x00;
222
223 /** Switch code: Keypad slide. When set, keyboard is exposed. */
224 public static final int SW_KEYPAD_SLIDE = 0x0a;
225
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700226 /** Switch code: Headphone. When set, headphone is inserted. */
227 public static final int SW_HEADPHONE_INSERT = 0x02;
228
229 /** Switch code: Microphone. When set, microphone is inserted. */
230 public static final int SW_MICROPHONE_INSERT = 0x04;
231
232 /** Switch code: Headphone/Microphone Jack. When set, something is inserted. */
233 public static final int SW_JACK_PHYSICAL_INSERT = 0x07;
234
235 public static final int SW_LID_BIT = 1 << SW_LID;
236 public static final int SW_KEYPAD_SLIDE_BIT = 1 << SW_KEYPAD_SLIDE;
237 public static final int SW_HEADPHONE_INSERT_BIT = 1 << SW_HEADPHONE_INSERT;
238 public static final int SW_MICROPHONE_INSERT_BIT = 1 << SW_MICROPHONE_INSERT;
239 public static final int SW_JACK_PHYSICAL_INSERT_BIT = 1 << SW_JACK_PHYSICAL_INSERT;
240 public static final int SW_JACK_BITS =
241 SW_HEADPHONE_INSERT_BIT | SW_MICROPHONE_INSERT_BIT | SW_JACK_PHYSICAL_INSERT_BIT;
242
243 /** Whether to use the dev/input/event or uevent subsystem for the audio jack. */
244 final boolean mUseDevInputEventForAudioJack;
245
Jeff Brown4ccb8232014-01-16 22:16:42 -0800246 public InputManagerService(Context context) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700247 this.mContext = context;
Jeff Brown4ccb8232014-01-16 22:16:42 -0800248 this.mHandler = new InputManagerHandler(DisplayThread.get().getLooper());
Jeff Brown05dc66a2011-03-02 14:41:58 -0800249
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700250 mUseDevInputEventForAudioJack =
251 context.getResources().getBoolean(R.bool.config_useDevInputEventForAudioJack);
252 Slog.i(TAG, "Initializing input manager, mUseDevInputEventForAudioJack="
253 + mUseDevInputEventForAudioJack);
Jeff Brown4532e612012-04-05 14:27:12 -0700254 mPtr = nativeInit(this, mContext, mHandler.getLooper().getQueue());
Jeff Brown4ccb8232014-01-16 22:16:42 -0800255
256 LocalServices.addService(InputManagerInternal.class, new LocalService());
Jeff Brown46b9ac02010-04-22 18:58:52 -0700257 }
Jeff Brown1a84fd12011-06-02 01:26:32 -0700258
Jeff Browna9d131c2012-09-20 16:48:17 -0700259 public void setWindowManagerCallbacks(WindowManagerCallbacks callbacks) {
260 mWindowManagerCallbacks = callbacks;
261 }
262
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700263 public void setWiredAccessoryCallbacks(WiredAccessoryCallbacks callbacks) {
264 mWiredAccessoryCallbacks = callbacks;
265 }
266
Jeff Brown46b9ac02010-04-22 18:58:52 -0700267 public void start() {
268 Slog.i(TAG, "Starting input manager");
Jeff Brown4532e612012-04-05 14:27:12 -0700269 nativeStart(mPtr);
270
271 // Add ourself to the Watchdog monitors.
272 Watchdog.getInstance().addMonitor(this);
Jeff Brown1a84fd12011-06-02 01:26:32 -0700273
274 registerPointerSpeedSettingObserver();
Jeff Browndaf4a122011-08-26 17:14:14 -0700275 registerShowTouchesSettingObserver();
276
Jeff Brownd4935962012-09-25 13:27:20 -0700277 mContext.registerReceiver(new BroadcastReceiver() {
278 @Override
279 public void onReceive(Context context, Intent intent) {
280 updatePointerSpeedFromSettings();
281 updateShowTouchesFromSettings();
282 }
283 }, new IntentFilter(Intent.ACTION_USER_SWITCHED), null, mHandler);
284
Jeff Brown1a84fd12011-06-02 01:26:32 -0700285 updatePointerSpeedFromSettings();
Jeff Browndaf4a122011-08-26 17:14:14 -0700286 updateShowTouchesFromSettings();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700287 }
Jeff Brown6ec6f792012-04-17 16:52:41 -0700288
Matthew Xie96313142012-06-29 16:57:31 -0700289 // TODO(BT) Pass in paramter for bluetooth system
Svetoslav Ganova0027152013-06-25 14:59:53 -0700290 public void systemRunning() {
Jeff Brown6ec6f792012-04-17 16:52:41 -0700291 if (DEBUG) {
292 Slog.d(TAG, "System ready.");
293 }
Jeff Browncf39bdf2012-05-18 14:41:19 -0700294 mNotificationManager = (NotificationManager)mContext.getSystemService(
295 Context.NOTIFICATION_SERVICE);
Jeff Brown6ec6f792012-04-17 16:52:41 -0700296 mSystemReady = true;
Jeff Brown6ec6f792012-04-17 16:52:41 -0700297
298 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
299 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
300 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
Jeff Brown69b07162013-11-07 00:30:16 -0800301 filter.addAction(Intent.ACTION_PACKAGE_REPLACED);
Jeff Brown6ec6f792012-04-17 16:52:41 -0700302 filter.addDataScheme("package");
303 mContext.registerReceiver(new BroadcastReceiver() {
304 @Override
305 public void onReceive(Context context, Intent intent) {
Jeff Browncf39bdf2012-05-18 14:41:19 -0700306 updateKeyboardLayouts();
Jeff Brown6ec6f792012-04-17 16:52:41 -0700307 }
308 }, filter, null, mHandler);
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700309
310 filter = new IntentFilter(BluetoothDevice.ACTION_ALIAS_CHANGED);
311 mContext.registerReceiver(new BroadcastReceiver() {
312 @Override
313 public void onReceive(Context context, Intent intent) {
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700314 reloadDeviceAliases();
315 }
316 }, filter, null, mHandler);
317
Jeff Browncf39bdf2012-05-18 14:41:19 -0700318 mHandler.sendEmptyMessage(MSG_RELOAD_DEVICE_ALIASES);
319 mHandler.sendEmptyMessage(MSG_UPDATE_KEYBOARD_LAYOUTS);
Jeff Brown6ec6f792012-04-17 16:52:41 -0700320 }
321
322 private void reloadKeyboardLayouts() {
Jeff Browncf39bdf2012-05-18 14:41:19 -0700323 if (DEBUG) {
324 Slog.d(TAG, "Reloading keyboard layouts.");
325 }
Jeff Brown6ec6f792012-04-17 16:52:41 -0700326 nativeReloadKeyboardLayouts(mPtr);
327 }
328
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700329 private void reloadDeviceAliases() {
Jeff Browncf39bdf2012-05-18 14:41:19 -0700330 if (DEBUG) {
331 Slog.d(TAG, "Reloading device names.");
332 }
Jeff Brown5bbd4b42012-04-20 19:28:00 -0700333 nativeReloadDeviceAliases(mPtr);
334 }
335
Jeff Brown4ccb8232014-01-16 22:16:42 -0800336 private void setDisplayViewportsInternal(DisplayViewport defaultViewport,
Jeff Brownd728bf52012-09-08 18:05:28 -0700337 DisplayViewport externalTouchViewport) {
338 if (defaultViewport.valid) {
339 setDisplayViewport(false, defaultViewport);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700340 }
Jeff Brownd728bf52012-09-08 18:05:28 -0700341
342 if (externalTouchViewport.valid) {
343 setDisplayViewport(true, externalTouchViewport);
344 } else if (defaultViewport.valid) {
345 setDisplayViewport(true, defaultViewport);
Jeff Brownb6997262010-10-08 22:31:17 -0700346 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700347 }
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700348
Jeff Brownd728bf52012-09-08 18:05:28 -0700349 private void setDisplayViewport(boolean external, DisplayViewport viewport) {
350 nativeSetDisplayViewport(mPtr, external,
351 viewport.displayId, viewport.orientation,
352 viewport.logicalFrame.left, viewport.logicalFrame.top,
353 viewport.logicalFrame.right, viewport.logicalFrame.bottom,
354 viewport.physicalFrame.left, viewport.physicalFrame.top,
Jeff Brown83d616a2012-09-09 20:33:43 -0700355 viewport.physicalFrame.right, viewport.physicalFrame.bottom,
356 viewport.deviceWidth, viewport.deviceHeight);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700357 }
Jeff Brownac143512012-04-05 18:57:33 -0700358
Jeff Brown6d0fec22010-07-23 21:28:06 -0700359 /**
360 * Gets the current state of a key or button by key code.
361 * @param deviceId The input device id, or -1 to consult all devices.
362 * @param sourceMask The input sources to consult, or {@link InputDevice#SOURCE_ANY} to
363 * consider all input sources. An input device is consulted if at least one of its
364 * non-class input source bits matches the specified source mask.
365 * @param keyCode The key code to check.
366 * @return The key state.
367 */
368 public int getKeyCodeState(int deviceId, int sourceMask, int keyCode) {
Jeff Brown4532e612012-04-05 14:27:12 -0700369 return nativeGetKeyCodeState(mPtr, deviceId, sourceMask, keyCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700370 }
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700371
Jeff Brown6d0fec22010-07-23 21:28:06 -0700372 /**
373 * Gets the current state of a key or button by scan code.
374 * @param deviceId The input device id, or -1 to consult all devices.
375 * @param sourceMask The input sources to consult, or {@link InputDevice#SOURCE_ANY} to
376 * consider all input sources. An input device is consulted if at least one of its
377 * non-class input source bits matches the specified source mask.
378 * @param scanCode The scan code to check.
379 * @return The key state.
380 */
381 public int getScanCodeState(int deviceId, int sourceMask, int scanCode) {
Jeff Brown4532e612012-04-05 14:27:12 -0700382 return nativeGetScanCodeState(mPtr, deviceId, sourceMask, scanCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700383 }
384
Jeff Brown6d0fec22010-07-23 21:28:06 -0700385 /**
386 * Gets the current state of a switch by switch code.
387 * @param deviceId The input device id, or -1 to consult all devices.
388 * @param sourceMask The input sources to consult, or {@link InputDevice#SOURCE_ANY} to
389 * consider all input sources. An input device is consulted if at least one of its
390 * non-class input source bits matches the specified source mask.
391 * @param switchCode The switch code to check.
392 * @return The switch state.
393 */
394 public int getSwitchState(int deviceId, int sourceMask, int switchCode) {
Jeff Brown4532e612012-04-05 14:27:12 -0700395 return nativeGetSwitchState(mPtr, deviceId, sourceMask, switchCode);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700396 }
397
Jeff Brown6d0fec22010-07-23 21:28:06 -0700398 /**
399 * Determines whether the specified key codes are supported by a particular device.
400 * @param deviceId The input device id, or -1 to consult all devices.
401 * @param sourceMask The input sources to consult, or {@link InputDevice#SOURCE_ANY} to
402 * consider all input sources. An input device is consulted if at least one of its
403 * non-class input source bits matches the specified source mask.
404 * @param keyCodes The array of key codes to check.
405 * @param keyExists An array at least as large as keyCodes whose entries will be set
406 * to true or false based on the presence or absence of support for the corresponding
407 * key codes.
408 * @return True if the lookup was successful, false otherwise.
409 */
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700410 @Override // Binder call
Jeff Brown6d0fec22010-07-23 21:28:06 -0700411 public boolean hasKeys(int deviceId, int sourceMask, int[] keyCodes, boolean[] keyExists) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700412 if (keyCodes == null) {
413 throw new IllegalArgumentException("keyCodes must not be null.");
414 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700415 if (keyExists == null || keyExists.length < keyCodes.length) {
416 throw new IllegalArgumentException("keyExists must not be null and must be at "
417 + "least as large as keyCodes.");
Jeff Brown46b9ac02010-04-22 18:58:52 -0700418 }
419
Jeff Brown4532e612012-04-05 14:27:12 -0700420 return nativeHasKeys(mPtr, deviceId, sourceMask, keyCodes, keyExists);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700421 }
422
Jeff Browna41ca772010-08-11 14:46:32 -0700423 /**
424 * Creates an input channel that will receive all input from the input dispatcher.
425 * @param inputChannelName The input channel name.
426 * @return The input channel.
427 */
428 public InputChannel monitorInput(String inputChannelName) {
429 if (inputChannelName == null) {
430 throw new IllegalArgumentException("inputChannelName must not be null.");
431 }
432
433 InputChannel[] inputChannels = InputChannel.openInputChannelPair(inputChannelName);
Jeff Brown4532e612012-04-05 14:27:12 -0700434 nativeRegisterInputChannel(mPtr, inputChannels[0], null, true);
Jeff Browna41ca772010-08-11 14:46:32 -0700435 inputChannels[0].dispose(); // don't need to retain the Java object reference
436 return inputChannels[1];
437 }
438
439 /**
440 * Registers an input channel so that it can be used as an input event target.
441 * @param inputChannel The input channel to register.
Jeff Brown928e0542011-01-10 11:17:36 -0800442 * @param inputWindowHandle The handle of the input window associated with the
443 * input channel, or null if none.
Jeff Browna41ca772010-08-11 14:46:32 -0700444 */
Jeff Brown928e0542011-01-10 11:17:36 -0800445 public void registerInputChannel(InputChannel inputChannel,
446 InputWindowHandle inputWindowHandle) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700447 if (inputChannel == null) {
448 throw new IllegalArgumentException("inputChannel must not be null.");
449 }
450
Jeff Brown4532e612012-04-05 14:27:12 -0700451 nativeRegisterInputChannel(mPtr, inputChannel, inputWindowHandle, false);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700452 }
453
Jeff Browna41ca772010-08-11 14:46:32 -0700454 /**
455 * Unregisters an input channel.
456 * @param inputChannel The input channel to unregister.
457 */
Jeff Brown46b9ac02010-04-22 18:58:52 -0700458 public void unregisterInputChannel(InputChannel inputChannel) {
459 if (inputChannel == null) {
460 throw new IllegalArgumentException("inputChannel must not be null.");
461 }
462
Jeff Brown4532e612012-04-05 14:27:12 -0700463 nativeUnregisterInputChannel(mPtr, inputChannel);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700464 }
Jeff Brown0029c662011-03-30 02:25:18 -0700465
466 /**
467 * Sets an input filter that will receive all input events before they are dispatched.
468 * The input filter may then reinterpret input events or inject new ones.
469 *
470 * To ensure consistency, the input dispatcher automatically drops all events
471 * in progress whenever an input filter is installed or uninstalled. After an input
472 * filter is uninstalled, it can no longer send input events unless it is reinstalled.
473 * Any events it attempts to send after it has been uninstalled will be dropped.
474 *
475 * @param filter The input filter, or null to remove the current filter.
476 */
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -0700477 public void setInputFilter(IInputFilter filter) {
Jeff Brown0029c662011-03-30 02:25:18 -0700478 synchronized (mInputFilterLock) {
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -0700479 final IInputFilter oldFilter = mInputFilter;
Jeff Brown0029c662011-03-30 02:25:18 -0700480 if (oldFilter == filter) {
481 return; // nothing to do
482 }
483
484 if (oldFilter != null) {
485 mInputFilter = null;
486 mInputFilterHost.disconnectLocked();
487 mInputFilterHost = null;
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -0700488 try {
489 oldFilter.uninstall();
490 } catch (RemoteException re) {
491 /* ignore */
492 }
Jeff Brown0029c662011-03-30 02:25:18 -0700493 }
494
495 if (filter != null) {
496 mInputFilter = filter;
497 mInputFilterHost = new InputFilterHost();
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -0700498 try {
499 filter.install(mInputFilterHost);
500 } catch (RemoteException re) {
501 /* ignore */
502 }
Jeff Brown0029c662011-03-30 02:25:18 -0700503 }
504
Jeff Brown4532e612012-04-05 14:27:12 -0700505 nativeSetInputFilterEnabled(mPtr, filter != null);
Jeff Brown0029c662011-03-30 02:25:18 -0700506 }
507 }
508
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700509 @Override // Binder call
Jeff Brownac143512012-04-05 18:57:33 -0700510 public boolean injectInputEvent(InputEvent event, int mode) {
Jeff Brown7fbdc842010-06-17 20:52:56 -0700511 if (event == null) {
512 throw new IllegalArgumentException("event must not be null");
513 }
Jeff Brownac143512012-04-05 18:57:33 -0700514 if (mode != InputManager.INJECT_INPUT_EVENT_MODE_ASYNC
515 && mode != InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH
516 && mode != InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_RESULT) {
517 throw new IllegalArgumentException("mode is invalid");
Jeff Brown7fbdc842010-06-17 20:52:56 -0700518 }
Jeff Brown6ec402b2010-07-28 15:48:59 -0700519
Jeff Brownac143512012-04-05 18:57:33 -0700520 final int pid = Binder.getCallingPid();
521 final int uid = Binder.getCallingUid();
522 final long ident = Binder.clearCallingIdentity();
523 final int result;
524 try {
525 result = nativeInjectInputEvent(mPtr, event, pid, uid, mode,
526 INJECTION_TIMEOUT_MILLIS, WindowManagerPolicy.FLAG_DISABLE_KEY_REPEAT);
527 } finally {
528 Binder.restoreCallingIdentity(ident);
529 }
530 switch (result) {
531 case INPUT_EVENT_INJECTION_PERMISSION_DENIED:
532 Slog.w(TAG, "Input event injection from pid " + pid + " permission denied.");
533 throw new SecurityException(
534 "Injecting to another application requires INJECT_EVENTS permission");
535 case INPUT_EVENT_INJECTION_SUCCEEDED:
536 return true;
537 case INPUT_EVENT_INJECTION_TIMED_OUT:
538 Slog.w(TAG, "Input event injection from pid " + pid + " timed out.");
539 return false;
540 case INPUT_EVENT_INJECTION_FAILED:
541 default:
542 Slog.w(TAG, "Input event injection from pid " + pid + " failed.");
543 return false;
544 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700545 }
Jeff Brown0029c662011-03-30 02:25:18 -0700546
Jeff Brown8d608662010-08-30 03:02:23 -0700547 /**
548 * Gets information about the input device with the specified id.
Craig Mautner2f39e9f2012-09-21 11:39:54 -0700549 * @param deviceId The device id.
Jeff Brown8d608662010-08-30 03:02:23 -0700550 * @return The input device or null if not found.
551 */
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700552 @Override // Binder call
Jeff Brown8d608662010-08-30 03:02:23 -0700553 public InputDevice getInputDevice(int deviceId) {
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700554 synchronized (mInputDevicesLock) {
555 final int count = mInputDevices.length;
556 for (int i = 0; i < count; i++) {
557 final InputDevice inputDevice = mInputDevices[i];
558 if (inputDevice.getId() == deviceId) {
559 return inputDevice;
560 }
561 }
562 }
563 return null;
Jeff Brown8d608662010-08-30 03:02:23 -0700564 }
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700565
Jeff Brown8d608662010-08-30 03:02:23 -0700566 /**
567 * Gets the ids of all input devices in the system.
568 * @return The input device ids.
569 */
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700570 @Override // Binder call
Jeff Brown8d608662010-08-30 03:02:23 -0700571 public int[] getInputDeviceIds() {
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700572 synchronized (mInputDevicesLock) {
573 final int count = mInputDevices.length;
574 int[] ids = new int[count];
575 for (int i = 0; i < count; i++) {
576 ids[i] = mInputDevices[i].getId();
577 }
578 return ids;
579 }
580 }
581
Jeff Browndaa37532012-05-01 15:54:03 -0700582 /**
583 * Gets all input devices in the system.
584 * @return The array of input devices.
585 */
586 public InputDevice[] getInputDevices() {
587 synchronized (mInputDevicesLock) {
588 return mInputDevices;
589 }
590 }
591
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700592 @Override // Binder call
593 public void registerInputDevicesChangedListener(IInputDevicesChangedListener listener) {
594 if (listener == null) {
595 throw new IllegalArgumentException("listener must not be null");
596 }
597
598 synchronized (mInputDevicesLock) {
599 int callingPid = Binder.getCallingPid();
600 if (mInputDevicesChangedListeners.get(callingPid) != null) {
601 throw new SecurityException("The calling process has already "
602 + "registered an InputDevicesChangedListener.");
603 }
604
605 InputDevicesChangedListenerRecord record =
606 new InputDevicesChangedListenerRecord(callingPid, listener);
607 try {
608 IBinder binder = listener.asBinder();
609 binder.linkToDeath(record, 0);
610 } catch (RemoteException ex) {
611 // give up
612 throw new RuntimeException(ex);
613 }
614
615 mInputDevicesChangedListeners.put(callingPid, record);
616 }
617 }
618
619 private void onInputDevicesChangedListenerDied(int pid) {
620 synchronized (mInputDevicesLock) {
621 mInputDevicesChangedListeners.remove(pid);
622 }
623 }
624
625 // Must be called on handler.
Jeff Browncf39bdf2012-05-18 14:41:19 -0700626 private void deliverInputDevicesChanged(InputDevice[] oldInputDevices) {
627 // Scan for changes.
628 int numFullKeyboardsAdded = 0;
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700629 mTempInputDevicesChangedListenersToNotify.clear();
Jeff Browncf39bdf2012-05-18 14:41:19 -0700630 mTempFullKeyboards.clear();
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700631 final int numListeners;
632 final int[] deviceIdAndGeneration;
633 synchronized (mInputDevicesLock) {
634 if (!mInputDevicesChangedPending) {
635 return;
636 }
637 mInputDevicesChangedPending = false;
638
639 numListeners = mInputDevicesChangedListeners.size();
640 for (int i = 0; i < numListeners; i++) {
641 mTempInputDevicesChangedListenersToNotify.add(
642 mInputDevicesChangedListeners.valueAt(i));
643 }
644
645 final int numDevices = mInputDevices.length;
646 deviceIdAndGeneration = new int[numDevices * 2];
647 for (int i = 0; i < numDevices; i++) {
648 final InputDevice inputDevice = mInputDevices[i];
649 deviceIdAndGeneration[i * 2] = inputDevice.getId();
650 deviceIdAndGeneration[i * 2 + 1] = inputDevice.getGeneration();
Jeff Browncf39bdf2012-05-18 14:41:19 -0700651
Jeff Brown7e4ff4b2012-05-30 14:32:16 -0700652 if (!inputDevice.isVirtual() && inputDevice.isFullKeyboard()) {
Jeff Browncf39bdf2012-05-18 14:41:19 -0700653 if (!containsInputDeviceWithDescriptor(oldInputDevices,
654 inputDevice.getDescriptor())) {
655 mTempFullKeyboards.add(numFullKeyboardsAdded++, inputDevice);
656 } else {
657 mTempFullKeyboards.add(inputDevice);
658 }
659 }
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700660 }
661 }
662
Jeff Browncf39bdf2012-05-18 14:41:19 -0700663 // Notify listeners.
Jeff Brownaf9e8d32012-04-12 17:32:48 -0700664 for (int i = 0; i < numListeners; i++) {
665 mTempInputDevicesChangedListenersToNotify.get(i).notifyInputDevicesChanged(
666 deviceIdAndGeneration);
667 }
Jeff Browncf39bdf2012-05-18 14:41:19 -0700668 mTempInputDevicesChangedListenersToNotify.clear();
669
670 // Check for missing keyboard layouts.
671 if (mNotificationManager != null) {
672 final int numFullKeyboards = mTempFullKeyboards.size();
673 boolean missingLayoutForExternalKeyboard = false;
674 boolean missingLayoutForExternalKeyboardAdded = false;
675 synchronized (mDataStore) {
676 for (int i = 0; i < numFullKeyboards; i++) {
677 final InputDevice inputDevice = mTempFullKeyboards.get(i);
678 if (mDataStore.getCurrentKeyboardLayout(inputDevice.getDescriptor()) == null) {
679 missingLayoutForExternalKeyboard = true;
680 if (i < numFullKeyboardsAdded) {
681 missingLayoutForExternalKeyboardAdded = true;
682 }
683 }
684 }
685 }
686 if (missingLayoutForExternalKeyboard) {
687 if (missingLayoutForExternalKeyboardAdded) {
688 showMissingKeyboardLayoutNotification();
689 }
690 } else if (mKeyboardLayoutNotificationShown) {
691 hideMissingKeyboardLayoutNotification();
692 }
693 }
694 mTempFullKeyboards.clear();
695 }
696
697 // Must be called on handler.
698 private void showMissingKeyboardLayoutNotification() {
699 if (!mKeyboardLayoutNotificationShown) {
700 if (mKeyboardLayoutIntent == null) {
701 final Intent intent = new Intent("android.settings.INPUT_METHOD_SETTINGS");
702 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
703 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
704 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700705 mKeyboardLayoutIntent = PendingIntent.getActivityAsUser(mContext, 0,
706 intent, 0, null, UserHandle.CURRENT);
Jeff Browncf39bdf2012-05-18 14:41:19 -0700707 }
708
709 Resources r = mContext.getResources();
710 Notification notification = new Notification.Builder(mContext)
711 .setContentTitle(r.getString(
712 R.string.select_keyboard_layout_notification_title))
713 .setContentText(r.getString(
714 R.string.select_keyboard_layout_notification_message))
715 .setContentIntent(mKeyboardLayoutIntent)
716 .setSmallIcon(R.drawable.ic_settings_language)
717 .setPriority(Notification.PRIORITY_LOW)
718 .build();
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700719 mNotificationManager.notifyAsUser(null,
720 R.string.select_keyboard_layout_notification_title,
721 notification, UserHandle.ALL);
Jeff Browncf39bdf2012-05-18 14:41:19 -0700722 mKeyboardLayoutNotificationShown = true;
723 }
724 }
725
726 // Must be called on handler.
727 private void hideMissingKeyboardLayoutNotification() {
728 if (mKeyboardLayoutNotificationShown) {
729 mKeyboardLayoutNotificationShown = false;
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700730 mNotificationManager.cancelAsUser(null,
731 R.string.select_keyboard_layout_notification_title,
732 UserHandle.ALL);
Jeff Browncf39bdf2012-05-18 14:41:19 -0700733 }
734 }
735
736 // Must be called on handler.
737 private void updateKeyboardLayouts() {
738 // Scan all input devices state for keyboard layouts that have been uninstalled.
739 final HashSet<String> availableKeyboardLayouts = new HashSet<String>();
740 visitAllKeyboardLayouts(new KeyboardLayoutVisitor() {
741 @Override
742 public void visitKeyboardLayout(Resources resources,
743 String descriptor, String label, String collection, int keyboardLayoutResId) {
744 availableKeyboardLayouts.add(descriptor);
745 }
746 });
747 synchronized (mDataStore) {
748 try {
749 mDataStore.removeUninstalledKeyboardLayouts(availableKeyboardLayouts);
750 } finally {
751 mDataStore.saveIfNeeded();
752 }
753 }
754
755 // Reload keyboard layouts.
756 reloadKeyboardLayouts();
757 }
758
Jeff Browncf39bdf2012-05-18 14:41:19 -0700759 private static boolean containsInputDeviceWithDescriptor(InputDevice[] inputDevices,
760 String descriptor) {
761 final int numDevices = inputDevices.length;
762 for (int i = 0; i < numDevices; i++) {
763 final InputDevice inputDevice = inputDevices[i];
764 if (inputDevice.getDescriptor().equals(descriptor)) {
765 return true;
766 }
767 }
768 return false;
Jeff Brown8d608662010-08-30 03:02:23 -0700769 }
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700770
771 @Override // Binder call
772 public KeyboardLayout[] getKeyboardLayouts() {
Jeff Brown6ec6f792012-04-17 16:52:41 -0700773 final ArrayList<KeyboardLayout> list = new ArrayList<KeyboardLayout>();
774 visitAllKeyboardLayouts(new KeyboardLayoutVisitor() {
775 @Override
776 public void visitKeyboardLayout(Resources resources,
Jeff Brownd9fec5d2012-05-17 16:01:54 -0700777 String descriptor, String label, String collection, int keyboardLayoutResId) {
778 list.add(new KeyboardLayout(descriptor, label, collection));
Jeff Brown6ec6f792012-04-17 16:52:41 -0700779 }
780 });
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700781 return list.toArray(new KeyboardLayout[list.size()]);
782 }
783
784 @Override // Binder call
785 public KeyboardLayout getKeyboardLayout(String keyboardLayoutDescriptor) {
786 if (keyboardLayoutDescriptor == null) {
787 throw new IllegalArgumentException("keyboardLayoutDescriptor must not be null");
788 }
789
Jeff Brown6ec6f792012-04-17 16:52:41 -0700790 final KeyboardLayout[] result = new KeyboardLayout[1];
791 visitKeyboardLayout(keyboardLayoutDescriptor, new KeyboardLayoutVisitor() {
792 @Override
793 public void visitKeyboardLayout(Resources resources,
Jeff Brownd9fec5d2012-05-17 16:01:54 -0700794 String descriptor, String label, String collection, int keyboardLayoutResId) {
795 result[0] = new KeyboardLayout(descriptor, label, collection);
Jeff Brown6ec6f792012-04-17 16:52:41 -0700796 }
797 });
798 if (result[0] == null) {
799 Log.w(TAG, "Could not get keyboard layout with descriptor '"
800 + keyboardLayoutDescriptor + "'.");
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700801 }
Jeff Brown6ec6f792012-04-17 16:52:41 -0700802 return result[0];
803 }
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700804
Jeff Brown6ec6f792012-04-17 16:52:41 -0700805 private void visitAllKeyboardLayouts(KeyboardLayoutVisitor visitor) {
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700806 final PackageManager pm = mContext.getPackageManager();
Jeff Brown6ec6f792012-04-17 16:52:41 -0700807 Intent intent = new Intent(InputManager.ACTION_QUERY_KEYBOARD_LAYOUTS);
808 for (ResolveInfo resolveInfo : pm.queryBroadcastReceivers(intent,
809 PackageManager.GET_META_DATA)) {
810 visitKeyboardLayoutsInPackage(pm, resolveInfo.activityInfo, null, visitor);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700811 }
812 }
813
Jeff Brown6ec6f792012-04-17 16:52:41 -0700814 private void visitKeyboardLayout(String keyboardLayoutDescriptor,
815 KeyboardLayoutVisitor visitor) {
816 KeyboardLayoutDescriptor d = KeyboardLayoutDescriptor.parse(keyboardLayoutDescriptor);
817 if (d != null) {
818 final PackageManager pm = mContext.getPackageManager();
819 try {
820 ActivityInfo receiver = pm.getReceiverInfo(
821 new ComponentName(d.packageName, d.receiverName),
822 PackageManager.GET_META_DATA);
823 visitKeyboardLayoutsInPackage(pm, receiver, d.keyboardLayoutName, visitor);
824 } catch (NameNotFoundException ex) {
825 }
826 }
827 }
828
829 private void visitKeyboardLayoutsInPackage(PackageManager pm, ActivityInfo receiver,
830 String keyboardName, KeyboardLayoutVisitor visitor) {
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700831 Bundle metaData = receiver.metaData;
832 if (metaData == null) {
Jeff Brown6ec6f792012-04-17 16:52:41 -0700833 return;
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700834 }
835
836 int configResId = metaData.getInt(InputManager.META_DATA_KEYBOARD_LAYOUTS);
837 if (configResId == 0) {
838 Log.w(TAG, "Missing meta-data '" + InputManager.META_DATA_KEYBOARD_LAYOUTS
839 + "' on receiver " + receiver.packageName + "/" + receiver.name);
Jeff Brown6ec6f792012-04-17 16:52:41 -0700840 return;
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700841 }
842
Jeff Brownd9fec5d2012-05-17 16:01:54 -0700843 CharSequence receiverLabel = receiver.loadLabel(pm);
844 String collection = receiverLabel != null ? receiverLabel.toString() : "";
845
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700846 try {
847 Resources resources = pm.getResourcesForApplication(receiver.applicationInfo);
848 XmlResourceParser parser = resources.getXml(configResId);
849 try {
850 XmlUtils.beginDocument(parser, "keyboard-layouts");
851
852 for (;;) {
853 XmlUtils.nextElement(parser);
854 String element = parser.getName();
855 if (element == null) {
856 break;
857 }
858 if (element.equals("keyboard-layout")) {
859 TypedArray a = resources.obtainAttributes(
860 parser, com.android.internal.R.styleable.KeyboardLayout);
861 try {
862 String name = a.getString(
863 com.android.internal.R.styleable.KeyboardLayout_name);
864 String label = a.getString(
865 com.android.internal.R.styleable.KeyboardLayout_label);
Jeff Brown2f095762012-05-10 21:29:33 -0700866 int keyboardLayoutResId = a.getResourceId(
867 com.android.internal.R.styleable.KeyboardLayout_keyboardLayout,
868 0);
869 if (name == null || label == null || keyboardLayoutResId == 0) {
870 Log.w(TAG, "Missing required 'name', 'label' or 'keyboardLayout' "
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700871 + "attributes in keyboard layout "
872 + "resource from receiver "
873 + receiver.packageName + "/" + receiver.name);
874 } else {
875 String descriptor = KeyboardLayoutDescriptor.format(
876 receiver.packageName, receiver.name, name);
Jeff Brown6ec6f792012-04-17 16:52:41 -0700877 if (keyboardName == null || name.equals(keyboardName)) {
878 visitor.visitKeyboardLayout(resources, descriptor,
Jeff Brownd9fec5d2012-05-17 16:01:54 -0700879 label, collection, keyboardLayoutResId);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700880 }
881 }
882 } finally {
883 a.recycle();
884 }
885 } else {
886 Log.w(TAG, "Skipping unrecognized element '" + element
887 + "' in keyboard layout resource from receiver "
888 + receiver.packageName + "/" + receiver.name);
889 }
890 }
891 } finally {
892 parser.close();
893 }
894 } catch (Exception ex) {
Jeff Brown6ec6f792012-04-17 16:52:41 -0700895 Log.w(TAG, "Could not parse keyboard layout resource from receiver "
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700896 + receiver.packageName + "/" + receiver.name, ex);
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700897 }
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700898 }
899
900 @Override // Binder call
Jeff Browncf39bdf2012-05-18 14:41:19 -0700901 public String getCurrentKeyboardLayoutForInputDevice(String inputDeviceDescriptor) {
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700902 if (inputDeviceDescriptor == null) {
903 throw new IllegalArgumentException("inputDeviceDescriptor must not be null");
904 }
905
Jeff Browna3bc5652012-04-17 11:42:25 -0700906 synchronized (mDataStore) {
Jeff Browncf39bdf2012-05-18 14:41:19 -0700907 return mDataStore.getCurrentKeyboardLayout(inputDeviceDescriptor);
Jeff Browna3bc5652012-04-17 11:42:25 -0700908 }
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700909 }
910
911 @Override // Binder call
Jeff Browncf39bdf2012-05-18 14:41:19 -0700912 public void setCurrentKeyboardLayoutForInputDevice(String inputDeviceDescriptor,
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700913 String keyboardLayoutDescriptor) {
914 if (!checkCallingPermission(android.Manifest.permission.SET_KEYBOARD_LAYOUT,
Jeff Browncf39bdf2012-05-18 14:41:19 -0700915 "setCurrentKeyboardLayoutForInputDevice()")) {
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700916 throw new SecurityException("Requires SET_KEYBOARD_LAYOUT permission");
917 }
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700918 if (inputDeviceDescriptor == null) {
919 throw new IllegalArgumentException("inputDeviceDescriptor must not be null");
920 }
Jeff Browncf39bdf2012-05-18 14:41:19 -0700921 if (keyboardLayoutDescriptor == null) {
922 throw new IllegalArgumentException("keyboardLayoutDescriptor must not be null");
923 }
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700924
Jeff Browna3bc5652012-04-17 11:42:25 -0700925 synchronized (mDataStore) {
926 try {
Jeff Browncf39bdf2012-05-18 14:41:19 -0700927 if (mDataStore.setCurrentKeyboardLayout(
928 inputDeviceDescriptor, keyboardLayoutDescriptor)) {
929 mHandler.sendEmptyMessage(MSG_RELOAD_KEYBOARD_LAYOUTS);
930 }
Jeff Browna3bc5652012-04-17 11:42:25 -0700931 } finally {
932 mDataStore.saveIfNeeded();
933 }
934 }
Jeff Browncf39bdf2012-05-18 14:41:19 -0700935 }
Jeff Brown6ec6f792012-04-17 16:52:41 -0700936
Jeff Browncf39bdf2012-05-18 14:41:19 -0700937 @Override // Binder call
938 public String[] getKeyboardLayoutsForInputDevice(String inputDeviceDescriptor) {
939 if (inputDeviceDescriptor == null) {
940 throw new IllegalArgumentException("inputDeviceDescriptor must not be null");
941 }
942
943 synchronized (mDataStore) {
944 return mDataStore.getKeyboardLayouts(inputDeviceDescriptor);
945 }
946 }
947
948 @Override // Binder call
949 public void addKeyboardLayoutForInputDevice(String inputDeviceDescriptor,
950 String keyboardLayoutDescriptor) {
951 if (!checkCallingPermission(android.Manifest.permission.SET_KEYBOARD_LAYOUT,
952 "addKeyboardLayoutForInputDevice()")) {
953 throw new SecurityException("Requires SET_KEYBOARD_LAYOUT permission");
954 }
955 if (inputDeviceDescriptor == null) {
956 throw new IllegalArgumentException("inputDeviceDescriptor must not be null");
957 }
958 if (keyboardLayoutDescriptor == null) {
959 throw new IllegalArgumentException("keyboardLayoutDescriptor must not be null");
960 }
961
962 synchronized (mDataStore) {
963 try {
964 String oldLayout = mDataStore.getCurrentKeyboardLayout(inputDeviceDescriptor);
965 if (mDataStore.addKeyboardLayout(inputDeviceDescriptor, keyboardLayoutDescriptor)
966 && !Objects.equal(oldLayout,
967 mDataStore.getCurrentKeyboardLayout(inputDeviceDescriptor))) {
968 mHandler.sendEmptyMessage(MSG_RELOAD_KEYBOARD_LAYOUTS);
969 }
970 } finally {
971 mDataStore.saveIfNeeded();
Jeff Brown6ec6f792012-04-17 16:52:41 -0700972 }
Jeff Browncf39bdf2012-05-18 14:41:19 -0700973 }
974 }
975
976 @Override // Binder call
977 public void removeKeyboardLayoutForInputDevice(String inputDeviceDescriptor,
978 String keyboardLayoutDescriptor) {
979 if (!checkCallingPermission(android.Manifest.permission.SET_KEYBOARD_LAYOUT,
980 "removeKeyboardLayoutForInputDevice()")) {
981 throw new SecurityException("Requires SET_KEYBOARD_LAYOUT permission");
982 }
983 if (inputDeviceDescriptor == null) {
984 throw new IllegalArgumentException("inputDeviceDescriptor must not be null");
985 }
986 if (keyboardLayoutDescriptor == null) {
987 throw new IllegalArgumentException("keyboardLayoutDescriptor must not be null");
988 }
989
990 synchronized (mDataStore) {
991 try {
992 String oldLayout = mDataStore.getCurrentKeyboardLayout(inputDeviceDescriptor);
993 if (mDataStore.removeKeyboardLayout(inputDeviceDescriptor,
994 keyboardLayoutDescriptor)
995 && !Objects.equal(oldLayout,
996 mDataStore.getCurrentKeyboardLayout(inputDeviceDescriptor))) {
997 mHandler.sendEmptyMessage(MSG_RELOAD_KEYBOARD_LAYOUTS);
998 }
999 } finally {
1000 mDataStore.saveIfNeeded();
1001 }
1002 }
1003 }
1004
1005 public void switchKeyboardLayout(int deviceId, int direction) {
1006 mHandler.obtainMessage(MSG_SWITCH_KEYBOARD_LAYOUT, deviceId, direction).sendToTarget();
1007 }
1008
1009 // Must be called on handler.
1010 private void handleSwitchKeyboardLayout(int deviceId, int direction) {
1011 final InputDevice device = getInputDevice(deviceId);
Jeff Browncf39bdf2012-05-18 14:41:19 -07001012 if (device != null) {
Craig Mautner2f39e9f2012-09-21 11:39:54 -07001013 final String inputDeviceDescriptor = device.getDescriptor();
Jeff Browncf39bdf2012-05-18 14:41:19 -07001014 final boolean changed;
1015 final String keyboardLayoutDescriptor;
1016 synchronized (mDataStore) {
1017 try {
1018 changed = mDataStore.switchKeyboardLayout(inputDeviceDescriptor, direction);
1019 keyboardLayoutDescriptor = mDataStore.getCurrentKeyboardLayout(
1020 inputDeviceDescriptor);
1021 } finally {
1022 mDataStore.saveIfNeeded();
1023 }
1024 }
1025
1026 if (changed) {
1027 if (mSwitchedKeyboardLayoutToast != null) {
1028 mSwitchedKeyboardLayoutToast.cancel();
1029 mSwitchedKeyboardLayoutToast = null;
1030 }
1031 if (keyboardLayoutDescriptor != null) {
1032 KeyboardLayout keyboardLayout = getKeyboardLayout(keyboardLayoutDescriptor);
1033 if (keyboardLayout != null) {
1034 mSwitchedKeyboardLayoutToast = Toast.makeText(
1035 mContext, keyboardLayout.getLabel(), Toast.LENGTH_SHORT);
1036 mSwitchedKeyboardLayoutToast.show();
1037 }
1038 }
1039
1040 reloadKeyboardLayouts();
1041 }
Jeff Brown6ec6f792012-04-17 16:52:41 -07001042 }
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001043 }
1044
Jeff Brown9302c872011-07-13 22:51:29 -07001045 public void setInputWindows(InputWindowHandle[] windowHandles) {
Jeff Brown4532e612012-04-05 14:27:12 -07001046 nativeSetInputWindows(mPtr, windowHandles);
Jeff Brown349703e2010-06-22 01:27:15 -07001047 }
1048
Jeff Brown9302c872011-07-13 22:51:29 -07001049 public void setFocusedApplication(InputApplicationHandle application) {
Jeff Brown4532e612012-04-05 14:27:12 -07001050 nativeSetFocusedApplication(mPtr, application);
Jeff Brown349703e2010-06-22 01:27:15 -07001051 }
1052
Jeff Brown349703e2010-06-22 01:27:15 -07001053 public void setInputDispatchMode(boolean enabled, boolean frozen) {
Jeff Brown4532e612012-04-05 14:27:12 -07001054 nativeSetInputDispatchMode(mPtr, enabled, frozen);
Jeff Brown349703e2010-06-22 01:27:15 -07001055 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08001056
1057 public void setSystemUiVisibility(int visibility) {
Jeff Brown4532e612012-04-05 14:27:12 -07001058 nativeSetSystemUiVisibility(mPtr, visibility);
Jeff Brown05dc66a2011-03-02 14:41:58 -08001059 }
1060
Jeff Browne6504122010-09-27 14:52:15 -07001061 /**
1062 * Atomically transfers touch focus from one window to another as identified by
1063 * their input channels. It is possible for multiple windows to have
1064 * touch focus if they support split touch dispatch
1065 * {@link android.view.WindowManager.LayoutParams#FLAG_SPLIT_TOUCH} but this
1066 * method only transfers touch focus of the specified window without affecting
1067 * other windows that may also have touch focus at the same time.
1068 * @param fromChannel The channel of a window that currently has touch focus.
1069 * @param toChannel The channel of the window that should receive touch focus in
1070 * place of the first.
1071 * @return True if the transfer was successful. False if the window with the
1072 * specified channel did not actually have touch focus at the time of the request.
1073 */
1074 public boolean transferTouchFocus(InputChannel fromChannel, InputChannel toChannel) {
1075 if (fromChannel == null) {
1076 throw new IllegalArgumentException("fromChannel must not be null.");
1077 }
1078 if (toChannel == null) {
1079 throw new IllegalArgumentException("toChannel must not be null.");
1080 }
Jeff Brown4532e612012-04-05 14:27:12 -07001081 return nativeTransferTouchFocus(mPtr, fromChannel, toChannel);
Jeff Browne6504122010-09-27 14:52:15 -07001082 }
Jeff Brown05dc66a2011-03-02 14:41:58 -08001083
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001084 @Override // Binder call
Jeff Brownac143512012-04-05 18:57:33 -07001085 public void tryPointerSpeed(int speed) {
1086 if (!checkCallingPermission(android.Manifest.permission.SET_POINTER_SPEED,
1087 "tryPointerSpeed()")) {
1088 throw new SecurityException("Requires SET_POINTER_SPEED permission");
1089 }
1090
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001091 if (speed < InputManager.MIN_POINTER_SPEED || speed > InputManager.MAX_POINTER_SPEED) {
1092 throw new IllegalArgumentException("speed out of range");
1093 }
1094
Jeff Brownac143512012-04-05 18:57:33 -07001095 setPointerSpeedUnchecked(speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001096 }
1097
1098 public void updatePointerSpeedFromSettings() {
Jeff Brownac143512012-04-05 18:57:33 -07001099 int speed = getPointerSpeedSetting();
1100 setPointerSpeedUnchecked(speed);
1101 }
1102
1103 private void setPointerSpeedUnchecked(int speed) {
1104 speed = Math.min(Math.max(speed, InputManager.MIN_POINTER_SPEED),
1105 InputManager.MAX_POINTER_SPEED);
1106 nativeSetPointerSpeed(mPtr, speed);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001107 }
1108
1109 private void registerPointerSpeedSettingObserver() {
1110 mContext.getContentResolver().registerContentObserver(
1111 Settings.System.getUriFor(Settings.System.POINTER_SPEED), true,
Jeff Brown4532e612012-04-05 14:27:12 -07001112 new ContentObserver(mHandler) {
Jeff Brown1a84fd12011-06-02 01:26:32 -07001113 @Override
1114 public void onChange(boolean selfChange) {
1115 updatePointerSpeedFromSettings();
1116 }
Jeff Brownd4935962012-09-25 13:27:20 -07001117 }, UserHandle.USER_ALL);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001118 }
1119
Jeff Brownac143512012-04-05 18:57:33 -07001120 private int getPointerSpeedSetting() {
1121 int speed = InputManager.DEFAULT_POINTER_SPEED;
Jeff Brown1a84fd12011-06-02 01:26:32 -07001122 try {
Jeff Brownd4935962012-09-25 13:27:20 -07001123 speed = Settings.System.getIntForUser(mContext.getContentResolver(),
1124 Settings.System.POINTER_SPEED, UserHandle.USER_CURRENT);
Jeff Brown1a84fd12011-06-02 01:26:32 -07001125 } catch (SettingNotFoundException snfe) {
1126 }
1127 return speed;
1128 }
1129
Jeff Browndaf4a122011-08-26 17:14:14 -07001130 public void updateShowTouchesFromSettings() {
1131 int setting = getShowTouchesSetting(0);
Jeff Brown4532e612012-04-05 14:27:12 -07001132 nativeSetShowTouches(mPtr, setting != 0);
Jeff Browndaf4a122011-08-26 17:14:14 -07001133 }
1134
1135 private void registerShowTouchesSettingObserver() {
1136 mContext.getContentResolver().registerContentObserver(
1137 Settings.System.getUriFor(Settings.System.SHOW_TOUCHES), true,
Jeff Brown4532e612012-04-05 14:27:12 -07001138 new ContentObserver(mHandler) {
Jeff Browndaf4a122011-08-26 17:14:14 -07001139 @Override
1140 public void onChange(boolean selfChange) {
1141 updateShowTouchesFromSettings();
1142 }
Jeff Brownd4935962012-09-25 13:27:20 -07001143 }, UserHandle.USER_ALL);
Jeff Browndaf4a122011-08-26 17:14:14 -07001144 }
1145
1146 private int getShowTouchesSetting(int defaultValue) {
1147 int result = defaultValue;
1148 try {
Jeff Brownd4935962012-09-25 13:27:20 -07001149 result = Settings.System.getIntForUser(mContext.getContentResolver(),
1150 Settings.System.SHOW_TOUCHES, UserHandle.USER_CURRENT);
Jeff Browndaf4a122011-08-26 17:14:14 -07001151 } catch (SettingNotFoundException snfe) {
1152 }
1153 return result;
1154 }
1155
Jeff Browna47425a2012-04-13 04:09:27 -07001156 // Binder call
1157 @Override
1158 public void vibrate(int deviceId, long[] pattern, int repeat, IBinder token) {
1159 if (repeat >= pattern.length) {
1160 throw new ArrayIndexOutOfBoundsException();
1161 }
1162
1163 VibratorToken v;
1164 synchronized (mVibratorLock) {
1165 v = mVibratorTokens.get(token);
1166 if (v == null) {
1167 v = new VibratorToken(deviceId, token, mNextVibratorTokenValue++);
1168 try {
1169 token.linkToDeath(v, 0);
1170 } catch (RemoteException ex) {
1171 // give up
1172 throw new RuntimeException(ex);
1173 }
1174 mVibratorTokens.put(token, v);
1175 }
1176 }
1177
1178 synchronized (v) {
1179 v.mVibrating = true;
1180 nativeVibrate(mPtr, deviceId, pattern, repeat, v.mTokenValue);
1181 }
1182 }
1183
1184 // Binder call
1185 @Override
1186 public void cancelVibrate(int deviceId, IBinder token) {
1187 VibratorToken v;
1188 synchronized (mVibratorLock) {
1189 v = mVibratorTokens.get(token);
1190 if (v == null || v.mDeviceId != deviceId) {
1191 return; // nothing to cancel
1192 }
1193 }
1194
1195 cancelVibrateIfNeeded(v);
1196 }
1197
1198 void onVibratorTokenDied(VibratorToken v) {
1199 synchronized (mVibratorLock) {
1200 mVibratorTokens.remove(v.mToken);
1201 }
1202
1203 cancelVibrateIfNeeded(v);
1204 }
1205
1206 private void cancelVibrateIfNeeded(VibratorToken v) {
1207 synchronized (v) {
1208 if (v.mVibrating) {
1209 nativeCancelVibrate(mPtr, v.mDeviceId, v.mTokenValue);
1210 v.mVibrating = false;
1211 }
1212 }
1213 }
1214
Jeff Brown4532e612012-04-05 14:27:12 -07001215 @Override
1216 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Browna3bc5652012-04-17 11:42:25 -07001217 if (mContext.checkCallingOrSelfPermission(Manifest.permission.DUMP)
Jeff Brown4532e612012-04-05 14:27:12 -07001218 != PackageManager.PERMISSION_GRANTED) {
1219 pw.println("Permission Denial: can't dump InputManager from from pid="
1220 + Binder.getCallingPid()
1221 + ", uid=" + Binder.getCallingUid());
1222 return;
1223 }
1224
1225 pw.println("INPUT MANAGER (dumpsys input)\n");
1226 String dumpStr = nativeDump(mPtr);
Jeff Browne33348b2010-07-15 23:54:05 -07001227 if (dumpStr != null) {
1228 pw.println(dumpStr);
1229 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001230 }
Jeff Brownb4ff35d2011-01-02 16:37:43 -08001231
Jeff Brownac143512012-04-05 18:57:33 -07001232 private boolean checkCallingPermission(String permission, String func) {
1233 // Quick check: if the calling permission is me, it's all okay.
1234 if (Binder.getCallingPid() == Process.myPid()) {
1235 return true;
1236 }
1237
1238 if (mContext.checkCallingPermission(permission) == PackageManager.PERMISSION_GRANTED) {
1239 return true;
1240 }
1241 String msg = "Permission Denial: " + func + " from pid="
1242 + Binder.getCallingPid()
1243 + ", uid=" + Binder.getCallingUid()
1244 + " requires " + permission;
1245 Slog.w(TAG, msg);
1246 return false;
1247 }
1248
Jeff Brown4532e612012-04-05 14:27:12 -07001249 // Called by the heartbeat to ensure locks are not held indefinitely (for deadlock detection).
Craig Mautner2f39e9f2012-09-21 11:39:54 -07001250 @Override
Jeff Brown89ef0722011-08-10 16:25:21 -07001251 public void monitor() {
1252 synchronized (mInputFilterLock) { }
Jeff Brown4532e612012-04-05 14:27:12 -07001253 nativeMonitor(mPtr);
Jeff Brown89ef0722011-08-10 16:25:21 -07001254 }
1255
Jeff Brown4532e612012-04-05 14:27:12 -07001256 // Native callback.
1257 private void notifyConfigurationChanged(long whenNanos) {
Jeff Browna9d131c2012-09-20 16:48:17 -07001258 mWindowManagerCallbacks.notifyConfigurationChanged();
Jeff Brown4532e612012-04-05 14:27:12 -07001259 }
1260
1261 // Native callback.
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001262 private void notifyInputDevicesChanged(InputDevice[] inputDevices) {
1263 synchronized (mInputDevicesLock) {
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001264 if (!mInputDevicesChangedPending) {
1265 mInputDevicesChangedPending = true;
Jeff Browncf39bdf2012-05-18 14:41:19 -07001266 mHandler.obtainMessage(MSG_DELIVER_INPUT_DEVICES_CHANGED,
1267 mInputDevices).sendToTarget();
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001268 }
Jeff Browncf39bdf2012-05-18 14:41:19 -07001269
1270 mInputDevices = inputDevices;
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001271 }
1272 }
1273
1274 // Native callback.
Jeff Brownbcc046a2012-09-27 20:46:43 -07001275 private void notifySwitch(long whenNanos, int switchValues, int switchMask) {
1276 if (DEBUG) {
1277 Slog.d(TAG, "notifySwitch: values=" + Integer.toHexString(switchValues)
1278 + ", mask=" + Integer.toHexString(switchMask));
1279 }
1280
Craig Mautner2f39e9f2012-09-21 11:39:54 -07001281 if ((switchMask & SW_LID_BIT) != 0) {
1282 final boolean lidOpen = ((switchValues & SW_LID_BIT) == 0);
Jeff Brownbcc046a2012-09-27 20:46:43 -07001283 mWindowManagerCallbacks.notifyLidSwitchChanged(whenNanos, lidOpen);
Jeff Brown53384282012-08-20 20:16:01 -07001284 }
Craig Mautner2f39e9f2012-09-21 11:39:54 -07001285
1286 if (mUseDevInputEventForAudioJack && (switchMask & SW_JACK_BITS) != 0) {
1287 mWiredAccessoryCallbacks.notifyWiredAccessoryChanged(whenNanos, switchValues,
1288 switchMask);
1289 }
Jeff Brown4532e612012-04-05 14:27:12 -07001290 }
1291
1292 // Native callback.
1293 private void notifyInputChannelBroken(InputWindowHandle inputWindowHandle) {
Jeff Browna9d131c2012-09-20 16:48:17 -07001294 mWindowManagerCallbacks.notifyInputChannelBroken(inputWindowHandle);
Jeff Brown4532e612012-04-05 14:27:12 -07001295 }
1296
1297 // Native callback.
1298 private long notifyANR(InputApplicationHandle inputApplicationHandle,
Jeff Brownbd181bb2013-09-10 16:44:24 -07001299 InputWindowHandle inputWindowHandle, String reason) {
1300 return mWindowManagerCallbacks.notifyANR(
1301 inputApplicationHandle, inputWindowHandle, reason);
Jeff Brown4532e612012-04-05 14:27:12 -07001302 }
1303
1304 // Native callback.
1305 final boolean filterInputEvent(InputEvent event, int policyFlags) {
1306 synchronized (mInputFilterLock) {
1307 if (mInputFilter != null) {
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07001308 try {
1309 mInputFilter.filterInputEvent(event, policyFlags);
1310 } catch (RemoteException e) {
1311 /* ignore */
1312 }
Jeff Brown4532e612012-04-05 14:27:12 -07001313 return false;
1314 }
1315 }
1316 event.recycle();
1317 return true;
1318 }
1319
1320 // Native callback.
1321 private int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags, boolean isScreenOn) {
Jeff Browna9d131c2012-09-20 16:48:17 -07001322 return mWindowManagerCallbacks.interceptKeyBeforeQueueing(
Jeff Brown4532e612012-04-05 14:27:12 -07001323 event, policyFlags, isScreenOn);
1324 }
1325
1326 // Native callback.
1327 private int interceptMotionBeforeQueueingWhenScreenOff(int policyFlags) {
Jeff Browna9d131c2012-09-20 16:48:17 -07001328 return mWindowManagerCallbacks.interceptMotionBeforeQueueingWhenScreenOff(policyFlags);
Jeff Brown4532e612012-04-05 14:27:12 -07001329 }
1330
1331 // Native callback.
1332 private long interceptKeyBeforeDispatching(InputWindowHandle focus,
1333 KeyEvent event, int policyFlags) {
Jeff Browna9d131c2012-09-20 16:48:17 -07001334 return mWindowManagerCallbacks.interceptKeyBeforeDispatching(focus, event, policyFlags);
Jeff Brown4532e612012-04-05 14:27:12 -07001335 }
1336
1337 // Native callback.
1338 private KeyEvent dispatchUnhandledKey(InputWindowHandle focus,
1339 KeyEvent event, int policyFlags) {
Jeff Browna9d131c2012-09-20 16:48:17 -07001340 return mWindowManagerCallbacks.dispatchUnhandledKey(focus, event, policyFlags);
Jeff Brown4532e612012-04-05 14:27:12 -07001341 }
1342
1343 // Native callback.
1344 private boolean checkInjectEventsPermission(int injectorPid, int injectorUid) {
1345 return mContext.checkPermission(android.Manifest.permission.INJECT_EVENTS,
1346 injectorPid, injectorUid) == PackageManager.PERMISSION_GRANTED;
1347 }
1348
1349 // Native callback.
1350 private int getVirtualKeyQuietTimeMillis() {
1351 return mContext.getResources().getInteger(
1352 com.android.internal.R.integer.config_virtualKeyQuietTimeMillis);
1353 }
1354
1355 // Native callback.
1356 private String[] getExcludedDeviceNames() {
1357 ArrayList<String> names = new ArrayList<String>();
1358
1359 // Read partner-provided list of excluded input devices
1360 XmlPullParser parser = null;
1361 // Environment.getRootDirectory() is a fancy way of saying ANDROID_ROOT or "/system".
1362 File confFile = new File(Environment.getRootDirectory(), EXCLUDED_DEVICES_PATH);
1363 FileReader confreader = null;
1364 try {
1365 confreader = new FileReader(confFile);
1366 parser = Xml.newPullParser();
1367 parser.setInput(confreader);
1368 XmlUtils.beginDocument(parser, "devices");
1369
1370 while (true) {
1371 XmlUtils.nextElement(parser);
1372 if (!"device".equals(parser.getName())) {
1373 break;
1374 }
1375 String name = parser.getAttributeValue(null, "name");
1376 if (name != null) {
1377 names.add(name);
1378 }
1379 }
1380 } catch (FileNotFoundException e) {
1381 // It's ok if the file does not exist.
1382 } catch (Exception e) {
1383 Slog.e(TAG, "Exception while parsing '" + confFile.getAbsolutePath() + "'", e);
1384 } finally {
1385 try { if (confreader != null) confreader.close(); } catch (IOException e) { }
1386 }
1387
1388 return names.toArray(new String[names.size()]);
1389 }
1390
1391 // Native callback.
1392 private int getKeyRepeatTimeout() {
1393 return ViewConfiguration.getKeyRepeatTimeout();
1394 }
1395
1396 // Native callback.
1397 private int getKeyRepeatDelay() {
1398 return ViewConfiguration.getKeyRepeatDelay();
1399 }
1400
1401 // Native callback.
1402 private int getHoverTapTimeout() {
1403 return ViewConfiguration.getHoverTapTimeout();
1404 }
1405
1406 // Native callback.
1407 private int getHoverTapSlop() {
1408 return ViewConfiguration.getHoverTapSlop();
1409 }
1410
1411 // Native callback.
1412 private int getDoubleTapTimeout() {
1413 return ViewConfiguration.getDoubleTapTimeout();
1414 }
1415
1416 // Native callback.
1417 private int getLongPressTimeout() {
1418 return ViewConfiguration.getLongPressTimeout();
1419 }
1420
1421 // Native callback.
1422 private int getPointerLayer() {
Jeff Browna9d131c2012-09-20 16:48:17 -07001423 return mWindowManagerCallbacks.getPointerLayer();
Jeff Brown4532e612012-04-05 14:27:12 -07001424 }
1425
1426 // Native callback.
1427 private PointerIcon getPointerIcon() {
1428 return PointerIcon.getDefaultIcon(mContext);
1429 }
1430
Jeff Brown6ec6f792012-04-17 16:52:41 -07001431 // Native callback.
1432 private String[] getKeyboardLayoutOverlay(String inputDeviceDescriptor) {
1433 if (!mSystemReady) {
1434 return null;
1435 }
1436
Jeff Browncf39bdf2012-05-18 14:41:19 -07001437 String keyboardLayoutDescriptor = getCurrentKeyboardLayoutForInputDevice(
1438 inputDeviceDescriptor);
Jeff Brown6ec6f792012-04-17 16:52:41 -07001439 if (keyboardLayoutDescriptor == null) {
1440 return null;
1441 }
1442
1443 final String[] result = new String[2];
1444 visitKeyboardLayout(keyboardLayoutDescriptor, new KeyboardLayoutVisitor() {
1445 @Override
1446 public void visitKeyboardLayout(Resources resources,
Jeff Brownd9fec5d2012-05-17 16:01:54 -07001447 String descriptor, String label, String collection, int keyboardLayoutResId) {
Jeff Brown6ec6f792012-04-17 16:52:41 -07001448 try {
1449 result[0] = descriptor;
1450 result[1] = Streams.readFully(new InputStreamReader(
Jeff Brown2f095762012-05-10 21:29:33 -07001451 resources.openRawResource(keyboardLayoutResId)));
Jeff Brown6ec6f792012-04-17 16:52:41 -07001452 } catch (IOException ex) {
1453 } catch (NotFoundException ex) {
1454 }
1455 }
1456 });
1457 if (result[0] == null) {
1458 Log.w(TAG, "Could not get keyboard layout with descriptor '"
1459 + keyboardLayoutDescriptor + "'.");
1460 return null;
1461 }
1462 return result;
1463 }
1464
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001465 // Native callback.
1466 private String getDeviceAlias(String uniqueId) {
Matthew Xie96313142012-06-29 16:57:31 -07001467 if (BluetoothAdapter.checkBluetoothAddress(uniqueId)) {
1468 // TODO(BT) mBluetoothService.getRemoteAlias(uniqueId)
1469 return null;
Jeff Brown5bbd4b42012-04-20 19:28:00 -07001470 }
1471 return null;
1472 }
1473
Jeff Brown4532e612012-04-05 14:27:12 -07001474 /**
1475 * Callback interface implemented by the Window Manager.
1476 */
Jeff Browna9d131c2012-09-20 16:48:17 -07001477 public interface WindowManagerCallbacks {
Jeff Brown4532e612012-04-05 14:27:12 -07001478 public void notifyConfigurationChanged();
1479
1480 public void notifyLidSwitchChanged(long whenNanos, boolean lidOpen);
1481
1482 public void notifyInputChannelBroken(InputWindowHandle inputWindowHandle);
1483
1484 public long notifyANR(InputApplicationHandle inputApplicationHandle,
Jeff Brownbd181bb2013-09-10 16:44:24 -07001485 InputWindowHandle inputWindowHandle, String reason);
Jeff Brown4532e612012-04-05 14:27:12 -07001486
1487 public int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags, boolean isScreenOn);
1488
1489 public int interceptMotionBeforeQueueingWhenScreenOff(int policyFlags);
1490
1491 public long interceptKeyBeforeDispatching(InputWindowHandle focus,
1492 KeyEvent event, int policyFlags);
1493
1494 public KeyEvent dispatchUnhandledKey(InputWindowHandle focus,
1495 KeyEvent event, int policyFlags);
1496
1497 public int getPointerLayer();
1498 }
1499
1500 /**
Craig Mautner2f39e9f2012-09-21 11:39:54 -07001501 * Callback interface implemented by WiredAccessoryObserver.
1502 */
1503 public interface WiredAccessoryCallbacks {
1504 public void notifyWiredAccessoryChanged(long whenNanos, int switchValues, int switchMask);
1505 }
1506
1507 /**
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001508 * Private handler for the input manager.
1509 */
1510 private final class InputManagerHandler extends Handler {
Jeff Browna9d131c2012-09-20 16:48:17 -07001511 public InputManagerHandler(Looper looper) {
1512 super(looper, null, true /*async*/);
Jeff Browna2910d02012-08-25 12:29:46 -07001513 }
1514
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001515 @Override
1516 public void handleMessage(Message msg) {
1517 switch (msg.what) {
1518 case MSG_DELIVER_INPUT_DEVICES_CHANGED:
Jeff Browncf39bdf2012-05-18 14:41:19 -07001519 deliverInputDevicesChanged((InputDevice[])msg.obj);
1520 break;
1521 case MSG_SWITCH_KEYBOARD_LAYOUT:
1522 handleSwitchKeyboardLayout(msg.arg1, msg.arg2);
1523 break;
1524 case MSG_RELOAD_KEYBOARD_LAYOUTS:
1525 reloadKeyboardLayouts();
1526 break;
1527 case MSG_UPDATE_KEYBOARD_LAYOUTS:
1528 updateKeyboardLayouts();
1529 break;
1530 case MSG_RELOAD_DEVICE_ALIASES:
1531 reloadDeviceAliases();
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001532 break;
1533 }
1534 }
1535 }
1536
1537 /**
Jeff Brown4532e612012-04-05 14:27:12 -07001538 * Hosting interface for input filters to call back into the input manager.
1539 */
Svetoslav Ganovc9c9a482012-07-16 08:46:07 -07001540 private final class InputFilterHost extends IInputFilterHost.Stub {
Jeff Brown0029c662011-03-30 02:25:18 -07001541 private boolean mDisconnected;
1542
1543 public void disconnectLocked() {
1544 mDisconnected = true;
1545 }
1546
Craig Mautner2f39e9f2012-09-21 11:39:54 -07001547 @Override
Jeff Brown0029c662011-03-30 02:25:18 -07001548 public void sendInputEvent(InputEvent event, int policyFlags) {
1549 if (event == null) {
1550 throw new IllegalArgumentException("event must not be null");
1551 }
1552
1553 synchronized (mInputFilterLock) {
1554 if (!mDisconnected) {
Jeff Brownac143512012-04-05 18:57:33 -07001555 nativeInjectInputEvent(mPtr, event, 0, 0,
1556 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC, 0,
Jeff Brown0029c662011-03-30 02:25:18 -07001557 policyFlags | WindowManagerPolicy.FLAG_FILTERED);
1558 }
1559 }
1560 }
1561 }
Jeff Brown9f25b7f2012-04-10 14:30:49 -07001562
1563 private static final class KeyboardLayoutDescriptor {
1564 public String packageName;
1565 public String receiverName;
1566 public String keyboardLayoutName;
1567
1568 public static String format(String packageName,
1569 String receiverName, String keyboardName) {
1570 return packageName + "/" + receiverName + "/" + keyboardName;
1571 }
1572
1573 public static KeyboardLayoutDescriptor parse(String descriptor) {
1574 int pos = descriptor.indexOf('/');
1575 if (pos < 0 || pos + 1 == descriptor.length()) {
1576 return null;
1577 }
1578 int pos2 = descriptor.indexOf('/', pos + 1);
1579 if (pos2 < pos + 2 || pos2 + 1 == descriptor.length()) {
1580 return null;
1581 }
1582
1583 KeyboardLayoutDescriptor result = new KeyboardLayoutDescriptor();
1584 result.packageName = descriptor.substring(0, pos);
1585 result.receiverName = descriptor.substring(pos + 1, pos2);
1586 result.keyboardLayoutName = descriptor.substring(pos2 + 1);
1587 return result;
1588 }
1589 }
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001590
Jeff Brown6ec6f792012-04-17 16:52:41 -07001591 private interface KeyboardLayoutVisitor {
1592 void visitKeyboardLayout(Resources resources,
Jeff Brownd9fec5d2012-05-17 16:01:54 -07001593 String descriptor, String label, String collection, int keyboardLayoutResId);
Jeff Brown6ec6f792012-04-17 16:52:41 -07001594 }
1595
Jeff Brownaf9e8d32012-04-12 17:32:48 -07001596 private final class InputDevicesChangedListenerRecord implements DeathRecipient {
1597 private final int mPid;
1598 private final IInputDevicesChangedListener mListener;
1599
1600 public InputDevicesChangedListenerRecord(int pid, IInputDevicesChangedListener listener) {
1601 mPid = pid;
1602 mListener = listener;
1603 }
1604
1605 @Override
1606 public void binderDied() {
1607 if (DEBUG) {
1608 Slog.d(TAG, "Input devices changed listener for pid " + mPid + " died.");
1609 }
1610 onInputDevicesChangedListenerDied(mPid);
1611 }
1612
1613 public void notifyInputDevicesChanged(int[] info) {
1614 try {
1615 mListener.onInputDevicesChanged(info);
1616 } catch (RemoteException ex) {
1617 Slog.w(TAG, "Failed to notify process "
1618 + mPid + " that input devices changed, assuming it died.", ex);
1619 binderDied();
1620 }
1621 }
1622 }
Jeff Browna47425a2012-04-13 04:09:27 -07001623
1624 private final class VibratorToken implements DeathRecipient {
1625 public final int mDeviceId;
1626 public final IBinder mToken;
1627 public final int mTokenValue;
1628
1629 public boolean mVibrating;
1630
1631 public VibratorToken(int deviceId, IBinder token, int tokenValue) {
1632 mDeviceId = deviceId;
1633 mToken = token;
1634 mTokenValue = tokenValue;
1635 }
1636
1637 @Override
1638 public void binderDied() {
1639 if (DEBUG) {
1640 Slog.d(TAG, "Vibrator token died.");
1641 }
1642 onVibratorTokenDied(this);
1643 }
1644 }
Jeff Brown4ccb8232014-01-16 22:16:42 -08001645
1646 private final class LocalService extends InputManagerInternal {
1647 @Override
1648 public void setDisplayViewports(
1649 DisplayViewport defaultViewport, DisplayViewport externalTouchViewport) {
1650 setDisplayViewportsInternal(defaultViewport, externalTouchViewport);
1651 }
1652 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001653}