Google Fit for Android: History API

Google Fit for Android: History API

Google Fit is a platform that allows developers to build applications that are focused on user fitness data. One of the tools Google has provided is Google Fit for Android, which is available as a package in Google Play Services.

In a previous tutorial, we explored how to use the Google Fit Recording API to store fitness data through Google Play Services. This tutorial expands on the topic by exploring how to access and update data stored in Google Fit using the History API.

What makes Google Fit powerful is that fitness data is stored through Google Cloud Services, which allows it to be accessed and analyzed at a later time. While the Sensors and Recording APIs are focused on gathering fitness data from an Android device, the History API is intended to give developers easy access to that stored data.

This tutorial walks you through a sample project that demonstrates how to work with the History API. The finished product can be downloaded from GitHub.

1. Project Setup

Step 1: Set Up the Developer Console

To use Google Fit, you need to create an OAuth 2.0 client ID and register your application through the Google Developer Console. You can find a detailed explanation of how to set up a project in the Google Developer Console in my tutorial about the Google Fit Sensors API.

Step 2: Create the Android Project

Once you have your application configured for authentication in the Google Developer Console, use the package name you registered to create a new Android application with a minimum SDK of 9 and an empty Activity.

With the base Android app created, open build.gradle and include Google Play Services under the dependencies node and sync your app.

compile 'com.google.android.gms:play-services-fitness:8.4.0'

You should now be able to include the necessary Google Play Services classes into your application. Before we dive into the Java code for this tutorial, open activity_main.xml and modify it so that it includes five Button items that we will use to demonstrate some of the functionality of the History API.

With the above code, we connect to Google Play Services and request access to the History API in Google Fit, as well as permission to read and write activity data. By adding the enableAutoManage property, Google Play Services manages connecting and disconnecting properly.

2. Using the Google Fit History API

Using the History API, you can retrieve and aggregate data, insert values that you have gathered in your app or delete data that may be stored. One important thing to note is that all of the History API operations in this tutorial must be done off of the main thread. In other words, each operation takes place in an AsyncTask:

Step 1: Displaying Data Over Time

One task that you may want to do is use or display data that has been collected over time. You can do this by creating a new DataReadRequest for a specific type of data that fits some query parameters.

You can group this data together in Bucket objects by time or sessions. For this example, you request aggregated step data for the last week and bucket that data by day.

Once you have your DataReadRequest created, you can request data from the GoogleApiClient using the Fitness.HistoryApi.readData command. You also call await on the API client so that the thread stalls for up to a minute while waiting for the query. If something goes wrong, the thread continues after that minute.

If the call returns successfully, you are able to access the user's fitness data from the DataReadResult object. Your step data request is returned in Bucket objects, though other types of data are returned as a List of DataSet objects. Depending on the type of data returned, you can access it with the following code:

Step 2: Displaying Data for Today

You now know how to retrieve Google Fit data based on a timeframe. One common use case developers have is needing data for the current day. Luckily, Google has made this easy by adding the Fitness.HistoryApi.readDailyTotal call, which creates a DailyTotalResult object containing data collected for the day.

Step 3: Inserting Data Into Google Fit

While being able to read data from Google Fit is important, there may be times where you need to add your own collected data to the Google Fit data store. To do this, you select a time range for the data and create a DataSource object that represents the kind of information that you put into Google Fit. Next, you create a DataSet object and place a new DataPoint into it for the insert operation.

For this example, we are going to assume the user has walked one million steps, or roughly 500 miles, and insert that raw step data into the last hour of the user's activity.

It is important to note that if you select a time range that spans multiple days, the number of steps that you insert is evenly distributed among those days.

Step 4: Update Data on Google Fit

Once you have inserted data into Google Fit, you are unable to insert any more data of that type if it overlaps with existing data. This could cause a problem if the user had walked 500 miles and then walked 500 more.

To solve this, you need to use the updateData call. Luckily, updating data is almost identical to inserting new data, except that you create a DataUpdateRequest instead.

Step 5: Deleting Data From Google Fit

The final operation available through the Google Fit History API is the ability to delete data that you have stored in Google Fit. This can be done by selecting a date range, creating a DataDeleteRequest object, and calling deleteData from the History API. While you can delete your own inserted data, data stored by Google Fit is not removed.

Conclusion

As you have seen, the History API is an incredibly powerful tool for accessing and manipulating the Google Fit data store. While other Google Fit APIs are intended to gather data, the History API allows you to maintain and access data for analysis. You should now be able to create amazing apps that take advantage of this feature.