5

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

Yevgeny Simkin
  • 27,946
  • 39
  • 137
  • 236
  • create a service in which you detect gestures and once a gesture is matched, start activity – Umer Farooq Jul 28 '13 at 17:44
  • @UmerFarooq this question http://stackoverflow.com/questions/6714020/how-can-a-service-listen-for-touch-gestures-events points in that direction as well, though it's never given a green checkmark... Does this approach actually work? – Yevgeny Simkin Jul 28 '13 at 17:47
  • Although I haven't worked with such custom activities, however the question you linked, links to this answer: http://stackoverflow.com/questions/4481226/creating-a-system-overlay-always-on-top-button-in-android This may help you. – Umer Farooq Jul 28 '13 at 17:55

2 Answers2

4

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.

android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • The app I link to above doesn't have any persistent notification in the status bar (as per the description in the docs)... is it possible that they're doing it some other way? Or is having your foreground Service put an icon in the status bar a recommended option that you can skip? – Yevgeny Simkin Jul 29 '13 at 06:12
  • it doesn't? weird. i can clearly see in all of the screenshots that it has a notification (with 4 squares). are you sure about this? – android developer Jul 29 '13 at 06:27
  • If we're talking about https://play.google.com/store/apps/details?id=com.shere.easycontroller&hl=en then yeah, no notification. However I'm looking at it on my 4.0.3 device (don't have the 4.3 device handy this second), so perhaps the status bar requirement was introduced later? – Yevgeny Simkin Jul 29 '13 at 06:41
  • on both apps, i can clearly see a notification on the status bar that just sits there, in all of the screenshots. i think you are looking at the wrong place. the requirement for a notification for each foreground service was intorduced a very long time ago, i think on 2.2 (or maybe even before), because google was "angry" about too many developers that make things run constantly without letting the users any indication,knowledge and control about it. – android developer Jul 29 '13 at 06:47
  • anyway, please don't try to hide the notification or make it transparent. it's a good thing to be clear to the user that something is running. – android developer Jul 29 '13 at 06:48
  • I'm not "trying" to hide it, I'm just curious how it works and what they're doing. I'm well aware of the best practices and am not keen on violating them. But I feel like I'm missing some fundamental understanding of how this feature is implemented, and am trying to get that info. And while I agree that the "screenshots" have something in them that looks like a notification, I'm actually running the app on my device and no notification is present! – Yevgeny Simkin Jul 29 '13 at 06:54
  • what device do you have? i've tried them out and they have a notification. can you please post a screenshot? also, what information are you missing from what i've written? – android developer Jul 29 '13 at 06:58
  • Added a screenshot. It's a Motorola Razr M running 4.0.3 on Verizon. – Yevgeny Simkin Jul 29 '13 at 07:04
  • this is weird. i remember i've tested the easy-controller app and it worked fine, and now i can't open the bottom bar, making this app useless. tested on galaxy s3, with android 4.2.2 . on galaxy s2 with android 4, it works and without a notification, meaning the app might be killed from time to time (try opening a heavy app, like a game for example). also, you can see that the app takes space at the bottom, so it's not invisible and therefore not "tapjacking". – android developer Jul 29 '13 at 07:19
  • I've set it to open from the left. Obviously it interferes with Google Now if it's set up to open from the bottom. – Yevgeny Simkin Jul 29 '13 at 07:21
  • no i really mean it doesn't work. i can't show the bar, no matter which position i choose for it. it doesn't even show the black area it used to show. – android developer Jul 29 '13 at 07:23
  • @Dr.Dredel i've found out this article: http://www.androidpolice.com/2013/07/29/android-engineer-explains-why-some-apps-suddenly-have-persistent-notifications-after-you-upgrade-to-android-4-3/ . they might be using a hack to hide the notification and still be in the foreground. however, starting android 4.3, it shouldn't work any longer (at least till the next hack is found) – android developer Jul 29 '13 at 22:40
  • I can vouch (having seen it work) that it works perfectly in 4.3 on at least the Samsung Nexus. However, as I said, if this *is how it's done, I don't really care since I have no objection to there being a notification in the status bar. – Yevgeny Simkin Jul 29 '13 at 23:13
  • what samsung nexus? nexus S or galaxy nexus? or maybe nexux 10 (the tablet) ? good luck with the app. – android developer Jul 30 '13 at 06:38
  • @androiddeveloper what is the purpose of `parent.removeView(view);` in this code. And is setting gravity to top left means the left side of the screen will detect swipe gestures? – Umer Farooq Aug 24 '13 at 20:09
  • i've written the parent.removeView as a mean to detach the view from its current parent (for example if it was declared in XML). if you just created the view without any parent, you can remove this code. putting the view in XML could help you estimate how it would look like, and you can also use its layoutParams if needed. about the gravity, this is correct, and if you are not familiar with gravity, you should read about it: http://developer.android.com/reference/android/view/Gravity.html – android developer Aug 24 '13 at 20:13
  • @androiddeveloper thanks man. Really where did you learn this stuff? – Umer Farooq Aug 24 '13 at 20:22
  • @UmerFarooq i read a lot of websites related to android. plus i read here and watch google lectures (including google io lectures). i'm developing for android for almost 3 years now... :) – android developer Aug 24 '13 at 20:41
  • @androiddeveloper what should I do in the service in order to keep the app alive? Is it necessary for the service to be a foreground service or it can be a background service? – Naruto Aug 28 '13 at 12:32
  • @androiddeveloper bro I have tried to implement this functionality but I am getting `force close`. I don't know what am I doing wrong. Here is the link to the code: http://bit.ly/1fj1OJ7 – Umer Farooq Aug 28 '13 at 14:43
  • @Naruto i like your show :) . anyway, about foreground services, they are used to let your app running "in the background". the OS has its privatizations of when to kill each app , depending on its state. better read this : http://developer.android.com/guide/components/processes-and-threads.html . a normal service will let the app being killed quite easily . – android developer Aug 28 '13 at 14:46
  • @UmerFarooq i don't know exactly what you are trying to do. can you please post a new question on SO , and ask a question ? you could show here a URL to the new post if you wish. – android developer Aug 28 '13 at 15:03
  • I am trying to show up a view just like the above app is doing, I have written the code for that reason. – Umer Farooq Aug 28 '13 at 15:05
  • @UmerFarooq this conversation has become quite long. please post a new question. try to minimize your code and show only the relevant code that you think is problematic. only then i will check your entire project which may be too large for such a question. please try to understand. – android developer Aug 28 '13 at 15:09
  • umm ok, I am posting a question. – Umer Farooq Aug 28 '13 at 15:13
  • @androiddeveloper lol thanks. Would you like to answer my question. I am trying to create an overlay activity which detects gestures. http://stackoverflow.com/questions/18494727/creating-an-overlay-activity – Naruto Aug 28 '13 at 18:49
