Keep android overlay always on top - android

I'm looking for a way to keep an application (System application) that creates an overlay of type WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY always on top of the screen, even on above any other overlay that can be created.
I'm adding the view directly with the WindowManager so I have tried few things:
Add an observer to the window and on each change call bringViewToFront for the overlay and it does not work.
Adding and removing the view each second..it works and it is always over other overlays but performance is very bad (Logically..)
Is what I'm looking for even possible? Thanks

Related

How to control WindowManager overlay priority in Oreo?

I have an overlay for my app, OV1, which is basically a button the user can move around on their screen. When this overlay is pressed, it spawns a second overlay, OV2, which contains multiple sub-buttons positioned around OV1's current location. OV2 also dims the rest of the screen while present, by use of the flag WindowManager.LayoutParams.FLAG_DIM_BEHIND.
One issue I had when initially implementing the above, is that the dimming effect was cast over everything that isn't OV2, including OV1, darkening and making it unclickable, which prevents OV2 from being closed as OV1 is the toggle button. The solution I found was setting layout params differently for each overlay:
OV1 gets:
params.type = WindowManager.LayoutParams.TYPE_PRIORITY_PHONE;
Whereas OV2 gets:
params.type = WindowManager.LayoutParams.TYPE_PHONE
Since OV1 had higher priority, it always stayed on top of OV2, so it wasn't affected by FLAG_DIM_BEHIND, and the user could interact with OV1 just fine to close the OV2 sub-button menu.
This has always worked fine, until I started updating the app to target Android's API 27 (Oreo). As part of the required changes, I've had to change both overlays' param types to WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, so I lost the ability to give OV1 a higher priority to make sure it stays on top of OV2. Is there an alternate way to replicate this behavior, ensuring OV1 remains the topmost overlay, or forcing it to the foreground whenever OV2 is spawned?
While I never found a solution for this, one workaround is to reattach the overlays to the window manager in the intended order (when several views of type TYPE_APPLICATION_OVERLAY are added to the window manager, the most recent ones go on top), by calling removeViewImmediate() followed by addView() for the view you want to be on top.
It's not pretty, as the user can see the overlay blink out of existence for a split second, but until there's a better way, it'll have to do.

WindowManager animation with "WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY" property in accessibility service

I am developing an application which has Accessibility service. With this service I will do my back end functionality(button clicks and navigating to other screens) by showing a window manager screen on top. In this window manager screen I need to add any animation(in worst case, I have to add a simple progress bar). My issue is,
1) If I add WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY (For Lollipop and below) OR WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY (For Marshmallow and above) property to window manager, I am successfully completing back end functionality but animation is hanging. It is occurring for simple progress bar also.
2) If I add WindowManager.LayoutParams.TYPE_PHONE to window manager, I am successfully showing animation but back end functionality is not working.
Any minor help is appreciable. Thanks in advance.
The difficulty in accessibility services with adding views is getting the correct permissions. Your approach on approach one is correct. The performance issues in such animations area an unrelated issue. Once a view has been successfully added to the window manager any concerns with view performance are similar to concerns as if you were in the context of an activity. Unless, you have some type of memory leak, view leak, etc. Adding overlay views to the window manager directly is prone to mismanagement, because you also have to remove those views yourself. Otherwise old instances of overlays will stick around forever and bog down performance.
Stick with option one from your services point of view and figure out your performance issues outside of the scope of this decision. They are only indirectly related. Your performance problem is not directly caused by this decision, but rather mistakes elsewhere. What these could be I can only provide a speculative answer on.

Entire screen overlay

just a simple question (hopefully):
Is there any easy way to make a simple translucent screen overlay on android? Just a solid color would work perfectly for starters. I would wager that this wouldnt be started as an activity but run as a service since the overlay should display over everything always until disabled, whether or not the application that hosts this overlay is running/paused/stoped.
It's easy to make an overlay for a specific activity/fragment, but I'd want this overlay to appear over the entire screen regardless of what's on screen (except maybe error dialogs, those seem to take presidence over anything).
Edit: To add more information, apps like "twilight" and "screen filter" seem to be able to do this sort of thing, whereas they are able to display a color at a custom transparency over the entire screen whether or not the app is running.
Yeah, it's possible to implement entire screen overlay using SYSTEM_ALERT_WINDOW permission and WindowManager, that's how Facebook chat heads works. Basically, you need to add your View to WindowManager instance, search WindowManager for more information.

Overlay a view in every activity

In an app I am working on, I'd like to have a bar with some controls always present at the bottom of the screen. It should overlay every activity in the app but also be able to disappear and reappear. To do this I've considered some options, such as simply using a linear layout and setting the visibility in every activity or using a fragment somehow. Probably those would work but I feel there must be a better solution. So my question is: what is the best way of doing this?
There are two ways you could do this. You could just use Fragments, and make your overlay be a fragment.
The other way would be to sublcass Activity with an AcitivityWithOverlay, which handles the overlay appearing and disappearing then have all of your activities inherit that. If I did it this way, I'd make my overlay a singleton so I wasn't creating extra versions all over the place that did the same thing.

android: control the overlay order of popups

I have multiple popups in my android app. I would like to control the "z-index" of the popups. I can't seem to find any way to control the relative positioning. Is this possible?
you basically cannot have z-index for your activities.
but whenever you want to bring any activity on the top, you can add the flag Intent.FLAG_ACTIVITY_REORDER_TO_FRONT to your Intent.
I've seen some peculiar things, like an alert dialog popping up behind my custom dialog. But after cleaning the project they behave properly (last called view is shown above/infront of the previous views)

Categories

Resources