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 Location co-ordinates on questionnaire submission #2997

Merged
merged 77 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
77 commits
Select commit Hold shift + click to select a range
563fca0
Add location permissions to get location details on submission
DebbieArita Jan 18, 2024
6c52152
Edit current location
DebbieArita Jan 18, 2024
dcfbe3c
Append precise and approximate location to the questionnaireResponse
DebbieArita Jan 22, 2024
6a9307e
Merge branch 'main' into questionnaire-location
DebbieArita Jan 22, 2024
093138c
Add application config checker
DebbieArita Jan 23, 2024
cf61cc8
Add logQuestionnaireLocation config to toggle functionality
DebbieArita Jan 23, 2024
ca395b2
Merge branch 'main' into questionnaire-location
DebbieArita Jan 23, 2024
eabd1be
Run SpotlessApply
DebbieArita Jan 23, 2024
13c170d
Merge branch 'main' into questionnaire-location
DebbieArita Feb 1, 2024
2427914
Run location calculation in background and add user checks
DebbieArita Feb 1, 2024
d2dd2f0
Update build gradle
DebbieArita Feb 2, 2024
e7babb2
Code refactors
qiarie Feb 6, 2024
57ee61e
Code cleanup
qiarie Feb 6, 2024
6d0a310
Merge remote-tracking branch 'origin/main' into questionnaire-location
qiarie Feb 6, 2024
16285f7
Refactor location implementation
qiarie Feb 6, 2024
ba0fc6f
Merge remote-tracking branch 'origin/main' into questionnaire-location
qiarie Feb 6, 2024
8d8df47
Rename location setup
qiarie Feb 6, 2024
66ec65f
Fix crash on opening location service settings after permissions
qiarie Feb 6, 2024
a910980
Merge remote-tracking branch 'origin/main' into questionnaire-location
qiarie Feb 6, 2024
06c6e7c
Merge branch 'main' into questionnaire-location
brandyodhiambo Feb 8, 2024
52bea17
Resolve merge conflict
brandyodhiambo Feb 8, 2024
c59f335
Add tests
DebbieArita Feb 9, 2024
8431a58
Allow location picking when location service & permissions are alread…
qiarie Feb 9, 2024
4c39e81
add service location test on QuestionnaireActivityTest
brandyodhiambo Feb 13, 2024
ae10339
Merge branch 'main' into questionnaire-location
brandyodhiambo Feb 13, 2024
7cbbac0
Merge remote-tracking branch 'origin/questionnaire-location' into que…
brandyodhiambo Feb 13, 2024
642542d
Merge branch 'main' into questionnaire-location
DebbieArita Feb 19, 2024
a686ce0
Add tests and Run SpotlessApply
DebbieArita Feb 19, 2024
1a8e82c
Merge remote-tracking branch 'origin/questionnaire-location' into que…
DebbieArita Feb 19, 2024
94a01e0
Merge branch 'main' into questionnaire-location
DebbieArita Feb 19, 2024
150c64f
Empty commit
DebbieArita Feb 19, 2024
3b46ada
Update RegisterCardList tests
DebbieArita Feb 20, 2024
7493e33
Merge remote-tracking branch 'origin/main' into questionnaire-location
qiarie Feb 21, 2024
2f47f93
Resolve merge conflicts and WIP questionnaire improvements
DebbieArita Feb 22, 2024
1a9ed26
Merge remote-tracking branch 'origin/main' into questionnaire-location
qiarie Feb 23, 2024
a06ae76
Log GPS location fetch failure
qiarie Feb 23, 2024
4e12927
Update QuestionnaireActivityTests
DebbieArita Feb 23, 2024
baae9b3
Merge branch 'main' into questionnaire-location
DebbieArita Feb 23, 2024
5e4dee6
Merge branch 'main' into questionnaire-location
DebbieArita Feb 23, 2024
2423fc5
Merge remote-tracking branch 'origin/questionnaire-location' into que…
DebbieArita Feb 23, 2024
516f484
Remove comments
DebbieArita Feb 23, 2024
8f63da1
Merge branch 'main' into questionnaire-location
Lentumunai-Mark Feb 26, 2024
554eb83
Update tests WIP
DebbieArita Feb 26, 2024
fe035a6
Merge branch 'main' into questionnaire-location
dubdabasoduba Feb 26, 2024
066d1b9
Merge branch 'main' into questionnaire-location
DebbieArita Feb 27, 2024
a107cc5
Merge branch 'main' into questionnaire-location
DebbieArita Feb 28, 2024
1797620
Update LocationUtils tests
DebbieArita Feb 28, 2024
d30b3bf
Run spotlessApply
DebbieArita Feb 28, 2024
5d2d057
Refactor PermissionsUtilsTest
DebbieArita Feb 28, 2024
4b054df
Refactor ResourceUtilsTest
DebbieArita Feb 28, 2024
56c26b3
Run SpotlessApply
DebbieArita Feb 28, 2024
14e18fe
Merge branch 'main' into questionnaire-location
pld Mar 4, 2024
1210019
Merge remote-tracking branch 'origin/main' into questionnaire-location
qiarie Mar 5, 2024
5623799
Update ApplicationConfiguration docs
qiarie Mar 5, 2024
da25d8d
Merge remote-tracking branch 'origin/main' into questionnaire-location
qiarie Mar 7, 2024
e3719a6
Merge remote-tracking branch 'origin/main' into questionnaire-location
qiarie Mar 7, 2024
b148c88
Merge branch 'main' into questionnaire-location
DebbieArita Mar 7, 2024
ed31f2d
Merge branch 'main' into questionnaire-location
dubdabasoduba Mar 8, 2024
e2857a2
Make logGpsLocations config a list in ApplicationConfiguration
qiarie Mar 11, 2024
cc8727a
Merge remote-tracking branch 'origin/main' into questionnaire-location
qiarie Mar 11, 2024
02e818b
Merge branch 'questionnaire-location' of github.com:opensrp/fhircore …
qiarie Mar 11, 2024
7f8a65c
Update tests
qiarie Mar 11, 2024
aebe8cf
Update test configs
qiarie Mar 11, 2024
76908ef
Merge branch 'main' into questionnaire-location
DebbieArita Mar 12, 2024
ebdd7bb
Update docs on logGpsLocation config option
qiarie Mar 12, 2024
e5577fe
Merge branch 'questionnaire-location' of github.com:opensrp/fhircore …
qiarie Mar 12, 2024
a6587f8
Merge remote-tracking branch 'origin/main' into questionnaire-location
qiarie Mar 12, 2024
1b4aaa4
Merge branch 'main' into questionnaire-location
dubdabasoduba Mar 12, 2024
f5a9f1a
Update android/quest/src/main/java/org/smartregister/fhircore/quest/u…
qiarie Mar 13, 2024
8568ea7
Merge branch 'main' into questionnaire-location
DebbieArita Mar 13, 2024
b4a0513
Run SpotlessApply
DebbieArita Mar 13, 2024
9cc8d0c
Update fhir Location from GPS and remove null safety
DebbieArita Mar 14, 2024
e5d3a9f
Run SpotlessApply
DebbieArita Mar 14, 2024
eeee521
Update test
DebbieArita Mar 14, 2024
6b009f2
Merge branch 'main' into questionnaire-location
dubdabasoduba Mar 15, 2024
34fffda
Update application config docs
DebbieArita Mar 15, 2024
ac82739
Update location permission denied string
DebbieArita Mar 15, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ data class ApplicationConfiguration(
val showLogo: Boolean = true,
val taskBackgroundWorkerBatchSize: Int = 500,
val eventWorkflows: List<EventWorkflow> = emptyList(),
val logQuestionnaireLocation: Boolean = false,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we know that we will (at some point) want to log location at multiple points, can we change this data structure to reflect that?

Suggested change
val logQuestionnaireLocation: Boolean = false,
val logLocationInClass: List<Class<T>> = emptyList(),

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pld Should we move the setting to QuestionnaireConfig?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is appropriate here, because in the future we plan to do location logging outside of the Questionnaire flow

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pld Did you have another use case in mind?

In cases where we would need to log the location in multiple places, I'd go with a location config with all options explicitly listed as below:

@Serializable
data class LocationConfig(
  val logQuestionnaire: Boolean = false,
  val logOtherPlace: Boolean = false,
) : Configuration()

Usage:

val logLocation: LocationConfig = LocationConfig(logQuestionnaire = true),

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pld Using reflection will not be possible in the application configuration. Kotlin Serialization will be unable to deserialize the type. We could opt for a list of enums instead. Every place that needs location logging will be identified by an enum type.

Example

enum class LocationLogArea { 
   QUESTIONNAIRE, 
   ANOTHER_PLACE,
   AND_ANOTHER_PLACE
}

ApplicationConfiguration

{
  "locationLogAreas" : [
     "QUESTIONNAIRE"
   ]
}

Suggest an appropriate name for the property/class.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK yea if we can't use class names, and maybe that won't map to the semantic anyhow, that's fine, enum seems straightforward.

The other use case I had in mind is that we might want to log the location every 5 minutes. Or if you could complete a task by clicking a button in the profile, we might want to log the location on click

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pld This is noted. We had discussed with @marklosh the need to have a periodic logger for their distance from current location use case since picking the location takes time.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have created this issue for the addition of enums to configure location logging

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok let's make the change in this PR before we merge it

) : Configuration()
8 changes: 6 additions & 2 deletions android/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ okhttp = "4.12.0"
okhttp-logging-interceptor = "4.11.0"
orchestrator = "1.4.2"
p2p-lib = "0.6.9-SNAPSHOT"
paging-compose = "3.2.0"
paging-runtime-ktx = "3.2.0"
playServicesLocation = "21.0.1"
paging = "3.2.1"
preference-ktx = "1.2.1"
prettytime = "5.0.2.Final"
Expand Down Expand Up @@ -164,8 +167,9 @@ okhttp = { group = "com.squareup.okhttp3", name = "okhttp", version.ref = "okhtt
okhttp-logging-interceptor = { group = "com.squareup.okhttp3", name = "logging-interceptor", version.ref = "okhttp-logging-interceptor" }
orchestrator = { group = "androidx.test", name = "orchestrator", version.ref = "orchestrator" }
p2p-lib = { group = "org.smartregister", name = "p2p-lib", version.ref = "p2p-lib" }
paging-compose = { group = "androidx.paging", name = "paging-compose", version.ref = "paging" }
paging-runtime-ktx = { group = "androidx.paging", name = "paging-runtime-ktx", version.ref = "paging" }
paging-compose = { group = "androidx.paging", name = "paging-compose", version.ref = "paging-compose" }
paging-runtime-ktx = { group = "androidx.paging", name = "paging-runtime-ktx", version.ref = "paging-runtime-ktx" }
play-services-location = { module = "com.google.android.gms:play-services-location", version.ref = "playServicesLocation" }
preference-ktx = { group = "androidx.preference", name = "preference-ktx", version.ref = "preference-ktx" }
prettytime = { group = "org.ocpsoft.prettytime", name = "prettytime", version.ref = "prettytime" }
retrofit = { group = "com.squareup.retrofit2", name = "retrofit", version.ref = "retrofit" }
Expand Down
1 change: 1 addition & 0 deletions android/quest/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ dependencies {
implementation(libs.material)
implementation(libs.dagger.hilt.android)
implementation(libs.hilt.work)
implementation(libs.play.services.location)

// Annotation processors
kapt(libs.hilt.compiler)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class RegisterCardListTest {
)
}

composeTestRule.onNodeWithTag(REGISTER_CARD_LIST_TEST_TAG).onChildren().assertCountEquals(2)
composeTestRule.onNodeWithTag(REGISTER_CARD_LIST_TEST_TAG).onChildren().assertCountEquals(3)

composeTestRule
.onNodeWithTag(REGISTER_CARD_LIST_TEST_TAG)
Expand Down Expand Up @@ -119,7 +119,7 @@ class RegisterCardListTest {
)
}

