title: Injecting Touch events to another app on Android
body: <p>I have a game without built-in controller support, with touch-based controls. I would like to use a gamepad with it and get some hands-on programming experience in Android.</p>
  
  <p>I want to figure out if I can have my app listen for controller events, and forward them to my games.</p>
  
  <p>I learned how to <a href="https://developer.android.com/training/game-controllers/controller-input" rel="noreferrer">process controller events</a>. Then I found out I cannot forward them due to security - programmatically sending touch events is now allowed for good reasons. </p>
  
  <p>According to <a href="https://www.pocketmagic.net/injecting-events-programatically-on-android/" rel="noreferrer">this blog (its SSL cert expired)</a>, there are three ways to inject touch events: </p>
  
  <ol>
  <li>Using internal APIs (prone to change, need root to send to another app)</li>
  <li>Using the public API, Instrumentation (need root to send to another app)</li>
  <li>Write directly to /dev/input/eventX (need root to change permissions)</li>
  </ol>
  
  <p>Other methods I have found include building your own platform, so you can set your application as a system app and sign it with the platform certificate, to provide it with the required permissions. </p>
  
  <p>Or wrapping another application within your own, but using Instrumentation to send touch events there still require root.</p>
  
  <p>I have also read about the Accessibility API, but it looks like it is considered a misuse of the API and maybe impossible in the future.</p>
  
  <p>I would rather not root my device, as many games have issues if they detect root.</p>
  
  <p>I was about to give up, but then I found apps like these:</p>
  
  <ol>
  <li><a href="https://play.google.com/store/apps/details?id=com.makeithappend.gamepadtouchmapperpaid" rel="noreferrer">Touch4Gamepad</a> - This one makes use of hardware called the Arduino Board. <a href="https://www.youtube.com/watch?v=JldvqLJF6yg" rel="noreferrer">Demo of Touch4Gamepad</a></li>
  <li><a href="https://play.google.com/store/apps/details?id=com.chaozhuo.gameassistant&amp;hl=en" rel="noreferrer">Octopus</a> - This one seems to be the approach I want to try, but it has pretty polarizing comments. It also does not work with Google Play service. <a href="https://www.youtube.com/watch?v=JKkvO2t5vow" rel="noreferrer">Demo of Octopus</a></li>
  <li><a href="https://www.youtube.com/watch?v=1Ch4AaGay2E" rel="noreferrer">DRO-CON</a> - This one seems to take an entirely different approach by doing it via a connected PC.</li>
  <li><a href="https://www.teamviewer.com/en/download/android/" rel="noreferrer">TeamViewer</a> - It seems to be able to remote control Android including sending touch events. According to this <a href="https://stackoverflow.com/questions/45414734/how-can-i-force-touch-events-on-none-routed-android-devices/45414862#45414862">this thread</a>, TeamViewer struck a deal with phone manufacturers to get the required permissions. <a href="https://community.teamviewer.com/t5/Knowledge-Base/Supported-manufacturers-for-remotely-controlling-Android-devices/ta-p/4730" rel="noreferrer">Supported Manufacturers for Remote Control</a></li>
  </ol>
  
  <p>Despite Android security, options 2 seem to be able to do it regardless. How is it doing it?</p>
  
  <p>EDIT: I downloaded Octopus' APK and took a look inside. It includes a file named libinject.so... so that's where it is doing its magic. I have a new direction to research now, but if anyone has links to resources related to this I would really appreciate it.</p>