TP-71295 | Sidharth Bamba | Geolocation api integration (#11556)
This commit is contained in:
@@ -163,6 +163,7 @@ object FirebaseRemoteConfigHelper {
|
||||
const val PREFETCH_DELAY_IN_MILLIS = "PREFETCH_DELAY_IN_MILLIS"
|
||||
const val ENABLE_REACT_PREFETCH_IN_JOURNEY = "ENABLE_REACT_PREFETCH_IN_JOURNEY"
|
||||
const val ENABLE_REACT_PREFETCH_IN_TAB = "ENABLE_REACT_PREFETCH_IN_TAB"
|
||||
const val NATIVE_GEOLOCATION_ENABLED = "NATIVE_GEOLOCATION_ENABLED"
|
||||
|
||||
fun init() {
|
||||
remoteConfig = getFirebaseRemoteConfig()
|
||||
|
||||
@@ -7,13 +7,18 @@
|
||||
|
||||
package com.navi.common.geocoding
|
||||
|
||||
import android.location.Geocoder
|
||||
import com.google.firebase.crashlytics.FirebaseCrashlytics
|
||||
import com.navi.base.sharedpref.CommonPrefConstants
|
||||
import com.navi.base.sharedpref.PreferenceManager
|
||||
import com.navi.common.firebaseremoteconfig.FirebaseRemoteConfigHelper
|
||||
import com.navi.common.firebaseremoteconfig.FirebaseRemoteConfigHelper.NATIVE_GEOLOCATION_ENABLED
|
||||
import com.navi.common.geocoding.model.DeviceLocation
|
||||
import com.navi.common.geocoding.model.network.GeocodingRequest
|
||||
import com.navi.common.network.models.isSuccessWithData
|
||||
import com.navi.common.utils.CommonNaviAnalytics
|
||||
import com.navi.common.utils.Constants.SOURCE_NATIVE_API
|
||||
import com.navi.common.utils.Constants.SOURCE_NETWORK
|
||||
import com.navi.common.utils.MqttEventBus
|
||||
import java.math.RoundingMode
|
||||
import java.text.DecimalFormat
|
||||
@@ -32,7 +37,7 @@ class FetchGeocodingUseCase(
|
||||
) {
|
||||
private val isRunning = AtomicBoolean(false)
|
||||
|
||||
fun execute(latitude: Double, longitude: Double) {
|
||||
fun execute(latitude: Double, longitude: Double, geocoder: Geocoder) {
|
||||
if (isRunning.get()) {
|
||||
return
|
||||
}
|
||||
@@ -47,42 +52,31 @@ class FetchGeocodingUseCase(
|
||||
return@launch
|
||||
}
|
||||
|
||||
val response =
|
||||
geocodingRepository.getLocationGeocoding(
|
||||
geocodingRequest =
|
||||
GeocodingRequest(
|
||||
latitude = latitude.toString(),
|
||||
longitude = longitude.toString()
|
||||
)
|
||||
)
|
||||
if (response.isSuccessWithData()) {
|
||||
val geocodingResponse = response.data!!
|
||||
geocodingEventTracker.onNewGoecodingFetched(geocodingResponse = geocodingResponse)
|
||||
val deviceLocation =
|
||||
DeviceLocation(
|
||||
latitude = latitude.toString(),
|
||||
longitude = longitude.toString(),
|
||||
timestamp = System.currentTimeMillis().toString(),
|
||||
city = geocodingResponse.city.orEmpty(),
|
||||
state = geocodingResponse.state.orEmpty(),
|
||||
district = geocodingResponse.district.orEmpty(),
|
||||
pincode = geocodingResponse.pincode.orEmpty()
|
||||
val deviceLocation =
|
||||
if (FirebaseRemoteConfigHelper.getBoolean(NATIVE_GEOLOCATION_ENABLED)) {
|
||||
geocodingEventTracker.sourceOfLocation(source = SOURCE_NATIVE_API)
|
||||
getDeviceLocationFromGeocoderApi(
|
||||
latitude = latitude,
|
||||
longitude = longitude,
|
||||
geocoder = geocoder
|
||||
)
|
||||
} else {
|
||||
geocodingEventTracker.sourceOfLocation(source = SOURCE_NETWORK)
|
||||
getDeviceLocationFromNetwork(latitude, longitude)
|
||||
}
|
||||
|
||||
geocodingEventTracker.onLocationFetched(
|
||||
deviceLocation = deviceLocation ?: DeviceLocation()
|
||||
)
|
||||
deviceLocation?.let {
|
||||
PreferenceManager.saveObjectSecurely(
|
||||
CommonPrefConstants.DEVICE_LOCATION,
|
||||
deviceLocation
|
||||
)
|
||||
MqttEventBus.triggerEvent(deviceLocation)
|
||||
} else {
|
||||
analyticsErrorEventTracker.onApiFailure(
|
||||
eventName = "device_geocoding_error",
|
||||
error = response.errors?.firstOrNull(),
|
||||
errorMessage = response.error,
|
||||
apiUrl = response.error?.apiUrl
|
||||
)
|
||||
}
|
||||
isRunning.set(false)
|
||||
}
|
||||
isRunning.set(false)
|
||||
}
|
||||
|
||||
private fun getLastSavedDeviceLocation(): DeviceLocation? {
|
||||
@@ -120,4 +114,62 @@ class FetchGeocodingUseCase(
|
||||
val decimalFormat = DecimalFormat("#.$hashes").apply { roundingMode = RoundingMode.DOWN }
|
||||
return decimalFormat
|
||||
}
|
||||
|
||||
private fun getDeviceLocationFromGeocoderApi(
|
||||
latitude: Double,
|
||||
longitude: Double,
|
||||
geocoder: Geocoder
|
||||
): DeviceLocation? {
|
||||
try {
|
||||
val addresses = geocoder.getFromLocation(latitude, longitude, 1)
|
||||
if (!addresses.isNullOrEmpty()) {
|
||||
val geoCodingApiAddress = addresses[0]
|
||||
val deviceLocation =
|
||||
DeviceLocation(
|
||||
latitude = latitude.toString(),
|
||||
longitude = longitude.toString(),
|
||||
timestamp = System.currentTimeMillis().toString(),
|
||||
city = geoCodingApiAddress.locality.orEmpty(),
|
||||
state = geoCodingApiAddress.adminArea.orEmpty(),
|
||||
district = geoCodingApiAddress.subAdminArea.orEmpty(),
|
||||
pincode = geoCodingApiAddress.postalCode.orEmpty()
|
||||
)
|
||||
return deviceLocation
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
FirebaseCrashlytics.getInstance().recordException(e)
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
private suspend fun getDeviceLocationFromNetwork(
|
||||
latitude: Double,
|
||||
longitude: Double,
|
||||
): DeviceLocation? {
|
||||
val response =
|
||||
geocodingRepository.getLocationGeocoding(
|
||||
geocodingRequest =
|
||||
GeocodingRequest(
|
||||
latitude = latitude.toString(),
|
||||
longitude = longitude.toString()
|
||||
)
|
||||
)
|
||||
if (response.isSuccessWithData()) {
|
||||
val geocodingResponse = response.data!!
|
||||
geocodingEventTracker.onNewGoecodingFetched(geocodingResponse = geocodingResponse)
|
||||
val deviceLocation =
|
||||
DeviceLocation(
|
||||
latitude = latitude.toString(),
|
||||
longitude = longitude.toString(),
|
||||
timestamp = System.currentTimeMillis().toString(),
|
||||
city = geocodingResponse.city.orEmpty(),
|
||||
state = geocodingResponse.state.orEmpty(),
|
||||
district = geocodingResponse.district.orEmpty(),
|
||||
pincode = geocodingResponse.pincode.orEmpty()
|
||||
)
|
||||
return deviceLocation
|
||||
}
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import android.annotation.SuppressLint
|
||||
import android.app.Activity
|
||||
import android.app.Application
|
||||
import android.content.Context
|
||||
import android.location.Geocoder
|
||||
import android.location.Location.distanceBetween
|
||||
import android.location.LocationManager
|
||||
import android.os.Build
|
||||
@@ -80,6 +81,7 @@ class NaviLocationManager(val applicationContext: Context? = CommonLibManager.ap
|
||||
|
||||
private val encryptionHelper: NaviDataEncryptionHelper = NaviDataEncryptionHelper()
|
||||
private val fetchGeocodingUseCase: FetchGeocodingUseCase = FetchGeocodingUseCase()
|
||||
val geoCoder = Geocoder(applicationContext!!)
|
||||
|
||||
private val locationCallback =
|
||||
object : LocationCallback() {
|
||||
@@ -90,7 +92,8 @@ class NaviLocationManager(val applicationContext: Context? = CommonLibManager.ap
|
||||
location?.let {
|
||||
fetchGeocodingUseCase.execute(
|
||||
latitude = location.latitude,
|
||||
longitude = location.longitude
|
||||
longitude = location.longitude,
|
||||
geocoder = geoCoder
|
||||
)
|
||||
val encryptionRequired =
|
||||
FirebaseRemoteConfigHelper.getBoolean(
|
||||
|
||||
@@ -19,6 +19,7 @@ import com.navi.base.utils.isNull
|
||||
import com.navi.base.utils.orFalse
|
||||
import com.navi.base.utils.orZero
|
||||
import com.navi.common.constants.VENDOR_NAVI_API
|
||||
import com.navi.common.geocoding.model.DeviceLocation
|
||||
import com.navi.common.geocoding.model.network.GeocodingResponse
|
||||
import com.navi.common.model.ModuleNameV2
|
||||
import com.navi.common.network.models.ErrorMessage
|
||||
@@ -1061,6 +1062,20 @@ class CommonNaviAnalytics private constructor() {
|
||||
mapOf("latitude" to latitude.toString(), "longitude" to longitude.toString())
|
||||
)
|
||||
}
|
||||
|
||||
fun sourceOfLocation(source: String) {
|
||||
NaviTrackEvent.trackEvent(
|
||||
eventName = "geocoding_source",
|
||||
eventValues = mapOf("source" to source)
|
||||
)
|
||||
}
|
||||
|
||||
fun onLocationFetched(deviceLocation: DeviceLocation) {
|
||||
NaviTrackEvent.trackEvent(
|
||||
eventName = "geocoding_location_fetched",
|
||||
eventValues = mapOf("deviceLocation" to deviceLocation.toString())
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
@@ -318,4 +318,8 @@ object Constants {
|
||||
const val INIT_RESOURCE_DOWNLOAD_WORKER = "init_resource_download_worker"
|
||||
const val RESOURCE_DOWNLOAD_BUCKET = "resource_download_bucket"
|
||||
const val BUCKET_SIZE = "bucket_size"
|
||||
|
||||
// source of geocoding
|
||||
const val SOURCE_NATIVE_API = "Android Native API"
|
||||
const val SOURCE_NETWORK = "MMI_API"
|
||||
}
|
||||
|
||||
@@ -501,5 +501,9 @@
|
||||
<key>PREFETCH_DELAY_IN_MILLIS</key>
|
||||
<value>3000</value>
|
||||
</entry>
|
||||
<entry>
|
||||
<key>IS_TAKE_GEOLOCATION_FROM_ANDROID_NATIVE_API</key>
|
||||
<value>true</value>
|
||||
</entry>
|
||||
|
||||
</defaultsMap>
|
||||
Reference in New Issue
Block a user