Search in sources :

Example 21 with AlertType

use of com.eveningoutpost.dexdrip.Models.AlertType in project xDrip-plus by jamorham.

the class Notifications method FileBasedNotifications.

/*
 * *************************************************************************************************************
 * Function for new notifications
 */
private void FileBasedNotifications(Context context) {
    ReadPerfs(context);
    Sensor sensor = Sensor.currentSensor();
    final BgReading bgReading = BgReading.last();
    if (bgReading == null) {
        // Sensor is stopped, or there is not enough data
        AlertPlayer.getPlayer().stopAlert(context, true, false);
        return;
    }
    final double calculated_value;
    /* KS TODO BestGlucose
        if (use_best_glucose) {
            this.dg = BestGlucose.getDisplayGlucose();
            if (dg != null) {
                bgReading.calculated_value = dg.mgdl;
                calculated_value = dg.mgdl;
            } else {
                calculated_value = bgReading.calculated_value;
                Log.wtf(TAG, "Could not obtain best glucose value!");
            }
        } else {*/
    calculated_value = bgReading.calculated_value;
    // }
    Log.d(TAG, "FileBasedNotifications called bgReading.calculated_value = " + bgReading.calculated_value + " calculated value: " + calculated_value);
    // In all this cases, bgReading.calculated_value should be 0.
    if (((sensor != null) || (Home.get_follower())) && calculated_value != 0) {
        AlertType newAlert = AlertType.get_highest_active_alert(context, calculated_value);
        if (newAlert == null) {
            Log.d(TAG, "FileBasedNotifications - No active notifcation exists, stopping all alerts");
            // No alert should work, Stop all alerts, but keep the snoozing...
            AlertPlayer.getPlayer().stopAlert(context, false, true);
            return;
        }
        AlertType activeBgAlert = ActiveBgAlert.alertTypegetOnly();
        if (activeBgAlert == null) {
            Log.d(TAG, "FileBasedNotifications we have a new alert, starting to play it... " + newAlert.name);
            // We need to create a new alert  and start playing
            boolean trendingToAlertEnd = trendingToAlertEnd(context, true, newAlert);
            // KS EditAlertActivity.
            AlertPlayer.getPlayer().startAlert(context, trendingToAlertEnd, newAlert, unitsConvert2Disp(doMgdl, calculated_value));
            return;
        }
        if (activeBgAlert.uuid.equals(newAlert.uuid)) {
            // disable alert on stale data
            if (prefs.getBoolean("disable_alerts_stale_data", false)) {
                int minutes = Integer.parseInt(prefs.getString("disable_alerts_stale_data_minutes", "15")) + 2;
                if ((new Date().getTime()) - (60000 * minutes) - BgReading.lastNoSenssor().timestamp > 0) {
                    Log.d(TAG, "FileBasedNotifications : active alert found but not replaying it because more than three readings missed :  " + newAlert.name);
                    return;
                }
            }
            Log.d(TAG, "FileBasedNotifications we have found an active alert, checking if we need to play it " + newAlert.name);
            boolean trendingToAlertEnd = trendingToAlertEnd(context, false, newAlert);
            // KS EditAlertActivity
            AlertPlayer.getPlayer().ClockTick(context, trendingToAlertEnd, unitsConvert2Disp(doMgdl, calculated_value));
            return;
        }
        // Currently the ui blocks having two alerts with the same alert value.
        boolean alertSnoozeOver = ActiveBgAlert.alertSnoozeOver();
        if (alertSnoozeOver) {
            Log.d(TAG, "FileBasedNotifications we had two alerts, the snoozed one is over, we fall down to deleting the snoozed and staring the new");
        // in such case it is not important which is higher.
        } else {
            // we have a new alert. If it is more important than the previous one. we need to stop
            // the older one and start a new one (We need to play even if we were snoozed).
            // If it is a lower level alert, we should keep being snoozed.
            // Example, if we have two alerts one for 90 and the other for 80. and we were already alerting for the 80
            // and we were snoozed. Now bg is 85, the alert for 80 is cleared, but we are alerting for 90.
            // We should not do anything if we are snoozed for the 80...
            // If one allert was high and the second one is low however, we alarm in any case (snoozing ignored).
            boolean opositeDirection = AlertType.OpositeDirection(activeBgAlert, newAlert);
            if (!opositeDirection) {
                AlertType newHigherAlert = AlertType.HigherAlert(activeBgAlert, newAlert);
                if ((newHigherAlert == activeBgAlert)) {
                    // the existing (snoozed) alert is the higher, No need to play it since it is snoozed.
                    Log.d(TAG, "FileBasedNotifications The new alert has the same direcotion, it is lower than the one snoozed, not playing it." + " newHigherAlert = " + newHigherAlert.name + "activeBgAlert = " + activeBgAlert.name);
                    return;
                }
            }
        }
        // For now, we are stopping the old alert and starting a new one.
        Log.d(TAG, "Found a new alert, that is higher than the previous one will play it. " + newAlert.name);
        AlertPlayer.getPlayer().stopAlert(context, true, false);
        boolean trendingToAlertEnd = trendingToAlertEnd(context, true, newAlert);
        // KS EditAlertActivity
        AlertPlayer.getPlayer().startAlert(context, trendingToAlertEnd, newAlert, unitsConvert2Disp(doMgdl, calculated_value));
    } else {
        AlertPlayer.getPlayer().stopAlert(context, true, false);
    }
}
Also used : AlertType(com.eveningoutpost.dexdrip.Models.AlertType) BgReading(com.eveningoutpost.dexdrip.Models.BgReading) Date(java.util.Date) Sensor(com.eveningoutpost.dexdrip.Models.Sensor)

