1 /** <lambda>null2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * ``` 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * ``` 10 * 11 * Unless required by applicable law or agreed to in writing, software distributed under the License 12 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 13 * or implied. See the License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 package com.android.healthconnect.testapps.toolbox.seed 17 18 import android.content.Context 19 import android.health.connect.HealthConnectManager 20 import android.health.connect.datatypes.DataOrigin 21 import android.health.connect.datatypes.Device 22 import android.health.connect.datatypes.Metadata 23 import android.health.connect.datatypes.SleepSessionRecord 24 import android.os.Build.MANUFACTURER 25 import android.os.Build.MODEL 26 import com.android.healthconnect.testapps.toolbox.utils.GeneralUtils.Companion.getMetaData 27 import com.android.healthconnect.testapps.toolbox.utils.GeneralUtils.Companion.insertRecords 28 import kotlinx.coroutines.runBlocking 29 import java.time.Duration.ofDays 30 import java.time.Duration.ofHours 31 import java.time.Instant 32 import java.time.temporal.ChronoUnit 33 34 class SeedSleepData(private val context: Context, private val manager: HealthConnectManager) { 35 36 private val start = Instant.now().truncatedTo(ChronoUnit.DAYS) 37 private val yesterday = start.minus(ofDays(1)) 38 private val lastWeek = start.minus(ofDays(7)) 39 private val lastMonth = start.minus(ofDays(31)) 40 41 fun seedSleepCategoryData(){ 42 runBlocking { 43 try { 44 seedSleepData() 45 } catch (ex: Exception) { 46 throw ex 47 } 48 } 49 } 50 51 private suspend fun seedSleepData(){ 52 val records = (1L..2).map { timeOffSet -> 53 getSleepSessionRecord(start.plus(ofHours(timeOffSet))) 54 } 55 val yesterdayRecords = (1L..2).map { timeOffSet -> 56 getSleepSessionRecord(yesterday.plus(ofHours(timeOffSet))) 57 } 58 val lastWeekRecords = (1L..2).map { timeOffSet -> 59 getSleepSessionRecord(lastWeek.plus(ofHours(timeOffSet))) 60 } 61 val lastMonthRecords = (1L..2).map { timeOffSet -> 62 getSleepSessionRecord(lastMonth.plus(ofHours(timeOffSet))) 63 } 64 65 insertRecords(records, manager) 66 insertRecords(yesterdayRecords, manager) 67 insertRecords(lastWeekRecords, manager) 68 insertRecords(lastMonthRecords, manager) 69 } 70 71 private fun getSleepSessionRecord(time: Instant): SleepSessionRecord { 72 return SleepSessionRecord.Builder(getMetaData(context), time, time.plusSeconds(1800)).build() 73 } 74 }