Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/NightscoutFoundation/xDrip
Browse files Browse the repository at this point in the history
…into workingt:1102
  • Loading branch information
jamorham committed Nov 2, 2016
2 parents 1b6f5da + 7028238 commit 70fb553
Show file tree
Hide file tree
Showing 11 changed files with 332 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class MissedReadingActivity extends ActivityWithMenu {

private CheckBox checkboxEnableAlert;
private CheckBox checkboxAllDay;
private CheckBox checkboxEnableReraise;

private LinearLayout layoutTimeBetween;
private LinearLayout timeInstructions;
Expand All @@ -35,9 +36,13 @@ public class MissedReadingActivity extends ActivityWithMenu {
private TextView timeInstructionsStart;
private TextView timeInstructionsEnd;
private EditText bgMissedMinutes;
private EditText bgMissedSnoozeMin;
private EditText bgMissedReraiseSec;

private TextView viewAlertTime;
private TextView vieSelectTime;
private TextView viewSelectTime;
private TextView viewSnoozeTime;
private TextView viewReraiseTime;


private int startHour = 0;
Expand All @@ -57,30 +62,41 @@ protected void onCreate(Bundle savedInstanceState) {
viewTimeEnd = (TextView) findViewById(R.id.missed_reading_time_end);
checkboxAllDay = (CheckBox) findViewById(R.id.missed_reading_all_day);
checkboxEnableAlert = (CheckBox) findViewById(R.id.missed_reading_enable_alert);
checkboxEnableReraise = (CheckBox) findViewById(R.id.missed_reading_enable_alerts_reraise);

layoutTimeBetween = (LinearLayout) findViewById(R.id.missed_reading_time_between);
timeInstructions = (LinearLayout) findViewById(R.id.missed_reading_instructions);
timeInstructionsStart = (TextView) findViewById(R.id.missed_reading_instructions_start);
timeInstructionsEnd = (TextView) findViewById(R.id.missed_reading_instructions_end);
bgMissedMinutes = (EditText) findViewById(R.id.missed_reading_bg_minutes);
bgMissedSnoozeMin = (EditText) findViewById(R.id.missed_reading_bg_snooze);
bgMissedReraiseSec = (EditText) findViewById(R.id.missed_reading_reraise_sec);
viewAlertTime = (TextView) findViewById(R.id.missed_reading_text_alert_time);
vieSelectTime = (TextView) findViewById(R.id.missed_reading_text_select_time);
viewSelectTime = (TextView) findViewById(R.id.missed_reading_text_select_time);
viewSnoozeTime = (TextView) findViewById(R.id.missed_reading_bg_snooze_text);
viewReraiseTime = (TextView) findViewById(R.id.missed_reading_reraise_sec_text);


// Set the different controls
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
int startMinutes = prefs.getInt("missed_readings_start", 0);
int endMinutes = prefs.getInt("missed_readings_end", 0);
boolean enableAlert = prefs.getBoolean("bg_missed_alerts",false);
boolean allDay = prefs.getBoolean("missed_readings_all_day",true);
boolean enableReraise = prefs.getBoolean("bg_missed_alerts_enable_alerts_reraise",false);

checkboxEnableAlert.setChecked(enableAlert);
checkboxAllDay.setChecked(allDay);
checkboxEnableAlert.setChecked(enableAlert);
checkboxEnableReraise.setChecked(enableReraise);

startHour = AlertType.time2Hours(startMinutes);
startMinute = AlertType.time2Minutes(startMinutes);
endHour = AlertType.time2Hours(endMinutes);
endMinute = AlertType.time2Minutes(endMinutes);
bgMissedMinutes.setText(prefs.getString("bg_missed_minutes", "30"));
int defaultSnooze = MissedReadingService.readPerfsInt(prefs, "other_alerts_snooze", 20);
bgMissedSnoozeMin.setText(prefs.getString("bg_missed_alerts_snooze", "" + defaultSnooze));
bgMissedReraiseSec.setText(prefs.getString("bg_missed_alerts_reraise_sec", "60"));

addListenerOnButtons();
enableAllControls();
Expand All @@ -94,9 +110,12 @@ public void onDestroy() {
prefs.edit().putInt("missed_readings_start", AlertType.toTime(startHour, startMinute)).apply();
prefs.edit().putInt("missed_readings_end", AlertType.toTime(endHour, endMinute)).apply();
prefs.edit().putString("bg_missed_minutes", bgMissedMinutes.getText().toString()).apply();
prefs.edit().putString("bg_missed_alerts_snooze", bgMissedSnoozeMin.getText().toString()).apply();
prefs.edit().putString("bg_missed_alerts_reraise_sec", bgMissedReraiseSec.getText().toString()).apply();

prefs.edit().putBoolean("bg_missed_alerts", checkboxEnableAlert.isChecked()).apply();
prefs.edit().putBoolean("missed_readings_all_day", checkboxAllDay.isChecked()).apply();
prefs.edit().putBoolean("bg_missed_alerts_enable_alerts_reraise", checkboxEnableReraise.isChecked()).apply();

context.startService(new Intent(context, MissedReadingService.class));
}
Expand All @@ -110,9 +129,14 @@ void EnableControls(boolean enabled) {
layoutTimeBetween.setEnabled(enabled);
timeInstructions.setEnabled(enabled);
checkboxAllDay.setEnabled(enabled);
checkboxEnableReraise.setEnabled(enabled);
bgMissedMinutes.setEnabled(enabled);
bgMissedSnoozeMin.setEnabled(enabled);
bgMissedReraiseSec.setEnabled(enabled);
viewAlertTime.setEnabled(enabled);
vieSelectTime.setEnabled(enabled);
viewSelectTime.setEnabled(enabled);
viewSnoozeTime.setEnabled(enabled);
viewReraiseTime.setEnabled(enabled);
}

void enableAllControls() {
Expand All @@ -129,6 +153,10 @@ void enableAllControls() {
} else {
setTimeRanges();
}

boolean enableReraise = checkboxEnableReraise.isChecked();
bgMissedReraiseSec.setEnabled(enableReraise);

}


Expand All @@ -141,13 +169,22 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
enableAllControls();
}
});

