Inconsistent outcomes with script

Post your questions and help other users.

Moderator: Martin

Post Reply
Econdoc
Posts: 153
Joined: 28 May 2016 20:06

Inconsistent outcomes with script

Post by Econdoc » 28 May 2019 18:36

I am not getting consistent results doing a fairly simple task: turning NFC on or off.

I am using Oxygen 9.0.13 (Android Pie).

My flow consists of two actions: Start Activity and ControlUI.


Start Activity
Action (android.intent.category.VIEW)
Category List (android.intent.category.DEFAULT)
Package Name (com.android.settings)
Class Name (com.andriod.settings.Settings$Advance
Flag List
FLAG_ACTIVITY_NEW_TASK
FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS


The second Action is ControlUI. Here is the script to Turn NFC ON:
sle=1000;
sleep(sle);

text = getText(984, 572);
sleep(sle);
control_is_off=text=="OFF";
if (control_is_off)
click("OFF");
sleep(sle); back(); back();

The irritating thing about my flow is that works sometimes and other times not. It just is not consistent. Anyone have a suggestion or two? Thanks.

Pepy
Posts: 76
Joined: 31 Oct 2018 10:53
Location: Canada

Re: Inconsistent outcomes with script

Post by Pepy » 30 May 2019 21:39

The class name is spelled wrong but that's probably just a typo from when you wrote the post. The correct one for me is: com.android.settings.Settings$AdvancedConnectedDeviceActivity

Your script worked fine for me when I inputted my co-ordinates, but as a general rule, I try not to use co-ordinates as it varies depending on device and perhaps DPI setting as well. Maybe you can give the script below a try after double-checking the co-ordinates:

Code: Select all

sleep(500);
if (getText(x, y) == "OFF")
{
  click("NFC");
}
sleep(500);
back();
back();
You can also just use the NFC Enabled condition, and if it's equal to false, use the same Start Activity and Control UI script but without the if statement.
Last edited by Pepy on 31 May 2019 01:45, edited 1 time in total.
Hope my post was helpful :D
Device: OnePlus 5T running crDroid, rooted with Magisk 8-)

Econdoc
Posts: 153
Joined: 28 May 2016 20:06

Re: Inconsistent outcomes with script

Post by Econdoc » 31 May 2019 00:27

Thanks for the response and the much cleaner code.
Yes, I truncated the class name. Sorry about that. That was all I could see on the screen.

I have found two other ways to accomplish the NFC task, both of which seem to work better for me.
1. Instead of using Start Activity, I used Launch App. The same Package Name and Class name were used; it seems to work better than Start Activity. I don't understand why, but that is what I experienced.
2. Using quickSettings() for NFC seemed to work reasonably well. Now that the Info, Gesture, and Close buttons are drag-able on the Overlay control of Control UI, that seemed to work pretty well too.

Your idea of using the NFC Enabled condition could easily be used with either of the above to simply matters. Just a click on the NFC tile in quickSettings() can toggle NFC the way I need.

thanks again.

Pepy
Posts: 76
Joined: 31 Oct 2018 10:53
Location: Canada

Re: Inconsistent outcomes with script

Post by Pepy » 31 May 2019 02:33

Start Activity just seems like a more advanced version of Launch App, so I'm not sure why it doesn't work as well.
Following up with my idea, if you're going to be using Launch App, this flow should be enough to get the job done: http://automagic4android.com/flow.php?i ... 38fd2b0d83

I personally would prefer using the first method since I won't need to worry about the flow breaking if I ever remove or movd the tile to another page.
Last edited by Pepy on 01 Jun 2019 23:21, edited 1 time in total.
Hope my post was helpful :D
Device: OnePlus 5T running crDroid, rooted with Magisk 8-)

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

Re: Inconsistent outcomes with script

Post by Desmanto » 01 Jun 2019 18:49

Need to debug what is the error per execution. Put debug dialog after the control UI. You can see the value of the {text} and {control_is_off}. Most likely it is null or some other data. Control UI silent error usually caused by insufficient sleep. Most of the time, it is the first sleep from previous action. So the start activity might have signaled Automagic to continue to next action, Control UI. But the UI of the setting hasn't fully loaded yet, causing null data in {text} when retrieved, inconsistent outcome. Sometimes it loaded up fast, hence the action is successful.

As Pepy pointed out, just use Launch App for most cases, usually it loads up faster. Use Start Activity only when the activity require extra fields, or need some form of data field or special flags ("exclude from recents" might be needed for password/security related task).
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