]> gitweb.stoutner.com Git - PrivacyCell.git/blobdiff - app/src/main/java/com/stoutner/privacycell/services/RealtimeMonitoringService.kt
Create a protocols activity. https://redmine.stoutner.com/issues/774
[PrivacyCell.git] / app / src / main / java / com / stoutner / privacycell / services / RealtimeMonitoringService.kt
index 23e7278c9bd3a20629fd4ebc005adb1ec12cd564..7d467dd1d2576165407af96325896c9d6cf0d90d 100644 (file)
@@ -31,7 +31,7 @@ import android.content.Intent
 import android.content.pm.PackageManager
 import android.os.Binder
 import android.os.IBinder
 import android.content.pm.PackageManager
 import android.os.Binder
 import android.os.IBinder
-import android.telephony.PhoneStateListener
+import android.telephony.PhoneStateListener  // This can be replaced by `TelephonyCallback` once the minimum API >= 31.
 import android.telephony.TelephonyDisplayInfo
 import android.telephony.TelephonyManager
 
 import android.telephony.TelephonyDisplayInfo
 import android.telephony.TelephonyManager
 
@@ -42,7 +42,7 @@ import androidx.work.WorkManager
 
 import com.stoutner.privacycell.R
 import com.stoutner.privacycell.activities.PrivacyCellActivity
 
 import com.stoutner.privacycell.R
 import com.stoutner.privacycell.activities.PrivacyCellActivity
-import com.stoutner.privacycell.workers.RegisterRealtimeListener
+import com.stoutner.privacycell.workers.RegisterRealtimeListenerWorker
 
 import java.util.concurrent.TimeUnit
 
 
 import java.util.concurrent.TimeUnit
 
@@ -60,7 +60,7 @@ class RealtimeMonitoringService : Service() {
 
     // Define the class variables.
     private var currentStatus = ""
 
     // Define the class variables.
     private var currentStatus = ""
-    private lateinit var phoneStateListener: PhoneStateListener
+    private lateinit var phoneStateListener: PhoneStateListener  // The `PhoneStateListener` can be replaced by `TelephonyCallback` once the minimum API >= 31.
 
     inner class ServiceBinder : Binder() {
         // Get a copy of this service as a binder.
 
     inner class ServiceBinder : Binder() {
         // Get a copy of this service as a binder.
@@ -127,7 +127,7 @@ class RealtimeMonitoringService : Service() {
         // Start the foreground notification.
         startForeground(NOTIFICATION_ID, notificationBuilder.build())
 
         // Start the foreground notification.
         startForeground(NOTIFICATION_ID, notificationBuilder.build())
 
-        // Define the phone state listener.
+        // Define the phone state listener.  The `PhoneStateListener` can be replaced by `TelephonyCallback` once the minimum API >= 31.
         phoneStateListener = object : PhoneStateListener() {
             override fun onDisplayInfoChanged(telephonyDisplayInfo: TelephonyDisplayInfo) {
                 // Populate the notification according to the network type.
         phoneStateListener = object : PhoneStateListener() {
             override fun onDisplayInfoChanged(telephonyDisplayInfo: TelephonyDisplayInfo) {
                 // Populate the notification according to the network type.
@@ -187,7 +187,7 @@ class RealtimeMonitoringService : Service() {
         if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
             // Create a register realtime listener work request that fires every hour.
             // This periodic request will fire shortly after being created (it fires about every hour near the beginning of the hour) and will reregister the listener if it gets garbage collected.
         if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
             // Create a register realtime listener work request that fires every hour.
             // This periodic request will fire shortly after being created (it fires about every hour near the beginning of the hour) and will reregister the listener if it gets garbage collected.
-            val registerRealtimeListenerWorkRequest = PeriodicWorkRequestBuilder<RegisterRealtimeListener>(1, TimeUnit.HOURS).build()
+            val registerRealtimeListenerWorkRequest = PeriodicWorkRequestBuilder<RegisterRealtimeListenerWorker>(1, TimeUnit.HOURS).build()
 
             // Register the realtime listener work request.
             WorkManager.getInstance(this).enqueueUniquePeriodicWork(getString(R.string.register_listener_work_request), ExistingPeriodicWorkPolicy.REPLACE, registerRealtimeListenerWorkRequest)
 
             // Register the realtime listener work request.
             WorkManager.getInstance(this).enqueueUniquePeriodicWork(getString(R.string.register_listener_work_request), ExistingPeriodicWorkPolicy.REPLACE, registerRealtimeListenerWorkRequest)
@@ -203,10 +203,10 @@ class RealtimeMonitoringService : Service() {
             // Get a handle for the telephony manager.
             val telephonyManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
 
             // Get a handle for the telephony manager.
             val telephonyManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
 
-            // Cancel the current listener if it exists.
+            // Cancel the current listener if it exists.  The `PhoneStateListener` can be replaced by `TelephonyCallback` once the minimum API >= 31.
             telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE)
 
             telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE)
 
-            // Listen for changes to the phone state.
+            // Listen for changes to the phone state.  The `PhoneStateListener` can be replaced by `TelephonyCallback` once the minimum API >= 31.
             telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_DISPLAY_INFO_CHANGED)
         }
     }
             telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_DISPLAY_INFO_CHANGED)
         }
     }