Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update requested permissions to interact with storage and send notifications #351

Merged
merged 1 commit into from
Feb 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 50 additions & 16 deletions app/src/main/java/crux/bphc/cms/activities/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
Expand All @@ -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() {
Expand Down
Loading