How to save/restore state?

Post your questions and help other users.

Moderator: Martin

Post Reply
derkork
Posts: 2
Joined: 09 Mar 2020 21:04

How to save/restore state?

Post by derkork » 10 Mar 2020 14:56

I'm coming from Tasker and currently try to switch over to Automagic. In Tasker when you change stuff inside of a profile, by default when the profile is deactivated the state is restored automatically to the state it had when activating a profile.

For example I want to automatically activate do not disturb mode (DND) when a specific app is opened and I want to set it back to the state it was once the app is deactivated or closed. So if my phone was already in DND mode when I activate the app, it will stay in DND mode when I deactivate the app. But if my phone was not in DND mode before i activated the APP, it goes into DND mode when I activate the app and once i close the APP it goes back to normal mode. In Tasker this state restoration is automatically built into the tasks.

Since there are no profiles in Automagic, I figured I would need two flows for this, one to be triggered when the app is activated which activates DND mode and a second one to be triggered when the app is deactivated which would restore the sound profile to how it was. However I don't know how I would restore the correct setting of the DND state in the second flow. I know that I can set variables but I didn't find any action to actually read the current sound profile so I could store it into some variable and later restore it from there.

Saving and restoring state seems to be a common thing with automation so there is probably a nice and easy way of doing this with Automagic, but it would seem I have not yet found it. Would someone please enlighten me about how to do this properly with Automagic?

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

Re: How to save/restore state?

Post by anuraag » 11 Mar 2020 10:21

This can be done in one flow.
Use 2 triggers. 1) App task started. 2) App task ended.
Set condition expression contains(trigger, "App Task Started")
If condition true then Set Interruptions Mode All
If condition false then Set Interruptions Mode None

For storing/restoring sound profile there are two actions present Store Audio Volumes and Restore Audio Volumes.

As a newbie to Automagic please read some topics here. It will give you good knowledge.
viewtopic.php?f=6&t=6993

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

Re: How to save/restore state?

Post by Desmanto » 11 Mar 2020 10:54

Hahaha, I am too detail and too long typing this, already ninja'd by anuraag. :D Most of my answer are similar to his, but I am too lazy to change the wording again.

I am ex tasker user too, but it has been long time I don't touch it. While knowledge in tasker can help creating flow in general, but some of the basic concept might become obstacles. You have to give up some of what you know in tasker and adopt Automagic's concept. Start with clean slate, just like you've never use any automation app before (I know it is difficult). Read my index at the Automagic vs Tasker and some from the General Concept section to understand better. (link in my signature or anuraag post above)

In short :
Tasker Profile - Event = Automagic's Trigger
Tasker Profile - State = Automagic's Condition
Condition can't be the starting point of the flow. Because Automagic's flow is event based trigger. It knows App is started from the trigger App Task Started. It knows the app is closed from Trigger App Task Ended. There is Condition App Task Running to check if app is running, but this is condition version; can't trigger flow.

AFAIR, the restoring state is using "exit task" right? In Automagic, it is not exit task, but use trigger with the opposite version of the entry one. So you are correct already. To create an exit task for Trigger App Task Started, you need another trigger App Task Ended with the same app. You can combine both trigger in single flow by using expression to split the branch.

To check current DND mode, use Condition Interruption Mode. To check ringing profile, use Condition Ringer Mode. Most of the current system state can be checked using condition (that's why condition in Automagic is somewhat equivalent to State in Tasker). Some might need to use action Init version (example Init Variable system setting), then checked using expression.

Detail step
Let's get thru the step by step to build flow and add more stuff to it. (I purposely type this in detail so maybe other can benefit from this too)
1. Single trigger + single action
The most basic flow for this is to just to switch DND on when opening the app
Trigger 1 : App Task Started, choose the app (I use Chrome as example)
Action 1 : Set Interruptions Mode - Priority (Enable DND)
1. Single trigger.png
1. Single trigger.png (12.41 KiB) Viewed 12037 times
You can create the similar flow, but with trigger App Task Ended, and Action Set Interruptions Mode - All (Disable DND). But this will result in 2 flows. We want to be efficient, so we combine both in the next step.

2. Combine entry and exit flow
We combine both flows, the started and ended triggers into single one. Then we split it using expression. True enable DND, false, disable DND.
Trigger 1 : App Task Started
Trigger 2 : App Task Ended
Condition : Expression | trigger == "App Task Started: com.android.chrome"
True - Action : Set Interruptions Mode - Priority (Enable DND)
False - Action : Set Interruptions Mode - All (Disable DND)
2. Combined flow.png
2. Combined flow.png (27.73 KiB) Viewed 12037 times
(you can use anuraag version above, so it is not limited to Chrome only)
Replace the expression with the trigger you got after you select the app. The flow will detect Chrome opened and enable DND. After the Chrome is closed, it will disable DND. At this point, the flows already works similar to Tasker profile with exit task.

3. Add additional DND check
From step 2, we add additional check to detect if DND is already enable prior opening the app. If DND is already enabled, we store the value in a global variable (glovar) : global_dnd = true. If not, then enable DND as usual. When exiting app, we check if the glovar has been set to true. If true, means prior DND is enabled, we leave it enabled (do no changes to the DND) and set the global_dnd = false, (for next execution). If false, means prior DND is disabled, we disable DND as usual.

First, create the global_dnd, type boolean and set it to false (or just simply execute the script global_dnd = false)
Trigger 1 : App Task Started
Trigger 2 : App Task Ended
Condition : Expression | trigger == "App Task Started: com.android.chrome"
__True - Condition : Interruptions Mode - Priority (DND enabled?)
____True - Action : Script | global_dnd = true
____False - Action : Set Interruptions Mode - Priority (Enable DND)
__False - Condition : Expression | global_dnd
____True - Action : Script | global_dnd = false
____False - Action : Set Interruptions Mode - All (Disable DND)
3. Additional DND check.png
3. Additional DND check.png (43.59 KiB) Viewed 12037 times
So what this flow will do
- Chrome opened with prior DND disabled >> DND enabled
- Chrome opened with prior DND enabled >> DND stay enabled
- Chrome closed with prior DND disabled >> DND disabled
- Chrome closed with prior DND enabled >> DND stay enabled

It is still possible to optimize this flow to be more compact, but I don't want to confuse you. Better stick with easy to understand first.
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.

derkork
Posts: 2
Joined: 09 Mar 2020 21:04

Re: How to save/restore state?

Post by derkork » 11 Mar 2020 12:15

This is fantastic thank you very much for all your help. I'll go over it and see if I can make it work here :)

Post Reply