1 /*
2  * Copyright (C) 2024 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 com.android.settingslib.media.domain.interactor
18 
19 import android.media.AudioDeviceAttributes
20 import com.android.settingslib.media.data.repository.SpatializerRepository
21 
22 class SpatializerInteractor(private val repository: SpatializerRepository) {
23 
24     /** Checks if spatial audio is available. */
isSpatialAudioAvailablenull25     suspend fun isSpatialAudioAvailable(audioDeviceAttributes: AudioDeviceAttributes): Boolean =
26         repository.isSpatialAudioAvailableForDevice(audioDeviceAttributes)
27 
28     /** Checks if head tracking is available. */
29     suspend fun isHeadTrackingAvailable(audioDeviceAttributes: AudioDeviceAttributes): Boolean =
30         repository.isHeadTrackingAvailableForDevice(audioDeviceAttributes)
31 
32     /** Checks if spatial audio is enabled for the [audioDeviceAttributes]. */
33     suspend fun isSpatialAudioEnabled(audioDeviceAttributes: AudioDeviceAttributes): Boolean =
34         repository.getSpatialAudioCompatibleDevices().contains(audioDeviceAttributes)
35 
36     /** Enables or disables spatial audio for [audioDeviceAttributes]. */
37     suspend fun setSpatialAudioEnabled(
38         audioDeviceAttributes: AudioDeviceAttributes,
39         isEnabled: Boolean
40     ) {
41         if (isEnabled) {
42             repository.addSpatialAudioCompatibleDevice(audioDeviceAttributes)
43         } else {
44             repository.removeSpatialAudioCompatibleDevice(audioDeviceAttributes)
45         }
46     }
47 
48     /** Checks if head tracking is enabled for the [audioDeviceAttributes]. */
isHeadTrackingEnablednull49     suspend fun isHeadTrackingEnabled(audioDeviceAttributes: AudioDeviceAttributes): Boolean =
50         repository.isHeadTrackingEnabled(audioDeviceAttributes)
51 
52     /** Enables or disables head tracking for the [audioDeviceAttributes]. */
53     suspend fun setHeadTrackingEnabled(
54         audioDeviceAttributes: AudioDeviceAttributes,
55         isEnabled: Boolean,
56     ) = repository.setHeadTrackingEnabled(audioDeviceAttributes, isEnabled)
57 }
58