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 private List<SleepStageInternal> mStages; 37 private String mNotes; 38 private String mTitle; 39 40 @Nullable getNotes()41 public String getNotes() { 42 return mNotes; 43 } 44 45 /** returns this object with the specified notes */ 46 @NonNull setNotes(String notes)47 public SleepSessionRecordInternal setNotes(String notes) { 48 mNotes = notes; 49 return this; 50 } 51 52 @Nullable getTitle()53 public String getTitle() { 54 return mTitle; 55 } 56 57 /** returns this object with the specified title */ 58 @NonNull setTitle(String title)59 public SleepSessionRecordInternal setTitle(String title) { 60 mTitle = title; 61 return this; 62 } 63 64 /** returns sleep stages of this session */ 65 @Nullable getSleepStages()66 public List<SleepStageInternal> getSleepStages() { 67 return mStages; 68 } 69 70 /** returns this object with sleep stages set */ setSleepStages(@onNull List<SleepStageInternal> stages)71 public SleepSessionRecordInternal setSleepStages(@NonNull List<SleepStageInternal> stages) { 72 Objects.requireNonNull(stages); 73 mStages = new ArrayList<>(stages); 74 return this; 75 } 76 77 /** Add sleep stage to record stages. */ addSleepStage(@ullable SleepStageInternal stage)78 public SleepSessionRecordInternal addSleepStage(@Nullable SleepStageInternal stage) { 79 if (stage != null) { 80 if (mStages == null) { 81 mStages = new ArrayList<>(); 82 } 83 mStages.add(stage); 84 } 85 return this; 86 } 87 88 @Override populateIntervalRecordTo(@onNull Parcel parcel)89 public void populateIntervalRecordTo(@NonNull Parcel parcel) { 90 parcel.writeString(mNotes); 91 parcel.writeString(mTitle); 92 SleepStageInternal.writeStagesToParcel(mStages, parcel); 93 } 94 95 @Override populateIntervalRecordFrom(@onNull Parcel parcel)96 public void populateIntervalRecordFrom(@NonNull Parcel parcel) { 97 mNotes = parcel.readString(); 98 mTitle = parcel.readString(); 99 mStages = SleepStageInternal.populateStagesFromParcel(parcel); 100 } 101 102 @NonNull 103 @Override toExternalRecord()104 public SleepSessionRecord toExternalRecord() { 105 SleepSessionRecord.Builder builder = 106 new SleepSessionRecord.Builder(buildMetaData(), getStartTime(), getEndTime()); 107 108 if (getStartZoneOffset() != null) { 109 builder.setStartZoneOffset(getStartZoneOffset()); 110 } 111 112 if (getEndZoneOffset() != null) { 113 builder.setEndZoneOffset(getEndZoneOffset()); 114 } 115 116 if (getNotes() != null) { 117 builder.setNotes(getNotes()); 118 } 119 120 if (getTitle() != null) { 121 builder.setTitle(getTitle()); 122 } 123 124 if (getSleepStages() != null) { 125 builder.setStages(SleepStageInternal.getExternalStages(mStages)); 126 } 127 return builder.buildWithoutValidation(); 128 } 129 } 130