From 304cf72c420195edd72bad57079aafb602631e83 Mon Sep 17 00:00:00 2001 From: Catfriend1 Date: Sat, 23 Mar 2019 16:57:25 +0100 Subject: [PATCH] Show notification if SDcard is full (fixes #381) (#382) * Add string: notification_out_of_disk_space * Show notification if SDcard is full (fixes #381) * Imported de translation * Handle event: FolderErrors "insufficient space in basic" (fixes #381) --- .../service/EventProcessor.java | 54 +++++++++++++++++-- .../syncthingandroid/service/RestApi.java | 4 +- app/src/main/res/values-de/strings.xml | 4 ++ app/src/main/res/values/strings.xml | 4 ++ 4 files changed, 59 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/nutomic/syncthingandroid/service/EventProcessor.java b/app/src/main/java/com/nutomic/syncthingandroid/service/EventProcessor.java index 71cc5ba6..48f3e1bd 100644 --- a/app/src/main/java/com/nutomic/syncthingandroid/service/EventProcessor.java +++ b/app/src/main/java/com/nutomic/syncthingandroid/service/EventProcessor.java @@ -15,6 +15,10 @@ import android.text.TextUtils; import android.util.Log; import com.annimon.stream.Stream; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; + import com.nutomic.syncthingandroid.R; import com.nutomic.syncthingandroid.SyncthingApp; import com.nutomic.syncthingandroid.activities.DeviceActivity; @@ -78,7 +82,7 @@ public class EventProcessor implements Runnable, RestApi.OnReceiveEventListener // If that's the case we've to start at zero because syncthing was restarted. mRestApi.getEvents(0, 1, new RestApi.OnReceiveEventListener() { @Override - public void onEvent(Event event) { + public void onEvent(Event event, JsonElement json) { } @Override @@ -96,7 +100,7 @@ public class EventProcessor implements Runnable, RestApi.OnReceiveEventListener * Performs the actual event handling. */ @Override - public void onEvent(Event event) { + public void onEvent(Event event, JsonElement json) { switch (event.type) { case "ConfigSaved": if (mRestApi != null) { @@ -119,6 +123,10 @@ public class EventProcessor implements Runnable, RestApi.OnReceiveEventListener completionInfo ); break; + case "FolderErrors": + LogV("Event " + event.type + ", data " + event.data); + onFolderErrors(json); + break; case "FolderRejected": onFolderRejected( (String) event.data.get("device"), // deviceId @@ -171,9 +179,7 @@ public class EventProcessor implements Runnable, RestApi.OnReceiveEventListener case "Starting": case "StartupComplete": case "StateChanged": - if (ENABLE_VERBOSE_LOG) { - LogV("Ignored event " + event.type + ", data " + event.data); - } + LogV("Ignored event " + event.type + ", data " + event.data); break; default: Log.d(TAG, "Unhandled event " + event.type); @@ -293,6 +299,36 @@ public class EventProcessor implements Runnable, RestApi.OnReceiveEventListener mNotificationHandler.showConsentNotification(notificationId, title, piAccept, piIgnore); } + private void onFolderErrors(JsonElement json) { + JsonElement data = ((JsonObject) json).get("data"); + if (data == null) { + Log.e(TAG, "onFolderErrors: data == null"); + return; + } + JsonArray errors = (JsonArray) ((JsonObject) data).get("errors"); + if (errors == null) { + Log.e(TAG, "onFolderErrors: errors == null"); + return; + } + for (int i = 0; i < errors.size(); i++) { + JsonElement error = errors.get(i); + if (error != null) { + String strError = ((JsonObject) error).get("error").toString(); + String strPath = ((JsonObject) error).get("path").toString(); + if (!TextUtils.isEmpty(strError) && + !TextUtils.isEmpty(strPath) && + strError.contains("insufficient space in basic")) { + String[] segments = strPath.split(File.separator); + String shortenedFileAndFolder = + segments.length < 2 ? + strPath : + segments[segments.length-2] + File.separator + segments[segments.length-1]; + mNotificationHandler.showCrashedNotification(R.string.notification_out_of_disk_space, shortenedFileAndFolder); + } + } + } + } + /** * Precondition: action != null */ @@ -300,6 +336,14 @@ public class EventProcessor implements Runnable, RestApi.OnReceiveEventListener String relativeFilePath = updatedFile.toString(); if (!TextUtils.isEmpty(error)) { Log.e(TAG, "onItemFinished: Error \"" + error + "\" reported on file: " + relativeFilePath); + if (error.contains("no space left on device")) { + String[] segments = relativeFilePath.split(File.separator); + String shortenedFileAndFolder = + segments.length < 2 ? + relativeFilePath : + segments[segments.length-2] + File.separator + segments[segments.length-1]; + mNotificationHandler.showCrashedNotification(R.string.notification_out_of_disk_space, shortenedFileAndFolder); + } return; } diff --git a/app/src/main/java/com/nutomic/syncthingandroid/service/RestApi.java b/app/src/main/java/com/nutomic/syncthingandroid/service/RestApi.java index 3539b35e..2b9b5dd1 100644 --- a/app/src/main/java/com/nutomic/syncthingandroid/service/RestApi.java +++ b/app/src/main/java/com/nutomic/syncthingandroid/service/RestApi.java @@ -749,7 +749,7 @@ public class RestApi { /** * Called for each event. */ - void onEvent(Event event); + void onEvent(Event event, JsonElement json); /** * Called after all available events have been processed. @@ -778,7 +778,7 @@ public class RestApi { if (lastId < event.id) lastId = event.id; - listener.onEvent(event); + listener.onEvent(event, json); } listener.onDone(lastId); diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index d05f70f7..50514366 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -821,6 +821,10 @@ Bitte melden Sie auftretende Probleme via GitHub. Laufkonditionen werden überwacht Andere Benachrichtigungen + + SD-Karte ist voll. Datei %1$s + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 2ad11767..9a0a746c 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -842,6 +842,10 @@ Please report any problems you encounter via Github. Monitoring run conditions Other notifications + + The SDcard is full. File %1$s + +