package com.notificationFramework.stimulus;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorManager;

import com.notificationFramework.stimulusStrategy.Accelerometer;
import com.notificationFramework.stimulusStrategy.SigMotionDetect;

/**
 * Created by Peter De Jonckheere on 12/02/2018.
 * <p>
 * This class is called upon when the device is booted to ensure that a stimulus process is
 * started upon boot of the device.
 * </p>
 */

public class BootLauncher extends BroadcastReceiver {

    /**
     * On receiving the OS broadcast that the device has been booted, a stimulus is started with a
     * strategy chosen based on the available hardware of the device.
     *
     * @param context the context from which the broadcast receiver can operate
     * @param intent  the intent which accompanies the broadcast sent
     */
    @Override
    public void onReceive(Context context, Intent intent) {
        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            SensorManager sm = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
            try {
                if (sm.getDefaultSensor(Sensor.TYPE_STEP_COUNTER) != null
                        && sm.getDefaultSensor(Sensor.TYPE_SIGNIFICANT_MOTION) != null) {
                    Stimulus stimulus = new SedentaryStimulus(context.getApplicationContext(),
                            new SigMotionDetect());
                } else {
                    Stimulus stimulus = new SedentaryStimulus(context.getApplicationContext(),
                            new Accelerometer());
                }
            } catch (NullPointerException e) {
                Stimulus stimulus = new SedentaryStimulus(context.getApplicationContext(),
                        new Accelerometer());
            }
        }
    }
}