Search in sources :

Example 1 with BgSparklineBuilder

use of com.eveningoutpost.dexdrip.UtilityModels.BgSparklineBuilder in project xDrip by NightscoutFoundation.

the class xDripWidget method displayCurrentInfo.

private static void displayCurrentInfo(AppWidgetManager appWidgetManager, int appWidgetId, Context context, RemoteViews views) {
    BgGraphBuilder bgGraphBuilder = new BgGraphBuilder(context);
    BgReading lastBgreading = BgReading.lastNoSenssor();
    final boolean showLines = Pref.getBoolean("widget_range_lines", false);
    final boolean showExstraStatus = Pref.getBoolean("extra_status_line", false) && Pref.getBoolean("widget_status_line", false);
    if (lastBgreading != null) {
        double estimate = 0;
        double estimated_delta = -9999;
        try {
            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).setBackgroundColor(ColorCache.getCol(ColorCache.X.color_widget_chart_background)).setHeight(height).setWidth(width).showHighLine(showLines).showLowLine(showLines).build());
            final BestGlucose.DisplayGlucose dg = (use_best_glucose) ? BestGlucose.getDisplayGlucose() : null;
            estimate = (dg != null) ? dg.mgdl : lastBgreading.calculated_value;
            String extrastring = "";
            String slope_arrow = (dg != null) ? dg.delta_arrow : lastBgreading.slopeArrow();
            String stringEstimate;
            if (dg == null) {
                // if not using best glucose helper
                if (BestGlucose.compensateNoise()) {
                    // this needs scaling based on noise intensity
                    estimate = BgGraphBuilder.best_bg_estimate;
                    estimated_delta = BgGraphBuilder.best_bg_estimate - BgGraphBuilder.last_bg_estimate;
                    // delta by minute
                    slope_arrow = BgReading.slopeToArrowSymbol(estimated_delta / (BgGraphBuilder.DEXCOM_PERIOD / 60000));
                    // currentBgValueText.setTypeface(null, Typeface.ITALIC);
                    // warning symbol !
                    extrastring = " \u26A0";
                }
                // TODO functionize this check as it is in multiple places
                if (Pref.getBooleanDefaultFalse("display_glucose_from_plugin") && (PluggableCalibration.getCalibrationPluginFromPreferences() != null)) {
                    extrastring += " " + context.getString(R.string.p_in_circle);
                }
            } else {
                // TODO make a couple of getters in dg for these functions
                extrastring = " " + dg.extra_string + ((dg.from_plugin) ? " " + context.getString(R.string.p_in_circle) : "");
                estimated_delta = dg.delta_mgdl;
                // TODO properly illustrate + standardize warning level
                if (dg.warning > 1)
                    slope_arrow = "";
            }
            // TODO use dg stale calculation and/or preformatted text
            if ((new Date().getTime()) - Home.stale_data_millis() - lastBgreading.timestamp > 0) {
                // estimate = lastBgreading.calculated_value;
                Log.d(TAG, "old value, estimate " + estimate);
                stringEstimate = bgGraphBuilder.unitized_string(estimate);
                // views.setTextViewText(R.id.widgetArrow, "--");
                slope_arrow = "--";
                views.setInt(R.id.widgetBg, "setPaintFlags", Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
            } else {
                // estimate = lastBgreading.calculated_value;
                stringEstimate = bgGraphBuilder.unitized_string(estimate);
                if (lastBgreading.hide_slope) {
                    slope_arrow = "--";
                }
                Log.d(TAG, "newish value, estimate " + stringEstimate + slope_arrow);
                views.setInt(R.id.widgetBg, "setPaintFlags", 0);
            }
            if (Sensor.isActive() || Home.get_follower()) {
                views.setTextViewText(R.id.widgetBg, stringEstimate);
                views.setTextViewText(R.id.widgetArrow, slope_arrow);
            } else {
                views.setTextViewText(R.id.widgetBg, "");
                views.setTextViewText(R.id.widgetArrow, "");
            }
            // is it really necessary to read this data once here and again in unitizedDeltaString?
            // couldn't we just use the unitizedDeltaString to detect the error condition?
            List<BgReading> bgReadingList = BgReading.latest(2, Home.get_follower());
            if (estimated_delta == -9999) {
                // use original delta
                if (bgReadingList != null && bgReadingList.size() == 2) {
                    views.setTextViewText(R.id.widgetDelta, bgGraphBuilder.unitizedDeltaString(true, true, Home.get_follower()));
                } else {
                    views.setTextViewText(R.id.widgetDelta, "--");
                }
            } else {
                // use compensated estimate
                views.setTextViewText(R.id.widgetDelta, bgGraphBuilder.unitizedDeltaStringRaw(true, true, estimated_delta));
            }
            // TODO use dg preformatted localized string
            int timeAgo = (int) Math.floor((new Date().getTime() - lastBgreading.timestamp) / (1000 * 60));
            if (timeAgo == 1) {
                views.setTextViewText(R.id.readingAge, timeAgo + " Minute ago" + extrastring);
            } else {
                views.setTextViewText(R.id.readingAge, timeAgo + " Minutes ago" + extrastring);
            }
            if (timeAgo > 15) {
                views.setTextColor(R.id.readingAge, Color.parseColor("#FFBB33"));
            } else {
                views.setTextColor(R.id.readingAge, Color.WHITE);
            }
            if (showExstraStatus) {
                views.setTextViewText(R.id.widgetStatusLine, Home.extraStatusLine());
                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);
            }
        } catch (RuntimeException e) {
            Log.e(TAG, "Got exception in displaycurrentinfo: " + e);
        }
    }
}
Also used : BgSparklineBuilder(com.eveningoutpost.dexdrip.UtilityModels.BgSparklineBuilder) BgReading(com.eveningoutpost.dexdrip.Models.BgReading) Paint(android.graphics.Paint) Date(java.util.Date) BgGraphBuilder(com.eveningoutpost.dexdrip.UtilityModels.BgGraphBuilder)

