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.InstantRecord; 21 import android.os.Parcel; 22 23 import java.time.Instant; 24 import java.time.LocalDate; 25 import java.time.ZoneOffset; 26 27 /** 28 * Base class for all health connect datatype records that require a time and a zone offseet. 29 * 30 * @param <T> instant record 31 * @hide 32 */ 33 public abstract class InstantRecordInternal<T extends InstantRecord> extends RecordInternal<T> { 34 private long mTime; 35 private int mZoneOffset; 36 getTimeInMillis()37 public long getTimeInMillis() { 38 return mTime; 39 } 40 getZoneOffsetInSeconds()41 public int getZoneOffsetInSeconds() { 42 return mZoneOffset; 43 } 44 45 /** 46 * @return the {@link LocalDate} object of this activity time. 47 */ 48 @Override 49 @NonNull getLocalDate()50 public LocalDate getLocalDate() { 51 return LocalDate.ofInstant(this.getTime(), this.getZoneOffset()); 52 } 53 54 @Override populateRecordFrom(@onNull Parcel parcel)55 void populateRecordFrom(@NonNull Parcel parcel) { 56 mTime = parcel.readLong(); 57 mZoneOffset = parcel.readInt(); 58 59 populateInstantRecordFrom(parcel); 60 } 61 62 @Override populateRecordTo(@onNull Parcel parcel)63 void populateRecordTo(@NonNull Parcel parcel) { 64 parcel.writeLong(mTime); 65 parcel.writeInt(mZoneOffset); 66 67 populateInstantRecordTo(parcel); 68 } 69 70 @Override getRecordTime()71 public long getRecordTime() { 72 return getTimeInMillis(); 73 } 74 getTime()75 Instant getTime() { 76 return Instant.ofEpochMilli(mTime); 77 } 78 79 /** 80 * @param time time to update this object with 81 * @return this object 82 */ 83 @NonNull setTime(long time)84 public InstantRecordInternal<T> setTime(long time) { 85 mTime = time; 86 return this; 87 } 88 getZoneOffset()89 ZoneOffset getZoneOffset() { 90 return ZoneOffset.ofTotalSeconds(mZoneOffset); 91 } 92 93 /** 94 * @param zoneOffset zoneOffset to update this object with 95 * @return this object 96 */ 97 @NonNull setZoneOffset(int zoneOffset)98 public InstantRecordInternal<T> setZoneOffset(int zoneOffset) { 99 mZoneOffset = zoneOffset; 100 return this; 101 } 102 103 /** 104 * Child class must implement this method and populates itself with the data present in {@code 105 * bundle}. Reads should be in the same order as write 106 */ populateInstantRecordFrom(@onNull Parcel parcel)107 abstract void populateInstantRecordFrom(@NonNull Parcel parcel); 108 109 /** 110 * Populate {@code bundle} with the data required to un-bundle self. This is used during IPC 111 * transmissions 112 */ populateInstantRecordTo(@onNull Parcel parcel)113 abstract void populateInstantRecordTo(@NonNull Parcel parcel); 114 } 115