Class WorkoutSample


public final class WorkoutSample extends SessionSample

A completed workout: what kind, how long, and the totals the platform computed for it.

Totals are nullable on purpose. A workout recorded without a heart-rate sensor has no energy total, and reporting that as zero would put a false 0 kcal into every summary. null means "not measured"; zero means "measured, and it was zero".

  • Field Details

    • WORKOUT_NOT_PERSISTED

      public static final String WORKOUT_NOT_PERSISTED

      Creates a workout spanning [startMillis, endMillis]. Metadata key set on a workout the platform could not store as a session record of its own.

      Neither HealthKit nor the Health Connect bridge accepts a workout through the sample write path in this release. The child measurements are persisted; the workout comes back for you to keep or upload. Check for this rather than assuming HealthSample.getId() is populated.

      if (workout.getMetadata().containsKey(
              WorkoutSample.WORKOUT_NOT_PERSISTED)) {
          uploadToMyServer(workout);
      }
      
      See Also:
    • SAMPLES_NOT_PERSISTED

      public static final String SAMPLES_NOT_PERSISTED

      Metadata key naming the data types the platform refused to store, comma separated, or absent when everything fed in was persisted.

      Health Connect has no single-value write form for the series-shaped types -- power, speed and both cadences -- which is exactly what a bike or foot pod feeds into a workout. Those samples cannot be stored there, so the workout names them rather than dropping them silently and resolving as though nothing had happened.

      See Also:
  • Method Details

    • create

      public static WorkoutSample create(WorkoutActivityType activityType, long startMillis, long endMillis)
    • getActivityType

      public WorkoutActivityType getActivityType()
      The activity, mapped onto this API's shared vocabulary. See getPlatformCode() when you need exactly what the platform said.
    • getPlatformCode

      public int getPlatformCode()

      The raw platform activity constant -- an HKWorkoutActivityType on iOS or an ExerciseSessionRecord exercise type on Android -- or -1 when unknown.

      This is the fidelity escape hatch for the deliberately partial WorkoutActivityType vocabulary. It is platform-specific by definition: the same integer means different things on the two platforms, so branch on getActivityType() first and only reach for this when you must.

    • setPlatformCode

      public void setPlatformCode(int platformCode)
      Records the raw platform activity constant. Called by ports.
    • getTotalEnergy

      public HealthQuantity getTotalEnergy()
      Energy burned across the workout, or null when not measured.
    • setTotalEnergy

      public void setTotalEnergy(HealthQuantity totalEnergy)

      Sets the energy total.

      Throws
      • IllegalArgumentException: if the quantity does not measure energy.
    • getTotalDistance

      public HealthQuantity getTotalDistance()
      Distance covered, or null when not measured.
    • setTotalDistance

      public void setTotalDistance(HealthQuantity totalDistance)

      Sets the distance total.

      Throws
      • IllegalArgumentException: if the quantity does not measure length.
    • getActiveDurationMillis

      public long getActiveDurationMillis()
      Time actually exercising, excluding pauses. Falls back to the wall duration when the platform did not report it separately.
    • setActiveDurationMillis

      public void setActiveDurationMillis(long activeDurationMillis)

      Sets the paused-time-excluded duration.

      Any negative value means "not reported separately", which is what getActiveDurationMillis() answers with the wall duration.

      Throws
      • IllegalArgumentException: if the value exceeds the workout's own duration.
    • getActiveDuration

      public Duration getActiveDuration()

      The active portion of this workout, as a Duration.

      The millis pair stays for the ports and the wire format; this is the type the rest of the framework speaks.

    • setActiveDuration

      public void setActiveDuration(Duration active)
      Sets the active portion of this workout.
    • toString

      public String toString()
      Description copied from class: Object
      Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method. The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of: getClass().getName() + '@' + Integer.toHexString(hashCode())
      Overrides:
      toString in class HealthSample