Inject geofencing in flavor module

pull/1369/head
Alex Baker 5 years ago
parent 5c5833ee8f
commit f0f6478dab

@ -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
}

@ -13,6 +13,12 @@
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>
<receiver android:name=".location.GoogleGeofenceTransitionIntentService$Broadcast"/>
<service
android:exported="false"
android:name=".location.GoogleGeofenceTransitionIntentService"
android:permission="android.permission.BIND_JOB_SERVICE"/>
</application>
</manifest>

@ -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
}

@ -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)
}

@ -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()))
}

@ -493,10 +493,10 @@
android:name=".locale.receiver.TaskerIntentService"
android:permission="android.permission.BIND_JOB_SERVICE"/>
<receiver android:name=".location.GeofenceTransitionsIntentService$Broadcast"/>
<receiver android:name=".location.AndroidGeofenceTransitionIntentService$Broadcast"/>
<service
android:exported="false"
android:name=".location.GeofenceTransitionsIntentService"
android:name=".location.AndroidGeofenceTransitionIntentService"
android:permission="android.permission.BIND_JOB_SERVICE"/>
<!-- Uses Library -->

@ -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)
}

@ -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
)

@ -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) }

@ -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)
}
Loading…
Cancel
Save