Stay Awake Timer (Toggling State Timer)

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

Stay Awake Timer (Toggling State Timer)

Post by Desmanto » 20 Jan 2019 18:14

Prologue
I have this stay awake timer concept for a long time. I have created this in tasker and have used it until now after switching to Automagic. My daily activity require me to download big files. And we have very slow connection at that time (now much better). So sometimes it can takes several hours to download a 500-1000 MB file. Now, with better speed, the downloaded files also grow bigger, so same several hours.

With android, sometimes when your screen is off, the system assassinate the downloader app in the background. Although now is not so aggressive anymore if we exclude the app; but occassionally, the download will tends to fail at certain point if the screen is off. So I want to make sure the screen is not off during the download.

I can make the screen timeout to never, but then I will forget to change it back at other condition, draining my battery out. It will be nice to change the timeout to certain minutes/hours from now and turn it back to normal timeout. So even if we forget it, the flow will automagically set the timeout back.

The similar concept can be expanded for many other things that have the toggling state. Example you want to turn airplane mode on for 4 hours and turn it off afterward. Or make the ringer silent for 2 hours and normal back afterward. Just change the action. So this flow can be called also as : Toggling State Timer

Stay Awake Timer
Stay Awake Timer.png
Stay Awake Timer.png (102.31 KiB) Viewed 12738 times
Triggers
The flow use 2 triggers. One is shortcut, which you will add to the home screen later. And the other one is global Variable date/time (glovar DT), which is the revert flow.

Flow Logic
The flow start with one of the 2 triggers. It has been protected by the expression, so the manual execution won't work (3 dot menu > execute). You have to add the shortcut to the home screen first and execute the flow from that shortcut.

First expression will split the trigger. If the trigger is shortcut, it will continues to show the input dialog to popup the choices of the time. If you don't choose anything at the input dialog (or cancel/back it), the expression will stop the flow until that point.

But if you choose something, it will be checked against the choice. If it is one of the valid time choice, then the flow will continue to set glovar to choosen time + current time. Then it will set the screen timeout to never, put a notif when it will be reverted back.

If the choice is the last choice, which is the revert value; then it will check if the glovardt is exists, it will set the screen timeout back to 5 minutes. Then delete the glovar. This means pressing the last choice will revert any running glovar DT and reset it back to nothing. (revert the timeout immediately). Then it produce a silent Notification on statusbar to notify the revert. If glovar DT is not exists yet, then it means no active toggling happen, do nothing.

If the glovardt has been set, after the glovardt is reached, the trigger will execute the flow and branch to the false branch. It will then remove the glovardt, then revert back the timeout and set the same notification on statusbar.

Initialization
This flow use glovar, but you don't have to set it at first run. It also only use up your glovar space when it is running, but not when there is no toggling active. The glovar DT will be set by the variable you put in the glovardt. So at first, you have to modify the Triggers shortcut name to the action you want. Then Modify the trigger Global Variable Date/Time to the glovar you want. At the first expression, edit the {glovardt} to be the glovar name you use in the trigger and {shortcut} to the name of the Trigger shortcut you use.

The time option you can choose, can be added/removed in the script after the expression. I put it into the {timemap} map object. Put the key as the choice, and the value as the minutes. So you can add something like "6 hours", 360 (minutes). I am sure you don't need so many choices, but you can practically add as many as you need. Put the last value for the cancellation of current set glovardt. The last choice value is never used in the script after this, but I just put 5 at there to denotes my default screen timeout.

Testing
Add the trigger shortcut to the home screen. Enable the flow and you can run the flow from the shortcut.
- Tap the shortcut and you will be greeted with the input dialog. Choose the time, you will see a notif that the timeout has been set to that value and when it will revert back. Check you glovar and you can see the new glovar has been created. Your screen timeout also become never.
- Tap the shortcut again and now choose the last choice (to reset the glovar). You will see the notification on statusbar that the timeout has been reverted. Check the glovar, it has been deleted, and timeout back to 5 minutes.
- Tap the shortcut (when you have no active glovard), choose last choice again or press back. Nothing happen. Since the glovardt is not there. (no active toggling)
- Tap the shortcut and choose one of the time, example 30 minutes. Wait until the timeout reached (do something else while waiting). Check back after 30 minutes, you will see the silent notif on statusbar to notify that the screen timeout has been reverted.

Modification
In case you want this flow for something else, you are free to copy this flow and give it other name. Example : "Silent Timer". Change the shortcut name to Silent Timer too, the glovardt to something else (as long as it doesn't conflict with your existing glovar), example : global_silent_reset. Then change the {glovardt} and {shortcut} to match the glovar you use and the new shortcut name.

Then change the 4 bottom element of the flow. 2 on the left are to toggle state, 2 on the right are to toggle the state back to default.

Set Screen Timeout : Never >> change to Set Ringer Mode: Silent
Notification on Screen >> change to the appropriate wording, example : Silent for {value}
=================================
Set Screen Timeout : 5 Minutes >> Change to Set Ringer Mode: Normal
Notification on Statusbar >> changet to appropriate wording, example : Ringer mode changed back to Normal


Why glovardt, not sleep? Why toast + notif?
Why use glovardt, not sleep? Because using action sleep with keep awake ticked for a long period, will create wakelock to the CPU. viewtopic.php?f=5&t=7251
Your phone won't enter the deep sleep/doze mode, causing huge idle battery drain compared to usual. So it is not recommended to use Action sleep for a sleep time more than 5 minutes.

Unticking the keep awake will cause the sleep end in non-exact time, since it will be determined by the maintenance windows of the doze mode to wake the device to check whether the sleep has passed its time. Ticking Wake on doze also still can delay up to 15 minutes, depends on the maintenance windows interval. But using glovardt will produce exact timeout, and cause no battery drain/wakelock at all (it works just like alarm). Hence, I think that Trigger Global Variable Date/Time is one of the best feature in Automagic, in terms of time related action.

Why use toast message on toggling and notif on statusbar when reverting?
We use notification on screen when toggling, since we do it manually, so we are most likely staring at the screen. We won't miss the toast message. But when reverting, it can happen when we are not looking at it. So we need some non intrusive notification, but can stayed even if we missed it at first; hence notification on statusbar is the best choice. Using notification on screen for reverting will most likely missed out at most cases.

More Advance option
I have another flow with much more complex concept. It has the option to set the custom timeout, and you can set the timeout to some time in the future. But i think it will overkill and too complex to put into this flow. This is supposed to be the toggling state of several minutes to hours, not until days. This flow also can be upgraded to support multiple action in single shortcut. But again, that will be covered in my other flow. And I use it not for a simple toggling, but to enable/disable flows for certain period. It even support multiple schedule timeout, which I will share in other time.

Epilogue
I hope this will help you to understand why I prefer to use glovardt rather than a long period of "sleep". This flow also serve as the complete concept of how to utilize the glovardt.
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.

Post Reply