Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 551
Nikola Anusev Avatar asked Nov 12 '14 19:11

Nikola Anusev


People also ask

Where is Google Fit data stored?

The Google Fit service processes and stores information collected from all supported devices where it's installed in the user's Google Account. While many users associate Google Fit with WearOS smartwatches, in reality the app does not require a smartwatch or a fitness tracker.

Can you import data to Google Fit?

But all the apps pair the same way, which makes it easy to get all your exercise data synced over to Google Fit. Open the MapMy app you want to pair. Scroll down and find “Google Fit.” Toggle it on and confirm permissions.


1 Answers

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

like image 76
stefan222 Avatar answered Oct 06 '22 08:10

stefan222