GoalsClient

public class GoalsClient extends GoogleApi<FitnessOptions>

Client for reading fitness Goals created by users in Google Fit.

The readCurrentGoals(GoalsReadRequest) method should be used whenever goals are needed.

The Goals Client should be accessed via the Fitness entry point. Example:


    GoogleSignInOptionsExtension fitnessOptions =
      FitnessOptions.builder()
          .addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
          .addDataType(DataType.TYPE_DISTANCE_DELTA, FitnessOptions.ACCESS_READ)
          .build();

    GoogleSignInAccount gsa = GoogleSignIn.getAccountForExtension(this, fitnessOptions);

    Task<List<Goal>> response = Fitness.getGoalsClient(this, gsa)
        .readCurrentGoals(new GoalsReadRequest.Builder()
            .addDataType(DataType.TYPE_STEP_COUNT_DELTA)
            .addDataType(DataType.TYPE_DISTANCE_DELTA)
            .build());

    List<Goal> goals = Tasks.await(response);
 

This Client can be combined with a subscription in the Recording Client to collect goal progress data in the background and query it later for displaying. A simple progress query example for a step metric goal:

    Calendar current = Calendar.getInstance();
    Task<DataReadResponse> response = Fitness.getHistoryClient(this, googleSigninAccount)
        .readData(new DataReadRequest.Builder()
            .read(DataType.TYPE_STEP_COUNT_DELTA)
            .setTimeRange(
                goal.getStartTime(current, TimeUnit.NANOSECONDS),
                goal.getEndTime(current, TimeUnit.NANOSECONDS),
                TimeUnit.NANOSECONDS)
            .build());
    DataReadResponse stepReadResponse = Tasks.await(response);
    List<DataPoint> dataPoints =
        stepReadResponse.getDataSet(DataType.TYPE_STEP_COUNT_DELTA).getDataPoints();

    int total = 0;
    for (DataPoint dataPoint : dataPoints) {
      total += dataPoint.getValues()[0].asInt();
    }
    double progress = total / goal.getMetricObjective().getValue();
 

Inherited Field Summary

Public Method Summary

Task<List<Goal>>
readCurrentGoals(GoalsReadRequest request)
Reads current goals from the user’s Google Fit store.

Inherited Method Summary

Public Methods

public Task<List<Goal>> readCurrentGoals (GoalsReadRequest request)

Reads current goals from the user’s Google Fit store.

Returns
  • Task containing current goals.