Page 1 of 1
Detect foreground app
Posted: 18 Nov 2019 20:19
by markd89
Hi,
I have a Flow which checks if my VPN is accessible and if not, then it starts the VPN. This works nicely. However, then the VPN is the foreground app.
I'd like my Flow to start VPN if needed, then swap back to the app that was in the foreground before the Flow started.
For example, I have Firefox open, the Flow runs, starts the VPN and then switches back to Firefox.
How to detect that foreground app?
Thanks!
Mark
Re: Detect foreground app
Posted: 19 Nov 2019 23:48
by markd89
Another (maybe easier option) - How can I minimize an app? Control-UI?
This way, I'd run the VPN app then minimize it and theoretically, I'd be back in the app I was before.
Thanks,
Mark
Re: Detect foreground app
Posted: 21 Nov 2019 19:32
by Martin
Hi,
You can use 'Control UI' for this. Maybe even a single line with back(); can work, depending on the VPN app.
Regards,
Martin
Re: Detect foreground app
Posted: 23 Nov 2019 16:05
by markd89
Thank you!!
Re: Detect foreground app
Posted: 09 Dec 2019 15:01
by markd89
OK, so this is not working so smoothly, so I'm back looking at alternatives.
I'd like the flow to:
1. Detect the current foreground app.
2. Do stuff (start VPN)
3. Switch back to the app detected in step 1.
I found a Github project
https://github.com/ricvalerio/foregroundappchecker which provides code to detect the foregound app. Can this be done in Automagic? If not, maybe the devs can use this code and add that capability?
Re: Detect foreground app
Posted: 09 Dec 2019 17:55
by Desmanto
Foreground app detection can be done already using trigger App Task Started, either using classic (Control UI) or usage statistic. However this require you to save every app changes first before getting into the app. To detect current foreground app without storing previous value, currently we still need root to get the dumpsys result. Once we have AM 1.38 released, we can do it without root anymore.
But for your case, it is much easier to use control UI : back(), to get back to the previous foreground app before the VPN app.
Re: Detect foreground app
Posted: 10 Dec 2019 16:45
by markd89
Desmanto wrote: ↑09 Dec 2019 17:55
Foreground app detection can be done already using trigger App Task Started, either using classic (Control UI) or usage statistic. However this require you to save every app changes first before getting into the app. To detect current foreground app without storing previous value, currently we still need root to get the dumpsys result. Once we have AM 1.38 released, we can do it without root anymore.
But for your case, it is much easier to use control UI : back(), to get back to the previous foreground app before the VPN app.
Thanks much for your response. The back() is imperfect just because of the VPN app. My phone is rooted. Is there a way with the current AM to get the current app (using root)?
Thanks again and look forward to 1.38
![Smile :-)](./images/smilies/icon_e_smile.gif)
Re: Detect foreground app
Posted: 10 Dec 2019 17:16
by markd89
I found this:
https://stackoverflow.com/questions/131 ... t-activity
This root command seems to work:
dumpsys activity recents | grep "Recent #0"
I'm working on chopping up the result to just get the app name now.
edit:
dumpsys activity recents | grep "Recent #0" | cut -f 3 -dA= | cut -f 1 -d' ' | tr -d "\n"
seems to get me the app name, at least in terminal where I am testing it. Next to incorporate into a flow!
Re: Detect foreground app
Posted: 10 Dec 2019 18:32
by Desmanto
Yeah, you got it. I was going to use the same method, but using regex instead of cut.
Execute root command
Code: Select all
dumpsys activity recents | grep 'Recent #0'
Then use regex to get the package name
Code: Select all
f = findAll(stdout, ' \\* Recent #0: TaskRecord\\{.* A=(.*) U=.*\\}', true);
pn = f[0][1];