Skip to content

Commit

Permalink
Merge pull request #105 from predictive-technology-laboratory/ios-health
Browse files Browse the repository at this point in the history
Integrated HealthKit
  • Loading branch information
MatthewGerber committed Sep 22, 2015
2 parents 9dba64f + ccbdeea commit 82fb7d8
Show file tree
Hide file tree
Showing 27 changed files with 954 additions and 165 deletions.
2 changes: 1 addition & 1 deletion Sensus.Android/Sensus.Android.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand Down
4 changes: 3 additions & 1 deletion Sensus.iOS/Entitlements.plist
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?xml version="1.0" encoding="UTF-8" ?>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.developer.healthkit</key>
<true/>
</dict>
</plist>
79 changes: 79 additions & 0 deletions Sensus.iOS/Probes/User/Health/iOSHealthKitBiologicalSexProbe.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright 2014 The Rector & Visitors of the University of Virginia
//
// 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.

using System;
using System.Collections.Generic;
using SensusService;
using System.Threading;
using HealthKit;
using Foundation;
using SensusService.Probes.User.Health;
using Newtonsoft.Json;

namespace Sensus.iOS.Probes.User.Health
{
public class iOSHealthKitBiologicalSexProbe : iOSHealthKitProbe
{
protected override string DefaultDisplayName
{
get
{
return "Biological Sex (HealthKit)";
}
}

public override Type DatumType
{
get
{
return typeof(BiologicalSexDatum);
}
}

public override int DefaultPollingSleepDurationMS
{
get
{
return int.MaxValue;
}
}

public iOSHealthKitBiologicalSexProbe()
: base(HKObjectType.GetCharacteristicType(HKCharacteristicTypeIdentifierKey.BiologicalSex))
{
}

protected override IEnumerable<Datum> Poll(CancellationToken cancellationToken)
{
List<Datum> data = new List<Datum>();

NSError error;
HKBiologicalSexObject biologicalSex = HealthStore.GetBiologicalSex(out error);

if (error == null)
{
if (biologicalSex.BiologicalSex == HKBiologicalSex.Female)
data.Add(new BiologicalSexDatum(DateTimeOffset.Now, BiologicalSex.Female));
else if (biologicalSex.BiologicalSex == HKBiologicalSex.Male)
data.Add(new BiologicalSexDatum(DateTimeOffset.Now, BiologicalSex.Male));
else if (biologicalSex.BiologicalSex == HKBiologicalSex.Other)
data.Add(new BiologicalSexDatum(DateTimeOffset.Now, BiologicalSex.Other));
}
else
SensusServiceHelper.Get().Logger.Log("Error reading biological sex: " + error.Description, LoggingLevel.Normal, GetType());

return data;
}
}
}
72 changes: 72 additions & 0 deletions Sensus.iOS/Probes/User/Health/iOSHealthKitBirthdateProbe.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright 2014 The Rector & Visitors of the University of Virginia
//
// 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.

using System;
using System.Collections.Generic;
using SensusService;
using System.Threading;
using HealthKit;
using Foundation;
using Xamarin.Forms.Platform.iOS;
using Newtonsoft.Json;

namespace Sensus.iOS.Probes.User.Health
{
public class iOSHealthKitBirthdateProbe : iOSHealthKitProbe
{
protected override string DefaultDisplayName
{
get
{
return "Birthdate (HealthKit)";
}
}

public override Type DatumType
{
get
{
return typeof(BirthdateDatum);
}
}

public override int DefaultPollingSleepDurationMS
{
get
{
return int.MaxValue;
}
}

public iOSHealthKitBirthdateProbe()
: base(HKObjectType.GetCharacteristicType(HKCharacteristicTypeIdentifierKey.DateOfBirth))
{
}

protected override IEnumerable<Datum> Poll(CancellationToken cancellationToken)
{
List<Datum> data = new List<Datum>();

NSError error;
NSDate dateOfBirth = HealthStore.GetDateOfBirth(out error);

if (error == null)
data.Add(new BirthdateDatum(DateTimeOffset.Now, new DateTimeOffset(dateOfBirth.ToDateTime())));
else
SensusServiceHelper.Get().Logger.Log("Error reading date of birth: " + error.Description, LoggingLevel.Normal, GetType());

return data;
}
}
}
89 changes: 89 additions & 0 deletions Sensus.iOS/Probes/User/Health/iOSHealthKitBloodTypeProbe.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright 2014 The Rector & Visitors of the University of Virginia
//
// 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.

