Yevgeny Simkin
Yevgeny Simkin

Reputation: 28349

How can one detect screen gestures outside ones own app?

I know that SO frowns heavily on "how do I do this" questions, but I don't see a way around it this time.

I'm a seasoned and expert Android developer so I feel really goofy asking this question, but I just installed this app and it does something that I thought was impossible.

Namely, it listens for screen gestures (in this case swipes from edges of the screen inward) REGARDLESS of where you are on your device... meaning, it listens when you're on the Launcher home screens, and when you're in OTHER apps... no matter what you're doing, it's listening and when it detects the swipe from the edge of the screen it lets you bring out a hidden settings drawer which then lives as a transparent (fragment? dialog?) View on top of whatever other app you're in and when dismissed (by hitting BACK) leaves you wherever you were in your previous experience.

I honestly have no clue how this is possible and would really love a nudge in the right direction. [EDIT]enter image description here

Upvotes: 5

Views: 4776

Answers (2)

android developer
android developer

Reputation: 116402

there are 2 things you have to handle:

  1. showing a view on top. for this, you need to use a special permission TYPE_SYSTEM_ALERT . here's a code for showing a view on top:

    final WindowManager.LayoutParams param=new WindowManager.LayoutParams();
    param.flags=WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
    final View view=findViewById(R.id.view1);
    final ViewGroup parent=(ViewGroup)view.getParent();
    if(parent!=null)
      parent.removeView(view);
    param.format=PixelFormat.RGBA_8888;
    param.type=WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
    param.gravity=Gravity.TOP|Gravity.LEFT;
    param.width=view.getLayoutParams().width;
    param.height=view.getLayoutParams().height;
    final WindowManager wmgr=(WindowManager)getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
    wmgr.addView(view,param);
    // TODO handle overlapping title bar and/or action bar
    // TODO you must add logic to remove the view
    // TODO you must use a special permission to use this method :android.permission.SYSTEM_ALERT_WINDOW
    
  2. keeping the app alive. for this, you need to create a service that runs in the foreground, which means it uses a notification. here's a link.

there are plenty of apps that have this ability. the first one i've seen was AirCalc.

Upvotes: 4

Rich Schuler
Rich Schuler

Reputation: 41972

This is now considered a security vulnerability that has been plugged in Android 4.0.3+: Question: TYPE_SYSTEM_OVERLAY in ICS This is/was called tapjacking. IMHO, creating something that depends on this functionality should now be avoided since it is now something that officially will be patched if other methods are found in future versions. However, that question does have an answer that shows you how to do it in 4.0.3+ If you do choose to go that route then be aware that your ability to do it could disappear with newer versions of Android.

Upvotes: 3

Related Questions