Example 22 with AlertType

use of com.eveningoutpost.dexdrip.Models.AlertType in project xDrip-plus by jamorham.

the class WatchUpdaterService method sendAlertTypeData.

private void sendAlertTypeData() {
    // KS
    try {
        forceGoogleApiConnect();
        List<AlertType> alerts = AlertType.getAllActive();
        if (alerts != null) {
            if (wear_integration) {
                Log.d(TAG, "sendAlertTypeData latest count = " + alerts.size());
                final DataMap entries = new DataMap();
                final ArrayList<DataMap> dataMaps = new ArrayList<>(alerts.size());
                for (AlertType alert : alerts) {
                    if (alert != null) {
                        dataMaps.add(dataMap(alert, "alert"));
                    }
                }
                // MOST IMPORTANT LINE FOR TIMESTAMP
                entries.putLong("time", new Date().getTime());
                entries.putDataMapArrayList("entries", dataMaps);
                new SendToDataLayerThread(WEARABLE_ALERTTYPE_DATA_PATH, googleApiClient).executeOnExecutor(xdrip.executor, entries);
            } else
                Log.d(TAG, "sendAlertTypeData latest count = 0");
        }
    } catch (NullPointerException e) {
        Log.e(TAG, "Nullpointer exception in sendAlertTypeData: " + e);
    }
}
Also used : AlertType(com.eveningoutpost.dexdrip.Models.AlertType) ArrayList(java.util.ArrayList) Date(java.util.Date) DataMap(com.google.android.gms.wearable.DataMap)

Example 23 with AlertType

use of com.eveningoutpost.dexdrip.Models.AlertType in project xDrip-plus by jamorham.

the class EditAlertActivity method verifyThreshold.

private boolean verifyThreshold(double threshold, boolean allDay, int startTime, int endTime) {
    List<AlertType> lowAlerts = AlertType.getAll(false);
    List<AlertType> highAlerts = AlertType.getAll(true);
    if (threshold < MIN_ALERT || threshold > MAX_ALERT) {
        Toast.makeText(getApplicationContext(), "threshold has to be between " + unitsConvert2Disp(doMgdl, MIN_ALERT) + " and " + unitsConvert2Disp(doMgdl, MAX_ALERT), Toast.LENGTH_LONG).show();
        return false;
    }
    // We want to make sure that for each threashold there is only one alert. Otherwise, which file should we play.
    for (AlertType lowAlert : lowAlerts) {
        if (lowAlert.threshold == threshold && overlapping(lowAlert, allDay, startTime, endTime) && lowAlert.active) {
            if (uuid == null || !uuid.equals(lowAlert.uuid)) {
                // new alert or not myself
                Toast.makeText(getApplicationContext(), "Each alert should have it's own threshold. Please choose another threshold.", Toast.LENGTH_LONG).show();
                return false;
            }
        }
    }
    for (AlertType highAlert : highAlerts) {
        if (highAlert.threshold == threshold && overlapping(highAlert, allDay, startTime, endTime) && highAlert.active) {
            if (uuid == null || !uuid.equals(highAlert.uuid)) {
                // new alert or not myself
                Toast.makeText(getApplicationContext(), "Each alert should have it's own threshold. Please choose another threshold.", Toast.LENGTH_LONG).show();
                return false;
            }
        }
    }
    // high alerts have to be higher than all low alerts...
    if (above) {
        for (AlertType lowAlert : lowAlerts) {
            if (threshold < lowAlert.threshold && overlapping(lowAlert, allDay, startTime, endTime) && lowAlert.active) {
                Toast.makeText(getApplicationContext(), "High alert threshold has to be higher than all low alerts. Please choose another threshold.", Toast.LENGTH_LONG).show();
                return false;
            }
        }
    } else {
        // low alert has to be lower than all high alerts
        for (AlertType highAlert : highAlerts) {
            if (threshold > highAlert.threshold && overlapping(highAlert, allDay, startTime, endTime) && highAlert.active) {
                Toast.makeText(getApplicationContext(), "Low alert threshold has to be lower than all high alerts. Please choose another threshold.", Toast.LENGTH_LONG).show();
                return false;
            }
        }
    }
    return true;
}
Also used : AlertType(com.eveningoutpost.dexdrip.Models.AlertType)

