From f0f6478dab3b38bddfc60428ddbb0347a9fccd22 Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Fri, 12 Feb 2021 09:58:53 -0600 Subject: [PATCH] Inject geofencing in flavor module --- .../generic/java/org/tasks/injection/FlavorModule.kt | 5 +++++ app/src/googleplay/AndroidManifest.xml | 6 ++++++ .../java/org/tasks/injection/FlavorModule.kt | 5 +++++ ...ce.kt => GoogleGeofenceTransitionIntentService.kt} | 4 ++-- .../{GeofenceClient.kt => GoogleGeofencing.kt} | 10 ++++++---- app/src/main/AndroidManifest.xml | 4 ++-- .../AndroidGeofenceTransitionIntentService.kt} | 4 ++-- .../java/org/tasks/location/AndroidGeofencing.kt} | 11 +++++++---- app/src/main/java/org/tasks/location/GeofenceApi.kt | 2 +- app/src/main/java/org/tasks/location/Geofencing.kt | 10 ++++++++++ 10 files changed, 46 insertions(+), 15 deletions(-) rename app/src/googleplay/java/org/tasks/location/{GeofenceTransitionsIntentService.kt => GoogleGeofenceTransitionIntentService.kt} (94%) rename app/src/googleplay/java/org/tasks/location/{GeofenceClient.kt => GoogleGeofencing.kt} (83%) rename app/src/{generic/java/org/tasks/location/GeofenceTransitionsIntentService.kt => main/java/org/tasks/location/AndroidGeofenceTransitionIntentService.kt} (90%) rename app/src/{generic/java/org/tasks/location/GeofenceClient.kt => main/java/org/tasks/location/AndroidGeofencing.kt} (76%) create mode 100644 app/src/main/java/org/tasks/location/Geofencing.kt diff --git a/app/src/generic/java/org/tasks/injection/FlavorModule.kt b/app/src/generic/java/org/tasks/injection/FlavorModule.kt index a574560ca..a79651246 100644 --- a/app/src/generic/java/org/tasks/injection/FlavorModule.kt +++ b/app/src/generic/java/org/tasks/injection/FlavorModule.kt @@ -4,7 +4,9 @@ import dagger.Module import dagger.Provides import dagger.hilt.InstallIn import dagger.hilt.components.SingletonComponent +import org.tasks.location.AndroidGeofencing import org.tasks.location.Geocoder +import org.tasks.location.Geofencing import org.tasks.location.MapboxGeocoder @Module @@ -12,4 +14,7 @@ import org.tasks.location.MapboxGeocoder class FlavorModule { @Provides fun getGeocoder(geocoder: MapboxGeocoder): Geocoder = geocoder + + @Provides + fun getGeofencing(geofencing: AndroidGeofencing): Geofencing = geofencing } \ No newline at end of file diff --git a/app/src/googleplay/AndroidManifest.xml b/app/src/googleplay/AndroidManifest.xml index f7a385266..9f2e9595f 100644 --- a/app/src/googleplay/AndroidManifest.xml +++ b/app/src/googleplay/AndroidManifest.xml @@ -13,6 +13,12 @@ android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/> + + + diff --git a/app/src/googleplay/java/org/tasks/injection/FlavorModule.kt b/app/src/googleplay/java/org/tasks/injection/FlavorModule.kt index a574560ca..4b81e5542 100644 --- a/app/src/googleplay/java/org/tasks/injection/FlavorModule.kt +++ b/app/src/googleplay/java/org/tasks/injection/FlavorModule.kt @@ -5,6 +5,8 @@ import dagger.Provides import dagger.hilt.InstallIn import dagger.hilt.components.SingletonComponent import org.tasks.location.Geocoder +import org.tasks.location.Geofencing +import org.tasks.location.GoogleGeofencing import org.tasks.location.MapboxGeocoder @Module @@ -12,4 +14,7 @@ import org.tasks.location.MapboxGeocoder class FlavorModule { @Provides fun getGeocoder(geocoder: MapboxGeocoder): Geocoder = geocoder + + @Provides + fun getGeofencing(geofencing: GoogleGeofencing): Geofencing = geofencing } \ No newline at end of file diff --git a/app/src/googleplay/java/org/tasks/location/GeofenceTransitionsIntentService.kt b/app/src/googleplay/java/org/tasks/location/GoogleGeofenceTransitionIntentService.kt similarity index 94% rename from app/src/googleplay/java/org/tasks/location/GeofenceTransitionsIntentService.kt rename to app/src/googleplay/java/org/tasks/location/GoogleGeofenceTransitionIntentService.kt index e3a2c924a..f2e2c6669 100644 --- a/app/src/googleplay/java/org/tasks/location/GeofenceTransitionsIntentService.kt +++ b/app/src/googleplay/java/org/tasks/location/GoogleGeofenceTransitionIntentService.kt @@ -14,7 +14,7 @@ import timber.log.Timber import javax.inject.Inject @AndroidEntryPoint -class GeofenceTransitionsIntentService : InjectingJobIntentService() { +class GoogleGeofenceTransitionIntentService : InjectingJobIntentService() { @Inject lateinit var locationDao: LocationDao @Inject lateinit var notifier: Notifier @@ -59,7 +59,7 @@ class GeofenceTransitionsIntentService : InjectingJobIntentService() { override fun onReceive(context: Context, intent: Intent) { enqueueWork( context, - GeofenceTransitionsIntentService::class.java, + GoogleGeofenceTransitionIntentService::class.java, JOB_ID_GEOFENCE_TRANSITION, intent) } diff --git a/app/src/googleplay/java/org/tasks/location/GeofenceClient.kt b/app/src/googleplay/java/org/tasks/location/GoogleGeofencing.kt similarity index 83% rename from app/src/googleplay/java/org/tasks/location/GeofenceClient.kt rename to app/src/googleplay/java/org/tasks/location/GoogleGeofencing.kt index cafb7610e..a675510fa 100644 --- a/app/src/googleplay/java/org/tasks/location/GeofenceClient.kt +++ b/app/src/googleplay/java/org/tasks/location/GoogleGeofencing.kt @@ -12,21 +12,23 @@ import org.tasks.data.MergedGeofence import org.tasks.data.Place import javax.inject.Inject -class GeofenceClient @Inject constructor(@ApplicationContext private val context: Context) { +class GoogleGeofencing @Inject constructor( + @ApplicationContext private val context: Context +): Geofencing { private val client = LocationServices.getGeofencingClient(context) @SuppressLint("MissingPermission") - fun addGeofences(geofence: MergedGeofence) { + override fun addGeofences(geofence: MergedGeofence) { client.addGeofences( GeofencingRequest.Builder().addGeofence(toGoogleGeofence(geofence)).build(), PendingIntent.getBroadcast( context, 0, - Intent(context, GeofenceTransitionsIntentService.Broadcast::class.java), + Intent(context, GoogleGeofenceTransitionIntentService.Broadcast::class.java), PendingIntent.FLAG_UPDATE_CURRENT)) } - fun removeGeofences(place: Place) { + override fun removeGeofences(place: Place) { client.removeGeofences(listOf(place.id.toString())) } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 1f3dae08b..9c03f6e28 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -493,10 +493,10 @@ android:name=".locale.receiver.TaskerIntentService" android:permission="android.permission.BIND_JOB_SERVICE"/> - + diff --git a/app/src/generic/java/org/tasks/location/GeofenceTransitionsIntentService.kt b/app/src/main/java/org/tasks/location/AndroidGeofenceTransitionIntentService.kt similarity index 90% rename from app/src/generic/java/org/tasks/location/GeofenceTransitionsIntentService.kt rename to app/src/main/java/org/tasks/location/AndroidGeofenceTransitionIntentService.kt index 585ddcbc8..a6c3e536e 100644 --- a/app/src/generic/java/org/tasks/location/GeofenceTransitionsIntentService.kt +++ b/app/src/main/java/org/tasks/location/AndroidGeofenceTransitionIntentService.kt @@ -12,7 +12,7 @@ import timber.log.Timber import javax.inject.Inject @AndroidEntryPoint -class GeofenceTransitionsIntentService : InjectingJobIntentService() { +class AndroidGeofenceTransitionIntentService : InjectingJobIntentService() { @Inject lateinit var locationDao: LocationDao @Inject lateinit var notifier: Notifier @@ -36,7 +36,7 @@ class GeofenceTransitionsIntentService : InjectingJobIntentService() { override fun onReceive(context: Context, intent: Intent) { enqueueWork( context, - GeofenceTransitionsIntentService::class.java, + AndroidGeofenceTransitionIntentService::class.java, JOB_ID_GEOFENCE_TRANSITION, intent) } diff --git a/app/src/generic/java/org/tasks/location/GeofenceClient.kt b/app/src/main/java/org/tasks/location/AndroidGeofencing.kt similarity index 76% rename from app/src/generic/java/org/tasks/location/GeofenceClient.kt rename to app/src/main/java/org/tasks/location/AndroidGeofencing.kt index 6245be679..0e48f6442 100644 --- a/app/src/generic/java/org/tasks/location/GeofenceClient.kt +++ b/app/src/main/java/org/tasks/location/AndroidGeofencing.kt @@ -11,11 +11,14 @@ import org.tasks.data.MergedGeofence import org.tasks.data.Place import javax.inject.Inject -class GeofenceClient @Inject constructor(@ApplicationContext private val context: Context) { +@Suppress("unused") +class AndroidGeofencing @Inject constructor( + @ApplicationContext private val context: Context +): Geofencing { private val client = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager @SuppressLint("MissingPermission") - fun addGeofences(@Suppress("UNUSED_PARAMETER") geofence: MergedGeofence) { + override fun addGeofences(geofence: MergedGeofence) { client.addProximityAlert( geofence.latitude, geofence.longitude, @@ -25,7 +28,7 @@ class GeofenceClient @Inject constructor(@ApplicationContext private val context ) } - fun removeGeofences(@Suppress("UNUSED_PARAMETER") place: Place) { + override fun removeGeofences(place: Place) { client.removeProximityAlert(createPendingIntent(place.id)) } @@ -33,7 +36,7 @@ class GeofenceClient @Inject constructor(@ApplicationContext private val context PendingIntent.getBroadcast( context, 0, - Intent(context, GeofenceTransitionsIntentService.Broadcast::class.java) + Intent(context, AndroidGeofenceTransitionIntentService.Broadcast::class.java) .setData(Uri.parse("tasks://geofence/$place")), PendingIntent.FLAG_UPDATE_CURRENT ) diff --git a/app/src/main/java/org/tasks/location/GeofenceApi.kt b/app/src/main/java/org/tasks/location/GeofenceApi.kt index 77a72a378..bfb4f624b 100644 --- a/app/src/main/java/org/tasks/location/GeofenceApi.kt +++ b/app/src/main/java/org/tasks/location/GeofenceApi.kt @@ -9,7 +9,7 @@ import javax.inject.Inject class GeofenceApi @Inject constructor( private val permissionChecker: PermissionChecker, private val locationDao: LocationDao, - private val client: GeofenceClient + private val client: Geofencing ) { suspend fun registerAll() = locationDao.getPlacesWithGeofences().forEach { update(it) } diff --git a/app/src/main/java/org/tasks/location/Geofencing.kt b/app/src/main/java/org/tasks/location/Geofencing.kt new file mode 100644 index 000000000..1a7313086 --- /dev/null +++ b/app/src/main/java/org/tasks/location/Geofencing.kt @@ -0,0 +1,10 @@ +package org.tasks.location + +import org.tasks.data.MergedGeofence +import org.tasks.data.Place + +interface Geofencing { + fun addGeofences(geofence: MergedGeofence) + + fun removeGeofences(place: Place) +} \ No newline at end of file