From f296cb7361e917a06750a5b0b808508e656b7b01 Mon Sep 17 00:00:00 2001 From: sim Date: Sun, 2 Oct 2022 17:05:36 +0200 Subject: [PATCH] Restart service when POST_NOTIFICATIONS is granted --- .../nextpush/activities/MainActivity.kt | 36 ++++++++++++------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/MainActivity.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/MainActivity.kt index 69547cf..71c1ae6 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/MainActivity.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/MainActivity.kt @@ -5,6 +5,7 @@ import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import android.content.IntentFilter +import android.content.pm.PackageManager import android.os.Build import android.os.Bundle import android.util.Log @@ -104,19 +105,28 @@ class MainActivity : AppCompatActivity() { private fun requestPermissions() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - registerForActivityResult( - ActivityResultContracts.RequestPermission() - ) { granted -> - if (!granted && - shouldShowRequestPermissionRationale(Manifest.permission.POST_NOTIFICATIONS)) { - AlertDialog.Builder(this) - .setTitle(getString(R.string.no_notification_dialog_title)) - .setMessage(R.string.no_notification_dialog_message) - .show() - } - } .launch( - Manifest.permission.POST_NOTIFICATIONS - ) + if (checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) + != PackageManager.PERMISSION_GRANTED) { + Log.d(TAG, "Requesting POST_NOTIFICATIONS permission") + registerForActivityResult( + ActivityResultContracts.RequestPermission() + ) { granted -> + Log.d(TAG, "POST_NOTIFICATIONS permission granted: $granted") + if (granted) { + restart() + } else { + if (shouldShowRequestPermissionRationale(Manifest.permission.POST_NOTIFICATIONS)) { + Log.d(TAG, "Show POST_NOTIFICATIONS permission rationale") + AlertDialog.Builder(this) + .setTitle(getString(R.string.no_notification_dialog_title)) + .setMessage(R.string.no_notification_dialog_message) + .show() + } + } + }.launch( + Manifest.permission.POST_NOTIFICATIONS + ) + } } }