Navigation Apps Auto turn on/off GPS

Share and discuss your flows and ideas with other users.

Moderator: Martin

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

Navigation Apps Auto turn on/off GPS

Post by Desmanto » 26 Aug 2017 17:36

This is my navigation apps flow. http://automagic4android.com/flow.php?i ... 058c9c10c4

It is used to auto turn on GPS when any navigation apps are active and turn off the GPS after 1 minute exit from the apps. This flow require root to run properly (GPS toggling needs root).

There are two triggers, App Task Ended and App Task Started. Please edit this two to match the apps those you use. For example, I just use Google Maps.

The next is expression to check which is the trigger (since we use 2 triggers in single flow). If the trigger is "App Task Started", it will enable the GPS if it is not enabled yet.

If not, it will sleep for 1 minute, then turn the GPS off. You can change the time to your need.

This flow use "Stop the currently ...." Flow execution policy. Which means if you exit from your navigation apps for several seconds (example : checking notification) and open the app again (within that sleep timer), the sleep will be resetted and start counting again only after you really exit from the app. Thus the flow will never wrongly turn off the GPS when you are still using the navigation apps.

UPDATE for non root user
I have modified the flow, changing the GPS action (2 actions) which require root, to Action set system setting, which can be used by non root user, with the help of write secure permission. (Thanks anuraag for the reminder)
http://automagic4android.com/flow.php?i ... 689620708f
Warning : Enabling adb, and using Action Set System Setting improperly can brick your phone. You may lose your data and may need reset your phone. There are cases of bootloop caused by improper modification to the system setting. Make sure you understand the risk before continuing. When in doubt, just stop. Do With Your Own Risk.

PC setup
To use this flow, you have to grant write secure setting permission to Automagic. if you have grant it (or you have root), you can skip to the Set system setting directly. You need adb for this, of course means a PC. Can be windows (preferred), linux or mac. No root is required, that's why we need the help of adb. Xda has a quite neat tutorial for this : How to Install ADB on Windows, macOS, and Linux. You should check it out.
For windows, the fastest way to install the adb is to use adb installer. Just need to run it as admin, yes yes yes, and it is done.

You might to need to install your phone specific driver to make sure the adb can recognize your phone. My phones are in this category, need to install additional phone driver provided in the internal emulated CD built-in. Do whatever necessary until adb can detect your devices. You might need to disable firewall for a while during the adb command, if the firewall policy is too tight.

