Java Code Examples for com.eveningoutpost.dexdrip.Models.BgReading#latest()

The following examples show how to use com.eveningoutpost.dexdrip.Models.BgReading#latest() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BgGraphBuilder.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
public static String unitizedDeltaString(boolean showUnit, boolean highGranularity, boolean is_follower, boolean doMgdl) {

        List<BgReading> last2 = BgReading.latest(2,is_follower);
        if (last2.size() < 2 || last2.get(0).timestamp - last2.get(1).timestamp > 20 * 60 * 1000) {
            // don't show delta if there are not enough values or the values are more than 20 mintes apart
            return "???";
        }

        double value = BgReading.currentSlope(is_follower) * 5 * 60 * 1000;

        return unitizedDeltaStringRaw(showUnit, highGranularity, value, doMgdl);
    }
 
Example 2
Source File: Unitized.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
public static String unitizedDeltaString(boolean showUnit, boolean highGranularity, boolean is_follower, boolean doMgdl) {

        List<BgReading> last2 = BgReading.latest(2,is_follower);
        if (last2.size() < 2 || last2.get(0).timestamp - last2.get(1).timestamp > 20 * 60 * 1000) {
            // don't show delta if there are not enough values or the values are more than 20 mintes apart
            return "???";
        }

        double value = BgReading.currentSlope(is_follower) * 5 * 60 * 1000;

        return unitizedDeltaStringRaw(showUnit, highGranularity, value, doMgdl);
    }
 
Example 3
Source File: Unitized.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
public static String unitizedDeltaString(boolean showUnit, boolean highGranularity, boolean is_follower, boolean doMgdl) {

        List<BgReading> last2 = BgReading.latest(2,is_follower);
        if (last2.size() < 2 || last2.get(0).timestamp - last2.get(1).timestamp > 20 * 60 * 1000) {
            // don't show delta if there are not enough values or the values are more than 20 mintes apart
            return "???";
        }

        double value = BgReading.currentSlope(is_follower) * 5 * 60 * 1000;

        return unitizedDeltaStringRaw(showUnit, highGranularity, value, doMgdl);
    }
 
Example 4
Source File: BgGraphBuilder.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
public static String unitizedDeltaString(boolean showUnit, boolean highGranularity, boolean is_follower, boolean doMgdl) {

        List<BgReading> last2 = BgReading.latest(2,is_follower);
        if (last2.size() < 2 || last2.get(0).timestamp - last2.get(1).timestamp > 20 * 60 * 1000) {
            // don't show delta if there are not enough values or the values are more than 20 mintes apart
            return "???";
        }

        double value = BgReading.currentSlope(is_follower) * 5 * 60 * 1000;

       return unitizedDeltaStringRaw(showUnit, highGranularity, value, doMgdl);
    }
 
Example 5
Source File: BgGraphBuilder.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
public static String unitizedDeltaString(boolean showUnit, boolean highGranularity, boolean is_follower, boolean doMgdl) {

        List<BgReading> last2 = BgReading.latest(2,is_follower);
        if (last2.size() < 2 || last2.get(0).timestamp - last2.get(1).timestamp > 20 * 60 * 1000) {
            // don't show delta if there are not enough values or the values are more than 20 mintes apart
            return "???";
        }

        double value = BgReading.currentSlope(is_follower) * 5 * 60 * 1000;

       return unitizedDeltaStringRaw(showUnit, highGranularity, value, doMgdl);
    }
 
Example 6
Source File: Unitized.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
public static String unitizedDeltaString(boolean showUnit, boolean highGranularity, boolean is_follower, boolean doMgdl) {

        List<BgReading> last2 = BgReading.latest(2,is_follower);
        if (last2.size() < 2 || last2.get(0).timestamp - last2.get(1).timestamp > 20 * 60 * 1000) {
            // don't show delta if there are not enough values or the values are more than 20 mintes apart
            return "???";
        }

        double value = BgReading.currentSlope(is_follower) * 5 * 60 * 1000;

        return unitizedDeltaStringRaw(showUnit, highGranularity, value, doMgdl);
    }
 
Example 7
Source File: BgGraphBuilder.java    From xDrip-Experimental with GNU General Public License v3.0 4 votes vote down vote up
public String unitizedDeltaString(boolean showUnit, boolean highGranularity) {

        List<BgReading> last2 = BgReading.latest(2);
        if(last2.size() < 2 || last2.get(0).timestamp - last2.get(1).timestamp > MAX_SLOPE_MINUTES * 60 * 1000){
            // don't show delta if there are not enough values or the values are more than 20 mintes apart
            return "???";
        }

        double value = BgReading.currentSlope() * 5*60*1000;

        if(Math.abs(value) > 100){
            // a delta > 100 will not happen with real BG values -> problematic sensor data
            return "ERR";
        }

        // TODO: allow localization from os settings once pebble doesn't require english locale
        DecimalFormat df = new DecimalFormat("#", new DecimalFormatSymbols(Locale.ENGLISH));
        String delta_sign = "";
        if (value > 0) { delta_sign = "+"; }
        if(doMgdl) {

            if(highGranularity){
                df.setMaximumFractionDigits(1);
            } else {
                df.setMaximumFractionDigits(0);
            }

            return delta_sign + df.format(unitized(value)) +  (showUnit?" mg/dl":"");
        } else {

            if(highGranularity){
                df.setMaximumFractionDigits(2);
            } else {
                df.setMaximumFractionDigits(1);
            }

            df.setMinimumFractionDigits(1);
            df.setMinimumIntegerDigits(1);
            return delta_sign + df.format(unitized(value)) + (showUnit?" mmol/l":"");
        }
    }
 
