目录

SamplePoint

Class Info

public class SamplePoint implements Parcelable

Sampling point class, which presents the sampled data of a specific type collected by a specific data collector at a given time or within a time range. It stores the values of each dimension of the data type at the given time (or within the time range) and the start time and end time of the sampling operation.

The sampling modes of sampling points are classified into instantaneous sampling and continuous sampling based on the definition of data types.

When setting the sampling point time, you are advised to call setSamplingTime() for instantaneous sampling and setTimeInterval() for continuous sampling (make sure to set a valid time duration).

setSamplingTime() cannot be used when creating the continuous sampling point. Otherwise, the creation will fail. This API can be used to update the end time of a created continuous sampling point. After the API is called, only the end time will be updated.

Nested Class Summary

Qualifier and Type

Class Name and Description

public static class

SamplePoint.Builder

Builder for SamplePoint.

Public Method Summary

Qualifier and Type

Method Name and Description

void

addMetadata(String key, String value)

Sets the metadata in SamplePoint for a key value pair.

static Builder

builder(DataCollector dataCollector)

Creates a SamplePoint object based on the data source.

final void

checkIntervalValidity()

Checks the interval validity.

static SamplePoint

create(DataCollector dataCollector)

Builds a sampling point.

DataCollector

getDataCollector()

Obtains the data collector of the sampling point.

DataType

getDataType()

Obtains the data type of the sampling point.

long

getEndTime(TimeUnit timeUnit)

Obtains the end timestamp of the sampling point.

Value

getFieldValue(Field field)

Obtains the attribute value.

Value

getFieldValue(String fieldName)

Obtains the attribute value.

Map<String, Value>

getFieldValues()

Obtains the values of all attributes.

long

getId()

Obtains the ID of the sampling point.

long

getInsertionTime(TimeUnit timeUnit)

Obtains the insertion time.

String

getMetadata()

Obtains the metadata from SamplePoint.

final DataCollector

getNullableRawCollector()

Obtains the raw data collector of the sampling point. If it is not set, null will be returned.

final DataCollector

getRawDataCollector()

Obtains the raw data collector of the sampling point.

long

getSamplingTime(TimeUnit timeUnit)

Obtains the timestamp of the sampling point.

long

getStartTime(TimeUnit timeUnit)

Obtains the start timestamp of the sampling point.

boolean

isMerged()

Checks whether data is merged.

void

setId(long id)

Sets the ID of the sampling point.

void

setInsertionTime(long insertTime, TimeUnit timeUnit)

Sets the data insertion time.

void

setMetadata(String metadata)

Sets the metadata in SamplePoint directly.

final SamplePoint

setSamplingTime(long sampleTime, TimeUnit timeUnit)

Sets the sampling timestamp of instantaneous data.

final SamplePoint

setTimeInterval(long start, long end, TimeUnit timeUnit)

Sets the sampling timestamp of data within an interval.

Public Methods

[h2]builder(DataCollector dataCollector)

Method

public static Builder builder(DataCollector dataCollector)

Creates a SamplePoint object based on the data source.

Parameters

Name

Description

dataCollector

Data collector.

Returns

Type

Description

Builder

Internal builder object for the data sampling point.

[h2]create(DataCollector dataCollector)

Method

public static SamplePoint create(DataCollector dataCollector)

Builds a sampling point.

Parameters

Name

Description

dataCollector

Data collector.

Returns

Type

Description

SamplePoint

Sampling point.

[h2]checkIntervalValidity()

Method

public final void checkIntervalValidity()

Rules for verifying the interval validity:

  1. The start time and end time must not be 0.
  2. The start time must not be later than the end time.
  3. The time span must not exceed 3 days.
  4. For the continuous data types, the start time must be earlier than the end time.
  5. The time span for collecting total or polymerized data must not exceed 366 days.
  6. For a data type that can't be identified (customized data type), conditions 1, 2, and 3 must be met.

[h2]getDataCollector()

Method

public DataCollector getDataCollector()

Obtains the data collector of the sampling point.

Returns

Type

Description

DataCollector

Data collector.

[h2]getDataType()

Method

public DataType getDataType()

Obtains the data type of the sampling point.

Returns

Type

Description

DataType

Data type.

[h2]getEndTime(TimeUnit timeUnit)

Method

public long getEndTime(TimeUnit timeUnit)

Obtains the end timestamp of the sampling point.

Parameters

Name

Description

timeUnit

Time unit.

Returns

Type

Description

long

End time.

[h2]getFieldValue(Field field)

Method

public Value getFieldValue(Field field)

Obtains the attribute value.

Parameters

Name

Description

field

Data attribute.

Returns

Type

Description

Value

Attribute value.

[h2]getFieldValue(String fieldName)

Method

public Value getFieldValue(String fieldName)

Obtains the attribute value.

Parameters

Name

Description

fieldName

Data attribute name.

Returns

Type

Description

Value

Attribute value.

[h2]getFieldValues()

Method

public Map<String, Value> getFieldValues()

Obtains the values of all attributes.

Returns

Type

Description

Map<String, Value>

Values of all attributes.

[h2]getId()

Method

public long getId()

Obtains the ID of the sampling point.

Returns

Type

Description

long

ID of the sampling point.

[h2]getInsertionTime(TimeUnit timeUnit)

Method

public long getInsertionTime(TimeUnit timeUnit)

Obtains the insertion time.

Parameters

Name

Description

timeUnit

Time unit.

Returns

Type

Description

long

Insertion time.

[h2]getNullableRawCollector()