Phone setup
At you phone, make sure the USB debugging is on, which is hidden in developer options. To enable developer options (if you haven't), tap 7 times at the build number/kernel version at about phone. After that, go to settings > Developer option > enable the USB Debugging, tap OK for the prompt.

After adb and drivers has been installed properly, and USB debugging has been turned on, connect your phone with the PC using the original cable (or any cable that support data transfer). For some phones, you might need to switch to MTP to enable the USB Debugging detection. You might see some drivers installing if it is the first time you connect the adb.

Command Prompt
At PC, open command prompt at the adb folder (or can be anywhere if you install system-wide). Type this command to check if your device has been recognized

Code: Select all

adb devices
You should see the some 8 hexadecimal that is your device adb serial number. There will be a prompt to allow the USB debugging connection for this PC, tap OK at the phone.

Then type this (or just copy from here) to grant automagic write secure setting permission

Code: Select all

adb shell pm grant ch.gridvision.ppam.androidautomagic android.permission.WRITE_SECURE_SETTINGS
After you enter it, there will be nothing display to indicate it is success. You can only know if it errors, there will be error code displayed. So if nothing appear, you are good to go.

The permission has been granted, now we can continue on the phone. Eject/unmount any mounted drive from you phone and unplug the cable. PC is not needed anymore.
You only need grant the permission once. The permission sustained until you uninstall automagic, factory reset your or change ROM.

Flow setup
The flow is doing the same as the root counterpart. The only changes are the GPS on/off part, replaced by Action Set System Setting.
GPS toggling is stored at system database at Secure Category, with the Name : location_providers_allowed
If you turn off the location feature, this value will be blank (null).
By default, most people just turn on network location (power saving mode) but not using GPS (GPS is off). For that case, the value will be network.
If you turn on GPS as well, the value change to network,gps

So to turn on gps, you will change the value to : network,gps and to turn it off : network
Tap that Set system setting at the end of the flow, you should see the action's setting.
Set System Setting - GPS on.png
Set System Setting - GPS on.png (105.02 KiB) Viewed 51482 times
By default, the checkbox at the bottom will be unchecked. That is to ensure that you understand the risk of changing system setting directly. Everytime you change the value, you have to check this again. In case you import my flow, this will be unchecked as well. You have to check it first so it won't generate error when you execute it.

But just to make sure that your phone behave the same, turn on your gps, then tap that 3 dots on the right of location_providers_allowed. You should see the value is the network,gps. Select it. Then turn off the GPS, and tap that 3 dots again. You should see the value is network. In case the value at your phone is different than mine, please change it accordingly. After you have make sure everything is correct, check the checkbox that you have verified the setting. Do the same also for the the checkbox to set GPS off.

Once you have verified both Set System Setting action, you can add the apps you want at the triggers and enable the flow. Try to open the apps those you have set and see if the flow works properly. I have tried this at my Pureshot LP 5.1 no root, just grant that write secure permission. The Set system setting successfully turn on/off GPS.

PS: You can use this Set System Setting to automate toggling/changing various of system internal setting. But not everything works in every phone (example airplane toggling doesn't work at my phone). By using Automagic, you don't need to install any additional plugin such as secure setting plugin anymore to do action like these.

Another UPDATE for non root user - Add Choose Navi Apps
I have add additional shortcut trigger that branch to input dialog to select the navi apps. This will ensure automagic always turn on the GPS before the navi app started. This should solve the problem with some phone which delay in detecting App Task Started trigger.
http://automagic4android.com/flow.php?i ... 6ebe4f09e3

I use a very similiar flow pattern from Bender64. He use Google Maps, Sygic and Waze; so I will use them as example. Triggers also have been modified to reflect the change. Add/remove any app you need, make sure to edit the triggers, the input dialog and the parallel branch expression to launch the app. Here, I can't just simply use single condition check anymore, since we have 3 triggers. I should use parallel for efficiency. The input dialog also use 3 parallel expression to check the value (since there are 3 apps)

The flow has been modified a bit in term of logic. You can add the shortcut to the home screen. If the flow is executed from shortcut, then it will shows input dialog, to select from the navi apps. After selected, it will launched the selected app, while turning on GPS. Looking at the sequence, Action Set System Setting (to turn on GPS) should be executed at the same moment as the Launch app. So I believe there will be no more delay, since they have executed at the same time, while loading the navi apps usually takes several seconds to completely started, the GPS should be on already before that. I always try to reuse the action if it is possible to optimize the flow, thus I reuse the action from the "App Task Started" branch.

If there is still delay (which is inconceiveable), then you should copy that condition GPS enabled and Action Set GPS on from "App Task Started" branch, put them between the input dialog and those 3 parallel expressiion. It will definitely no more delay, because the GPS was turned on before launching the app.

Why we still keep the "App Task Started" branch? Because if we switch back to home screen for checking something and switch back, we want to reset the flow timer. Even though the trigger delayed, it is still better that way, rather than have to use shortcut trigger everytime we just need to switch back to previous nav app which has been launched before.
Last edited by Desmanto on 01 Sep 2017 09:23, edited 2 times in total.
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.

tphg
Posts: 57
Joined: 17 Apr 2017 05:31

Re: Navigation Apps Auto turn on/off GPS

Post by tphg » 28 Aug 2017 07:46

Thank you for sharing your flow.
How could we know the syntax of Automagic's expressions, like the one you used in this flow? I tried to search everywhere but couldn't find the list of all expressions.
Would be grateful if you could help.
Thanks in advance.

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

Re: Navigation Apps Auto turn on/off GPS

Post by Desmanto » 28 Aug 2017 13:42

jennietodd141 wrote:I enjoyed reading your post!
* dragon ball super episode 106 | animeyt
Thank you, I'm glad you enjoyed it.
tphg wrote:Thank you for sharing your flow.
How could we know the syntax of Automagic's expressions, like the one you used in this flow? I tried to search everywhere but couldn't find the list of all expressions.
Would be grateful if you could help.
Thanks in advance.
At Action Script, Action Control UI or Condition Expression, there is button for Function. When you tap that, you can look at all available function. You can filter it to search possible usage that you want. Example you want to delete something from a list. If you find using "delete", you got nothing. Try "erase", also nothing. Turns out, you can try "remove", and you will see list of function you can use to suit your needs. So need to try a lot and maybe try synonim of the word to see if there is the function for that. Last resort will be googling with the keyword.

Or Just tap the help at Script, there is a long detailed description of the syntax you can use. Script and Expression use the same syntax, but expression must always evaluated to either true or false. While Control UI introduces more functions specific for UI. You can also read it at PC for better view, at
https://automagic4android.com/en/help/c ... ion_script

The first time I switched to automagic, I spent 4 days reading the help at https://automagic4android.com/en/help
to make sure I get the idea what I can do with Automagic. Probably that is the speed boost that helps me a lot, besides that I used to be tasker user for 3 years.
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.

tphg
Posts: 57
Joined: 17 Apr 2017 05:31

Re: Navigation Apps Auto turn on/off GPS

Post by tphg » 28 Aug 2017 15:27

Desmanto wrote:At Action Script, Action Control UI or Condition Expression, there is button for Function. When you tap that, you can look at all available function. You can filter it to search possible usage that you want. Example you want to delete something from a list. If you find using "delete", you got nothing. Try "erase", also nothing. Turns out, you can try "remove", and you will see list of function you can use to suit your needs. So need to try a lot and maybe try synonim of the word to see if there is the function for that. Last resort will be googling with the keyword.

Or Just tap the help at Script, there is a long detailed description of the syntax you can use. Script and Expression use the same syntax, but expression must always evaluated to either true or false. While Control UI introduces more functions specific for UI. You can also read it at PC for better view, at
https://automagic4android.com/en/help/c ... ion_script

The first time I switched to automagic, I spent 4 days reading the help at https://automagic4android.com/en/help
to make sure I get the idea what I can do with Automagic. Probably that is the speed boost that helps me a lot, besides that I used to be tasker user for 3 years.
Thank you very much for taking your time for instruct me how to use expression.
In fact before using Automagic, I already went through the Help page as given by you above.
But it is still not clear to me in using expression, although I had tried to search by keyword for the expression you used in your flow: matches(trigger, "App Task Started:.*")
Never mind, may be I am not intelligent enough to learn this logic program.

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

Re: Navigation Apps Auto turn on/off GPS

Post by Desmanto » 28 Aug 2017 16:21

tphg wrote: Thank you very much for taking your time for instruct me how to use expression.
In fact before using Automagic, I already went through the Help page as given by you above.
But it is still not clear to me in using expression, although I had tried to search by keyword for the expression you used in your flow: matches(trigger, "App Task Started:.*")
Never mind, may be I am not intelligent enough to learn this logic program.
Oh, sorry. You mean the explanation for the : matches(trigger, "App Task Started:.*")

The function is matches(s, pattern), which return true when the variable s match the regex pattern.
In my flow, s is the trigger, and the regex to be matched is "App Task Started:.*"
"App Task Started:" part is clear, you want to match exactly as this at the beginning of the text.
While the ".*" part, . (dot) means match any character except newline, * (asterisk) means zero or more character it precedes.
So ".*" means match zero or more characters onward.

Regex "App Task Started:.*" will match something like following
App Task Started:
App Task Started: this is test
App Task Started: com.google.android.apps.maps

That ensure even if you edit the trigger and add your own apps inside, the regex will still match it properly.
There is regex tester at the script, that you can paste the above text and the regex pattern to try it out. (this is one of the feature I love from Automagic)

We can actually just use contains(s, search), which give the same result for this case. Since it is not a complex regex yet. (there is no ".*", since it is not regex)

Code: Select all

contains(trigger, "App Task Started:")
But when I create that flow, I am learning regex already. So I wanna use regex as often as possible.

The best way to learn to about which function to use is when we need it. I hit a lot of wall during the first 3 days of migrating my tasks from tasker to Automagic.
One of the biggest obstacle which I found out later by googling it (and landed on automagic help page again :D ), is the == equal in the expression.
So to test if x = 1, in the expression i must type it as : x == 1, not x = 1 !
Single equal sign = is to assign value to variable, while double equal sign == is to check if the left part is the same as the right part.
Fortunately there is debug dialog which really help a lot in finding why the script didn't perform as I want.

Don't give up yet, everything is difficult before it becomes easy.
Feel free to drop at the user help subforum, and state clearly what you want to do with the script/expression (or the flow).
I will try to help as far as I can. ;)
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.

tphg
Posts: 57
Joined: 17 Apr 2017 05:31

Re: Navigation Apps Auto turn on/off GPS

Post by tphg » 29 Aug 2017 00:51

Desmanto wrote:Oh, sorry. You mean the explanation for the : matches(trigger, "App Task Started:.*")

The function is matches(s, pattern), which return true when the variable s match the regex pattern.
In my flow, s is the trigger, and the regex to be matched is "App Task Started:.*"
"App Task Started:" part is clear, you want to match exactly as this at the beginning of the text.
While the ".*" part, . (dot) means match any character except newline, * (asterisk) means zero or more character it precedes.
So ".*" means match zero or more characters onward.

Regex "App Task Started:.*" will match something like following
App Task Started:
App Task Started: this is test
App Task Started: com.google.android.apps.maps

That ensure even if you edit the trigger and add your own apps inside, the regex will still match it properly.
There is regex tester at the script, that you can paste the above text and the regex pattern to try it out. (this is one of the feature I love from Automagic)

We can actually just use contains(s, search), which give the same result for this case. Since it is not a complex regex yet. (there is no ".*", since it is not regex)

Code: Select all

contains(trigger, "App Task Started:")
But when I create that flow, I am learning regex already. So I wanna use regex as often as possible.

The best way to learn to about which function to use is when we need it. I hit a lot of wall during the first 3 days of migrating my tasks from tasker to Automagic.
One of the biggest obstacle which I found out later by googling it (and landed on automagic help page again :D ), is the == equal in the expression.
So to test if x = 1, in the expression i must type it as : x == 1, not x = 1 !
Single equal sign = is to assign value to variable, while double equal sign == is to check if the left part is the same as the right part.
Fortunately there is debug dialog which really help a lot in finding why the script didn't perform as I want.

Don't give up yet, everything is difficult before it becomes easy.
Feel free to drop at the user help subforum, and state clearly what you want to do with the script/expression (or the flow).
I will try to help as far as I can. ;)
Many thanks for your assistance always.
I can learn a lot from your explanation especially the difference between = and == which I was also confused.
I'm wondering if there is some web link that can help me to learn more on those syntax.

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

