[Android Software Development] brandall:
[TUT] Tasker - Total Data Connection & Sync Control V4 [MASSIVE Battery Savings!!!]

Assuming your device is already very battery efficient because you are using my CPU profiles from here, next up is controlling your data connections and syncing schedule.

New to Tasker? Call yourself a geek?! Delete your XDA account immediately or get a free trial from here

The Theory

In order, the most important things in my life are as follows:

1)	Preserving my phone’s battery life.

2)	Sexually satisfying my girlfriend.

As you can see, it’s pretty important for me that I can get through a couple of days with medium usage without having a panic attack about the distance I am from my charger. Download my DistanceFromCharger profile here (just kidding).

So, I looked at my data connections and their battery usage when my scheduled once hourly sync of widgets, emails, facebook, twitter, rss etc etc took place. Without extensive testing, it became clear that these synchronisations became misaligned from the hourly slot due to data quantity, connection time/errors etc etc. Over time, it is therefore theoretically possible to have 10 applications/widgets/processes causing a data sync separately every six minutes (60 mins/10) and although some might say that the total data volume may be the same overall, the act of using the data connection so many more times tells me it will use more battery life. Do you concur?

Even if you don’t (liar!), the following Tasker profiles are to completely manage your wireless connectivity and prevent syncing of unwanted applications at misaligned times.

The Practice

My phone starts its day set to 2g networks only, data connection off, wireless off, screen brightness set so low I need there to be solar eclipse for me to see it and all system volumes set to only alert nearby dogs that I have a text message – all naturally for maximum battery preservation. Depending on the day, my location, calendar entries etc Tasker selects a relevant profile for me to use.

For the benefit of the profiles below, I am going to assume the phone starts in full battery saving mode and go from there. Once you’re up to speed with the flow of the profiles, you can adjust them accordingly to suit your usage.

Firstly, I learnt pretty recently that a wireless connection actually uses less battery than 3g (yes, honestly) and therefore whenever I sync data, I look to use wireless first. For those with constricting data plans, I’m assuming this suits you too?

Wireless states to consider:

On

Off

On and connected to a preferred network

On and connected to an open network

On but not connected to a network

On and recently disconnected from a network

I think that just about covers it?

Download and install Locale Execute Plug-in free from the market here. Import all the profiles (un-tick them) and tasks and with the relevant profile and tasks open (it will make a whole lot more sense then), follow the guide below.

Before we can look at the syncing profile itself, we need to set up some variables for the above first. For battery preservation, I couldn’t have an individual profile for each of the above, as many of them would be active at the same time and constantly on - After a lot of head scratching (and I mean a lot) I came up with the profile WIFI-OnCon let’s have a look at it:

We start with the state of ‘Wifi Connected’. This state means wifi is connected to a network (rather than just ‘on’). The task WifiOnConVar firstly runs a separate task WIFIPREFCON.

WIFIPREFCON writes a text file containing the standard wifi info variable %WIFII (you can take a look at the output file for your information). The next step is to extract just the SSID from the file so Tasker can set this as a variable %WIFIPREF (short for a wifi preferred network). The task ends with speech for you to test the variable has been set correctly (make sure you are connected to a network at the time).

Next, WifiOnConVar sets a variable %WIFIOC (standing for wifi on and connected) to a value of 2. It then sets a variable of %WIFIHOME to the value 2 only IF the name matches that of your home wireless SSID. You can set and add as many of these variables in as you like e.g. %WIFIWORK, %WIFIGIRLFRIENDHOUSE, %WIFIMASSAGEPARLOUR with the IF statement for their respective SSID’s. Setting up these variables is very useful to let Tasker determine your location for use in other profiles, without the need of running GPS or cell tower location profiles (yes, I’m a genius).

Ok, so at the moment, Tasker knows wifi is on and connected (%WIFIOC set to value 2) and you are at home (%WIFIHOME set to your home SSID) (assuming you are testing this at home). A speech task confirms the variables are set.

The next state to consider is when wifi is no longer BOTH on AND connected. We do this in the exit task – the exit task starting when the state of on and connected is no longer true. Firstly it checks if wifi is still on, if it is (goto action 7), it sets the variable %WIFIOC to a value of 3 (another profile reads this shortly) and then clears the name of the preferred network in %WIFIPREF as of course, that cannot be connected anymore…

If the wifi is off, it sets the variable %WIFIHOME to the value 1 and if you have added multiple preferred network variables (%WIFIWORK etc), you should insert that it sets these to a value of 1 too. A speech action confirms the values for your testing purposes (remember, if you delete the speech when you are happy it is working properly, you’ll need to change the Goto Action number accordingly!), the variable %WIFIPREF is cleared as of course you cannot be connected to a preferred network anymore and the task STOPs.