Example 2 with BgSparklineBuilder

use of com.eveningoutpost.dexdrip.UtilityModels.BgSparklineBuilder in project xDrip by NightscoutFoundation.

the class PebbleDisplayTrend method sendTrendToPebble.

public void sendTrendToPebble() {
    // create a sparkline bitmap to send to the pebble
    Log.i(TAG, "sendTrendToPebble called: sendStep= " + sendStep + ", messageInTransit= " + messageInTransit + ", transactionFailed= " + transactionFailed + ", sendStep= " + sendStep);
    if (!done && (sendStep == 1 && ((!messageInTransit && !transactionOk && !transactionFailed) || (messageInTransit && !transactionOk && transactionFailed)))) {
        if (!messageInTransit && !transactionOk && !transactionFailed) {
            if (!PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean("pebble_display_trend", false)) {
                sendStep = 5;
                transactionFailed = false;
                transactionOk = false;
                done = true;
                current_size = 0;
                buff = null;
            }
            boolean highLine = PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean("pebble_high_line", false);
            boolean lowLine = PreferenceManager.getDefaultSharedPreferences(mContext).getBoolean("pebble_low_line", false);
            String trendPeriodString = PreferenceManager.getDefaultSharedPreferences(mContext).getString("pebble_trend_period", "3");
            Integer trendPeriod = Integer.parseInt(trendPeriodString);
            if ((trendPeriod != lastTrendPeriod) || (JoH.ratelimit("pebble-bggraphbuilder", 60))) {
                long end = System.currentTimeMillis() + (60000 * 5);
                long start = end - (60000 * 60 * trendPeriod) - (60000 * 10);
                this.bgGraphBuilder = new BgGraphBuilder(context, start, end, NUM_VALUES, true);
                lastTrendPeriod = trendPeriod;
            }
            Log.d(TAG, "sendTrendToPebble: highLine is " + highLine + ", lowLine is " + lowLine + ",trendPeriod is " + trendPeriod);
            // encode the trend bitmap as a PNG
            int depth = 16;
            Bitmap bgTrend;
            if (pebble_platform == 0) {
                Log.d(TAG, "sendTrendToPebble: Encoding trend as Monochrome.");
                depth = 2;
                bgTrend = new BgSparklineBuilder(mContext).setBgGraphBuilder(bgGraphBuilder).setStart(System.currentTimeMillis() - 60000 * 60 * trendPeriod).setEnd(System.currentTimeMillis()).setHeightPx(63).setWidthPx(84).showHighLine(highLine).showLowLine(lowLine).setTinyDots(true).setSmallDots(false).build();
            } else {
                bgTrend = new BgSparklineBuilder(mContext).setBgGraphBuilder(bgGraphBuilder).setStart(System.currentTimeMillis() - 60000 * 60 * trendPeriod).setEnd(System.currentTimeMillis()).setHeightPx(84).setWidthPx(144).showHighLine(highLine).showLowLine(lowLine).setTinyDots().setSmallDots().build();
            }
            byte[] img = SimpleImageEncoder.encodeBitmapAsPNG(bgTrend, true, depth, true);
            image_size = img.length;
            buff = ByteBuffer.wrap(img);
            bgTrend.recycle();
            // Prepare the TREND_BEGIN_KEY dictionary.  We expect the length of the image to always be less than 65535 bytes.
            if (buff != null) {
                if (dictionary == null) {
                    dictionary = new PebbleDictionary();
                }
                dictionary.addInt16(TREND_BEGIN_KEY, (short) image_size);
                Log.d(TAG, "sendTrendToPebble: Sending TREND_BEGIN_KEY to pebble, image size is " + image_size);
            } else {
                Log.d(TAG, "sendTrendToPebble: Error converting stream to ByteBuffer, buff is null.");
                sendStep = 4;
                return;
            }
        }
        transactionFailed = false;
        transactionOk = false;
        messageInTransit = true;
        sendDataToPebble(this.dictionary);
    }
    if (sendStep == 1 && !done && !messageInTransit && transactionOk && !transactionFailed) {
        Log.i(TAG, "sendTrendToPebble: sendStep " + sendStep + " complete.");
        dictionary.remove(TREND_BEGIN_KEY);
        current_size = 0;
        sendStep = 2;
        transactionOk = false;
    }
    if (!done && ((sendStep == 2 && !messageInTransit) || sendStep == 3 && transactionFailed)) {
        if (!transactionFailed && !messageInTransit) {
            // send image chunks to Pebble.
            Log.d(TAG, "sendTrendToPebble: current_size is " + current_size + ", image_size is " + image_size);
            if (current_size < image_size) {
                dictionary.remove(TREND_DATA_KEY);
                if ((image_size <= (current_size + CHUNK_SIZE))) {
                    chunk = new byte[image_size - current_size];
                    Log.d(TAG, "sendTrendToPebble: sending chunk of size " + (image_size - current_size));
                    buff.get(chunk, 0, image_size - current_size);
                    sendStep = 3;
                } else {
                    chunk = new byte[CHUNK_SIZE];
                    Log.d(TAG, "sendTrendToPebble: sending chunk of size " + CHUNK_SIZE);
                    buff.get(chunk, 0, CHUNK_SIZE);
                    current_size += CHUNK_SIZE;
                }
                dictionary.addBytes(TREND_DATA_KEY, chunk);
            }
        }
        Log.d(TAG, "sendTrendToPebble: Sending TREND_DATA_KEY to pebble, current_size is " + current_size);
        transactionFailed = false;
        transactionOk = false;
        messageInTransit = true;
        sendDataToPebble(this.dictionary);
    }
    if (sendStep == 3 && !done && !messageInTransit && transactionOk && !transactionFailed) {
        Log.i(TAG, "sendTrendToPebble: sendStep " + sendStep + " complete.");
        dictionary.remove(TREND_DATA_KEY);
        sendStep = 4;
        transactionOk = false;
        buff = null;
        stream = null;
    }
    if (!done && (sendStep == 4 && ((!messageInTransit && !transactionOk && !transactionFailed) || (messageInTransit && !transactionOk && transactionFailed)))) {
        if (!transactionFailed) {
            // prepare the TREND_END_KEY dictionary and send it.
            dictionary.addUint8(TREND_END_KEY, (byte) 0);
            Log.d(TAG, "sendTrendToPebble: Sending TREND_END_KEY to pebble.");
        }
        transactionFailed = false;
        transactionOk = false;
        messageInTransit = true;
        sendDataToPebble(this.dictionary);
    }
    if (sendStep == 4 && !done && transactionOk && !messageInTransit && !transactionFailed) {
        Log.i(TAG, "sendTrendToPebble: sendStep " + sendStep + " complete.");
        dictionary.remove(TREND_END_KEY);
        sendStep = 5;
        transactionFailed = false;
        transactionOk = false;
        done = true;
        current_size = 0;
        buff = null;
    }
}
Also used : Bitmap(android.graphics.Bitmap) BgSparklineBuilder(com.eveningoutpost.dexdrip.UtilityModels.BgSparklineBuilder) PebbleDictionary(com.getpebble.android.kit.util.PebbleDictionary) BgGraphBuilder(com.eveningoutpost.dexdrip.UtilityModels.BgGraphBuilder)

