• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.health.connect.datatypes.ExerciseLap;
21 import android.health.connect.datatypes.units.Length;
22 import android.os.Parcel;
23 
24 import com.android.internal.annotations.VisibleForTesting;
25 
26 import java.time.Instant;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.Objects;
30 
31 /**
32  * Internal ExerciseLap. Part of {@link ExerciseSessionRecordInternal}
33  *
34  * @hide
35  */
36 public class ExerciseLapInternal {
37 
38     private long mStartTime;
39     private long mEndTime;
40     private double mLength;
41 
42     /** Reads lap from parcel. */
43     @VisibleForTesting
readFromParcel(Parcel parcel)44     public static ExerciseLapInternal readFromParcel(Parcel parcel) {
45         return new ExerciseLapInternal()
46                 .setStarTime((parcel.readLong()))
47                 .setEndTime(parcel.readLong())
48                 .setLength(parcel.readDouble());
49     }
50 
writeLapsToParcel(List<ExerciseLapInternal> laps, Parcel parcel)51     static void writeLapsToParcel(List<ExerciseLapInternal> laps, Parcel parcel) {
52         if (laps == null) {
53             parcel.writeInt(0);
54             return;
55         }
56 
57         parcel.writeInt(laps.size());
58         for (ExerciseLapInternal lap : laps) {
59             lap.writeToParcel(parcel);
60         }
61     }
62 
getExternalLaps(@onNull List<ExerciseLapInternal> internalLaps)63     static List<ExerciseLap> getExternalLaps(@NonNull List<ExerciseLapInternal> internalLaps) {
64         List<ExerciseLap> externalLaps = new ArrayList<>(internalLaps.size());
65         internalLaps.forEach((lap) -> externalLaps.add(lap.toExternalRecord()));
66         return externalLaps;
67     }
68 
populateLapsFromParcel(Parcel parcel)69     static List<ExerciseLapInternal> populateLapsFromParcel(Parcel parcel) {
70         int lapsSize = parcel.readInt();
71         if (lapsSize == 0) {
72             return null;
73         }
74 
75         ArrayList<ExerciseLapInternal> laps = new ArrayList<>(lapsSize);
76         for (int i = 0; i < lapsSize; i++) {
77             laps.add(ExerciseLapInternal.readFromParcel(parcel));
78         }
79         return laps;
80     }
81 
82     /** Returns laps start time. */
83     @VisibleForTesting
writeToParcel(Parcel parcel)84     public void writeToParcel(Parcel parcel) {
85         parcel.writeLong(mStartTime);
86         parcel.writeLong(mEndTime);
87         parcel.writeDouble(mLength);
88     }
89 
90     /** Converts to external record. */
91     @VisibleForTesting
toExternalRecord()92     public ExerciseLap toExternalRecord() {
93         ExerciseLap.Builder builder =
94                 new ExerciseLap.Builder(
95                         Instant.ofEpochMilli(getStartTime()), Instant.ofEpochMilli(getEndTime()));
96 
97         if (getLength() != 0) {
98             builder.setLength(Length.fromMeters(getLength()));
99         }
100         return builder.buildWithoutValidation();
101     }
102 
103     /** Returns lap length in meters. */
getLength()104     public double getLength() {
105         return mLength;
106     }
107 
108     /** Set laps length. return record with the length set. */
setLength(double lengthInMeters)109     public ExerciseLapInternal setLength(double lengthInMeters) {
110         mLength = lengthInMeters;
111         return this;
112     }
113 
114     /** Sets lap start time. Returns record with start time set. */
setStarTime(long startTime)115     public ExerciseLapInternal setStarTime(long startTime) {
116         mStartTime = startTime;
117         return this;
118     }
119 
120     /** Sets lap end time. Returns record with end time set. */
setEndTime(long endTime)121     public ExerciseLapInternal setEndTime(long endTime) {
122         mEndTime = endTime;
123         return this;
124     }
125 
126     /** Returns laps start time. */
getStartTime()127     public long getStartTime() {
128         return mStartTime;
129     }
130 
131     /** Returns laps end time. */
getEndTime()132     public long getEndTime() {
133         return mEndTime;
134     }
135 
136     @Override
equals(Object o)137     public boolean equals(Object o) {
138         if (this == o) return true;
139         if (!(o instanceof ExerciseLapInternal)) return false;
140         ExerciseLapInternal that = (ExerciseLapInternal) o;
141         return mStartTime == that.mStartTime
142                 && mEndTime == that.mEndTime
143                 && mLength == that.mLength;
144     }
145 
146     @Override
hashCode()147     public int hashCode() {
148         return Objects.hash(mStartTime, mEndTime, mLength);
149     }
150 }
151