That overcomplicated Android simple app

This article is part of a series that introduces you to BADASS: a framework to help you to kick the ass of your Android development time.

This a short introduction to how an an Android application (app) with simple functionality can be much much harder to code properly than you might think.

There is no simple app

Nowadays a typical Android app gets data from internet/API/connected device, does some treatment on it, and give it to the user.

It sounds simple but in the real life it’s not.

Two examples (among so many):

  • The user wants to get data but the server is not available (user have no connection, in an underground parking for example).
  • The data you get from the server needs few seconds to be downloaded/computed for display. Problem: on Android, with the ANR system you must manage this task in background

As an Android developer, I’ve spent several years struggling with this kind of constraints, patching my apps each time I discover a new one. And I’ve often found new constraint. Often, frequently, regularly…

To avoid a (boring) theoretical approach, I will use a feature of the BadassWeather sample.

My app:

It’s a small and easy feature to understand, and you will see that it deals with all the problems I try to resolve.

A simple feature in four NOT simple steps

Intuitively this feature should work like this:

Step 1: The user opens the app

Step 2: The app get the device location

Step 3: The app does a internet request to have the weather

Step 4: The app displays weather to the user in a couple of milliseconds

Simple as fuck, Right?

Let’s review in details these four steps to check.

The user experience doesn’t always work like this. Android uses classical apps but also widgets and notifications to display data to the user.

The user could look at notifications and widgets literally anytime and you can’t know when. User doesn’t tap anywhere or launch anything, he just looks at widget/notification.

So you have to try to keep these elements always updated with the current weather even if the user doesn’t open the app.

Well, that’s not so simple. Not at all.

First of all, since Android M, you need an explicit permission to get GPS precise coordinate. It’s not difficult, but the process has many steps with some troubling issues. For example how do you do if the user doesn’t allow the app to get GPS coordinates?

After this, you have to connect to FusedLocationApi (especially if you want to know when location changes)

Note :
If you want to manage every possible case, you must check if Google Play Services is available to use this API. It’s obviously available if the user installed the app with Google Play, but you may have to check anyway (This is an example of how developers must pay attention to many, many details).

Once connected you have to wait for the system to give you the location data. And, by definition, the location can change anytime.

So you must check/get the permission, possibly check Google Play Services presence, connect to FusedLocationApi and wait for location and changes.

All of this is asynchronous so location change can happen anytime…even when app is closed.

An this is the nominal case, what do you do if, for example, app lose the connection to the API?

Firs of all Android is forcing you to do that in the background (For non developers: you must do something to prevent to block your app during the task)

And you know what? You need the location to have weather: How do you do if app is waiting Android to give the location? If location is not available for a while? If new location is detected when app is closed ?

And what do you do if there is no Internet to reach the weather data server? if the server is down?

Well (again), that’s not so simple (again). The response from server is not “Rainy” but something like this more than 3000 lines XML! So you have to do some treatment before you give it to the user.

And it could take some time, and if you just do it in your activity, you have an app that doesn’t respond to users interaction or worse: the ANR of the death.

Application Not Responding …

So with just an app with simple feature and without trying to explore all scenarios (how do you udpdate widgets and notifications?) I have just a big piece of sh…constraints to deal with.

Well, If you want to make a good app, you must try to list all of these constraints.

And you know what? I just speak About it THERE.

BACK TO BADASS HUB.

About me

Pierre Benayoun, 42. Working and drinking red wine in Bordeaux (France). Computer engineer since 2001, Android developer since 2011 and unfortunate one man startup since 2013 (but keep trying). I’m also freelance, happy with my current mission but always open to new opportunities.

Thanks

Thanks to:

Acknowledgements

Even if the linguistic quality of this article is far from perfect, it would be much worse without Deepl translator.

Android developer in Bordeaux, France. Computer engineer since 2001, Android developer since 2011 and unfortunate one man startup since 2013.