From 8ebced9481dab2e3b2ab8b4d3e1ccc9aabdd3c89 Mon Sep 17 00:00:00 2001
From: akwizgran <michael@briarproject.org>
Date: Thu, 29 Mar 2018 11:45:03 +0100
Subject: [PATCH] Show notification for low memory shutdown.

---
 .../briar/android/BriarService.java           | 20 +++++++++++++++++++
 briar-android/src/main/res/values/strings.xml |  3 +++
 2 files changed, 23 insertions(+)

diff --git a/briar-android/src/main/java/org/briarproject/briar/android/BriarService.java b/briar-android/src/main/java/org/briarproject/briar/android/BriarService.java
index c88bf061bc..3e77d52c0f 100644
--- a/briar-android/src/main/java/org/briarproject/briar/android/BriarService.java
+++ b/briar-android/src/main/java/org/briarproject/briar/android/BriarService.java
@@ -22,6 +22,7 @@ import org.briarproject.bramble.api.system.AndroidExecutor;
 import org.briarproject.briar.R;
 import org.briarproject.briar.android.logout.HideUiActivity;
 import org.briarproject.briar.android.navdrawer.NavDrawerActivity;
+import org.briarproject.briar.android.splash.SplashScreenActivity;
 
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.atomic.AtomicBoolean;
@@ -221,6 +222,7 @@ public class BriarService extends Service {
 		super.onLowMemory();
 		LOG.warning("Memory is low");
 		shutdownFromBackground();
+		showLowMemoryShutdownNotification();
 	}
 
 	private void shutdownFromBackground() {
@@ -245,6 +247,24 @@ public class BriarService extends Service {
 		}).start();
 	}
 
+	private void showLowMemoryShutdownNotification() {
+		androidExecutor.runOnUiThread(() -> {
+			NotificationCompat.Builder b = new NotificationCompat.Builder(
+					BriarService.this, FAILURE_CHANNEL_ID);
+			b.setSmallIcon(android.R.drawable.stat_notify_error);
+			b.setContentTitle(getText(
+					R.string.low_memory_shutdown_notification_title));
+			b.setContentText(getText(
+					R.string.low_memory_shutdown_notification_text));
+			Intent i = new Intent(this, SplashScreenActivity.class);
+			b.setContentIntent(PendingIntent.getActivity(this, 0, i, 0));
+			b.setAutoCancel(true);
+			Object o = getSystemService(NOTIFICATION_SERVICE);
+			NotificationManager nm = (NotificationManager) o;
+			nm.notify(FAILURE_NOTIFICATION_ID, b.build());
+		});
+	}
+
 	/**
 	 * Waits for all services to start before returning.
 	 */
diff --git a/briar-android/src/main/res/values/strings.xml b/briar-android/src/main/res/values/strings.xml
index 14de9071b5..c67a15e350 100644
--- a/briar-android/src/main/res/values/strings.xml
+++ b/briar-android/src/main/res/values/strings.xml
@@ -411,4 +411,7 @@
 	<string name="permission_camera_denied_body">You have denied access to the camera, but adding contacts requires using the camera.\n\nPlease consider granting access.</string>
 	<string name="permission_camera_denied_toast">Camera permission was not granted</string>
 
+	<!-- Low Memory Notification -->
+	<string name="low_memory_shutdown_notification_title">Signed out of Briar</string>
+	<string name="low_memory_shutdown_notification_text">Signed out due to lack of memory.</string>
 </resources>
-- 
GitLab