8

I am trying to add touch events from a file to the current application (build a new touch event according to the data found in the file ), and I am trying to understand the chain of calls when a "real" touch event is triggered.

From all the searches I conducted, I found that Activity.dispatchTouchEvent(ev) is the first method called when we have a touch event, then its forwarded to ViewGroup.dispatchTouchEvent and then to View.dispatchTouchEvent.

I want to find what is being called before Activity.dispatchTouchEvent(ev) and how the events is transferred from the HW to this method.

Zain
  • 37,492
  • 7
  • 60
  • 84
Foad Rezek
  • 694
  • 2
  • 10
  • 22
  • Why do you have to simulate the actual touch event? Can you just call the code that would be called by the touch event instead? – GLee Dec 16 '13 at 18:30
  • I want to control one device by another device,I build a connection which transfers the events from the controlling device via WIFI-p2p to the controlled device, saves them in a file in a specific format, and then on the controlled device I need to read the events from the file, build event objects and inject them somehow to the currently running application without changing the application and without assuming I know the code of this app. So that is why I cant call the code of the app, I want to catch the method which calls to Activity.dispatchTouchEvent and try to mess with the code there – Foad Rezek Dec 16 '13 at 18:35
  • use Thread.dumpStackTrace() – pskink Dec 16 '13 at 18:48
  • @Foad - Were you ever able to figure it out? – TorukMakto Jan 25 '14 at 16:07
  • @myCodeHurts no I didn't – Foad Rezek Jan 27 '14 at 08:38

2 Answers2

3

Maybe what you want is how android input framework works. The following blog about android input framework architecture may helps you.

Android Input Framework Architecture[Part 1] : Introduction to InputReader & InputDispatcher

The input process can simply be put as:

input hardware ------> kernel/driver(input protocol) -----> EventHub(framework/base/libs/ui) getevent------> InputReader ----> inputDispatcher ----> Window manager.

  1. First, InputReader convert the meta input event to android event (etc. MotionEvent/KeyEvent).
  2. Then, InputDispatcher dispatcher the event to WindowManager
  3. WindowManager calls Window.Callback.dispatchTouchEvent(***).
chaman
  • 31
  • 5
2

In Activty the method dispatchTouchEvent is :

 public boolean dispatchTouchEvent(MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
        onUserInteraction();
    }
    if (getWindow().superDispatchTouchEvent(ev)) {
        return true;
    }
    return onTouchEvent(ev);
}

you must call super.dispatchTouchEvent() method in ur own activity.

so that u can reach this line :getWindow().superDispatchTouchEvent(ev)

getWindwow() return the instance of PhoneWindow.

PhoneWindow.superDispatchTouchEvent() will call the DecorView's method ,and in that method ,DecorView.dispatchTouchEvent will be called.

So the event has been passed to the views.

    //in Class PhoneWindow
 public boolean superDispatchTouchEvent(MotionEvent event) {
      return mDecor.superDispatchTouchEvent(event);
 }

and

//in class DecorView
public boolean superDispatchTouchEvent(MotionEvent event) {
      return super.dispatchTouchEvent(event);
}
Ivan Sheihets
  • 802
  • 8
  • 17
Cheng G
  • 21
  • 2