@Override
        public void onDataReceived(DataReadResult dataReadResult) {
          Log.d(TAG, "Received step result");

          final String DATE_FORMAT = "dd/MM";
          SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
          List<Column> columns = new ArrayList<>();
          List<SubcolumnValue> values;
          List<AxisValue> xAxisValues = new ArrayList<>();

          if (dataReadResult.getBuckets().size() > 0) {
            fitnessFragment.mColumnChart.clearAnimation();
            for (Bucket bucket : dataReadResult.getBuckets()) { // days/hours, depends on request
              DataSet dataset = bucket.getDataSet(DataType.TYPE_STEP_COUNT_DELTA);
              List<DataPoint> dataPointList = dataset.getDataPoints();
              for (DataPoint dp : dataPointList) {
                // add to bar chart
                String timestamp = dateFormat.format(dp.getTimestamp(TimeUnit.MILLISECONDS));
                float steps = (float) dp.getValue(Field.FIELD_STEPS).asInt();

                // Set axis labels
                xAxisValues.add(new AxisValue(columns.size()).setLabel(timestamp));

                values = new ArrayList<>();
                values.add(new SubcolumnValue(steps, ChartUtils.COLOR_RED));
                columns.add(new Column(values));
              }
            }
            fitnessFragment.setStepsData(xAxisValues, columns);
          }
        }
        @Override
        public void onDataReceived(DataReadResult dataReadResult) {
          Log.d(TAG, "Received activity result");
          final String DATE_FORMAT = "dd/MM";
          SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);

          List<Line> lines = new ArrayList<>(); // all lines
          List<PointValue> calories; // a single line
          calories = new ArrayList<>();
          int x_val = 0;
          List<AxisValue> xAxisValues = new ArrayList<>();

          if (dataReadResult.getBuckets().size() > 0) {
            for (Bucket bucket : dataReadResult.getBuckets()) {
              String date = dateFormat.format(bucket.getStartTime(TimeUnit.MILLISECONDS));
              DataSet dataSet = bucket.getDataSet(DataType.AGGREGATE_CALORIES_EXPENDED);
              Log.d(TAG, "New databucket with date: " + date);

              // Increase counter
              x_val = x_val + 1;

              // Set axis labels
              xAxisValues.add(new AxisValue(x_val).setLabel(date));

              // Add a zero measurement point
              calories.add(new PointValue(x_val, 0));

              List<DataPoint> dataPointList = dataSet.getDataPoints();
              for (DataPoint dp : dataPointList) {
                String timestamp = dateFormat.format(dp.getTimestamp(TimeUnit.MILLISECONDS));
                float calorie = dp.getValue(Field.FIELD_CALORIES).asFloat();

                // Set graph values
                calories.set(x_val - 1, new PointValue(x_val, calorie));
                Log.d(TAG, "timestamp: " + timestamp + ", calories: " + calorie);
              }
            }
            Line line = new Line(calories);
            line.setColor(ChartUtils.COLOR_BLUE);
            line.setHasLines(true);
            line.setHasPoints(false);
            line.setCubic(false);
            line.setHasLabels(false);
            line.setHasLabelsOnlyForSelected(true);
            lines.add(line);

            fitnessFragment.setCalorieData(xAxisValues, lines);
          }
        }
        @Override
        public void onDataReceived(DataReadResult dataReadResult) {
          // GoogleApiSubscription.printData(dataReadResult);

          Log.d(TAG, "Received generic result");
          if (dataReadResult.getDataSets().size() > 0) {
            // Go through the requested data types
            Bundle bundle = formatActivity(dataReadResult);
          }
        }