using System;
using HealthKit;
using System.Collections.Generic;
using SensusService;
using System.Threading;
using Foundation;
using SensusService.Probes.User.Health;
using Newtonsoft.Json;

namespace Sensus.iOS.Probes.User.Health
{
public class iOSHealthKitBloodTypeProbe : iOSHealthKitProbe
{
protected override string DefaultDisplayName
{
get
{
return "Blood Type (HealthKit)";
}
}

public override Type DatumType
{
get
{
return typeof(BloodTypeDatum);
}
}

public override int DefaultPollingSleepDurationMS
{
get
{
return int.MaxValue;
}
}

public iOSHealthKitBloodTypeProbe()
: base(HKObjectType.GetCharacteristicType(HKCharacteristicTypeIdentifierKey.BloodType))
{
}

protected override IEnumerable<Datum> Poll(CancellationToken cancellationToken)
{
List<Datum> data = new List<Datum>();

NSError error;
HKBloodTypeObject bloodType = HealthStore.GetBloodType(out error);

if (error == null)
{
if (bloodType.BloodType == HKBloodType.ABNegative)
data.Add(new BloodTypeDatum(DateTimeOffset.Now, BloodType.ABNegative));
else if (bloodType.BloodType == HKBloodType.ABPositive)
data.Add(new BloodTypeDatum(DateTimeOffset.Now, BloodType.ABPositive));
else if (bloodType.BloodType == HKBloodType.ANegative)
data.Add(new BloodTypeDatum(DateTimeOffset.Now, BloodType.ANegative));
else if (bloodType.BloodType == HKBloodType.APositive)
data.Add(new BloodTypeDatum(DateTimeOffset.Now, BloodType.APositive));
else if (bloodType.BloodType == HKBloodType.BNegative)
data.Add(new BloodTypeDatum(DateTimeOffset.Now, BloodType.BNegative));
else if (bloodType.BloodType == HKBloodType.BPositive)
data.Add(new BloodTypeDatum(DateTimeOffset.Now, BloodType.BPositive));
else if (bloodType.BloodType == HKBloodType.ONegative)
data.Add(new BloodTypeDatum(DateTimeOffset.Now, BloodType.ONegative));
else if (bloodType.BloodType == HKBloodType.OPositive)
data.Add(new BloodTypeDatum(DateTimeOffset.Now, BloodType.OPositive));
}
else
SensusServiceHelper.Get().Logger.Log("Error reading blood type: " + error.Description, LoggingLevel.Normal, GetType());

return data;
}
}
}
65 changes: 65 additions & 0 deletions Sensus.iOS/Probes/User/Health/iOSHealthKitHeightProbe.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2014 The Rector & Visitors of the University of Virginia
//
// 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.

using System;
using SensusService.Probes.User.Health;
using HealthKit;
using Xamarin.Forms.Platform.iOS;
using SensusService;
using Newtonsoft.Json;

namespace Sensus.iOS.Probes.User.Health
{
public class iOSHealthKitHeightProbe : iOSHealthKitSamplingProbe
{
protected override string DefaultDisplayName
{
get
{
return "Height (HealthKit)";
}
}

public override Type DatumType
{
get
{
return typeof(HeightDatum);
}
}

public override int DefaultPollingSleepDurationMS
{
get
{
return int.MaxValue;
}
}

public iOSHealthKitHeightProbe()
: base(HKObjectType.GetQuantityType(HKQuantityTypeIdentifierKey.Height))
{
}

protected override Datum ConvertSampleToDatum(HKSample sample)
{
HKQuantitySample quantitySample = sample as HKQuantitySample;

if (quantitySample == null)
return null;
else
return new HeightDatum(new DateTimeOffset(quantitySample.StartDate.ToDateTime()), quantitySample.Quantity.GetDoubleValue(HKUnit.Inch));
}
}
}
Loading

0 comments on commit 82fb7d8

Please sign in to comment.