]> gitweb.stoutner.com Git - PrivacyCell.git/blobdiff - app/src/main/java/com/stoutner/privacycell/fragments/SettingsFragment.kt
Bump target API to 33. https://redmine.stoutner.com/issues/890
[PrivacyCell.git] / app / src / main / java / com / stoutner / privacycell / fragments / SettingsFragment.kt
index 20dfb37be3cdbb74e847fabe05125ba0b4546569..b5acce90fb45e06283ca471915c044a74f364b9f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2021-2022 Soren Stoutner <soren@stoutner.com>.
+ * Copyright 2021-2022 Soren Stoutner <soren@stoutner.com>.
  *
  * This file is part of Privacy Cell <https://www.stoutner.com/privacy-cell>.
  *
@@ -22,7 +22,9 @@ package com.stoutner.privacycell.fragments
 import android.Manifest
 import android.content.Intent
 import android.content.SharedPreferences
+import android.content.SharedPreferences.OnSharedPreferenceChangeListener
 import android.content.pm.PackageManager
+import android.os.Build
 import android.os.Bundle
 import android.os.Handler
 import android.os.Looper
@@ -34,11 +36,13 @@ import androidx.preference.PreferenceFragmentCompat
 import androidx.work.WorkManager
 
 import com.stoutner.privacycell.R
+import com.stoutner.privacycell.activities.PrivacyCellActivity
+import com.stoutner.privacycell.dialogs.NotificationPermissionDialog
 import com.stoutner.privacycell.services.RealtimeMonitoringService
 
 class SettingsFragment : PreferenceFragmentCompat() {
     // Declare the class variables.
-    private lateinit var sharedPreferenceChangeListener: SharedPreferences.OnSharedPreferenceChangeListener
+    private lateinit var sharedPreferenceChangeListener: OnSharedPreferenceChangeListener
 
     // Declare the class views.
     private lateinit var realtimeMonitoringPreference: Preference
@@ -153,9 +157,9 @@ class SettingsFragment : PreferenceFragmentCompat() {
         sharedPreferences.registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener)
     }
 
-    private fun getSharedPreferenceChangeListener(): SharedPreferences.OnSharedPreferenceChangeListener {
+    private fun getSharedPreferenceChangeListener(): OnSharedPreferenceChangeListener {
         // Return the shared preference change listener.
-        return SharedPreferences.OnSharedPreferenceChangeListener {sharedPreferences, key ->
+        return OnSharedPreferenceChangeListener { sharedPreferences: SharedPreferences, key: String? ->
             when (key) {
                 getString(R.string.realtime_monitoring_key) -> {
                     // Update the icon.
@@ -175,8 +179,32 @@ class SettingsFragment : PreferenceFragmentCompat() {
 
                     // Start or stop the service.
                     if (sharedPreferences.getBoolean(getString(R.string.realtime_monitoring_key), false)) {  // Realtime monitoring has been enabled.
-                        // Start the realtime monitoring service.
-                        requireActivity().startService(Intent(context, RealtimeMonitoringService::class.java))
+                        // Start the service according to the API.
+                        if (Build.VERSION.SDK_INT >= 33) {  // The device API is >= 33.
+                            // Check to see if the post notification has been granted.
+                            if (ActivityCompat.checkSelfPermission(requireContext(), Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED) {  // The permission has been granted.
+                                // Start the realtime monitoring service.
+                                requireActivity().startService(Intent(context, RealtimeMonitoringService::class.java))
+                            } else {  // The post notification permission has not been granted.
+                                // Check if the user has previously denied the post notifications permission.
+                                if (ActivityCompat.shouldShowRequestPermissionRationale(requireActivity(), Manifest.permission.POST_NOTIFICATIONS)) {  // Show a dialog explaining the request first.
+                                    // Check to see if a notification permission dialog is already displayed.  This happens if the app is restarted while the dialog is shown.
+                                    if (requireActivity().supportFragmentManager.findFragmentByTag(getString(R.string.notification_permission)) == null) {  // No dialog is currently shown.
+                                        // Instantiate the notification permission dialog fragment.
+                                        val notificationPermissionDialogFragment = NotificationPermissionDialog()
+
+                                        // Show the notification permission alert dialog.  The permission will be requested when the dialog is closed.
+                                        notificationPermissionDialogFragment.show(requireActivity().supportFragmentManager, getString(R.string.notification_permission))
+                                    }
+                                } else {  // Show the permission request directly.
+                                    // Request the post notifications permission directly.
+                                    ActivityCompat.requestPermissions(requireActivity(), arrayOf(Manifest.permission.POST_NOTIFICATIONS), PrivacyCellActivity.NOTIFICATION_PERMISSION_REQUEST_CODE)
+                                }
+                            }
+                        } else {   // The device API is < 33.
+                            // Start the realtime monitoring service.
+                            requireActivity().startService(Intent(context, RealtimeMonitoringService::class.java))
+                        }
                     } else {  // Realtime monitoring has been disabled.
                         // Stop the realtime monitoring service.
                         requireActivity().stopService(Intent(context, RealtimeMonitoringService::class.java))
@@ -232,4 +260,4 @@ class SettingsFragment : PreferenceFragmentCompat() {
         // Restart the activity after 400 milliseconds, so that the app has enough time to save the change to the preference.
         restartHandler.postDelayed(restartRunnable, 400)
     }
-}
\ No newline at end of file
+}