Set any state on/off

Post your questions and help other users.

Moderator: Martin

Post Reply
User avatar
digitalstone
Posts: 342
Joined: 21 Oct 2017 12:36
Location: The Netherlands

Set any state on/off

Post by digitalstone » 25 Aug 2018 14:14

Let's say you need to turn on WiFi for example:

But in some situations WiFi is already on.
Is it in those cases better to place a condition in front of the WiFi-set action, or is this checking already done internally by Automagic?
...
Or doesn't this matter altogether?
Because, perhaps, Android itself only responds to those instructions when they are different than current value.
Phone: LG Nexus 5X (rooted vanilla Android 7.1.2)

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

Re: Set any state on/off

Post by Desmanto » 25 Aug 2018 18:32

I got the same question too. Usually i will just add a condition to check if the feature I want has been turned on or not yet, in this case, condition wifi state.

But looking from the logcat, if the wifi is on, it seems android simply ignore the request. This is the logcat if my wifi is on and I turn it on again using action Set wifi state. It query 4 lines and the wifi stay on, no other events triggered.

Code: Select all

I AM_fz   : [App tester] Action 'Set WiFi State: On' enable = true                                                                                                                               
D WifiService: setWifiEnabled: true pid=2730, uid=10196, package=ch.gridvision.ppam.androidautomagic
I WifiService: setWifiEnabled package=ch.gridvision.ppam.androidautomagic uid=10196 enable=true     
I WifiService: acquireWifiLock uid=10196 lockMode=1                                                 
I WifiService: releaseWifiLock uid=10196                                                            
I AM_e    : [App tester] End executing action 'Set WiFi State: On'                                  

If the wifi is off, the logcat is much longer, because it includes much more events that happen when the wifi is on. (a lot of broadcast)

How ever this is the action done when you check the state, much shorter. It only query one line of code to the system, instead of 4 lines when we just use the set wifi state without checking

Code: Select all

2730 I AM_e    : [App tester] Start executing condition 'WiFi State: Enabled'                     
4042 I WifiService: getWifiEnabledState uid=10196                                                 
2730 I AM_e    : [App tester] End executing condition 'WiFi State: Enabled' with return value true
So yeah, I would say it is better add condition to check the state first. It require less CPU cycle (assuming each line consume one cycle) if the wifi is on already. If the wifi is still off, we only add one cpu cycle to query the state anyway.

However this my conclusion from the test above. Under the hood, probably it can be different. Let's wait for Martin confirmation about this.
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.

User avatar
digitalstone
Posts: 342
Joined: 21 Oct 2017 12:36
Location: The Netherlands

Re: Set any state on/off

Post by digitalstone » 25 Aug 2018 20:54

Thanks for checking on that.
Looks indeed that it doesn't technically matter, but it would still be more efficient cpu-wise when avoiding switching something to 'on-state' when already on.

And you're right, there could be more to it than the logcat output.
We'll wait for Martin to reply.
Phone: LG Nexus 5X (rooted vanilla Android 7.1.2)

Post Reply