com.samsung.android.sdk.healthdata

Interface HealthDataResolver.AggregateRequest

  • Enclosing class:
    HealthDataResolver


    public static interface HealthDataResolver.AggregateRequest
    This interface represents an aggregate request with aggregate functions and the specified time unit to group result values.

    Request for Aggregating Data

    HealthDataResolver.aggregate() helps that your application aggregates Samsung Health's data.
    The following information is mandatory to build an AggregateRequest instance.

    You can make a request instance with HealthDataResolver.AggregateRequest.Builder.build().

    Example: Aggregate Function

    To know the average body temperature by tags, you can set an AggregateRequest object with:

    • setDataType()
    • addFunction()
    • addGroup()

       public class HealthDataResolverExample {
      
           // The state of connection
           private HealthDataStore mStore;
      
           public static final String APP_TAG = "MyApp";
      
           private void getAverageTemperatureByTag(long startTime, long endTime) {
               HealthDataResolver.AggregateRequest request = new HealthDataResolver.AggregateRequest.Builder()
                       .setDataType(HealthConstants.BodyTemperature.HEALTH_DATA_TYPE)
                       .addFunction(HealthDataResolver.AggregateRequest.AggregateFunction.AVG,
                               HealthConstants.BodyTemperature.TEMPERATURE, "avr_temperature")
                       .addGroup(HealthConstants.BodyTemperature.COMMENT, "tag")
                       .setLocalTimeRange(HealthConstants.BodyTemperature.START_TIME, HealthConstants.BodyTemperature.TIME_OFFSET,
                              startTime, endTime)
                       .build();

    The aggregate request can be sent with a listener asynchronously.

               HealthDataResolver resolver = new HealthDataResolver(mStore, null);
      
               try {
                   resolver.aggregate(request).setResultListener(mTempAggrResult);
               } catch (Exception e) {
                   Log.d(APP_TAG, "Aggregating health data fails.");
               }
           }

    The result includes following values:
    • grouped with COMMENT as tag alias
    • average temperature value for each tag as avr_temperature alias

           private final HealthResultHolder.ResultListener<HealthDataResolver.AggregateResult> mTempAggrResult=
                   new HealthResultHolder.ResultListener<HealthDataResolver.AggregateResult>() {
      
               @Override
               public void onResult(HealthDataResolver.AggregateResult result) {
      
                   try {
                       Iterator<HealthData> iterator = result.iterator();
      
                       if (iterator.hasNext()) {
                          HealthData data = iterator.next();
      
                           // Gets the tag name
                           String tag = data.getString("tag");
      
                           // Gets the average temperature
                           float temperature = data.getFloat("avr_temperature");
      
                           // ...
                       }
                   } finally {
                       result.close();
                   }
               }
           };
       }

    Example: Aggregate Function and Time Group

    You can set an AggregateRequest object with:

    • setDataType()
    • addFunction()
    • setTimeGroup()
    • setSort()

    If you want to get the daily glucose average value, create an AggregateRequest object as the following example.

       public class HealthDataResolverExample {
      
           // The state of connection
           private HealthDataStore mStore;
      
           public static final String APP_TAG = "MyApp";
      
           private void getDailyGlucose(long startTime, long endTime) {
               HealthDataResolver.AggregateRequest request = new HealthDataResolver.AggregateRequest.Builder()
                       .setDataType(HealthConstants.BloodGlucose.HEALTH_DATA_TYPE)
                       .addFunction(HealthDataResolver.AggregateRequest.AggregateFunction.AVG,
                               HealthConstants.BloodGlucose.GLUCOSE, "average")
                       .setTimeGroup(HealthDataResolver.AggregateRequest.TimeGroupUnit.DAILY,
                               1, HealthConstants.BloodGlucose.START_TIME,
                               HealthConstants.BloodGlucose.TIME_OFFSET, "day")
                       .setLocalTimeRange(HealthConstants.BloodGlucose.START_TIME, HealthConstants.BloodGlucose.TIME_OFFSET,
                              startTime, endTime)
                       .setSort("days", HealthDataResolver.SortOrder.DESC)
                       .build();

    The aggregate request can be sent with a listener asynchronously.

               HealthDataResolver resolver = new HealthDataResolver(mStore, null);
      
               try {
                   resolver.aggregate(request).setResultListener(mGlucoseAggrResult);
               } catch (Exception e) {
                   Log.d(APP_TAG, "Aggregating health data fails.");
               }
           }

    The result includes following values:
    • grouped with DAILY time unit as day alias
    • average blood glucose value for each day as average alias
    • sorted with DESC

           private final HealthResultHolder.ResultListener<HealthDataResolver.AggregateResult> mGlucoseAggrResult =
                   new HealthResultHolder.ResultListener<HealthDataResolver.AggregateResult>() {
      
               @Override
               public void onResult(HealthDataResolver.AggregateResult result) {
      
                   try {
                       Iterator<HealthData> iterator = result.iterator();
      
                       if (iterator.hasNext()) {
                           HealthData data = iterator.next();
                           // Gets day information
                           String tag = data.getString("day");
                           // Gets the average glucose
                           float avg = data.getFloat("average");
                           // ...
                       }
                   } finally {
                       result.close();
                   }
               };
           }

    Since:
    1.0.0