Skip to content

Conversation

@klnfreedom
Copy link

This pull request introduces improved permission handling for health data retrieval on Android and adds support for tracking the raw workout activity type in the Dart data model. The changes focus on ensuring that sensitive health data is only accessed when the appropriate permissions are granted, with clear logging and error handling, and on making the workout data model more robust and informative.

Android: Enhanced permission checks and error handling

  • Added a new HealthPermissionChecker class to centralize and simplify permission checks for location, distance, calories burned, and steps data. This class ensures that data is only accessed if the corresponding permissions are granted, improving security and user privacy. (android/src/main/kotlin/cachet/plugins/health/HealthPermissionChecker.kt)
  • Updated HealthDataReader to use HealthPermissionChecker for checking permissions before retrieving distance, calories burned, and steps data. If permissions are missing, the code now logs a message and skips data retrieval for that type, instead of attempting access. (android/src/main/kotlin/cachet/plugins/health/HealthDataReader.kt) [1] [2] [3] [4]
  • Improved error handling in HealthDataReader by returning a more descriptive error with an empty list when data is unavailable, instead of just an empty list. (android/src/main/kotlin/cachet/plugins/health/HealthDataReader.kt)

Copy link

@SamadiPour SamadiPour left a comment

Choose a reason for hiding this comment

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

Thanks for this PR. I also have this issue because it doesn't work without requesting these permissions. You also can't submit to Google Play if you request extra permissions that you don't actually need.
I added a few small notes that can improve it.

var totalDistance = 0.0
for (distanceRec in distanceRequest.records) {
totalDistance += distanceRec.distance.inMeters
if (permissionChecker.isLocationPermissionGranted() && permissionChecker.isHealthDistancePermissionGranted()) {

Choose a reason for hiding this comment

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

Why do you need both permissions to obtain distance? From what I understand, you only need READ_DISTANCE to access this.
Even if having location permission means we can access the distance, then the condition should be OR instead of AND


// Get distance data
val distanceRequest = healthConnectClient.readRecords(

Choose a reason for hiding this comment

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

I think you forgot to remove this. You are requesting distance twice.

),
),
)
var totalEnergyBurned = 0.0

Choose a reason for hiding this comment

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

I think it would be better to return null for these values when we don't have permission. This way, we have an indication of whether we don't have access to this permission or if it's actually 0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants