From e920b6bc2d8590649c081fad52785c34e76af993 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Mon, 1 Aug 2022 16:40:52 +0200 Subject: [PATCH] Glunovo: catch SecurityException --- .../plugins/source/GlunovoPlugin.kt | 130 +++++++++--------- 1 file changed, 67 insertions(+), 63 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/source/GlunovoPlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/source/GlunovoPlugin.kt index 239a317781..edde142213 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/source/GlunovoPlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/source/GlunovoPlugin.kt @@ -89,80 +89,84 @@ class GlunovoPlugin @Inject constructor( private fun handleNewData() { if (!isEnabled()) return - context.contentResolver.query(contentUri, null, null, null, null)?.let { cr -> - val glucoseValues = mutableListOf() - val calibrations = mutableListOf() - cr.moveToFirst() + try { + context.contentResolver.query(contentUri, null, null, null, null)?.let { cr -> + val glucoseValues = mutableListOf() + val calibrations = mutableListOf() + cr.moveToFirst() - while (!cr.isAfterLast) { - val timestamp = cr.getLong(0) - val value = cr.getDouble(1) //value in mmol/l... - val curr = cr.getDouble(2) + while (!cr.isAfterLast) { + val timestamp = cr.getLong(0) + val value = cr.getDouble(1) //value in mmol/l... + val curr = cr.getDouble(2) - // bypass already processed - if (timestamp < sp.getLong(R.string.key_last_processed_glunovo_timestamp, 0L)) { - cr.moveToNext() - continue - } + // bypass already processed + if (timestamp < sp.getLong(R.string.key_last_processed_glunovo_timestamp, 0L)) { + cr.moveToNext() + continue + } - if (timestamp > dateUtil.now() || timestamp == 0L) { - aapsLogger.error(LTag.BGSOURCE, "Error in received data date/time $timestamp") - cr.moveToNext() - continue - } + if (timestamp > dateUtil.now() || timestamp == 0L) { + aapsLogger.error(LTag.BGSOURCE, "Error in received data date/time $timestamp") + cr.moveToNext() + continue + } - if (value < 2 || value > 25) { - aapsLogger.error(LTag.BGSOURCE, "Error in received data value (value out of bounds) $value") - cr.moveToNext() - continue - } + if (value < 2 || value > 25) { + aapsLogger.error(LTag.BGSOURCE, "Error in received data value (value out of bounds) $value") + cr.moveToNext() + continue + } - if (curr != 0.0) - glucoseValues += CgmSourceTransaction.TransactionGlucoseValue( - timestamp = timestamp, - value = value * Constants.MMOLL_TO_MGDL, - raw = 0.0, - noise = null, - trendArrow = GlucoseValue.TrendArrow.NONE, - sourceSensor = GlucoseValue.SourceSensor.GLUNOVO_NATIVE - ) - else - calibrations.add( - CgmSourceTransaction.Calibration( + if (curr != 0.0) + glucoseValues += CgmSourceTransaction.TransactionGlucoseValue( timestamp = timestamp, - value = value, - glucoseUnit = TherapyEvent.GlucoseUnit.MMOL + value = value * Constants.MMOLL_TO_MGDL, + raw = 0.0, + noise = null, + trendArrow = GlucoseValue.TrendArrow.NONE, + sourceSensor = GlucoseValue.SourceSensor.GLUNOVO_NATIVE ) - ) - sp.putLong(R.string.key_last_processed_glunovo_timestamp, timestamp) - cr.moveToNext() - } - cr.close() + else + calibrations.add( + CgmSourceTransaction.Calibration( + timestamp = timestamp, + value = value, + glucoseUnit = TherapyEvent.GlucoseUnit.MMOL + ) + ) + sp.putLong(R.string.key_last_processed_glunovo_timestamp, timestamp) + cr.moveToNext() + } + cr.close() - if (glucoseValues.isNotEmpty() || calibrations.isNotEmpty()) - repository.runTransactionForResult(CgmSourceTransaction(glucoseValues, calibrations, null)) - .doOnError { - aapsLogger.error(LTag.DATABASE, "Error while saving values from Glunovo App", it) - } - .blockingGet() - .also { savedValues -> - savedValues.inserted.forEach { - xDripBroadcast.send(it) - aapsLogger.debug(LTag.DATABASE, "Inserted bg $it") + if (glucoseValues.isNotEmpty() || calibrations.isNotEmpty()) + repository.runTransactionForResult(CgmSourceTransaction(glucoseValues, calibrations, null)) + .doOnError { + aapsLogger.error(LTag.DATABASE, "Error while saving values from Glunovo App", it) } - savedValues.calibrationsInserted.forEach { calibration -> - calibration.glucose?.let { glucosevalue -> - uel.log( - UserEntry.Action.CALIBRATION, - UserEntry.Sources.Dexcom, - ValueWithUnit.Timestamp(calibration.timestamp), - ValueWithUnit.TherapyEventType(calibration.type), - ValueWithUnit.fromGlucoseUnit(glucosevalue, calibration.glucoseUnit.toString) - ) + .blockingGet() + .also { savedValues -> + savedValues.inserted.forEach { + xDripBroadcast.send(it) + aapsLogger.debug(LTag.DATABASE, "Inserted bg $it") + } + savedValues.calibrationsInserted.forEach { calibration -> + calibration.glucose?.let { glucosevalue -> + uel.log( + UserEntry.Action.CALIBRATION, + UserEntry.Sources.Dexcom, + ValueWithUnit.Timestamp(calibration.timestamp), + ValueWithUnit.TherapyEventType(calibration.type), + ValueWithUnit.fromGlucoseUnit(glucosevalue, calibration.glucoseUnit.toString) + ) + } + aapsLogger.debug(LTag.DATABASE, "Inserted calibration $calibration") } - aapsLogger.debug(LTag.DATABASE, "Inserted calibration $calibration") } - } + } + } catch (e: SecurityException) { + aapsLogger.error(e.localizedMessage) } }