X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyCell.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacycell%2Fworkers%2FRegisterRealtimeListener.kt;h=86774708ed0f58208a1d9908417b92c8cd682c2f;hp=eaa8a5512d45f37d9ce80d233c3fa606acfeb146;hb=0797811d24cfaab7a49ec071c8a752e950213981;hpb=cb92ea552a5ffa8ca3142053660e3a73afc9240a diff --git a/app/src/main/java/com/stoutner/privacycell/workers/RegisterRealtimeListener.kt b/app/src/main/java/com/stoutner/privacycell/workers/RegisterRealtimeListener.kt index eaa8a55..8677470 100644 --- a/app/src/main/java/com/stoutner/privacycell/workers/RegisterRealtimeListener.kt +++ b/app/src/main/java/com/stoutner/privacycell/workers/RegisterRealtimeListener.kt @@ -35,29 +35,26 @@ import com.stoutner.privacycell.R import com.stoutner.privacycell.services.RealtimeMonitoringService class RegisterRealtimeListener (appContext: Context, workerParams: WorkerParameters) : Worker(appContext, workerParams) { - // Store the application context. - private val context = appContext - override fun doWork(): Result { // Get a handle for the shared preferences. - val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context) + val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(applicationContext) // Get the realtime monitoring preference. 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. - val realtimeMonitoring = sharedPreferences.getBoolean(context.getString(R.string.realtime_monitoring_key), true) + val realtimeMonitoring = sharedPreferences.getBoolean(applicationContext.getString(R.string.realtime_monitoring_key), true) // Perform the functions according to the realtime monitoring status. if (realtimeMonitoring) { // Realtime monitoring is enabled. // Get a handle for the activity manager. - val activityManager: ActivityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager + 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. val runningServiceInfoList: List = activityManager.getRunningServices(1) // Check to see if the service is currently running. if (runningServiceInfoList.isEmpty()) { // The service is currently stopped. - // Start the service. - context.startService(Intent(context, RealtimeMonitoringService::class.java)) + // Start the service as a foreground service, which is required because the worker is running in the background. + applicationContext.startForegroundService(Intent(applicationContext, RealtimeMonitoringService::class.java)) } else { // The service is currently running. // Create a service connection. val serviceConnection = object : ServiceConnection { @@ -72,7 +69,7 @@ class RegisterRealtimeListener (appContext: Context, workerParams: WorkerParamet realtimeMonitoringService.registerTelephonyManagerListener() // Unbind the service. - context.unbindService(this) + applicationContext.unbindService(this) } override fun onServiceDisconnected(componentName: ComponentName) { @@ -81,11 +78,11 @@ class RegisterRealtimeListener (appContext: Context, workerParams: WorkerParamet } // Bind to the realtime monitoring service. - context.bindService(Intent(context, RealtimeMonitoringService::class.java), serviceConnection, 0) + applicationContext.bindService(Intent(applicationContext, RealtimeMonitoringService::class.java), serviceConnection, 0) } } else { // Realtime monitoring is disabled. // Cancel the realtime listener work request. - WorkManager.getInstance(context).cancelUniqueWork(context.getString(R.string.register_listener_work_request)) + WorkManager.getInstance(applicationContext).cancelUniqueWork(applicationContext.getString(R.string.register_listener_work_request)) } // Return a success.