Example 8
Source File: xDripWidget.java    From xDrip-Experimental with GNU General Public License v3.0 4 votes vote down vote up
private static void displayCurrentInfo(AppWidgetManager appWidgetManager, int appWidgetId, Context context, RemoteViews views) {
    BgGraphBuilder bgGraphBuilder = new BgGraphBuilder(context);
    BgReading lastBgreading = BgReading.lastNoSenssor();

    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
    boolean showLines = settings.getBoolean("widget_range_lines", false);

    if (lastBgreading != null) {
        double estimate = 0;
        int height = appWidgetManager.getAppWidgetOptions(appWidgetId).getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT);
        int width = appWidgetManager.getAppWidgetOptions(appWidgetId).getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH);
        views.setImageViewBitmap(R.id.widgetGraph, new BgSparklineBuilder(context)
                .setBgGraphBuilder(bgGraphBuilder)
                .setHeight(height).setWidth(width).showHighLine(showLines).showLowLine(showLines).build());

        if ((new Date().getTime()) - (60000 * 11) - lastBgreading.timestamp > 0) {
            estimate = lastBgreading.calculated_value;
            Log.d(TAG, "old value, estimate " + estimate);
            views.setTextViewText(R.id.widgetBg, bgGraphBuilder.unitized_string(estimate));
            views.setTextViewText(R.id.widgetArrow, "--");
            views.setInt(R.id.widgetBg, "setPaintFlags", Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
        } else {
            estimate = lastBgreading.calculated_value;
            String stringEstimate = bgGraphBuilder.unitized_string(estimate);
            String slope_arrow = lastBgreading.slopeArrow();
            if (lastBgreading.hide_slope) {
                slope_arrow = "--";
            }
            Log.d(TAG, "newish value, estimate " + stringEstimate + slope_arrow);
            views.setTextViewText(R.id.widgetBg, stringEstimate);
            views.setTextViewText(R.id.widgetArrow, slope_arrow);
            views.setInt(R.id.widgetBg, "setPaintFlags", 0);
        }
        List<BgReading> bgReadingList =  BgReading.latest(2);
        if(bgReadingList != null && bgReadingList.size() == 2) {

            views.setTextViewText(R.id.widgetDelta, bgGraphBuilder.unitizedDeltaString(true, true));
        } else {
            views.setTextViewText(R.id.widgetDelta, "--");
        }
        int timeAgo =(int) Math.floor((new Date().getTime() - lastBgreading.timestamp)/(1000*60));
        if (timeAgo == 1) {
            views.setTextViewText(R.id.readingAge, timeAgo + " Minute ago");
        } else {
            views.setTextViewText(R.id.readingAge, timeAgo + " Minutes ago");
        }
        if (timeAgo > 15) {
            views.setTextColor(R.id.readingAge, Color.parseColor("#FFBB33"));
        } else {
            views.setTextColor(R.id.readingAge, Color.WHITE);
        }

        if(settings.getBoolean("extra_status_line", false) && settings.getBoolean("widget_status_line", false)) {
            views.setTextViewText(R.id.widgetStatusLine, Home.extraStatusLine(settings));
            views.setViewVisibility(R.id.widgetStatusLine, View.VISIBLE);
        } else {
            views.setTextViewText(R.id.widgetStatusLine, "");
            views.setViewVisibility(R.id.widgetStatusLine, View.GONE);
        }

        if (bgGraphBuilder.unitized(estimate) <= bgGraphBuilder.lowMark) {
            views.setTextColor(R.id.widgetBg, Color.parseColor("#C30909"));
            views.setTextColor(R.id.widgetDelta, Color.parseColor("#C30909"));
            views.setTextColor(R.id.widgetArrow, Color.parseColor("#C30909"));
        } else if (bgGraphBuilder.unitized(estimate) >= bgGraphBuilder.highMark) {
            views.setTextColor(R.id.widgetBg, Color.parseColor("#FFBB33"));
            views.setTextColor(R.id.widgetDelta, Color.parseColor("#FFBB33"));
            views.setTextColor(R.id.widgetArrow, Color.parseColor("#FFBB33"));
        } else {
            views.setTextColor(R.id.widgetBg, Color.WHITE);
            views.setTextColor(R.id.widgetDelta, Color.WHITE);
            views.setTextColor(R.id.widgetArrow, Color.WHITE);
        }
    }
}
 