Example 3 with BgSparklineBuilder

use of com.eveningoutpost.dexdrip.UtilityModels.BgSparklineBuilder in project xDrip-plus by jamorham.

the class PebbleDisplayTrendOld method sendTrendToPebble.

private synchronized void sendTrendToPebble(boolean clearTrend) {
    // create a sparkline bitmap to send to the pebble
    final Bitmap blankTrend;
    if (clearTrend) {
        blankTrend = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
        Log.d(TAG, "Attempting to blank trend");
    } else {
        blankTrend = null;
        didTrend = true;
    }
    Log.i(TAG, // 
    "sendTrendToPebble called: sendStep= " + sendStep + ", messageInTransit= " + messageInTransit + ", transactionFailed= " + transactionFailed + ", sendStep= " + sendStep);
    if (!done && (sendStep == 1 && (// 
    (!messageInTransit && !transactionOk && !transactionFailed) || (messageInTransit && !transactionOk && transactionFailed)))) {
        if (!messageInTransit && !transactionOk && !transactionFailed) {
            if (!clearTrend && (!doWeDisplayTrendData())) {
                sendStep = 5;
                transactionFailed = false;
                transactionOk = false;
                done = true;
                current_size = 0;
                buff = null;
            }
            boolean highLine = getBooleanValue("pebble_high_line");
            boolean lowLine = getBooleanValue("pebble_low_line");
            String trendPeriodString = PreferenceManager.getDefaultSharedPreferences(this.context).getString("pebble_trend_period", "3");
            Integer trendPeriod = Integer.parseInt(trendPeriodString);
            if ((trendPeriod != lastTrendPeriod) || (JoH.ratelimit("pebble-bggraphbuilder", 60))) {
                long end = System.currentTimeMillis() + (60000 * 5);
                long start = end - (60000 * 60 * trendPeriod) - (60000 * 10);
                this.bgGraphBuilder = new BgGraphBuilder(context, start, end, NUM_VALUES, true);
                lastTrendPeriod = trendPeriod;
            }
            Log.d(TAG, "sendTrendToPebble: highLine is " + highLine + ", lowLine is " + lowLine + ",trendPeriod is " + trendPeriod);
            Bitmap bgTrend = new BgSparklineBuilder(this.context).setBgGraphBuilder(this.bgGraphBuilder).setStart(System.currentTimeMillis() - 60000 * 60 * trendPeriod).setEnd(System.currentTimeMillis()).setHeightPx(// 84
            PebbleUtil.pebbleDisplayType == PebbleDisplayType.TrendClassic ? 63 : 84).setWidthPx(// 144
            PebbleUtil.pebbleDisplayType == PebbleDisplayType.TrendClassic ? 84 : 144).showHighLine(highLine).showLowLine(lowLine).setTinyDots(Pref.getBooleanDefaultFalse("pebble_tiny_dots")).setShowFiltered(Pref.getBooleanDefaultFalse("pebble_filtered_line")).build();
            // encode the trend bitmap as a PNG
            final byte[] img = SimpleImageEncoder.encodeBitmapAsPNG(clearTrend ? blankTrend : bgTrend, true, PebbleUtil.pebbleDisplayType == PebbleDisplayType.TrendClassic ? 2 : 16, true);
            if (debugPNG) {
                try {
                    // save debug image output
                    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("/sdcard/download/xdrip-trend-debug.png"));
                    bos.write(img);
                    bos.flush();
                    bos.close();
                } catch (FileNotFoundException e) {
                } catch (IOException e) {
                }
                // also save full colour
                final byte[] img2 = SimpleImageEncoder.encodeBitmapAsPNG(bgTrend, true, 16, true);
                try {
                    // save debug image output
                    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("/sdcard/download/xdrip-trend-debug-colour.png"));
                    bos.write(img2);
                    bos.flush();
                    bos.close();
                } catch (FileNotFoundException e) {
                } catch (IOException e) {
                }
            }
            image_size = img.length;
            buff = ByteBuffer.wrap(img);
            bgTrend.recycle();
            // Prepare the TREND_BEGIN_KEY dictionary.  We expect the length of the image to always be less than 65535 bytes.
            if (buff != null) {
                // if (this.dictionary == null) {
                // this.dictionary = new PebbleDictionary();
                // }
                this.dictionary.addInt16(TREND_BEGIN_KEY, (short) image_size);
                Log.d(TAG, "sendTrendToPebble: Sending TREND_BEGIN_KEY to pebble, image size is " + image_size);
            } else {
                Log.d(TAG, "sendTrendToPebble: Error converting stream to ByteBuffer, buff is null.");
                sendStep = 4;
                return;
            }
        }
        transactionFailed = false;
        transactionOk = false;
        messageInTransit = true;
        sendDataToPebble(this.dictionary);
    }
    if (sendStep == 1 && !done && !messageInTransit && transactionOk && !transactionFailed) {
        Log.i(TAG, "sendTrendToPebble: sendStep " + sendStep + " complete.");
        this.dictionary.remove(TREND_BEGIN_KEY);
        current_size = 0;
        sendStep = 2;
        transactionOk = false;
    }
    if (!done && ((sendStep == 2 && !messageInTransit) || sendStep == 3 && transactionFailed)) {
        if (!transactionFailed && !messageInTransit) {
            // send image chunks to Pebble.
            if (d)
                Log.d(TAG, "sendTrendToPebble: current_size is " + current_size + ", image_size is " + image_size);
            if (current_size < image_size) {
                this.dictionary.remove(TREND_DATA_KEY);
                if ((image_size <= (current_size + CHUNK_SIZE))) {
                    chunk = new byte[image_size - current_size];
                    if (d)
                        Log.d(TAG, "sendTrendToPebble: sending chunk of size " + (image_size - current_size));
                    buff.get(chunk, 0, image_size - current_size);
                    sendStep = 3;
                } else {
                    chunk = new byte[CHUNK_SIZE];
                    if (d)
                        Log.d(TAG, "sendTrendToPebble: sending chunk of size " + CHUNK_SIZE);
                    buff.get(chunk, 0, CHUNK_SIZE);
                    current_size += CHUNK_SIZE;
                }
                this.dictionary.addBytes(TREND_DATA_KEY, chunk);
            }
        }
        Log.d(TAG, "sendTrendToPebble: Sending TREND_DATA_KEY to pebble, current_size is " + current_size);
        transactionFailed = false;
        transactionOk = false;
        messageInTransit = true;
        sendDataToPebble(this.dictionary);
    }
    if (sendStep == 3 && !done && !messageInTransit && transactionOk && !transactionFailed) {
        Log.i(TAG, "sendTrendToPebble: sendStep " + sendStep + " complete.");
        this.dictionary.remove(TREND_DATA_KEY);
        sendStep = 4;
        transactionOk = false;
        buff = null;
    // stream = null;
    }
    if (!done && (sendStep == 4 && (// 
    (!messageInTransit && !transactionOk && !transactionFailed) || (messageInTransit && !transactionOk && transactionFailed)))) {
        if (!transactionFailed) {
            // prepare the TREND_END_KEY dictionary and send it.
            this.dictionary.addUint8(TREND_END_KEY, (byte) 0);
            Log.d(TAG, "sendTrendToPebble: Sending TREND_END_KEY to pebble.");
        }
        transactionFailed = false;
        transactionOk = false;
        messageInTransit = true;
        sendDataToPebble(this.dictionary);
    }
    if (sendStep == 4 && !done && transactionOk && !messageInTransit && !transactionFailed) {
        Log.i(TAG, "sendTrendToPebble: sendStep " + sendStep + " complete.");
        this.dictionary.remove(TREND_END_KEY);
        sendStep = 5;
        transactionFailed = false;
        transactionOk = false;
        done = true;
        current_size = 0;
        buff = null;
        // cleared
        if (clearTrend)
            didTrend = false;
    }
}
Also used : Bitmap(android.graphics.Bitmap) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) BgSparklineBuilder(com.eveningoutpost.dexdrip.UtilityModels.BgSparklineBuilder) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) BgGraphBuilder(com.eveningoutpost.dexdrip.UtilityModels.BgGraphBuilder)

