Multi Press

Share and discuss your flows and ideas with other users.

Moderators: MURTUMA, Martin

User avatar
Posts: 1606
Joined: 21 Jul 2017 18:50

Multi Press

Postby Desmanto » 03 Jan 2019 18:33

Multi Press

Download here : Multi Press.xml
Multi Press.png
Multi Press.png (150.57 KiB) Viewed 484 times

This flow concept is used to execute certain action based on how many trigger it receives consecutively before timeout. The example flow takes on trigger Hardware Key Event, on VOLUME_DOWN, key press down and block the event from being sent (System won't lower sound volume). We use 4 states (4 press) in this design. If pressed once, and waited for 1 second, Notification on Screen : State 1, will pop up. Press twice before 1 second timeout in between, Notif : State 2. Press thrice : State 3 and so on until State 4. If pressed more than 4 times, then it will redirected to another action, in this case is Vibrate. This is used to prevent accidental press more than 4. So 5, 6, 7, .... press will do some useless warning action to warn that it is outside of the designated press

The concept of this flow is similar to what Mi keys do.

Flow Execution
The flow use FEP : Stop, which will stop the existing running execution. (visit my FEP thread : viewtopic.php?f=3&t=6985) It is used to stop the previous press action, so it will reregister with the next trigger. So action from press 1 will be cancelled by press 2, action from press 2 cancelled by press 3 and so on.

How the flow works
Before execution, glovar is 0. At the first execution, it will add glovar by 1. It will wait for 1 second. Then it will check current glovar value. Since it is first execution, glovar is 1, it will go to branch 1. If there is no other trigger during this period, notification on screen : State 1 will be shown. The glovar is resetted to 0, starting all over again. If there is execution during that 1 second wait, second press trigger will stop the first press execution. It will increment glovar by 1 again, so now it is 2. The flow will wait 1 second before go to the branch 2. Repeating the same, until glovar is outside of the designated index. If the key is press more than 4, then anything further will fall into the last expression, which is bigger than 4. It will do the last warning action.

You will need one glovar for this flow, global_multipress. Create this glovar manually in the global variables menu and set the value to 0 (Number). Without this, the flow will fail without warning, since it will add null. I won't create the error checking part, since the glovar is a one-time creation, making the error checking part redundant.

If you need several flows like this one, you can save some glovar space by merging the similar flows into single glovar map. Create the glovar in map type.
global_multipress, map type. Add new key and add the identifier name, set the key value to 0. Example
global_multipress :
volumedown : 0
volumeup : 0
proximity : 0
Later, instead of using global_multipress, use the key from the map
Code: Select all
global_multipress["volumedown"] = global_multipress["volumedown"] + 1;

To test the flow, after creating the glovar, simply enable the flow. (You need to enable accessibility services for Automagic for this to work, since it use Key Event.) Press volume down button at your phone once, wait for a second, you should see toast message : State 1. Repeat the same to see other notif.

This flow usually will be coupled with triggers that can execute quickly and consecutively. Some of these triggers are sensors based, which can drain battery just by being enabled. So most likely you need to use another flow to enable/disable this flow for use only during certain period/condition. Example, only when bluetooth is connected or charging in car.

Change the trigger to any other trigger you need. You can still use Hardware key event, but use other key instead. For example, if you have bluetooth headset or remote shutter, you can map the play/pause button or the camera button to multiple function using the same press. You can use any other quick and sucessive trigger, such as
proximity/light/compass sensor : wave 1 to play music, wave twice to pause, wave thrice to next, etc.
Http request trigger : coupled with other IoT device which can send http post, can be use to map a single button to multiple function
Device orientation : flip the device to certain face for several times to perfom certain special action
Fingerprint gesture (for device that support it) : Swipe certain times to perform certain action
NFC tag : put once for Normal profile, put twice for vibrate.
Shake : Shake once to vibrate, shake twice to silent.

Change the glovar name to your need. You might want to use glovar map as pointed above.
Change the action in each branch as needed. You can set for example,
press 1 = play/pause
press 2 = next track
press 3 = previous track
press 4 = stop music,
press 5 or more = play certain warning sound and vibrate.

You can change the behaviour of last branch 4 to always execute the branch 4. Meaning press 4 or more will always result in "Notif State 4". Simply connect the expression 5 to the action in branch 4. Or you can simply remove the 5 branch, and modify the branch 4 expression to
Code: Select all
global_multipress >= 4

If you need more/less than 4 action, add/remove the branch accordingly, and modify the last branch. So if you need only 2, remove branch 3 and 4, modify the last expression to > 2. If you need 6, add another 2 branches, and modify the last expression to > 6.

If you need varying pressing time, you can modify the sleep time in each execution, by using another list to store the timing. Branch 1 set to 750 ms, branch 2 500 ms, branch 3 2000 ms, branch 4 1000 ms, etc...., such as
Code: Select all
multisleep = newList(750, 500, 2000, 1000);

So the first script now become
Code: Select all
global_multipress = global_multipress + 1;
multisleep = newList(750, 500, 2000, 1000);
len = length(multisleep);
sle = if(global_multipress > len) multisleep[len-1] else multisleep[global_multipress - 1];

But it is too hassle to do it and I think it is just overkill for general purpose. In most cases, you probably never need more than 3 press. Longer press require longer time and more exhausting to press until the desired branch. If you need multiple choices and you have direct access to the phone (touch screen input), better use Input Dialog - Single Choice Menu. This flow is used to coupled with other device/means where you don't have direct to the phone or need special background task running using special method. (secret action).

This flow actually can be created without using glovar, but use multiple condition execution count. However, to add or remove additional branch will be much more difficult and confusing, since we need to rearrange half of the branch. You also have to give each execution count a unique name, and the higher the press needed, the more element execution it need (adding up unnecessary latency).
Index of Automagic useful thread List of my other useful posts (and others')

Posts: 57
Joined: 17 Apr 2017 06:31

Re: Multi Press

Postby tphg » 04 Jan 2019 01:12

Thanks for making this forums alive with a useful flow, @Desmanto.

Return to Flow Sharing Area

Who is online

Users browsing this forum: No registered users and 8 guests