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; /** * 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"); //possibly check here for android notification settings if(shared.getBoolean(context.getString(R.string.notf_switch), true)) { Log.d("CALLBACK", "NOTIFICATION"); Intent i = new Intent(context.getApplicationContext(), com.example.notification.SedentaryNotification.class); context.getApplicationContext().startService(i); } //Stops previous service //Crashes here when app is killed and strategy is therefore null. Log.d("STRATEGY",strategy.toString()); Intent intent1 = new Intent(context, strategy.getClass()); context.stopService(intent1); //Restarts the chosen strategy process. chooseStrategy(strategy, context.getApplicationContext()); } }