From 86731d839decc4df39f2fa765f6727e64e151cb6 Mon Sep 17 00:00:00 2001 From: Soren Stoutner Date: Mon, 30 May 2022 11:15:26 -0700 Subject: [PATCH] Improve logic for turning off realtime monitoring. https://redmine.stoutner.com/issues/850 --- app/build.gradle | 8 +- app/src/main/AndroidManifest.xml | 1 - .../activities/PrivacyCellActivity.kt | 2 + .../services/RealtimeMonitoringService.kt | 304 +++++++++--------- .../workers/RegisterRealtimeListenerWorker.kt | 8 +- .../workers/RestartServiceWorker.kt | 23 +- app/src/main/res/layout/webview_dialog.xml | 6 +- build.gradle | 4 +- gradle/wrapper/gradle-wrapper.properties | 2 +- 9 files changed, 195 insertions(+), 163 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index f697490..c19642c 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -23,7 +23,7 @@ plugins { } android { - compileSdk 31 + compileSdk 32 defaultConfig { applicationId "com.stoutner.privacycell" @@ -45,6 +45,8 @@ android { proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } + + namespace 'com.stoutner.privacycell' } dependencies { @@ -57,8 +59,8 @@ dependencies { implementation 'androidx.work:work-runtime-ktx:2.7.1' // Include the Kotlin standard library. This should be the same version number listed in the project build.gradle. - implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.6.10' + implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.6.21' // Include the Google material library. - implementation 'com.google.android.material:material:1.5.0' + implementation 'com.google.android.material:material:1.6.0' } \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 4deb14c..408848d 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -22,7 +22,6 @@ diff --git a/app/src/main/java/com/stoutner/privacycell/activities/PrivacyCellActivity.kt b/app/src/main/java/com/stoutner/privacycell/activities/PrivacyCellActivity.kt index 86e7be5..16772d2 100644 --- a/app/src/main/java/com/stoutner/privacycell/activities/PrivacyCellActivity.kt +++ b/app/src/main/java/com/stoutner/privacycell/activities/PrivacyCellActivity.kt @@ -142,6 +142,7 @@ class PrivacyCellActivity : AppCompatActivity(), NavigationView.OnNavigationItem // Define the phone state listener. The `PhoneStateListener` can be replaced by `TelephonyCallback` once the minimum API >= 31. phoneStateListener = object : PhoneStateListener() { + @Deprecated("Deprecated in Java") @SuppressLint("SwitchIntDef") override fun onDisplayInfoChanged(telephonyDisplayInfo: TelephonyDisplayInfo) { // Declare the stingray dialog type integer. @@ -261,6 +262,7 @@ class PrivacyCellActivity : AppCompatActivity(), NavigationView.OnNavigationItem } } + @Deprecated("Deprecated in Java") override fun onServiceStateChanged(serviceState: ServiceState) { // Get the network registration info for the voice network, which is the second of the three entries (the first appears to be Wi-Fi and the third appears to be the cell data network). val networkRegistrationInfo = serviceState.networkRegistrationInfoList[1] diff --git a/app/src/main/java/com/stoutner/privacycell/services/RealtimeMonitoringService.kt b/app/src/main/java/com/stoutner/privacycell/services/RealtimeMonitoringService.kt index a48757f..68f9228 100644 --- a/app/src/main/java/com/stoutner/privacycell/services/RealtimeMonitoringService.kt +++ b/app/src/main/java/com/stoutner/privacycell/services/RealtimeMonitoringService.kt @@ -81,163 +81,171 @@ class RealtimeMonitoringService : Service() { // Get a handle for the shared preferences. val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this) - // Get a handle for the notification manager. - val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager + // Check to see if realtime monitoring is enabled. Sometimes the shared preferences can't return a value in time, because Android sucks. + // So, the default value is set to true, which is the safest value if the shared preferences can't be queried. + if (sharedPreferences.getBoolean(applicationContext.getString(R.string.realtime_monitoring_key), true)) { // Realtime monitoring is enabled. + // Get a handle for the notification manager. + val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager + + // Create a notification channel group. + notificationManager.createNotificationChannelGroup(NotificationChannelGroup(REALTIME_MONITORING, getString(R.string.realtime_monitoring))) + + // Prepare the notification channels. + val secureNetworkChannel = NotificationChannel(SECURE_NETWORK, getString(R.string.secure_network_channel), NotificationManager.IMPORTANCE_HIGH) + val insecureNetworkChannel = NotificationChannel(INSECURE_NETWORK, getString(R.string.insecure_network_channel), NotificationManager.IMPORTANCE_HIGH) + val antiquatedNetworkChannel = NotificationChannel(ANTIQUATED_NETWORK, getString(R.string.antiquated_network_channel), NotificationManager.IMPORTANCE_HIGH) + val unknownNetworkChannel = NotificationChannel(UNKNOWN_NETWORK, getString(R.string.unknown_network_channel), NotificationManager.IMPORTANCE_LOW) + + // Set the notification channel group. + secureNetworkChannel.group = REALTIME_MONITORING + insecureNetworkChannel.group = REALTIME_MONITORING + antiquatedNetworkChannel.group = REALTIME_MONITORING + unknownNetworkChannel.group = REALTIME_MONITORING + + // Disable the notification dots. + secureNetworkChannel.setShowBadge(false) + insecureNetworkChannel.setShowBadge(false) + antiquatedNetworkChannel.setShowBadge(false) + unknownNetworkChannel.setShowBadge(false) + + // Set the primary channel notifications to be public. + secureNetworkChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC + insecureNetworkChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC + antiquatedNetworkChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC + + // Create the notification channels. + notificationManager.createNotificationChannel(secureNetworkChannel) + notificationManager.createNotificationChannel(insecureNetworkChannel) + notificationManager.createNotificationChannel(antiquatedNetworkChannel) + notificationManager.createNotificationChannel(unknownNetworkChannel) + + // Create a notification builder. + val notificationBuilder = Notification.Builder(this, UNKNOWN_NETWORK) + + // Create an intent to open Privacy Cell. + val privacyCellIntent = Intent(this, PrivacyCellActivity::class.java) + + // Create a pending intent from the Privacy Cell intent. + val privacyCellPendingIntent = PendingIntent.getActivity(this, 0, privacyCellIntent, PendingIntent.FLAG_IMMUTABLE) + + // Set the notification to open Privacy Cell. + notificationBuilder.setContentIntent(privacyCellPendingIntent) + + // Set the notification text. + notificationBuilder.setContentText(getString(R.string.unknown_network)) + + // Set the notification icon. + notificationBuilder.setSmallIcon(R.drawable.antiquated_notification_enabled) + + // Set the color. + notificationBuilder.setColor(getColor(R.color.red_notification_icon)) + + // Start the foreground notification. + startForeground(NOTIFICATION_ID, notificationBuilder.build()) + + // Define the phone state listener. The `PhoneStateListener` can be replaced by `TelephonyCallback` once the minimum API >= 31. + phoneStateListener = object : PhoneStateListener() { + @Deprecated("Deprecated in Java") + override fun onDisplayInfoChanged(telephonyDisplayInfo: TelephonyDisplayInfo) { + // Get the consider 3G antiquated preference. + val consider3gAntiquated = sharedPreferences.getBoolean(getString(R.string.consider_3g_antiquated_key), false) + + // Populate the notification according to the network type. + if ((telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_NR) || (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_IWLAN) || + (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_UNKNOWN)) { // This is a secure network. + // Only update the notification if the network status has changed. + if (currentStatus != SECURE_NETWORK) { + // Create a secure network notification builder. + val secureNetworkNotificationBuilder = Notification.Builder(applicationContext, SECURE_NETWORK) + + // Set the notification to open Privacy Cell. + secureNetworkNotificationBuilder.setContentIntent(privacyCellPendingIntent) + + // Set the notification text. + secureNetworkNotificationBuilder.setContentText(getString(R.string.secure_network)) + + // Set the notification icon. + secureNetworkNotificationBuilder.setSmallIcon(R.drawable.secure_notification_enabled) + + // Set the color. + secureNetworkNotificationBuilder.setColor(getColor(R.color.blue_icon)) + + // Update the notification. + notificationManager.notify(NOTIFICATION_ID, secureNetworkNotificationBuilder.build()) + + // Store the new network status. + currentStatus = SECURE_NETWORK + } + } else if ((telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_LTE) || (!consider3gAntiquated && (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_1xRTT || + (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_EVDO_0) || (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_EVDO_A) || + (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_EVDO_B) || (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_EHRPD) || + (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_UMTS) || (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_TD_SCDMA) || + (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_HSDPA) || (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_HSUPA) || + (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_HSPA) || (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_HSPAP)))) { + // This is an insecure network. + // Only update the notification if the network status has changed. + if (currentStatus != INSECURE_NETWORK) { + // Create an insecure network notification builder. + val insecureNetworkNotificationBuilder = Notification.Builder(applicationContext, INSECURE_NETWORK) + + // Set the notification to open Privacy Cell. + insecureNetworkNotificationBuilder.setContentIntent(privacyCellPendingIntent) + + // Set the notification text. + insecureNetworkNotificationBuilder.setContentText(getString(R.string.insecure_network)) + + // Set the notification icon. + insecureNetworkNotificationBuilder.setSmallIcon(R.drawable.insecure_notification_enabled) + + // Set the color. + insecureNetworkNotificationBuilder.setColor(getColor(R.color.yellow_notification_icon)) + + // Update the notification. + notificationManager.notify(NOTIFICATION_ID, insecureNetworkNotificationBuilder.build()) + + // Store the new network status. + currentStatus = INSECURE_NETWORK + } + } else { // This is an antiquated network. + // Only update the notification if the network status has changed. + if (currentStatus != ANTIQUATED_NETWORK) { + // Create an antiquated network notification builder. + val antiquatedNetworkNotificationBuilder = Notification.Builder(applicationContext, ANTIQUATED_NETWORK) - // Create a notification channel group. - notificationManager.createNotificationChannelGroup(NotificationChannelGroup(REALTIME_MONITORING, getString(R.string.realtime_monitoring))) + // Set the notification to open Privacy Cell. + antiquatedNetworkNotificationBuilder.setContentIntent(privacyCellPendingIntent) + + // Set the notification text. + antiquatedNetworkNotificationBuilder.setContentText(getString(R.string.antiquated_network)) - // Prepare the notification channels. - val secureNetworkChannel = NotificationChannel(SECURE_NETWORK, getString(R.string.secure_network_channel), NotificationManager.IMPORTANCE_HIGH) - val insecureNetworkChannel = NotificationChannel(INSECURE_NETWORK, getString(R.string.insecure_network_channel), NotificationManager.IMPORTANCE_HIGH) - val antiquatedNetworkChannel = NotificationChannel(ANTIQUATED_NETWORK, getString(R.string.antiquated_network_channel), NotificationManager.IMPORTANCE_HIGH) - val unknownNetworkChannel = NotificationChannel(UNKNOWN_NETWORK, getString(R.string.unknown_network_channel), NotificationManager.IMPORTANCE_LOW) + // Set the notification icon. + antiquatedNetworkNotificationBuilder.setSmallIcon(R.drawable.antiquated_notification_enabled) - // Set the notification channel group. - secureNetworkChannel.group = REALTIME_MONITORING - insecureNetworkChannel.group = REALTIME_MONITORING - antiquatedNetworkChannel.group = REALTIME_MONITORING - unknownNetworkChannel.group = REALTIME_MONITORING + // Set the color. + antiquatedNetworkNotificationBuilder.setColor(getColor(R.color.red_notification_icon)) - // Disable the notification dots. - secureNetworkChannel.setShowBadge(false) - insecureNetworkChannel.setShowBadge(false) - antiquatedNetworkChannel.setShowBadge(false) - unknownNetworkChannel.setShowBadge(false) - - // Set the primary channel notifications to be public. - secureNetworkChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC - insecureNetworkChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC - antiquatedNetworkChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC - - // Create the notification channels. - notificationManager.createNotificationChannel(secureNetworkChannel) - notificationManager.createNotificationChannel(insecureNetworkChannel) - notificationManager.createNotificationChannel(antiquatedNetworkChannel) - notificationManager.createNotificationChannel(unknownNetworkChannel) - - // Create a notification builder. - val notificationBuilder = Notification.Builder(this, UNKNOWN_NETWORK) - - // Create an intent to open Privacy Cell. - val privacyCellIntent = Intent(this, PrivacyCellActivity::class.java) - - // Create a pending intent from the Privacy Cell intent. - val privacyCellPendingIntent = PendingIntent.getActivity(this, 0, privacyCellIntent, PendingIntent.FLAG_IMMUTABLE) - - // Set the notification to open Privacy Cell. - notificationBuilder.setContentIntent(privacyCellPendingIntent) - - // Set the notification text. - notificationBuilder.setContentText(getString(R.string.unknown_network)) - - // Set the notification icon. - notificationBuilder.setSmallIcon(R.drawable.antiquated_notification_enabled) - - // Set the color. - notificationBuilder.setColor(getColor(R.color.red_notification_icon)) - - // Start the foreground notification. - startForeground(NOTIFICATION_ID, notificationBuilder.build()) - - // 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) { - // Get the consider 3G antiquated preference. - val consider3gAntiquated = sharedPreferences.getBoolean(getString(R.string.consider_3g_antiquated_key), false) - - // Populate the notification according to the network type. - if ((telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_NR) || (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_IWLAN) || - (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_UNKNOWN)) { // This is a secure network. - // Only update the notification if the network status has changed. - if (currentStatus != SECURE_NETWORK) { - // Create a secure network notification builder. - val secureNetworkNotificationBuilder = Notification.Builder(applicationContext, SECURE_NETWORK) - - // Set the notification to open Privacy Cell. - secureNetworkNotificationBuilder.setContentIntent(privacyCellPendingIntent) - - // Set the notification text. - secureNetworkNotificationBuilder.setContentText(getString(R.string.secure_network)) - - // Set the notification icon. - secureNetworkNotificationBuilder.setSmallIcon(R.drawable.secure_notification_enabled) - - // Set the color. - secureNetworkNotificationBuilder.setColor(getColor(R.color.blue_icon)) - - // Update the notification. - notificationManager.notify(NOTIFICATION_ID, secureNetworkNotificationBuilder.build()) - - // Store the new network status. - currentStatus = SECURE_NETWORK - } - } else if ((telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_LTE) || (!consider3gAntiquated && (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_1xRTT || - (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_EVDO_0) || (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_EVDO_A) || - (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_EVDO_B) || (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_EHRPD) || - (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_UMTS) || (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_TD_SCDMA) || - (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_HSDPA) || (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_HSUPA) || - (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_HSPA) || (telephonyDisplayInfo.networkType == TelephonyManager.NETWORK_TYPE_HSPAP)))) { - // This is an insecure network. - // Only update the notification if the network status has changed. - if (currentStatus != INSECURE_NETWORK) { - // Create an insecure network notification builder. - val insecureNetworkNotificationBuilder = Notification.Builder(applicationContext, INSECURE_NETWORK) - - // Set the notification to open Privacy Cell. - insecureNetworkNotificationBuilder.setContentIntent(privacyCellPendingIntent) - - // Set the notification text. - insecureNetworkNotificationBuilder.setContentText(getString(R.string.insecure_network)) - - // Set the notification icon. - insecureNetworkNotificationBuilder.setSmallIcon(R.drawable.insecure_notification_enabled) - - // Set the color. - insecureNetworkNotificationBuilder.setColor(getColor(R.color.yellow_notification_icon)) - - // Update the notification. - notificationManager.notify(NOTIFICATION_ID, insecureNetworkNotificationBuilder.build()) - - // Store the new network status. - currentStatus = INSECURE_NETWORK - } - } else { // This is an antiquated network. - // Only update the notification if the network status has changed. - if (currentStatus != ANTIQUATED_NETWORK) { - // Create an antiquated network notification builder. - val antiquatedNetworkNotificationBuilder = Notification.Builder(applicationContext, ANTIQUATED_NETWORK) - - // Set the notification to open Privacy Cell. - antiquatedNetworkNotificationBuilder.setContentIntent(privacyCellPendingIntent) - - // Set the notification text. - antiquatedNetworkNotificationBuilder.setContentText(getString(R.string.antiquated_network)) - - // Set the notification icon. - antiquatedNetworkNotificationBuilder.setSmallIcon(R.drawable.antiquated_notification_enabled) - - // Set the color. - antiquatedNetworkNotificationBuilder.setColor(getColor(R.color.red_notification_icon)) - - // Update the notification. - notificationManager.notify(NOTIFICATION_ID, antiquatedNetworkNotificationBuilder.build()) - - // Store the new network status. - currentStatus = ANTIQUATED_NETWORK + // Update the notification. + notificationManager.notify(NOTIFICATION_ID, antiquatedNetworkNotificationBuilder.build()) + + // Store the new network status. + currentStatus = ANTIQUATED_NETWORK + } } } } - } - // Check to see if the read phone state permission has been granted. - 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(1, TimeUnit.HOURS).build() + // Check to see if the read phone state permission has been granted. + 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(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) + } + } else { // Realtime monitoring is disabled. This can happen if the restart listener work request fires after realtime monitoring has been disabled. + // Cancel the realtime listener work request. + WorkManager.getInstance(applicationContext).cancelUniqueWork(applicationContext.getString(R.string.register_listener_work_request)) } // Return a sticky service. diff --git a/app/src/main/java/com/stoutner/privacycell/workers/RegisterRealtimeListenerWorker.kt b/app/src/main/java/com/stoutner/privacycell/workers/RegisterRealtimeListenerWorker.kt index 7fe86d0..6bb76ad 100644 --- a/app/src/main/java/com/stoutner/privacycell/workers/RegisterRealtimeListenerWorker.kt +++ b/app/src/main/java/com/stoutner/privacycell/workers/RegisterRealtimeListenerWorker.kt @@ -1,5 +1,5 @@ /* - * Copyright © 2021 Soren Stoutner . + * Copyright © 2021-2022 Soren Stoutner . * * This file is part of Privacy Cell . * @@ -46,11 +46,12 @@ class RegisterRealtimeListenerWorker(appContext: Context, workerParameters: Work val realtimeMonitoring = sharedPreferences.getBoolean(applicationContext.getString(R.string.realtime_monitoring_key), true) // Perform the functions according to the realtime monitoring status. + @Suppress("DEPRECATION") // The deprecated `getRunningServices()` now only returns services started by Privacy Cell, but that is all we want to know anyway. if (realtimeMonitoring) { // Realtime monitoring is enabled. // Get a handle for the activity manager. val activityManager: ActivityManager = applicationContext.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager - // Get a list of the running service info. The deprecated `getRunningServices()` now only returns services stared by Privacy Cell, but that is all we want to know anyway. + // Get a list of the running service info. The deprecated `getRunningServices()` now only returns services started by Privacy Cell, but that is all we want to know anyway. val runningServiceInfoList: List = activityManager.getRunningServices(1) // Check to see if the service is currently running. @@ -89,6 +90,9 @@ class RegisterRealtimeListenerWorker(appContext: Context, workerParameters: Work applicationContext.bindService(Intent(applicationContext, RealtimeMonitoringService::class.java), serviceConnection, 0) } } else { // Realtime monitoring is disabled. + // Stop the realtime monitoring service. + applicationContext.stopService(Intent(applicationContext, RealtimeMonitoringService::class.java)) + // Cancel the realtime listener work request. WorkManager.getInstance(applicationContext).cancelUniqueWork(applicationContext.getString(R.string.register_listener_work_request)) } diff --git a/app/src/main/java/com/stoutner/privacycell/workers/RestartServiceWorker.kt b/app/src/main/java/com/stoutner/privacycell/workers/RestartServiceWorker.kt index 64b4eca..720ca7a 100644 --- a/app/src/main/java/com/stoutner/privacycell/workers/RestartServiceWorker.kt +++ b/app/src/main/java/com/stoutner/privacycell/workers/RestartServiceWorker.kt @@ -1,5 +1,5 @@ /* - * Copyright © 2021 Soren Stoutner . + * Copyright © 2021-2022 Soren Stoutner . * * This file is part of Privacy Cell . * @@ -22,14 +22,31 @@ package com.stoutner.privacycell.workers import android.content.Context import android.content.Intent +import androidx.preference.PreferenceManager +import androidx.work.WorkManager import androidx.work.Worker import androidx.work.WorkerParameters + +import com.stoutner.privacycell.R import com.stoutner.privacycell.services.RealtimeMonitoringService class RestartServiceWorker(appContext: Context, workerParameters: WorkerParameters) : Worker(appContext, workerParameters) { override fun doWork(): Result { - // Start the realtime monitoring service as a foreground service, which is required because the worker is running in the background. - applicationContext.startForegroundService(Intent(applicationContext, RealtimeMonitoringService::class.java)) + // Get a handle for the shared preferences. + val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(applicationContext) + + // Check to see if realtime monitoring is enabled. Sometimes the shared preferences can't return a value in time, because Android sucks. + // So, the default value is set to true, which is the safest value if the shared preferences can't be queried. + if (sharedPreferences.getBoolean(applicationContext.getString(R.string.realtime_monitoring_key), true)) { // Realtime monitoring is enabled. + // Start the realtime monitoring service as a foreground service, which is required because the worker is running in the background. + applicationContext.startForegroundService(Intent(applicationContext, RealtimeMonitoringService::class.java)) + } else { // Realtime monitoring is disabled. + // Stop the realtime monitoring service. + applicationContext.stopService(Intent(applicationContext, RealtimeMonitoringService::class.java)) + + // Cancel the realtime listener work request. + WorkManager.getInstance(applicationContext).cancelUniqueWork(applicationContext.getString(R.string.register_listener_work_request)) + } // Return a success. return Result.success() diff --git a/app/src/main/res/layout/webview_dialog.xml b/app/src/main/res/layout/webview_dialog.xml index d585bb0..aeb1f4f 100644 --- a/app/src/main/res/layout/webview_dialog.xml +++ b/app/src/main/res/layout/webview_dialog.xml @@ -1,7 +1,7 @@