Example 4 with BgSparklineBuilder

use of com.eveningoutpost.dexdrip.UtilityModels.BgSparklineBuilder in project xDrip-plus by jamorham.

the class xDripWidget method displayCurrentInfo.

private static void displayCurrentInfo(AppWidgetManager appWidgetManager, int appWidgetId, Context context, RemoteViews views) {
    BgGraphBuilder bgGraphBuilder = new BgGraphBuilder(context);
    BgReading lastBgreading = BgReading.lastNoSenssor();
    final boolean showLines = Pref.getBoolean("widget_range_lines", false);
    final boolean showExstraStatus = Pref.getBoolean("extra_status_line", false) && Pref.getBoolean("widget_status_line", false);
    if (lastBgreading != null) {
        double estimate = 0;
        double estimated_delta = -9999;
        try {
            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).setBackgroundColor(ColorCache.getCol(ColorCache.X.color_widget_chart_background)).setHeight(height).setWidth(width).showHighLine(showLines).showLowLine(showLines).build());
            final BestGlucose.DisplayGlucose dg = (use_best_glucose) ? BestGlucose.getDisplayGlucose() : null;
            estimate = (dg != null) ? dg.mgdl : lastBgreading.calculated_value;
            String extrastring = "";
            String slope_arrow = (dg != null) ? dg.delta_arrow : lastBgreading.slopeArrow();
            String stringEstimate;
            if (dg == null) {
                // if not using best glucose helper
                if (BestGlucose.compensateNoise()) {
                    // this needs scaling based on noise intensity
                    estimate = BgGraphBuilder.best_bg_estimate;
                    estimated_delta = BgGraphBuilder.best_bg_estimate - BgGraphBuilder.last_bg_estimate;
                    // delta by minute
                    slope_arrow = BgReading.slopeToArrowSymbol(estimated_delta / (BgGraphBuilder.DEXCOM_PERIOD / 60000));
                    // currentBgValueText.setTypeface(null, Typeface.ITALIC);
                    // warning symbol !
                    extrastring = " \u26A0";
                }
                // TODO functionize this check as it is in multiple places
                if (Pref.getBooleanDefaultFalse("display_glucose_from_plugin") && (PluggableCalibration.getCalibrationPluginFromPreferences() != null)) {
                    extrastring += " " + context.getString(R.string.p_in_circle);
                }
            } else {
                // TODO make a couple of getters in dg for these functions
                extrastring = " " + dg.extra_string + ((dg.from_plugin) ? " " + context.getString(R.string.p_in_circle) : "");
                estimated_delta = dg.delta_mgdl;
                // TODO properly illustrate + standardize warning level
                if (dg.warning > 1)
                    slope_arrow = "";
            }
            // TODO use dg stale calculation and/or preformatted text
            if ((new Date().getTime()) - Home.stale_data_millis() - lastBgreading.timestamp > 0) {
                // estimate = lastBgreading.calculated_value;
                Log.d(TAG, "old value, estimate " + estimate);
                stringEstimate = bgGraphBuilder.unitized_string(estimate);
                // views.setTextViewText(R.id.widgetArrow, "--");
                slope_arrow = "--";
                views.setInt(R.id.widgetBg, "setPaintFlags", Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
            } else {
                // estimate = lastBgreading.calculated_value;
                stringEstimate = bgGraphBuilder.unitized_string(estimate);
                if (lastBgreading.hide_slope) {
                    slope_arrow = "--";
                }
                Log.d(TAG, "newish value, estimate " + stringEstimate + slope_arrow);
                views.setInt(R.id.widgetBg, "setPaintFlags", 0);
            }
            if (Sensor.isActive() || Home.get_follower()) {
                views.setTextViewText(R.id.widgetBg, stringEstimate);
                views.setTextViewText(R.id.widgetArrow, slope_arrow);
            } else {
                views.setTextViewText(R.id.widgetBg, "");
                views.setTextViewText(R.id.widgetArrow, "");
            }
            // is it really necessary to read this data once here and again in unitizedDeltaString?
            // couldn't we just use the unitizedDeltaString to detect the error condition?
            List<BgReading> bgReadingList = BgReading.latest(2, Home.get_follower());
            if (estimated_delta == -9999) {
                // use original delta
                if (bgReadingList != null && bgReadingList.size() == 2) {
                    views.setTextViewText(R.id.widgetDelta, bgGraphBuilder.unitizedDeltaString(true, true, Home.get_follower()));
                } else {
                    views.setTextViewText(R.id.widgetDelta, "--");
                }
            } else {
                // use compensated estimate
                views.setTextViewText(R.id.widgetDelta, bgGraphBuilder.unitizedDeltaStringRaw(true, true, estimated_delta));
            }
            // TODO use dg preformatted localized string
            int timeAgo = (int) Math.floor((new Date().getTime() - lastBgreading.timestamp) / (1000 * 60));
            if (timeAgo == 1) {
                views.setTextViewText(R.id.readingAge, timeAgo + " Minute ago" + extrastring);
            } else {
                views.setTextViewText(R.id.readingAge, timeAgo + " Minutes ago" + extrastring);
            }
            if (timeAgo > 15) {
                views.setTextColor(R.id.readingAge, Color.parseColor("#FFBB33"));
            } else {
                views.setTextColor(R.id.readingAge, Color.WHITE);
            }
            if (showExstraStatus) {
                views.setTextViewText(R.id.widgetStatusLine, Home.extraStatusLine());
                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);
            }
        } catch (RuntimeException e) {
            Log.e(TAG, "Got exception in displaycurrentinfo: " + e);
        }
    }
}
Also used : BgSparklineBuilder(com.eveningoutpost.dexdrip.UtilityModels.BgSparklineBuilder) BgReading(com.eveningoutpost.dexdrip.Models.BgReading) Paint(android.graphics.Paint) Date(java.util.Date) BgGraphBuilder(com.eveningoutpost.dexdrip.UtilityModels.BgGraphBuilder)

