Skip to content
Snippets Groups Projects
Commit 36b7cd26 authored by Peter Joseph De Jonckheere CESM2014's avatar Peter Joseph De Jonckheere CESM2014
Browse files

Fixed feedback notifications and goal notifications

parent 7b7084dd
No related branches found
No related tags found
No related merge requests found
Showing with 261 additions and 193 deletions
...@@ -54,11 +54,9 @@ ...@@ -54,11 +54,9 @@
</service> </service>
<service android:name="com.notificationFramework.stimulusStrategy.StepCounter"> <service android:name="com.notificationFramework.stimulusStrategy.StepCounter">
</service> </service>
<receiver android:name="com.notificationFramework.stimulus.SedentaryStimulus"> <receiver android:name="com.notificationFramework.notification.FeedbackNotificationCanceller">
</receiver>
<receiver android:name="com.notificationFramework.stimulus.GoalStimulus">
</receiver> </receiver>
<receiver android:name="com.notificationFramework.stimulus.FeedbackStimulus"> <receiver android:name="com.notificationFramework.stimulus.SedentaryStimulus">
</receiver> </receiver>
<receiver android:name="com.notificationFramework.stimulus.BootLauncher"> <receiver android:name="com.notificationFramework.stimulus.BootLauncher">
<intent-filter> <intent-filter>
......
...@@ -9,6 +9,7 @@ import android.content.Intent; ...@@ -9,6 +9,7 @@ import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.SystemClock; import android.os.SystemClock;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.notificationFramework.sedentary.frontEnd.R; import com.notificationFramework.sedentary.frontEnd.R;
...@@ -50,6 +51,7 @@ public class FeedbackNotification extends SedentaryNotification { ...@@ -50,6 +51,7 @@ public class FeedbackNotification extends SedentaryNotification {
builder.setVibrate(new long[]{0, 500, 0, 500}); builder.setVibrate(new long[]{0, 500, 0, 500});
} }
setCancellingAlarm(); setCancellingAlarm();
Log.d("Feedback", "Should be");
return builder; return builder;
} }
...@@ -69,6 +71,7 @@ public class FeedbackNotification extends SedentaryNotification { ...@@ -69,6 +71,7 @@ public class FeedbackNotification extends SedentaryNotification {
private void setCancellingAlarm(){ private void setCancellingAlarm(){
AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
Log.d("Cancelling??", "hopefully");
Intent i = new Intent(this, Intent i = new Intent(this,
com.notificationFramework.notification.FeedbackNotificationCanceller.class); com.notificationFramework.notification.FeedbackNotificationCanceller.class);
PendingIntent pi = PendingIntent.getBroadcast(this, PendingIntent pi = PendingIntent.getBroadcast(this,
...@@ -76,6 +79,6 @@ public class FeedbackNotification extends SedentaryNotification { ...@@ -76,6 +79,6 @@ public class FeedbackNotification extends SedentaryNotification {
SharedPreferences shared = this.getSharedPreferences(getString( SharedPreferences shared = this.getSharedPreferences(getString(
R.string.preference_file_key), Context.MODE_PRIVATE); R.string.preference_file_key), Context.MODE_PRIVATE);
am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + (1000 * 60 * 2), pi); SystemClock.elapsedRealtime() + 3000, pi);
} }
} }
...@@ -18,7 +18,7 @@ public class FeedbackNotificationCanceller extends BroadcastReceiver { ...@@ -18,7 +18,7 @@ public class FeedbackNotificationCanceller extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
try { try {
nm.cancel(context.getResources().getInteger(R.integer.notffeedback_id)); nm.cancelAll();
} catch(NullPointerException e){ } catch(NullPointerException e){
Log.i("NOTIFICATION", "NOTIFICATION WAS CLICKED ON"); Log.i("NOTIFICATION", "NOTIFICATION WAS CLICKED ON");
} }
......
package com.notificationFramework.sedentary.frontEnd; package com.notificationFramework.sedentary.frontEnd;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.icu.util.RangeValueIterator; import android.icu.util.RangeValueIterator;
import android.support.v4.content.LocalBroadcastManager; import android.support.v4.content.LocalBroadcastManager;
...@@ -44,9 +46,67 @@ public class DataAnalysis { ...@@ -44,9 +46,67 @@ public class DataAnalysis {
Log.i("NOTIFICATION", "NOTIFICATION WAS CLICKED ON"); Log.i("NOTIFICATION", "NOTIFICATION WAS CLICKED ON");
} }
Intent i = new Intent(context.getApplicationContext(), Intent i = new Intent("FEEDBACKFILTER");
com.notificationFramework.stimulus.FeedbackStimulus.class); final LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context.getApplicationContext());
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context.getApplicationContext()); BroadcastReceiver feedback = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("FEEDBACK STIM", "EHH HELP");
SharedPreferences shared =
context.getSharedPreferences(context.getString(R.string.preference_file_key), Context.MODE_PRIVATE);
if ((checkTime(shared, context)) && (shared.getBoolean(context.getString(R.string.notf_switch), true))) {
Intent i = new Intent(context.getApplicationContext(),
com.notificationFramework.notification.FeedbackNotification.class);
context.getApplicationContext().startService(i);
}
lbm.unregisterReceiver(this);
}
/**
* Checks the current time against the do not disturb time by first checking if the time spans 2
* days, then checking between the first time and midnight, then midnight and the second time,
* followed finally by the simple check between the two times if the time does not span 2 days.
* It is done in this way to avoid compications with date changes as each time is obtained using
* the current calendar as a template so if done correctly dates should not need to be changed.
*
* @param shared the SharedPreferences instance set up in the onReceive() method
* @param context the application context which this method has been called from
* @return false if the time is within the do not disturb period and so a notification should
* not be sent, true if a notification should be sent
*/
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;
}
}
};
lbm.registerReceiver(feedback, new IntentFilter("FEEDBACKFILTER"));
lbm.sendBroadcast(i); lbm.sendBroadcast(i);
logAcknowledgement(context); logAcknowledgement(context);
return true; return true;
......
package com.notificationFramework.stimulus;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import com.notificationFramework.sedentary.frontEnd.R;
import java.util.Calendar;
/**
* Created by pharmacy on 25/06/2018.
*/
public class FeedbackStimulus extends BroadcastReceiver implements Stimulus {
/**
* Upon receiving a broadcast from a strategy this method is triggered and starts the related
* service which sends the notification after checking relevant personalised settings.
*
* @param context the context of the broadcast
* @param intent the intent with which the broadcast was sent
*/
@Override
public void onReceive(Context context, Intent intent) {
SharedPreferences shared =
context.getSharedPreferences(context.getString(R.string.preference_file_key), Context.MODE_PRIVATE);
if ((checkTime(shared, context)) && (shared.getBoolean(context.getString(R.string.notf_switch), true))) {
Intent i = new Intent(context.getApplicationContext(),
com.notificationFramework.notification.FeedbackNotification.class);
context.getApplicationContext().startService(i);
}
}
/**
* Checks the current time against the do not disturb time by first checking if the time spans 2
* days, then checking between the first time and midnight, then midnight and the second time,
* followed finally by the simple check between the two times if the time does not span 2 days.
* It is done in this way to avoid compications with date changes as each time is obtained using
* the current calendar as a template so if done correctly dates should not need to be changed.
*
* @param shared the SharedPreferences instance set up in the onReceive() method
* @param context the application context which this method has been called from
* @return false if the time is within the do not disturb period and so a notification should
* not be sent, true if a notification should be sent
*/
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;
}
}
}
package com.notificationFramework.stimulus;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import com.notificationFramework.sedentary.frontEnd.R;
import java.util.Calendar;
/**
* Created by Peter De Jonckheere on 09/03/2018.
* <p>
* The concrete implementation of the Stimulus interface which handles requests for goal
* notifications to be sent.
* </p>
*
* @see android.content.BroadcastReceiver
*/
public class GoalStimulus extends BroadcastReceiver implements Stimulus {
/**
* Upon receiving a broadcast from a strategy this method is triggered and starts the related
* service which sends the notification after checking relevant personalised settings.
*
* @param context the context of the broadcast
* @param intent the intent with which the broadcast was sent
*/
@Override
public void onReceive(Context context, Intent intent) {
SharedPreferences shared =
context.getSharedPreferences(context.getString(R.string.preference_file_key), Context.MODE_PRIVATE);
if ((checkTime(shared, context)) && (shared.getBoolean(context.getString(R.string.notf_switch), true))) {
Intent i = new Intent(context.getApplicationContext(),
com.notificationFramework.notification.GoalNotification.class);
context.getApplicationContext().startService(i);
}
}
/**
* Checks the current time against the do not disturb time by first checking if the time spans 2
* days, then checking between the first time and midnight, then midnight and the second time,
* followed finally by the simple check between the two times if the time does not span 2 days.
* It is done in this way to avoid compications with date changes as each time is obtained using
* the current calendar as a template so if done correctly dates should not need to be changed.
*
* @param shared the SharedPreferences instance set up in the onReceive() method
* @param context the application context which this method has been called from
* @return false if the time is within the do not disturb period and so a notification should
* not be sent, true if a notification should be sent
*/
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;
}
}
}
...@@ -3,6 +3,7 @@ package com.notificationFramework.stimulus; ...@@ -3,6 +3,7 @@ package com.notificationFramework.stimulus;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.support.v4.content.LocalBroadcastManager; import android.support.v4.content.LocalBroadcastManager;
import android.util.Log; import android.util.Log;
...@@ -165,10 +166,72 @@ public class SedentaryStimulus extends BroadcastReceiver implements Stimulus { ...@@ -165,10 +166,72 @@ public class SedentaryStimulus extends BroadcastReceiver implements Stimulus {
Log.d("AVGTHRESH", String.valueOf(ackThreshold)); Log.d("AVGTHRESH", String.valueOf(ackThreshold));
float avgAck = shared.getFloat(context.getString(R.string.avg_ack), 0); float avgAck = shared.getFloat(context.getString(R.string.avg_ack), 0);
Log.d("AVGACK", String.valueOf(avgAck)); Log.d("AVGACK", String.valueOf(avgAck));
//test
avgAck = 1;
ackThreshold = 0;
if(avgAck > ackThreshold){ if(avgAck > ackThreshold){
Intent i = new Intent(context.getApplicationContext(), Log.d("SEDSTIM", "I ASSUME");
com.notificationFramework.stimulus.FeedbackStimulus.class); Intent i = new Intent("FEEDBACKFILTER");
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context.getApplicationContext()); final LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context.getApplicationContext());
BroadcastReceiver feedback = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("FEEDBACK STIM", "EHH HELP");
SharedPreferences shared =
context.getSharedPreferences(context.getString(R.string.preference_file_key), Context.MODE_PRIVATE);
if ((checkTime(shared, context)) && (shared.getBoolean(context.getString(R.string.notf_switch), true))) {
Intent i = new Intent(context.getApplicationContext(),
com.notificationFramework.notification.FeedbackNotification.class);
context.getApplicationContext().startService(i);
}
lbm.unregisterReceiver(this);
}
/**
* Checks the current time against the do not disturb time by first checking if the time spans 2
* days, then checking between the first time and midnight, then midnight and the second time,
* followed finally by the simple check between the two times if the time does not span 2 days.
* It is done in this way to avoid compications with date changes as each time is obtained using
* the current calendar as a template so if done correctly dates should not need to be changed.
*
* @param shared the SharedPreferences instance set up in the onReceive() method
* @param context the application context which this method has been called from
* @return false if the time is within the do not disturb period and so a notification should
* not be sent, true if a notification should be sent
*/
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;
}
}
};
lbm.registerReceiver(feedback, new IntentFilter("FEEDBACKFILTER"));
lbm.sendBroadcast(i); lbm.sendBroadcast(i);
SaveFile.recordNotification(0,0,0,1, context); SaveFile.recordNotification(0,0,0,1, context);
return false; return false;
...@@ -180,7 +243,7 @@ public class SedentaryStimulus extends BroadcastReceiver implements Stimulus { ...@@ -180,7 +243,7 @@ public class SedentaryStimulus extends BroadcastReceiver implements Stimulus {
private boolean checkContext(SharedPreferences shared, Context context){ private boolean checkContext(SharedPreferences shared, Context context){
int mostPop = shared.getInt(context.getString(R.string.store_mcp), -1); int mostPop = shared.getInt(context.getString(R.string.store_mcp), -1);
float temp = shared.getFloat(context.getString(R.string.store_temp), 18); float temp = shared.getFloat(context.getString(R.string.store_temp), 18);
int lastPlace = shared.getInt(context.getString(R.string.store_place), 0); // int lastPlace = shared.getInt(context.getString(R.string.store_place), 0);
int ackTotal = shared.getInt(context.getString(R.string.ack_total), 0); int ackTotal = shared.getInt(context.getString(R.string.ack_total), 0);
int placeAck = shared.getInt(context.getString(R.string.place_ack_total), 0); int placeAck = shared.getInt(context.getString(R.string.place_ack_total), 0);
double comp = (double) placeAck / ackTotal; double comp = (double) placeAck / ackTotal;
......
...@@ -3,8 +3,10 @@ package com.notificationFramework.stimulusStrategy; ...@@ -3,8 +3,10 @@ package com.notificationFramework.stimulusStrategy;
import android.app.AlarmManager; import android.app.AlarmManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.app.Service; import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.hardware.Sensor; import android.hardware.Sensor;
import android.hardware.SensorEvent; import android.hardware.SensorEvent;
...@@ -16,15 +18,10 @@ import android.support.annotation.Nullable; ...@@ -16,15 +18,10 @@ import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager; import android.support.v4.content.LocalBroadcastManager;
import android.util.Log; import android.util.Log;
import com.google.android.gms.awareness.Awareness;
import com.google.android.gms.awareness.state.Weather;
import com.google.android.gms.location.places.PlaceLikelihood;
import com.notificationFramework.sedentary.frontEnd.R; import com.notificationFramework.sedentary.frontEnd.R;
import com.notificationFramework.sedentary.frontEnd.RequestPermission;
import com.notificationFramework.sedentary.frontEnd.SaveFile; import com.notificationFramework.sedentary.frontEnd.SaveFile;
import java.util.Calendar; import java.util.Calendar;
import java.util.List;
/** /**
...@@ -180,9 +177,66 @@ public class Accelerometer extends Service implements StimulusStrategy, SensorEv ...@@ -180,9 +177,66 @@ public class Accelerometer extends Service implements StimulusStrategy, SensorEv
* class. * class.
*/ */
public void goalNotify() { public void goalNotify() {
Intent i = new Intent(getBaseContext(), Intent i = new Intent("FEEDBACKFILTER");
com.notificationFramework.stimulus.GoalStimulus.class); final LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this.getApplicationContext());
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this); BroadcastReceiver goal = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
SharedPreferences shared =
context.getSharedPreferences(context.getString(R.string.preference_file_key), Context.MODE_PRIVATE);
if ((checkTime(shared, context)) && (shared.getBoolean(context.getString(R.string.notf_switch), true))) {
Intent i = new Intent(context.getApplicationContext(),
com.notificationFramework.notification.GoalNotification.class);
context.getApplicationContext().startService(i);
}
lbm.unregisterReceiver(this);
}
/**
* Checks the current time against the do not disturb time by first checking if the time spans 2
* days, then checking between the first time and midnight, then midnight and the second time,
* followed finally by the simple check between the two times if the time does not span 2 days.
* It is done in this way to avoid compications with date changes as each time is obtained using
* the current calendar as a template so if done correctly dates should not need to be changed.
*
* @param shared the SharedPreferences instance set up in the onReceive() method
* @param context the application context which this method has been called from
* @return false if the time is within the do not disturb period and so a notification should
* not be sent, true if a notification should be sent
*/
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;
}
}
};
lbm.registerReceiver(goal, new IntentFilter("GOALFILTER"));
lbm.sendBroadcast(i); lbm.sendBroadcast(i);
} }
......
...@@ -4,8 +4,10 @@ import android.Manifest; ...@@ -4,8 +4,10 @@ import android.Manifest;
import android.app.AlarmManager; import android.app.AlarmManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.app.Service; import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.hardware.Sensor; import android.hardware.Sensor;
...@@ -260,10 +262,66 @@ public class SigMotionDetect extends Service implements StimulusStrategy { ...@@ -260,10 +262,66 @@ public class SigMotionDetect extends Service implements StimulusStrategy {
* broadcast is then sent to the relevant stimulus class and on to the relevant notification * broadcast is then sent to the relevant stimulus class and on to the relevant notification
* class. * class.
*/ */
public void goalNotify() { public void goalNotify() { Intent i = new Intent("FEEDBACKFILTER");
Intent i = new Intent(getBaseContext(), final LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this.getApplicationContext());
com.notificationFramework.stimulus.GoalStimulus.class); BroadcastReceiver goal = new BroadcastReceiver() {
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this); @Override
public void onReceive(Context context, Intent intent) {
SharedPreferences shared =
context.getSharedPreferences(context.getString(R.string.preference_file_key), Context.MODE_PRIVATE);
if ((checkTime(shared, context)) && (shared.getBoolean(context.getString(R.string.notf_switch), true))) {
Intent i = new Intent(context.getApplicationContext(),
com.notificationFramework.notification.GoalNotification.class);
context.getApplicationContext().startService(i);
}
lbm.unregisterReceiver(this);
}
/**
* Checks the current time against the do not disturb time by first checking if the time spans 2
* days, then checking between the first time and midnight, then midnight and the second time,
* followed finally by the simple check between the two times if the time does not span 2 days.
* It is done in this way to avoid compications with date changes as each time is obtained using
* the current calendar as a template so if done correctly dates should not need to be changed.
*
* @param shared the SharedPreferences instance set up in the onReceive() method
* @param context the application context which this method has been called from
* @return false if the time is within the do not disturb period and so a notification should
* not be sent, true if a notification should be sent
*/
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;
}
}
};
lbm.registerReceiver(goal, new IntentFilter("GOALFILTER"));
lbm.sendBroadcast(i); lbm.sendBroadcast(i);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment