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.CyclingPedalingCadenceRecord;
22 import android.health.connect.datatypes.Identifier;
23 import android.health.connect.datatypes.RecordTypeIdentifier;
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 CyclingPedalingCadenceRecord
35  * @hide
36  */
37 @Identifier(recordIdentifier = RecordTypeIdentifier.RECORD_TYPE_CYCLING_PEDALING_CADENCE)
38 public class CyclingPedalingCadenceRecordInternal
39         extends SeriesRecordInternal<
40                 CyclingPedalingCadenceRecord,
41                 CyclingPedalingCadenceRecord.CyclingPedalingCadenceRecordSample> {
42     @SuppressWarnings("NullAway.Init") // TODO(b/317029272): fix this suppression
43     private Set<CyclingPedalingCadenceRecordSample> mCyclingPedalingCadenceRecordSamples;
44 
45     @Override
46     @NonNull
getSamples()47     public Set<CyclingPedalingCadenceRecordSample> getSamples() {
48         return mCyclingPedalingCadenceRecordSamples;
49     }
50 
51     @NonNull
52     @Override
setSamples(Set<? extends Sample> samples)53     public CyclingPedalingCadenceRecordInternal setSamples(Set<? extends Sample> samples) {
54         Objects.requireNonNull(samples);
55         this.mCyclingPedalingCadenceRecordSamples =
56                 (Set<CyclingPedalingCadenceRecordSample>) samples;
57         return this;
58     }
59 
60     @Override
61     @NonNull
toExternalRecord()62     public CyclingPedalingCadenceRecord toExternalRecord() {
63         return new CyclingPedalingCadenceRecord.Builder(
64                         buildMetaData(), getStartTime(), getEndTime(), getExternalSamples())
65                 .setStartZoneOffset(getStartZoneOffset())
66                 .setEndZoneOffset(getEndZoneOffset())
67                 .buildWithoutValidation();
68     }
69 
70     @Override
populateIntervalRecordFrom(@onNull Parcel parcel)71     void populateIntervalRecordFrom(@NonNull Parcel parcel) {
72         int size = parcel.readInt();
73         mCyclingPedalingCadenceRecordSamples = new HashSet<>(size);
74         for (int i = 0; i < size; i++) {
75             mCyclingPedalingCadenceRecordSamples.add(
76                     new CyclingPedalingCadenceRecordSample(parcel.readDouble(), parcel.readLong()));
77         }
78     }
79 
80     @Override
populateIntervalRecordTo(@onNull Parcel parcel)81     void populateIntervalRecordTo(@NonNull Parcel parcel) {
82         parcel.writeInt(mCyclingPedalingCadenceRecordSamples.size());
83         for (CyclingPedalingCadenceRecordSample cyclingPedalingCadenceRecordSample :
84                 mCyclingPedalingCadenceRecordSamples) {
85             parcel.writeDouble(cyclingPedalingCadenceRecordSample.getRevolutionsPerMinute());
86             parcel.writeLong(cyclingPedalingCadenceRecordSample.getEpochMillis());
87         }
88     }
89 
90     private List<CyclingPedalingCadenceRecord.CyclingPedalingCadenceRecordSample>
getExternalSamples()91             getExternalSamples() {
92         List<CyclingPedalingCadenceRecord.CyclingPedalingCadenceRecordSample>
93                 cyclingPedalingCadenceRecords =
94                         new ArrayList<>(mCyclingPedalingCadenceRecordSamples.size());
95         for (CyclingPedalingCadenceRecordSample cyclingPedalingCadenceRecordSample :
96                 mCyclingPedalingCadenceRecordSamples) {
97             cyclingPedalingCadenceRecords.add(
98                     new CyclingPedalingCadenceRecord.CyclingPedalingCadenceRecordSample(
99                             cyclingPedalingCadenceRecordSample.getRevolutionsPerMinute(),
100                             Instant.ofEpochMilli(
101                                     cyclingPedalingCadenceRecordSample.getEpochMillis()),
102                             true));
103         }
104         return cyclingPedalingCadenceRecords;
105     }
106 
107     /**
108      * @see CyclingPedalingCadenceRecord.CyclingPedalingCadenceRecordSample
109      */
110     public static final class CyclingPedalingCadenceRecordSample implements Sample {
111         private final double mRevolutionsPerMinute;
112         private final long mEpochMillis;
113 
CyclingPedalingCadenceRecordSample(double revolutionsPerMinute, long epochMillis)114         public CyclingPedalingCadenceRecordSample(double revolutionsPerMinute, long epochMillis) {
115             mRevolutionsPerMinute = revolutionsPerMinute;
116             mEpochMillis = epochMillis;
117         }
118 
getRevolutionsPerMinute()119         public double getRevolutionsPerMinute() {
120             return mRevolutionsPerMinute;
121         }
122 
getEpochMillis()123         public long getEpochMillis() {
124             return mEpochMillis;
125         }
126 
127         @Override
equals(@ullable Object object)128         public boolean equals(@Nullable Object object) {
129             if (super.equals(object)
130                     && object
131                             instanceof
132                             CyclingPedalingCadenceRecordInternal
133                                     .CyclingPedalingCadenceRecordSample) {
134                 CyclingPedalingCadenceRecordInternal.CyclingPedalingCadenceRecordSample other =
135                         (CyclingPedalingCadenceRecordInternal.CyclingPedalingCadenceRecordSample)
136                                 object;
137                 return getEpochMillis() == other.getEpochMillis();
138             }
139             return false;
140         }
141 
142         @Override
hashCode()143         public int hashCode() {
144             return Objects.hash(getEpochMillis());
145         }
146     }
147 }
148