Example 9
Source File: xDripWidget.java    From xDrip with GNU General Public License v3.0 4 votes vote down vote up
public static void displayCurrentInfo() {
    BgGraphBuilder bgGraphBuilder = new BgGraphBuilder(mContext);
    BgReading lastBgreading = BgReading.lastNoSenssor();
    if (lastBgreading != null) {
        double estimate = 0;
        if ((new Date().getTime()) - (60000 * 11) - lastBgreading.timestamp > 0) {
            estimate = lastBgreading.calculated_value;
            Log.d(TAG, "old value, estimate " + estimate);
            views.setTextViewText(R.id.widgetBg, bgGraphBuilder.unitized_string(estimate));
            views.setTextViewText(R.id.widgetArrow, "--");
            views.setInt(R.id.widgetBg, "setPaintFlags", Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
        } else {
            estimate = lastBgreading.calculated_value;
            String stringEstimate = bgGraphBuilder.unitized_string(estimate);
            String slope_arrow = BgReading.slopeArrow((lastBgreading.calculated_value_slope * 60000));
            if (lastBgreading.hide_slope) {
                slope_arrow = "--";
            }
            Log.d(TAG, "newish value, estimate " + stringEstimate + slope_arrow);
            views.setTextViewText(R.id.widgetBg, stringEstimate);
            views.setTextViewText(R.id.widgetArrow, slope_arrow);
            views.setInt(R.id.widgetBg, "setPaintFlags", 0);
        }
        List<BgReading> bgReadingList =  BgReading.latest(2);
        if(bgReadingList != null && bgReadingList.size() == 2) {

            views.setTextViewText(R.id.widgetDelta, bgGraphBuilder.unitizedDeltaString(lastBgreading.calculated_value - bgReadingList.get(1).calculated_value));
        } else {
            views.setTextViewText(R.id.widgetDelta, "--");
        }
        int timeAgo =(int) Math.floor((new Date().getTime() - lastBgreading.timestamp)/(1000*60));
        if (timeAgo == 1) {
            views.setTextViewText(R.id.readingAge, timeAgo + " Minute ago");
        } else {
            views.setTextViewText(R.id.readingAge, timeAgo + " Minutes ago");
        }
        if (timeAgo > 15) {
            views.setTextColor(R.id.readingAge, Color.parseColor("#FFBB33"));
        } else {
            views.setTextColor(R.id.readingAge, Color.WHITE);
        }

        if (bgGraphBuilder.unitized(estimate) <= bgGraphBuilder.lowMark) {
            views.setTextColor(R.id.widgetBg, Color.parseColor("#C30909"));
            views.setTextColor(R.id.widgetDelta, Color.parseColor("#C30909"));
            views.setTextColor(R.id.widgetArrow, Color.parseColor("#C30909"));
        } else if (bgGraphBuilder.unitized(estimate) >= bgGraphBuilder.highMark) {
            views.setTextColor(R.id.widgetBg, Color.parseColor("#FFBB33"));
            views.setTextColor(R.id.widgetDelta, Color.parseColor("#FFBB33"));
            views.setTextColor(R.id.widgetArrow, Color.parseColor("#FFBB33"));
        } else {
            views.setTextColor(R.id.widgetBg, Color.WHITE);
            views.setTextColor(R.id.widgetDelta, Color.WHITE);
            views.setTextColor(R.id.widgetArrow, Color.WHITE);
        }
    }
}
 
Example 10
Source File: BgReadingTable.java    From xDrip-plus with GNU General Public License v3.0 4 votes vote down vote up
private void getData() {
    final List<BgReading> latest = BgReading.latest(5000);
    ListAdapter adapter = new BgReadingAdapter(this, latest);

    this.setListAdapter(adapter);
}
 
Example 11
Source File: PersistentHigh.java    From xDrip-plus with GNU General Public License v3.0 4 votes vote down vote up
public static boolean checkForPersistentHigh() {

        // skip if not enabled
        if (!Pref.getBooleanDefaultFalse("persistent_high_alert_enabled")) return false;


        final List<BgReading> last = BgReading.latest(1);
        if ((last != null) && (last.size() > 0)) {

            final double highMarkMgDl = Home.convertToMgDlIfMmol(
                    JoH.tolerantParseDouble(Pref.getString("highValue", "170"), 170d));

            final long now = JoH.tsl();
            final long since = now - last.get(0).timestamp;
            // only process if last reading <10 mins
            if (since < MINUTE_IN_MS * 10) {
                // check if exceeding high
                if (last.get(0).getDg_mgdl() > highMarkMgDl) {

                    final double this_slope = last.get(0).getDg_slope() * MINUTE_IN_MS;
                    Log.d(TAG, "CheckForPersistentHigh: Slope: " + JoH.qs(this_slope)+ " "+JoH.dateTimeText(last.get(0).timestamp));

                    // if not falling
                    if (this_slope > 0 && !last.get(0).hide_slope) {
                        final long high_since = Pref.getLong(PERSISTENT_HIGH_SINCE, 0);
                        if (high_since == 0) {
                            // no previous persistent high so set start as now
                            Pref.setLong(PERSISTENT_HIGH_SINCE, now);
                            Log.d(TAG, "Registering start of persistent high at time now");
                        } else {
                            final long high_for_mins = (now - high_since) / MINUTE_IN_MS;
                            long threshold_mins;
                            try {
                                threshold_mins = Long.parseLong(Pref.getString("persistent_high_threshold_mins", "60"));
                            } catch (NumberFormatException e) {
                                threshold_mins = 60;
                                Home.toaststaticnext("Invalid persistent high for longer than minutes setting: using 60 mins instead");
                            }
                            if (high_for_mins > threshold_mins) {
                                // we have been high for longer than the threshold - raise alert

                                // except if alerts are disabled
                                if (Pref.getLong("alerts_disabled_until", 0) > new Date().getTime()) {
                                    Log.i(TAG, "checkforPersistentHigh: Notifications are currently disabled cannot alert!!");
                                    return false;
                                }

                                if (!dataQualityCheck(high_since, highMarkMgDl)) {
                                    Log.d(TAG, "Insufficient data quality to raise persistent high alert");
                                    return false;
                                }

                                Log.i(TAG, "Persistent high for: " + high_for_mins + " mins -> alerting");
                                Notifications.persistentHighAlert(xdrip.getAppContext(), true, xdrip.getAppContext().getString(R.string.persistent_high_for_greater_than) + (int) high_for_mins + xdrip.getAppContext().getString(R.string.space_mins));
                                return true;
                            } else {
                                Log.d(TAG, "Persistent high below time threshold at: " + high_for_mins);
                            }
                        }
                    }
                } else {
                    // not high - cancel any existing
                    if (Pref.getLong(PERSISTENT_HIGH_SINCE, 0) != 0) {
                        Log.i(TAG, "Cancelling previous persistent high as we are no longer high");
                        Pref.setLong(PERSISTENT_HIGH_SINCE, 0); // clear it
                        Notifications.persistentHighAlert(xdrip.getAppContext(), false, ""); // cancel it
                    }
                }
            }
        }
        return false; // actually we should probably return void as we do everything inside this method
    }
 
Example 12
Source File: Notifications.java    From xDrip-plus with GNU General Public License v3.0 4 votes vote down vote up
private boolean notificationSetter(Context context) {
    ReadPerfs(context);
    final long end = System.currentTimeMillis() + (60000 * 5);
    final long start = end - (60000 * 60 * 3) - (60000 * 10);
    BgGraphBuilder bgGraphBuilder = new BgGraphBuilder(context, start, end);
    //BgGraphBuilder bgGraphBuilder = new BgGraphBuilder(context);
    if (bg_ongoing && (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)) {
        bgOngoingNotification(bgGraphBuilder);
    }
    if (prefs.getLong("alerts_disabled_until", 0) > new Date().getTime()) {
        Log.d("NOTIFICATIONS", "Notifications are currently disabled!!");
        return false;
    }
    
    boolean unclearReading = BgReading.getAndRaiseUnclearReading(context);
    
    if (unclearReading) {
        AlertPlayer.getPlayer().stopAlert(context, false, true);
    } else {
        FileBasedNotifications(context);
        BgReading.checkForDropAllert(context);
        BgReading.checkForRisingAllert(context);
    }
    // TODO: Add this alerts as well to depend on unclear sensor reading.
    PersistentHigh.checkForPersistentHigh();
    //KS evaluateLowPredictionAlarm();
    //KS reportNoiseChanges();


    Sensor sensor = Sensor.currentSensor();
    // TODO need to check performance of rest of this method when in follower mode
    final List<BgReading> bgReadings = BgReading.latest(3);
    final List<Calibration> calibrations = Calibration.allForSensorLimited(3);
    if (bgReadings == null || bgReadings.size() < 3) {
        return unclearReading;
    }
    if (calibrations == null || calibrations.size() < 2) {
        return unclearReading;
    }
    BgReading bgReading = bgReadings.get(0);

    if (calibration_notifications) {
        // TODO this should only clear double calibration once after calibrations are achieved
        if (bgReadings.size() >= 3) {
            if (calibrations.size() == 0 && (new Date().getTime() - bgReadings.get(2).timestamp <= (60000 * 30)) && sensor != null) {
                if ((sensor.started_at + (60000 * 60 * 2)) < new Date().getTime()) {
                    doubleCalibrationRequest();
                } else {
                    // TODO should be aware of state
                    clearDoubleCalibrationRequest();
                }
            } else {
                clearDoubleCalibrationRequest();
            }
        } else {
            clearDoubleCalibrationRequest();
        }
        // bgreadings criteria possibly needs a review
        if (CalibrationRequest.shouldRequestCalibration(bgReading) && (new Date().getTime() - bgReadings.get(2).timestamp <= (60000 * 24))) {
            if ((!PowerStateReceiver.is_power_connected()) || (Pref.getBooleanDefaultFalse("calibration_alerts_while_charging"))) {
                if (JoH.pratelimit("calibration-request-notification", CALIBRATION_REQUEST_MAX_FREQUENCY)) {
                    extraCalibrationRequest();
                }
            }
        } else {
            // TODO should be aware of state
            clearExtraCalibrationRequest();
        }
        if (calibrations.size() >= 1 && (Math.abs((new Date().getTime() - calibrations.get(0).timestamp)) / (1000 * 60 * 60) > 12)
                && (CalibrationRequest.isSlopeFlatEnough(BgReading.last(true)))) {
            Log.d("NOTIFICATIONS", "Calibration difference in hours: " + ((new Date().getTime() - calibrations.get(0).timestamp)) / (1000 * 60 * 60));
            if ((!PowerStateReceiver.is_power_connected()) || (Pref.getBooleanDefaultFalse("calibration_alerts_while_charging"))) {
                // TODO check slope
                if (JoH.pratelimit("calibration-request-notification", CALIBRATION_REQUEST_MIN_FREQUENCY) || Pref.getBooleanDefaultFalse("calibration_alerts_repeat")) {
                    calibrationRequest();
                }
            }
        } else {
            // TODO should be aware of state
            clearCalibrationRequest();
        }

    } else {
        clearAllCalibrationNotifications();
    }
    return unclearReading;
}
 
Example 13
Source File: BgGraphBuilder.java    From xDrip-plus with GNU General Public License v3.0 4 votes vote down vote up
public String oldunitizedDeltaString(boolean showUnit, boolean highGranularity) {

        List<BgReading> last2 = BgReading.latest(2);
        if(last2.size() < 2 || last2.get(0).timestamp - last2.get(1).timestamp > MAX_SLOPE_MINUTES * 60 * 1000){
            // don't show delta if there are not enough values or the values are more than 20 mintes apart
            return "???";
        }

        double value = BgReading.currentSlope() * 5*60*1000;

        if(Math.abs(value) > 100){
            // a delta > 100 will not happen with real BG values -> problematic sensor data
            return "ERR";
        }

        // TODO: allow localization from os settings once pebble doesn't require english locale
        DecimalFormat df = new DecimalFormat("#", new DecimalFormatSymbols(Locale.ENGLISH));
        String delta_sign = "";
        if (value > 0) { delta_sign = "+"; }
        if(doMgdl) {

            if(highGranularity){
                df.setMaximumFractionDigits(1);
            } else {
                df.setMaximumFractionDigits(0);
            }

            return delta_sign + df.format(unitized(value)) +  (showUnit?" mg/dl":"");
        } else {

            if(highGranularity){
                df.setMaximumFractionDigits(2);
            } else {
                df.setMaximumFractionDigits(1);
            }

            df.setMinimumFractionDigits(1);
            df.setMinimumIntegerDigits(1);
            return delta_sign + df.format(unitized(value)) + (showUnit?" mmol/l":"");
        }
    }
 
Example 14
Source File: BgReadingTable.java    From xDrip with GNU General Public License v3.0 4 votes vote down vote up
private void getData() {
    final List<BgReading> latest = BgReading.latest(50);
    ListAdapter adapter = new BgReadingAdapter(this, latest);

    this.setListAdapter(adapter);
}
 
Example 15
Source File: Notifications.java    From xDrip-Experimental with GNU General Public License v3.0 4 votes vote down vote up
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public Notification createOngoingNotification(Context context) {
    mContext = context;
    long end = System.currentTimeMillis() + (60000 * 5);
    long start = end - (60000 * 60*3) -  (60000 * 10);
    BgGraphBuilder bgGraphBuilder = new BgGraphBuilder(mContext, start, end);
    ReadPerfs(mContext);
    Intent intent = new Intent(mContext, Home.class);
    List<BgReading> lastReadings = BgReading.latest(2);
    BgReading lastReading = null;
    if (lastReadings != null && lastReadings.size() >= 2) {
        lastReading = lastReadings.get(0);
    }

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(mContext);
    stackBuilder.addParentStack(Home.class);
    stackBuilder.addNextIntent(intent);
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(
                    0,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );

    NotificationCompat.Builder b = new NotificationCompat.Builder(mContext);
    //b.setOngoing(true);
    b.setCategory(NotificationCompat.CATEGORY_STATUS);
    String titleString = lastReading == null ? "BG Reading Unavailable" : (lastReading.displayValue(mContext) + " " + lastReading.slopeArrow());
    b.setContentTitle(titleString)
            .setContentText("xDrip Data collection service is running.")
            .setSmallIcon(R.drawable.ic_action_communication_invert_colors_on)
            .setUsesChronometer(false);
    if (lastReading != null) {
        b.setWhen(lastReading.timestamp);
        String deltaString = "Delta: " + bgGraphBuilder.unitizedDeltaString(true, true);
        b.setContentText(deltaString);
        iconBitmap = new BgSparklineBuilder(mContext)
                .setHeight(64)
                .setWidth(64)
                .setStart(System.currentTimeMillis() - 60000 * 60 * 3)
                .setBgGraphBuilder(bgGraphBuilder)
                .build();
        b.setLargeIcon(iconBitmap);
        NotificationCompat.BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle();
        notifiationBitmap = new BgSparklineBuilder(mContext)
                .setBgGraphBuilder(bgGraphBuilder)
                .showHighLine()
                .showLowLine()
                .build();
        bigPictureStyle.bigPicture(notifiationBitmap)
                .setSummaryText(deltaString)
                .setBigContentTitle(titleString);
        b.setStyle(bigPictureStyle);
    }
    b.setContentIntent(resultPendingIntent);
    return b.build();
}
 
Example 16
Source File: PersistentHigh.java    From xDrip with GNU General Public License v3.0 4 votes vote down vote up
public static boolean checkForPersistentHigh() {

        // skip if not enabled
        if (!Pref.getBooleanDefaultFalse("persistent_high_alert_enabled")) return false;


        final List<BgReading> last = BgReading.latest(1);
        if ((last != null) && (last.size() > 0)) {

            final double highMarkMgDl = Home.convertToMgDlIfMmol(
                    JoH.tolerantParseDouble(Pref.getString("highValue", "170"), 170d));

            final long now = JoH.tsl();
            final long since = now - last.get(0).timestamp;
            // only process if last reading <10 mins
            if (since < MINUTE_IN_MS * 10) {
                // check if exceeding high
                if (last.get(0).getDg_mgdl() > highMarkMgDl) {

                    final double this_slope = last.get(0).getDg_slope() * MINUTE_IN_MS;
                    Log.d(TAG, "CheckForPersistentHigh: Slope: " + JoH.qs(this_slope)+ " "+JoH.dateTimeText(last.get(0).timestamp));

                    // if not falling
                    if (this_slope > 0 && !last.get(0).hide_slope) {
                        final long high_since = Pref.getLong(PERSISTENT_HIGH_SINCE, 0);
                        if (high_since == 0) {
                            // no previous persistent high so set start as now
                            Pref.setLong(PERSISTENT_HIGH_SINCE, now);
                            Log.d(TAG, "Registering start of persistent high at time now");
                        } else {
                            final long high_for_mins = (now - high_since) / MINUTE_IN_MS;
                            long threshold_mins;
                            try {
                                threshold_mins = Long.parseLong(Pref.getString("persistent_high_threshold_mins", "60"));
                            } catch (NumberFormatException e) {
                                threshold_mins = 60;
                                Home.toaststaticnext("Invalid persistent high for longer than minutes setting: using 60 mins instead");
                            }
                            if (high_for_mins > threshold_mins) {
                                // we have been high for longer than the threshold - raise alert

                                // except if alerts are disabled
                                if (Pref.getLong("alerts_disabled_until", 0) > new Date().getTime()) {
                                    Log.i(TAG, "checkforPersistentHigh: Notifications are currently disabled cannot alert!!");
                                    return false;
                                }

                                if (!dataQualityCheck(high_since, highMarkMgDl)) {
                                    Log.d(TAG, "Insufficient data quality to raise persistent high alert");
                                    return false;
                                }

                                Log.i(TAG, "Persistent high for: " + high_for_mins + " mins -> alerting");
                                Notifications.persistentHighAlert(xdrip.getAppContext(), true, xdrip.getAppContext().getString(R.string.persistent_high_for_greater_than) + (int) high_for_mins + xdrip.getAppContext().getString(R.string.space_mins));
                                return true;
                            } else {
                                Log.d(TAG, "Persistent high below time threshold at: " + high_for_mins);
                            }
                        }
                    }
                } else {
                    // not high - cancel any existing
                    if (Pref.getLong(PERSISTENT_HIGH_SINCE, 0) != 0) {
                        Log.i(TAG, "Cancelling previous persistent high as we are no longer high");
                        Pref.setLong(PERSISTENT_HIGH_SINCE, 0); // clear it
                        Notifications.persistentHighAlert(xdrip.getAppContext(), false, ""); // cancel it
                    }
                }
            }
        }
        return false; // actually we should probably return void as we do everything inside this method
    }
 
Example 17
Source File: Notifications.java    From xDrip-Experimental with GNU General Public License v3.0 4 votes vote down vote up
private void notificationSetter(Context context, AtomicBoolean unclearReading) {
    ReadPerfs(context);
    if (bg_ongoing && (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)) {
        bgOngoingNotification();
    }
    if (prefs.getLong("alerts_disabled_until", 0) > new Date().getTime()) {
        Log.d("NOTIFICATIONS", "Notifications are currently disabled!!");
        return;
    }
    FileBasedNotifications(context, unclearReading);
    BgReading.checkForDropAllert(context);
    BgReading.checkForRisingAllert(context);

    Sensor sensor = Sensor.currentSensor();

    List<BgReading> bgReadings = BgReading.latest(3);
    List<Calibration> calibrations = Calibration.allForSensorInLastFourDays();
    if (bgReadings == null || bgReadings.size() < 3) {
        return;
    }
    if (calibrations == null || calibrations.size() < 2) {
        return;
    }
    BgReading bgReading = bgReadings.get(0);

    if (calibration_notifications) {
        if (bgReadings.size() >= 3) {
            if (calibrations.size() == 0 && (new Date().getTime() - bgReadings.get(2).timestamp <= (60000 * 30)) && sensor != null) {
                if ((sensor.started_at + (60000 * 60 * 2)) < new Date().getTime()) {
                    doubleCalibrationRequest();
                } else {
                    clearDoubleCalibrationRequest();
                }
            } else {
                clearDoubleCalibrationRequest();
            }
        } else {
            clearDoubleCalibrationRequest();
        }
        if (CalibrationRequest.shouldRequestCalibration(bgReading) && (new Date().getTime() - bgReadings.get(2).timestamp <= (60000 * 24))) {
            extraCalibrationRequest();
        } else {
            clearExtraCalibrationRequest();
        }
        if (calibrations.size() >= 1 && Math.abs((new Date().getTime() - calibrations.get(0).timestamp)) / (1000 * 60 * 60) > 12) {
            Log.d("NOTIFICATIONS", "Calibration difference in hours: " + ((new Date().getTime() - calibrations.get(0).timestamp)) / (1000 * 60 * 60));
            calibrationRequest();
        } else {
            clearCalibrationRequest();
        }

    } else {
        clearAllCalibrationNotifications();
    }
}
 
Example 18
Source File: PersistentHigh.java    From xDrip with GNU General Public License v3.0 4 votes vote down vote up
public static boolean checkForPersistentHigh() {

        // skip if not enabled
        if (!Pref.getBooleanDefaultFalse("persistent_high_alert_enabled")) return false;


        final List<BgReading> last = BgReading.latest(1);
        if ((last != null) && (last.size() > 0)) {

            final double highMarkMgDl = Home.convertToMgDlIfMmol(
                    JoH.tolerantParseDouble(Pref.getString("highValue", "170"), 170d));

            final long now = JoH.tsl();
            final long since = now - last.get(0).timestamp;
            // only process if last reading <10 mins
            if (since < MINUTE_IN_MS * 10) {
                // check if exceeding high
                if (last.get(0).getDg_mgdl() > highMarkMgDl) {

                    final double this_slope = last.get(0).getDg_slope() * MINUTE_IN_MS;
                    Log.d(TAG, "CheckForPersistentHigh: Slope: " + JoH.qs(this_slope)+ " "+JoH.dateTimeText(last.get(0).timestamp));

                    // if not falling
                    if (this_slope > 0 && !last.get(0).hide_slope) {
                        final long high_since = Pref.getLong(PERSISTENT_HIGH_SINCE, 0);
                        if (high_since == 0) {
                            // no previous persistent high so set start as now
                            Pref.setLong(PERSISTENT_HIGH_SINCE, now);
                            Log.d(TAG, "Registering start of persistent high at time now");
                        } else {
                            final long high_for_mins = (now - high_since) / MINUTE_IN_MS;
                            long threshold_mins;
                            try {
                                threshold_mins = Long.parseLong(Pref.getString("persistent_high_threshold_mins", "60"));
                            } catch (NumberFormatException e) {
                                threshold_mins = 60;
                                Home.toaststaticnext("Invalid persistent high for longer than minutes setting: using 60 mins instead");
                            }
                            if (high_for_mins > threshold_mins) {
                                // we have been high for longer than the threshold - raise alert

                                // except if alerts are disabled
                                if (Pref.getLong("alerts_disabled_until", 0) > new Date().getTime()) {
                                    Log.i(TAG, "checkforPersistentHigh: Notifications are currently disabled cannot alert!!");
                                    return false;
                                }

                                if (!dataQualityCheck(high_since, highMarkMgDl)) {
                                    Log.d(TAG, "Insufficient data quality to raise persistent high alert");
                                    return false;
                                }

                                Log.i(TAG, "Persistent high for: " + high_for_mins + " mins -> alerting");
                                Notifications.persistentHighAlert(xdrip.getAppContext(), true, xdrip.getAppContext().getString(R.string.persistent_high_for_greater_than) + (int) high_for_mins + xdrip.getAppContext().getString(R.string.space_mins));
                                return true;
                            } else {
                                Log.d(TAG, "Persistent high below time threshold at: " + high_for_mins);
                            }
                        }
                    }
                } else {
                    // not high - cancel any existing
                    if (Pref.getLong(PERSISTENT_HIGH_SINCE, 0) != 0) {
                        Log.i(TAG, "Cancelling previous persistent high as we are no longer high");
                        Pref.setLong(PERSISTENT_HIGH_SINCE, 0); // clear it
                        Notifications.persistentHighAlert(xdrip.getAppContext(), false, ""); // cancel it
                    }
                }
            }
        }
        return false; // actually we should probably return void as we do everything inside this method
    }
 
Example 19
Source File: Notifications.java    From xDrip with GNU General Public License v3.0 4 votes vote down vote up
private boolean notificationSetter(Context context) {
    ReadPerfs(context);
    final long end = System.currentTimeMillis() + (60000 * 5);
    final long start = end - (60000 * 60 * 3) - (60000 * 10);
    BgGraphBuilder bgGraphBuilder = new BgGraphBuilder(context, start, end);
    //BgGraphBuilder bgGraphBuilder = new BgGraphBuilder(context);
    if (bg_ongoing && (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)) {
        bgOngoingNotification(bgGraphBuilder);
    }
    if (prefs.getLong("alerts_disabled_until", 0) > new Date().getTime()) {
        Log.d("NOTIFICATIONS", "Notifications are currently disabled!!");
        return false;
    }
    
    boolean unclearReading = BgReading.getAndRaiseUnclearReading(context);
    
    if (unclearReading) {
        AlertPlayer.getPlayer().stopAlert(context, false, true);
    } else {
        FileBasedNotifications(context);
        BgReading.checkForDropAllert(context);
        BgReading.checkForRisingAllert(context);
    }
    // TODO: Add this alerts as well to depend on unclear sensor reading.
    PersistentHigh.checkForPersistentHigh();
    //KS evaluateLowPredictionAlarm();
    //KS reportNoiseChanges();


    Sensor sensor = Sensor.currentSensor();
    // TODO need to check performance of rest of this method when in follower mode
    final List<BgReading> bgReadings = BgReading.latest(3);
    final List<Calibration> calibrations = Calibration.allForSensorLimited(3);
    if (bgReadings == null || bgReadings.size() < 3) {
        return unclearReading;
    }
    if (calibrations == null || calibrations.size() < 2) {
        return unclearReading;
    }
    BgReading bgReading = bgReadings.get(0);

    if (calibration_notifications) {
        // TODO this should only clear double calibration once after calibrations are achieved
        if (bgReadings.size() >= 3) {
            if (calibrations.size() == 0 && (new Date().getTime() - bgReadings.get(2).timestamp <= (60000 * 30)) && sensor != null) {
                if ((sensor.started_at + (60000 * 60 * 2)) < new Date().getTime()) {
                    doubleCalibrationRequest();
                } else {
                    // TODO should be aware of state
                    clearDoubleCalibrationRequest();
                }
            } else {
                clearDoubleCalibrationRequest();
            }
        } else {
            clearDoubleCalibrationRequest();
        }
        // bgreadings criteria possibly needs a review
        if (CalibrationRequest.shouldRequestCalibration(bgReading) && (new Date().getTime() - bgReadings.get(2).timestamp <= (60000 * 24))) {
            if ((!PowerStateReceiver.is_power_connected()) || (Pref.getBooleanDefaultFalse("calibration_alerts_while_charging"))) {
                if (JoH.pratelimit("calibration-request-notification", CALIBRATION_REQUEST_MAX_FREQUENCY)) {
                    extraCalibrationRequest();
                }
            }
        } else {
            // TODO should be aware of state
            clearExtraCalibrationRequest();
        }
        if (calibrations.size() >= 1 && (Math.abs((new Date().getTime() - calibrations.get(0).timestamp)) / (1000 * 60 * 60) > 12)
                && (CalibrationRequest.isSlopeFlatEnough(BgReading.last(true)))) {
            Log.d("NOTIFICATIONS", "Calibration difference in hours: " + ((new Date().getTime() - calibrations.get(0).timestamp)) / (1000 * 60 * 60));
            if ((!PowerStateReceiver.is_power_connected()) || (Pref.getBooleanDefaultFalse("calibration_alerts_while_charging"))) {
                // TODO check slope
                if (JoH.pratelimit("calibration-request-notification", CALIBRATION_REQUEST_MIN_FREQUENCY) || Pref.getBooleanDefaultFalse("calibration_alerts_repeat")) {
                    calibrationRequest();
                }
            }
        } else {
            // TODO should be aware of state
            clearCalibrationRequest();
        }

    } else {
        clearAllCalibrationNotifications();
    }
    return unclearReading;
}
 
Example 20
Source File: BgGraphBuilder.java    From xDrip with GNU General Public License v3.0 4 votes vote down vote up
public String oldunitizedDeltaString(boolean showUnit, boolean highGranularity) {

        List<BgReading> last2 = BgReading.latest(2);
        if(last2.size() < 2 || last2.get(0).timestamp - last2.get(1).timestamp > MAX_SLOPE_MINUTES * 60 * 1000){
            // don't show delta if there are not enough values or the values are more than 20 mintes apart
            return "???";
        }

        double value = BgReading.currentSlope() * 5*60*1000;

        if(Math.abs(value) > 100){
            // a delta > 100 will not happen with real BG values -> problematic sensor data
            return "ERR";
        }

        // TODO: allow localization from os settings once pebble doesn't require english locale
        DecimalFormat df = new DecimalFormat("#", new DecimalFormatSymbols(Locale.ENGLISH));
        String delta_sign = "";
        if (value > 0) { delta_sign = "+"; }
        if(doMgdl) {

            if(highGranularity){
                df.setMaximumFractionDigits(1);
            } else {
                df.setMaximumFractionDigits(0);
            }

            return delta_sign + df.format(unitized(value)) +  (showUnit?" mg/dl":"");
        } else {

            if(highGranularity){
                df.setMaximumFractionDigits(2);
            } else {
                df.setMaximumFractionDigits(1);
            }

            df.setMinimumFractionDigits(1);
            df.setMinimumIntegerDigits(1);
            return delta_sign + df.format(unitized(value)) + (showUnit?" mmol/l":"");
        }
    }