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.PowerRecord;
23 import android.health.connect.datatypes.RecordTypeIdentifier;
24 import android.health.connect.datatypes.units.Power;
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 PowerRecord
36  * @hide
37  */
38 @Identifier(recordIdentifier = RecordTypeIdentifier.RECORD_TYPE_POWER)
39 public class PowerRecordInternal
40         extends SeriesRecordInternal<PowerRecord, PowerRecord.PowerRecordSample> {
41     @SuppressWarnings("NullAway.Init") // TODO(b/317029272): fix this suppression
42     private Set<PowerRecordSample> mPowerRecordSamples;
43 
44     @Override
populateIntervalRecordFrom(@onNull Parcel parcel)45     void populateIntervalRecordFrom(@NonNull Parcel parcel) {
46         int size = parcel.readInt();
47         mPowerRecordSamples = new HashSet<>(size);
48         for (int i = 0; i < size; i++) {
49             mPowerRecordSamples.add(new PowerRecordSample(parcel.readDouble(), parcel.readLong()));
50         }
51     }
52 
53     @Override
54     @NonNull
getSamples()55     public Set<PowerRecordSample> getSamples() {
56         return mPowerRecordSamples;
57     }
58 
59     @NonNull
60     @Override
setSamples(Set<? extends Sample> samples)61     public PowerRecordInternal setSamples(Set<? extends Sample> samples) {
62         Objects.requireNonNull(samples);
63         this.mPowerRecordSamples = (Set<PowerRecordSample>) samples;
64         return this;
65     }
66 
67     @Override
68     @NonNull
toExternalRecord()69     public PowerRecord toExternalRecord() {
70         return new PowerRecord.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(mPowerRecordSamples.size());
80         for (PowerRecordSample powerRecordSample : mPowerRecordSamples) {
81             parcel.writeDouble(powerRecordSample.getPower());
82             parcel.writeLong(powerRecordSample.getEpochMillis());
83         }
84     }
85 
getExternalSamples()86     private List<PowerRecord.PowerRecordSample> getExternalSamples() {
87         List<PowerRecord.PowerRecordSample> powerRecords =
88                 new ArrayList<>(mPowerRecordSamples.size());
89         for (PowerRecordSample powerRecordSample : mPowerRecordSamples) {
90             powerRecords.add(
91                     new PowerRecord.PowerRecordSample(
92                             Power.fromWatts(powerRecordSample.getPower()),
93                             Instant.ofEpochMilli(powerRecordSample.getEpochMillis()),
94                             true));
95         }
96         return powerRecords;
97     }
98 
99     /**
100      * @see PowerRecord.PowerRecordSample
101      */
102     public static final class PowerRecordSample implements Sample {
103         private final double mPower;
104         private final long mEpochMillis;
105 
PowerRecordSample(double power, long epochMillis)106         public PowerRecordSample(double power, long epochMillis) {
107             mPower = power;
108             mEpochMillis = epochMillis;
109         }
110 
getPower()111         public double getPower() {
112             return mPower;
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 PowerRecordInternal.PowerRecordSample) {
122                 PowerRecordInternal.PowerRecordSample other =
123                         (PowerRecordInternal.PowerRecordSample) 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