Search code examples
androidrootgestures

how to make a secret gesture on android screen?


i would like to create a secret event gesture that will be hardcoded to my app to activate a function.

a secret hidden admin menu, that will ask for admin password and do admin staff, and i would like it to show up after the user do:

5 fingers anywere on screen for 5 seconds

this is to avoid adding a visible admenistrator button to the app since the user does not need to see it and has no use for it.

can anyone provide me with a part of code to achieve this? i searched but not found an example for the kind of gesture i need...

p.s: the code can be for rooted devices.. since it for specific rooted device..

thanks alot for the help :-)


Solution

  • You can set an OnTouchListener on your root View, then check the MotionEvent's getPointerCount() method along with a timer to check this.

    Here's a brief example:

    import android.view.View.OnTouchListener;

    private static final int FIVE_SECONDS = 5 * 1000; // 5s * 1000 ms/s
    private long fiveFingerDownTime = -1;
    
    
    getWindow().getDecorView().findViewById(android.R.id.content).setOnTouchListener(new OnTouchListener() {        
    
    @Override
    public boolean onTouch(View v, MotionEvent ev) {
        final int action = ev.getAction();
        switch (action & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_POINTER_DOWN:
                if (ev.getPointerCount() == 5) {
                    // We have five fingers touching, so start the timer
                    fiveFingerDownTime = System.currentTimeMillis();
                }
                break;
    
            case MotionEvent.ACTION_POINTER_UP:
                if (ev.getPointerCount() < 5) {
                    // Fewer than five fingers, so reset the timer
                    fiveFingerDownTime = -1;
                }
                final long now = System.currentTimeMillis();
                if (now - fiveFingerDownTime > FIVE_SECONDS && fiveFingerDownTime != -1) {
                    // Five fingers have been down for 5 seconds!
                    // TODO Do something
                }
    
                break;
        }
    
        return true;
    }
    });