How to properly retrieve package name of the app that inserted data to Google Fit?

1.2k views Asked by At

I have a following code I am using to retrieve a list of user's activities from Google Fit:

public void getActivitiesData(Date from, Date till) {
    DataReadRequest readRequest = new DataReadRequest.Builder()
        .aggregate(DataType.TYPE_ACTIVITY_SEGMENT, DataType.AGGREGATE_ACTIVITY_SUMMARY)
        .bucketByTime(1, TimeUnit.DAYS)
        .setTimeRange(from.getTime(), till.getTime(), TimeUnit.MILLISECONDS)
        .build();

    Fitness.HistoryApi.readData(apiClient, readRequest).setResultCallback(new com.google.android.gms.common.api.ResultCallback<DataReadResult>() {
        @Override
        public void onResult(DataReadResult dataReadResult) {
            Status status = dataReadResult.getStatus();
            if (status.isSuccess()) {

                for (Bucket bucket : dataReadResult.getBuckets()) {
                    if (!bucket.getDataSets().isEmpty()) {
                        DataSet dataSet = bucket.getDataSets().get(0);
                        String sourceAppPackageName = getSourceAppPackageNameFromDataSet(dataSet);
                        for (DataPoint dp : dataSet.getDataPoints()) {
                            for (Field field : dp.getDataType().getFields()) {
                                String fieldName = field.getName();
                                if (fieldName != null && fieldName.equals("activity")) {
                                    String type = FitnessActivities.getValue(dp);
                                    Date from = new Date(dp.getStartTime(TimeUnit.MILLISECONDS));
                                    Date till = new Date(dp.getEndTime(TimeUnit.MILLISECONDS));

                                    // store retrieved values to the data object, omitted
                                }
                            }
                        }
                    }
                }
            }
        }
    });
}

private static String getSourceAppPackageNameFromDataSet(DataSet dataSet) {
    String result = null;

    if (dataSet.getDataSource() != null) {
        result = dataSet.getDataSource().getAppPackageName();
    }

    return result;
}

To insert activities into Google Fit, I've used the Google Fit app and Runkeeper (right now, these apps seem to be only ones that are integrated with Fit).

My code retrieves these activities as expected, however, for each activity, my getSourceAppPackageNameFromDataSet() method returns "com.google.android.gms" as a package name. As per Data Attribution section in Google Fit documentation, I would expect the method to return a package name of either Runkeeper or Google Fit, but this does not happen.

Am I doing something horribly wrong, or is this a bug in Google Fit?

1

There are 1 answers

0
stefan222 On BEST ANSWER

DataPoint.getOriginalDataSource().getAppPackageName() will do the trick. It returns com.withings.wiscale2 for my Withings scale, while DataSet.getDataSource().getAppPackageName()always returns com.google.android.gms.

There's a similar question right here: DataSource.getAppPackageName() always returns "com.google.android.gms" in Google Fit