composeTestRule.onNodeWithTag(REGISTER_CARD_LIST_TEST_TAG).onChildren().assertCountEquals(3)
composeTestRule.onNodeWithTag(REGISTER_CARD_LIST_TEST_TAG).onChildren().assertCountEquals(4)

composeTestRule
.onNodeWithTag(REGISTER_CARD_LIST_TEST_TAG)
Expand Down
3 changes: 3 additions & 0 deletions android/quest/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,7 @@
android:resource="@xml/file_paths" />
</provider>
</application>

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,26 @@

package org.smartregister.fhircore.quest.ui.questionnaire

import android.Manifest
import android.app.Activity
import android.app.AlertDialog
import android.content.Intent
import android.location.Location
import android.os.Bundle
import android.os.Parcelable
import android.provider.Settings
import android.view.View
import android.widget.Toast
import androidx.activity.OnBackPressedCallback
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.viewModels
import androidx.core.os.bundleOf
import androidx.fragment.app.commit
import androidx.lifecycle.lifecycleScope
import com.google.android.fhir.datacapture.QuestionnaireFragment
import com.google.android.fhir.logicalId
import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.LocationServices
import dagger.hilt.android.AndroidEntryPoint
import java.io.Serializable
import java.util.LinkedList
Expand All @@ -51,6 +58,9 @@
import org.smartregister.fhircore.engine.util.extension.showToast
import org.smartregister.fhircore.quest.R
import org.smartregister.fhircore.quest.databinding.QuestionnaireActivityBinding
import org.smartregister.fhircore.quest.util.LocationUtils
import org.smartregister.fhircore.quest.util.PermissionUtils
import org.smartregister.fhircore.quest.util.ResourceUtils
import timber.log.Timber

