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