From 9642ff7caca38019c391a8415e7026164b97e97c Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Wed, 15 Aug 2018 22:09:13 +0200 Subject: [PATCH] use local broadcast for dbaccess --- .../info/nightscout/androidaps/MainApp.java | 5 +++ .../plugins/NSClientInternal/NSUpload.java | 31 ++++++++++--------- .../receivers/DBAccessReceiver.java | 7 ++--- 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/MainApp.java b/app/src/main/java/info/nightscout/androidaps/MainApp.java index ac5f401a55..514cf06278 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainApp.java +++ b/app/src/main/java/info/nightscout/androidaps/MainApp.java @@ -44,6 +44,7 @@ import info.nightscout.androidaps.plugins.Loop.LoopPlugin; import info.nightscout.androidaps.plugins.NSClientInternal.NSClientPlugin; import info.nightscout.androidaps.plugins.NSClientInternal.NSUpload; import info.nightscout.androidaps.plugins.NSClientInternal.receivers.AckAlarmReceiver; +import info.nightscout.androidaps.plugins.NSClientInternal.receivers.DBAccessReceiver; import info.nightscout.androidaps.plugins.OpenAPSAMA.OpenAPSAMAPlugin; import info.nightscout.androidaps.plugins.OpenAPSMA.OpenAPSMAPlugin; import info.nightscout.androidaps.plugins.OpenAPSSMB.OpenAPSSMBPlugin; @@ -100,6 +101,7 @@ public class MainApp extends Application { private static DataReceiver dataReceiver = new DataReceiver(); private static NSAlarmReceiver alarmReciever = new NSAlarmReceiver(); private static AckAlarmReceiver ackAlarmReciever = new AckAlarmReceiver(); + private static DBAccessReceiver dbAccessReciever = new DBAccessReceiver(); private LocalBroadcastManager lbm; public static boolean devBranch; @@ -232,6 +234,9 @@ public class MainApp extends Application { //register ack alarm lbm.registerReceiver(ackAlarmReciever, new IntentFilter(Intents.ACTION_ACK_ALARM)); + + //register dbaccess + lbm.registerReceiver(dbAccessReciever, new IntentFilter(Intents.ACTION_DATABASE)); } private void startKeepAliveService() { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/NSUpload.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/NSUpload.java index 314ee38972..5a1f06a247 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/NSUpload.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/NSUpload.java @@ -8,6 +8,7 @@ import android.os.Build; import android.os.Bundle; import android.preference.PreferenceManager; import android.support.annotation.Nullable; +import android.support.v4.content.LocalBroadcastManager; import org.apache.commons.lang3.StringUtils; import org.json.JSONArray; @@ -71,7 +72,7 @@ public class NSUpload { Intent intent = new Intent(Intents.ACTION_DATABASE); intent.putExtras(bundle); intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); - context.sendBroadcast(intent); + LocalBroadcastManager.getInstance(context).sendBroadcast(intent); DbLogger.dbAdd(intent, data.toString()); } catch (JSONException e) { log.error("Unhandled exception", e); @@ -113,7 +114,7 @@ public class NSUpload { Intent intent = new Intent(Intents.ACTION_DATABASE); intent.putExtras(bundle); intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); - context.sendBroadcast(intent); + LocalBroadcastManager.getInstance(context).sendBroadcast(intent); DbLogger.dbAdd(intent, data.toString()); } } catch (JSONException e) { @@ -139,7 +140,7 @@ public class NSUpload { Intent intent = new Intent(Intents.ACTION_DATABASE); intent.putExtras(bundle); intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); - context.sendBroadcast(intent); + LocalBroadcastManager.getInstance(context).sendBroadcast(intent); DbLogger.dbAdd(intent, data.toString()); } catch (JSONException e) { log.error("Unhandled exception", e); @@ -167,7 +168,7 @@ public class NSUpload { Intent intent = new Intent(Intents.ACTION_DATABASE); intent.putExtras(bundle); intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); - context.sendBroadcast(intent); + LocalBroadcastManager.getInstance(context).sendBroadcast(intent); DbLogger.dbAdd(intent, data.toString()); } catch (JSONException e) { log.error("Unhandled exception", e); @@ -195,7 +196,7 @@ public class NSUpload { Intent intent = new Intent(Intents.ACTION_DATABASE); intent.putExtras(bundle); intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); - context.sendBroadcast(intent); + LocalBroadcastManager.getInstance(context).sendBroadcast(intent); DbLogger.dbAdd(intent, data.toString()); } catch (JSONException e) { log.error("Unhandled exception", e); @@ -265,7 +266,7 @@ public class NSUpload { Intent intent = new Intent(Intents.ACTION_DATABASE); intent.putExtras(bundle); intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); - context.sendBroadcast(intent); + LocalBroadcastManager.getInstance(context).sendBroadcast(intent); DbLogger.dbAdd(intent, deviceStatus.mongoRecord().toString()); } catch (JSONException e) { log.error("Unhandled exception", e); @@ -346,7 +347,7 @@ public class NSUpload { Intent intent = new Intent(Intents.ACTION_DATABASE); intent.putExtras(bundle); intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); - context.sendBroadcast(intent); + LocalBroadcastManager.getInstance(context).sendBroadcast(intent); DbLogger.dbAdd(intent, data.toString()); } } catch (JSONException e) { @@ -394,7 +395,7 @@ public class NSUpload { Intent intent = new Intent(Intents.ACTION_DATABASE); intent.putExtras(bundle); intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); - context.sendBroadcast(intent); + LocalBroadcastManager.getInstance(context).sendBroadcast(intent); DbLogger.dbAdd(intent, data.toString()); } catch (Exception e) { log.error("Unhandled exception", e); @@ -412,7 +413,7 @@ public class NSUpload { Intent intent = new Intent(Intents.ACTION_DATABASE); intent.putExtras(bundle); intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); - context.sendBroadcast(intent); + LocalBroadcastManager.getInstance(context).sendBroadcast(intent); DbLogger.dbRemove(intent, _id); } catch (Exception e) { log.error("Unhandled exception", e); @@ -435,7 +436,7 @@ public class NSUpload { Intent intent = new Intent(Intents.ACTION_DATABASE); intent.putExtras(bundle); intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); - context.sendBroadcast(intent); + LocalBroadcastManager.getInstance(context).sendBroadcast(intent); DbLogger.dbAdd(intent, data.toString()); } catch (JSONException e) { log.error("Unhandled exception", e); @@ -465,7 +466,7 @@ public class NSUpload { Intent intent = new Intent(Intents.ACTION_DATABASE); intent.putExtras(bundle); intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); - context.sendBroadcast(intent); + LocalBroadcastManager.getInstance(context).sendBroadcast(intent); DbLogger.dbAdd(intent, data.toString()); } @@ -489,7 +490,7 @@ public class NSUpload { Intent intent = new Intent(Intents.ACTION_DATABASE); intent.putExtras(bundle); intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); - context.sendBroadcast(intent); + LocalBroadcastManager.getInstance(context).sendBroadcast(intent); DbLogger.dbAdd(intent, data.toString()); } @@ -511,7 +512,7 @@ public class NSUpload { Intent intent = new Intent(Intents.ACTION_DATABASE); intent.putExtras(bundle); intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); - context.sendBroadcast(intent); + LocalBroadcastManager.getInstance(context).sendBroadcast(intent); DbLogger.dbAdd(intent, data.toString()); } } @@ -536,7 +537,7 @@ public class NSUpload { Intent intent = new Intent(Intents.ACTION_DATABASE); intent.putExtras(bundle); intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); - context.sendBroadcast(intent); + LocalBroadcastManager.getInstance(context).sendBroadcast(intent); DbLogger.dbAdd(intent, data.toString()); } @@ -550,7 +551,7 @@ public class NSUpload { Intent intent = new Intent(Intents.ACTION_DATABASE); intent.putExtras(bundle); intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); - context.sendBroadcast(intent); + LocalBroadcastManager.getInstance(context).sendBroadcast(intent); DbLogger.dbRemove(intent, _id); } catch (Exception e) { log.error("Unhandled exception", e); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/receivers/DBAccessReceiver.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/receivers/DBAccessReceiver.java index 771cfb6215..9ea03565aa 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/receivers/DBAccessReceiver.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/receivers/DBAccessReceiver.java @@ -38,6 +38,9 @@ public class DBAccessReceiver extends BroadcastReceiver { if (bundles == null) return; if (!bundles.containsKey("action")) return; + if (L.isEnabled(L.NSCLIENT)) + BundleLogger.log(bundles); + String collection = null; String _id = null; JSONObject data = null; @@ -46,19 +49,16 @@ public class DBAccessReceiver extends BroadcastReceiver { collection = bundles.getString("collection"); } catch (Exception e) { log.error("Unhandled exception", e); - BundleLogger.log(bundles); } try { _id = bundles.getString("_id"); } catch (Exception e) { log.error("Unhandled exception", e); - BundleLogger.log(bundles); } try { data = new JSONObject(bundles.getString("data")); } catch (Exception e) { log.error("Unhandled exception", e); - BundleLogger.log(bundles); } if (data == null && !action.equals("dbRemove") || _id == null && action.equals("dbRemove")) { @@ -75,7 +75,6 @@ public class DBAccessReceiver extends BroadcastReceiver { data.put("NSCLIENT_ID", nsclientid); } catch (JSONException e) { log.error("Unhandled exception", e); - BundleLogger.log(bundles); } if (!isAllowedCollection(collection)) {