Skip to content

Instantly share code, notes, and snippets.

@ColeMurray
Created March 23, 2015 22:48
Show Gist options
  • Save ColeMurray/c5a98fcce8c051ef35b6 to your computer and use it in GitHub Desktop.
Save ColeMurray/c5a98fcce8c051ef35b6 to your computer and use it in GitHub Desktop.
Google Fit Cumulative History
DataReadResult step_result = getStepData(mFitClient,buildStepRequest(date));
public static DataReadResult getStepData (GoogleApiClient mClient, DataReadRequest request){
PendingResult <DataReadResult> pendingResult =
Fitness.HistoryApi.readData(mClient,request);
DataReadResult dataReadResult = pendingResult.await();
printStepResult(dataReadResult);
return dataReadResult;
}
private static void printStepResult (DataReadResult dataReadResult){
int stepCount = 0;
DataSet dataSet = dataReadResult.getDataSet(DataType.TYPE_STEP_COUNT_CUMULATIVE);
for (DataPoint dp : dataSet.getDataPoints()) {
Log.d(TAG, "Data Returned of type:" + dp.getDataType().getName());
Log.d(TAG, "Data Point:");
Log.i(TAG, "\tType: " + dp.getDataType().getName());
Log.i(TAG, "\tStart: " + CalendarUtil.dateFormat.format(dp.getStartTime(TimeUnit.MILLISECONDS)));
Log.i(TAG, "\tEnd: " + CalendarUtil.dateFormat.format(dp.getEndTime(TimeUnit.MILLISECONDS)));
for (Field field : dp.getDataType().getFields()) {
Log.i(TAG, "\tField: " + field.getName() +
" Value: " + dp.getValue(field));
stepCount += dp.getValue(field).asInt();
}
}
Log.d(TAG, "StepCount: " + stepCount);
}
public static DataReadRequest buildStepRequest(Date date){
Calendar cal = Calendar.getInstance();
cal.setTime(date);
Long[] startEndTimes = CalendarUtil.getStartandEndTime(cal);
DataReadRequest dataReadRequest = new DataReadRequest.Builder()
.read(DataType.TYPE_STEP_COUNT_CUMULATIVE)
.setTimeRange(startEndTimes[0],startEndTimes[1], TimeUnit.MILLISECONDS)
.build();
return dataReadRequest;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment