Java Functions / Battery Manager

Skip to first unread message

John W.

unread,
Jun 2, 2019, 9:05:27 AM6/2/19
to Tasker
https://developer.android.com/training/monitoring-device-state/battery-monitoring#java

I have tried valiantly to adapt this to Tasker, but I just can't get the Tasker code right. Can anyone *please* help me make sense of it?

Yes, I realize that Tasker has a means of detecting these state attributes, but only as a profile context, but there are a few reasons that make creating a separate profile undesirable. I also realize that Notification Listener followed by a Regex can also indirectly yield the charging source, but that is suboptimal and less efficient (and in general I really try to avoid plugins as much as possible)

P's and Q's in advance.

Robert Ryan

unread,
Jun 2, 2019, 11:47:45 AM6/2/19
to tas...@googlegroups.com
I am not sure what you are trying to achieve, but you can get constant updates about battery status by monitoring the android.intent.action.BATTERY_CHANGED broadcast.

This gives you access to all of the variables in the attached document. Just save the ones you need to global variables. 

        Event: Intent Received 
        Action: android.intent.action.BATTERY_CHANGED 
        Cat: Default 
        Cat: Default 
Battery Changed.txt

John W.

unread,
Jun 3, 2019, 12:25:38 AM6/3/19
to Tasker
Thanks for your willingness to assist, but I rather clearly stated that creating a separate profile was undesirable.

As for what I am trying to accomplish (and I'm not altogether sure why it would be relevant), the aim is to interrogate Android to ascertain which charging method is active so that various performance parameters can be adjusted accordingly (i.e. screen timeout, screen brightness, etc, etc). For Tasker's "restore settings" to work optimally, the charging method needs to be evaluated within the same profile with the charging state context. It would be ridiculously inelegant to create four different profiles for Fast AC, AC, USB, and wireless.

Robert Ryan

unread,
Jun 3, 2019, 1:38:33 AM6/3/19
to tas...@googlegroups.com
This works for me:

        Test
        
    A1: Java Function 
        Return: ifilter 
        Class Or Object: IntentFilter 
        Function: new
        {IntentFilter} (String) 
        Param: "android.intent.action.BATTERY_CHANGED" 
        
    A2: Java Function 
        Return: battery_status 
        Class Or Object: CONTEXT 
        Function: registerReceiver
        {Intent} (BroadcastReceiver, IntentFilter) 
        Param: ifilter 
        
    A3: Java Function 
        Return: %charge_plug 
        Class Or Object: battery_status 
        Function: getIntExtra
        {int} (String, int) 
        Param: "plugged" 
        Param: -1 
        
        <0 = none, 1 = AC, 2 = USB, 4 = wireless>
    A4: Flash 
        Text: %charge_plug 
        Long: Off 

John W.

unread,
Jun 3, 2019, 11:14:53 AM6/3/19
to Tasker
Brilliantly sleuthed, my friend. It took me several attempts entering the lines to get it to work, but work it did!!! FWIW, I've been using Tasker since April 2012 and I pride myself in übertight (occasionally minified) algorithms (whether that's in Tasker, Perl, or Regex), but I've just never been able to get the hang of hybridizing Tasker and Java. But I think that you have helped me grasp the adaptation much better and I'll soon be off to the races on my next action rewrite.

Thank you, and thanks again!

John W.

unread,
Jun 3, 2019, 11:19:54 AM6/3/19
to Tasker
Unless Joao could incorporate the fused location Play Service, that would save me some work.

Robert Ryan

unread,
Jun 3, 2019, 11:28:43 AM6/3/19
to tas...@googlegroups.com
AutoLocation uses the Fused Location service, if you don't mind using a plug-in. 

Tbh, I've never seen any evidence that the Fused Location service is any better or more efficient than the standard location service that Tasker uses. 

YMMV

On June 3, 2019 9:19:57 AM "John W." <johnwood...@gmail.com> wrote:

Unless Joao could incorporate the fused location Play Service, that would save me some work.

-- 
You received this message because you are subscribed to the Google Groups "Tasker" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tasker+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

John W.

unread,
Jun 3, 2019, 11:34:00 AM6/3/19
to Tasker
But while I have your ear, do you know how to get the "last location" from android?

The following lines successfully return the address location info, but requires that I first get a coordinates fix within tasker. For efficiency, I'd love to be able to query Android for the last seen coordinates. (Or a way to get the address info directly from Android without having to ask Android for a location lookup.)