Re: Navigation Apps Auto turn on/off GPS

Post by Desmanto » 29 Aug 2017 15:00

tphg wrote: Many thanks for your assistance always.
I can learn a lot from your explanation especially the difference between = and == which I was also confused.
I'm wondering if there is some web link that can help me to learn more on those syntax.
You are welcome. Let's learn together then. I believe if I am able to share something and other people can easily understand what I share; that means I also have understand the topic properly.

That = vs == problem is somewhat common in several programming languages. Learning any programming language should help a lot in understanding the use of the syntax.
I learn a lot of programming languages, but only to basic level so that I can create a very simple script for my particular needs at the moment I learn it. For most of them, I just glance thru the quick tutorial/introduction, then try to create the script. When I hit the wall, just google until I found the solution.

Not the best way to learn it, so sometimes if I think I might use that again in the future, I will sit down and really learn it from 0. Some of those are
1. python, to integrate automagic with eventghost in windows. And also to create some automation at windows
2. xml and Xpath, to parse data from xml, also useful in automagic
3. json, and some javascript, parse object from json, automagic map somehow closely related to this
4. regex, not a programming language, but very useful for parsing data (as the one I use in this flow)
5. java, to call java function, or more advanced function from android.

Automagic scripting language is inspired a lot by java/javascript viewtopic.php?f=6&t=1735
It is a totally different set of language, only used proprietary in Automagic. So, you won't find any tutorial out there. The only way to learn it is directly test in Automagic.