3

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.

Community
  • 1
  • 1
Rich Schuler
  • 41,814
  • 6
  • 72
  • 59
  • Perhaps they are using a different methodology? I just tested this app on a Nexus running 4.3 and it works perfectly, so whatever they're doing is not affected by whatever security hole was plugged in 4.0.3. – Yevgeny Simkin Jul 29 '13 at 06:07
  • 1
    i think that since it's clearly not tapjacking, it's ok. there are plenty of apps that show things on top that the user can interact with. after facebook has shown their chat-heads, there are even apps that have their own rounded heads images: https://play.google.com/store/apps/details?id=com.boatmob.floating.touch https://play.google.com/store/apps/details?id=robj.floating.notifications http://lifehacker.com/paranoid-android-brings-chat-head-style-multitasking-to-496388053 – android developer Jul 29 '13 at 06:36
  • 1
    @Dr.Dredel I can see the arguments for and against it. So I'm not inclined to make a judgement if you should do it or not. I'd only suggest to be aware that the ability to do that might be removed in the future. One can hope they might replace the hacks with a more 'official' method of doing it. – Rich Schuler Jul 29 '13 at 20:18
  • @Qberticus, I sumarilly agree with your above comment - and though 2 years later the feature continues to work in the the latest Android release, this is one of those features that I really wish they just up and made an official solution for. I can propose a name like `VisualService` or `DetachedActivity`... Come on Google! (I mean Alphabet) hop to it! – Yevgeny Simkin Sep 05 '15 at 20:16