- Ask POST_NOTIFICATIONS permission on Android 13+ devices

This commit is contained in:
Juha Heinanen
2023-01-16 21:22:20 +02:00
parent f21219e5dc
commit b6bc5376ce
4 changed files with 47 additions and 15 deletions

View File

@ -19,6 +19,7 @@
<uses-permission android:name="android.permission.READ_CONTACTS" /> <uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" /> <uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" /> <uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH" />

View File

@ -630,7 +630,10 @@ class MainActivity : AppCompatActivity() {
BaresipService.callActionUri = URLDecoder.decode(intent.data.toString(), "UTF-8") BaresipService.callActionUri = URLDecoder.decode(intent.data.toString(), "UTF-8")
} }
if (!BaresipService.isServiceRunning) requestPermissionLauncher =
registerForActivityResult(ActivityResultContracts.RequestPermission()) {}
if (!BaresipService.isServiceRunning) {
if (File(filesDir.absolutePath + "/accounts").exists()) { if (File(filesDir.absolutePath + "/accounts").exists()) {
val accounts = String( val accounts = String(
Utils.getFileContents(filesDir.absolutePath + "/accounts")!!, Utils.getFileContents(filesDir.absolutePath + "/accounts")!!,
@ -642,15 +645,13 @@ class MainActivity : AppCompatActivity() {
firstRun = true firstRun = true
startBaresip() startBaresip()
} }
}
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()
} }
requestPermissionLauncher =
registerForActivityResult(ActivityResultContracts.RequestPermission()) {}
} // OnCreate } // OnCreate
override fun onStart() { override fun onStart() {
@ -1180,7 +1181,6 @@ class MainActivity : AppCompatActivity() {
} }
handleNextEvent() handleNextEvent()
} }
private fun reStart() { private fun reStart() {
@ -1560,11 +1560,41 @@ class MainActivity : AppCompatActivity() {
} }
private fun startBaresip() { private fun startBaresip() {
if (Build.VERSION.SDK_INT >= 33) {
when {
ContextCompat.checkSelfPermission(
this,
Manifest.permission.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, Manifest.permission.POST_NOTIFICATIONS
) -> {
layout.showSnackBar(
binding.root,
getString(R.string.no_notifications),
Snackbar.LENGTH_INDEFINITE,
getString(R.string.ok)
) {
requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)
}
}
else -> {
requestPermissionLauncher.launch(Manifest.permission.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",

View File

@ -534,12 +534,11 @@
Android versioon 9 saakka tarkista myös Asetukset → Sovellukset → baresip → Käyttöluvat → Android versioon 9 saakka tarkista myös Asetukset → Sovellukset → baresip → Käyttöluvat →
Tallennustila ja että tiedosto \'%1$s\' on Download-kansiossa. Tallennustila ja että tiedosto \'%1$s\' on Download-kansiossa.
</string> </string>
<string name="no_calls">Et voi soittaa puheluita tai vastata niihin ilman Mikrofoni-käyttöoikeutta.</string> <string name="no_notifications">Et voi käyttää tätä sovellusta ilman Ilmoitukset-lupaa.</string>
<string name="no_video_calls">Salli Kamera-sovelluslupa jotta voit soittaa videopuheluita ja vastata niihin.</string> <string name="no_calls">Et voi soittaa puheluita tai vastata niihin ilman Mikrofoni-lupaa.</string>
<string name="no_backup">Et voi tallentaa sovelluksen tietoja ilman <string name="no_video_calls">Salli Kamera-lupa jotta voit soittaa videopuheluita ja vastata niihin.</string>
Tallennustila-käyttöoikeutta.</string> <string name="no_backup">Et voi tallentaa sovelluksen tietoja ilman Tallennustila-lupaa.</string>
<string name="no_restore">Et voi palauttaa sovelluksen tietoja ilman <string name="no_restore">Et voi palauttaa sovelluksen tietoja ilman Tallennustila-lupaa.</string>
Tallennustila-käyttöoikeutta.</string>
<string name="no_cameras">Sinulla ei ole yhtään tuettua video-kameraa.</string> <string name="no_cameras">Sinulla ei ole yhtään tuettua video-kameraa.</string>
<string name="show_password">Näytä salasana</string> <string name="show_password">Näytä salasana</string>
<string name="no_network">Ei verkkoyhteyttä!</string> <string name="no_network">Ei verkkoyhteyttä!</string>

View File

@ -506,6 +506,8 @@
also check Apps → baresip → Permissions → Storage and that file \'%1$s\' exists also check Apps → baresip → Permissions → Storage and that file \'%1$s\' exists
in Download folder. in Download folder.
</string> </string>
<string name="no_notifications">You are not able to use this application without \"Notifications\"
permission.</string>
<string name="no_calls">You are not able to make or answer calls without \"Microphone\" <string name="no_calls">You are not able to make or answer calls without \"Microphone\"
permission.</string> permission.</string>
<string name="no_video_calls">Grant \"Camera\" permission to make or answer video calls.</string> <string name="no_video_calls">Grant \"Camera\" permission to make or answer video calls.</string>