package com.project.ilsaenterprise.services;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

public class BackServiceRestarter {
    private Context context;

    public BackServiceRestarter(Context context){
        this.context = context;
    }

    public void setRestarter(){
        Intent intent = new Intent(context, NewTrackerBroadcast.class);

        PendingIntent sender = PendingIntent.getBroadcast(context, 2, intent, 0);
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        if(alarmManager != null){
            long triggerAfter = 5000;
            long triggerEvery = 5000;
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,triggerAfter, triggerEvery, sender);
        }
    }

    public void cancelRestarter(){
        Intent intent = new Intent(context, NewTrackerBroadcast.class);

        PendingIntent sender = PendingIntent.getBroadcast(context, 2, intent, 0);
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        if(alarmManager != null)
            alarmManager.cancel(sender);
    }
}
