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.ExerciseSegment;
21 import android.health.connect.datatypes.ExerciseSegmentType;
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 ExerciseSegment. Part of {@link ExerciseSessionRecordInternal}
33  *
34  * @hide
35  */
36 public class ExerciseSegmentInternal {
37 
38     private long mStartTime;
39     private long mEndTime;
40 
41     @ExerciseSegmentType.ExerciseSegmentTypes private int mSegmentType;
42 
43     private int mRepetitionsCount;
44 
45     /** Reads record from parcel. */
46     @VisibleForTesting
readFromParcel(Parcel parcel)47     public static ExerciseSegmentInternal readFromParcel(Parcel parcel) {
48         return new ExerciseSegmentInternal()
49                 .setStarTime(parcel.readLong())
50                 .setEndTime(parcel.readLong())
51                 .setRepetitionsCount(parcel.readInt())
52                 .setSegmentType(parcel.readInt());
53     }
54 
55     @SuppressWarnings("NullAway") // TODO(b/317029272): fix this suppression
populateSegmentsFromParcel(Parcel parcel)56     static List<ExerciseSegmentInternal> populateSegmentsFromParcel(Parcel parcel) {
57         int size = parcel.readInt();
58         if (size == 0) {
59             return null;
60         }
61         ArrayList<ExerciseSegmentInternal> segments = new ArrayList<>(size);
62         for (int i = 0; i < size; i++) {
63             segments.add(ExerciseSegmentInternal.readFromParcel(parcel));
64         }
65         return segments;
66     }
67 
getExternalSegments( @onNull List<ExerciseSegmentInternal> internalSegments)68     static List<ExerciseSegment> getExternalSegments(
69             @NonNull List<ExerciseSegmentInternal> internalSegments) {
70         List<ExerciseSegment> externalSegments = new ArrayList<>(internalSegments.size());
71         internalSegments.forEach((segment) -> externalSegments.add(segment.toExternalRecord()));
72         return externalSegments;
73     }
74 
writeSegmentsToParcel(List<ExerciseSegmentInternal> segments, Parcel parcel)75     static void writeSegmentsToParcel(List<ExerciseSegmentInternal> segments, Parcel parcel) {
76         if (segments == null) {
77             parcel.writeInt(0);
78             return;
79         }
80         parcel.writeInt(segments.size());
81         segments.forEach((segment) -> segment.writeToParcel(parcel));
82     }
83 
84     /** Writes record to parcel. */
85     @VisibleForTesting
writeToParcel(Parcel parcel)86     public void writeToParcel(Parcel parcel) {
87         parcel.writeLong(mStartTime);
88         parcel.writeLong(mEndTime);
89         parcel.writeInt(mRepetitionsCount);
90         parcel.writeInt(mSegmentType);
91     }
92 
93     /** Sets segment type. Returns record with type set. */
94     @VisibleForTesting
toExternalRecord()95     public ExerciseSegment toExternalRecord() {
96         return new ExerciseSegment.Builder(
97                         Instant.ofEpochMilli(getStartTime()),
98                         Instant.ofEpochMilli(getEndTime()),
99                         getSegmentType())
100                 .setRepetitionsCount(getRepetitionsCount())
101                 .buildWithoutValidation();
102     }
103 
104     /** Sets segment start time. Returns record with start time set. */
setStarTime(long startTime)105     public ExerciseSegmentInternal setStarTime(long startTime) {
106         mStartTime = startTime;
107         return this;
108     }
109 
110     /** Sets segment end time. Returns record with end time set. */
setEndTime(long endTime)111     public ExerciseSegmentInternal setEndTime(long endTime) {
112         mEndTime = endTime;
113         return this;
114     }
115 
116     /** Returns segment start time. */
getStartTime()117     public long getStartTime() {
118         return mStartTime;
119     }
120 
121     /** Returns segments end time. */
getEndTime()122     public long getEndTime() {
123         return mEndTime;
124     }
125 
126     /** Returns segments repetitions count. */
getRepetitionsCount()127     public int getRepetitionsCount() {
128         return mRepetitionsCount;
129     }
130 
131     /** Sets segment repetitions count. Return record with repetitions set. */
setRepetitionsCount(int repetitionsCount)132     public ExerciseSegmentInternal setRepetitionsCount(int repetitionsCount) {
133         mRepetitionsCount = repetitionsCount;
134         return this;
135     }
136 
137     /** Returns segment type. */
getSegmentType()138     public int getSegmentType() {
139         return mSegmentType;
140     }
141 
142     /** Sets segment type. Returns record with type set. */
setSegmentType(int segmentType)143     public ExerciseSegmentInternal setSegmentType(int segmentType) {
144         mSegmentType = segmentType;
145         return this;
146     }
147 
148     @Override
equals(Object o)149     public boolean equals(Object o) {
150         if (this == o) return true;
151         if (!(o instanceof ExerciseSegmentInternal)) return false;
152         ExerciseSegmentInternal that = (ExerciseSegmentInternal) o;
153         return mSegmentType == that.mSegmentType
154                 && mRepetitionsCount == that.mRepetitionsCount
155                 && mStartTime == that.mStartTime
156                 && mEndTime == that.mEndTime;
157     }
158 
159     @Override
hashCode()160     public int hashCode() {
161         return Objects.hash(mStartTime, mEndTime, mSegmentType, mRepetitionsCount);
162     }
163 }
164