Example 5 with BgSparklineBuilder

use of com.eveningoutpost.dexdrip.UtilityModels.BgSparklineBuilder in project xDrip by NightscoutFoundation.

the class PebbleDisplayTrendOld method sendTrendToPebble.

private synchronized void sendTrendToPebble(boolean clearTrend) {
    // create a sparkline bitmap to send to the pebble
    final Bitmap blankTrend;
    if (clearTrend) {
        blankTrend = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
        Log.d(TAG, "Attempting to blank trend");
    } else {
        blankTrend = null;
        didTrend = true;
    }
    Log.i(TAG, // 
    "sendTrendToPebble called: sendStep= " + sendStep + ", messageInTransit= " + messageInTransit + ", transactionFailed= " + transactionFailed + ", sendStep= " + sendStep);
    if (!done && (sendStep == 1 && (// 
    (!messageInTransit && !transactionOk && !transactionFailed) || (messageInTransit && !transactionOk && transactionFailed)))) {
        if (!messageInTransit && !transactionOk && !transactionFailed) {
            if (!clearTrend && (!doWeDisplayTrendData())) {
                sendStep = 5;
                transactionFailed = false;
                transactionOk = false;
                done = true;
                current_size = 0;
                buff = null;
            }
            boolean highLine = getBooleanValue("pebble_high_line");
            boolean lowLine = getBooleanValue("pebble_low_line");
            String trendPeriodString = PreferenceManager.getDefaultSharedPreferences(this.context).getString("pebble_trend_period", "3");
            Integer trendPeriod = Integer.parseInt(trendPeriodString);
            if ((trendPeriod != lastTrendPeriod) || (JoH.ratelimit("pebble-bggraphbuilder", 60))) {
                long end = System.currentTimeMillis() + (60000 * 5);
                long start = end - (60000 * 60 * trendPeriod) - (60000 * 10);
                this.bgGraphBuilder = new BgGraphBuilder(context, start, end, NUM_VALUES, true);
                lastTrendPeriod = trendPeriod;
            }
            Log.d(TAG, "sendTrendToPebble: highLine is " + highLine + ", lowLine is " + lowLine + ",trendPeriod is " + trendPeriod);
            Bitmap bgTrend = new BgSparklineBuilder(this.context).setBgGraphBuilder(this.bgGraphBuilder).setStart(System.currentTimeMillis() - 60000 * 60 * trendPeriod).setEnd(System.currentTimeMillis()).setHeightPx(// 84
            PebbleUtil.pebbleDisplayType == PebbleDisplayType.TrendClassic ? 63 : 84).setWidthPx(// 144
            PebbleUtil.pebbleDisplayType == PebbleDisplayType.TrendClassic ? 84 : 144).showHighLine(highLine).showLowLine(lowLine).setTinyDots(Pref.getBooleanDefaultFalse("pebble_tiny_dots")).setShowFiltered(Pref.getBooleanDefaultFalse("pebble_filtered_line")).build();
            // encode the trend bitmap as a PNG
            final byte[] img = SimpleImageEncoder.encodeBitmapAsPNG(clearTrend ? blankTrend : bgTrend, true, PebbleUtil.pebbleDisplayType == PebbleDisplayType.TrendClassic ? 2 : 16, true);
            if (debugPNG) {
                try {
                    // save debug image output
                    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("/sdcard/download/xdrip-trend-debug.png"));
                    bos.write(img);
                    bos.flush();
                    bos.close();
                } catch (FileNotFoundException e) {
                } catch (IOException e) {
                }
                // also save full colour
                final byte[] img2 = SimpleImageEncoder.encodeBitmapAsPNG(bgTrend, true, 16, true);
                try {
                    // save debug image output
                    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("/sdcard/download/xdrip-trend-debug-colour.png"));
                    bos.write(img2);
                    bos.flush();
                    bos.close();
                } catch (FileNotFoundException e) {
                } catch (IOException e) {
                }
            }
            image_size = img.length;
            buff = ByteBuffer.wrap(img);
            bgTrend.recycle();
            // Prepare the TREND_BEGIN_KEY dictionary.  We expect the length of the image to always be less than 65535 bytes.
            if (buff != null) {
                // if (this.dictionary == null) {
                // this.dictionary = new PebbleDictionary();
                // }
                this.dictionary.addInt16(TREND_BEGIN_KEY, (short) image_size);
                Log.d(TAG, "sendTrendToPebble: Sending TREND_BEGIN_KEY to pebble, image size is " + image_size);
            } else {
                Log.d(TAG, "sendTrendToPebble: Error converting stream to ByteBuffer, buff is null.");
                sendStep = 4;
                return;
            }
        }
        transactionFailed = false;
        transactionOk = false;
        messageInTransit = true;
        sendDataToPebble(this.dictionary);
    }
    if (sendStep == 1 && !done && !messageInTransit && transactionOk && !transactionFailed) {
        Log.i(TAG, "sendTrendToPebble: sendStep " + sendStep + " complete.");
        this.dictionary.remove(TREND_BEGIN_KEY);
        current_size = 0;
        sendStep = 2;
        transactionOk = false;
    }
    if (!done && ((sendStep == 2 && !messageInTransit) || sendStep == 3 && transactionFailed)) {
        if (!transactionFailed && !messageInTransit) {
            // send image chunks to Pebble.
            if (d)
                Log.d(TAG, "sendTrendToPebble: current_size is " + current_size + ", image_size is " + image_size);
            if (current_size < image_size) {
                this.dictionary.remove(TREND_DATA_KEY);
                if ((image_size <= (current_size + CHUNK_SIZE))) {
                    chunk = new byte[image_size - current_size];
                    if (d)
                        Log.d(TAG, "sendTrendToPebble: sending chunk of size " + (image_size - current_size));
                    buff.get(chunk, 0, image_size - current_size);
                    sendStep = 3;
                } else {
                    chunk = new byte[CHUNK_SIZE];
                    if (d)
                        Log.d(TAG, "sendTrendToPebble: sending chunk of size " + CHUNK_SIZE);
                    buff.get(chunk, 0, CHUNK_SIZE);
                    current_size += CHUNK_SIZE;
                }
                this.dictionary.addBytes(TREND_DATA_KEY, chunk);
            }
        }
        Log.d(TAG, "sendTrendToPebble: Sending TREND_DATA_KEY to pebble, current_size is " + current_size);
        transactionFailed = false;
        transactionOk = false;
        messageInTransit = true;
        sendDataToPebble(this.dictionary);
    }
    if (sendStep == 3 && !done && !messageInTransit && transactionOk && !transactionFailed) {
        Log.i(TAG, "sendTrendToPebble: sendStep " + sendStep + " complete.");
        this.dictionary.remove(TREND_DATA_KEY);
        sendStep = 4;
        transactionOk = false;
        buff = null;
    // stream = null;
    }
    if (!done && (sendStep == 4 && (// 
    (!messageInTransit && !transactionOk && !transactionFailed) || (messageInTransit && !transactionOk && transactionFailed)))) {
        if (!transactionFailed) {
            // prepare the TREND_END_KEY dictionary and send it.
            this.dictionary.addUint8(TREND_END_KEY, (byte) 0);
            Log.d(TAG, "sendTrendToPebble: Sending TREND_END_KEY to pebble.");
        }
        transactionFailed = false;
        transactionOk = false;
        messageInTransit = true;
        sendDataToPebble(this.dictionary);
    }
    if (sendStep == 4 && !done && transactionOk && !messageInTransit && !transactionFailed) {
        Log.i(TAG, "sendTrendToPebble: sendStep " + sendStep + " complete.");
        this.dictionary.remove(TREND_END_KEY);
        sendStep = 5;
        transactionFailed = false;
        transactionOk = false;
        done = true;
        current_size = 0;
        buff = null;
        // cleared
        if (clearTrend)
            didTrend = false;
    }
}
Also used : Bitmap(android.graphics.Bitmap) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) BgSparklineBuilder(com.eveningoutpost.dexdrip.UtilityModels.BgSparklineBuilder) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) BgGraphBuilder(com.eveningoutpost.dexdrip.UtilityModels.BgGraphBuilder)

Aggregations

BgGraphBuilder (com.eveningoutpost.dexdrip.UtilityModels.BgGraphBuilder)6 BgSparklineBuilder (com.eveningoutpost.dexdrip.UtilityModels.BgSparklineBuilder)6 Bitmap (android.graphics.Bitmap)4 Paint (android.graphics.Paint)2 BgReading (com.eveningoutpost.dexdrip.Models.BgReading)2 PebbleDictionary (com.getpebble.android.kit.util.PebbleDictionary)2 BufferedOutputStream (java.io.BufferedOutputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 Date (java.util.Date)2