Example 24 with AlertType

use of com.eveningoutpost.dexdrip.Models.AlertType in project xDrip-plus by jamorham.

the class EditAlertActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    xdrip.checkForcedEnglish(xdrip.getAppContext());
    super.onCreate(savedInstanceState);
    mContext = this;
    setContentView(R.layout.activity_edit_alert);
    viewHeader = (TextView) findViewById(R.id.view_alert_header);
    buttonSave = (Button) findViewById(R.id.edit_alert_save);
    buttonRemove = (Button) findViewById(R.id.edit_alert_remove);
    buttonTest = (Button) findViewById(R.id.edit_alert_test);
    buttonalertMp3 = (Button) findViewById(R.id.Button_alert_mp3_file);
    buttonPreSnooze = (Button) findViewById(R.id.edit_alert_pre_snooze);
    alertText = (EditText) findViewById(R.id.edit_alert_text);
    alertThreshold = (EditText) findViewById(R.id.edit_alert_threshold);
    alertMp3File = (EditText) findViewById(R.id.edit_alert_mp3_file);
    checkboxAllDay = (CheckBox) findViewById(R.id.check_alert_time);
    checkboxVibrate = (CheckBox) findViewById(R.id.check_vibrate);
    checkboxDisabled = (CheckBox) findViewById(R.id.view_alert_check_disable);
    layoutTimeBetween = (LinearLayout) findViewById(R.id.time_between);
    timeInstructions = (LinearLayout) findViewById(R.id.time_instructions);
    timeInstructionsStart = (TextView) findViewById(R.id.time_instructions_start);
    timeInstructionsEnd = (TextView) findViewById(R.id.time_instructions_end);
    viewTimeStart = (TextView) findViewById(R.id.view_alert_time_start);
    viewTimeEnd = (TextView) findViewById(R.id.view_alert_time_end);
    editSnooze = (EditText) findViewById(R.id.edit_snooze);
    reraise = (EditText) findViewById(R.id.reraise);
    viewAlertOverrideText = (TextView) findViewById(R.id.view_alert_override_silent);
    checkboxAlertOverride = (CheckBox) findViewById(R.id.check_override_silent);
    this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    addListenerOnButtons();
    if (BgGraphBuilder.isXLargeTablet(getApplicationContext())) {
        viewHeader.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
        buttonSave.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
        buttonRemove.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
        buttonTest.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
        buttonalertMp3.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
        buttonPreSnooze.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
        alertText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
        alertThreshold.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
        alertMp3File.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
        checkboxAllDay.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
        checkboxVibrate.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
        checkboxDisabled.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
        viewTimeStart.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
        viewTimeEnd.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
        editSnooze.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
        reraise.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
        viewAlertOverrideText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
        ((TextView) findViewById(R.id.view_alert_text)).setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
        ((TextView) findViewById(R.id.view_alert_threshold)).setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
        ((TextView) findViewById(R.id.view_alert_default_snooze)).setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
        ((TextView) findViewById(R.id.view_alert_mp3_file)).setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
        ((TextView) findViewById(R.id.view_alert_time)).setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
        ((TextView) findViewById(R.id.view_alert_time_between)).setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
        ((TextView) findViewById(R.id.view_alert_disable)).setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
    }
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    doMgdl = (prefs.getString("units", "mgdl").compareTo("mgdl") == 0);
    if (!doMgdl) {
        alertThreshold.setInputType(InputType.TYPE_CLASS_NUMBER);
        alertThreshold.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
        alertThreshold.setKeyListener(DigitsKeyListener.getInstance(false, true));
    }
    uuid = getExtra(savedInstanceState, "uuid", null);
    String status;
    int alertReraise;
    int defaultSnooze;
    if (uuid == null) {
        // This is a new alert
        above = Boolean.parseBoolean(getExtra(savedInstanceState, "above", null));
        checkboxAllDay.setChecked(true);
        checkboxVibrate.setChecked(true);
        checkboxDisabled.setChecked(false);
        checkboxAlertOverride.setChecked(true);
        audioPath = "";
        alertMp3File.setText(shortPath(audioPath));
        alertMp3File.setKeyListener(null);
        defaultSnooze = SnoozeActivity.getDefaultSnooze(above);
        buttonRemove.setVisibility(View.GONE);
        // One can not snooze an alert that is still not in the database...
        buttonPreSnooze.setVisibility(View.GONE);
        status = getString(R.string.adding) + " " + (above ? getString(R.string.high) : getString(R.string.low)) + " " + getString(R.string.alert);
        startHour = 0;
        startMinute = 0;
        endHour = 23;
        endMinute = 59;
        alertReraise = 1;
    } else {
        // We are editing an alert
        AlertType at = AlertType.get_alert(uuid);
        if (at == null) {
            Log.wtf(TAG, "Error editing alert, when that alert does not exist...");
            Intent returnIntent = new Intent();
            setResult(RESULT_CANCELED, returnIntent);
            finish();
            return;
        }
        above = at.above;
        alertText.setText(at.name);
        alertThreshold.setText(unitsConvert2Disp(doMgdl, at.threshold));
        checkboxAllDay.setChecked(at.all_day);
        checkboxVibrate.setChecked(at.vibrate);
        checkboxDisabled.setChecked(!at.active);
        checkboxAlertOverride.setChecked(at.override_silent_mode);
        defaultSnooze = at.default_snooze;
        if (defaultSnooze == 0) {
            SnoozeActivity.getDefaultSnooze(above);
        }
        audioPath = getExtra(savedInstanceState, "audioPath", at.mp3_file);
        alertMp3File.setText(shortPath(audioPath));
        status = "editing " + (above ? "high" : "low") + " alert";
        startHour = AlertType.time2Hours(at.start_time_minutes);
        startMinute = AlertType.time2Minutes(at.start_time_minutes);
        endHour = AlertType.time2Hours(at.end_time_minutes);
        endMinute = AlertType.time2Minutes(at.end_time_minutes);
        alertReraise = at.minutes_between;
        if (uuid.equals(AlertType.LOW_ALERT_55)) {
            // This is the 55 alert, can not be edited
            alertText.setKeyListener(null);
            alertThreshold.setKeyListener(null);
            buttonalertMp3.setEnabled(false);
            checkboxAllDay.setEnabled(false);
            checkboxVibrate.setEnabled(false);
            checkboxAlertOverride.setEnabled(false);
            reraise.setEnabled(false);
        }
    }
    reraise.setText(String.valueOf(alertReraise));
    alertMp3File.setKeyListener(null);
    viewHeader.setText(status);
    setDefaultSnoozeSpinner(defaultSnooze);
    setPreSnoozeSpinner();
    enableAllDayControls();
    setDisabledView();
    enableVibrateControls();
}
Also used : AlertType(com.eveningoutpost.dexdrip.Models.AlertType) SharedPreferences(android.content.SharedPreferences) TextView(android.widget.TextView) Intent(android.content.Intent) Paint(android.graphics.Paint)

Aggregations

AlertType (com.eveningoutpost.dexdrip.Models.AlertType)24 ActiveBgAlert (com.eveningoutpost.dexdrip.Models.ActiveBgAlert)10 Date (java.util.Date)10 TextView (android.widget.TextView)4 DataMap (com.google.android.gms.wearable.DataMap)4 ArrayList (java.util.ArrayList)4 Dialog (android.app.Dialog)2 Intent (android.content.Intent)2 SharedPreferences (android.content.SharedPreferences)2 Paint (android.graphics.Paint)2 View (android.view.View)2 Button (android.widget.Button)2 NumberPicker (android.widget.NumberPicker)2 BgReading (com.eveningoutpost.dexdrip.Models.BgReading)2 Sensor (com.eveningoutpost.dexdrip.Models.Sensor)2 Gson (com.google.gson.Gson)2 GsonBuilder (com.google.gson.GsonBuilder)2 DateTypeAdapter (com.google.gson.internal.bind.DateTypeAdapter)2 HashMap (java.util.HashMap)2