From d6494cd27c74b0dc1c208f36428d592f9855bdad Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Mon, 24 Mar 2025 17:09:20 +0100 Subject: [PATCH] Ask for full screen intent if not granted --- .../java/org/linphone/ui/main/MainActivity.kt | 27 ++++++++++- .../ui/main/viewmodel/MainViewModel.kt | 45 ++++++++++++------- app/src/main/res/values-fr/strings.xml | 1 + app/src/main/res/values/strings.xml | 1 + 4 files changed, 56 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/org/linphone/ui/main/MainActivity.kt b/app/src/main/java/org/linphone/ui/main/MainActivity.kt index 99f96af86..f8effd852 100644 --- a/app/src/main/java/org/linphone/ui/main/MainActivity.kt +++ b/app/src/main/java/org/linphone/ui/main/MainActivity.kt @@ -120,12 +120,23 @@ class MainActivity : GenericActivity() { ) { isGranted -> if (isGranted) { Log.i("$TAG POST_NOTIFICATIONS permission has been granted") - viewModel.updatePostNotificationsPermission() + viewModel.updateMissingPermissionAlert() } else { Log.w("$TAG POST_NOTIFICATIONS permission has been denied!") } } + private val fullScreenIntentPermissionLauncher = registerForActivityResult( + ActivityResultContracts.RequestPermission() + ) { isGranted -> + if (isGranted) { + Log.i("$TAG USE_FULL_SCREEN_INTENT permission has been granted") + viewModel.updateMissingPermissionAlert() + } else { + Log.w("$TAG USE_FULL_SCREEN_INTENT permission has been denied!") + } + } + @SuppressLint("InlinedApi") override fun onCreate(savedInstanceState: Bundle?) { // Must be done before the setContentView @@ -204,6 +215,18 @@ class MainActivity : GenericActivity() { } } + viewModel.askFullScreenIntentPermissionEvent.observe(this) { + it.consume { + if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.USE_FULL_SCREEN_INTENT)) { + Log.w("$TAG Asking for USE_FULL_SCREEN_INTENT permission") + fullScreenIntentPermissionLauncher.launch(Manifest.permission.USE_FULL_SCREEN_INTENT) + } else { + Log.i("$TAG Permission request for USE_FULL_SCREEN_INTENT will be automatically denied, go to manage app full screen intent android settings instead") + Compatibility.requestFullScreenIntentPermission(this) + } + } + } + viewModel.defaultAccountRegistrationErrorEvent.observe(this) { it.consume { error -> val tag = "DEFAULT_ACCOUNT_REGISTRATION_ERROR" @@ -397,7 +420,7 @@ class MainActivity : GenericActivity() { viewModel.enableAccountMonitoring(true) viewModel.checkForNewAccount() viewModel.updateNetworkReachability() - viewModel.updatePostNotificationsPermission() + viewModel.updateMissingPermissionAlert() } override fun onNewIntent(intent: Intent) { diff --git a/app/src/main/java/org/linphone/ui/main/viewmodel/MainViewModel.kt b/app/src/main/java/org/linphone/ui/main/viewmodel/MainViewModel.kt index 83c0077cc..b6ff5e166 100644 --- a/app/src/main/java/org/linphone/ui/main/viewmodel/MainViewModel.kt +++ b/app/src/main/java/org/linphone/ui/main/viewmodel/MainViewModel.kt @@ -19,13 +19,10 @@ */ package org.linphone.ui.main.viewmodel -import android.Manifest -import android.content.pm.PackageManager import android.os.Build import androidx.annotation.RequiresApi import androidx.annotation.UiThread import androidx.annotation.WorkerThread -import androidx.core.content.ContextCompat import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope @@ -33,6 +30,7 @@ import kotlinx.coroutines.launch import org.linphone.LinphoneApplication.Companion.coreContext import org.linphone.LinphoneApplication.Companion.corePreferences import org.linphone.R +import org.linphone.compatibility.Compatibility import org.linphone.core.Account import org.linphone.core.Call import org.linphone.core.ChatMessage @@ -60,6 +58,7 @@ class MainViewModel const val MWI_MESSAGES_WAITING = 4 const val NON_DEFAULT_ACCOUNT_NOTIFICATIONS = 5 const val NON_DEFAULT_ACCOUNT_NOT_CONNECTED = 10 + const val FULL_SCREEN_INTENTS_PERMISSION_NOT_GRANTED = 16 const val SEND_NOTIFICATIONS_PERMISSION_NOT_GRANTED = 17 const val NETWORK_NOT_REACHABLE = 19 const val SINGLE_CALL = 20 @@ -94,6 +93,10 @@ class MainViewModel MutableLiveData>() } + val askFullScreenIntentPermissionEvent: MutableLiveData> by lazy { + MutableLiveData>() + } + val showNewAccountToastEvent: MutableLiveData> by lazy { MutableLiveData>() } @@ -350,7 +353,7 @@ class MainViewModel } } - updatePostNotificationsPermission() + updateMissingPermissionAlert() if (VFS.isEnabled(coreContext.context)) { val cache = corePreferences.vfsCachePath @@ -383,8 +386,13 @@ class MainViewModel } @UiThread - fun updatePostNotificationsPermission() { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + fun updateMissingPermissionAlert() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { + coreContext.postOnCoreThread { + checkFullScreenIntentNotificationPermission() + checkPostNotificationsPermission() + } + } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { coreContext.postOnCoreThread { checkPostNotificationsPermission() } @@ -418,7 +426,9 @@ class MainViewModel fun onTopBarClicked() { if (atLeastOneCall.value == true) { goBackToCallEvent.value = Event(true) - } else if (!isPostNotificationsPermissionGranted()) { + } else if (!Compatibility.hasFullScreenIntentPermission(coreContext.context)) { + askFullScreenIntentPermissionEvent.value = Event(true) + } else if (!Compatibility.isPostNotificationsPermissionGranted(coreContext.context)) { askPostNotificationsPermissionEvent.value = Event(true) } else { openDrawerEvent.value = Event(true) @@ -554,7 +564,7 @@ class MainViewModel NETWORK_NOT_REACHABLE -> { alertIcon.postValue(R.drawable.wifi_slash) } - SEND_NOTIFICATIONS_PERMISSION_NOT_GRANTED -> { + SEND_NOTIFICATIONS_PERMISSION_NOT_GRANTED, FULL_SCREEN_INTENTS_PERMISSION_NOT_GRANTED -> { alertIcon.postValue(R.drawable.bell_simple_slash) } SINGLE_CALL, MULTIPLE_CALLS -> { @@ -598,21 +608,24 @@ class MainViewModel } } - private fun isPostNotificationsPermissionGranted(): Boolean { - return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - ContextCompat.checkSelfPermission( - coreContext.context, - Manifest.permission.POST_NOTIFICATIONS - ) == PackageManager.PERMISSION_GRANTED + @RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE) + @WorkerThread + private fun checkFullScreenIntentNotificationPermission() { + if (!Compatibility.hasFullScreenIntentPermission(coreContext.context)) { + Log.w("$TAG USE_FULL_SCREEN_INTENT seems to be not granted!") + val label = AppUtils.getString(R.string.full_screen_intent_permission_not_granted) + coreContext.postOnCoreThread { + addAlert(FULL_SCREEN_INTENTS_PERMISSION_NOT_GRANTED, label) + } } else { - true + removeAlert(FULL_SCREEN_INTENTS_PERMISSION_NOT_GRANTED) } } @RequiresApi(Build.VERSION_CODES.TIRAMISU) @WorkerThread private fun checkPostNotificationsPermission() { - if (!isPostNotificationsPermissionGranted()) { + if (!Compatibility.isPostNotificationsPermissionGranted(coreContext.context)) { Log.w("$TAG POST_NOTIFICATIONS seems to be not granted!") val label = AppUtils.getString(R.string.post_notifications_permission_not_granted) coreContext.postOnCoreThread { diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index 199f90295..736e51e0c 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -790,6 +790,7 @@ Favoris Suggestions La permission de poster des notifications n\'est pas donnée ! + La permission d\'afficher les appels entrants n\'est pas donnée ! %s message vocal en attente %s messages vocaux en attente diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e18b0e560..492ac6b3b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -831,6 +831,7 @@ Favorites Suggestions Post notifications permission not granted! + Show incoming call permission not granted! %s new voice message %s new voice messages