Skip to content
This repository has been archived by the owner on Jan 10, 2024. It is now read-only.

Commit

Permalink
JobIntentService replacement with WorkManager (#1588)
Browse files Browse the repository at this point in the history
* replace geofencing JobIntentService with workmanager
* Finish changing all jobIntentServices into Workers
* removal of old files
* remove services from manifest
* remove startservice calls, use workmanager methods
---------
Co-authored-by: Frank Elsinga <frank@elsinga.de>
  • Loading branch information
zaghdaneh authored Jul 5, 2023
1 parent 0c8e525 commit 33bf492
Show file tree
Hide file tree
Showing 13 changed files with 180 additions and 182 deletions.
11 changes: 0 additions & 11 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -506,17 +506,9 @@
</intent-filter>
</receiver>

<service
android:name=".service.SilenceService"
android:exported="false"
android:permission="android.permission.BIND_JOB_SERVICE" />
<service
android:name=".service.ScanResultsAvailableReceiver$NeverShowAgainService"
android:exported="false" />
<service
android:name=".service.QueryLocationsService"
android:exported="false"
android:permission="android.permission.BIND_JOB_SERVICE" />
<service
android:name=".service.FcmReceiverService"
android:enabled="true"
Expand All @@ -526,9 +518,6 @@
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service
android:name=".service.GeofencingRegistrationService"
android:permission="android.permission.BIND_JOB_SERVICE" />

<receiver android:name=".service.GeofencingUpdateReceiver" />
<receiver android:name=".service.GeofencingStartupReceiver"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import de.tum.`in`.tumcampusapp.component.ui.news.NewsController
import de.tum.`in`.tumcampusapp.component.ui.onboarding.StartupActivity
import de.tum.`in`.tumcampusapp.database.TcaDb
import de.tum.`in`.tumcampusapp.di.injector
import de.tum.`in`.tumcampusapp.service.SilenceService
import de.tum.`in`.tumcampusapp.service.SilenceWorker
import de.tum.`in`.tumcampusapp.service.StartSyncReceiver
import de.tum.`in`.tumcampusapp.utils.Const
import de.tum.`in`.tumcampusapp.utils.Utils
Expand Down Expand Up @@ -183,19 +183,19 @@ class SettingsFragment : PreferenceFragmentCompat(), Preference.OnPreferenceClic
// If the silent mode was activated, start the service. This will invoke
// the service to call onHandleIntent which checks available lectures
if (key == Const.SILENCE_SERVICE) {
val service = Intent(requireContext(), SilenceService::class.java)
val service = Intent(requireContext(), SilenceWorker::class.java)
if (sharedPrefs.getBoolean(key, false)) {
if (!SilenceService.hasPermissions(requireContext())) {
if (!SilenceWorker.hasPermissions(requireContext())) {
// disable until silence service permission is resolved
val silenceSwitch = findPreference(Const.SILENCE_SERVICE) as SwitchPreferenceCompat
silenceSwitch.isChecked = false
Utils.setSetting(requireContext(), Const.SILENCE_SERVICE, false)
SilenceService.requestPermissions(requireContext())
SilenceWorker.requestPermissions(requireContext())
} else {
requireContext().startService(service)
SilenceWorker.enqueueWork(requireContext())
}
} else {
requireContext().stopService(service)
SilenceWorker.dequeueWork(requireContext())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import de.tum.`in`.tumcampusapp.component.tumui.calendar.model.EventsResponse
import de.tum.`in`.tumcampusapp.component.ui.transportation.TransportController
import de.tum.`in`.tumcampusapp.database.TcaDb
import de.tum.`in`.tumcampusapp.databinding.FragmentCalendarBinding
import de.tum.`in`.tumcampusapp.service.QueryLocationsService
import de.tum.`in`.tumcampusapp.service.QueryLocationWorker
import de.tum.`in`.tumcampusapp.utils.*
import io.reactivex.Completable
import io.reactivex.android.schedulers.AndroidSchedulers
Expand Down Expand Up @@ -193,7 +193,7 @@ class CalendarFragment :
private fun onCalendarImportedIntoDatabase() {
// Update the action bar to display the enabled menu options
requireActivity().invalidateOptionsMenu()
QueryLocationsService.enqueueWork(requireContext())
QueryLocationWorker.enqueueWork(requireContext())
}

private fun scheduleNotifications(events: List<Event>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import de.tum.`in`.tumcampusapp.component.ui.chat.model.ChatMember
import de.tum.`in`.tumcampusapp.component.ui.onboarding.di.OnboardingComponent
import de.tum.`in`.tumcampusapp.component.ui.onboarding.di.OnboardingComponentProvider
import de.tum.`in`.tumcampusapp.databinding.FragmentOnboardingExtrasBinding
import de.tum.`in`.tumcampusapp.service.SilenceService
import de.tum.`in`.tumcampusapp.service.SilenceWorker
import de.tum.`in`.tumcampusapp.utils.CacheManager
import de.tum.`in`.tumcampusapp.utils.Const
import de.tum.`in`.tumcampusapp.utils.NetUtils
Expand Down Expand Up @@ -69,8 +69,8 @@ class OnboardingExtrasFragment : FragmentForLoadingInBackground<ChatMember>(
silentModeCheckBox.isChecked =
Utils.getSettingBool(requireContext(), Const.SILENCE_SERVICE, false)
silentModeCheckBox.setOnCheckedChangeListener { _, isChecked ->
if (isChecked && !SilenceService.hasPermissions(requireContext())) {
SilenceService.requestPermissions(requireContext())
if (isChecked && !SilenceWorker.hasPermissions(requireContext())) {
SilenceWorker.requestPermissions(requireContext())
silentModeCheckBox.isChecked = false
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package de.tum.`in`.tumcampusapp.component.ui.overview

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.net.ConnectivityManager
import android.net.Network
Expand All @@ -26,7 +25,7 @@ import de.tum.`in`.tumcampusapp.databinding.FragmentMainBinding
import de.tum.`in`.tumcampusapp.di.ViewModelFactory
import de.tum.`in`.tumcampusapp.di.injector
import de.tum.`in`.tumcampusapp.service.DownloadWorker
import de.tum.`in`.tumcampusapp.service.SilenceService
import de.tum.`in`.tumcampusapp.service.SilenceWorker
import de.tum.`in`.tumcampusapp.utils.Const
import de.tum.`in`.tumcampusapp.utils.NetUtils
import de.tum.`in`.tumcampusapp.utils.Utils
Expand Down Expand Up @@ -96,9 +95,8 @@ class MainFragment : BaseFragment<Unit>(
ItemTouchHelper(ItemTouchHelperCallback()).attachToRecyclerView(cardsRecyclerView)
}

// Start silence Service (if already started it will just invoke a check)
val service = Intent(requireContext(), SilenceService::class.java)
requireContext().startService(service)
// Enqueue a request for the silence worker (if already started it will just invoke a check)
SilenceWorker.enqueueWork(requireContext())

viewModel.cards.observe(viewLifecycleOwner) {
it?.let { onNewCardsAvailable(it) }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package de.tum.`in`.tumcampusapp.service

import android.Manifest
import android.annotation.SuppressLint
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import androidx.core.content.ContextCompat
import androidx.work.Data
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.WorkRequest
import androidx.work.Worker
import androidx.work.WorkerParameters
import com.google.android.gms.location.Geofence
import com.google.android.gms.location.GeofencingClient
import com.google.android.gms.location.GeofencingRequest
import com.google.android.gms.location.LocationServices
import de.tum.`in`.tumcampusapp.utils.Const
import de.tum.`in`.tumcampusapp.utils.Utils

class GeoFencingRegistrationWorker(appContext: Context, workerParams: WorkerParameters) :
Worker(appContext, workerParams) {

private lateinit var locationClient: GeofencingClient

private fun isLocationPermissionGranted(): Boolean {
return ContextCompat.checkSelfPermission(
applicationContext, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
}

@SuppressLint("MissingPermission")
override fun doWork(): Result {
locationClient = LocationServices.getGeofencingClient(applicationContext)

Utils.log("Geo fencing service worker started …")
if (!isLocationPermissionGranted()) {
return Result.failure()
}
// get the data to create the Geofence request
val id = inputData.getString(Const.ADD_GEOFENCE_ID) ?: return Result.failure()
val latitude = inputData.getDouble(Const.ADD_GEOFENCE_LAT, 48.137430)
val longitude = inputData.getDouble(Const.ADD_GEOFENCE_LON, 11.575490)
val range = inputData.getFloat(Const.ADD_GEOFENCE_RANGE, 50_000f)

// build request
val geofence = Geofence.Builder()
.setRequestId(id)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER or Geofence.GEOFENCE_TRANSITION_EXIT)
.setCircularRegion(latitude, longitude, range)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.build()

val geofencingRequest = GeofencingRequest.Builder()
.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER or GeofencingRequest.INITIAL_TRIGGER_EXIT)
.addGeofences(arrayListOf(geofence))
.build()

val geofenceIntent = Intent(applicationContext, GeofencingUpdateReceiver::class.java)
val geofencePendingIntent = PendingIntent.getBroadcast(
applicationContext, 0, geofenceIntent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)

locationClient.addGeofences(geofencingRequest, geofencePendingIntent)
Utils.log("Registered new Geofence")
// Indicate whether the work finished successfully with the Result*/
return Result.success()
}

companion object {

/**
* Helper method for creating an intent containing a geofencing request
*/
fun buildGeofence(id: String, latitude: Double, longitude: Double, range: Float): WorkRequest {
val data = Data.Builder()
.putString(Const.ADD_GEOFENCE_ID, id)
.putDouble(Const.ADD_GEOFENCE_LAT, latitude)
.putDouble(Const.ADD_GEOFENCE_LON, longitude)
.putFloat(Const.ADD_GEOFENCE_RANGE, range)
.build()

return OneTimeWorkRequestBuilder<GeoFencingRegistrationWorker>()
.setInputData(data)
.build()
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package de.tum.`in`.tumcampusapp.service
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import androidx.work.WorkManager
import de.tum.`in`.tumcampusapp.utils.Const.DISTANCE_IN_METER
import de.tum.`in`.tumcampusapp.utils.Const.MUNICH_GEOFENCE
import de.tum.`in`.tumcampusapp.utils.Utils
Expand All @@ -16,9 +17,10 @@ class GeofencingStartupReceiver : BroadcastReceiver() {

Utils.log("Restarting geofencing due to " + intent?.action)
context?.let {
val geofencingIntent = GeofencingRegistrationService.buildGeofence(it, MUNICH_GEOFENCE,
val geofencingWorker = GeoFencingRegistrationWorker.buildGeofence(MUNICH_GEOFENCE,
48.137430, 11.575490, DISTANCE_IN_METER)
GeofencingRegistrationService.startGeofencing(it, geofencingIntent)
val workManager = WorkManager.getInstance(context)
workManager.enqueue(geofencingWorker)
}
}

Expand All @@ -28,4 +30,4 @@ class GeofencingStartupReceiver : BroadcastReceiver() {
intent.action == "android.intent.action.QUICKBOOT_POWERON" ||
intent.action == "android.location.PROVIDERS_CHANGED")
}
}
}
Loading

0 comments on commit 33bf492

Please sign in to comment.