From 88a8d08f5657805c7c68f36505387fc9f13c77a6 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Thu, 11 Nov 2021 14:23:22 +0100 Subject: [PATCH] restore xdrip broadcast --- .../nsclient/services/NSClientService.kt | 75 ++---------------- .../nightscout/androidaps/services/Intents.kt | 7 +- .../androidaps/utils/XDripBroadcast.kt | 76 ++++++++++++++++++- 3 files changed, 81 insertions(+), 77 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/services/NSClientService.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/services/NSClientService.kt index 77a09e402e..5a5bda55a3 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/services/NSClientService.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/services/NSClientService.kt @@ -3,7 +3,6 @@ package info.nightscout.androidaps.plugins.general.nsclient.services import android.annotation.SuppressLint import android.content.Context import android.content.Intent -import android.content.pm.ResolveInfo import android.os.* import androidx.work.OneTimeWorkRequest import com.google.common.base.Charsets @@ -17,7 +16,6 @@ import info.nightscout.androidaps.events.EventConfigBuilderChange import info.nightscout.androidaps.events.EventPreferenceChange import info.nightscout.androidaps.interfaces.Config import info.nightscout.androidaps.interfaces.DataSyncSelector -import info.nightscout.androidaps.interfaces.PluginType import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.logging.LTag import info.nightscout.androidaps.plugins.bus.RxBus @@ -41,12 +39,12 @@ import info.nightscout.androidaps.plugins.general.overview.notifications.Notific import info.nightscout.androidaps.plugins.profile.local.LocalProfilePlugin import info.nightscout.androidaps.plugins.source.NSClientSourcePlugin.NSClientSourceWorker import info.nightscout.androidaps.receivers.DataWorker -import info.nightscout.androidaps.services.Intents import info.nightscout.androidaps.utils.DateUtil import info.nightscout.androidaps.utils.FabricPrivacy import info.nightscout.androidaps.utils.JsonHelper.safeGetString import info.nightscout.androidaps.utils.JsonHelper.safeGetStringAllowNull import info.nightscout.androidaps.utils.T.Companion.mins +import info.nightscout.androidaps.utils.XDripBroadcast import info.nightscout.androidaps.utils.buildHelper.BuildHelper import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.androidaps.utils.rx.AapsSchedulers @@ -80,6 +78,7 @@ class NSClientService : DaggerService() { @Inject lateinit var dataWorker: DataWorker @Inject lateinit var dataSyncSelector: DataSyncSelector @Inject lateinit var repository: AppRepository + @Inject lateinit var xDripBroadcast: XDripBroadcast companion object { @@ -476,15 +475,7 @@ class NSClientService : DaggerService() { OneTimeWorkRequest.Builder(LocalProfilePlugin.NSProfileWorker::class.java) .setInputData(dataWorker.storeInputData(profileStoreJson, null)) .build()) - if (sp.getBoolean(R.string.key_nsclient_localbroadcasts, false)) { - val bundle = Bundle() - bundle.putString("profile", profileStoreJson.toString()) - bundle.putBoolean("delta", isDelta) - val intent = Intent(Intents.ACTION_NEW_PROFILE) - intent.putExtras(bundle) - intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES) - broadcast(intent) - } + xDripBroadcast.sendProfile(profileStoreJson) } } if (data.has("treatments")) { @@ -502,18 +493,7 @@ class NSClientService : DaggerService() { OneTimeWorkRequest.Builder(NSClientAddUpdateWorker::class.java) .setInputData(dataWorker.storeInputData(addedOrUpdatedTreatments, null)) .build()) - if (sp.getBoolean(R.string.key_nsclient_localbroadcasts, false)) { - val splitted = splitArray(addedOrUpdatedTreatments) - for (part in splitted) { - val bundle = Bundle() - bundle.putString("treatments", part.toString()) - bundle.putBoolean("delta", isDelta) - val intent = Intent(Intents.ACTION_CHANGED_TREATMENT) - intent.putExtras(bundle) - intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES) - broadcast(intent) - } - } + xDripBroadcast.sendTreatments(addedOrUpdatedTreatments) } } if (data.has("devicestatus")) { @@ -550,18 +530,7 @@ class NSClientService : DaggerService() { dataWorker.enqueue(OneTimeWorkRequest.Builder(NSClientSourceWorker::class.java) .setInputData(dataWorker.storeInputData(sgvs, null)) .build()) - val splitted = splitArray(sgvs) - if (sp.getBoolean(R.string.key_nsclient_localbroadcasts, false)) { - for (part in splitted) { - val bundle = Bundle() - bundle.putString("sgvs", part.toString()) - bundle.putBoolean("delta", isDelta) - val intent = Intent(Intents.ACTION_NEW_SGV) - intent.putExtras(bundle) - intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES) - broadcast(intent) - } - } + xDripBroadcast.sendSgvs(sgvs) } rxBus.send(EventNSClientNewLog("LAST", dateUtil.dateAndTimeString(latestDateInReceivedData))) } catch (e: JSONException) { @@ -675,40 +644,6 @@ class NSClientService : DaggerService() { } } - private fun splitArray(array: JSONArray): List { - var ret: MutableList = ArrayList() - try { - val size = array.length() - var count = 0 - var newarr: JSONArray? = null - for (i in 0 until size) { - if (count == 0) { - if (newarr != null) ret.add(newarr) - newarr = JSONArray() - count = 20 - } - newarr?.put(array[i]) - --count - } - if (newarr != null && newarr.length() > 0) ret.add(newarr) - } catch (e: JSONException) { - aapsLogger.error("Unhandled exception", e) - ret = ArrayList() - ret.add(array) - } - return ret - } - - private fun broadcast(intent: Intent) { - val receivers: List = packageManager.queryBroadcastReceivers(intent, 0) - for (resolveInfo in receivers) - resolveInfo.activityInfo.packageName?.let { - intent.setPackage(it) - sendBroadcast(intent) - aapsLogger.debug(LTag.CORE, "Sending broadcast " + intent.action + " to: " + it) - } - } - init { if (handler == null) { val handlerThread = HandlerThread(NSClientService::class.java.simpleName + "Handler") diff --git a/app/src/main/java/info/nightscout/androidaps/services/Intents.kt b/app/src/main/java/info/nightscout/androidaps/services/Intents.kt index c52c68e4c6..bf3a8d3695 100644 --- a/app/src/main/java/info/nightscout/androidaps/services/Intents.kt +++ b/app/src/main/java/info/nightscout/androidaps/services/Intents.kt @@ -7,12 +7,11 @@ interface Intents { // AAPS -> Xdrip const val ACTION_NEW_TREATMENT = "info.nightscout.client.NEW_TREATMENT" - const val ACTION_CHANGED_TREATMENT = "info.nightscout.client.CHANGED_TREATMENT" - const val ACTION_REMOVED_TREATMENT = "info.nightscout.client.REMOVED_TREATMENT" const val ACTION_NEW_PROFILE = "info.nightscout.client.NEW_PROFILE" const val ACTION_NEW_SGV = "info.nightscout.client.NEW_SGV" - const val ACTION_NEW_MBG = "info.nightscout.client.NEW_MBG" - const val ACTION_NEW_CAL = "info.nightscout.client.NEW_CAL" + + // AAPS -> xDrip 640G mode + const val XDRIP_PLUS_NS_EMULATOR = "com.eveningoutpost.dexdrip.NS_EMULATOR" // xDrip -> AAPS const val ACTION_NEW_BG_ESTIMATE = "com.eveningoutpost.dexdrip.BgEstimate" diff --git a/app/src/main/java/info/nightscout/androidaps/utils/XDripBroadcast.kt b/app/src/main/java/info/nightscout/androidaps/utils/XDripBroadcast.kt index d669ef8bc3..98cf2c90f9 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/XDripBroadcast.kt +++ b/app/src/main/java/info/nightscout/androidaps/utils/XDripBroadcast.kt @@ -7,6 +7,7 @@ import info.nightscout.androidaps.R import info.nightscout.androidaps.database.entities.GlucoseValue import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.logging.LTag +import info.nightscout.androidaps.services.Intents import info.nightscout.androidaps.utils.sharedPreferences.SP import org.json.JSONArray import org.json.JSONException @@ -24,6 +25,7 @@ class XDripBroadcast @Inject constructor( private val sp: SP ) { + // sent in 640G mode fun send(glucoseValue: GlucoseValue) { if (sp.getBoolean(R.string.key_dexcomg5_xdripupload, false)) { val format = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.US) @@ -41,7 +43,7 @@ class XDripBroadcast @Inject constructor( bundle.putString("action", "add") bundle.putString("collection", "entries") bundle.putString("data", entriesBody.toString()) - val intent = Intent(XDRIP_PLUS_NS_EMULATOR) + val intent = Intent(Intents.XDRIP_PLUS_NS_EMULATOR) intent.putExtras(bundle).addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES) context.sendBroadcast(intent) val receivers = context.packageManager.queryBroadcastReceivers(intent, 0) @@ -57,7 +59,75 @@ class XDripBroadcast @Inject constructor( } } - companion object { - const val XDRIP_PLUS_NS_EMULATOR = "com.eveningoutpost.dexdrip.NS_EMULATOR" + // sent in NSClient dbaccess mode + fun sendProfile(profileStoreJson: JSONObject) { + if (sp.getBoolean(R.string.key_nsclient_localbroadcasts, false)) + broadcast( + Intent(Intents.ACTION_NEW_PROFILE).apply { + addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES) + putExtras(Bundle().apply { putString("profile", profileStoreJson.toString()) }) + } + ) + + } + + // sent in NSClient dbaccess mode + fun sendTreatments(addedOrUpdatedTreatments: JSONArray) { + if (sp.getBoolean(R.string.key_nsclient_localbroadcasts, false)) + splitArray(addedOrUpdatedTreatments).forEach { part -> + broadcast( + Intent(Intents.ACTION_NEW_TREATMENT).apply { + addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES) + putExtras(Bundle().apply { putString("treatments", part.toString()) }) + } + ) + } + } + + // sent in NSClient dbaccess mode + fun sendSgvs(sgvs: JSONArray) { + if (sp.getBoolean(R.string.key_nsclient_localbroadcasts, false)) + splitArray(sgvs).forEach { part -> + broadcast( + Intent(Intents.ACTION_NEW_SGV).apply { + addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES) + putExtras(Bundle().apply { putString("sgvs", part.toString()) }) + } + ) + } + } + + private fun splitArray(array: JSONArray): List { + var ret: MutableList = ArrayList() + try { + val size = array.length() + var count = 0 + var newarr: JSONArray? = null + for (i in 0 until size) { + if (count == 0) { + if (newarr != null) ret.add(newarr) + newarr = JSONArray() + count = 20 + } + newarr?.put(array[i]) + --count + } + if (newarr != null && newarr.length() > 0) ret.add(newarr) + } catch (e: JSONException) { + aapsLogger.error("Unhandled exception", e) + ret = ArrayList() + ret.add(array) + } + return ret + } + + private fun broadcast(intent: Intent) { + context.packageManager.queryBroadcastReceivers(intent, 0).forEach { resolveInfo -> + resolveInfo.activityInfo.packageName?.let { + intent.setPackage(it) + context.sendBroadcast(intent) + aapsLogger.debug(LTag.CORE, "Sending broadcast " + intent.action + " to: " + it) + } + } } } \ No newline at end of file