Exemple #4
0
  // Una vez obtenidos los datos los subimos a parse
  private void uploadData(DataReadResult dataReadResult) {

    // [START parse_read_data_result]
    // If the DataReadRequest object specified aggregated data, dataReadResult will be returned
    // as buckets containing DataSets, instead of just DataSets.
    if (dataReadResult.getBuckets().size() > 0) {
      for (Bucket bucket : dataReadResult.getBuckets()) {
        List<DataSet> dataSets = bucket.getDataSets();
        for (DataSet dataSet : dataSets) {
          dumpDataSet(dataSet);
        }
      }
    } else if (dataReadResult.getDataSets().size() > 0) {
      for (DataSet dataSet : dataReadResult.getDataSets()) {
        dumpDataSet(dataSet);
      }
    }
    // [END parse_read_data_result]

  }
  @Override
  public void execute(Context ctx, Map<String, Value> context)
      throws UnknownObjectException, RuleExecutionException {
    GoogleApiClient client = ((GoogleFitChannel) channel).acquireClient(ctx);

    try {
      long now = System.currentTimeMillis();
      DataReadRequest.Builder builder =
          new DataReadRequest.Builder()
              .read(dataType.getType())
              .setTimeRange(now - 24 * 3600 * 1000, now, TimeUnit.MILLISECONDS);

      DataReadResult result = Fitness.HistoryApi.readData(client, builder.build()).await();

      DataSet dataSet = result.getDataSet(dataType.getType());
      List<DataPoint> dataPoints = dataSet.getDataPoints();

      DataPoint latest = null;
      for (DataPoint point : dataPoints) {
        if (latest == null || point.getTimestampNanos() >= latest.getTimestampNanos())
          latest = point;
      }
      if (latest == null) throw new RuleExecutionException("Google API did not return any data");

      com.google.android.gms.fitness.data.Value value = latest.getValue(dataType.getField());

      String dataTypeId = dataType.toString();

      if (value.getFormat() == Field.FORMAT_INT32)
        context.put(FITNESS_CURRENT_VALUE_PREFIX + dataTypeId, new Value.Number(value.asInt()));
      else if (value.getFormat() == Field.FORMAT_FLOAT)
        context.put(FITNESS_CURRENT_VALUE_PREFIX + dataTypeId, new Value.Number(value.asFloat()));
      else throw new RuleExecutionException("Google Fit data point has invalid type");
    } finally {
      ((GoogleFitChannel) channel).releaseClient();
    }
  }
  private Bundle formatDistance(DataReadResult dataReadResult) {
    Bundle bundle = new Bundle();

    // Distance
    List<DataPoint> dataPointList =
        dataReadResult.getDataSet(DataType.TYPE_DISTANCE_DELTA).getDataPoints();
    float[] distance = new float[dataPointList.size()];
    long[] startTime = new long[dataPointList.size()];
    long[] endTime = new long[dataPointList.size()];
    int idx = 0;
    for (DataPoint dp : dataPointList) {
      distance[idx] = dp.getValue(Field.FIELD_DISTANCE).asFloat();
      startTime[idx] = dp.getStartTime(TimeUnit.MILLISECONDS);
      endTime[idx] = dp.getEndTime(TimeUnit.MILLISECONDS);
      ++idx;
    }
    bundle.putFloatArray("DISTANCE", distance);
    bundle.putLongArray("START_TIME", startTime);
    bundle.putLongArray("END_TIME", endTime);
    Log.d(TAG, String.format("Distance data points: %d", idx));

    return bundle;
  }
  private Bundle formatStepCount(DataReadResult dataReadResult) {
    Bundle bundle = new Bundle();

    // Step Count
    List<DataPoint> dataPointList =
        dataReadResult.getDataSet(DataType.TYPE_STEP_COUNT_DELTA).getDataPoints();
    int[] steps = new int[dataPointList.size()];
    long[] startTime = new long[dataPointList.size()];
    long[] endTime = new long[dataPointList.size()];
    int idx = 0;
    for (DataPoint dp : dataPointList) {
      steps[idx] = dp.getValue(Field.FIELD_STEPS).asInt();
      startTime[idx] = dp.getStartTime(TimeUnit.MILLISECONDS);
      endTime[idx] = dp.getEndTime(TimeUnit.MILLISECONDS);
      ++idx;
    }
    bundle.putIntArray("STEPS", steps);
    bundle.putLongArray("START_TIME", startTime);
    bundle.putLongArray("END_TIME", endTime);
    Log.d(TAG, String.format("Step data points: %d", idx));

    return bundle;
  }
  private Bundle formatActivity(DataReadResult dataReadResult) {
    Bundle bundle = new Bundle();

    // Activity Recognition
    List<DataPoint> dataPointList =
        dataReadResult.getDataSet(DataType.TYPE_ACTIVITY_SAMPLE).getDataPoints();
    int[] activity = new int[dataPointList.size()];
    long[] endTime = new long[dataPointList.size()];
    float[] confidence = new float[dataPointList.size()];
    int idx = 0;
    for (DataPoint dp : dataPointList) {
      activity[idx] = dp.getValue(Field.FIELD_ACTIVITY).asInt();
      confidence[idx] = dp.getValue(Field.FIELD_CONFIDENCE).asFloat();
      endTime[idx] = dp.getEndTime(TimeUnit.MILLISECONDS);
      ++idx;
    }
    bundle.putIntArray("ACTIVITY", activity);
    bundle.putFloatArray("CONFIDENCE", confidence);
    bundle.putLongArray("TIMESTAMP", endTime);
    Log.d(TAG, String.format("Activity data points: %d", idx));

    return bundle;
  }
  private Bundle formatCalories(DataReadResult dataReadResult) {
    Bundle bundle = new Bundle();

    // Calorie expenditure
    List<DataPoint> dataPointList =
        dataReadResult.getDataSet(DataType.TYPE_CALORIES_EXPENDED).getDataPoints();
    float[] calories = new float[dataPointList.size()];
    long[] startTime = new long[dataPointList.size()];
    long[] endTime = new long[dataPointList.size()];
    int idx = 0;
    for (DataPoint dp : dataPointList) {
      calories[idx] = dp.getValue(Field.FIELD_CALORIES).asFloat();
      startTime[idx] = dp.getStartTime(TimeUnit.MILLISECONDS);
      endTime[idx] = dp.getEndTime(TimeUnit.MILLISECONDS);
      ++idx;
    }
    bundle.putFloatArray("CALORIES", calories);
    bundle.putLongArray("START_TIME", startTime);
    bundle.putLongArray("END_TIME", endTime);
    Log.d(TAG, String.format("Calorie data points: %d", idx));

    return bundle;
  }
        @Override
        public void onDataReceived(DataReadResult dataReadResult) {
          Log.d(TAG, "Received activity result");
          final String DATE_FORMAT = "dd/MM";
          SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
          // GoogleApiSubscription.printData(dataReadResult);

          List<Line> lines = new ArrayList<>(); // all lines
          List<PointValue> walking = new ArrayList<>(); // a single line
          List<PointValue> biking = new ArrayList<>();
          List<PointValue> in_vehicle = new ArrayList<>();
          List<AxisValue> xAxisValues = new ArrayList<>();

          int x_val = 0;

          if (dataReadResult.getBuckets().size() > 0) {
            for (Bucket bucket : dataReadResult.getBuckets()) {
              String date = dateFormat.format(bucket.getStartTime(TimeUnit.MILLISECONDS));
              DataSet dataSet = bucket.getDataSet(DataType.AGGREGATE_ACTIVITY_SUMMARY);
              Log.d(TAG, "New databucket with date: " + date);

              // Increase counter
              x_val = x_val + 1;

              // Set axis labels
              xAxisValues.add(new AxisValue(x_val).setLabel(date));

              // Add a zero measurement point
              walking.add(new PointValue(x_val, 0));
              biking.add(new PointValue(x_val, 0));
              in_vehicle.add(new PointValue(x_val, 0));

              List<DataPoint> dataPointList = dataSet.getDataPoints();
              for (DataPoint dp : dataPointList) {
                String timestamp = dateFormat.format(dp.getTimestamp(TimeUnit.MILLISECONDS));
                String activity = dp.getValue(Field.FIELD_ACTIVITY).asActivity();
                int duration = dp.getValue(Field.FIELD_DURATION).asInt();

                if (dp.getValue(Field.FIELD_ACTIVITY).asActivity() == "walking") {
                  walking.set(x_val - 1, new PointValue(x_val, duration / 60000));
                } else if (dp.getValue(Field.FIELD_ACTIVITY).asActivity() == "biking") {
                  biking.set(x_val - 1, new PointValue(x_val, duration / 60000));
                } else if (dp.getValue(Field.FIELD_ACTIVITY).asActivity() == "in_vehicle") {
                  in_vehicle.set(x_val - 1, new PointValue(x_val, duration / 60000));
                }
                Log.d(
                    TAG,
                    "timestamp: "
                        + timestamp
                        + ", activity: "
                        + activity
                        + ", duration: "
                        + duration / 60000);
              }
            }
            Line line = new Line(walking);
            line.setColor(ChartUtils.COLOR_GREEN);
            line.setHasLines(true);
            line.setHasPoints(false);
            line.setCubic(false);
            lines.add(line);

            line = new Line(biking);
            line.setColor(ChartUtils.COLOR_BLUE);
            line.setHasLines(true);
            line.setHasPoints(false);
            line.setCubic(false);
            lines.add(line);

            line = new Line(in_vehicle);
            line.setColor(ChartUtils.COLOR_VIOLET);
            line.setHasLines(true);
            line.setHasPoints(false);
            line.setCubic(false);
            lines.add(line);

            fitnessFragment.setActivityData(xAxisValues, lines);
          }
        }