Detect foreground app

Post your questions and help other users.

Moderator: Martin

Post Reply
markd89
Posts: 17
Joined: 18 Apr 2014 16:08

Detect foreground app

Post by markd89 » 18 Nov 2019 20:19

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

markd89
Posts: 17
Joined: 18 Apr 2014 16:08

Re: Detect foreground app

Post by markd89 » 19 Nov 2019 23:48

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

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: Detect foreground app

Post by Martin » 21 Nov 2019 19:32

Hi,

You can use 'Control UI' for this. Maybe even a single line with back(); can work, depending on the VPN app.

Regards,
Martin

markd89
Posts: 17
Joined: 18 Apr 2014 16:08

Re: Detect foreground app

Post by markd89 » 23 Nov 2019 16:05

Thank you!!

markd89
Posts: 17
Joined: 18 Apr 2014 16:08

Re: Detect foreground app

Post by markd89 » 09 Dec 2019 15:01

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?

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: Detect foreground app

Post by Desmanto » 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.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

markd89
Posts: 17
Joined: 18 Apr 2014 16:08

Re: Detect foreground app

Post by markd89 » 10 Dec 2019 16:45

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 :-)

markd89
Posts: 17
Joined: 18 Apr 2014 16:08

Re: Detect foreground app

Post by markd89 » 10 Dec 2019 17:16

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!
Last edited by markd89 on 11 Dec 2019 16:00, edited 1 time in total.

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: Detect foreground app

Post by Desmanto » 10 Dec 2019 18:32

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];
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

Post Reply