From 61b8057744329062addeb156449be50cec825ada Mon Sep 17 00:00:00 2001
From: Torsten Grote <t@grobox.de>
Date: Fri, 10 Dec 2021 10:39:43 -0300
Subject: [PATCH] Add a BroadcastReceiver that re-starts the mailbox when the
 device boots or the app gets upgraded

---
 mailbox-android/src/main/AndroidManifest.xml  | 18 +++++-
 .../mailbox/android/StartReceiver.kt          | 55 +++++++++++++++++++
 2 files changed, 72 insertions(+), 1 deletion(-)
 create mode 100644 mailbox-android/src/main/java/org/briarproject/mailbox/android/StartReceiver.kt

diff --git a/mailbox-android/src/main/AndroidManifest.xml b/mailbox-android/src/main/AndroidManifest.xml
index 2d7e4c82..ee9849f4 100644
--- a/mailbox-android/src/main/AndroidManifest.xml
+++ b/mailbox-android/src/main/AndroidManifest.xml
@@ -2,11 +2,16 @@
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="org.briarproject.mailbox">
 
+    <uses-feature
+        android:name="android.hardware.touchscreen"
+        android:required="false" />
+
     <uses-permission android:name="android.permission.INTERNET" />
     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
     <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
     <uses-permission android:name="android.permission.WAKE_LOCK" />
+    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
 
     <application
         android:name=".android.MailboxApplication"
@@ -19,7 +24,9 @@
 
         <service android:name=".android.MailboxService" />
 
-        <activity android:name=".android.MainActivity">
+        <activity
+            android:name=".android.MainActivity"
+            android:exported="true">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
 
@@ -29,6 +36,15 @@
 
         <receiver android:name=".core.system.AlarmReceiver" />
 
+        <receiver
+            android:name=".android.StartReceiver"
+            android:exported="false">
+            <intent-filter>
+                <action android:name="android.intent.action.BOOT_COMPLETED" />
+                <action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
+            </intent-filter>
+        </receiver>
+
     </application>
 
 </manifest>
diff --git a/mailbox-android/src/main/java/org/briarproject/mailbox/android/StartReceiver.kt b/mailbox-android/src/main/java/org/briarproject/mailbox/android/StartReceiver.kt
new file mode 100644
index 00000000..9865d440
--- /dev/null
+++ b/mailbox-android/src/main/java/org/briarproject/mailbox/android/StartReceiver.kt
@@ -0,0 +1,55 @@
+/*
+ *     Briar Mailbox
+ *     Copyright (C) 2021-2022  The Briar Project
+ *
+ *     This program is free software: you can redistribute it and/or modify
+ *     it under the terms of the GNU Affero General Public License as
+ *     published by the Free Software Foundation, either version 3 of the
+ *     License, or (at your option) any later version.
+ *
+ *     This program is distributed in the hope that it will be useful,
+ *     but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *     GNU Affero General Public License for more details.
+ *
+ *     You should have received a copy of the GNU Affero General Public License
+ *     along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+package org.briarproject.mailbox.android
+
+import android.content.BroadcastReceiver
+import android.content.Context
+import android.content.Intent
+import android.content.Intent.ACTION_BOOT_COMPLETED
+import android.content.Intent.ACTION_MY_PACKAGE_REPLACED
+import dagger.hilt.android.AndroidEntryPoint
+import org.briarproject.mailbox.core.lifecycle.LifecycleManager
+import org.briarproject.mailbox.core.lifecycle.LifecycleManager.LifecycleState.NOT_STARTED
+import org.briarproject.mailbox.core.util.LogUtils.debug
+import org.slf4j.LoggerFactory.getLogger
+import javax.inject.Inject
+
+private val LOG = getLogger(StartReceiver::class.java)
+
+@AndroidEntryPoint
+class StartReceiver : BroadcastReceiver() {
+
+    @Inject
+    internal lateinit var lifecycleManager: LifecycleManager
+
+    override fun onReceive(context: Context, intent: Intent) {
+        val action = intent.action
+        if (action != ACTION_BOOT_COMPLETED && action != ACTION_MY_PACKAGE_REPLACED) return
+
+        val lifecycleState = lifecycleManager.lifecycleStateFlow.value
+        LOG.debug { "Received $action in state ${lifecycleState.name}" }
+        if (lifecycleState == NOT_STARTED) {
+            // On API 31, we can still start a foreground service from background here:
+            // https://developer.android.com/guide/components/foreground-services#background-start-restriction-exemptions
+            MailboxService.startService(context)
+        }
+    }
+
+}
-- 
GitLab