@AndroidEntryPoint
Expand All @@ -62,9 +72,16 @@
private lateinit var viewBinding: QuestionnaireActivityBinding
private var questionnaire: Questionnaire? = null
private var alertDialog: AlertDialog? = null
private lateinit var fusedLocationClient: FusedLocationProviderClient
private var currLocation: Location? = null
private lateinit var locationPermissionLauncher: ActivityResultLauncher<Array<String>>
private lateinit var activityResultLauncher: ActivityResultLauncher<Intent>

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

setupLocationServices()

Check warning on line 83 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt#L83

Added line #L83 was not covered by tests

setTheme(org.smartregister.fhircore.engine.R.style.AppTheme_Questionnaire)
viewBinding = QuestionnaireActivityBinding.inflate(layoutInflater)
setContentView(viewBinding.root)
Expand Down Expand Up @@ -107,6 +124,94 @@
)
}

fun setupLocationServices() {
if (viewModel.applicationConfiguration.logQuestionnaireLocation) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (viewModel.applicationConfiguration.logQuestionnaireLocation) {
if (this.javaClass in viewModel.applicationConfiguration.logLocationInClass) {

fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)

Check warning on line 129 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt#L129

Added line #L129 was not covered by tests

if (!LocationUtils.isLocationEnabled(this)) {
openLocationServicesSettings()

Check warning on line 132 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt#L132

Added line #L132 was not covered by tests
}

if (!hasLocationPermissions()) {
launchLocationPermissionsDialog()

Check warning on line 136 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt#L136

Added line #L136 was not covered by tests
}

if (LocationUtils.isLocationEnabled(this) && hasLocationPermissions()) {
fetchLocation(true)

Check warning on line 140 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt#L140

Added line #L140 was not covered by tests
}
}
}

