From 59dad7de454f0e4d4715abad40d4c155bca131dc Mon Sep 17 00:00:00 2001 From: Jason Goveas <95233082+JustAGhost23@users.noreply.github.com> Date: Sun, 25 Feb 2024 01:09:06 +0530 Subject: [PATCH] Update requested permissions to interact with storage and send notifications (#351) As of Android 13, apps will have to request permission to send notifications, and already have access to files in their app-specific directory on external storage --- .../crux/bphc/cms/activities/MainActivity.kt | 66 ++++++++++++++----- 1 file changed, 50 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/crux/bphc/cms/activities/MainActivity.kt b/app/src/main/java/crux/bphc/cms/activities/MainActivity.kt index e719ddf0..d4fde3ef 100644 --- a/app/src/main/java/crux/bphc/cms/activities/MainActivity.kt +++ b/app/src/main/java/crux/bphc/cms/activities/MainActivity.kt @@ -4,6 +4,7 @@ import android.Manifest import android.content.Intent import android.content.pm.PackageManager import android.net.Uri +import android.os.Build import android.os.Bundle import android.view.MenuItem import android.widget.Toast @@ -255,19 +256,38 @@ class MainActivity : AppCompatActivity() { } private fun askPermission() { - if (ContextCompat.checkSelfPermission(this, - Manifest.permission.READ_EXTERNAL_STORAGE) - != PackageManager.PERMISSION_GRANTED) { + val requiredPermissions = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + Manifest.permission.POST_NOTIFICATIONS + } + else { + Manifest.permission.READ_EXTERNAL_STORAGE + } + if (ContextCompat.checkSelfPermission( + this, + requiredPermissions + ) + != PackageManager.PERMISSION_GRANTED + ) { // Should we show an explanation? - if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE)) { + if (ActivityCompat.shouldShowRequestPermissionRationale( + this, + requiredPermissions + ) + ) { + val messageText = if (requiredPermissions == Manifest.permission.READ_EXTERNAL_STORAGE) { + "We need permission to move course content" + } + else { + "We need permission to send course notifications" + } MaterialAlertDialogBuilder(this) - .setTitle("Permission required") - .setMessage("We need permission to move course content") - .setPositiveButton("OK") { _, _ -> - requestReadStoragePermission() - } - .show() + .setTitle("Permission required") + .setMessage(messageText) + .setPositiveButton("OK") { _, _ -> + requestReadStoragePermission() + } + .show() } else { requestReadStoragePermission() } @@ -276,14 +296,28 @@ class MainActivity : AppCompatActivity() { } } + private val askPermission = registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { granted -> + if (granted.containsValue(false)) + askPermission() + else + startDataMigrationIfRequired() + } + private fun requestReadStoragePermission() { - val askPermission = registerForActivityResult(ActivityResultContracts.RequestPermission()) { granted -> - if (!granted) - askPermission() - else - startDataMigrationIfRequired() + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + askPermission.launch( + arrayOf( + Manifest.permission.POST_NOTIFICATIONS, + ) + ) + } + else { + askPermission.launch( + arrayOf( + Manifest.permission.READ_EXTERNAL_STORAGE + ) + ) } - askPermission.launch(Manifest.permission.READ_EXTERNAL_STORAGE) } private fun startDataMigrationIfRequired() {