When I migrate from tasker, I am afraid about the variable handling, because need to relearn the scripting. Tasker use separate action to parse each variable. Turns out, Automagic scripting is easier to use and more flexible. Even though confusing at first (to adapt with the style), but within a week, I already can recreate most variable handling in tasker using automagic script, with more flexible and faster execution (I have tested the speed). It is easier to the degree I can do something in automagic script, but don't know how to do the same thing in python :D

Ff you learn other language, it will still help a lot. I suggest javascript and python, which are easy to start with. Java is more difficult IMHO. Tutorialspoint wording is quite easy to understand. They have a bunch of tutorials.
https://www.tutorialspoint.com/javascript/index.htm
https://www.tutorialspoint.com/python/
No need to understand everything, just get the idea how something get done via the examples. The rest, when you encounter problem, just google it. Sometimes i don't know how to do something in automagic, but I search the solution in python or javascript. :D Then I find the similiar function that behaves the same in automagic. The logic for solving is the same, it is just the syntax/functions used are different. Just like tutorial for doing something, but presented in different language/subtitle.
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.

tphg
Posts: 57
Joined: 17 Apr 2017 05:31

Re: Navigation Apps Auto turn on/off GPS

Post by tphg » 30 Aug 2017 00:35

Desmanto wrote:You are welcome. Let's learn together then. I believe if I am able to share something and other people can easily understand what I share; that means I also have understand the topic properly.