fun hasLocationPermissions(): Boolean {
return PermissionUtils.checkPermissions(
this,
listOf(
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION,

Check warning on line 150 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt#L146-L150

Added lines #L146 - L150 were not covered by tests
),
)
}

fun openLocationServicesSettings() {
activityResultLauncher =
PermissionUtils.getStartActivityForResultLauncher(this) { resultCode, _ ->

Check warning on line 157 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt#L156-L157

Added lines #L156 - L157 were not covered by tests
if (resultCode == RESULT_OK || hasLocationPermissions()) {
fetchLocation()

Check warning on line 159 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt#L159

Added line #L159 was not covered by tests
}
}

val intent = Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)
showLocationSettingsDialog(intent)

Check warning on line 164 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt#L163-L164

Added lines #L163 - L164 were not covered by tests
}

private fun showLocationSettingsDialog(intent: Intent) {
AlertDialog.Builder(this)
.setMessage(getString(R.string.location_services_disabled))
.setCancelable(true)

Check warning on line 170 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt#L168-L170

Added lines #L168 - L170 were not covered by tests
.setPositiveButton(getString(R.string.yes)) { _, _ -> activityResultLauncher.launch(intent) }
.setNegativeButton(getString(R.string.no)) { dialog, _ -> dialog.cancel() }
.show()

Check warning on line 173 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt#L172-L173

Added lines #L172 - L173 were not covered by tests
}

fun launchLocationPermissionsDialog() {
locationPermissionLauncher =
PermissionUtils.getLocationPermissionLauncher(
this,
onFineLocationPermissionGranted = { fetchLocation(true) },
onCoarseLocationPermissionGranted = { fetchLocation(false) },

Check warning on line 181 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt#L177-L181

Added lines #L177 - L181 were not covered by tests
onLocationPermissionDenied = {
Toast.makeText(
this,
getString(R.string.location_permissions_denied),
Toast.LENGTH_SHORT,

Check warning on line 186 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt#L183-L186

Added lines #L183 - L186 were not covered by tests
)
.show()
Timber.e("Location permissions denied")
},

Check warning on line 190 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt#L188-L190

Added lines #L188 - L190 were not covered by tests
)

locationPermissionLauncher.launch(
arrayOf(
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,

Check warning on line 196 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt#L195-L196

Added lines #L195 - L196 were not covered by tests
),
)
}

