com.samsung.android.sdk.healthdata

Class HealthDataResolver



  • public class HealthDataResolver
    extends Object
    This class is able to access health data to insert, read, update, and delete with the specified filter and some aggregate functions.

    Data Management

    Health data can be accessed with following interfaces mainly.

    Filter

    Handling a part of data is a common use case rather than accessing whole data for the health data type. The Health data framework provides a filter to specify the precise scope for health data and is able to access required data only.

    Asynchronous Data Request

    The asynchronous request is used normally for operations that take a time such as reading data with conditions.
    If you need to request asynchronously, set the result listener with HealthResultHolder.setResultListener(HealthResultHolder.ResultListener).

    The following example shows how to make an asynchronous request.

       public class HealthDataResolverExample {
      
           // The state of connection
           HealthDataStore mStore;
      
           public static final String APP_TAG = "MyApp";
      
           public void readGlucoseAsynchronously(long startTime, long endTime) {
      
               HealthDataResolver resolver = new HealthDataResolver(mStore, null);
      
               HealthDataResolver.ReadRequest request = new HealthDataResolver.ReadRequest.Builder()
                   .setDataType(HealthConstants.BloodGlucose.HEALTH_DATA_TYPE)
                   .setLocalTimeRange(HealthConstants.BloodGlucose.START_TIME, HealthConstants.BloodGlucose.TIME_OFFSET,
                                  startTime, endTime)
                   .build();
      
               try {
                   resolver.read(request).setResultListener(mRdResult);
               } catch (Exception e) {
                   Log.d(APP_TAG, "Reading health data fails.");
               }
           }
      
           private final HealthResultHolder.ResultListener<HealthDataResolver.ReadResult> mRdResult =
               new HealthResultHolder.ResultListener<HealthDataResolver.ReadResult>(){
      
               @Override
               public void onResult(HealthDataResolver.ReadResult result) {
      
                   try {
                       Iterator<HealthData> iterator = result.iterator();
      
                       if (iterator.hasNext()) {
                          HealthData data = iterator.next();
                          float glucoseValue = data.getFloat(HealthConstants.BloodGlucose.GLUCOSE);
                   } finally {
                       result.close();
                   }
           };
       }

    Synchronous DataRequest

    Though asynchronous data request is used commonly, there is a way to get the query result synchronously with HealthResultHolder.await().

    Note, not to make the synchronous request on the UI thread. HealthResultHolder.await() throws an exception if it is called on the main thread. It can hang your application caused by taking a time to handle the synchronous query.

    The following example shows how to make a synchronous request.

       public class HealthDataResolverExample {
      
           // The state of connection
           HealthDataStore mStore;
      
           public static final String APP_TAG = "MyApp";
      
           public void readGlucoseSynchronously(long startTime, long endTime) {
               HealthDataResolver resolver = new HealthDataResolver(mStore, null);
      
               HealthDataResolver.ReadRequest request = new HealthDataResolver.ReadRequest.Builder()
                   .setDataType(HealthConstants.BloodGlucose.HEALTH_DATA_TYPE)
                   .setLocalTimeRange(HealthConstants.BloodGlucose.START_TIME, HealthConstants.BloodGlucose.TIME_OFFSET,
                                  startTime, endTime)
                   .build();
      
               try {
                   // Checks the result immediately
                   HealthDataResolver.ReadResult rdResult = resolver.read(request).await();
      
                   try {
                       Iterator<HealthData> iterator = rdResult.iterator();
                       if (iterator.hasNext()) {
                           HealthData data = iterator.next();
                           float glucoseValue = data.getFloat(HealthConstants.BloodGlucose.GLUCOSE);
                       }
                   finally {
                       rdResult.close();
                   }
               } catch (Exception e) {
                   Log.d(APP_TAG, "Reading health data fails.");
               }
           }
       }

    Acquiring Data Permission

    Getting data permission is required after connection to the health data store to synchronize health data with Samsung Health. Samsung Health SDK for Android provides the data permission UI to get the user's consent. The user can agree to share health data with the application through the permission UI and the application can get required data permission Check HealthPermissionManager to use the permission UI.

    If you try to read or write health data without permission, the SDK gives SecurityException.

    Acquiring Instant Data Permission

    Gaining the HealthConstants.HealthDocument type's permission is different with other data types. It is available by acquiring the instant permission.
    Instant permission is created for one-time data access. It is proper to handle very sensitive health data. The data type needs the instant permission is HealthConstants.HealthDocument.
    An app needs the instant permission whenever it accesses health document data.

    If you call HealthDataResolver.readWithPermission(), its permission pop-up is shown.
    The user can select one or more health documents in the list and read the selected document.

    Class Relationship of HealthDataResolver

    See a relationship of HealthDataResolver with other classes.

    Since:
    1.0.0