Find UID of foreground app

Post your questions and help other users.

Moderator: Martin

Post Reply
drsood
Posts: 16
Joined: 06 Oct 2013 07:42

Find UID of foreground app

Post by drsood » 18 Oct 2014 17:52

Here's a shell script thats finds out the uid of the current foreground app

Code: Select all

cat /proc/$(pidof $( dumpsys window windows | awk '/mCurrentFocus/ { sub(/\/.*/,""); print $3}' ))/status | awk '/Uid:/ {print $2}'
But it doesnt seem to work with automagic probably because of the {} brackets. And enclosing the command inside '' doesnt seem to work too because ' are being already used in the command. I tried using a combo of App task running and App process runing but I cant get the process name of the current foreground app.

App task running condition might be having a BUG because it always reports automagic activity as the current foreground app (if run from shortcut/gesture). The only way to circumwent is to add a show pause (sleep) between shortcut execution and appt task running condition

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

Re: Find UID of foreground app

Post by Martin » 19 Oct 2014 08:09

Hi,

Yes, the command does not work since Automagic will interpret the parts within curly braces.
I would assign the command to a string variable first and use single quotes to avoid that the curly braces are interpreted. Single quotes within the command and backslash need to be escaped using a backslash. An action Script could look like this:

Code: Select all

cmd = 'cat /proc/$(pidof $( dumpsys window windows | awk \'/mCurrentFocus/ { sub(/\\/.*/,""); print $3}\' ))/status | awk \'/Uid:/ {print $2}\''
and then use {cmd} in action Execute Root Command.

You could also directly use an inline script in the action:
{'cat /proc/$(pidof $( dumpsys window windows | awk \'/mCurrentFocus/ { sub(/\\/.*/,""); print $3}\' ))/status | awk \'/Uid:/ {print $2}\''}


A shortcut on Android can only be used to start an activity so Automagic receives the shortcut with an invisible activity that will be closed immediately again. Automagic thus becomes the topmost task for a short duration when a shortcut trigger is executed. I agree that the outcome of the condition is surprising in this case but a sleep is the recommended workaround for the moment. I'll ignore the shortcut activity in the next version so that the sleep is not required anymore. Thanks for reporting!

Regards,
Martin

drsood
Posts: 16
Joined: 06 Oct 2013 07:42

Re: Find UID of foreground app

Post by drsood » 19 Oct 2014 15:22

The inline script was exactly what I was looking for :D :D :D
Thanks a lot Martin

Post Reply