Skip to content

Commit ac14351

Browse files
author
Jeff Brown
committed
Move some APIs from window manager to input manager.
Simplified input injection API down to just one call. Removed all input state reading API. It was only used by the window manager policy and required a permission that applications could not obtain. READ_INPUT_STATE is now unused and deprecated. Change-Id: I41278141586ddee9468cae0fb59ff0dced6cbc00
1 parent 030711c commit ac14351

File tree

18 files changed

+435
-703
lines changed

18 files changed

+435
-703
lines changed

api/current.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ package android {
8080
field public static final java.lang.String READ_EXTERNAL_STORAGE = "android.permission.READ_EXTERNAL_STORAGE";
8181
field public static final java.lang.String READ_FRAME_BUFFER = "android.permission.READ_FRAME_BUFFER";
8282
field public static final java.lang.String READ_HISTORY_BOOKMARKS = "com.android.browser.permission.READ_HISTORY_BOOKMARKS";
83-
field public static final java.lang.String READ_INPUT_STATE = "android.permission.READ_INPUT_STATE";
83+
field public static final deprecated java.lang.String READ_INPUT_STATE = "android.permission.READ_INPUT_STATE";
8484
field public static final java.lang.String READ_LOGS = "android.permission.READ_LOGS";
8585
field public static final java.lang.String READ_PHONE_STATE = "android.permission.READ_PHONE_STATE";
8686
field public static final java.lang.String READ_PROFILE = "android.permission.READ_PROFILE";

cmds/input/src/com/android/commands/input/Input.java

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616

1717
package com.android.commands.input;
1818

19+
import android.hardware.input.InputManager;
1920
import android.os.RemoteException;
2021
import android.os.ServiceManager;
2122
import android.os.SystemClock;
2223
import android.util.Log;
23-
import android.view.IWindowManager;
24+
import android.view.InputDevice;
2425
import android.view.KeyCharacterMap;
2526
import android.view.KeyEvent;
2627
import android.view.MotionEvent;
@@ -33,8 +34,6 @@
3334
public class Input {
3435
private static final String TAG = "Input";
3536

36-
private IWindowManager mWindowManager;
37-
3837
/**
3938
* Command-line entry point.
4039
*
@@ -44,13 +43,6 @@ public static void main(String[] args) {
4443
(new Input()).run(args);
4544
}
4645

47-
private IWindowManager getWindowManager() {
48-
if (mWindowManager == null) {
49-
mWindowManager = (IWindowManager.Stub.asInterface(ServiceManager.getService("window")));
50-
}
51-
return mWindowManager;
52-
}
53-
5446
private void run(String[] args) {
5547
if (args.length < 1) {
5648
showUsage();
@@ -127,8 +119,10 @@ private void sendText(String text) {
127119

128120
private void sendKeyEvent(int keyCode) {
129121
long now = SystemClock.uptimeMillis();
130-
injectKeyEvent(new KeyEvent(now, now, KeyEvent.ACTION_DOWN, keyCode, 0));
131-
injectKeyEvent(new KeyEvent(now, now, KeyEvent.ACTION_UP, keyCode, 0));
122+
injectKeyEvent(new KeyEvent(now, now, KeyEvent.ACTION_DOWN, keyCode, 0, 0,
123+
KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0, InputDevice.SOURCE_KEYBOARD));
124+
injectKeyEvent(new KeyEvent(now, now, KeyEvent.ACTION_UP, keyCode, 0, 0,
125+
KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0, InputDevice.SOURCE_KEYBOARD));
132126
}
133127

134128
private void sendTap(float x, float y) {
@@ -150,31 +144,22 @@ private void sendSwipe(float x1, float y1, float x2, float y2) {
150144
}
151145

152146
private void injectKeyEvent(KeyEvent event) {
153-
try {
154-
Log.i(TAG, "InjectKeyEvent: " + event);
155-
getWindowManager().injectKeyEvent(event, true);
156-
} catch (RemoteException ex) {
157-
Log.i(TAG, "RemoteException", ex);
158-
}
147+
Log.i(TAG, "InjectKeyEvent: " + event);
148+
InputManager.injectInputEvent(event, InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);
159149
}
160150

161151
private void injectPointerEvent(MotionEvent event) {
162-
try {
163-
Log.i("Input", "InjectPointerEvent: " + event);
164-
getWindowManager().injectPointerEvent(event, true);
165-
} catch (RemoteException ex) {
166-
Log.i(TAG, "RemoteException", ex);
167-
} finally {
168-
event.recycle();
169-
}
152+
event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
153+
Log.i("Input", "InjectPointerEvent: " + event);
154+
InputManager.injectInputEvent(event, InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);
170155
}
171156

172157
private static final float lerp(float a, float b, float alpha) {
173158
return (b - a) * alpha + a;
174159
}
175160

176161
private void showUsage() {
177-
System.err.println("usage: input [text|keyevent]");
162+
System.err.println("usage: input ...");
178163
System.err.println(" input text <string>");
179164
System.err.println(" input keyevent <key code>");
180165
System.err.println(" input tap <x> <y>");

core/java/android/app/ContextImpl.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import android.hardware.ISerialManager;
4646
import android.hardware.SensorManager;
4747
import android.hardware.SerialManager;
48+
import android.hardware.input.IInputManager;
4849
import android.hardware.input.InputManager;
4950
import android.hardware.usb.IUsbManager;
5051
import android.hardware.usb.UsbManager;
@@ -325,9 +326,9 @@ public Object createStaticService() {
325326
}});
326327

327328
registerService(INPUT_SERVICE, new ServiceFetcher() {
328-
public Object createService(ContextImpl ctx) {
329-
return new InputManager(ctx);
330-
}});
329+
public Object createService(ContextImpl ctx) {
330+
return new InputManager(ctx);
331+
}});
331332

332333
registerService(INPUT_METHOD_SERVICE, new ServiceFetcher() {
333334
public Object createService(ContextImpl ctx) {

core/java/android/app/Instrumentation.java

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import android.content.IntentFilter;
2424
import android.content.pm.ActivityInfo;
2525
import android.content.res.Configuration;
26+
import android.hardware.input.InputManager;
2627
import android.os.Bundle;
2728
import android.os.Debug;
2829
import android.os.IBinder;
@@ -35,6 +36,7 @@
3536
import android.util.AndroidRuntimeException;
3637
import android.util.Log;
3738
import android.view.IWindowManager;
39+
import android.view.InputDevice;
3840
import android.view.KeyCharacterMap;
3941
import android.view.KeyEvent;
4042
import android.view.MotionEvent;
@@ -859,11 +861,30 @@ public void sendStringSync(String text) {
859861
*/
860862
public void sendKeySync(KeyEvent event) {
861863
validateNotAppThread();
862-
try {
863-
(IWindowManager.Stub.asInterface(ServiceManager.getService("window")))
864-
.injectKeyEvent(event, true);
865-
} catch (RemoteException e) {
864+
865+
long downTime = event.getDownTime();
866+
long eventTime = event.getEventTime();
867+
int action = event.getAction();
868+
int code = event.getKeyCode();
869+
int repeatCount = event.getRepeatCount();
870+
int metaState = event.getMetaState();
871+
int deviceId = event.getDeviceId();
872+
int scancode = event.getScanCode();
873+
int source = event.getSource();
874+
int flags = event.getFlags();
875+
if (source == InputDevice.SOURCE_UNKNOWN) {
876+
source = InputDevice.SOURCE_KEYBOARD;
877+
}
878+
if (eventTime == 0) {
879+
eventTime = SystemClock.uptimeMillis();
880+
}
881+
if (downTime == 0) {
882+
downTime = eventTime;
866883
}
884+
KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
885+
deviceId, scancode, flags | KeyEvent.FLAG_FROM_SYSTEM, source);
886+
InputManager.injectInputEvent(newEvent,
887+
InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);
867888
}
868889

869890
/**
@@ -902,11 +923,10 @@ public void sendCharacterSync(int keyCode) {
902923
*/
903924
public void sendPointerSync(MotionEvent event) {
904925
validateNotAppThread();
905-
try {
906-
(IWindowManager.Stub.asInterface(ServiceManager.getService("window")))
907-
.injectPointerEvent(event, true);
908-
} catch (RemoteException e) {
926+
if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) == 0) {
927+
event.setSource(InputDevice.SOURCE_TOUCHSCREEN);
909928
}
929+
InputManager.injectInputEvent(event, InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);
910930
}
911931

912932
/**
@@ -922,11 +942,10 @@ public void sendPointerSync(MotionEvent event) {
922942
*/
923943
public void sendTrackballEventSync(MotionEvent event) {
924944
validateNotAppThread();
925-
try {
926-
(IWindowManager.Stub.asInterface(ServiceManager.getService("window")))
927-
.injectTrackballEvent(event, true);
928-
} catch (RemoteException e) {
945+
if ((event.getSource() & InputDevice.SOURCE_CLASS_TRACKBALL) == 0) {
946+
event.setSource(InputDevice.SOURCE_TRACKBALL);
929947
}
948+
InputManager.injectInputEvent(event, InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);
930949
}
931950

932951
/**

core/java/android/hardware/input/IInputManager.aidl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@
1616

1717
package android.hardware.input;
1818

19+
import android.view.InputDevice;
20+
import android.view.InputEvent;
21+
1922
/** @hide */
2023
interface IInputManager {
24+
// Gets input device information.
25+
InputDevice getInputDevice(int deviceId);
26+
int[] getInputDeviceIds();
27+
28+
// Reports whether the hardware supports the given keys; returns true if successful
29+
boolean hasKeys(int deviceId, int sourceMask, in int[] keyCodes, out boolean[] keyExists);
30+
31+
// Temporarily changes the pointer speed.
32+
void tryPointerSpeed(int speed);
33+
34+
// Injects an input event into the system. To inject into windows owned by other
35+
// applications, the caller must have the INJECT_EVENTS permission.
36+
boolean injectInputEvent(in InputEvent ev, int mode);
2137
}

0 commit comments

Comments
 (0)