1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.health.connect.internal.datatypes;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.health.connect.datatypes.Identifier;
22 import android.health.connect.datatypes.RecordTypeIdentifier;
23 import android.health.connect.datatypes.StepsCadenceRecord;
24 import android.os.Parcel;
25 
26 import java.time.Instant;
27 import java.util.ArrayList;
28 import java.util.HashSet;
29 import java.util.List;
30 import java.util.Objects;
31 import java.util.Set;
32 
33 /**
34  * @see StepsCadenceRecord
35  * @hide
36  */
37 @Identifier(recordIdentifier = RecordTypeIdentifier.RECORD_TYPE_STEPS_CADENCE)
38 public class StepsCadenceRecordInternal
39         extends SeriesRecordInternal<
40                 StepsCadenceRecord, StepsCadenceRecord.StepsCadenceRecordSample> {
41     @SuppressWarnings("NullAway.Init") // TODO(b/317029272): fix this suppression
42     private Set<StepsCadenceRecordSample> mStepsCadenceRecordSamples;
43 
44     @Override
45     @NonNull
getSamples()46     public Set<StepsCadenceRecordSample> getSamples() {
47         return mStepsCadenceRecordSamples;
48     }
49 
50     @NonNull
51     @Override
setSamples(Set<? extends Sample> samples)52     public StepsCadenceRecordInternal setSamples(Set<? extends Sample> samples) {
53         Objects.requireNonNull(samples);
54         this.mStepsCadenceRecordSamples = (Set<StepsCadenceRecordSample>) samples;
55         return this;
56     }
57 
58     @Override
59     @NonNull
toExternalRecord()60     public StepsCadenceRecord toExternalRecord() {
61         return new StepsCadenceRecord.Builder(
62                         buildMetaData(), getStartTime(), getEndTime(), getExternalSamples())
63                 .setStartZoneOffset(getStartZoneOffset())
64                 .setEndZoneOffset(getEndZoneOffset())
65                 .buildWithoutValidation();
66     }
67 
68     @Override
populateIntervalRecordFrom(@onNull Parcel parcel)69     void populateIntervalRecordFrom(@NonNull Parcel parcel) {
70         int size = parcel.readInt();
71         mStepsCadenceRecordSamples = new HashSet<>(size);
72         for (int i = 0; i < size; i++) {
73             mStepsCadenceRecordSamples.add(
74                     new StepsCadenceRecordSample(parcel.readDouble(), parcel.readLong()));
75         }
76     }
77 
getExternalSamples()78     private List<StepsCadenceRecord.StepsCadenceRecordSample> getExternalSamples() {
79         List<StepsCadenceRecord.StepsCadenceRecordSample> stepsCadenceRecords =
80                 new ArrayList<>(mStepsCadenceRecordSamples.size());
81         for (StepsCadenceRecordSample stepsCadenceRecordSample : mStepsCadenceRecordSamples) {
82             stepsCadenceRecords.add(
83                     new StepsCadenceRecord.StepsCadenceRecordSample(
84                             stepsCadenceRecordSample.getRate(),
85                             Instant.ofEpochMilli(stepsCadenceRecordSample.getEpochMillis()),
86                             true));
87         }
88         return stepsCadenceRecords;
89     }
90 
91     @Override
populateIntervalRecordTo(@onNull Parcel parcel)92     void populateIntervalRecordTo(@NonNull Parcel parcel) {
93         parcel.writeInt(mStepsCadenceRecordSamples.size());
94         for (StepsCadenceRecordSample stepsCadenceRecordSample : mStepsCadenceRecordSamples) {
95             parcel.writeDouble(stepsCadenceRecordSample.getRate());
96             parcel.writeLong(stepsCadenceRecordSample.getEpochMillis());
97         }
98     }
99 
100     /**
101      * @see StepsCadenceRecord.StepsCadenceRecordSample
102      */
103     public static final class StepsCadenceRecordSample implements Sample {
104         private final double mRate;
105         private final long mEpochMillis;
106 
StepsCadenceRecordSample(double rate, long epochMillis)107         public StepsCadenceRecordSample(double rate, long epochMillis) {
108             mRate = rate;
109             mEpochMillis = epochMillis;
110         }
111 
getRate()112         public double getRate() {
113             return mRate;
114         }
115 
getEpochMillis()116         public long getEpochMillis() {
117             return mEpochMillis;
118         }
119 
120         @Override
equals(@ullable Object object)121         public boolean equals(@Nullable Object object) {
122             if (super.equals(object)
123                     && object instanceof StepsCadenceRecordInternal.StepsCadenceRecordSample) {
124                 StepsCadenceRecordInternal.StepsCadenceRecordSample other =
125                         (StepsCadenceRecordInternal.StepsCadenceRecordSample) object;
126                 return getEpochMillis() == other.getEpochMillis();
127             }
128             return false;
129         }
130 
131         @Override
hashCode()132         public int hashCode() {
133             return Objects.hash(getEpochMillis());
134         }
135     }
136 }
137