Skip to content
Snippets Groups Projects
SedentaryStimulus.java 3.45 KiB
package com.notificationFramework.stimulus;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;

import com.notificationFramework.sedentary.frontEnd.R;
import com.notificationFramework.stimulusStrategy.StimulusStrategy;

import java.util.Calendar;

/**
 * Created by pharmacy on 09/01/2018.
 */

public class SedentaryStimulus extends BroadcastReceiver implements Stimulus {

    private static StimulusStrategy strategy;

    public SedentaryStimulus(){

    }

    public SedentaryStimulus(Context context){
        chooseStrategy(null, context);
    }

    public SedentaryStimulus(Context context, StimulusStrategy s){
        chooseStrategy(s, context);
    }


    private void chooseStrategy(StimulusStrategy s, Context context) {
        if(s != null){
            strategy = s;
        } else{
            strategy = defaultStrategy;
        }
        Intent intent = new Intent(context, strategy.getClass());
        SharedPreferences shared = context.getSharedPreferences(context.getString(R.string.preference_file_key), Context.MODE_PRIVATE);
        if(shared.getBoolean(context.getString(R.string.notf_switch), true)) {

            Log.d("STIM", "SERVICE STARTED");

            context.startService(intent);
        }
    }




    @Override
    public void onReceive(Context context, Intent intent) {
        SharedPreferences shared = context.getSharedPreferences(context.getString(R.string.preference_file_key), Context.MODE_PRIVATE);
        if(shared.getBoolean(context.getString(R.string.notf_switch), true
        && checkTime())) {
            Intent i = new Intent(context.getApplicationContext(), com.notificationFramework.notification.SedentaryNotification.class);
            context.getApplicationContext().startService(i);
        }

        if(strategy == null){
            strategy = defaultStrategy;
        }

        Intent intent1 = new Intent(context, strategy.getClass());
        context.stopService(intent1);
        //Restarts the chosen strategy process.
        chooseStrategy(strategy, context.getApplicationContext());

    }

    private boolean checkTime(){
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 23);
        Calendar cal2 = Calendar.getInstance();
        //Might need to check for month to modulo properly
        int days = checkMonth(cal2.get(Calendar.MONTH));
        cal2.set(Calendar.DATE, ((cal2.get(Calendar.DATE)+ 1)% days));
        cal2.set(Calendar.HOUR_OF_DAY, 9);
        Calendar curCal = Calendar.getInstance();
        if(curCal.getTime().after(cal.getTime()) && curCal.getTime().before(cal2.getTime())){
           return false;
        } else {
            return true;
        }
    }

    private int checkMonth(int month){
        switch (month){
            case Calendar.JANUARY:
            case Calendar.MARCH:
            case Calendar.MAY:
            case Calendar.JULY:
            case Calendar.AUGUST:
            case Calendar.OCTOBER:
            case Calendar.DECEMBER:
                return 31;
            case Calendar.APRIL:
            case Calendar.JUNE:
            case Calendar.SEPTEMBER:
            case Calendar.NOVEMBER:
                return 30;
            case Calendar.FEBRUARY:
                if (Calendar.YEAR % 4 == 0){
                    return 29;
                } else {
                    return 28;
                }
        }
        return -1;
    }
}