1

I have a Service that can draw a Canvas on top of all applications using a SYSTEM_ALERT_WINDOW which contains a custom View.

My Service and custom View both implement View.OnTouchListener

The onTouch(View v, MotionEvent ev){} method of the Service returns true while the custom View returns false. This allows me to display my custom View on top of all apps and still interact with the underlying Activity. This part works.

I also want the Service to simulate a touch event in the specified coordinates in the current Activity that is underneath the SYSTEM_ALERT_WINDOW.

I was hoping I could call View.dispatchTouchEvent(...) on the custom View and because onTouch(...) returns false, the touch event would get passed on to the underlying Activity. It does not work.

How can I simulate touch events in any Activity in this situation?

I have used code from the following sources...

Draw a canvas on top of all applications:

http://www.piwai.info/chatheads-basics/

Draw an Android canvas on top of all applications?

Passing touches to underlying app:

How can I interact with elements behind a translucent Android app?

Simulating touches:

How to simulate a touch event in Android?

Community
  • 1
  • 1
  • AFAIK, you can't, for obvious security reasons, except probably on rooted devices. Even the accessibility APIs don't support this AFAICT. Allowing arbitrary apps to fake input to other apps would be a major security hole. – CommonsWare May 12 '15 at 11:45
  • Is it at least possible to simulate touch input in apps that are part of the Android OS if the views are accessed specifically? Can I do something like getView().dispatchTouchEvent(..)? –  May 13 '15 at 10:58
  • "Can I do something like getView().dispatchTouchEvent(..)?" -- no, because the `View` is in another process, not yours. – CommonsWare May 13 '15 at 12:24
  • Is there an alternative way to interact with a view automatically? –  May 13 '15 at 15:26
  • You can use whatever the accessibility APIs offer you. You can probably do much more on a rooted device, but I don't even really know where to point you for what to do in that case. – CommonsWare May 13 '15 at 15:33
  • CommonsWare: Could you make this an answer, please? I will accept it. –  May 16 '15 at 13:39
  • http://stackoverflow.com/questions/11635934/touch-through-translucent-app – Shirish Herwade Jul 14 '16 at 07:20
  • How does this app do it : Airplane Mode - https://play.google.com/store/apps/details?id=com.floriandraschbacher.airplanemode.free – android developer Feb 03 '17 at 19:03

1 Answers1

0

AFAIK, you can't, for obvious security reasons, except probably on rooted devices. Even the accessibility APIs don't support this AFAICT. Allowing arbitrary apps to fake input to other apps would be a major security hole.

If your objective is to do this during testing, instrumentation testing allows you to simulate touch events in your own app.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • can I use instrumentation in a background service? My device is rooted. I tried that, I got no error, but how can I make sure that the view was touched if the touch was sent from a background service? Thanks. – Dania Mar 15 '16 at 11:13
  • Is it perhaps possible these days? Maybe via accessibility APIs ? – android developer Jan 08 '17 at 13:43
  • @androiddeveloper: AFAIK, the accessibility APIs still do not allow delivery of specific touch events. – CommonsWare Jan 08 '17 at 13:52
  • @CommonsWare How do eye tracking apps do it (this for example: http://www.androidcentral.com/eye-tribe-gaze-tracking-enables-finger-free-fruit-ninja-android ) ? Via hardware that uses adb commands? Or maybe Samsung-only APIs, as someone wrote me a long time ago about TeamViewer: http://stackoverflow.com/a/20391295/878126 ? And what about key events? I know that there are many apps that allow emulating a global back key (like this one: https://play.google.com/store/apps/details?id=com.appspot.app58us.backkey ) . How do they work? – android developer Jan 08 '17 at 14:00
  • @CommonsWare This can help for both people with disability, and for UI automation tools for power users and developers. Anyway, I've made a request: https://code.google.com/p/android/issues/detail?id=231758 – android developer Jan 08 '17 at 14:18