26

Android enables apps to draw over other apps with android.permission.SYSTEM_ALERT_WINDOW and it's called a floating/overlaying app. For example Facebook Messenger has always visible chat bubbles at the edges of screen.

My question is: Is it possible to detect or block in Java code any app which draws over my app?

jrwm
  • 261
  • 3
  • 5

1 Answers1

15

There is a View#onFilterTouchEventForSecurity() method you can override to detect if the motion event has the FLAG_WINDOW_IS_OBSCURED. This will let you know if something is drawn on top of your view.

@Override
public boolean onFilterTouchEventForSecurity(MotionEvent event) {
    if ((event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) == MotionEvent.FLAG_WINDOW_IS_OBSCURED){
        // show error message
        return false;
    }
    return super.onFilterTouchEventForSecurity(event);
}

If you just want to protect your app from tap jacking due to another app drawing over your app you can add setFilterTouchesWhenObscured to your views via XML or programmatically.

Xavier Rubio Jansana
  • 6,388
  • 1
  • 27
  • 50
Don Cung
  • 211
  • 2
  • 4