Finally, to complete the wifi control, we need to deal with the wireless still being on, but out of range of a preferred network (that you were connected to). This is definitely a chance to save some battery life, as we’ve all left wifi on by accident when we are not going to be using it. Above, we set the variable of wifi on and connected (%WIFIOC) to a value of 3 when the state became ON but NOT connected. The profile of WIFI-AutoDiscon is set up to come to life when %WIFIOC is set to a value of 3. It activates the task to start a one minute countdown before checking on the wifi state again. This one minute wait allows time where you may have been in a black spot in your office or home and experienced a short disconnection. At the end of the minute, it checks that the value of %WIFIOC has not been changed back to 2 (it has reconnected to a preferred network) and then switches off wifi with your battery giving you a high-5.

That concludes the wireless management. The only missing eventualities are:

On and connected to an open network. (I have little use for this, but can’t sort something out if the demand is high enough).

On and not connected to a network. Not having a profile for this may surprise you, but in actually fact, how often do you turn wireless on when you are not around a preferred network and just leave it? The only reason you would surely do this, is if you were scanning for open networks, in which case a battery saving profile is pretty irrelevant to the scenario. Remember, the state of on and not connected to a network when you were previously connected IS covered, the %WIFIOC variable 3 and the WIFI-AutoDiscon profile monitors this. Happy?

If not, set up a new profile with the state of wifi connected and click ‘invert’ – but this of course will be active when you’re not even using wifi at all…

Taking control of your data syncing

The profile AutoSync is of course time activated, as you would set up your normal ‘between hours’ for email synchronisation. The repeat value sets how often you would like to sync your emails, widgets, applications, weather updates etc etc, by simply restricting their access to an active data connection in-between-times.

The task of AutoSync itself, firstly checks your battery is above a chosen threshold (you decide) to perform the action and that it’s not the weekend (if you want). Again, once you get to grips with the task flow, you can add in separate entries for individual applications, actions etc based on an individual setting – not syncing your work email on the weekend for example.

Action 4 sets a new variable of %WIFIORS. This stands for the ‘original state of wifi’ when the task began and populates using the previously created %WIFIOC variable. The purpose of this is to make sure that once the task has finished it does not turn off wifi if you were in the middle of using it – it will return wifi to the state it was when the task started – clever hey?

Next it uses the third party Locale Execute Plug-in. Locale will be executing commands that will create and record a logcat file for you for the duration of your sync. The purpose of this is so you can review the logcat file and determine what applications are syncing and pulling data. I’ll discuss this further shortly. LOGCAT ACTIONS REMOVED IN LATER VERSIONS DUE TO ERRORS.

The task continues by turning on wifi (if it is already on nothing will change) and waiting to see if it connects to a preferred network by checking the value of the variable %WIFIOC. If a wifi connection is established, it skips to turning on automatic syncing (settings/accounts and sync to view what will sync), if it isn’t, it turns wifi off and uses your phone’s data connection until either eventuality ends up at action 16 where wifi is requested to switch off IF it was off when the task began using the value of %WIFIORS (clever hey).

Mobile Data

(added in V4)

Due to popular demand, the context of mobile data has been added as a profile and then to the tasks. Limitations allow the task only to tell if the mobile data is on, it cannot tell if your preferences are 2g only for example. The variable %MOBORS acts in the same way as %WIFIORS above and will not switch off the mobile data connection if it was on at the beginning of AutoSync. As standard, the profiles assume that your device was set to 2g only to preserve battery. The tasks therefore look to return the device to this state. If you always use 3g preferred, you will need to remove these entries.

Finally, logcat commands are run and files are created, renamed with date and time stamp, moved and zipped up for your future perusal.. *LOGCAT ACTIONS REMOVED IN LATER VERSIONS* The task STOPS if %WIFIORS tells tasker that wifi was on originally or it continues on to switch to 2g only and kill the mobile data connection (these wouldn’t have been activated if wifi had connected or already been on).

So there you have it… Full wifi control and full sync management, with not a single byte of data or rouge connection in sight… You are free to switch off your wifi, data connection, uncheck auto-sync (settings/accounts and sync) and go to 2g only. Tick the profile boxes, hit apply and it’s job done.

Don't forget to set all of your applications and widgets etc that will be syncing to just below the AutoSync repeat time!

Give me your feedback please folks and don’t forget the thanks meter! – At the moment I feel very unappreciated :(

What do I do with the logcat file?

*THESE HAVE NOW BEEN REMOVED*

The logcat files are zipped up and stored in the /tasker folder on your sd card. Looking through these will alert you to any unexpected processes that are requesting to send/receive data. These may be applications you don’t use any more and therefore could be uninstalled or archived (archiving can be achieved with applications such as Bloat Freezer).

Another reason I have stored the logcat is so Tasker could read it and therefore confirm which of your applications have successfully synced, or alert you to failures. This is effort to design and I will only go on to do this by popular demand. In addition, Tasker could be used to kill the rouge processes that are lining up to sync, but are unnecessary… Again, you tell me…

What’s next?

Over to you! Please give me your suggestions. These profiles were set up to suit my individual usage and it may be that most of you focus on your mobile data connection management rather than wifi? Don’t be afraid to post and let me know.

Credits

Unknown for zip file profile

Bdanders for wifi variable inspiration