use of com.eveningoutpost.dexdrip.UtilityModels.BgSparklineBuilder in project xDrip-plus by jamorham.
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;
}
}
Aggregations