Flow order of execution

General discussions about Automagic and automation in general

Moderator: Martin

Post Reply
Ghlave
Posts: 28
Joined: 06 Aug 2013 18:06

Flow order of execution

Post by Ghlave » 06 Jul 2014 22:52

Do flows have an order of execution? I have a flow that detects when the music playing state has changed, and sets a variable to true if it is. When headphones are plugged in, a flow evaluates the variable to see if it was true, and resumes music if so.

The problem I'm having is that if I unplug the headphones, the music stops via the android os, but my flow that detects the play state sometimes lags, and then doesn't set the variable properly because it thinks the music wasn't playing. I'm wondering if my other flows activating are causing my ToggleMusicmode flow to execute last, thus it no longer recognizes that music was playing because the android os stopped it before the flow could check.


exported_data_20140706_174843.xml
(5.62 KiB) Downloaded 905 times

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

Re: Flow order of execution

Post by Martin » 07 Jul 2014 09:19

Hi,

Flows are executed as soon as the trigger of the flow detects an event by the system, multiple flows can be executed in parallel so flows in general don't have a specified order of execution and no priority.
You can delay the execution of one flow by adding an action Sleep: 5s when flows are executed almost at the same time but one flow should be executed later.

Regards,
Martin

Ghlave
Posts: 28
Joined: 06 Aug 2013 18:06

Re: Flow order of execution

Post by Ghlave » 07 Jul 2014 15:39

I don't think I could do that in the context of what I'm doing. Would there be a way I could have something that checks the constant state of music playing while headphones are plugged in and stores that result in a variable? All my efforts so far to do this have resulted in the android os stopping the music from playing, and then flow execution seems to be slightly lagged behind that so the music playing check results in a false. I have tried using this with a headphone disconnect trigger as well.

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

Re: Flow order of execution

Post by Martin » 07 Jul 2014 19:56

Maybe I did not yet understand the initial problem completely. When you play music on your device and then unplug the headphones, the music stops playing and you want to resume playing music. Is this correct?
On my device the wired headset unplugged event is sent by Android about 1 second after the headset has been actually unplugged. The media apps get informed about 1 second earlier that the audio will be played using the speakers of the device.

Following flows should work for this situation:

Flow 1:
-trigger Media Utilities State is Not Playing becomes true
-action Sleep: 2s
-condition Expression: global_just_unplugged
--> true: action Plugin: Resume music
-action Script: global_just_unplugged = false

Flow 2:
-trigger Wired Headset Unplugged
-action Script: global_just_unplugged = true

You could also use trigger General Broadcast with action android.media.AUDIO_BECOMING_NOISY to react to the same event that the audio players use to stop playing back audio which is received way earlier than the headset trigger. You could use this to reduce the sleep duration to ~500ms in flow 1.

Ghlave
Posts: 28
Joined: 06 Aug 2013 18:06

Re: Flow order of execution

Post by Ghlave » 08 Jul 2014 03:52

That is somewhat what I'm attempting, but let me clarify it. When I'm playing music on my device and the headphones become unplugged, the music stops playing. When I plug my headphones in, I'd like it to begin playing again, but only if it was playing before. Basically a resume playing when headphones become connected, but only if music was playing at the time they were disconnected.

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

Re: Flow order of execution

Post by Martin » 08 Jul 2014 07:49

Ahh, OK. Then you could use a modified version of the first flow to set a flag that music was playing just before the headphone was unplugged and restart playing with a third flow when the headset is plugged again:

Flow 1:
-trigger Media Utilities State is Not Playing becomes true
-action Script: global_music_playing_when_unplugged = false
-action Sleep: 2s
-condition Expression: global_just_unplugged
--> true: -action Script: global_music_playing_when_unplugged = true
-action Script: global_just_unplugged = false

Flow 2:
-trigger Wired Headset Unplugged
-action Script: global_just_unplugged = true

Flow 3:
-trigger Wired Headset Plugged
-condition Expression: global_music_playing_when_unplugged
--> true: -action Plugin: Resume music

Ghlave
Posts: 28
Joined: 06 Aug 2013 18:06

Re: Flow order of execution

Post by Ghlave » 09 Jul 2014 03:43

Well, so far your logic seems rock solid. I haven't had it goof up yet. Thanks for lending some clarity!

Post Reply