]> gitweb.stoutner.com Git - PrivacyCell.git/blobdiff - app/src/main/java/com/stoutner/privacycell/fragments/SettingsFragment.kt
Fix a crash in realtime monitoring when the READ_PHONE_STATE permission is denied...
[PrivacyCell.git] / app / src / main / java / com / stoutner / privacycell / fragments / SettingsFragment.kt
index 573cec3fa62fdce2024fdf6d867879a8a048ca83..dd8c9c728fd08ffa4864c4c76701615a8e507bb3 100644 (file)
 
 package com.stoutner.privacycell.fragments
 
+import android.Manifest
 import android.content.Intent
 import android.content.SharedPreferences
+import android.content.pm.PackageManager
 import android.os.Bundle
 import android.os.Handler
 import android.os.Looper
 import android.provider.Settings
 
+import androidx.core.app.ActivityCompat
 import androidx.preference.Preference
 import androidx.preference.PreferenceFragmentCompat
 import androidx.work.WorkManager
@@ -54,13 +57,22 @@ class SettingsFragment : PreferenceFragmentCompat() {
         val insecureNetworkNotificationPreference = findPreference<Preference>(getString(R.string.insecure_network_notification_key))!!
         bottomAppBarPreference = findPreference(getString(R.string.bottom_app_bar_key))!!
 
-        // Set the realtime monitoring preference icon.
-        if (sharedPreferences.getBoolean(getString(R.string.realtime_monitoring_key), false)) {
-            // Set the enabled icon.
-            realtimeMonitoringPreference.setIcon(R.drawable.realtime_monitoring_enabled)
+        // Only enable the realtime monitoring preference if the READ_PHONE_STATE permission has been granted.
+        realtimeMonitoringPreference.isEnabled = (ActivityCompat.checkSelfPermission(requireContext(), Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED)
+
+        // Set the realtime monitoring icon according to the status.
+        if (realtimeMonitoringPreference.isEnabled) {
+            // Set the realtime monitoring preference icon.
+            if (sharedPreferences.getBoolean(getString(R.string.realtime_monitoring_key), false)) {
+                // Set the enabled icon.
+                realtimeMonitoringPreference.setIcon(R.drawable.realtime_monitoring_enabled)
+            } else {
+                // Set the disabled icon.
+                realtimeMonitoringPreference.setIcon(R.drawable.realtime_monitoring_disabled)
+            }
         } else {
-            // Set the disabled icon.
-            realtimeMonitoringPreference.setIcon(R.drawable.realtime_monitoring_disabled)
+            // Set the ghosted icon.
+            realtimeMonitoringPreference.setIcon(R.drawable.realtime_monitoring_ghosted)
         }
 
         // Set the notification preferences to depend on the realtime monitoring preference.