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.RecordTypeIdentifier; 23 import android.health.connect.datatypes.SleepSessionRecord; 24 import android.os.Parcel; 25 26 import java.util.ArrayList; 27 import java.util.List; 28 import java.util.Objects; 29 30 /** 31 * @see SleepSessionRecord 32 * @hide 33 */ 34 @Identifier(recordIdentifier = RecordTypeIdentifier.RECORD_TYPE_SLEEP_SESSION) 35 public final class SleepSessionRecordInternal extends IntervalRecordInternal<SleepSessionRecord> { 36 @SuppressWarnings("NullAway.Init") // TODO(b/317029272): fix this suppression 37 private List<SleepStageInternal> mStages; 38 39 @SuppressWarnings("NullAway.Init") // TODO(b/317029272): fix this suppression 40 private String mNotes; 41 42 @SuppressWarnings("NullAway.Init") // TODO(b/317029272): fix this suppression 43 private String mTitle; 44 45 @Nullable getNotes()46 public String getNotes() { 47 return mNotes; 48 } 49 50 /** returns this object with the specified notes */ 51 @NonNull setNotes(String notes)52 public SleepSessionRecordInternal setNotes(String notes) { 53 mNotes = notes; 54 return this; 55 } 56 57 @Nullable getTitle()58 public String getTitle() { 59 return mTitle; 60 } 61 62 /** returns this object with the specified title */ 63 @NonNull setTitle(String title)64 public SleepSessionRecordInternal setTitle(String title) { 65 mTitle = title; 66 return this; 67 } 68 69 /** returns sleep stages of this session */ 70 @Nullable getSleepStages()71 public List<SleepStageInternal> getSleepStages() { 72 return mStages; 73 } 74 75 /** returns this object with sleep stages set */ setSleepStages(@onNull List<SleepStageInternal> stages)76 public SleepSessionRecordInternal setSleepStages(@NonNull List<SleepStageInternal> stages) { 77 Objects.requireNonNull(stages); 78 mStages = new ArrayList<>(stages); 79 return this; 80 } 81 82 /** Add sleep stage to record stages. */ addSleepStage(@ullable SleepStageInternal stage)83 public SleepSessionRecordInternal addSleepStage(@Nullable SleepStageInternal stage) { 84 if (stage != null) { 85 if (mStages == null) { 86 mStages = new ArrayList<>(); 87 } 88 mStages.add(stage); 89 } 90 return this; 91 } 92 93 @Override populateIntervalRecordTo(@onNull Parcel parcel)94 public void populateIntervalRecordTo(@NonNull Parcel parcel) { 95 parcel.writeString(mNotes); 96 parcel.writeString(mTitle); 97 SleepStageInternal.writeStagesToParcel(mStages, parcel); 98 } 99 100 @Override populateIntervalRecordFrom(@onNull Parcel parcel)101 public void populateIntervalRecordFrom(@NonNull Parcel parcel) { 102 mNotes = parcel.readString(); 103 mTitle = parcel.readString(); 104 mStages = SleepStageInternal.populateStagesFromParcel(parcel); 105 } 106 107 @NonNull 108 @Override toExternalRecord()109 public SleepSessionRecord toExternalRecord() { 110 SleepSessionRecord.Builder builder = 111 new SleepSessionRecord.Builder(buildMetaData(), getStartTime(), getEndTime()); 112 113 if (getStartZoneOffset() != null) { 114 builder.setStartZoneOffset(getStartZoneOffset()); 115 } 116 117 if (getEndZoneOffset() != null) { 118 builder.setEndZoneOffset(getEndZoneOffset()); 119 } 120 121 if (getNotes() != null) { 122 builder.setNotes(getNotes()); 123 } 124 125 if (getTitle() != null) { 126 builder.setTitle(getTitle()); 127 } 128 129 if (getSleepStages() != null) { 130 builder.setStages(SleepStageInternal.getExternalStages(mStages)); 131 } 132 return builder.buildWithoutValidation(); 133 } 134 } 135