Reputation: 56
I'm working on a remote access application (similiar to AnyDesk) to access mobiles on a lan network and use them with just your browser. At this point, I'm able to send the device display output via WebSockets and access to it live via a browser (from my PC). This works fine and I can see whatever I do in the mobile, but in my PC. Now, I'm trying to find a way to simulate a tap input: the client (my pc) sends the x and y coordinates to the mobile (via WebSocket) and I'm able to get those coordinates in my android code, but I can't find a way to fire this input. Im using Adnroid Studio Hedhedog and my project is written in Java Gradle with the minSDK version 30.
I've tried whatever is in my knwoledge, as I hadn't ever programmed in Java, to get this work.
Tried using instrumentation:
public class InputSimulator {
public static void simulateTap (int x, int y) {
Instrumentation inst = new Instrumentation();
long timeDown = SystemClock.uptimeMillis();
long eventTime = timeDown + 100;
MotionEvent motionEventDown = MotionEvent.obtain(timeDown,eventTime,MotionEvent.ACTION_DOWN,x,y,0);
inst.sendPointerSync(motionEventDown);
MotionEvent motionEventUP = MotionEvent.obtain(timeDown,eventTime,MotionEvent.ACTION_UP,x,y,0);
inst.sendPointerSync(motionEventUp);
motionEventDown.recycle();
motionEventUp.recycle();
}
}
This would work if android security would allow me (above 8.0 is not allowed, I think): this raises java.lang.SecurityException: Injecting to another application requires INJECT_EVENTS permission
when outside my application. Adding this permission in the MANIFEST, shows an error: Permissions with the protection level signature, privileged or signatureOrSystem are only granted to system apps
...
Using Runtime to run a command also doesn't seem to work...
public class InputSimulator {
public static void simulateTap (int x, int y) {
Process process = Runtime.getRuntime().exec("input " + x + " " + y);
int exitCode = process.waitFor();
Log.d("INPUT COMMAND CODE: " + exitCode); // exitCode is 0 and no tap is performed.
}
}
Also tried to create an AccesibilityService
but can't find the way to make my code work.
Looking and searching through the entire web to find a way to launch a virtual input screen tap: seems impossible, even more in lasts android versions. Is there a way to perform a tap, even outside my application (maybe a service or sum)? Do I only have left to use rooted devices or install my application at a system level? Note that I don't want to send input events inside my application, but any application that the user has open.
Upvotes: 0
Views: 181