Google Fit Health Api - 健身:数据点乱序

时间:2017-08-04 17:47:58

标签: android google-fit google-fit-sdk

我在我的应用程序中做了一些阅读,我发现它的工作速度非常慢,并且日志中包含了大量这些消息:

08-04 20:33:33.691 27900-18653/? W/Fitness: Trimming data point [13:00:00-13:39:00] outputted with range [13:38:00-13:39:00]
                                            Point: OutputDataPoint{[# axie@5df14cc8]@[2017-08-04 13:00:00 - 2017-08-04 13:39:00,raw=0,insert=0](derived:com.google.calories.expended:com.google.android.gms:default_calories_expended derived:com.google.calories.expended:com.google.android.gms:from_activities_local)}
08-04 20:33:33.693 27900-18653/? W/Fitness: Data point out of order: qjf{DataPoint{109.8@[1501480774279000000, 1501480774279000000,raw=0,insert=0](d:weight:gms::merge_weight_local r:weight:com.google.android.apps.fitness::user_input)}}
08-04 20:33:33.694 27900-18653/? W/Fitness: Data point out of order: qjf{DataPoint{109.8@[1501480774279000000, 1501480774279000000,raw=0,insert=0](d:weight:gms::merge_weight_local r:weight:com.google.android.apps.fitness::user_input)}}

这些有问题吗?如果是这样,我该如何避免这些问题或缓解/解决问题?

它发生在代码的这一部分:

Fitness.HistoryApi.readData(mGoogleClient, queryStepCount(startTime, endTime))
        .setResultCallback(new ResultCallback<DataReadResult>() {
            @Override
            public void onResult(@NonNull final DataReadResult stepsDataReadResult) {
                Fitness.HistoryApi.readData(mGoogleClient, queryDistance(startTime, endTime))
                        .setResultCallback(new ResultCallback<DataReadResult>() {
                            @Override
                            public void onResult(final @NonNull DataReadResult distanceDataReadResult) {
                                Fitness.HistoryApi.readData(mGoogleClient, queryCalories(startTime, endTime))
                                        .setResultCallback(new ResultCallback<DataReadResult>() {
                                            @Override
                                            public void onResult(@NonNull DataReadResult caloriesDataReadResult) {
                                                createOrDeletePointGoogleFit(context,
                                                        generateKey(startTime, endTime),
                                                        startTime,
                                                        endTime,
                                                        countStepData(stepsDataReadResult),
                                                        calculateDistanceData(distanceDataReadResult),
                                                        calculateCaloriesData(caloriesDataReadResult),
                                                        googleFitActivity);
                                            }
                                        });
                            }
                        });
            }
        });

以下是使用的数据查询:

static DataReadRequest queryStepCount(long startTime, long endTime) {
    return new DataReadRequest.Builder()
            .aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA)
            .bucketByTime(1, TimeUnit.MINUTES)
            .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
            .build();
}

static DataReadRequest queryDistance(long startTime, long endTime) {
    return new DataReadRequest.Builder()
            .aggregate(DataType.TYPE_DISTANCE_DELTA, DataType.AGGREGATE_DISTANCE_DELTA)
            .bucketByTime(1, TimeUnit.MINUTES)
            .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
            .build();
}

static DataReadRequest queryCalories(long startTime, long endTime) {
    return new DataReadRequest.Builder()
            .aggregate(DataType.TYPE_CALORIES_EXPENDED, DataType.AGGREGATE_CALORIES_EXPENDED)
            .bucketByTime(1, TimeUnit.MINUTES)
            .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
            .build();
}

方法countStepDatacalculateDistanceData

final class DataParser {

    private static final String TAG = DataParser.class.getSimpleName();

    private static final String DATA_PACKAGE_STEPS = "com.google.step_count.delta";
    private static final String DATA_PACKAGE_DISTANCE = "com.google.distance.delta";
    private static final String DATA_PACKAGE_CALORIES = "com.google.calories.expended";

    private DataParser() {
        throw new AssertionError("Class not designed for instantiation");
    }

    static int countStepData(DataReadResult dataReadResult) {
        int stepCount = 0;
        for (Bucket bucket : dataReadResult.getBuckets()) {
            for (DataSet dataSet : bucket.getDataSets()) {
                stepCount += parseDataSetForSteps(dataSet);
            }
        }
        return stepCount;
    }

    static float calculateDistanceData(DataReadResult dataReadResult) {
        float distance = 0;
        for (Bucket bucket : dataReadResult.getBuckets()) {
            for (DataSet dataSet : bucket.getDataSets()) {
                distance += parseDataSetForDistance(dataSet);
            }
        }
        return distance;
    }

    static float calculateCaloriesData(DataReadResult dataReadResult) {
        float distance = 0;
        for (Bucket bucket : dataReadResult.getBuckets()) {
            for (DataSet dataSet : bucket.getDataSets()) {
                distance += parseDataSetForCalories(dataSet);
            }
        }
        return distance;
    }

    private static int parseDataSetForSteps(DataSet dataSet) {
        return parseIntData(dataSet, DATA_PACKAGE_STEPS);
    }

    private static int parseIntData(DataSet dataSet, String dataPackage) {
        int data = 0;
        for (DataPoint dataPoint : dataSet.getDataPoints()) {
            if (dataPoint.getDataType().getName().equals(dataPackage)) {
                for (Field field : dataPoint.getDataType().getFields()) {
                    if (dataPoint.getValue(field).asInt() > 0) {
                        data += dataPoint.getValue(field).asInt();
                    }
                }
            }
        }
        return data;
    }

    private static float parseDataSetForDistance(DataSet dataSet) {
        return parseFloatData(dataSet, DATA_PACKAGE_DISTANCE);
    }

    private static float parseDataSetForCalories(DataSet dataSet) {
        return parseFloatData(dataSet, DATA_PACKAGE_CALORIES);
    }

    private static float parseFloatData(DataSet dataSet, String dataPackage) {
        float data = 0;
        for (DataPoint dataPoint : dataSet.getDataPoints()) {
            if (dataPoint.getDataType().getName().equals(dataPackage)) {
                for (Field field : dataPoint.getDataType().getFields()) {
                    if (dataPoint.getValue(field).asFloat() > 0) {
                        data += dataPoint.getValue(field).asFloat();
                    }
                }
            }
        }
        return data;
    }
}

我传递给所有这些的数据都是这样获得的:

    DataReadRequest readRequest = new DataReadRequest.Builder()
            .read(DataType.TYPE_ACTIVITY_SEGMENT)
            .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
            .build();
    Fitness.HistoryApi.readData(mGoogleClient, readRequest).setResultCallback(new ResultCallback<DataReadResult>() {

还有:

DataReadRequest readRequest = new DataReadRequest.Builder()
                            .aggregate(DataType.TYPE_ACTIVITY_SEGMENT, DataType.AGGREGATE_ACTIVITY_SUMMARY)
                            .bucketByTime(1, TimeUnit.MINUTES)
                            .setTimeRange(startTime, endTimes.get(i), TimeUnit.MILLISECONDS)
                            .build();
                    Fitness.HistoryApi.readData(mGoogleClient, readRequest).setResultCallback(new ResultCallback<DataReadResult>() {
                        @Override
                        public void onResult(DataReadResult dataReadResult) {

0 个答案:

没有答案