From 2ae7e6d392cd9109d189713ab07be2a9df4a80ea Mon Sep 17 00:00:00 2001
From: Adrien Beraud <adrien.beraud@savoirfairelinux.com>
Date: Wed, 30 Apr 2025 15:44:36 -0400
Subject: [PATCH] DRingService: implement onTimeout

Change-Id: I99b7d1c41e8e6f055c062a4c9dbb8312d6815531
---
 jami-android/app/src/main/AndroidManifest.xml   |  4 ++--
 .../main/java/cx/ring/service/DRingService.kt   | 17 +++++++++++++++--
 2 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/jami-android/app/src/main/AndroidManifest.xml b/jami-android/app/src/main/AndroidManifest.xml
index 12a488a71..911ee5f24 100644
--- a/jami-android/app/src/main/AndroidManifest.xml
+++ b/jami-android/app/src/main/AndroidManifest.xml
@@ -48,7 +48,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
     <uses-permission android:name="android.permission.FOREGROUND_SERVICE_MICROPHONE" />
     <uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION" />
     <uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
-    <!--<uses-permission android:name="android.permission.FOREGROUND_SERVICE_REMOTE_MESSAGING" />-->
+    <uses-permission android:name="android.permission.FOREGROUND_SERVICE_REMOTE_MESSAGING" />
     <uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PROJECTION" />
     <uses-permission android:name="android.permission.FOREGROUND_SERVICE_PHONE_CALL" />
     <uses-permission android:name="com.android.providers.tv.permission.WRITE_EPG_DATA" />
@@ -332,7 +332,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
         <service
             android:name=".service.DRingService"
             android:exported="false"
-            android:foregroundServiceType="dataSync">
+            android:foregroundServiceType="dataSync|remoteMessaging">
             <intent-filter>
                 <action android:name=".service.DRingService" />
             </intent-filter>
diff --git a/jami-android/app/src/main/java/cx/ring/service/DRingService.kt b/jami-android/app/src/main/java/cx/ring/service/DRingService.kt
index 6ee7b113c..41aca9981 100644
--- a/jami-android/app/src/main/java/cx/ring/service/DRingService.kt
+++ b/jami-android/app/src/main/java/cx/ring/service/DRingService.kt
@@ -22,6 +22,7 @@ import android.content.BroadcastReceiver
 import android.content.Context
 import android.content.Intent
 import android.content.IntentFilter
+import android.content.pm.ServiceInfo
 import android.database.ContentObserver
 import android.net.*
 import android.net.ConnectivityManager.NetworkCallback
@@ -179,15 +180,27 @@ class DRingService : Service() {
     private fun showSystemNotification(settings: Settings) {
         try {
             if (settings.enablePermanentService) {
-                startForeground(NOTIFICATION_ID, mNotificationService.serviceNotification as Notification)
+                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
+                    startForeground(NOTIFICATION_ID, mNotificationService.serviceNotification as Notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_REMOTE_MESSAGING)
+                } else {
+                    startForeground(NOTIFICATION_ID, mNotificationService.serviceNotification as Notification)
+                }
             } else {
-                stopForeground(true)
+                stopForeground(STOP_FOREGROUND_REMOVE)
             }
         } catch (e: Exception) {
             Log.e(TAG, "Can't start or stop foreground service: ${e.message}")
         }
     }
 
+    override fun onTimeout(startId: Int, fgsType: Int) {
+        try {
+            stopForeground(STOP_FOREGROUND_REMOVE)
+        } catch (e: Exception) {
+            Log.e(TAG, "Can't start or stop foreground service: ${e.message}")
+        }
+    }
+
     override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
         // Log.i(TAG, "onStartCommand " + (intent == null ? "null" : intent.getAction()) + " " + flags + " " + startId);
         if (intent != null) {
-- 
GitLab