Skip to content

Commit

Permalink
Merge branch 'master' into feature/combine-workout-types
Browse files Browse the repository at this point in the history
  • Loading branch information
SlimShadyIAm committed Aug 22, 2024
2 parents 3ba6222 + e076c8e commit 6ebea09
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 115 deletions.
67 changes: 31 additions & 36 deletions packages/health/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

Enables reading and writing health data from/to Apple Health and Health Connect.

> [!IMPORTANT]
> Google has deprecated the Google Fit API. According to the [documentation](https://developers.google.com/fit/android), the API will no longer be available after **June 30, 2025**. As such, this package has removed support for Google Fit as of version 11.0.0 and users are urged to upgrade as soon as possible.
> **NOTE:** Google has deprecated the Google Fit API. According to the [documentation](https://developers.google.com/fit/android), as of **May 1st 2014** developers cannot sign up for using the API. As such, this package has removed support for Google Fit as of version 11.0.0 and users are urged to upgrade as soon as possible.
The plugin supports:

Expand All @@ -26,7 +25,7 @@ See the tables below for supported health and workout data types.

### Apple Health (iOS)

Step 1: Append the `Info.plist` with the following 2 entries
First, add the following 2 entries to the `Info.plist`:

```xml
<key>NSHealthShareUsageDescription</key>
Expand All @@ -35,24 +34,9 @@ Step 1: Append the `Info.plist` with the following 2 entries
<string>We will sync your data with the Apple Health app to give you better insights</string>
```

Step 2: Open your Flutter project in Xcode by right clicking on the "ios" folder and selecting "Open in Xcode". Next, enable "HealthKit" by adding a capability inside the "Signing & Capabilities" tab of the Runner target's settings.
Then, open your Flutter project in Xcode by right clicking on the "ios" folder and selecting "Open in Xcode". Next, enable "HealthKit" by adding a capability inside the "Signing & Capabilities" tab of the Runner target's settings.

### Android

Starting from API level 28 (Android 9.0) accessing some fitness data (e.g. Steps) requires a special permission. To set it add the following line to your `AndroidManifest.xml` file.

```xml
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION"/>
```

Additionally, for workouts, if the distance of a workout is requested then the location permissions below are needed.

```xml
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
```

#### Health Connect
### Google Health Connect (Android)

Health Connect requires the following lines in the `AndroidManifest.xml` file (see also the example app):

Expand Down Expand Up @@ -81,17 +65,38 @@ In the Health Connect permissions activity there is a link to your privacy polic
</activity-alias>
```

Health Connect on Android it requires special permissions in the `AndroidManifest.xml` file. The permissions can be found here: <https://developer.android.com/guide/health-and-fitness/health-connect/data-and-data-types/data-types>
For each data type you want to access, the READ and WRITE permissions need to be added to the `AndroidManifest.xml` file. The list of [permissions](https://developer.android.com/health-and-fitness/guides/health-connect/plan/data-types#permissions) can be found here on the [data types](https://developer.android.com/health-and-fitness/guides/health-connect/plan/data-types) page.

Example shown here (can also be found in the example app):
An example of asking for permission to read and write heart rate data is shown below and more examples can also be found in the example app.

```xml
<uses-permission android:name="android.permission.health.READ_HEART_RATE"/>
<uses-permission android:name="android.permission.health.WRITE_HEART_RATE"/>
...
```

Furthermore, an `intent-filter` needs to be added to the `.MainActivity` activity.
Accessing fitness data (e.g. Steps) requires permission to access the "Activity Recognition" API. To set it add the following line to your `AndroidManifest.xml` file.

```xml
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION"/>
```

Additionally, for workouts, if the distance of a workout is requested then the location permissions below are needed.

```xml
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
```

Because this is labeled as a `dangerous` protection level, the permission system will not grant it automatically and it requires the user's action.
You can prompt the user for it using the [permission_handler](https://pub.dev/packages/permission_handler) plugin.
Follow the plugin setup instructions and add the following line before requesting the data:

```dart
await Permission.activityRecognition.request();
await Permission.location.request();
```

Finally, an `intent-filter` needs to be added to the `.MainActivity` activity.

```xml
<activity
Expand All @@ -109,17 +114,7 @@ Furthermore, an `intent-filter` needs to be added to the `.MainActivity` activit

There's a `debug`, `main` and `profile` version which are chosen depending on how you start your app. In general, it's sufficient to add permission only to the `main` version.

Because this is labeled as a `dangerous` protection level, the permission system will not grant it automatically and it requires the user's action.

You can prompt the user for it using the [permission_handler](https://pub.dev/packages/permission_handler) plugin.
Follow the plugin setup instructions and add the following line before requesting the data:

```dart
await Permission.activityRecognition.request();
await Permission.location.request();
```

### Android 14
#### Android 14

This plugin uses the new `registerForActivityResult` when requesting permissions from Health Connect.
In order for that to work, the Main app's activity should extend `FlutterFragmentActivity` instead of `FlutterActivity`.
Expand All @@ -137,7 +132,7 @@ class MainActivity: FlutterFragmentActivity() {
}
```

### Android X
#### Android X

Replace the content of the `android/gradle.properties` file with the following lines:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ class HealthPlugin(private var channel: MethodChannel? = null) :
) {
handler?.post { mResult?.error(errorCode, errorMessage, errorDetails) }
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?): Boolean {
return false
}

/** Handle calls from the MethodChannel */
override fun onMethodCall(call: MethodCall, result: Result) {
Expand Down
5 changes: 0 additions & 5 deletions packages/health/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,6 @@ class _HealthAppState extends State<HealthApp> {
type: HealthDataType.HEART_RATE,
startTime: earlier,
endTime: now);
success &= await Health().writeHealthData(
value: 30,
type: HealthDataType.HEART_RATE_VARIABILITY_RMSSD,
startTime: earlier,
endTime: now);
success &= await Health().writeHealthData(
value: 37,
type: HealthDataType.BODY_TEMPERATURE,
Expand Down
110 changes: 53 additions & 57 deletions packages/health/lib/health.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 4 additions & 8 deletions packages/health/lib/src/health_plugin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,10 @@ class Health {
throw ArgumentError(
"Adding workouts should be done using the writeWorkoutData method.");
}
// If not implemented on platform, throw an exception
if (!isDataTypeAvailable(type)) {
throw HealthException(type, 'Not available on platform $platformType');
}
endTime ??= startTime;
if (startTime.isAfter(endTime)) {
throw ArgumentError("startTime must be equal or earlier than endTime");
Expand Down Expand Up @@ -1190,12 +1194,7 @@ class Health {
HealthWorkoutActivityType.YOGA,

// Android only
HealthWorkoutActivityType.BIKING_HAND,
HealthWorkoutActivityType.BIKING_MOUNTAIN,
HealthWorkoutActivityType.BIKING_ROAD,
HealthWorkoutActivityType.BIKING_SPINNING,
HealthWorkoutActivityType.BIKING_STATIONARY,
HealthWorkoutActivityType.BIKING_UTILITY,
HealthWorkoutActivityType.CALISTHENICS,
HealthWorkoutActivityType.DANCING,
HealthWorkoutActivityType.FRISBEE_DISC,
Expand All @@ -1213,9 +1212,6 @@ class Health {
HealthWorkoutActivityType.SURFING,
HealthWorkoutActivityType.SWIMMING_OPEN_WATER,
HealthWorkoutActivityType.SWIMMING_POOL,
HealthWorkoutActivityType.WALKING_FITNESS,
HealthWorkoutActivityType.WALKING_NORDIC,
HealthWorkoutActivityType.WALKING_STROLLER,
HealthWorkoutActivityType.WALKING_TREADMILL,
HealthWorkoutActivityType.WEIGHTLIFTING,
HealthWorkoutActivityType.WHEELCHAIR,
Expand Down
4 changes: 3 additions & 1 deletion packages/health/lib/src/health_value_types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ class WorkoutHealthValue extends HealthValue {
factory WorkoutHealthValue.fromHealthDataPoint(dynamic dataPoint) =>
WorkoutHealthValue(
workoutActivityType: HealthWorkoutActivityType.values.firstWhere(
(element) => element.name == dataPoint['workoutActivityType']),
(element) => element.name == dataPoint['workoutActivityType'],
orElse: () => HealthWorkoutActivityType.OTHER,
),
totalEnergyBurned: dataPoint['totalEnergyBurned'] != null
? (dataPoint['totalEnergyBurned'] as num).toInt()
: null,
Expand Down
8 changes: 0 additions & 8 deletions packages/health/lib/src/heath_data_types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -529,12 +529,7 @@ enum HealthWorkoutActivityType {
WRESTLING,

// Android only
BIKING_HAND,
BIKING_MOUNTAIN,
BIKING_ROAD,
BIKING_SPINNING,
BIKING_STATIONARY,
BIKING_UTILITY,
CALISTHENICS,
DANCING,
FRISBEE_DISC,
Expand All @@ -551,9 +546,6 @@ enum HealthWorkoutActivityType {
STRENGTH_TRAINING,
SWIMMING_OPEN_WATER,
SWIMMING_POOL,
WALKING_FITNESS,
WALKING_NORDIC,
WALKING_STROLLER,
WALKING_TREADMILL,
WEIGHTLIFTING,
WHEELCHAIR,
Expand Down

0 comments on commit 6ebea09

Please sign in to comment.