My app's launcher activity checks a bunch of requirements like whether GPS is enabled, there's a network connection, the app has device administrator privileges, etc. Some of these checks display dialogs or take the user to another activity to resolve the unsatisfied requirements. I'm looking for a way of doing this that's easy to extend with more requirements and easy for others to read and understand.
My original solution was to perform the checks sequentially, each in its own method, with each method either taking remedial action or calling the next method in the sequence. The sequence is started in onResume()
, so upon returning from another activity it picks up with the first check that doesn't pass. This got messy fast because some of the requirements are conditional. For example, on Lollipop it checks if the app is the device owner, and whether it might be able to become the device owner. If it can, it tells the user to run the appropriate adb shell
command. Naturally, this whole sequence of checks is skipped on KitKat. So the chain of methods is actually more like a tangled braid. It's hard to trace the logic of it or verify that each step eventually calls the next under all possible conditions. I'm reluctant to make any changes to it, and I just wrote it myself a couple weeks ago.
My other idea was to number the steps (or associate them with the elements of an enum
) and pass the last-completed step between activity instances. But I anticipate this getting messy, too.
How would you structure this?