1

I'm deploying an app which create a cursor using a OverlayView. I'm able to move it around the screen and outside my app (service).

But now I want to implement the touch event. I'm running the cursor in a service, so I can still move it in the menu or anywhere. But I don't know how to make a click.

So once I'm outside my app if this service gets a click event (programmatically or remotely), I want to make a touch event on the view under my cursor.

Any idea how to get the view under it or how to send a system touch event?

This is how I control and draw the cursor:

class OverlayView extends ViewGroup {
private Paint mLoadPaint;
boolean mShowCursor;

Bitmap  cursor;
public int x = 0,y = 0;

public void Update(int nx, int ny) {
    x = nx; y = ny;
}
public void ShowCursor(boolean status) {
    mShowCursor = status;
}
public boolean isCursorShown() {
    return mShowCursor;
}

public OverlayView(Context context) {
    super(context);
    cursor = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
    mLoadPaint = new Paint();
    mLoadPaint.setAntiAlias(true);
    mLoadPaint.setTextSize(10);
    mLoadPaint.setARGB(255, 255, 0, 0);
}


@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    //canvas.drawText("Hello World", 0, 0, mLoadPaint);
    if (mShowCursor) canvas.drawBitmap(cursor,x,y,null);
}

@Override
protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    return true;
}

Thank you

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Marcel
  • 2,094
  • 3
  • 23
  • 37
  • hi can you please post more of your code..I'm trying to do the same thing – Abhi Apr 21 '16 at 09:15
  • hi..this is what i did for your question. i'm moving the mouse pointer overlay by changing coordinates in a service and sending its coordinates to a method that checks if any of the `view`s coordinates in layout matches with the overlay's coordinate and perform a click using `button.performClick()`. this is what i did..can you please tell me how you did it – Abhi Apr 22 '16 at 07:29
  • Hi, this was 3 years ago and was for the company where I was working, so unfortunately I do not remember what I did nor I have the code. I just can remember that I went native, I modified the behavior from the native code writing directly into the kernel. But that was because it was our own device. – Marcel Apr 22 '16 at 07:49

1 Answers1

1

The normal way to do that is using Instrumentation.sendPointerSync(motionEvent); which use the permission :

<uses-permission android:name="android.permission.INJECT_EVENTS">
</uses-permission>

But ... to receive the INJECT_EVENTS permission your app must be signed by the same signature that the system is signed with. (The platform key of the device where the app is running ...) Ex: if you want to run your app on a samsung device, you have to sign your app with the samsung secret key ... Only the constructor can do that... (sry)

In conclusion, your application can inject events only inside itself. (otherwise it would be a security nightmare, imagine an apk which allow an user to control your phone ...)

Edit : If you have the key (for exemple if you build your own device, own ROM) you should sign the apk with the signapk.jar (a tool included with the Android platform source bundle) :

java -jar signapk.jar certificate.pem key.pk8 your-app.apk your-signed-app.apk
Goo
  • 1,318
  • 1
  • 13
  • 31
  • mmmh... ok thank you! Maybe I have the sign of the device that I'm working on. If not I would try to create a driver and make my own mouse pointer. Any idea where to find info of that? – Marcel Jan 23 '13 at 20:57
  • I don't know your device targer, but if you make a driver, i think the security principle should be the same, at a moment if you want to install it on a classic device (i.e. a non root device) your code must present an "indentification" (ie being signed). So i think you must work for rooted phones, or for your own Device/ROM whose you control the security. – Goo Jan 23 '13 at 23:59
  • you can find a way to use inject event on a rooted device here : http://stackoverflow.com/a/7328055/1720391 – Goo Jan 24 '13 at 00:06
  • Cool! really helpful! I will try it. My target device are rooted (the part of the server) the client don't need to be. – Marcel Jan 24 '13 at 07:53