Method

public final DataCollector getNullableRawCollector()

Obtains the raw data collector of the sampling point. If it is not set, null will be returned.

Returns

Type

Description

DataCollector

Data collector.

[h2]getRawDataCollector()

Method

public final DataCollector getRawDataCollector()

Obtains the raw data collector of the sampling point.

Returns

Type

Description

DataCollector

Data collector.

[h2]getSamplingTime(TimeUnit timeUnit)

Method

public long getSamplingTime(TimeUnit timeUnit)

Obtains the timestamp of the sampling point.

Parameters

Name

Description

timeUnit

Time unit.

Returns

Type

Description

long

Timestamp.

[h2]getStartTime(TimeUnit timeUnit)

Method

public long getStartTime(TimeUnit timeUnit)

Obtains the start timestamp of the sampling point.

Parameters

Name

Description

timeUnit

Time unit.

Returns

Type

Description

long

Start time.

[h2]isMerged()

Method

public boolean isMerged()

Checks whether data is merged.

Returns

Type

Description

boolean

Whether data is merged.

[h2]setId()

Method

public void setId(long id)

Sets the ID of the sampling point.

Parameters

Name

Description

ID of the sampling point.

ID of the sampling point.

[h2]setInsertionTime(long insertTime, TimeUnit timeUnit)

Method

public void setInsertionTime(long insertTime, TimeUnit timeUnit)

Sets the data insertion time.

Parameters

Name

Description

insertTime

Data insertion time.

timeUnit

Time unit.

[h2]setSamplingTime(long sampleTime, TimeUnit timeUnit)

Method

public final SamplePoint setSamplingTime(long sampleTime, TimeUnit timeUnit)

Sets the sampling timestamp of instantaneous data.

Parameters

Name

Description

sampleTime

Timestamp.

timeUnit

Time unit.

Returns

Type

Description

SamplePoint

Sampling point.

[h2]setTimeInterval(long start, long end, TimeUnit timeUnit)

Method

public final SamplePoint setTimeInterval(long start, long end, TimeUnit timeUnit)

Sets the sampling timestamp of data within an interval.

Parameters

Name

Description

start

Start time.

end

End time.

timeUnit

Time unit.

Returns

Type

Description

SamplePoint

Sampling point.

[h2]getMetadata()

Method

public String getMetadata()

Obtains the metadata from SamplePoint.

Returns

Type

Description

String

Metadata.

[h2]setMetadata(String metadata)

Method

public void setMetadata(String metadata)

Sets the metadata in SamplePoint.

Parameters

Name

Description

metadata

Metadata, which can be null, an empty string, or a string of 1 to 10000 characters.

See Also:

addMetadata(String key, String value)

[h2]addMetadata(String key, String value)

Method

public void addMetadata(String key, String value)

Sets the metadata in SamplePoint for a key value pair.

Parameters

Name

Description

key

Key of the metadata.

value

Value of the metadata.

If the data type of the created SamplePoint is step count or blood glucose, and you want to display the data written to Health Service Kit on the home screen of the Huawei Health app, you need to set the metadata in SamplePoint based on the following key-value pairs.

  • For the step count data type, the key must be set to motion_type (motion status), and the value can be set as follows:
    • RUN (running)
    • WALK (walking)
    • STAIRS (stairs climbing)
    • UNKNOWN (unknown)
  • For the blood glucose data type, the key must be set to mIsConfirmed (indicating that the user has confirmed the data), and the value can be set to:
    • true (indicating that the user has confirmed the data)
    • false (indicating that the user has not confirmed the data)

The following example shows how to create a SampleSet for the step count data type, in order to call the API for adding fitness and health data.

// Step 1: Build a DataCollector object.
DataCollector dataCollector = new DataCollector.Builder().setPackageName(getApplicationContext())
    .setDataType(DataType.DT_CONTINUOUS_STEPS_DELTA)
    .setDataStreamName("STEPS_DELTA")
    .setDataGenerateType(DataCollector.DATA_TYPE_RAW)
    .build();

// Step 2: Create a sampling dataset based on the data collector.
final SampleSet sampleSet = SampleSet.create(dataCollector);

// Step 3: Build the start time, end time, and incremental step count for a DT_CONTINUOUS_STEPS_DELTA sampling point.
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date startDate = null;
Date endDate = null;
try {
    // Enter the start time and end time. The standard UNIX timestamp is used for storage, without considering the time zone differences.
     // Note that the start time and end time of the sampling point data of the inserted fitness and health data must be later than the UNIX timestamp corresponding to January 1, 2014.
    startDate = dateFormat.parse("2020-03-17 09:00:00");
    endDate = dateFormat.parse("2020-03-17 09:05:00");
} catch (ParseException e) {
    logger("Time parsing error");
}
int stepsDelta = 200;

// Step 4: Build a DT_CONTINUOUS_STEPS_DELTA sampling point.
SamplePoint samplePoint = sampleSet.createSamplePoint()
    .setTimeInterval(startDate.getTime(), endDate.getTime(), TimeUnit.MILLISECONDS);
samplePoint.getFieldValue(Field.FIELD_STEPS_DELTA).setIntValue(stepsDelta);
// To display the written step count data in the Huawei Health app, add the following metadata to the sampling points:
samplePoint.addMetadata("motion_type", "RUN");

// Step 5: Save a DT_CONTINUOUS_STEPS_DELTA sampling point to the sampling dataset. You can repeat steps 3 to 5 to add more sampling points to the sampling dataset.
sampleSet.addSample(samplePoint);