There are literally entire books written on how to get data packets from a mobile device to a server and back. There are protocols and strategies like Mobile IP, WAP, WiFi, WiMax, and so on. But there is relatively little written about the ways we can write our applications to better deal with the conditions of a mobile device: limited memory, battery and power consumption concerns, connectivity issues, malicious (intentionally or not) networks, and a host of other issues. I’m going to explain some of the ways we expose these issues to provide a more useful abstraction in the Brigand library.
Brigand provides you with a handy API for scheduling HTTP requests that can be completed hours or days later. It’s goal is to remove the common misconception that an HTTP request is immediate.
The first thing we want our library to be aware of is battery state and level. For Android, we access the battery status using a simple call (like most of these things, it will require a Context)
From this, we can extract the battery level:
We can also check what we’re charging from
At the moment, we don’t do it, but we plan on extending this to actually determine the exact amount of power remaining in absolute terms. This will allow us to set policies according to the actual amount of power that will be consumed in order to perform the request.
The most important distinction we can make is what type of connectivity we have at the moment. This means differentiating between low power networks, such as WiFi or GPRS, and high power networks, such as HSPA and LTE. For those of you who feel like you are missing out from the alphabet soup here, the basic reasoning is that cellphone radios work in multiple modes, with orders of magnitude of difference in power consumption between modes. This means that each second spent sending data actively chews up hours of equivalent idle time.
We can work around a good chunk of this to make intelligent decisions, such as waiting for another application to switch the radio mode into high power mode, and then bundle our data with theirs, letting them take the blame for the majority of the power usage (switching between modes takes a lot of power, and can eat hours of battery life).
Unfortunately, we can’t request that Android wake up our app when the device switches to DCH mode, but we can ask to be woken up when connectivity changes and sleep for a short period (about half a minute) until other apps already make the requests. We can also fall back on other triggers to ensure our request is made in a timely fashion.
Time is an illusion; lunchtime doubly so.
- Douglas Adams
However, we silly apes pay enough attention to it that it can make all the difference for an app that needs to send analytics data. For example, you may want to schedule your analytics data for transmission in the night, when the device is more likely to be plugged in for a long period of time, with little change to connectivity.
More importantly, it gives us the chance to change our policies after a period of time. This is useful if we need to ensure our data is sent within 30 minutes, but we prefer to not be blamed for battery usage.
Disabling Application Components
Efficiently doing this can result in your application being woken up more often than you’d like. For example, if you don’t have any queued transfers, there’s no reason to wake up your application. To prevent that happening, you can actually request that Android turn off certain components:
And then once you’ve queued your first transfer, you can turn the listeners on based on the queueing policy:
The two use cases which need to be power aware are as follows:
- Lazy Transmission - this is the scenario where you’d like the data to be sent within 30 minutes, but you’d prefer to not get blamed for the power usage. Once 30 minutes have passed, we allow initiating the radio state change, so long as the battery isn’t below 20%, which we only allow after 60 minutes.
- Analytics - analytics data is extremely low priority. If the battery is between 0 and 30%, we never send this data. Between 30% and 50%, we only send it if either charging or connected to wifi. Above 50%, we still prefer to wait until someone else initiates a data transfer to piggyback onto.
Brigand handles the latter case at the moment, but we plan on supporting the former in the future.
What Brigand is
Brigand is the opportunistic bastard of all communication libraries. It implements most of these strategies, and we plan on adding support in the future for the remainder.
What Brigand isn’t
Brigand is not Volley. It isn’t a general purpose communication library. It is specifically designed for custom sync protocols and analytics data reporting. Trying to use it for other things will usually result in it destroying your device, drinking your beer, and molesting your cat. Beware.