package com.mridang.speedo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import androidx.preference.PreferenceManager;

import java.util.Objects;

/**
 * Broadcast receiver class to help start the traffic monitoring service when the phone boots up only
 * if the service is enabled.
 */
public class BootReceiver extends BroadcastReceiver {

    /**
     * Receiver method for the phone boot that starts the traffic monitoring service
     */
    @Override
    public void onReceive(Context appContext, Intent bootIntent) {
        if (!(Objects.equals(bootIntent.getAction(), Intent.ACTION_BOOT_COMPLETED))
                || Objects.equals(bootIntent.getAction(), Intent.ACTION_MY_PACKAGE_REPLACED)) {
            return;
        }

        if (PreferenceManager.getDefaultSharedPreferences(appContext).getBoolean("enabled", true)) {
            appContext.startService(new Intent(appContext, TrafficService.class));
        }
    }
}