Fixed issues with background mode advanced setting

This commit is contained in:
Sylvain Berfini 2022-01-27 10:52:03 +01:00
parent eb86ae1573
commit 81884dd218
7 changed files with 59 additions and 15 deletions

View file

@ -31,6 +31,7 @@ import androidx.lifecycle.lifecycleScope
import java.io.File
import kotlinx.coroutines.launch
import org.linphone.LinphoneApplication.Companion.coreContext
import org.linphone.LinphoneApplication.Companion.corePreferences
import org.linphone.R
import org.linphone.activities.GenericFragment
import org.linphone.activities.assistant.AssistantActivity
@ -67,12 +68,11 @@ class SideMenuFragment : GenericFragment<SideMenuFragmentBinding>() {
}
sharedViewModel.accountRemoved.observe(
viewLifecycleOwner,
{
Log.i("[Side Menu] Account removed, update accounts list")
viewModel.updateAccountsList()
}
)
viewLifecycleOwner
) {
Log.i("[Side Menu] Account removed, update accounts list")
viewModel.updateAccountsList()
}
viewModel.accountsSettingsListener = object : SettingListenerStub() {
override fun onAccountClicked(identity: String) {
@ -110,8 +110,15 @@ class SideMenuFragment : GenericFragment<SideMenuFragmentBinding>() {
}
binding.setQuitClickListener {
Log.i("[Side Menu] Quitting app")
requireActivity().finishAndRemoveTask()
coreContext.stop()
if (!corePreferences.keepServiceAlive) {
Log.i("[Side Menu] Stopping Core")
coreContext.stop()
} else {
Log.w("[Side Menu] Keep Service alive setting enabled, don't destroy the Core")
}
}
onBackPressedCallback.isEnabled = false

View file

@ -26,6 +26,7 @@ import android.app.PendingIntent
import android.bluetooth.BluetoothAdapter
import android.content.ContentValues
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.net.Uri
import android.os.Build
@ -240,5 +241,9 @@ class Api21Compatibility {
fun getImeFlagsForSecureChatRoom(): Int {
return EditorInfo.IME_FLAG_NO_EXTRACT_UI
}
fun startForegroundService(context: Context, intent: Intent) {
context.startService(intent)
}
}
}

View file

@ -24,6 +24,7 @@ import android.annotation.SuppressLint
import android.annotation.TargetApi
import android.app.*
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.media.AudioAttributes
import android.os.VibrationEffect
@ -278,5 +279,9 @@ class Api26Compatibility {
fun getImeFlagsForSecureChatRoom(): Int {
return EditorInfo.IME_FLAG_NO_EXTRACT_UI or EditorInfo.IME_FLAG_NO_PERSONALIZED_LEARNING
}
fun startForegroundService(context: Context, intent: Intent) {
context.startForegroundService(intent)
}
}
}

View file

@ -201,6 +201,14 @@ class Compatibility {
return Api26Compatibility.createCallNotification(context, call, notifiable, pendingIntent, channel, notificationsManager)
}
fun startForegroundService(context: Context, intent: Intent) {
if (Version.sdkAboveOrEqual(Version.API26_O_80)) {
Api26Compatibility.startForegroundService(context, intent)
} else {
Api21Compatibility.startForegroundService(context, intent)
}
}
/* Call */
fun canDrawOverlay(context: Context): Boolean {

View file

@ -321,6 +321,11 @@ class CoreContext(val context: Context, coreConfig: Config) {
FileUtils.clearExistingPlainFiles()
}
if (corePreferences.keepServiceAlive) {
Log.i("[Context] Background mode setting is enabled, starting Service")
notificationsManager.startForeground()
}
Log.i("[Context] Started")
}

View file

@ -67,6 +67,8 @@ class CoreService : CoreService() {
} else {
Log.w("[Service] Task removed but Core in not in background, skipping")
}
} else {
Log.i("[Service] Task removed but we were asked to keep the service alive, so doing nothing")
}
super.onTaskRemoved(rootIntent)

View file

@ -248,16 +248,18 @@ class NotificationsManager(private val context: Context) {
// causing the notification to be missed by the user...
Log.i("[Notifications Manager] Getting destroyed, clearing foreground Service & call notifications")
if (currentForegroundServiceNotificationId > 0) {
notificationManager.cancel(currentForegroundServiceNotificationId)
currentForegroundServiceNotificationId = 0
if (currentForegroundServiceNotificationId > 0 && !corePreferences.keepServiceAlive) {
Log.i("[Notifications Manager] Clearing foreground Service")
stopForegroundNotification()
}
for (notifiable in callNotificationsMap.values) {
notificationManager.cancel(notifiable.notificationId)
if (callNotificationsMap.size > 0) {
Log.i("[Notifications Manager] Clearing call notifications")
for (notifiable in callNotificationsMap.values) {
notificationManager.cancel(notifiable.notificationId)
}
}
stopForegroundNotification()
coreContext.core.removeListener(listener)
}
@ -286,7 +288,13 @@ class NotificationsManager(private val context: Context) {
Log.w("[Notifications Manager] Can't start service as foreground without a service, starting it now")
val intent = Intent()
intent.setClass(coreContext.context, CoreService::class.java)
coreContext.context.startService(intent)
try {
Compatibility.startForegroundService(coreContext.context, intent)
} catch (ise: IllegalStateException) {
Log.e("[Notifications Manager] Failed to start Service: $ise")
} catch (se: SecurityException) {
Log.e("[Notifications Manager] Failed to start Service: $se")
}
}
}
@ -294,7 +302,11 @@ class NotificationsManager(private val context: Context) {
service = coreService
when {
currentForegroundServiceNotificationId != 0 -> {
Log.e("[Notifications Manager] There is already a foreground service notification [$currentForegroundServiceNotificationId]")
if (currentForegroundServiceNotificationId != SERVICE_NOTIF_ID) {
Log.e("[Notifications Manager] There is already a foreground service notification [$currentForegroundServiceNotificationId]")
} else {
Log.i("[Notifications Manager] There is already a foreground service notification, no need to use the call notification to keep Service alive")
}
}
coreContext.core.callsNb > 0 -> {
// When this method will be called, we won't have any notification yet