Search in sources :

Example 11 with AlertType

use of com.eveningoutpost.dexdrip.Models.AlertType in project xDrip by NightscoutFoundation.

the class ListenerService method syncAlertTypeData.

private void syncAlertTypeData(DataMap dataMap, Context context) {
    // KS
    Log.d(TAG, "syncAlertTypeData");
    ArrayList<DataMap> entries = dataMap.getDataMapArrayList("entries");
    if (entries != null) {
        Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().registerTypeAdapter(Date.class, new DateTypeAdapter()).serializeSpecialFloatingPointValues().create();
        Log.d(TAG, "syncAlertTypeData add AlertType Table entries count=" + entries.size());
        // ensure database has already been initialized
        Sensor.InitDb(context);
        AlertType.remove_all();
        for (DataMap entry : entries) {
            if (entry != null) {
                String alertrecord = entry.getString("alert");
                if (alertrecord != null) {
                    AlertType data = gson.fromJson(alertrecord, AlertType.class);
                    AlertType exists = AlertType.get_alert(data.uuid);
                    if (exists != null) {
                        Log.d(TAG, "syncAlertTypeData AlertType exists for uuid=" + data.uuid + " name=" + data.name);
                        exists.name = data.name;
                        exists.active = data.active;
                        exists.volume = data.volume;
                        exists.vibrate = data.vibrate;
                        exists.light = data.light;
                        exists.override_silent_mode = data.override_silent_mode;
                        exists.predictive = data.predictive;
                        exists.time_until_threshold_crossed = data.time_until_threshold_crossed;
                        exists.above = data.above;
                        exists.threshold = data.threshold;
                        exists.all_day = data.all_day;
                        exists.start_time_minutes = data.start_time_minutes;
                        exists.end_time_minutes = data.end_time_minutes;
                        exists.minutes_between = data.minutes_between;
                        exists.default_snooze = data.default_snooze;
                        exists.text = data.text;
                        exists.mp3_file = data.mp3_file;
                        exists.save();
                    } else {
                        data.save();
                        Log.d(TAG, "syncAlertTypeData AlertType does not exist for uuid=" + data.uuid);
                    }
                    exists = AlertType.get_alert(data.uuid);
                    if (exists != null)
                        Log.d(TAG, "syncAlertTypeData AlertType GSON saved BG: " + exists.toS());
                    else
                        Log.d(TAG, "syncAlertTypeData AlertType GSON NOT saved");
                }
            }
        }
    }
}
Also used : AlertType(com.eveningoutpost.dexdrip.Models.AlertType) DateTypeAdapter(com.google.gson.internal.bind.DateTypeAdapter) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) DataMap(com.google.android.gms.wearable.DataMap)

Example 12 with AlertType

use of com.eveningoutpost.dexdrip.Models.AlertType in project xDrip by NightscoutFoundation.

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)

Example 13 with AlertType

use of com.eveningoutpost.dexdrip.Models.AlertType in project xDrip by NightscoutFoundation.

the class AlertPlayer method ClockTick.

// Check the state and alrarm if needed
public void ClockTick(Context ctx, boolean trendingToAlertEnd, String bgValue) {
    if (trendingToAlertEnd) {
        Log.d(TAG, "ClockTick: This alert is trending to it's end will not do anything");
        return;
    }
    ActiveBgAlert activeBgAlert = ActiveBgAlert.getOnly();
    if (activeBgAlert == null) {
        // Nothing to do ...
        return;
    }
    if (activeBgAlert.ready_to_alarm()) {
        // also don't cancel notification
        stopAlert(ctx, false, false, false);
        int timeFromStartPlaying = activeBgAlert.getUpdatePlayTime();
        AlertType alert = AlertType.get_alert(activeBgAlert.alert_uuid);
        if (alert == null) {
            Log.d(TAG, "ClockTick: The alert was already deleted... will not play");
            ActiveBgAlert.ClearData();
            return;
        }
        Log.d(TAG, "ClockTick: Playing the alert again");
        long nextAlertTime = alert.getNextAlertTime(ctx);
        activeBgAlert.updateNextAlertAt(nextAlertTime);
        Vibrate(ctx, alert, bgValue, alert.override_silent_mode, timeFromStartPlaying);
    }
}
Also used : AlertType(com.eveningoutpost.dexdrip.Models.AlertType) ActiveBgAlert(com.eveningoutpost.dexdrip.Models.ActiveBgAlert)

Example 14 with AlertType

use of com.eveningoutpost.dexdrip.Models.AlertType in project xDrip by NightscoutFoundation.

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 15 with AlertType

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

the class ListenerService method syncAlertTypeData.

private void syncAlertTypeData(DataMap dataMap, Context context) {
    // KS
    Log.d(TAG, "syncAlertTypeData");
    ArrayList<DataMap> entries = dataMap.getDataMapArrayList("entries");
    if (entries != null) {
        Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().registerTypeAdapter(Date.class, new DateTypeAdapter()).serializeSpecialFloatingPointValues().create();
        Log.d(TAG, "syncAlertTypeData add AlertType Table entries count=" + entries.size());
        // ensure database has already been initialized
        Sensor.InitDb(context);
        AlertType.remove_all();
        for (DataMap entry : entries) {
            if (entry != null) {
                String alertrecord = entry.getString("alert");
                if (alertrecord != null) {
                    AlertType data = gson.fromJson(alertrecord, AlertType.class);
                    AlertType exists = AlertType.get_alert(data.uuid);
                    if (exists != null) {
                        Log.d(TAG, "syncAlertTypeData AlertType exists for uuid=" + data.uuid + " name=" + data.name);
                        exists.name = data.name;
                        exists.active = data.active;
                        exists.volume = data.volume;
                        exists.vibrate = data.vibrate;
                        exists.light = data.light;
                        exists.override_silent_mode = data.override_silent_mode;
                        exists.predictive = data.predictive;
                        exists.time_until_threshold_crossed = data.time_until_threshold_crossed;
                        exists.above = data.above;
                        exists.threshold = data.threshold;
                        exists.all_day = data.all_day;
                        exists.start_time_minutes = data.start_time_minutes;
                        exists.end_time_minutes = data.end_time_minutes;
                        exists.minutes_between = data.minutes_between;
                        exists.default_snooze = data.default_snooze;
                        exists.text = data.text;
                        exists.mp3_file = data.mp3_file;
                        exists.save();
                    } else {
                        data.save();
                        Log.d(TAG, "syncAlertTypeData AlertType does not exist for uuid=" + data.uuid);
                    }
                    exists = AlertType.get_alert(data.uuid);
                    if (exists != null)
                        Log.d(TAG, "syncAlertTypeData AlertType GSON saved BG: " + exists.toS());
                    else
                        Log.d(TAG, "syncAlertTypeData AlertType GSON NOT saved");
                }
            }
        }
    }
}
Also used : AlertType(com.eveningoutpost.dexdrip.Models.AlertType) DateTypeAdapter(com.google.gson.internal.bind.DateTypeAdapter) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) DataMap(com.google.android.gms.wearable.DataMap)

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