That = vs == problem is somewhat common in several programming languages. Learning any programming language should help a lot in understanding the use of the syntax.
I learn a lot of programming languages, but only to basic level so that I can create a very simple script for my particular needs at the moment I learn it. For most of them, I just glance thru the quick tutorial/introduction, then try to create the script. When I hit the wall, just google until I found the solution.

Not the best way to learn it, so sometimes if I think I might use that again in the future, I will sit down and really learn it from 0. Some of those are
1. python, to integrate automagic with eventghost in windows. And also to create some automation at windows
2. xml and Xpath, to parse data from xml, also useful in automagic
3. json, and some javascript, parse object from json, automagic map somehow closely related to this
4. regex, not a programming language, but very useful for parsing data (as the one I use in this flow)
5. java, to call java function, or more advanced function from android.

Automagic scripting language is inspired a lot by java/javascript viewtopic.php?f=6&t=1735
It is a totally different set of language, only used proprietary in Automagic. So, you won't find any tutorial out there. The only way to learn it is directly test in Automagic.

When I migrate from tasker, I am afraid about the variable handling, because need to relearn the scripting. Tasker use separate action to parse each variable. Turns out, Automagic scripting is easier to use and more flexible. Even though confusing at first (to adapt with the style), but within a week, I already can recreate most variable handling in tasker using automagic script, with more flexible and faster execution (I have tested the speed). It is easier to the degree I can do something in automagic script, but don't know how to do the same thing in python :D

Ff you learn other language, it will still help a lot. I suggest javascript and python, which are easy to start with. Java is more difficult IMHO. Tutorialspoint wording is quite easy to understand. They have a bunch of tutorials.
https://www.tutorialspoint.com/javascript/index.htm
https://www.tutorialspoint.com/python/
No need to understand everything, just get the idea how something get done via the examples. The rest, when you encounter problem, just google it. Sometimes i don't know how to do something in automagic, but I search the solution in python or javascript. :D Then I find the similiar function that behaves the same in automagic. The logic for solving is the same, it is just the syntax/functions used are different. Just like tutorial for doing something, but presented in different language/subtitle.
Now I understand expression in Automagic is actually for advance users who have basic knowledge on programming, rather than for general users like me.
In fact I am really interested in programming but have no time to learn.
Will invite you going for a beer if one day you have opportunity coming to my place.

anuraag
Posts: 371
Joined: 24 Jan 2015 02:06

Re: Navigation Apps Auto turn on/off GPS

Post by anuraag » 30 Aug 2017 00:46

Non root users can use builtin secure setting of automagic to toogle gps.

tphg
Posts: 57
Joined: 17 Apr 2017 05:31

Re: Navigation Apps Auto turn on/off GPS

Post by tphg » 30 Aug 2017 09:40

anuraag wrote:Non root users can use builtin secure setting of automagic to toogle gps.
Sounds interesting for non-root users. I have LG G4 with Marshmallow that there is no way to root it so far.
Please could you give more detail instruction on how to do it.
Thanks in advance.

Post Reply