Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a GPS Service. #373

Merged
merged 4 commits into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ dependencies {
// Map Libre
implementation(libs.android.sdk)
implementation(libs.ramani.maplibre)


// Location
implementation(libs.play.services.location)

// Hilt
implementation(libs.hilt.android)
implementation(libs.androidx.hilt.navigation.compose)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.github.swent.echo.connectivity

import androidx.compose.runtime.State
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.mapbox.mapboxsdk.geometry.LatLng
import kotlin.time.DurationUnit
import kotlin.time.toDuration
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import org.junit.Assert.assertNotNull
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class GPSServiceImplTest {

companion object {
const val LOCATION_DELAY_MILLIS = 3000
}

private lateinit var gpsService: GPSService

@get:Rule val composeTestRule = createComposeRule()

@Test
fun shouldRefreshLocation() {
lateinit var location: State<LatLng?>
composeTestRule.setContent {
gpsService = GPSServiceImpl(LocalContext.current)
location = gpsService.userLocation.collectAsState()
}
runBlocking { delay(LOCATION_DELAY_MILLIS.toDuration(DurationUnit.MILLISECONDS)) }
assertNotNull(location.value)
}
}
17 changes: 17 additions & 0 deletions app/src/main/java/com/github/swent/echo/connectivity/GPSService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.github.swent.echo.connectivity

import com.mapbox.mapboxsdk.geometry.LatLng
import kotlinx.coroutines.flow.StateFlow

/** A service that provides the user's current location if it's accessible. */
interface GPSService {

/** The user location as a state flow. */
val userLocation: StateFlow<LatLng?>

/** The current user location. */
fun currentUserLocation(): LatLng?

/** Force the service to refresh the user location */
fun refreshUserLocation()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.github.swent.echo.connectivity

import android.content.Context
import android.content.pm.PackageManager
import android.location.LocationManager
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat.getSystemService
import com.github.swent.echo.compose.navigation.LOCATION_PERMISSIONS
import com.google.android.gms.location.LocationServices
import com.mapbox.mapboxsdk.geometry.LatLng
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow

/**
* A simple implementation of a [GPSService]
*
* @param context The context of the application that's using this service.
*/
class GPSServiceImpl(val context: Context) : GPSService {
private val locationProviderClient = LocationServices.getFusedLocationProviderClient(context)

// This manager allows to check whether the location is disabled in settings.
private val locationManager: LocationManager? =
getSystemService(context, LocationManager::class.java)

// User's last known location
private var _location = MutableStateFlow<LatLng?>(null)

/** Updates the last known location. */
private fun getLocation() {
if (
LOCATION_PERMISSIONS.any {
ActivityCompat.checkSelfPermission(context, it) == PackageManager.PERMISSION_GRANTED
}
) {
locationProviderClient.lastLocation.addOnSuccessListener {
it?.apply {
val old = _location.value
_location.compareAndSet(
old,
this.let { location -> LatLng(location.latitude, location.longitude) }
)
}
// Request again if null, unless location is turned off in settings.
// Even if this process has permissions to access the location, as
// long as it's disabled in settings, the listener will return null.
locationManager?.apply {
// TODO maybe also check for NETWORK_PROVIDER
if (isProviderEnabled(LocationManager.GPS_PROVIDER)) {
it ?: getLocation()
}
}
}
}
}

init {
getLocation()
}

override val userLocation: StateFlow<LatLng?> = _location.asStateFlow()

override fun currentUserLocation(): LatLng? = _location.value

override fun refreshUserLocation() = getLocation()
}
22 changes: 22 additions & 0 deletions app/src/main/java/com/github/swent/echo/di/GPSServiceModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.github.swent.echo.di

import android.app.Application
import com.github.swent.echo.connectivity.GPSService
import com.github.swent.echo.connectivity.GPSServiceImpl
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object GPSServiceModule {

@Singleton
@Provides
fun provideGPSService(application: Application): GPSService {
val context = application.applicationContext
return GPSServiceImpl(context)
}
}
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ androidSdk = "10.2.0"
#####################
kotlinxSerializationJson = "1.6.3"
ktorClientAndroid = "2.3.9"
playServicesLocation = "21.3.0"
ramaniMaplibre = "0.3.0"
robolectric = "4.12"
roomRuntime = "2.6.1"
Expand Down Expand Up @@ -47,6 +48,7 @@ hilt-android-testing = { module = "com.google.dagger:hilt-android-testing", vers
kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinxSerializationJson" }
ktor-client-android = { module = "io.ktor:ktor-client-android", version.ref = "ktorClientAndroid" }
mockk-android = { module = "io.mockk:mockk-android", version.ref = "mockk" }
play-services-location = { module = "com.google.android.gms:play-services-location", version.ref = "playServicesLocation" }
ramani-maplibre = { module = "org.ramani-maps:ramani-maplibre", version.ref = "ramaniMaplibre" }
robolectric = { module = "org.robolectric:robolectric", version.ref = "robolectric" }
slf4j-simple = { module = "org.slf4j:slf4j-simple", version.ref = "slf4jSimple" }
Expand Down