From 38100e758ee17aad3e258a642dbfdc4b15389636 Mon Sep 17 00:00:00 2001 From: AdrianLxM Date: Mon, 24 Jul 2017 01:50:27 +0200 Subject: [PATCH] nsclient treatments local --- app/src/main/AndroidManifest.xml | 3 - .../info/nightscout/androidaps/MainApp.java | 17 +++ .../broadcasts/BroadcastTreatment.java | 108 ++++++++++++++---- 3 files changed, 101 insertions(+), 27 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 74271acb90..25a88a5939 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -58,9 +58,6 @@ - - - diff --git a/app/src/main/java/info/nightscout/androidaps/MainApp.java b/app/src/main/java/info/nightscout/androidaps/MainApp.java index 9dff7b0b5a..dec4f1c932 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainApp.java +++ b/app/src/main/java/info/nightscout/androidaps/MainApp.java @@ -2,8 +2,10 @@ package info.nightscout.androidaps; import android.app.Application; import android.content.Intent; +import android.content.IntentFilter; import android.content.res.Resources; import android.support.annotation.Nullable; +import android.support.v4.content.LocalBroadcastManager; import com.crashlytics.android.Crashlytics; import com.crashlytics.android.answers.Answers; @@ -17,6 +19,7 @@ import org.slf4j.LoggerFactory; import java.util.ArrayList; +import info.nightscout.androidaps.Services.Intents; import info.nightscout.androidaps.db.DatabaseHelper; import info.nightscout.androidaps.interfaces.InsulinInterface; import info.nightscout.androidaps.interfaces.PluginBase; @@ -59,6 +62,7 @@ import info.nightscout.androidaps.plugins.SourceXdrip.SourceXdripPlugin; import info.nightscout.androidaps.plugins.Treatments.TreatmentsFragment; import info.nightscout.androidaps.plugins.Wear.WearFragment; import info.nightscout.androidaps.plugins.XDripStatusline.StatuslinePlugin; +import info.nightscout.androidaps.receivers.DataReceiver; import info.nightscout.androidaps.receivers.KeepAliveReceiver; import info.nightscout.utils.NSUpload; import io.fabric.sdk.android.Fabric; @@ -77,6 +81,9 @@ public class MainApp extends Application { private static ArrayList pluginsList = null; + private static DataReceiver dataReceiver = new DataReceiver(); + private LocalBroadcastManager lbm; + @Override public void onCreate() { super.onCreate(); @@ -92,6 +99,8 @@ public class MainApp extends Application { sInstance = this; sResources = getResources(); + registerLocalBroadcastReceiver(); + if (pluginsList == null) { pluginsList = new ArrayList<>(); // Register all tabs in app here @@ -155,6 +164,14 @@ public class MainApp extends Application { } }); t.start(); + + } + + private void registerLocalBroadcastReceiver() { + lbm = LocalBroadcastManager.getInstance(this); + lbm.registerReceiver(dataReceiver, new IntentFilter(Intents.ACTION_NEW_TREATMENT)); + lbm.registerReceiver(dataReceiver, new IntentFilter(Intents.ACTION_CHANGED_TREATMENT)); + lbm.registerReceiver(dataReceiver, new IntentFilter(Intents.ACTION_REMOVED_TREATMENT)); } private void startKeepAliveService() { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastTreatment.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastTreatment.java index 60540d7613..b6711b75e0 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastTreatment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/broadcasts/BroadcastTreatment.java @@ -5,6 +5,7 @@ import android.content.Intent; import android.content.pm.ResolveInfo; import android.os.Bundle; import android.os.TransactionTooLargeException; +import android.support.v4.content.LocalBroadcastManager; import org.json.JSONArray; import org.json.JSONException; @@ -15,9 +16,11 @@ import org.slf4j.LoggerFactory; import java.util.ArrayList; import java.util.List; +import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.Services.Intents; import info.nightscout.androidaps.plugins.NSClientInternal.data.NSTreatment; import info.nightscout.utils.SP; +import info.nightscout.utils.ToastUtils; /** * Created by mike on 20.02.2016. @@ -27,21 +30,27 @@ public class BroadcastTreatment { public static void handleNewTreatment(NSTreatment treatment, Context context, boolean isDelta) { - if(!SP.getBoolean("nsclient_localbroadcasts", true)) return; - Bundle bundle = new Bundle(); bundle.putString("treatment", treatment.getData().toString()); bundle.putBoolean("delta", isDelta); Intent intent = new Intent(Intents.ACTION_NEW_TREATMENT); intent.putExtras(bundle); intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); - context.sendBroadcast(intent); + LocalBroadcastManager.getInstance(MainApp.instance()).sendBroadcast(intent); + + if(SP.getBoolean("nsclient_localbroadcasts", true)) { + bundle = new Bundle(); + bundle.putString("treatment", treatment.getData().toString()); + bundle.putBoolean("delta", isDelta); + intent = new Intent(Intents.ACTION_NEW_TREATMENT); + intent.putExtras(bundle); + intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); + context.sendBroadcast(intent); + } } public static void handleNewTreatment(JSONArray treatments, Context context, boolean isDelta) { - if(!SP.getBoolean("nsclient_localbroadcasts", true)) return; - List splitted = splitArray(treatments); for (JSONArray part: splitted) { Bundle bundle = new Bundle(); @@ -50,42 +59,73 @@ public class BroadcastTreatment { Intent intent = new Intent(Intents.ACTION_NEW_TREATMENT); intent.putExtras(bundle); intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); - context.sendBroadcast(intent); + LocalBroadcastManager.getInstance(MainApp.instance()).sendBroadcast(intent); + } + + if(SP.getBoolean("nsclient_localbroadcasts", true)){ + splitted = splitArray(treatments); + for (JSONArray part: splitted) { + Bundle bundle = new Bundle(); + bundle.putString("treatments", part.toString()); + bundle.putBoolean("delta", isDelta); + Intent intent = new Intent(Intents.ACTION_NEW_TREATMENT); + intent.putExtras(bundle); + intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); + context.sendBroadcast(intent); + } } } public void handleChangedTreatment(JSONObject treatment, Context context, boolean isDelta) { - if(!SP.getBoolean("nsclient_localbroadcasts", true)) return; - Bundle bundle = new Bundle(); bundle.putString("treatment", treatment.toString()); bundle.putBoolean("delta", isDelta); Intent intent = new Intent(Intents.ACTION_CHANGED_TREATMENT); intent.putExtras(bundle); intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); - context.sendBroadcast(intent); - } + LocalBroadcastManager.getInstance(MainApp.instance()).sendBroadcast(intent); - public static void handleChangedTreatment(JSONArray treatments, Context context, boolean isDelta) { - if(!SP.getBoolean("nsclient_localbroadcasts", true)) return; - - List splitted = splitArray(treatments); - for (JSONArray part: splitted) { - Bundle bundle = new Bundle(); - bundle.putString("treatments", part.toString()); + if(SP.getBoolean("nsclient_localbroadcasts", true)) { + bundle = new Bundle(); + bundle.putString("treatment", treatment.toString()); bundle.putBoolean("delta", isDelta); - Intent intent = new Intent(Intents.ACTION_CHANGED_TREATMENT); + intent = new Intent(Intents.ACTION_CHANGED_TREATMENT); intent.putExtras(bundle); intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); context.sendBroadcast(intent); } } - public static void handleRemovedTreatment(JSONObject treatment, Context context, boolean isDelta) { + public static void handleChangedTreatment(JSONArray treatments, Context context, boolean isDelta) { - if(!SP.getBoolean("nsclient_localbroadcasts", true)) return; + List splitted = splitArray(treatments); + for (JSONArray part : splitted) { + Bundle bundle = new Bundle(); + bundle.putString("treatments", part.toString()); + bundle.putBoolean("delta", isDelta); + Intent intent = new Intent(Intents.ACTION_CHANGED_TREATMENT); + intent.putExtras(bundle); + intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); + LocalBroadcastManager.getInstance(MainApp.instance()).sendBroadcast(intent); + } + + if(SP.getBoolean("nsclient_localbroadcasts", true)) { + splitted = splitArray(treatments); + for (JSONArray part : splitted) { + Bundle bundle = new Bundle(); + bundle.putString("treatments", part.toString()); + bundle.putBoolean("delta", isDelta); + Intent intent = new Intent(Intents.ACTION_CHANGED_TREATMENT); + intent.putExtras(bundle); + intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); + context.sendBroadcast(intent); + } + } + } + + public static void handleRemovedTreatment(JSONObject treatment, Context context, boolean isDelta) { Bundle bundle = new Bundle(); bundle.putString("treatment", treatment.toString()); @@ -93,20 +133,40 @@ public class BroadcastTreatment { Intent intent = new Intent(Intents.ACTION_REMOVED_TREATMENT); intent.putExtras(bundle); intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); - context.sendBroadcast(intent); + LocalBroadcastManager.getInstance(MainApp.instance()).sendBroadcast(intent); + + + if(SP.getBoolean("nsclient_localbroadcasts", true)) { + bundle = new Bundle(); + bundle.putString("treatment", treatment.toString()); + bundle.putBoolean("delta", isDelta); + intent = new Intent(Intents.ACTION_REMOVED_TREATMENT); + intent.putExtras(bundle); + intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); + context.sendBroadcast(intent); + } } public static void handleRemovedTreatment(JSONArray treatments, Context context, boolean isDelta) { - if(!SP.getBoolean("nsclient_localbroadcasts", true)) return; - Bundle bundle = new Bundle(); bundle.putString("treatments", treatments.toString()); bundle.putBoolean("delta", isDelta); Intent intent = new Intent(Intents.ACTION_REMOVED_TREATMENT); intent.putExtras(bundle); intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); - context.sendBroadcast(intent); + LocalBroadcastManager.getInstance(MainApp.instance()).sendBroadcast(intent); + + + if(SP.getBoolean("nsclient_localbroadcasts", true)) { + bundle = new Bundle(); + bundle.putString("treatments", treatments.toString()); + bundle.putBoolean("delta", isDelta); + intent = new Intent(Intents.ACTION_REMOVED_TREATMENT); + intent.putExtras(bundle); + intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); + context.sendBroadcast(intent); + } }