checkboxEnableAlert.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
// @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
enableAllControls();
}
});

checkboxEnableReraise.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
// @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
enableAllControls();
}
});


View.OnClickListener startTimeListener = new View.OnClickListener() {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
Expand Down Expand Up @@ -163,20 +164,7 @@ public static AlertType get_highest_active_alert(Context context, double bg) {
return null;
}

Boolean bg_unclear_readings_alerts = prefs.getBoolean("bg_unclear_readings_alerts", false);
Long UnclearTimeSetting = Long.parseLong(prefs.getString("bg_unclear_readings_minutes", "90")) * 60000;

Long UnclearTime = BgReading.getUnclearTime(UnclearTimeSetting);

AlertType at;
if (UnclearTime >= UnclearTimeSetting && bg_unclear_readings_alerts ) {
Log.d("NOTIFICATIONS", "Readings have been unclear for too long!!");
Notifications.bgUnclearAlert(context);
}
if ((UnclearTime > 0 ) && bg_unclear_readings_alerts) {
Log.d(TAG_ALERT, "We are in an clear state, but not for too long. Alerts are disabled");
return null;
}
at = get_highest_active_alert_helper(bg, prefs);
if (at != null) {
Log.d(TAG_ALERT, "get_highest_active_alert_helper returned alert uuid = " + at.uuid + " alert name = " + at.name);
Expand Down Expand Up @@ -427,7 +415,6 @@ public static void CreateStaticAlerts() {


public static void testAll(Context context) {

remove_all();
add_alert(null, "high alert 1", true, 180, true, 10, null, 0, 0, true, 20, true, true);
add_alert(null, "high alert 2", true, 200, true, 10, null, 0, 0, true, 20, true, true);
Expand Down
53 changes: 29 additions & 24 deletions app/src/main/java/com/eveningoutpost/dexdrip/Models/BgReading.java
Original file line number Diff line number Diff line change
Expand Up @@ -1216,11 +1216,6 @@ public static void checkForRisingAllert(Context context) {
return;
}

if(IsUnclearTime(context)) {
Log.d(TAG_ALERT, "checkForRisingAllert we are in an clear time, returning without doing anything");
return ;
}

String riseRate = prefs.getString("rising_bg_val", "2");
float friseRate = 2;

Expand Down Expand Up @@ -1250,11 +1245,6 @@ public static void checkForDropAllert(Context context) {
return;
}

if(IsUnclearTime(context)) {
Log.d(TAG_ALERT, "checkForDropAllert we are in an clear time, returning without doing anything");
return ;
}

String dropRate = prefs.getString("falling_bg_val", "2");
float fdropRate = 2;

Expand Down Expand Up @@ -1311,18 +1301,38 @@ private static boolean checkForDropRiseAllert(float MaxSpeed, boolean drop) {
return true;
}

private static boolean IsUnclearTime(Context context) {
// Make sure that this function either sets the alert or removes it.
public static boolean getAndRaiseUnclearReading(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);

if(prefs.getLong("alerts_disabled_until", 0) > new Date().getTime()){
Log.d("NOTIFICATIONS", "getAndRaiseUnclearReading Notifications are currently disabled!!");
UserNotification.DeleteNotificationByType("bg_unclear_readings_alert");
return false;
}

Boolean bg_unclear_readings_alerts = prefs.getBoolean("bg_unclear_readings_alerts", false);
if(bg_unclear_readings_alerts) {
Long UnclearTimeSetting = Long.parseLong(prefs.getString("bg_unclear_readings_minutes", "90")) * 60000;
Long unclearTime = getUnclearTime(UnclearTimeSetting);
if (unclearTime > 0) {
Log.d(TAG_ALERT, "IsUnclearTime we are in an clear time, returning true");
return true;
}
if (!bg_unclear_readings_alerts || (!DexCollectionType.hasFiltered())) {
Log.d(TAG_ALERT, "getUnclearReading returned false since feature is disabled");
UserNotification.DeleteNotificationByType("bg_unclear_readings_alert");
return false;
}
Long UnclearTimeSetting = Long.parseLong(prefs.getString("bg_unclear_readings_minutes", "90")) * 60000;

Long UnclearTime = BgReading.getUnclearTime(UnclearTimeSetting);

if (UnclearTime >= UnclearTimeSetting ) {
Log.d("NOTIFICATIONS", "Readings have been unclear for too long!!");
Notifications.bgUnclearAlert(context);
return true;
}

UserNotification.DeleteNotificationByType("bg_unclear_readings_alert");

if (UnclearTime > 0 ) {
Log.d(TAG_ALERT, "We are in an clear state, but not for too long. Alerts are disabled");
return true;
}

return false;
}
/*
Expand All @@ -1338,11 +1348,6 @@ public static boolean trendingToAlertEnd(Context context, boolean above) {
// TODO: check if we are not in an UnclerTime.
Log.d(TAG_ALERT, "trendingToAlertEnd called");

if(IsUnclearTime(context)) {
Log.d(TAG_ALERT, "trendingToAlertEnd we are in an clear time, returning false");
return false;
}

List<BgReading> latest = getXRecentPoints(3);
if(latest == null) {
Log.d(TAG_ALERT, "trendingToAlertEnd we don't have enough points from the last 15 minutes, returning false");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.eveningoutpost.dexdrip.Models;

import android.provider.BaseColumns;
import android.util.Log;

import com.activeandroid.Model;
import com.activeandroid.annotation.Column;
Expand All @@ -12,6 +11,8 @@
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import com.eveningoutpost.dexdrip.Models.UserError.Log;
import com.eveningoutpost.dexdrip.UtilityModels.AlertPlayer;

/**
* Created by stephenblack on 11/29/14.
Expand All @@ -20,6 +21,8 @@
@Table(name = "Notifications", id = BaseColumns._ID)
public class UserNotification extends Model {

// For 'other alerts' this will be the time that the alert should be raised again.
// For calibration alerts this is the time that the alert was played.
@Column(name = "timestamp", index = true)
public double timestamp;

Expand Down Expand Up @@ -54,7 +57,7 @@ public class UserNotification extends Model {
"bg_alert", "calibration_alert", "double_calibration_alert",
"extra_calibration_alert", "bg_unclear_readings_alert",
"bg_missed_alerts", "bg_rise_alert", "bg_fall_alert");
private final static String TAG = "UserNotification";
private final static String TAG = AlertPlayer.class.getSimpleName();


public static UserNotification lastBgAlert() {
Expand Down Expand Up @@ -124,9 +127,20 @@ public static void DeleteNotificationByType(String type) {
}
}

public static UserNotification create(String message, String type) {
public static void snoozeAlert(String type, long snoozeMinutes) {
UserNotification userNotification = GetNotificationByType(type);
if(userNotification == null) {
Log.e(TAG, "Error snoozeAlert did not find an alert for type " + type);
return;
}
userNotification.timestamp = new Date().getTime() + snoozeMinutes * 60000;
userNotification.save();

}

public static UserNotification create(String message, String type, long timestamp) {
UserNotification userNotification = new UserNotification();
userNotification.timestamp = new Date().getTime();
userNotification.timestamp = timestamp;
userNotification.message = message;
if (type == "bg_alert") {
userNotification.bg_alert = true;
Expand All @@ -146,7 +160,7 @@ public static UserNotification create(String message, String type) {
userNotification.bg_fall_alert = true;
} else {
Log.d(TAG,"Saving workaround for: "+type+" "+message);
Home.setPreferencesString("UserNotification:timestamp:" + type, JoH.qs((JoH.ts())));
Home.setPreferencesString("UserNotification:timestamp:" + type, JoH.qs(timestamp));
Home.setPreferencesString("UserNotification:message:" + type, message);
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,13 @@ protected void onHandleIntent(Intent intent) {
}

bg_missed_minutes = readPerfsInt(prefs, "bg_missed_minutes", 30);
otherAlertSnooze = readPerfsInt(prefs, "other_alerts_snooze", 20);
final long now = new Date().getTime();

if (BgReading.getTimeSinceLastReading() >= (bg_missed_minutes * 1000 * 60) &&
prefs.getLong("alerts_disabled_until", 0) <= now &&
inTimeFrame(prefs)) {
Notifications.bgMissedAlert(context);
checkBackAfterSnoozeTime(now);
checkBackAfterSnoozeTime(context, now);
} else {

long disabletime = prefs.getLong("alerts_disabled_until", 0) - now;
Expand All @@ -99,23 +98,30 @@ private boolean inTimeFrame(SharedPreferences prefs) {
return AlertType.s_in_time_frame(allDay, startMinutes, endMinutes);
}

public void checkBackAfterSnoozeTime(long now) {
private void checkBackAfterSnoozeTime(Context context, long now) {
// This is not 100% acurate, need to take in account also the time of when this alert was snoozed.
UserNotification userNotification = UserNotification.GetNotificationByType("bg_missed_alerts");
if(userNotification == null) {
setAlarm(otherAlertSnooze * 1000 * 60);
// No active alert exists, should not happen, we have just created it.
Log.wtf(TAG, "No active alert exists.");
setAlarm(getOtherAlertReraiseSec(context, "bg_missed_alerts") * 1000, false);
} else {
// we have an alert that is snoozed until userNotification.timestamp
setAlarm((long)userNotification.timestamp - now + otherAlertSnooze * 1000 * 60);
// we have an alert that should be re-raised on userNotification.timestamp
long alarmIn = (long)userNotification.timestamp - now;
if(alarmIn < 0) {
alarmIn = 0;
}
setAlarm(alarmIn, true);
}
}

public void checkBackAfterMissedTime(long alarmIn) {
setAlarm(alarmIn);
setAlarm(alarmIn, false);
}

public void setAlarm(long alarmIn) {
if(alarmIn < 5 * 60 * 1000) {

// alarmIn is relative time ms
public void setAlarm(long alarmIn, boolean force) {
if(!force && (alarmIn < 5 * 60 * 1000)) {
// No need to check more than once every 5 minutes
alarmIn = 5 * 60 * 1000;
}
Expand All @@ -140,4 +146,16 @@ static public int readPerfsInt(SharedPreferences prefs, String name, int default
return defaultValue;
}
}

static public long getOtherAlertReraiseSec(Context context, String alertName) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean enableAlertsReraise = prefs.getBoolean(alertName + "_enable_alerts_reraise" , false);
if(enableAlertsReraise) {
return readPerfsInt(prefs, alertName + "_reraise_sec", 60);
} else {
int defaultSnooze = readPerfsInt(prefs, "other_alerts_snooze", 20);
return 60 * readPerfsInt(prefs, alertName + "_snooze", defaultSnooze);
}

}
}
Loading

0 comments on commit 70fb553

Please sign in to comment.