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 69 @SuppressWarnings("NullAway") // TODO(b/317029272): fix this suppression populateLapsFromParcel(Parcel parcel)70 static List<ExerciseLapInternal> populateLapsFromParcel(Parcel parcel) { 71 int lapsSize = parcel.readInt(); 72 if (lapsSize == 0) { 73 return null; 74 } 75 76 ArrayList<ExerciseLapInternal> laps = new ArrayList<>(lapsSize); 77 for (int i = 0; i < lapsSize; i++) { 78 laps.add(ExerciseLapInternal.readFromParcel(parcel)); 79 } 80 return laps; 81 } 82 83 /** Returns laps start time. */ 84 @VisibleForTesting writeToParcel(Parcel parcel)85 public void writeToParcel(Parcel parcel) { 86 parcel.writeLong(mStartTime); 87 parcel.writeLong(mEndTime); 88 parcel.writeDouble(mLength); 89 } 90 91 /** Converts to external record. */ 92 @VisibleForTesting toExternalRecord()93 public ExerciseLap toExternalRecord() { 94 ExerciseLap.Builder builder = 95 new ExerciseLap.Builder( 96 Instant.ofEpochMilli(getStartTime()), Instant.ofEpochMilli(getEndTime())); 97 98 if (getLength() != 0) { 99 builder.setLength(Length.fromMeters(getLength())); 100 } 101 return builder.buildWithoutValidation(); 102 } 103 104 /** Returns lap length in meters. */ getLength()105 public double getLength() { 106 return mLength; 107 } 108 109 /** Set laps length. return record with the length set. */ setLength(double lengthInMeters)110 public ExerciseLapInternal setLength(double lengthInMeters) { 111 mLength = lengthInMeters; 112 return this; 113 } 114 115 /** Sets lap start time. Returns record with start time set. */ setStarTime(long startTime)116 public ExerciseLapInternal setStarTime(long startTime) { 117 mStartTime = startTime; 118 return this; 119 } 120 121 /** Sets lap end time. Returns record with end time set. */ setEndTime(long endTime)122 public ExerciseLapInternal setEndTime(long endTime) { 123 mEndTime = endTime; 124 return this; 125 } 126 127 /** Returns laps start time. */ getStartTime()128 public long getStartTime() { 129 return mStartTime; 130 } 131 132 /** Returns laps end time. */ getEndTime()133 public long getEndTime() { 134 return mEndTime; 135 } 136 137 @Override equals(Object o)138 public boolean equals(Object o) { 139 if (this == o) return true; 140 if (!(o instanceof ExerciseLapInternal)) return false; 141 ExerciseLapInternal that = (ExerciseLapInternal) o; 142 return mStartTime == that.mStartTime 143 && mEndTime == that.mEndTime 144 && mLength == that.mLength; 145 } 146 147 @Override hashCode()148 public int hashCode() { 149 return Objects.hash(mStartTime, mEndTime, mLength); 150 } 151 } 152