5

Commonsware's WakefulIntentService works beautifully but there are some things I do not quite get. Below is the core of the service - a stripped down version of the source :

class WIS extends IntentService {

    private static final String NAME = WIS.class.getName() + ".Lock";
    private static volatile WakeLock lockStatic = null;

    synchronized private static PowerManager.WakeLock getLock(Context context) {
        if (lockStatic == null) {
            PowerManager mgr = (PowerManager) context
                    .getSystemService(Context.POWER_SERVICE);
            lockStatic = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, NAME);
            lockStatic.setReferenceCounted(true);
        }
        return (lockStatic);
    }

    public static void startWIS(Context ctxt, Intent i) {
        getLock(ctxt.getApplicationContext()).acquire();
        ctxt.startService(i);
    }

    public WIS(String name) {
        super(name);
        setIntentRedelivery(true);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        PowerManager.WakeLock lock = getLock(this.getApplicationContext());
        if (!lock.isHeld() || (flags & START_FLAG_REDELIVERY) != 0) { // ?
            lock.acquire();
        }
        super.onStartCommand(intent, flags, startId);
        return (START_REDELIVER_INTENT);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        try {
            // do your thing
        } finally {
            PowerManager.WakeLock lock = getLock(this.getApplicationContext());
            if (lock.isHeld()) lock.release();
        }
    }
}

Questions

  • What happens if the process is killed just after the onReceive() of our alarm receiver returns ? That is if service onCreate() (if the service is not already instantiated) or onStartCommand() never run. AFAIK a process killed takes its locks with it. Or is this an impossible scenario ?
  • In view of the previous should (flags & START_FLAG_RETRY) be added ?
  • Why the if (!lock.isHeld()) check ?
  • Why is this.getApplicationContext() needed ? is not this enough ?
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361

2 Answers2

6

AFAIK a process killed takes its locks with it.

Correct.

Or is this an impossible scenario ?

It's fairly unlikely, but certainly not impossible.

In view of the previous should (flags & START_FLAG_RETRY) be added ?

That should be covered by START_FLAG_REDELIVERY. AFAIK, with START_REDELIVER_INTENT, there is no RETRY without REDELIVERY. If you have evidence to the contrary, I'd love to see it.

Why the if (!lock.isHeld()) check ?

Calling release() on a WakeLock that is not held results in an exception. This is just a safety blanket to ensure we don't wind up throwing an unnecessary exception. In theory, it should never be needed; in theory, I should have hair.

Why is this.getApplicationContext() needed ? is not this enough ?

We create a WakeLock, which we hold in a static data member. Probably the getSystemService() call does not wind up putting the Context that called it inside the PowerManager. And, even if it did, probably the Context would not be passed to the resulting WakeLock instance. However, to be safe, by using getApplicationContext(), we obtain the WakeLock in a fashion that ensures that the only Context we could possibly "leak" is the singleton application context, which, as a singleton, is effectively pre-leaked. :-)

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • "If you have evidence to the contrary, I'd love to see it." - not a bit, just the docs are not clear that when START_FLAG_RETRY is set then START_FLAG_REDELIVERY is also set – Mr_and_Mrs_D Nov 19 '13 at 15:34
  • A- important - if the scenario I describe happens and onCreate (or even the Constructor where we `setRedeliverIntent(true)`) did not run - we loose ? Or the system will run the `onStartCommand` with START_FLAG_RETRY ? – Mr_and_Mrs_D Nov 19 '13 at 15:38
  • 1
    @Mr_and_Mrs_D: "the docs are not clear that when START_FLAG_RETRY is set then START_FLAG_REDELIVERY is also set " -- `START_FLAG_REDELIVERY` is set when `START_REDELIVER_INTENT` is used, and I am not aware of a case where I would ever see `START_FLAG_RETRY` on its own. "this initialization of reference fields to null is needed ?" -- no, that's personal coding style. "if the scenario I describe happens" -- I have no idea what would happen in that case, sorry. – CommonsWare Nov 19 '13 at 15:42
  • @CommonsWare while performing network operation on `wakefulintentservice` it throws `NetworkonMainThread` Exception ?? – Amresh Aug 20 '15 at 17:13
  • @Ryderz: No, it won't. Partly, that is because `WakefulIntentService` does not do any network I/O itself. Partly, that is because `doWakefulWork()` is called on a background thread. – CommonsWare Aug 20 '15 at 17:19
  • @CommonsWare Thanks! But everytime my activity re-launches `wakeupservice` gets restarted, not able to detect whether running or not? – Amresh Aug 25 '15 at 10:41
  • HI @CommonsWare Is this useful library compatible with apps target Android Oreo? – Rajesh K Dec 25 '18 at 14:24
  • @RajeshK: `WakefulIntentService` has been discontinued. Use `JobIntentService` (or `WorkManager`, once it reaches 1.0.0 -- it is still `1.0.0-beta1` right now). – CommonsWare Dec 25 '18 at 21:04
  • @CommonsWare Thanks for the reply. – Rajesh K Dec 26 '18 at 20:09
1

Sorry don't have enough rep to comment but it looks like you have a race condition with the two checks if(held)release and if(!held)acquire. I.e. making the ref volatile is not enough to guard you against races.

These are composite statements invoked on different threads. You likely want to enter a sync block on a private final Object lock = new Object() for those two checks so they are done atomically. Very corner-y but thought I'd mention it. Let me know if you disagree. Thanks.

Creos
  • 2,445
  • 3
  • 27
  • 45