Skip to content

Commit

Permalink
fix: [ANDROAPP-6605] Use Network provider for location services
Browse files Browse the repository at this point in the history
  • Loading branch information
xavimolloy authored and vgarciabnz committed Nov 4, 2024
1 parent f68cc11 commit 33cffb4
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 13 deletions.
3 changes: 3 additions & 0 deletions commons/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission
android:name="android.permission.ACCESS_COARSE_LOCATION"
android:maxSdkVersion="22" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import android.content.pm.PackageManager
import android.location.Location
import android.location.LocationListener
import android.location.LocationManager
import android.os.Bundle
import androidx.core.app.ActivityCompat
import androidx.core.location.LocationListenerCompat

private const val FUSED_LOCATION_PROVIDER = "fused"

Expand Down Expand Up @@ -55,7 +55,7 @@ open class LocationProviderImpl(val context: Context) : LocationProvider {
onLocationProviderChanged: () -> Unit,
) {
if (hasPermission()) {
locationListener = object : LocationListener {
locationListener = object : LocationListenerCompat {
override fun onLocationChanged(location: Location) {
onNewLocation(location)
}
Expand All @@ -67,15 +67,10 @@ open class LocationProviderImpl(val context: Context) : LocationProvider {
override fun onProviderDisabled(provider: String) {
onLocationProviderChanged()
}

@Deprecated("Deprecated in Java")
override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) {
// Need implementation for compatibility
}
}

locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
LocationManager.NETWORK_PROVIDER,
500,
0f,
requireNotNull(locationListener),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package org.dhis2.maps.managers

import android.annotation.SuppressLint
import android.app.Activity
import android.location.LocationListener
import android.os.Bundle
import androidx.core.content.ContextCompat
import androidx.core.location.LocationListenerCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
Expand Down Expand Up @@ -79,7 +79,7 @@ abstract class MapManager(

fun init(
mapStyles: List<BaseMapStyle>,
locationListener: LocationListener? = null,
locationListener: LocationListenerCompat? = null,
onInitializationFinished: () -> Unit = {},
onMissingPermission: (PermissionsManager?) -> Unit,
) {
Expand Down Expand Up @@ -237,7 +237,7 @@ abstract class MapManager(
private fun enableLocationComponent(
style: Style,
onMissingPermission: (PermissionsManager?) -> Unit,
locationListener: LocationListener?,
locationListener: LocationListenerCompat?,
) {
map?.locationComponent?.apply {
if (PermissionsManager.areLocationPermissionsGranted(mapView.context)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package org.dhis2.maps.views
import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.location.LocationListener
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.activity.viewModels
Expand All @@ -13,6 +12,7 @@ import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.core.location.LocationListenerCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
Expand All @@ -39,7 +39,7 @@ class MapSelectorActivity : AppCompatActivity() {

private val locationProvider = MapLocationEngine(this)

private val locationListener = LocationListener { location ->
private val locationListener = LocationListenerCompat { location ->
mapSelectorViewModel.onNewLocation(
SelectedLocation.GPSResult(
location.latitude,
Expand Down

0 comments on commit 33cffb4

Please sign in to comment.