Improved permissions handling
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
package com.tutpro.baresip
|
package com.tutpro.baresip
|
||||||
|
|
||||||
import android.Manifest
|
|
||||||
import android.Manifest.permission.*
|
import android.Manifest.permission.*
|
||||||
import android.annotation.SuppressLint
|
import android.annotation.SuppressLint
|
||||||
import android.app.Activity
|
import android.app.Activity
|
||||||
@@ -107,7 +106,6 @@ class MainActivity : AppCompatActivity() {
|
|||||||
private var resumeUap = 0L
|
private var resumeUap = 0L
|
||||||
private var resumeCall: Call? = null
|
private var resumeCall: Call? = null
|
||||||
private var resumeAction = ""
|
private var resumeAction = ""
|
||||||
private var firstRun = false
|
|
||||||
|
|
||||||
private val onBackPressedCallback = object : OnBackPressedCallback(true) {
|
private val onBackPressedCallback = object : OnBackPressedCallback(true) {
|
||||||
override fun handleOnBackPressed() {
|
override fun handleOnBackPressed() {
|
||||||
@@ -353,7 +351,7 @@ class MainActivity : AppCompatActivity() {
|
|||||||
|
|
||||||
callButton.setOnClickListener {
|
callButton.setOnClickListener {
|
||||||
if (aorSpinner.selectedItemPosition >= 0) {
|
if (aorSpinner.selectedItemPosition >= 0) {
|
||||||
if (Utils.checkPermissions(this, arrayOf(Manifest.permission.RECORD_AUDIO)))
|
if (Utils.checkPermissions(this, arrayOf(RECORD_AUDIO)))
|
||||||
makeCall()
|
makeCall()
|
||||||
else
|
else
|
||||||
Toast.makeText(applicationContext, R.string.no_calls, Toast.LENGTH_SHORT).show()
|
Toast.makeText(applicationContext, R.string.no_calls, Toast.LENGTH_SHORT).show()
|
||||||
@@ -662,6 +660,46 @@ class MainActivity : AppCompatActivity() {
|
|||||||
BaresipService.callActionUri = URLDecoder.decode(intent.data.toString(), "UTF-8")
|
BaresipService.callActionUri = URLDecoder.decode(intent.data.toString(), "UTF-8")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
permissions = if (Build.VERSION.SDK_INT >= 33)
|
||||||
|
arrayOf(POST_NOTIFICATIONS, RECORD_AUDIO, BLUETOOTH_CONNECT)
|
||||||
|
else if (Build.VERSION.SDK_INT >= 31)
|
||||||
|
arrayOf(RECORD_AUDIO, BLUETOOTH_CONNECT)
|
||||||
|
else
|
||||||
|
arrayOf(RECORD_AUDIO)
|
||||||
|
|
||||||
|
requestPermissionLauncher =
|
||||||
|
registerForActivityResult(ActivityResultContracts.RequestPermission()) {}
|
||||||
|
requestPermissionsLauncher =
|
||||||
|
registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) {
|
||||||
|
val denied = mutableListOf<String>()
|
||||||
|
val shouldShow = mutableListOf<String>()
|
||||||
|
it.forEach { permission ->
|
||||||
|
Log.e(TAG, "${permission.key} : ${permission.value}")
|
||||||
|
if (!permission.value) {
|
||||||
|
denied.add(permission.key)
|
||||||
|
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
|
||||||
|
permission.key))
|
||||||
|
shouldShow.add(permission.key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (denied.contains(POST_NOTIFICATIONS) &&
|
||||||
|
!shouldShow.contains(POST_NOTIFICATIONS)) {
|
||||||
|
with(MaterialAlertDialogBuilder(this, R.style.AlertDialogTheme)) {
|
||||||
|
setTitle("Cannot start baresip")
|
||||||
|
setMessage("Notifications permission has not been granted")
|
||||||
|
setPositiveButton(getString(R.string.ok)) { _, _ ->
|
||||||
|
quitRestart(false)
|
||||||
|
}
|
||||||
|
show()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (shouldShow.isNotEmpty())
|
||||||
|
askPermissions(permissions)
|
||||||
|
else
|
||||||
|
startBaresip()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!BaresipService.isServiceRunning) {
|
if (!BaresipService.isServiceRunning) {
|
||||||
if (File(filesDir.absolutePath + "/accounts").exists()) {
|
if (File(filesDir.absolutePath + "/accounts").exists()) {
|
||||||
val accounts = String(
|
val accounts = String(
|
||||||
@@ -671,21 +709,10 @@ class MainActivity : AppCompatActivity() {
|
|||||||
askPasswords(accounts)
|
askPasswords(accounts)
|
||||||
} else {
|
} else {
|
||||||
// Baresip is started for the first time
|
// Baresip is started for the first time
|
||||||
firstRun = true
|
askPermissions(permissions)
|
||||||
startBaresip()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
permissions = if (Build.VERSION.SDK_INT >= 31)
|
|
||||||
arrayOf(RECORD_AUDIO, BLUETOOTH_CONNECT)
|
|
||||||
else
|
|
||||||
arrayOf(RECORD_AUDIO)
|
|
||||||
|
|
||||||
requestPermissionLauncher =
|
|
||||||
registerForActivityResult(ActivityResultContracts.RequestPermission()) {}
|
|
||||||
requestPermissionsLauncher =
|
|
||||||
registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) {}
|
|
||||||
|
|
||||||
if (Preferences(applicationContext).displayTheme != AppCompatDelegate.getDefaultNightMode()) {
|
if (Preferences(applicationContext).displayTheme != AppCompatDelegate.getDefaultNightMode()) {
|
||||||
AppCompatDelegate.setDefaultNightMode(Preferences(applicationContext).displayTheme)
|
AppCompatDelegate.setDefaultNightMode(Preferences(applicationContext).displayTheme)
|
||||||
delegate.applyDayNight()
|
delegate.applyDayNight()
|
||||||
@@ -697,15 +724,6 @@ class MainActivity : AppCompatActivity() {
|
|||||||
super.onStart()
|
super.onStart()
|
||||||
Log.e(TAG, "Main onStart")
|
Log.e(TAG, "Main onStart")
|
||||||
|
|
||||||
|
|
||||||
if (!Utils.checkPermissions(this, permissions))
|
|
||||||
if (firstRun) {
|
|
||||||
firstRun = false
|
|
||||||
askPermissions(permissions)
|
|
||||||
} else {
|
|
||||||
requestPermissionsLauncher.launch(permissions)
|
|
||||||
}
|
|
||||||
|
|
||||||
val action = intent.getStringExtra("action")
|
val action = intent.getStringExtra("action")
|
||||||
if (action != null) {
|
if (action != null) {
|
||||||
// MainActivity was not visible when call, message, or transfer request came in
|
// MainActivity was not visible when call, message, or transfer request came in
|
||||||
@@ -1592,46 +1610,16 @@ class MainActivity : AppCompatActivity() {
|
|||||||
askPasswords(accounts)
|
askPasswords(accounts)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
startBaresip()
|
requestPermissionsLauncher.launch(permissions)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun startBaresip() {
|
private fun startBaresip() {
|
||||||
if (Build.VERSION.SDK_INT >= 33) {
|
|
||||||
when {
|
|
||||||
ContextCompat.checkSelfPermission(
|
|
||||||
this,
|
|
||||||
POST_NOTIFICATIONS
|
|
||||||
) == PackageManager.PERMISSION_GRANTED -> {
|
|
||||||
Log.d(TAG, "Notifications permission granted")
|
|
||||||
baresipService.action = "Start"
|
baresipService.action = "Start"
|
||||||
startService(baresipService)
|
startService(baresipService)
|
||||||
if (atStartup)
|
if (atStartup)
|
||||||
moveTaskToBack(true)
|
moveTaskToBack(true)
|
||||||
}
|
}
|
||||||
ActivityCompat.shouldShowRequestPermissionRationale(
|
|
||||||
this, POST_NOTIFICATIONS
|
|
||||||
) -> {
|
|
||||||
layout.showSnackBar(
|
|
||||||
binding.root,
|
|
||||||
getString(R.string.no_notifications),
|
|
||||||
Snackbar.LENGTH_INDEFINITE,
|
|
||||||
getString(R.string.ok)
|
|
||||||
) {
|
|
||||||
requestPermissionLauncher.launch(POST_NOTIFICATIONS)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else -> {
|
|
||||||
requestPermissionLauncher.launch(POST_NOTIFICATIONS)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
baresipService.action = "Start"
|
|
||||||
startService(baresipService)
|
|
||||||
if (atStartup)
|
|
||||||
moveTaskToBack(true)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun backup(password: String) {
|
private fun backup(password: String) {
|
||||||
val files = arrayListOf("accounts", "call_history", "config", "contacts", "messages", "uuid",
|
val files = arrayListOf("accounts", "call_history", "config", "contacts", "messages", "uuid",
|
||||||
|
|||||||
@@ -542,10 +542,10 @@
|
|||||||
<string name="audio_focus_denied">Audio fokus evätty!</string>
|
<string name="audio_focus_denied">Audio fokus evätty!</string>
|
||||||
<string name="permissions_rationale">Tarvittavat luvat</string>
|
<string name="permissions_rationale">Tarvittavat luvat</string>
|
||||||
<string name="audio_permissions">baresip tarvitsee Mikrofoni-luvan puheluita varten ja
|
<string name="audio_permissions">baresip tarvitsee Mikrofoni-luvan puheluita varten ja
|
||||||
Lähellä olevat laitteet -luvan Bluetooth-mikrofonin/kaiuttimen havaitsemista varten, mikäli
|
Lähellä olevat laitteet -luvan Bluetooth-mikrofonin/kaiuttimen havaitsemista varten ja
|
||||||
sellaista halutaan käyttää, ja Ilmoitukset-luvan ilmoitusten lähettämistä varten.</string>
|
Ilmoitukset-luvan ilmoitusten lähettämistä varten.</string>
|
||||||
<string name="audio_and_video_permissions">baresip tarvitsee Mikrofoni-luvan puheluita varten,
|
<string name="audio_and_video_permissions">baresip tarvitsee Mikrofoni-luvan puheluita varten,
|
||||||
Kamera-luvan videopuheluita varten ja Lähellä olevat laitteet -luvan Bluetooth-mikrofonin/kaiuttimen
|
Kamera-luvan videopuheluita varten ja Lähellä olevat laitteet -luvan
|
||||||
havaitsemista varten, mikäli sellaista halutaan käyttää, ja Ilmoitukset-luvan ilmoitusten
|
Bluetooth-mikrofonin/kaiuttimen havaitsemista varten ja Ilmoitukset-luvan ilmoitusten
|
||||||
lähettämistä varten.</string>
|
näyttämistä varten.</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
|||||||
@@ -522,10 +522,9 @@
|
|||||||
<string name="audio_focus_denied">Audio focus denied!</string>
|
<string name="audio_focus_denied">Audio focus denied!</string>
|
||||||
<string name="permissions_rationale">Permissions rationale</string>
|
<string name="permissions_rationale">Permissions rationale</string>
|
||||||
<string name="audio_permissions">baresip needs \"Microphone\" permission for voice calls,
|
<string name="audio_permissions">baresip needs \"Microphone\" permission for voice calls,
|
||||||
\"Nearby devices\" permission for Bluetooth microphone/speaker detection if you want to use
|
\"Nearby devices\" permission for Bluetooth microphone/speaker detection, and
|
||||||
such a device, and \"Notifications\" permission for sending notifications.</string>
|
\"Notifications\" permission for sending notifications.</string>
|
||||||
<string name="audio_and_video_permissions">baresip needs \"Microphone\" permission for voice calls,
|
<string name="audio_and_video_permissions">baresip needs \"Microphone\" permission for voice calls,
|
||||||
\"Camera\" permission for video calls, and \"Nearby devices\" permission for Bluetooth
|
\"Camera\" permission for video calls, and \"Nearby devices\" permission for Bluetooth
|
||||||
microphone/speaker detection if you want to use such a device, and \"Notifications\"
|
microphone/speaker detection, and \"Notifications\"permission for posting notifications.</string>
|
||||||
permission for posting notifications.</string>
|
|
||||||
</resources>
|
</resources>
|
||||||
|
|||||||
Reference in New Issue
Block a user