| /* |
| * Copyright (C) 2023 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| package android.health.connect.datatypes; |
| |
| import static android.health.connect.datatypes.RecordTypeIdentifier.RECORD_TYPE_FLOORS_CLIMBED; |
| |
| import android.annotation.FloatRange; |
| import android.annotation.NonNull; |
| import android.health.connect.HealthConnectManager; |
| import android.health.connect.datatypes.validation.ValidationUtils; |
| import android.health.connect.internal.datatypes.FloorsClimbedRecordInternal; |
| |
| import java.time.Instant; |
| import java.time.ZoneOffset; |
| import java.util.Objects; |
| |
| /** Captures the number of floors climbed by the user between the start and end time. */ |
| @Identifier(recordIdentifier = RECORD_TYPE_FLOORS_CLIMBED) |
| public final class FloorsClimbedRecord extends IntervalRecord { |
| /** |
| * Metric identifier to get total floors climbed using aggregate APIs in {@link |
| * HealthConnectManager} |
| */ |
| @NonNull |
| public static final AggregationType<Double> FLOORS_CLIMBED_TOTAL = |
| new AggregationType<>( |
| AggregationType.AggregationTypeIdentifier |
| .FLOORS_CLIMBED_RECORD_FLOORS_CLIMBED_TOTAL, |
| AggregationType.SUM, |
| RECORD_TYPE_FLOORS_CLIMBED, |
| Double.class); |
| |
| private final double mFloors; |
| |
| /** |
| * @param metadata Metadata to be associated with the record. See {@link Metadata}. |
| * @param startTime Start time of this activity |
| * @param startZoneOffset Zone offset of the user when the activity started |
| * @param endTime End time of this activity |
| * @param endZoneOffset Zone offset of the user when the activity finished |
| * @param floors Number of floors of this activity. Valid range: 0-1000000. |
| * @param skipValidation Boolean flag to skip validation of record values. |
| */ |
| private FloorsClimbedRecord( |
| @NonNull Metadata metadata, |
| @NonNull Instant startTime, |
| @NonNull ZoneOffset startZoneOffset, |
| @NonNull Instant endTime, |
| @NonNull ZoneOffset endZoneOffset, |
| @FloatRange(from = 0f, to = 1000000f) double floors, |
| boolean skipValidation) { |
| super(metadata, startTime, startZoneOffset, endTime, endZoneOffset, skipValidation); |
| if (!skipValidation) { |
| ValidationUtils.requireInRange(floors, 0.0, 1000000.0, "floors"); |
| } |
| mFloors = floors; |
| } |
| |
| /** |
| * @return number of floors climbed. |
| */ |
| @FloatRange(from = 0f, to = 1000000f) |
| public double getFloors() { |
| return mFloors; |
| } |
| |
| /** |
| * Indicates whether some other object is "equal to" this one. |
| * |
| * @param o the reference object with which to compare. |
| * @return {@code true} if this object is the same as the obj |
| */ |
| @Override |
| public boolean equals(Object o) { |
| if (this == o) return true; |
| if (!super.equals(o)) return false; |
| FloorsClimbedRecord that = (FloorsClimbedRecord) o; |
| return getFloors() == that.getFloors(); |
| } |
| |
| /** |
| * @return a hash code value for this object. |
| */ |
| @Override |
| public int hashCode() { |
| return Objects.hash(super.hashCode(), getFloors()); |
| } |
| |
| /** Builder class for {@link FloorsClimbedRecord} */ |
| public static final class Builder { |
| private final Metadata mMetadata; |
| private final Instant mStartTime; |
| private final Instant mEndTime; |
| private ZoneOffset mStartZoneOffset; |
| private ZoneOffset mEndZoneOffset; |
| private final double mFloors; |
| |
| /** |
| * @param metadata Metadata to be associated with the record. See {@link Metadata}. |
| * @param startTime Start time of this activity |
| * @param endTime End time of this activity |
| * @param floors Number of floors. Required field. Valid range: 0-1000000. |
| */ |
| public Builder( |
| @NonNull Metadata metadata, |
| @NonNull Instant startTime, |
| @NonNull Instant endTime, |
| double floors) { |
| Objects.requireNonNull(metadata); |
| Objects.requireNonNull(startTime); |
| Objects.requireNonNull(endTime); |
| mMetadata = metadata; |
| mStartTime = startTime; |
| mEndTime = endTime; |
| mFloors = floors; |
| mStartZoneOffset = ZoneOffset.systemDefault().getRules().getOffset(startTime); |
| mEndZoneOffset = ZoneOffset.systemDefault().getRules().getOffset(endTime); |
| } |
| |
| /** Sets the zone offset of the user when the activity started */ |
| @NonNull |
| public Builder setStartZoneOffset(@NonNull ZoneOffset startZoneOffset) { |
| Objects.requireNonNull(startZoneOffset); |
| |
| mStartZoneOffset = startZoneOffset; |
| return this; |
| } |
| |
| /** Sets the zone offset of the user when the activity ended */ |
| @NonNull |
| public Builder setEndZoneOffset(@NonNull ZoneOffset endZoneOffset) { |
| Objects.requireNonNull(endZoneOffset); |
| |
| mEndZoneOffset = endZoneOffset; |
| return this; |
| } |
| |
| /** Sets the start zone offset of this record to system default. */ |
| @NonNull |
| public Builder clearStartZoneOffset() { |
| mStartZoneOffset = RecordUtils.getDefaultZoneOffset(); |
| return this; |
| } |
| |
| /** Sets the start zone offset of this record to system default. */ |
| @NonNull |
| public Builder clearEndZoneOffset() { |
| mEndZoneOffset = RecordUtils.getDefaultZoneOffset(); |
| return this; |
| } |
| |
| /** |
| * @return Object of {@link FloorsClimbedRecord} without validating the values. |
| * @hide |
| */ |
| @NonNull |
| public FloorsClimbedRecord buildWithoutValidation() { |
| return new FloorsClimbedRecord( |
| mMetadata, |
| mStartTime, |
| mStartZoneOffset, |
| mEndTime, |
| mEndZoneOffset, |
| mFloors, |
| true); |
| } |
| |
| /** |
| * @return Object of {@link FloorsClimbedRecord} |
| */ |
| @NonNull |
| public FloorsClimbedRecord build() { |
| return new FloorsClimbedRecord( |
| mMetadata, |
| mStartTime, |
| mStartZoneOffset, |
| mEndTime, |
| mEndZoneOffset, |
| mFloors, |
| false); |
| } |
| } |
| |
| /** @hide */ |
| @Override |
| public FloorsClimbedRecordInternal toRecordInternal() { |
| FloorsClimbedRecordInternal recordInternal = |
| (FloorsClimbedRecordInternal) |
| new FloorsClimbedRecordInternal() |
| .setUuid(getMetadata().getId()) |
| .setPackageName(getMetadata().getDataOrigin().getPackageName()) |
| .setLastModifiedTime( |
| getMetadata().getLastModifiedTime().toEpochMilli()) |
| .setClientRecordId(getMetadata().getClientRecordId()) |
| .setClientRecordVersion(getMetadata().getClientRecordVersion()) |
| .setManufacturer(getMetadata().getDevice().getManufacturer()) |
| .setModel(getMetadata().getDevice().getModel()) |
| .setDeviceType(getMetadata().getDevice().getType()) |
| .setRecordingMethod(getMetadata().getRecordingMethod()); |
| recordInternal.setStartTime(getStartTime().toEpochMilli()); |
| recordInternal.setEndTime(getEndTime().toEpochMilli()); |
| recordInternal.setStartZoneOffset(getStartZoneOffset().getTotalSeconds()); |
| recordInternal.setEndZoneOffset(getEndZoneOffset().getTotalSeconds()); |
| recordInternal.setFloors(mFloors); |
| return recordInternal; |
| } |
| } |