package org.gingercal; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.PowerManager; /** Boot, periodic-sync and reminder alarms all land here. */ public class AlarmReceiver extends BroadcastReceiver { public static final String SYNC = "org.gingercal.SYNC"; public static final String REMIND = "org.gingercal.REMIND"; private static PowerManager.WakeLock lock; @Override public void onReceive(Context c, Intent i) { String a = i.getAction(); if (REMIND.equals(a)) { Reminders.fire(c); } else if (SYNC.equals(a)) { acquire(c); App.requestSync(c, false, false); } else { // BOOT_COMPLETED / package replaced: alarms don't survive either App.scheduleSync(c); Reminders.schedule(c); } } static synchronized void acquire(Context c) { if (lock == null) { PowerManager pm = (PowerManager) c.getApplicationContext().getSystemService("power"); lock = pm.newWakeLock(1 /* PARTIAL_WAKE_LOCK */, "GingerCal sync"); lock.setReferenceCounted(false); } lock.acquire(5 * 60 * 1000L); } static synchronized void release() { if (lock != null && lock.isHeld()) lock.release(); } }