How do I ensure that the Android library project code is running only in one of the installed applications that integrate it?

advertisements

I'm developing a library project that will be integrated into some popular android applications which can be seen in Google Play.

Let assume that user can have two or more applications installed, and each one can integrate my library. The library have some specific code used to detect changes of environment's state. The state is simply sent to my server. The problem is that environment state processing takes a lot of CPU power, but in a short period of time. Cycles of processing are started by AlarmManager, using "non wake up" broadcasts that launch proper IntentService.

My goal is to implement the library such way, that only one instance integrated into application can do the work. I mean only one library module should act as "active". If there are more applications installed on user's device - then they shouldn't overlap.

How to achieve it? I was thinking about some kind of permission validation, and cross package detection, but couldn't imagine how to implement it.


I'd try something related to the CSMA/CD collision detection technique that's used (or used to be used more often) in networking.

You don't want to commit to a specific instance to be always doing the work, since you don't know if that one would get uninstalled. So instead, make the decision anew each time (since it really doesn't matter which does it at any given time).

It gets a little complicated, because it's not a trivial problem to solve, but I like the idea of someone perhaps generalizing this solution for anyone to use (open-source what you do with this?).

When the initial broadcast arrives, send out a custom broadcast (identified as coming from your particular app) that you're also listening for. If you don't receive any other of that same broadcast within, say, a second, then go ahead and do the work, since there must be no other instances of your library willing to do the work.

If you do get a message from at least one other library (keep track of all of them that you hear from), wait a random amount of time. If you receive a message from another library saying "I'll do it" within that amount of time, then immediately send out a message meaning "okay, you do it". If you don't, then send out a message saying "I'll do it", and wait for every other library you received a message from at the beginning to send a "okay, you do it" message. Then do the work.

If you send a "I'll do it" message, but get an "I'll do it" message from another library as well, then start the process over. The fact that each library waits a random time to send the "I'll do it" means there will rarely be collisions like this, and they certainly shouldn't often happen multiple times in a row.

I hope I've explained this well enough that you can make it happen. If not, please ask for clarification, or look at how this is done in the networking world. What I'm trying to describe is like what's called "Collision Detection", for example as referenced here: https://en.wikipedia.org/wiki/CSMA/CD