Search code examples
androidgoogle-fit

Retrieve recent walks from the Google Fit Android API


Currently I'm trying to develop a native Android application that retrieves information from the History_API. I'm already able to retrieve calories, daily steps and active time. Now I was thinking about taking it to the next level and try to view different type of activities retrieved from the history api by using the code below:

    protected Void doInBackground(Void... params) {
        DataReadRequest readRequest = queryFitnessData();

        DataReadResult dataReadResult =
                Fitness.HistoryApi.readData(mGoogleApiClient, readRequest).await(1, TimeUnit.MINUTES);

        getData(dataReadResult);
        return null;
   }

This is the function that defines the request:

    private DataReadRequest queryFitnessData() {
        Calendar cal = Calendar.getInstance();
        Date now = new Date();
        cal.setTime(now);
        long endTime = cal.getTimeInMillis();
        cal.add(Calendar.WEEK_OF_MONTH, -1);
        long startTime = cal.getTimeInMillis();

        DataSource ACTIVITY_SEGMENT = new DataSource.Builder()
                .setDataType(DataType.TYPE_ACTIVITY_SEGMENT)
                .setType(DataSource.TYPE_DERIVED)
                .build();

        DataReadRequest readRequest = new DataReadRequest.Builder()
                .aggregate(ACTIVITY_SEGMENT, DataType.AGGREGATE_ACTIVITY_SUMMARY)
                .bucketByTime(5, TimeUnit.MINUTES)
                .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
                .build();

        return readRequest;
    }

And this seemed to work fine in the afternoon, but when working on it further just now, it isn't displaying the views any longer. Looking in the log files showed that the buckets returned all where of the type 'in_vehicle'. Which makes sense why the code below doesn't add the views to my array.

    private void getData(DataReadResult result) {
        if (result.getBuckets().size() > 0) {
            Log.i(TAG, "Bucket size: " + result.getBuckets().size());
            for(Bucket bucket : result.getBuckets()) {
                long activeTime = bucket.getEndTime(TimeUnit.MINUTES) - bucket.getStartTime(TimeUnit.MINUTES);

                Log.i(TAG, bucket.getActivity());

                switch (bucket.getActivity()) {
                    case "walking":
                        Log.i(TAG, "Adding a walking card");
                        activityData.add(new CustomActivityCard("Walking","Place, 27 juli",Long.toString(activeTime) + " minutes","5 kilometers","2340 steps","1340 calories"));
                        break;
                    case "running":
                        Log.i(TAG, "Adding a running card");
                        activityData.add(new CustomActivityCard("Running","Place, 27 juli",Long.toString(activeTime) + " minutes","5 kilometers","2340 steps","1340 calories"));
                        break;
                    case "biking":
                        Log.i(TAG, "Adding a cycling card");
                        activityData.add(new CustomActivityCard("Cycling","Place, 27 juli",Long.toString(activeTime) + " minutes","5 kilometers","2340 steps","1340 calories"));
                        break;
                }
            }
        }
    }

I can't understand why the code was displaying walking and running activities this afternoon, and suddenly only returns activities that are while I was driving. One possible solution I can think of is that my app isn't subscribed to any of the datatypes. So my questions are:

  1. Does my app need to be subscribed to be able to retrieve activities (e.g. walking)?
  2. How should I retrieve all the activities performed on a day or within a week?

Solution

  • I'm not sure yet if the subscriptions suggested in the comments made any difference, but one thing that definitely helped in order to get to the solution was by changing the code piece below:

    DataReadRequest readRequest = new DataReadRequest.Builder()
        .aggregate(ACTIVITY_SEGMENT, DataType.AGGREGATE_ACTIVITY_SUMMARY)
        .bucketByTime(5, TimeUnit.MINUTES)
        .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
        .build();
    

    To something like the DataRequest below, this made me able to retrieve the walking activities of the current day

    DataReadRequest readRequest = new DataReadRequest.Builder()
        .aggregate(DataType.TYPE_ACTIVITY_SEGMENT, DataType.AGGREGATE_ACTIVITY_SUMMARY)
        .bucketByActivitySegment(7, TimeUnit.MINUTES)
        .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
        .build();
    

    Hope this will help others as well!