fun fetchLocation(highAccuracy: Boolean = true) {
lifecycleScope.launch {
try {

Check warning on line 203 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt#L201-L203

Added lines #L201 - L203 were not covered by tests
if (highAccuracy) {
currLocation = LocationUtils.getAccurateLocation(fusedLocationClient)
DebbieArita marked this conversation as resolved.
Show resolved Hide resolved
} else {
currLocation = LocationUtils.getApproximateLocation(fusedLocationClient)
}
} catch (e: Exception) {
Timber.e(e, "Failed to get GPS location")

Check warning on line 210 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt#L209-L210

Added lines #L209 - L210 were not covered by tests
DebbieArita marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.clear()
Expand Down Expand Up @@ -238,6 +343,13 @@
if (questionnaireResponse != null && questionnaire != null) {
viewModel.run {
setProgressState(QuestionnaireProgressState.ExtractionInProgress(true))

if (currLocation != null) {
questionnaireResponse.contained.add(
ResourceUtils.createLocationResource(gpsLocation = currLocation),

Check warning on line 349 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt#L348-L349

Added lines #L348 - L349 were not covered by tests
)
}

handleQuestionnaireSubmission(
questionnaire = questionnaire!!,
currentQuestionnaireResponse = questionnaireResponse,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,11 @@
import org.hl7.fhir.r4.model.ResourceType
import org.hl7.fhir.r4.model.StringType
import org.smartregister.fhircore.engine.BuildConfig
import org.smartregister.fhircore.engine.configuration.ConfigType
import org.smartregister.fhircore.engine.configuration.ConfigurationRegistry
import org.smartregister.fhircore.engine.configuration.GroupResourceConfig
import org.smartregister.fhircore.engine.configuration.QuestionnaireConfig
import org.smartregister.fhircore.engine.configuration.app.ApplicationConfiguration
import org.smartregister.fhircore.engine.data.local.DefaultRepository
import org.smartregister.fhircore.engine.domain.model.ActionParameter
import org.smartregister.fhircore.engine.domain.model.ActionParameterType
Expand Down Expand Up @@ -102,6 +105,7 @@
val sharedPreferencesHelper: SharedPreferencesHelper,
val fhirOperator: FhirOperator,
val fhirPathDataExtractor: FhirPathDataExtractor,
val configurationRegistry: ConfigurationRegistry,

Check warning on line 108 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireViewModel.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireViewModel.kt#L108

Added line #L108 was not covered by tests
) : ViewModel() {
private val parser = FhirContext.forR4Cached().newJsonParser()

Expand All @@ -119,6 +123,10 @@
val questionnaireProgressStateLiveData: LiveData<QuestionnaireProgressState?>
get() = _questionnaireProgressStateLiveData

val applicationConfiguration: ApplicationConfiguration by lazy {

Check warning on line 126 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireViewModel.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireViewModel.kt#L126

Added line #L126 was not covered by tests
configurationRegistry.retrieveConfiguration(ConfigType.Application)
}

/**
* This function retrieves the [Questionnaire] as configured via the [QuestionnaireConfig]. The
* retrieved [Questionnaire] can be pre-populated with computed values from the Rules engine.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright 2021-2024 Ona Systems, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.smartregister.fhircore.quest.util

import android.annotation.SuppressLint
import android.content.Context
import android.location.Location
import android.location.LocationManager
import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.Priority
import com.google.android.gms.tasks.CancellationToken
import com.google.android.gms.tasks.CancellationTokenSource
import com.google.android.gms.tasks.OnTokenCanceledListener
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import timber.log.Timber

object LocationUtils {

fun isLocationEnabled(context: Context): Boolean {
val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager

return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) ||
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
}

@SuppressLint("MissingPermission")
suspend fun getAccurateLocation(
fusedLocationClient: FusedLocationProviderClient,
): Location? {
return withContext(Dispatchers.IO) {

Check warning on line 48 in android/quest/src/main/java/org/smartregister/fhircore/quest/util/LocationUtils.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/util/LocationUtils.kt#L48

Added line #L48 was not covered by tests
suspendCoroutine<Location> { continuation ->
fusedLocationClient
.getCurrentLocation(
Priority.PRIORITY_HIGH_ACCURACY,
object : CancellationToken() {

Check warning on line 53 in android/quest/src/main/java/org/smartregister/fhircore/quest/util/LocationUtils.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/util/LocationUtils.kt#L50-L53

Added lines #L50 - L53 were not covered by tests
override fun onCanceledRequested(p0: OnTokenCanceledListener) =
CancellationTokenSource().token

Check warning on line 55 in android/quest/src/main/java/org/smartregister/fhircore/quest/util/LocationUtils.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/util/LocationUtils.kt#L55

Added line #L55 was not covered by tests

override fun isCancellationRequested() = false

Check warning on line 57 in android/quest/src/main/java/org/smartregister/fhircore/quest/util/LocationUtils.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/util/LocationUtils.kt#L57

Added line #L57 was not covered by tests
},
)
.addOnSuccessListener { location: Location? ->

Check warning on line 60 in android/quest/src/main/java/org/smartregister/fhircore/quest/util/LocationUtils.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/util/LocationUtils.kt#L60

Added line #L60 was not covered by tests
if (location != null) {
Timber.d(
"Accurate location - lat: ${location.latitude}; long: ${location.longitude}; alt: ${location.altitude}",

Check warning on line 63 in android/quest/src/main/java/org/smartregister/fhircore/quest/util/LocationUtils.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/util/LocationUtils.kt#L62-L63

Added lines #L62 - L63 were not covered by tests
)
continuation.resume(location)

Check warning on line 65 in android/quest/src/main/java/org/smartregister/fhircore/quest/util/LocationUtils.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/util/LocationUtils.kt#L65

Added line #L65 was not covered by tests
}
}
.addOnFailureListener { e ->
Timber.e(e, "Failed to get accurate location")
continuation.resumeWithException(e)

Check warning on line 70 in android/quest/src/main/java/org/smartregister/fhircore/quest/util/LocationUtils.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/util/LocationUtils.kt#L68-L70

Added lines #L68 - L70 were not covered by tests
}
}
}
}

@SuppressLint("MissingPermission")
suspend fun getApproximateLocation(
fusedLocationClient: FusedLocationProviderClient,
): Location? {
return withContext(Dispatchers.IO) {

Check warning on line 80 in android/quest/src/main/java/org/smartregister/fhircore/quest/util/LocationUtils.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/util/LocationUtils.kt#L80

Added line #L80 was not covered by tests
suspendCoroutine<Location> { continuation ->
fusedLocationClient
.getCurrentLocation(
Priority.PRIORITY_BALANCED_POWER_ACCURACY,
object : CancellationToken() {

Check warning on line 85 in android/quest/src/main/java/org/smartregister/fhircore/quest/util/LocationUtils.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/util/LocationUtils.kt#L82-L85

Added lines #L82 - L85 were not covered by tests
override fun onCanceledRequested(p0: OnTokenCanceledListener) =
CancellationTokenSource().token

Check warning on line 87 in android/quest/src/main/java/org/smartregister/fhircore/quest/util/LocationUtils.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/util/LocationUtils.kt#L87

Added line #L87 was not covered by tests

override fun isCancellationRequested() = false

Check warning on line 89 in android/quest/src/main/java/org/smartregister/fhircore/quest/util/LocationUtils.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/util/LocationUtils.kt#L89

Added line #L89 was not covered by tests
},
)
.addOnSuccessListener { location: Location? ->

Check warning on line 92 in android/quest/src/main/java/org/smartregister/fhircore/quest/util/LocationUtils.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/util/LocationUtils.kt#L92

Added line #L92 was not covered by tests
if (location != null) {
Timber.d(
"Approx location - lat: ${location.latitude}; long: ${location.longitude}; alt: ${location.altitude}",

Check warning on line 95 in android/quest/src/main/java/org/smartregister/fhircore/quest/util/LocationUtils.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/util/LocationUtils.kt#L94-L95

Added lines #L94 - L95 were not covered by tests
qiarie marked this conversation as resolved.
Show resolved Hide resolved
)
continuation.resume(location)

Check warning on line 97 in android/quest/src/main/java/org/smartregister/fhircore/quest/util/LocationUtils.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/util/LocationUtils.kt#L97

Added line #L97 was not covered by tests
}
}
.addOnFailureListener { e -> continuation.resumeWithException(e) }

Check warning on line 100 in android/quest/src/main/java/org/smartregister/fhircore/quest/util/LocationUtils.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/util/LocationUtils.kt#L100

Added line #L100 was not covered by tests
}
}
}
}
Loading
Loading