From 29b4f0063be8518b0f4fb2b2bd505a0e9bdc9a94 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Fri, 17 Feb 2017 22:12:35 +0100 Subject: [PATCH] better handling readonly permission --- .../NSClientInternal/acks/NSAddAck.java | 2 +- .../services/NSClientService.java | 24 ++++++++++++++----- .../plugins/Overview/Notification.java | 1 + app/src/main/res/values/strings.xml | 1 + 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/acks/NSAddAck.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/acks/NSAddAck.java index 3cf2551afa..b4ba0c3364 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/acks/NSAddAck.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/acks/NSAddAck.java @@ -41,7 +41,7 @@ public class NSAddAck implements Ack { JSONObject response = (JSONObject) (args[0]); if (response.has("result")) { _id = null; - if (response.getString("result").equals("Not authorized")) { + if (response.getString("result").contains("Not")) { MainApp.bus().post(new EventNSClientRestart()); return; } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/services/NSClientService.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/services/NSClientService.java index 97af63caec..3b7f57c7d4 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/services/NSClientService.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/services/NSClientService.java @@ -56,6 +56,9 @@ import info.nightscout.androidaps.plugins.NSClientInternal.data.NSStatus; import info.nightscout.androidaps.plugins.NSClientInternal.data.NSTreatment; import info.nightscout.androidaps.plugins.NSClientInternal.events.EventNSClientNewLog; import info.nightscout.androidaps.plugins.NSClientInternal.events.EventNSClientStatus; +import info.nightscout.androidaps.plugins.Overview.Notification; +import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification; +import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; import info.nightscout.utils.SP; import info.nightscout.utils.SafeParse; import io.socket.client.IO; @@ -75,6 +78,7 @@ public class NSClientService extends Service { public static Socket mSocket; public static boolean isConnected = false; + public static boolean hasWriteAuth = false; private static Integer dataCounter = 0; @@ -234,6 +238,7 @@ public class NSClientService extends Service { if (mSocket != null) { MainApp.bus().post(new EventNSClientNewLog("NSCLIENT", "destroy")); isConnected = false; + hasWriteAuth = false; mSocket.disconnect(); mSocket = null; } @@ -264,6 +269,7 @@ public class NSClientService extends Service { if (ack.write_treatment) connectionStatus += "T"; connectionStatus += ')'; isConnected = true; + hasWriteAuth = ack.write && ack.write_treatment; MainApp.bus().post(new EventNSClientStatus(connectionStatus)); MainApp.bus().post(new EventNSClientNewLog("AUTH", connectionStatus)); if (!ack.write) { @@ -272,6 +278,12 @@ public class NSClientService extends Service { if (!ack.write_treatment) { MainApp.bus().post(new EventNSClientNewLog("ERROR", "Write treatment permission not granted !!!!")); } + if (!hasWriteAuth) { + Notification noperm = new Notification(Notification.NSCLIENT_NO_WRITE_PERMISSION, MainApp.sResources.getString(R.string.nowritepermission), Notification.URGENT); + MainApp.bus().post(new EventNewNotification(noperm)); + } else { + MainApp.bus().post(new EventDismissNotification(Notification.NSCLIENT_NO_WRITE_PERMISSION)); + } lastReception = new Date(); } @@ -491,7 +503,7 @@ public class NSClientService extends Service { public void dbUpdate(DbRequest dbr, NSUpdateAck ack) { try { - if (!isConnected) return; + if (!isConnected || !hasWriteAuth) return; JSONObject message = new JSONObject(); message.put("collection", dbr.collection); message.put("_id", dbr._id); @@ -505,7 +517,7 @@ public class NSClientService extends Service { public void dbUpdateUnset(DbRequest dbr, NSUpdateAck ack) { try { - if (!isConnected) return; + if (!isConnected || !hasWriteAuth) return; JSONObject message = new JSONObject(); message.put("collection", dbr.collection); message.put("_id", dbr._id); @@ -519,7 +531,7 @@ public class NSClientService extends Service { public void dbRemove(DbRequest dbr, NSUpdateAck ack) { try { - if (!isConnected) return; + if (!isConnected || !hasWriteAuth) return; JSONObject message = new JSONObject(); message.put("collection", dbr.collection); message.put("_id", dbr._id); @@ -542,7 +554,7 @@ public class NSClientService extends Service { public void dbAdd(DbRequest dbr, NSAddAck ack) { try { - if (!isConnected) return; + if (!isConnected || !hasWriteAuth) return; JSONObject message = new JSONObject(); message.put("collection", dbr.collection); message.put("data", dbr.data); @@ -564,7 +576,7 @@ public class NSClientService extends Service { } public void doPing() { - if (!isConnected) return; + if (!isConnected || !hasWriteAuth) return; MainApp.bus().post(new EventNSClientNewLog("PING", "Sending")); uploading = true; JSONObject message = new JSONObject(); @@ -612,7 +624,7 @@ public class NSClientService extends Service { if (UploadQueue.queue.size() == 0) return; - if (!isConnected) return; + if (!isConnected || !hasWriteAuth) return; MainApp.bus().post(new EventNSClientNewLog("QUEUE", "Resend started: " + reason)); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Notification.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Notification.java index 5d83a76cc7..0508917312 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Notification.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Notification.java @@ -23,6 +23,7 @@ public class Notification { public static final int OLD_NSCLIENT = 8; public static final int INVALID_PHONE_NUMBER = 9; public static final int APPROACHING_DAILY_LIMIT = 10; + public static final int NSCLIENT_NO_WRITE_PERMISSION = 10; public int id; public Date date; diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3c9e7149b9..70b6ea72b7 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -539,4 +539,5 @@ Clear log nsclientinternal_autoscroll nsclientinternal_paused + NSCLIENT has no write permission