A1: Java Function [ Return:geo Class Or Object:Geocoder Function:new
{Geocoder} (Context) Param:CONTEXT Param: Param: Param: Param: Param: Param: Continue Task After Error:On ]

A2: Java Function [ Return:%temp Class Or Object:geo Function:getFromLocation
{List} (double, double, int) Param:%par(6) Param:%par(7) Param:1 Param: Param: Param: Param: Continue Task After Error:On ]

A3: Variable Search Replace [ Variable:%temp(1) Search:(?ix) \".*?\" | (?<=\=)(?!(?:null|true|\-?\d+\.\d+)(?:\,|\]|$))[^\,\"]+(?=\,|\]|$) Ignore Case:Off Multi-Line:Off One Match Only:Off Store Matches In Array:%temp(1) Replace Matches:Off Replace With: ] If [ %temp(1) Set ]

<TaskerData sr="" dvi="1" tv="5.7.2">
<Task sr="task64">
<cdate>1559575345236</cdate>
<edate>1559575420447</edate>
<id>64</id>
<nme>Sandbox - Location</nme>
<Action sr="act0" ve="7">
<code>664</code>
<se>false</se>
<Str sr="arg0" ve="3">geo</Str>
<Str sr="arg1" ve="3">Geocoder</Str>
<Str sr="arg2" ve="3">new
{Geocoder} (Context)</Str>
<Str sr="arg3" ve="3">CONTEXT</Str>
<Str sr="arg4" ve="3"/>
<Str sr="arg5" ve="3"/>
<Str sr="arg6" ve="3"/>
<Str sr="arg7" ve="3"/>
<Str sr="arg8" ve="3"/>
<Str sr="arg9" ve="3"/>
</Action>
<Action sr="act1" ve="7">
<code>664</code>
<se>false</se>
<Str sr="arg0" ve="3">%temp</Str>
<Str sr="arg1" ve="3">geo</Str>
<Str sr="arg2" ve="3">getFromLocation
{List} (double, double, int)</Str>
<Str sr="arg3" ve="3">%par(6)</Str>
<Str sr="arg4" ve="3">%par(7)</Str>
<Str sr="arg5" ve="3">1</Str>
<Str sr="arg6" ve="3"/>
<Str sr="arg7" ve="3"/>
<Str sr="arg8" ve="3"/>
<Str sr="arg9" ve="3"/>
</Action>
<Action sr="act2" ve="7">
<code>598</code>
<Str sr="arg0" ve="3">%temp(1)</Str>
<Str sr="arg1" ve="3">(?ix) \".*?\" | (?&lt;=\=)(?!(?:null|true|\-?\d+\.\d+)(?:\,|\]|$))[^\,\"]+(?=\,|\]|$)</Str>
<Int sr="arg2" val="0"/>
<Int sr="arg3" val="0"/>
<Int sr="arg4" val="0"/>
<Str sr="arg5" ve="3">%temp(1)</Str>
<Int sr="arg6" val="0"/>
<Str sr="arg7" ve="3"/>
<ConditionList sr="if">
<Condition sr="c0" ve="3">
<lhs>%temp(1)</lhs>
<op>12</op>
<rhs></rhs>
</Condition>
</ConditionList>
</Action>
</Task>
</TaskerData>

Robert Ryan

unread,
Jun 3, 2019, 12:02:21 PM6/3/19
to tas...@googlegroups.com
On June 3, 2019 9:34:04 AM "John W." <johnwood...@gmail.com> wrote:

But while I have your ear, do you know how to get the "last location" from android? 

Try this:

        Test
        
    A1: Java Function 
        Return: (LocationManager) location_manager 
        Class Or Object: CONTEXT 
        Function: getSystemService
        {Object} (Class) 
        Param: LocationManager 
        
        <Replace "gps" with "network" if you want the last Net location>
    A2: Java Function 
        Return: location 
        Class Or Object: location_manager 
        Function: getLastKnownLocation
        {Location} (String) 
        Param: "gps" 
        
    A3: Java Function 
        Return: %latitude 
        Class Or Object: location 
        Function: getLatitude
        {double} () 
        
    A4: Java Function 
        Return: %longitude 
        Class Or Object: location 
        Function: getLongitude
        {double} () 
        
    A5: Flash 
        Text: %latitude,%longitude 
        Long: On 

John W.

unread,
Jun 3, 2019, 4:39:28 PM6/3/19
to Tasker
Dude, you're zanily brilliant (and clearly a dev) and you just opened a whole treasure trove of pirate booty! Who knew that Android had a vertical accuracy? (not presently useful, but still cool) 

You've also taken my ego down quite a few pegs, bro. I just sooo dislike using a shotgun when a rifle will do. Don't get me wrong, there are some masterfully-scoped plugins like RESTask that run a dozen times every day (and I'm also a huge fan of app integrations like AquaMail, TimeRecording, and FolderSync) but my academic brain would just prefer multiple finely-scoped plugins over massive do-everythings. Autolocation is like 97% unnecessary for me; Geotask seemed lighter-weight, but since I no longer need geofences, I was solely using it to retrieve last known location. Now I can uninstall it. 

Incidentally, one of my favorite location tweaks is Cell Near ignoring cells that are GSM:0.* and ~R^[^.]*$ . Works better than a geofence and profiles do not inactivate when in the sub-level parking garage or a library vault. If there is not enough granularity, I might use an action qualifier to control which TimeRec punch to issue. For example, a single "Work" profile can service multiple job sites where an action qualifier like %LOC%LOCN ~R 28.57[12]\d+,-85.85[45]\d+ causes a TimeRec punch for "Site/Client A" and %LOC%LOCN ~R 28.52[34]\d+,-85.87[67]\d+ causes a punch for "Site/Client B" or %WIFII ~R (?i:\p{XDigit}:){4}2[9e]:01 would issue a TimeRec punch that I am teaching AA101 (which is taught in the south wing of Building 1) and %WIFII ~R (?i:\p{XDigit}:){4}[ab][34]:\d{2} would issue a TimeRec punch that I am teaching MM304 (which is taught in Annex G).

Yes, it takes more savvy to do this, but battery conservation as an adequate payout (and I've already created a logger profiles to create CSVs of towers and APs). But even so, I have yet to see a geofencing plugin that is this scoped. 

John W.

unread,
Jun 7, 2019, 12:17:15 PM6/7/19
to Tasker
Just to document an interesting l
param (for anyone who subsequently comes upon this thread), the last-known query is not limited solely to "gps" and "network"; "passive" will retrieve whichever of the two is more recent. While undesirable for pinpointing position, if the aim is resolving an address, "passive" is likely a better choice. Dense buildings (like academic libraries or government archives) and subterranean structures could result in a very old GPS fix when a newer network fix will accurately resolve the address. Likewise, in extremelt rural areas, network fixes could be unavailable. The "passive" option will grab whichever is newest.

Also, Tasker's location vars do not update with Android, but Android *does* update with Tasker. So doing a Tasker location update before an address lookup will ensure that Android knows you migrated from an out parcel fast food joint to the mall just behind it (which probably has a different street address).

Admitted, kinda frivolous, but highly useful for tracking events such as sales visits or journals. Add a task shortcut with a voice promoting to add a quick encounter memo or summary. Everything can be written to a csv for Excel or Google Sheets. The timestamps and coords allow calculation of mileage, travel time, and visit time. Also useful if documenting such things as business or medical expense tax itemizations.

Robert Ryan

unread,
Jun 7, 2019, 3:08:43 PM6/7/19
to tas...@googlegroups.com
Not frivolous at all 😁

I was just working on a project where this information was very useful. 

Is that officially documented anywhere? I can't find it in the Android developers reference. 

-- 
You received this message because you are subscribed to the Google Groups "Tasker" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tasker+un...@googlegroups.com.

John W.

unread,
Jun 7, 2019, 4:18:47 PM6/7/19
to Tasker
It was documented elsewhere regarding a different function/object/intent (I don't remember where I saw it). On a hunch, I tried it with your algorithm and it worked! It might not work forever, but it definitely works for now. I suspect that it exists deeper in the Android location awareness and got documented in one spot and forgotten in others. Also functional as a param, "fused", but it's 100% "fused"-up. ;-)

John W.

unread,
Jul 3, 2019, 12:14:43 AM7/3/19
to Tasker
Hey RR, just wanted to thank you again for your java help. The resulting routines are still going strong and I am grateful every time.
Reply all
Reply to author
Forward
0 new messages