1 /** 2 * Copyright (C) 2022 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.controller.autodelete.api 17 18 import android.health.connect.HealthConnectException 19 import android.health.connect.HealthConnectManager 20 import com.android.healthconnect.controller.service.IoDispatcher 21 import com.android.healthconnect.controller.shared.usecase.UseCaseResults 22 import javax.inject.Inject 23 import javax.inject.Singleton 24 import kotlinx.coroutines.CoroutineDispatcher 25 import kotlinx.coroutines.withContext 26 27 @Singleton 28 class UpdateAutoDeleteUseCase 29 @Inject 30 constructor( 31 private val healthConnectManager: HealthConnectManager, 32 @IoDispatcher private val dispatcher: CoroutineDispatcher 33 ) { 34 35 companion object { 36 private const val DAYS_IN_MONTH = 30 37 } 38 39 /** Updates the stored auto-delete range. */ invokenull40 suspend operator fun invoke(numberOfMonths: Int): UseCaseResults<Unit> = 41 withContext(dispatcher) { 42 try { 43 healthConnectManager.setRecordRetentionPeriodInDays( 44 numberOfMonths * DAYS_IN_MONTH, Runnable::run) {} 45 UseCaseResults.Success(Unit) 46 } catch (ex: HealthConnectException) { 47 UseCaseResults.Failed(ex) 48 } 49 } 50 } 51