blob: fe306b31939552e9b32c74b3c539893147edc21c [file] [log] [blame]
Jeff Brown46b9ac0a2010-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
17package com.android.server;
18
19import com.android.internal.util.XmlUtils;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070020
21import org.xmlpull.v1.XmlPullParser;
22
23import android.content.Context;
Jeff Brown349703e2010-06-22 01:27:15 -070024import android.content.pm.PackageManager;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070025import android.content.res.Configuration;
26import android.os.Environment;
Jeff Brownae9fc032010-08-18 15:51:08 -070027import android.os.SystemProperties;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070028import android.util.Slog;
29import android.util.Xml;
30import android.view.InputChannel;
Jeff Brown8d608662010-08-30 03:02:23 -070031import android.view.InputDevice;
Jeff Brown6ec402b2010-07-28 15:48:59 -070032import android.view.InputEvent;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070033import android.view.Surface;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070034
35import java.io.BufferedReader;
36import java.io.File;
37import java.io.FileInputStream;
38import java.io.FileNotFoundException;
39import java.io.FileReader;
40import java.io.IOException;
41import java.io.InputStreamReader;
42import java.io.PrintWriter;
43import java.util.ArrayList;
Jeff Brown8d608662010-08-30 03:02:23 -070044import java.util.Properties;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070045
46/*
47 * Wraps the C++ InputManager and provides its callbacks.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070048 */
49public class InputManager {
50 static final String TAG = "InputManager";
51
52 private final Callbacks mCallbacks;
53 private final Context mContext;
54 private final WindowManagerService mWindowManagerService;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070055
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070056 private static native void nativeInit(Callbacks callbacks);
57 private static native void nativeStart();
58 private static native void nativeSetDisplaySize(int displayId, int width, int height);
59 private static native void nativeSetDisplayOrientation(int displayId, int rotation);
60
Jeff Brown6d0fec22010-07-23 21:28:06 -070061 private static native int nativeGetScanCodeState(int deviceId, int sourceMask,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070062 int scanCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -070063 private static native int nativeGetKeyCodeState(int deviceId, int sourceMask,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070064 int keyCode);
Jeff Brown6d0fec22010-07-23 21:28:06 -070065 private static native int nativeGetSwitchState(int deviceId, int sourceMask,
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070066 int sw);
Jeff Brown6d0fec22010-07-23 21:28:06 -070067 private static native boolean nativeHasKeys(int deviceId, int sourceMask,
68 int[] keyCodes, boolean[] keyExists);
Jeff Browna41ca772010-08-11 14:46:32 -070069 private static native void nativeRegisterInputChannel(InputChannel inputChannel,
70 boolean monitor);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070071 private static native void nativeUnregisterInputChannel(InputChannel inputChannel);
Jeff Brown6ec402b2010-07-28 15:48:59 -070072 private static native int nativeInjectInputEvent(InputEvent event,
73 int injectorPid, int injectorUid, int syncMode, int timeoutMillis);
Jeff Brown349703e2010-06-22 01:27:15 -070074 private static native void nativeSetInputWindows(InputWindow[] windows);
75 private static native void nativeSetInputDispatchMode(boolean enabled, boolean frozen);
76 private static native void nativeSetFocusedApplication(InputApplication application);
Jeff Brown8d608662010-08-30 03:02:23 -070077 private static native InputDevice nativeGetInputDevice(int deviceId);
Jeff Brown57c59372010-09-21 18:22:55 -070078 private static native void nativeGetInputConfiguration(Configuration configuration);
Jeff Brown8d608662010-08-30 03:02:23 -070079 private static native int[] nativeGetInputDeviceIds();
Jeff Browne6504122010-09-27 14:52:15 -070080 private static native boolean nativeTransferTouchFocus(InputChannel fromChannel,
81 InputChannel toChannel);
Jeff Browne33348b2010-07-15 23:54:05 -070082 private static native String nativeDump();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -070083
Jeff Brown7fbdc842010-06-17 20:52:56 -070084 // Input event injection constants defined in InputDispatcher.h.
85 static final int INPUT_EVENT_INJECTION_SUCCEEDED = 0;
86 static final int INPUT_EVENT_INJECTION_PERMISSION_DENIED = 1;
87 static final int INPUT_EVENT_INJECTION_FAILED = 2;
88 static final int INPUT_EVENT_INJECTION_TIMED_OUT = 3;
89
Jeff Brown6ec402b2010-07-28 15:48:59 -070090 // Input event injection synchronization modes defined in InputDispatcher.h
91 static final int INPUT_EVENT_INJECTION_SYNC_NONE = 0;
92 static final int INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT = 1;
93 static final int INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_FINISH = 2;
94
Jeff Brown6d0fec22010-07-23 21:28:06 -070095 // Key states (may be returned by queries about the current state of a
96 // particular key code, scan code or switch).
97
98 /** The key state is unknown or the requested key itself is not supported. */
99 public static final int KEY_STATE_UNKNOWN = -1;
100
101 /** The key is up. /*/
102 public static final int KEY_STATE_UP = 0;
103
104 /** The key is down. */
105 public static final int KEY_STATE_DOWN = 1;
106
107 /** The key is down but is a virtual key press that is being emulated by the system. */
108 public static final int KEY_STATE_VIRTUAL = 2;
109
Jeff Browne33348b2010-07-15 23:54:05 -0700110 public InputManager(Context context, WindowManagerService windowManagerService) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700111 this.mContext = context;
112 this.mWindowManagerService = windowManagerService;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700113
114 this.mCallbacks = new Callbacks();
115
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700116 init();
117 }
118
119 private void init() {
120 Slog.i(TAG, "Initializing input manager");
121 nativeInit(mCallbacks);
122 }
123
124 public void start() {
125 Slog.i(TAG, "Starting input manager");
126 nativeStart();
127 }
128
129 public void setDisplaySize(int displayId, int width, int height) {
130 if (width <= 0 || height <= 0) {
131 throw new IllegalArgumentException("Invalid display id or dimensions.");
132 }
133
134 Slog.i(TAG, "Setting display #" + displayId + " size to " + width + "x" + height);
135 nativeSetDisplaySize(displayId, width, height);
136 }
137
138 public void setDisplayOrientation(int displayId, int rotation) {
139 if (rotation < Surface.ROTATION_0 || rotation > Surface.ROTATION_270) {
140 throw new IllegalArgumentException("Invalid rotation.");
141 }
142
143 Slog.i(TAG, "Setting display #" + displayId + " orientation to " + rotation);
144 nativeSetDisplayOrientation(displayId, rotation);
145 }
146
147 public void getInputConfiguration(Configuration config) {
148 if (config == null) {
149 throw new IllegalArgumentException("config must not be null.");
150 }
151
Jeff Brown57c59372010-09-21 18:22:55 -0700152 nativeGetInputConfiguration(config);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700153 }
154
Jeff Brown6d0fec22010-07-23 21:28:06 -0700155 /**
156 * Gets the current state of a key or button by key code.
157 * @param deviceId The input device id, or -1 to consult all devices.
158 * @param sourceMask The input sources to consult, or {@link InputDevice#SOURCE_ANY} to
159 * consider all input sources. An input device is consulted if at least one of its
160 * non-class input source bits matches the specified source mask.
161 * @param keyCode The key code to check.
162 * @return The key state.
163 */
164 public int getKeyCodeState(int deviceId, int sourceMask, int keyCode) {
165 return nativeGetKeyCodeState(deviceId, sourceMask, keyCode);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700166 }
167
Jeff Brown6d0fec22010-07-23 21:28:06 -0700168 /**
169 * Gets the current state of a key or button by scan code.
170 * @param deviceId The input device id, or -1 to consult all devices.
171 * @param sourceMask The input sources to consult, or {@link InputDevice#SOURCE_ANY} to
172 * consider all input sources. An input device is consulted if at least one of its
173 * non-class input source bits matches the specified source mask.
174 * @param scanCode The scan code to check.
175 * @return The key state.
176 */
177 public int getScanCodeState(int deviceId, int sourceMask, int scanCode) {
178 return nativeGetScanCodeState(deviceId, sourceMask, scanCode);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700179 }
180
Jeff Brown6d0fec22010-07-23 21:28:06 -0700181 /**
182 * Gets the current state of a switch by switch code.
183 * @param deviceId The input device id, or -1 to consult all devices.
184 * @param sourceMask The input sources to consult, or {@link InputDevice#SOURCE_ANY} to
185 * consider all input sources. An input device is consulted if at least one of its
186 * non-class input source bits matches the specified source mask.
187 * @param switchCode The switch code to check.
188 * @return The switch state.
189 */
190 public int getSwitchState(int deviceId, int sourceMask, int switchCode) {
191 return nativeGetSwitchState(deviceId, sourceMask, switchCode);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700192 }
193
Jeff Brown6d0fec22010-07-23 21:28:06 -0700194 /**
195 * Determines whether the specified key codes are supported by a particular device.
196 * @param deviceId The input device id, or -1 to consult all devices.
197 * @param sourceMask The input sources to consult, or {@link InputDevice#SOURCE_ANY} to
198 * consider all input sources. An input device is consulted if at least one of its
199 * non-class input source bits matches the specified source mask.
200 * @param keyCodes The array of key codes to check.
201 * @param keyExists An array at least as large as keyCodes whose entries will be set
202 * to true or false based on the presence or absence of support for the corresponding
203 * key codes.
204 * @return True if the lookup was successful, false otherwise.
205 */
206 public boolean hasKeys(int deviceId, int sourceMask, int[] keyCodes, boolean[] keyExists) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700207 if (keyCodes == null) {
208 throw new IllegalArgumentException("keyCodes must not be null.");
209 }
Jeff Brown6d0fec22010-07-23 21:28:06 -0700210 if (keyExists == null || keyExists.length < keyCodes.length) {
211 throw new IllegalArgumentException("keyExists must not be null and must be at "
212 + "least as large as keyCodes.");
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700213 }
214
Jeff Brown6d0fec22010-07-23 21:28:06 -0700215 return nativeHasKeys(deviceId, sourceMask, keyCodes, keyExists);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700216 }
217
Jeff Browna41ca772010-08-11 14:46:32 -0700218 /**
219 * Creates an input channel that will receive all input from the input dispatcher.
220 * @param inputChannelName The input channel name.
221 * @return The input channel.
222 */
223 public InputChannel monitorInput(String inputChannelName) {
224 if (inputChannelName == null) {
225 throw new IllegalArgumentException("inputChannelName must not be null.");
226 }
227
228 InputChannel[] inputChannels = InputChannel.openInputChannelPair(inputChannelName);
229 nativeRegisterInputChannel(inputChannels[0], true);
230 inputChannels[0].dispose(); // don't need to retain the Java object reference
231 return inputChannels[1];
232 }
233
234 /**
235 * Registers an input channel so that it can be used as an input event target.
236 * @param inputChannel The input channel to register.
237 */
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700238 public void registerInputChannel(InputChannel inputChannel) {
239 if (inputChannel == null) {
240 throw new IllegalArgumentException("inputChannel must not be null.");
241 }
242
Jeff Browna41ca772010-08-11 14:46:32 -0700243 nativeRegisterInputChannel(inputChannel, false);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700244 }
245
Jeff Browna41ca772010-08-11 14:46:32 -0700246 /**
247 * Unregisters an input channel.
248 * @param inputChannel The input channel to unregister.
249 */
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700250 public void unregisterInputChannel(InputChannel inputChannel) {
251 if (inputChannel == null) {
252 throw new IllegalArgumentException("inputChannel must not be null.");
253 }
254
255 nativeUnregisterInputChannel(inputChannel);
256 }
257
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700258 /**
Jeff Brown6ec402b2010-07-28 15:48:59 -0700259 * Injects an input event into the event system on behalf of an application.
260 * The synchronization mode determines whether the method blocks while waiting for
261 * input injection to proceed.
262 *
263 * {@link #INPUT_EVENT_INJECTION_SYNC_NONE} never blocks. Injection is asynchronous and
264 * is assumed always to be successful.
265 *
266 * {@link #INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_RESULT} waits for previous events to be
267 * dispatched so that the input dispatcher can determine whether input event injection will
268 * be permitted based on the current input focus. Does not wait for the input event to
269 * finish processing.
270 *
271 * {@link #INPUT_EVENT_INJECTION_SYNC_WAIT_FOR_FINISH} waits for the input event to
272 * be completely processed.
273 *
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700274 * @param event The event to inject.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700275 * @param injectorPid The pid of the injecting application.
276 * @param injectorUid The uid of the injecting application.
Jeff Brown6ec402b2010-07-28 15:48:59 -0700277 * @param syncMode The synchronization mode.
Jeff Brown7fbdc842010-06-17 20:52:56 -0700278 * @param timeoutMillis The injection timeout in milliseconds.
279 * @return One of the INPUT_EVENT_INJECTION_XXX constants.
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700280 */
Jeff Brown6ec402b2010-07-28 15:48:59 -0700281 public int injectInputEvent(InputEvent event, int injectorPid, int injectorUid,
282 int syncMode, int timeoutMillis) {
Jeff Brown7fbdc842010-06-17 20:52:56 -0700283 if (event == null) {
284 throw new IllegalArgumentException("event must not be null");
285 }
286 if (injectorPid < 0 || injectorUid < 0) {
287 throw new IllegalArgumentException("injectorPid and injectorUid must not be negative.");
288 }
289 if (timeoutMillis <= 0) {
290 throw new IllegalArgumentException("timeoutMillis must be positive");
291 }
Jeff Brown6ec402b2010-07-28 15:48:59 -0700292
293 return nativeInjectInputEvent(event, injectorPid, injectorUid, syncMode, timeoutMillis);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700294 }
295
Jeff Brown8d608662010-08-30 03:02:23 -0700296 /**
297 * Gets information about the input device with the specified id.
298 * @param id The device id.
299 * @return The input device or null if not found.
300 */
301 public InputDevice getInputDevice(int deviceId) {
302 return nativeGetInputDevice(deviceId);
303 }
304
305 /**
306 * Gets the ids of all input devices in the system.
307 * @return The input device ids.
308 */
309 public int[] getInputDeviceIds() {
310 return nativeGetInputDeviceIds();
311 }
312
Jeff Brown349703e2010-06-22 01:27:15 -0700313 public void setInputWindows(InputWindow[] windows) {
314 nativeSetInputWindows(windows);
315 }
316
317 public void setFocusedApplication(InputApplication application) {
318 nativeSetFocusedApplication(application);
319 }
320
Jeff Brown349703e2010-06-22 01:27:15 -0700321 public void setInputDispatchMode(boolean enabled, boolean frozen) {
322 nativeSetInputDispatchMode(enabled, frozen);
323 }
324
Jeff Browne6504122010-09-27 14:52:15 -0700325 /**
326 * Atomically transfers touch focus from one window to another as identified by
327 * their input channels. It is possible for multiple windows to have
328 * touch focus if they support split touch dispatch
329 * {@link android.view.WindowManager.LayoutParams#FLAG_SPLIT_TOUCH} but this
330 * method only transfers touch focus of the specified window without affecting
331 * other windows that may also have touch focus at the same time.
332 * @param fromChannel The channel of a window that currently has touch focus.
333 * @param toChannel The channel of the window that should receive touch focus in
334 * place of the first.
335 * @return True if the transfer was successful. False if the window with the
336 * specified channel did not actually have touch focus at the time of the request.
337 */
338 public boolean transferTouchFocus(InputChannel fromChannel, InputChannel toChannel) {
339 if (fromChannel == null) {
340 throw new IllegalArgumentException("fromChannel must not be null.");
341 }
342 if (toChannel == null) {
343 throw new IllegalArgumentException("toChannel must not be null.");
344 }
345 return nativeTransferTouchFocus(fromChannel, toChannel);
346 }
347
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700348 public void dump(PrintWriter pw) {
Jeff Browne33348b2010-07-15 23:54:05 -0700349 String dumpStr = nativeDump();
350 if (dumpStr != null) {
351 pw.println(dumpStr);
352 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700353 }
354
355 private static final class VirtualKeyDefinition {
356 public int scanCode;
357
358 // configured position data, specified in display coords
359 public int centerX;
360 public int centerY;
361 public int width;
362 public int height;
363 }
364
Jeff Brown8d608662010-08-30 03:02:23 -0700365 private static final class InputDeviceCalibration {
366 public String[] keys;
367 public String[] values;
368 }
369
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700370 /*
371 * Callbacks from native.
372 */
373 private class Callbacks {
374 static final String TAG = "InputManager-Callbacks";
375
376 private static final boolean DEBUG_VIRTUAL_KEYS = false;
377 private static final String EXCLUDED_DEVICES_PATH = "etc/excluded-input-devices.xml";
Jeff Brown8d608662010-08-30 03:02:23 -0700378 private static final String CALIBRATION_DIR_PATH = "usr/idc/";
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700379
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700380 @SuppressWarnings("unused")
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700381 public void virtualKeyDownFeedback() {
382 mWindowManagerService.mInputMonitor.virtualKeyDownFeedback();
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700383 }
384
385 @SuppressWarnings("unused")
Jeff Brown57c59372010-09-21 18:22:55 -0700386 public void notifyConfigurationChanged(long whenNanos) {
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700387 mWindowManagerService.sendNewConfiguration();
388 }
389
390 @SuppressWarnings("unused")
391 public void notifyLidSwitchChanged(long whenNanos, boolean lidOpen) {
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700392 mWindowManagerService.mInputMonitor.notifyLidSwitchChanged(whenNanos, lidOpen);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700393 }
394
395 @SuppressWarnings("unused")
Jeff Brown7fbdc842010-06-17 20:52:56 -0700396 public void notifyInputChannelBroken(InputChannel inputChannel) {
Jeff Brown349703e2010-06-22 01:27:15 -0700397 mWindowManagerService.mInputMonitor.notifyInputChannelBroken(inputChannel);
Jeff Brown7fbdc842010-06-17 20:52:56 -0700398 }
Jeff Brown7fbdc842010-06-17 20:52:56 -0700399
400 @SuppressWarnings("unused")
Jeff Brown519e0242010-09-15 15:18:56 -0700401 public long notifyANR(Object token, InputChannel inputChannel) {
402 return mWindowManagerService.mInputMonitor.notifyANR(token, inputChannel);
Jeff Brown349703e2010-06-22 01:27:15 -0700403 }
404
405 @SuppressWarnings("unused")
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700406 public int interceptKeyBeforeQueueing(long whenNanos, int keyCode, boolean down,
407 int policyFlags, boolean isScreenOn) {
408 return mWindowManagerService.mInputMonitor.interceptKeyBeforeQueueing(
409 whenNanos, keyCode, down, policyFlags, isScreenOn);
Jeff Brown349703e2010-06-22 01:27:15 -0700410 }
411
412 @SuppressWarnings("unused")
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700413 public boolean interceptKeyBeforeDispatching(InputChannel focus, int action,
414 int flags, int keyCode, int metaState, int repeatCount, int policyFlags) {
Jeff Brown349703e2010-06-22 01:27:15 -0700415 return mWindowManagerService.mInputMonitor.interceptKeyBeforeDispatching(focus,
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700416 action, flags, keyCode, metaState, repeatCount, policyFlags);
Jeff Brown349703e2010-06-22 01:27:15 -0700417 }
418
419 @SuppressWarnings("unused")
420 public boolean checkInjectEventsPermission(int injectorPid, int injectorUid) {
421 return mContext.checkPermission(
422 android.Manifest.permission.INJECT_EVENTS, injectorPid, injectorUid)
423 == PackageManager.PERMISSION_GRANTED;
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700424 }
425
426 @SuppressWarnings("unused")
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700427 public boolean filterTouchEvents() {
428 return mContext.getResources().getBoolean(
429 com.android.internal.R.bool.config_filterTouchEvents);
430 }
431
432 @SuppressWarnings("unused")
433 public boolean filterJumpyTouchEvents() {
434 return mContext.getResources().getBoolean(
435 com.android.internal.R.bool.config_filterJumpyTouchEvents);
436 }
437
438 @SuppressWarnings("unused")
439 public VirtualKeyDefinition[] getVirtualKeyDefinitions(String deviceName) {
440 ArrayList<VirtualKeyDefinition> keys = new ArrayList<VirtualKeyDefinition>();
441
442 try {
443 FileInputStream fis = new FileInputStream(
444 "/sys/board_properties/virtualkeys." + deviceName);
445 InputStreamReader isr = new InputStreamReader(fis);
446 BufferedReader br = new BufferedReader(isr, 2048);
447 String str = br.readLine();
448 if (str != null) {
449 String[] it = str.split(":");
450 if (DEBUG_VIRTUAL_KEYS) Slog.v(TAG, "***** VIRTUAL KEYS: " + it);
451 final int N = it.length-6;
452 for (int i=0; i<=N; i+=6) {
453 if (!"0x01".equals(it[i])) {
Jeff Brown8d608662010-08-30 03:02:23 -0700454 Slog.w(TAG, "Unknown virtual key type at elem #"
455 + i + ": " + it[i] + " for device " + deviceName);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700456 continue;
457 }
458 try {
459 VirtualKeyDefinition key = new VirtualKeyDefinition();
460 key.scanCode = Integer.parseInt(it[i+1]);
461 key.centerX = Integer.parseInt(it[i+2]);
462 key.centerY = Integer.parseInt(it[i+3]);
463 key.width = Integer.parseInt(it[i+4]);
464 key.height = Integer.parseInt(it[i+5]);
465 if (DEBUG_VIRTUAL_KEYS) Slog.v(TAG, "Virtual key "
466 + key.scanCode + ": center=" + key.centerX + ","
467 + key.centerY + " size=" + key.width + "x"
468 + key.height);
469 keys.add(key);
470 } catch (NumberFormatException e) {
Jeff Brown8d608662010-08-30 03:02:23 -0700471 Slog.w(TAG, "Bad number in virtual key definition at region "
472 + i + " in: " + str + " for device " + deviceName, e);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700473 }
474 }
475 }
476 br.close();
477 } catch (FileNotFoundException e) {
Jeff Brown8d608662010-08-30 03:02:23 -0700478 Slog.i(TAG, "No virtual keys found for device " + deviceName + ".");
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700479 } catch (IOException e) {
Jeff Brown8d608662010-08-30 03:02:23 -0700480 Slog.w(TAG, "Error reading virtual keys for device " + deviceName + ".", e);
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700481 }
482
483 return keys.toArray(new VirtualKeyDefinition[keys.size()]);
484 }
485
486 @SuppressWarnings("unused")
Jeff Brown8d608662010-08-30 03:02:23 -0700487 public InputDeviceCalibration getInputDeviceCalibration(String deviceName) {
488 // Calibration is specified as a sequence of colon-delimited key value pairs.
489 Properties properties = new Properties();
490 File calibrationFile = new File(Environment.getRootDirectory(),
491 CALIBRATION_DIR_PATH + deviceName + ".idc");
492 if (calibrationFile.exists()) {
493 try {
494 properties.load(new FileInputStream(calibrationFile));
495 } catch (IOException ex) {
496 Slog.w(TAG, "Error reading input device calibration properties for device "
497 + deviceName + " from " + calibrationFile + ".", ex);
498 }
499 } else {
500 Slog.i(TAG, "No input device calibration properties found for device "
501 + deviceName + ".");
502 return null;
503 }
504
505 InputDeviceCalibration calibration = new InputDeviceCalibration();
506 calibration.keys = properties.keySet().toArray(new String[properties.size()]);
507 calibration.values = properties.values().toArray(new String[properties.size()]);
508 return calibration;
509 }
510
511 @SuppressWarnings("unused")
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700512 public String[] getExcludedDeviceNames() {
513 ArrayList<String> names = new ArrayList<String>();
514
515 // Read partner-provided list of excluded input devices
516 XmlPullParser parser = null;
517 // Environment.getRootDirectory() is a fancy way of saying ANDROID_ROOT or "/system".
518 File confFile = new File(Environment.getRootDirectory(), EXCLUDED_DEVICES_PATH);
519 FileReader confreader = null;
520 try {
521 confreader = new FileReader(confFile);
522 parser = Xml.newPullParser();
523 parser.setInput(confreader);
524 XmlUtils.beginDocument(parser, "devices");
525
526 while (true) {
527 XmlUtils.nextElement(parser);
528 if (!"device".equals(parser.getName())) {
529 break;
530 }
531 String name = parser.getAttributeValue(null, "name");
532 if (name != null) {
533 names.add(name);
534 }
535 }
536 } catch (FileNotFoundException e) {
537 // It's ok if the file does not exist.
538 } catch (Exception e) {
539 Slog.e(TAG, "Exception while parsing '" + confFile.getAbsolutePath() + "'", e);
540 } finally {
541 try { if (confreader != null) confreader.close(); } catch (IOException e) { }
542 }
543
544 return names.toArray(new String[names.size()]);
545 }
Jeff Brownae9fc032010-08-18 15:51:08 -0700546
547 @SuppressWarnings("unused")
548 public int getMaxEventsPerSecond() {
549 int result = 0;
550 try {
551 result = Integer.parseInt(SystemProperties.get("windowsmgr.max_events_per_sec"));
552 } catch (NumberFormatException e) {
553 }
554 if (result < 1) {
Jeff Brown3d8c9bd2010-08-18 17:48:53 -0700555 result = 60;
Jeff Brownae9fc032010-08-18 15:51:08 -0700556 }
557 return result;
558 }
Jeff Brown46b9ac0a2010-04-22 18:58:52 -0700559 }
560}