package com.example.stimulus; import android.app.Notification; import android.app.NotificationManager; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; import android.net.ConnectivityManager; import android.util.Log; import android.widget.Toast; import com.example.pharmacy.frontEnd.R; import com.example.stimulusStrategy.StimulusStrategy; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * 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; } // strategy.monitor(); 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) { //If a condition - such as time elapsed since last notification or not night time //Intent data used here to configure notification?? Intent would need to be configured properly for this. SharedPreferences shared = context.getSharedPreferences(context.getString(R.string.preference_file_key), Context.MODE_PRIVATE); Log.d("CALLBACK", "SERVICE STARTED"); //Do time/date check here for late night notifications //May also add advanced settings page to take care of this and other things such as led color, sound and timing of led and sound if(shared.getBoolean(context.getString(R.string.notf_switch), true && checkTime())) { Log.d("CALLBACK", "NOTIFICATION"); Intent i = new Intent(context.getApplicationContext(), com.example.notification.SedentaryNotification.class); context.getApplicationContext().startService(i); } //Stops previous service 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 cal2.set(Calendar.DATE, (cal2.get(Calendar.DATE)+ 1)); 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; } } }