1 /* <lambda>null2 * 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 package com.android.healthconnect.controller.route 17 18 import android.health.connect.HealthConnectManager 19 import android.health.connect.ReadRecordsRequestUsingIds 20 import android.health.connect.ReadRecordsResponse 21 import android.health.connect.datatypes.ExerciseSessionRecord 22 import androidx.core.os.asOutcomeReceiver 23 import com.android.healthconnect.controller.service.IoDispatcher 24 import com.android.healthconnect.controller.shared.usecase.BaseUseCase 25 import javax.inject.Inject 26 import javax.inject.Singleton 27 import kotlinx.coroutines.CoroutineDispatcher 28 import kotlinx.coroutines.suspendCancellableCoroutine 29 30 @Singleton 31 /** Use case loading an exercise route of a session. */ 32 class LoadExerciseRouteUseCase 33 @Inject 34 constructor( 35 private val healthConnectManager: HealthConnectManager, 36 @IoDispatcher private val dispatcher: CoroutineDispatcher 37 ) : BaseUseCase<String, ExerciseSessionRecord?>(dispatcher) { 38 39 @Suppress("PARAMETER_NAME_CHANGED_ON_OVERRIDE") // for readability 40 override suspend fun execute(sessionId: String): ExerciseSessionRecord? { 41 val request = 42 ReadRecordsRequestUsingIds.Builder(ExerciseSessionRecord::class.java) 43 .addId(sessionId) 44 .build() 45 val records: List<ExerciseSessionRecord> = 46 suspendCancellableCoroutine<ReadRecordsResponse<ExerciseSessionRecord>> { continuation 47 -> 48 healthConnectManager.readRecords( 49 request, Runnable::run, continuation.asOutcomeReceiver()) 50 } 51 .records 52 if (records.isEmpty() || !records[0].hasRoute()) { 53 return null 54 } 55 return records[0] 56 } 57 } 58