1 /* <lambda>null2 * 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.systemui.volume 18 19 import android.media.IVolumeController 20 import com.android.settingslib.volume.data.model.VolumeControllerEvent 21 import com.android.settingslib.volume.data.repository.AudioRepository 22 import com.android.systemui.dagger.qualifiers.Application 23 import javax.inject.Inject 24 import kotlinx.coroutines.CoroutineScope 25 import kotlinx.coroutines.flow.Flow 26 import com.android.app.tracing.coroutines.launchTraced as launch 27 28 /** 29 * This class is a bridge between 30 * [com.android.settingslib.volume.data.repository.AudioRepository.volumeControllerEvents] and the 31 * old code that uses [IVolumeController] interface directly. 32 */ 33 class VolumeControllerAdapter 34 @Inject 35 constructor( 36 @Application private val coroutineScope: CoroutineScope, 37 private val audioRepository: AudioRepository, 38 ) { 39 40 /** Collects [Flow] of [VolumeControllerEvent] into [IVolumeController]. */ 41 fun collectToController(controller: IVolumeController) { 42 coroutineScope.launch { 43 audioRepository.volumeControllerEvents.collect { event -> 44 when (event) { 45 is VolumeControllerEvent.VolumeChanged -> 46 controller.volumeChanged(event.streamType, event.flags) 47 VolumeControllerEvent.Dismiss -> controller.dismiss() 48 is VolumeControllerEvent.DisplayCsdWarning -> 49 controller.displayCsdWarning(event.csdWarning, event.displayDurationMs) 50 is VolumeControllerEvent.DisplaySafeVolumeWarning -> 51 controller.displaySafeVolumeWarning(event.flags) 52 is VolumeControllerEvent.MasterMuteChanged -> 53 controller.masterMuteChanged(event.flags) 54 is VolumeControllerEvent.SetA11yMode -> controller.setA11yMode(event.mode) 55 is VolumeControllerEvent.SetLayoutDirection -> 56 controller.setLayoutDirection(event.layoutDirection) 57 } 58 } 59 } 60 } 61 62 fun notifyVolumeControllerVisible(isVisible: Boolean) { 63 coroutineScope.launch { audioRepository.notifyVolumeControllerVisible(isVisible) } 64 } 65 } 66