5

I look for a way how to inject input events such as touch or keyboard events into the own application, from code within that particular app. NOT into an Android OS or to other apps b/c that needs a signature-level permission android.permission.INJECT_EVENTS or rooted device and I have to avoid both. I look for a code that runs on a non-rooted device without anyhow elevated rights.

I'm somehow making a bit of progress with code based on the following concept:

View v = findViewById(android.R.id.content).getRootView();
v.dispatchTouchEvent(e);
2
  • Why do you need to do this? If its your own app, isn't it more convenient and maintainable to call functions directly, rather than emulating touch events? Unless you're writing a test framework, in which case I'd look at how the existing ones do it. Commented Nov 8, 2016 at 14:54
  • @GabeSechan - it is a part of the library that is linked with an arbitrary app, so that I don't have that possibility. The test framework is a good approximation but it should be a part of the production-grade app. Commented Nov 8, 2016 at 16:17

3 Answers 3

1
+50

I think getRootView and dispatch it should works well, if there is any problem with it. Please just brief it.

Or there is an more high level but tricky way to do this. in View.java, there is a hide api called getViewRootImpl() which will return a object of android.view.ViewRootImpl of this Activity. And there is a method called enqueueInputEvent(InputEvent event) on it. So that you can use reflection to get this method.

As I have just finished a project which using Keyboard to send touch commands to the screen, I think when you are trying to inject these kind of events to the activity, multiple touch might be a problem. When you try to send another finger to the screen. The event action must be ACTION_POINTER_DOWN with the index shift of the array named properties. And you should also handle the properties and coordses arrays which including the info of all the fingers each time, even if you just need to move only one finger at this time. The finger's id which is set to properties[i].id is used to identify your finger. But the index of the array is not solid.

        for (int i = 0; i < size; i++) {
            properties[i].id = fingerId;
            properties[i].toolType = MotionEvent.TOOL_TYPE_FINGER;

            coordses[i].x = currentX;
            coordses[i].y = currentY;
            coordses[i].pressure = 1;
            coordses[i].size = DEFAULT_SIZE;
        }
        event = MotionEvent.obtain(
                    mDownTime, SystemClock.uptimeMillis(),
                    ((size - 1) << MotionEvent.ACTION_POINTER_INDEX_SHIFT)
                            + MotionEvent.ACTION_POINTER_DOWN,
                    size,
                    properties, coordses, DEFAULT_META_STATE, DEFAULT_BUTTON_STATE,
                    DEFAULT_PRECISION_X, DEFAULT_PRECISION_Y, DEFAULT_DEVICE_ID,
                    DEFAULT_EDGE_FLAGS, InputDevice.SOURCE_TOUCHSCREEN, DEFAULT_FLAGS);
Sign up to request clarification or add additional context in comments.

1 Comment

I was trying this approach already. The issue I observed that user was able to open the menu but not to close.
0

I managed to solve that in a way that it handles not only a Activity root window but also to a windows that are created for various overlays such as menus and dialogs.
The code is here: https://github.com/TeskaLabs/Remote-Access-Android/blob/master/tlra/src/main/java/com/teskalabs/tlra/android/inapp/InAppInputManager.java

1 Comment

Do you happen to know where I can find the code example? The link returns a 404.
0

In our own application you can just use the input cli command:

  public static final void tapButton (int x, int y) throws IOException, InterruptedException {
         Runtime.getRuntime().exec("input tap " + x + " " + y);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.