package info.nightscout.android.medtronic.service;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.WakefulBroadcastReceiver;
import android.util.Log;

import java.util.Date;

/**
 * Created by lgoedhart on 14/07/2016.
 */
public class MedtronicCnlAlarmReceiver extends WakefulBroadcastReceiver {
    private static final String TAG = MedtronicCnlAlarmReceiver.class.getSimpleName();
    private static final int ALARM_ID = 102; // Alarm id

    private static PendingIntent pi = null;
    private static AlarmManager am = null;

    @Override
    public void onReceive(final Context context, Intent intent) {
        // Start the IntentService
        Log.d(TAG, "Received broadcast message at " + new Date(System.currentTimeMillis()));
        Intent service = new Intent(context, MedtronicCnlIntentService.class);
        startWakefulService(context, service);
        restartAlarm();
    }

    public void setContext(Context context) {
        cancelAlarm();

        am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, MedtronicCnlAlarmReceiver.class);
        pi = PendingIntent.getBroadcast(context, ALARM_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    }

    // Setting the alarm in 15 seconds from now
    public void setAlarm() {
        setAlarm(System.currentTimeMillis());
    }

    // Setting the alarm to call onRecieve
    public void setAlarm(long millis) {
        if (am == null || pi == null)
            return;

        cancelAlarm();

        // don't trigger the past and at least 30 sec away
        if (millis < System.currentTimeMillis())
            millis = System.currentTimeMillis();

        Log.d(TAG, "AlarmManager set to fire   at " + new Date(millis));
        am.setExact(AlarmManager.RTC_WAKEUP, millis, pi);
    }

    // restarting the alarm after MedtronicCnlIntentService.POLL_PERIOD_MS from now
    public void restartAlarm() {
        setAlarm(System.currentTimeMillis() + MedtronicCnlIntentService.POLL_PERIOD_MS + MedtronicCnlIntentService.POLL_GRACE_PERIOD_MS);
    }

    // Cancel the alarm.
    public void cancelAlarm() {
        if (am == null || pi == null)
            return;

        am.cancel(pi);
    }

}

@@ -167,32 +167,57 @@
 content.Intent;%0A
+import android.os.Build;%0A
 import android.s
@@ -610,17 +610,28 @@
 Intent p
-i
+endingIntent
  = null;
@@ -664,17 +664,27 @@
 anager a
-m
+larmManager
  = null;
@@ -1129,17 +1129,27 @@
        a
-m
+larmManager
  = (Alar
@@ -1294,17 +1294,28 @@
        p
-i
+endingIntent
  = Pendi
@@ -1624,33 +1624,43 @@
  %7B%0A        if (a
-m
+larmManager
  == null %7C%7C pi =
@@ -1648,33 +1648,44 @@
 ger == null %7C%7C p
-i
+endingIntent
  == null)%0A      
@@ -1966,55 +1966,409 @@
     
-am.setExact(AlarmManager.RTC_WAKEUP, millis, pi
+if (Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.M) %7B%0A            alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, millis, pendingIntent);%0A        %7D else if (Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.KITKAT) %7B%0A            alarmManager.setExact(AlarmManager.RTC_WAKEUP, millis, pendingIntent);%0A        %7D else%0A            alarmManager.set(AlarmManager.RTC_WAKEUP, millis, pendingIntent
 );%0A 
@@ -2705,17 +2705,27 @@
    if (a
-m
+larmManager
  == null
@@ -2729,17 +2729,28 @@
 ull %7C%7C p
-i
+endingIntent
  == null
@@ -2785,19 +2785,40 @@
    a
-m.cancel(pi
+larmManager.cancel(pendingIntent
 );%0A 

use different methods to set alarm depending on OS version