Ejemplo n.º 1
0
 public override bool IsDataTypeAvailable(HealthDataType healthDataType)
 {
     try
     {
         return(healthDataType.ToGoogleFit() != null);
     }
     catch (ArgumentOutOfRangeException e)
     {
         Debug.WriteLine($"GoogleFit - Datatype {healthDataType} is not supported in this device", e);
         return(false);
     }
 }
Ejemplo n.º 2
0
        protected override async Task <IEnumerable <T> > Query <T>(HealthDataType healthDataType,
                                                                   AggregateTime aggregateTime,
                                                                   DateTime startDate, DateTime endDate)
        {
            var authorized = _healthService.HasOAuthPermission(_healthService.FitnessReadOptions(new HealthDataType[] { healthDataType }));

            if (!authorized)
            {
                throw new UnauthorizedAccessException($"Not enough permissions to request {healthDataType}");
            }

            var  fitData   = healthDataType.ToGoogleFit();
            long startTime = startDate.ToJavaTimeStamp();
            long endTime   = endDate.ToJavaTimeStamp();

            var readBuilder = new DataReadRequest.Builder()
                              .SetTimeRange(startTime, endTime, TimeUnit.Milliseconds)
                              .EnableServerQueries();

            if (aggregateTime != AggregateTime.None)
            {
                readBuilder.Aggregate(fitData.TypeIdentifier, fitData.AggregateType);

                switch (aggregateTime)
                {
                case AggregateTime.Year:
                    readBuilder.BucketByTime(365, TimeUnit.Days);
                    break;

                case AggregateTime.Month:
                    readBuilder.BucketByTime(31, TimeUnit.Days);
                    break;

                case AggregateTime.Week:
                    readBuilder.BucketByTime(7, TimeUnit.Days);
                    break;

                case AggregateTime.Day:
                    readBuilder.BucketByTime(1, TimeUnit.Days);
                    break;

                case AggregateTime.Hour:
                    readBuilder.BucketByTime(1, TimeUnit.Hours);
                    break;
                }
            }
            else
            {
                readBuilder.Read(fitData.TypeIdentifier);
            }

            var readRequest = readBuilder.Build();

            var response = await FitnessClass.GetHistoryClient(_currentActivity, GoogleSignIn.GetLastSignedInAccount(_currentActivity))
                           .ReadDataAsync(readRequest).ConfigureAwait(false);

            double valueToSubstract = 0;

            if (healthDataType == HealthDataType.CaloriesActive)
            {
                valueToSubstract = await GetBasalAvg(endDate);
            }

            if (response == null)
            {
                return(new List <T>());
            }

            if (response.Buckets.Any())
            {
                var output = new List <T>();
                foreach (var bucket in response.Buckets)
                {
                    var dataSet = bucket.GetDataSet(fitData.AggregateType);
                    output.AddRange((IEnumerable <T>)dataSet.DataPoints.Select(result =>
                                                                               CreateAggregatedData(result, fitData, valueToSubstract)));

                    if (!dataSet.DataPoints.Any() && healthDataType == HealthDataType.Droid_BasalMetabolicRate)
                    {
                        output.Add(
                            new AggregatedHealthData()
                        {
                            StartDate = bucket.GetStartTime(TimeUnit.Milliseconds).ToDateTime(),
                            EndDate   = bucket.GetEndTime(TimeUnit.Milliseconds).ToDateTime(),
                            Sum       = valueToSubstract
                        } as T);
                    }
                }

                return(output);
            }

            return((IEnumerable <T>)response.GetDataSet(fitData.TypeIdentifier)?.DataPoints?
                   .Select(dataPoint => new HealthData
            {
                StartDate = dataPoint.GetStartTime(TimeUnit.Milliseconds).ToDateTime(),
                EndDate = dataPoint.GetEndTime(TimeUnit.Milliseconds).ToDateTime(),
                Value = ReadValue(dataPoint, fitData.Unit, valueToSubstract) ?? 0,
                UserEntered = dataPoint.OriginalDataSource?.StreamName == "user_input"
            }).ToList());
        }