Skip to content
Snippets Groups Projects
SedentaryStimulus.java 3.64 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 Peter De Jonckheere 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());
        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);
        Log.d("NOTIFICATION", "SENT");
        if((checkTime(shared, context)) && (shared.getBoolean(context.getString(R.string.notf_switch), true))) {
            Log.d("NOTIFICATION", "TIME?");
            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);
        chooseStrategy(strategy, context.getApplicationContext());
    }


    /**
     *
     * @param shared
     * @param context
     * @return
     */
    private boolean checkTime(SharedPreferences shared, Context context) {
        Calendar start = Calendar.getInstance();
        start.set(Calendar.HOUR_OF_DAY, shared.getInt(context.getString(R.string.dnd_shour), 23));
        start.set(Calendar.MINUTE, shared.getInt(context.getString(R.string.dnd_smin), 0));
        Calendar midnightCal = Calendar.getInstance();
        midnightCal.set(Calendar.HOUR_OF_DAY, 23);
        midnightCal.set(Calendar.MINUTE, 59);
        midnightCal.set(Calendar.SECOND, 59);
        Calendar end = Calendar.getInstance();
        end.set(Calendar.HOUR_OF_DAY, shared.getInt(context.getString(R.string.dnd_ehour), 9));
        end.set(Calendar.MINUTE, shared.getInt(context.getString(R.string.dnd_emin), 0));
        Calendar curCal = Calendar.getInstance();
        if (shared.getInt(context.getString(R.string.dnd_shour), 23) > shared.getInt(context.getString(R.string.dnd_ehour), 9)) {
            if (curCal.getTime().after(start.getTime()) && curCal.getTime().before(midnightCal.getTime())) {
                return false;
            } else {
                midnightCal.set(Calendar.HOUR_OF_DAY, 0);
                midnightCal.set(Calendar.MINUTE, 0);
                midnightCal.set(Calendar.SECOND, 1);
                if (curCal.getTime().after(midnightCal.getTime()) && curCal.getTime().before(end.getTime())) {
                    return false;
                } else {
                    return true;
                }
            }
        } else if (curCal.getTime().after(start.getTime()) && curCal.getTime().before(end.getTime())) {
            return false;
        } else {
            return true;
        }
    }
}