use local broadcast for dbaccess

This commit is contained in:
Milos Kozak 2018-08-15 22:09:13 +02:00
parent 27bfab5e15
commit 9642ff7cac
3 changed files with 24 additions and 19 deletions

View file

@ -44,6 +44,7 @@ import info.nightscout.androidaps.plugins.Loop.LoopPlugin;
import info.nightscout.androidaps.plugins.NSClientInternal.NSClientPlugin; import info.nightscout.androidaps.plugins.NSClientInternal.NSClientPlugin;
import info.nightscout.androidaps.plugins.NSClientInternal.NSUpload; import info.nightscout.androidaps.plugins.NSClientInternal.NSUpload;
import info.nightscout.androidaps.plugins.NSClientInternal.receivers.AckAlarmReceiver; 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.OpenAPSAMA.OpenAPSAMAPlugin;
import info.nightscout.androidaps.plugins.OpenAPSMA.OpenAPSMAPlugin; import info.nightscout.androidaps.plugins.OpenAPSMA.OpenAPSMAPlugin;
import info.nightscout.androidaps.plugins.OpenAPSSMB.OpenAPSSMBPlugin; import info.nightscout.androidaps.plugins.OpenAPSSMB.OpenAPSSMBPlugin;
@ -100,6 +101,7 @@ public class MainApp extends Application {
private static DataReceiver dataReceiver = new DataReceiver(); private static DataReceiver dataReceiver = new DataReceiver();
private static NSAlarmReceiver alarmReciever = new NSAlarmReceiver(); private static NSAlarmReceiver alarmReciever = new NSAlarmReceiver();
private static AckAlarmReceiver ackAlarmReciever = new AckAlarmReceiver(); private static AckAlarmReceiver ackAlarmReciever = new AckAlarmReceiver();
private static DBAccessReceiver dbAccessReciever = new DBAccessReceiver();
private LocalBroadcastManager lbm; private LocalBroadcastManager lbm;
public static boolean devBranch; public static boolean devBranch;
@ -232,6 +234,9 @@ public class MainApp extends Application {
//register ack alarm //register ack alarm
lbm.registerReceiver(ackAlarmReciever, new IntentFilter(Intents.ACTION_ACK_ALARM)); lbm.registerReceiver(ackAlarmReciever, new IntentFilter(Intents.ACTION_ACK_ALARM));
//register dbaccess
lbm.registerReceiver(dbAccessReciever, new IntentFilter(Intents.ACTION_DATABASE));
} }
private void startKeepAliveService() { private void startKeepAliveService() {

View file

@ -8,6 +8,7 @@ import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray; import org.json.JSONArray;
@ -71,7 +72,7 @@ public class NSUpload {
Intent intent = new Intent(Intents.ACTION_DATABASE); Intent intent = new Intent(Intents.ACTION_DATABASE);
intent.putExtras(bundle); intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
context.sendBroadcast(intent); LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
DbLogger.dbAdd(intent, data.toString()); DbLogger.dbAdd(intent, data.toString());
} catch (JSONException e) { } catch (JSONException e) {
log.error("Unhandled exception", e); log.error("Unhandled exception", e);
@ -113,7 +114,7 @@ public class NSUpload {
Intent intent = new Intent(Intents.ACTION_DATABASE); Intent intent = new Intent(Intents.ACTION_DATABASE);
intent.putExtras(bundle); intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
context.sendBroadcast(intent); LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
DbLogger.dbAdd(intent, data.toString()); DbLogger.dbAdd(intent, data.toString());
} }
} catch (JSONException e) { } catch (JSONException e) {
@ -139,7 +140,7 @@ public class NSUpload {
Intent intent = new Intent(Intents.ACTION_DATABASE); Intent intent = new Intent(Intents.ACTION_DATABASE);
intent.putExtras(bundle); intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
context.sendBroadcast(intent); LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
DbLogger.dbAdd(intent, data.toString()); DbLogger.dbAdd(intent, data.toString());
} catch (JSONException e) { } catch (JSONException e) {
log.error("Unhandled exception", e); log.error("Unhandled exception", e);
@ -167,7 +168,7 @@ public class NSUpload {
Intent intent = new Intent(Intents.ACTION_DATABASE); Intent intent = new Intent(Intents.ACTION_DATABASE);
intent.putExtras(bundle); intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
context.sendBroadcast(intent); LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
DbLogger.dbAdd(intent, data.toString()); DbLogger.dbAdd(intent, data.toString());
} catch (JSONException e) { } catch (JSONException e) {
log.error("Unhandled exception", e); log.error("Unhandled exception", e);
@ -195,7 +196,7 @@ public class NSUpload {
Intent intent = new Intent(Intents.ACTION_DATABASE); Intent intent = new Intent(Intents.ACTION_DATABASE);
intent.putExtras(bundle); intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
context.sendBroadcast(intent); LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
DbLogger.dbAdd(intent, data.toString()); DbLogger.dbAdd(intent, data.toString());
} catch (JSONException e) { } catch (JSONException e) {
log.error("Unhandled exception", e); log.error("Unhandled exception", e);
@ -265,7 +266,7 @@ public class NSUpload {
Intent intent = new Intent(Intents.ACTION_DATABASE); Intent intent = new Intent(Intents.ACTION_DATABASE);
intent.putExtras(bundle); intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
context.sendBroadcast(intent); LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
DbLogger.dbAdd(intent, deviceStatus.mongoRecord().toString()); DbLogger.dbAdd(intent, deviceStatus.mongoRecord().toString());
} catch (JSONException e) { } catch (JSONException e) {
log.error("Unhandled exception", e); log.error("Unhandled exception", e);
@ -346,7 +347,7 @@ public class NSUpload {
Intent intent = new Intent(Intents.ACTION_DATABASE); Intent intent = new Intent(Intents.ACTION_DATABASE);
intent.putExtras(bundle); intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
context.sendBroadcast(intent); LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
DbLogger.dbAdd(intent, data.toString()); DbLogger.dbAdd(intent, data.toString());
} }
} catch (JSONException e) { } catch (JSONException e) {
@ -394,7 +395,7 @@ public class NSUpload {
Intent intent = new Intent(Intents.ACTION_DATABASE); Intent intent = new Intent(Intents.ACTION_DATABASE);
intent.putExtras(bundle); intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
context.sendBroadcast(intent); LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
DbLogger.dbAdd(intent, data.toString()); DbLogger.dbAdd(intent, data.toString());
} catch (Exception e) { } catch (Exception e) {
log.error("Unhandled exception", e); log.error("Unhandled exception", e);
@ -412,7 +413,7 @@ public class NSUpload {
Intent intent = new Intent(Intents.ACTION_DATABASE); Intent intent = new Intent(Intents.ACTION_DATABASE);
intent.putExtras(bundle); intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
context.sendBroadcast(intent); LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
DbLogger.dbRemove(intent, _id); DbLogger.dbRemove(intent, _id);
} catch (Exception e) { } catch (Exception e) {
log.error("Unhandled exception", e); log.error("Unhandled exception", e);
@ -435,7 +436,7 @@ public class NSUpload {
Intent intent = new Intent(Intents.ACTION_DATABASE); Intent intent = new Intent(Intents.ACTION_DATABASE);
intent.putExtras(bundle); intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
context.sendBroadcast(intent); LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
DbLogger.dbAdd(intent, data.toString()); DbLogger.dbAdd(intent, data.toString());
} catch (JSONException e) { } catch (JSONException e) {
log.error("Unhandled exception", e); log.error("Unhandled exception", e);
@ -465,7 +466,7 @@ public class NSUpload {
Intent intent = new Intent(Intents.ACTION_DATABASE); Intent intent = new Intent(Intents.ACTION_DATABASE);
intent.putExtras(bundle); intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
context.sendBroadcast(intent); LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
DbLogger.dbAdd(intent, data.toString()); DbLogger.dbAdd(intent, data.toString());
} }
@ -489,7 +490,7 @@ public class NSUpload {
Intent intent = new Intent(Intents.ACTION_DATABASE); Intent intent = new Intent(Intents.ACTION_DATABASE);
intent.putExtras(bundle); intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
context.sendBroadcast(intent); LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
DbLogger.dbAdd(intent, data.toString()); DbLogger.dbAdd(intent, data.toString());
} }
@ -511,7 +512,7 @@ public class NSUpload {
Intent intent = new Intent(Intents.ACTION_DATABASE); Intent intent = new Intent(Intents.ACTION_DATABASE);
intent.putExtras(bundle); intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
context.sendBroadcast(intent); LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
DbLogger.dbAdd(intent, data.toString()); DbLogger.dbAdd(intent, data.toString());
} }
} }
@ -536,7 +537,7 @@ public class NSUpload {
Intent intent = new Intent(Intents.ACTION_DATABASE); Intent intent = new Intent(Intents.ACTION_DATABASE);
intent.putExtras(bundle); intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
context.sendBroadcast(intent); LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
DbLogger.dbAdd(intent, data.toString()); DbLogger.dbAdd(intent, data.toString());
} }
@ -550,7 +551,7 @@ public class NSUpload {
Intent intent = new Intent(Intents.ACTION_DATABASE); Intent intent = new Intent(Intents.ACTION_DATABASE);
intent.putExtras(bundle); intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
context.sendBroadcast(intent); LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
DbLogger.dbRemove(intent, _id); DbLogger.dbRemove(intent, _id);
} catch (Exception e) { } catch (Exception e) {
log.error("Unhandled exception", e); log.error("Unhandled exception", e);

View file

@ -38,6 +38,9 @@ public class DBAccessReceiver extends BroadcastReceiver {
if (bundles == null) return; if (bundles == null) return;
if (!bundles.containsKey("action")) return; if (!bundles.containsKey("action")) return;
if (L.isEnabled(L.NSCLIENT))
BundleLogger.log(bundles);
String collection = null; String collection = null;
String _id = null; String _id = null;
JSONObject data = null; JSONObject data = null;
@ -46,19 +49,16 @@ public class DBAccessReceiver extends BroadcastReceiver {
collection = bundles.getString("collection"); collection = bundles.getString("collection");
} catch (Exception e) { } catch (Exception e) {
log.error("Unhandled exception", e); log.error("Unhandled exception", e);
BundleLogger.log(bundles);
} }
try { try {
_id = bundles.getString("_id"); _id = bundles.getString("_id");
} catch (Exception e) { } catch (Exception e) {
log.error("Unhandled exception", e); log.error("Unhandled exception", e);
BundleLogger.log(bundles);
} }
try { try {
data = new JSONObject(bundles.getString("data")); data = new JSONObject(bundles.getString("data"));
} catch (Exception e) { } catch (Exception e) {
log.error("Unhandled exception", e); log.error("Unhandled exception", e);
BundleLogger.log(bundles);
} }
if (data == null && !action.equals("dbRemove") || _id == null && action.equals("dbRemove")) { 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); data.put("NSCLIENT_ID", nsclientid);
} catch (JSONException e) { } catch (JSONException e) {
log.error("Unhandled exception", e); log.error("Unhandled exception", e);
BundleLogger.log(bundles);
} }
if (!isAllowedCollection(collection)) { if (!isAllowedCollection(collection)) {