Added data sync foreground service when transfering files through chat (upload & download)

This commit is contained in:
Sylvain Berfini 2024-07-11 10:20:15 +02:00
parent bf1140fb38
commit c7032b6000
5 changed files with 175 additions and 3 deletions

View file

@ -162,6 +162,13 @@
android:stopWithTask="false"
android:label="@string/app_name" />
<service
android:name=".core.CoreFileTransferService"
android:exported="false"
android:foregroundServiceType="dataSync"
android:stopWithTask="false"
android:label="@string/app_name" />
<service android:name="org.linphone.core.tools.firebase.FirebaseMessaging"
android:enabled="true"
android:exported="false">

View file

@ -0,0 +1,146 @@
/*
* Copyright (c) 2010-2024 Belledonne Communications SARL.
*
* This file is part of linphone-android
* (see https://www.linphone.org).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.linphone.core
import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.os.IBinder
import androidx.annotation.MainThread
import androidx.annotation.WorkerThread
import androidx.core.app.ActivityCompat
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import org.linphone.LinphoneApplication.Companion.coreContext
import org.linphone.R
import org.linphone.core.tools.Log
import org.linphone.core.tools.service.FileTransferService
@MainThread
class CoreFileTransferService : FileTransferService() {
companion object {
private const val TAG = "[Core File Transfer Service]"
}
var builder = NotificationCompat.Builder(this, SERVICE_NOTIFICATION_CHANNEL_ID)
private val coreListener = object : CoreListenerStub() {
@WorkerThread
override fun onRemainingNumberOfFileTransferChanged(
core: Core,
downloadCount: Int,
uploadCount: Int
) {
updateNotificationContent(downloadCount, uploadCount)
}
}
override fun onCreate() {
super.onCreate()
coreContext.core.addListener(coreListener)
Log.i("$TAG Created")
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Log.i("$TAG onStartCommand")
return super.onStartCommand(intent, flags, startId)
}
override fun onTaskRemoved(rootIntent: Intent?) {
Log.i("$TAG Task removed, doing nothing")
super.onTaskRemoved(rootIntent)
}
override fun onDestroy() {
Log.i("$TAG onDestroy")
coreContext.core.removeListener(coreListener)
super.onDestroy()
}
override fun onBind(p0: Intent?): IBinder? {
return null
}
override fun createServiceNotification() {
mServiceNotification = builder.setContentTitle(
getString(R.string.notification_file_transfer_title)
)
.setContentText(getString(R.string.notification_file_transfer_startup_message))
.setSmallIcon(R.drawable.linphone_notification)
.setAutoCancel(false)
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setWhen(System.currentTimeMillis())
.setShowWhen(false)
.setOngoing(true)
.setProgress(0, 0, true)
.build()
coreContext.postOnCoreThread { core ->
val downloadingFilesCount = core.remainingDownloadFileCount
val uploadingFilesCount = core.remainingUploadFileCount
updateNotificationContent(downloadingFilesCount, uploadingFilesCount)
}
}
@WorkerThread
private fun updateNotificationContent(downloadingFilesCount: Int, uploadingFilesCount: Int) {
Log.i(
"$TAG [$downloadingFilesCount] file(s) being downloaded, [$uploadingFilesCount] file(s) being uploaded"
)
val downloadText = resources.getQuantityString(
R.plurals.notification_file_transfer_download,
downloadingFilesCount,
"$downloadingFilesCount"
)
val uploadText = resources.getQuantityString(
R.plurals.notification_file_transfer_upload,
uploadingFilesCount,
"$uploadingFilesCount"
)
val message = if (downloadingFilesCount > 0 && uploadingFilesCount > 0) {
getString(
R.string.notification_file_transfer_upload_download_message,
downloadText,
uploadText
)
} else if (downloadingFilesCount > 0) {
downloadText
} else if (uploadingFilesCount > 0) {
uploadText
} else {
""
}
mServiceNotification = builder.setContentText(message).build()
val notificationsManager = NotificationManagerCompat.from(this)
if (ActivityCompat.checkSelfPermission(
this,
Manifest.permission.POST_NOTIFICATIONS
) == PackageManager.PERMISSION_GRANTED
) {
notificationsManager.notify(SERVICE_NOTIF_ID, mServiceNotification)
} else {
Log.e("$TAG POST_NOTIFICATIONS permission wasn't granted!")
}
}
}

View file

@ -26,7 +26,6 @@ import androidx.core.app.NotificationCompat
import org.linphone.R
import org.linphone.core.tools.Log
import org.linphone.core.tools.service.PushService
import org.linphone.notifications.NotificationsManager
@MainThread
class CorePushService : PushService() {
@ -68,8 +67,7 @@ class CorePushService : PushService() {
.setSmallIcon(R.drawable.linphone_notification)
.setAutoCancel(false)
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.setGroup(NotificationsManager.CHAT_NOTIFICATIONS_GROUP)
.setVisibility(NotificationCompat.VISIBILITY_SECRET)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setWhen(System.currentTimeMillis())
.setShowWhen(false)
.setOngoing(true)

View file

@ -57,6 +57,16 @@
<string name="notification_missed_call_title">Appel manqué</string>
<string name="notification_push_received_title">&appName;</string>
<string name="notification_push_received_message">Recherche de nouveaux messages</string>
<string name="notification_file_transfer_title">&appName;</string>
<string name="notification_file_transfer_startup_message">Transfert de fichier(s) en cours</string>
<plurals name="notification_file_transfer_upload" tools:ignore="MissingQuantity">
<item quantity="one">%s fichier en cours d\'envoi</item>
<item quantity="other">%s fichiers en cours d\'envoi</item>
</plurals>
<plurals name="notification_file_transfer_download" tools:ignore="MissingQuantity">
<item quantity="one">%s fichier en cours de réception</item>
<item quantity="other">%s fichiers en cours de réception</item>
</plurals>
<!-- First screens user see when app is installed and started -->
<string name="welcome_page_title">Bienvenue</string>

View file

@ -93,6 +93,17 @@
<string name="notification_missed_call_title">Missed call</string>
<string name="notification_push_received_title">&appName;</string>
<string name="notification_push_received_message">Searching for new messages</string>
<string name="notification_file_transfer_title">&appName;</string>
<string name="notification_file_transfer_startup_message">File(s) transfer in progress</string>
<plurals name="notification_file_transfer_upload">
<item quantity="one">%s file being uploaded</item>
<item quantity="other">%s files being uploaded</item>
</plurals>
<plurals name="notification_file_transfer_download">
<item quantity="one">%s file being downloaded</item>
<item quantity="other">%s files being downloaded</item>
</plurals>
<string name="notification_file_transfer_upload_download_message" translatable="false">%s, %s</string>
<!-- First screens user see when app is installed and started -->
<string name="welcome_page_title">Welcome</string>