• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.dialog.ringer.domain
18 
19 import android.media.AudioManager
20 import android.media.AudioManager.RINGER_MODE_NORMAL
21 import android.media.AudioManager.RINGER_MODE_SILENT
22 import android.media.AudioManager.RINGER_MODE_VIBRATE
23 import com.android.settingslib.volume.data.repository.AudioSystemRepository
24 import com.android.settingslib.volume.shared.model.RingerMode
25 import com.android.systemui.plugins.VolumeDialogController
26 import com.android.systemui.volume.dialog.dagger.scope.VolumeDialog
27 import com.android.systemui.volume.dialog.domain.interactor.VolumeDialogStateInteractor
28 import com.android.systemui.volume.dialog.ringer.data.repository.VolumeDialogRingerFeedbackRepository
29 import com.android.systemui.volume.dialog.ringer.shared.model.VolumeDialogRingerModel
30 import com.android.systemui.volume.dialog.shared.model.VolumeDialogStateModel
31 import javax.inject.Inject
32 import kotlinx.coroutines.CoroutineScope
33 import kotlinx.coroutines.flow.Flow
34 import kotlinx.coroutines.flow.SharingStarted
35 import kotlinx.coroutines.flow.filterNotNull
36 import kotlinx.coroutines.flow.mapNotNull
37 import kotlinx.coroutines.flow.stateIn
38 
39 /** Exposes [VolumeDialogRingerModel]. */
40 @VolumeDialog
41 class VolumeDialogRingerInteractor
42 @Inject
43 constructor(
44     @VolumeDialog private val coroutineScope: CoroutineScope,
45     volumeDialogStateInteractor: VolumeDialogStateInteractor,
46     private val controller: VolumeDialogController,
47     private val audioSystemRepository: AudioSystemRepository,
48     private val ringerFeedbackRepository: VolumeDialogRingerFeedbackRepository,
49 ) {
50     val ringerModel: Flow<VolumeDialogRingerModel> =
51         volumeDialogStateInteractor.volumeDialogState
52             .mapNotNull { toRingerModel(it) }
53             .stateIn(coroutineScope, SharingStarted.Eagerly, null)
54             .filterNotNull()
55 
56     private fun toRingerModel(state: VolumeDialogStateModel): VolumeDialogRingerModel? {
57         return state.streamModels[AudioManager.STREAM_RING]?.let {
58             VolumeDialogRingerModel(
59                 availableModes =
60                     mutableListOf(RingerMode(RINGER_MODE_NORMAL), RingerMode(RINGER_MODE_SILENT))
61                         .also { list ->
62                             if (controller.hasVibrator()) {
63                                 list.add(RingerMode(RINGER_MODE_VIBRATE))
64                             }
65                         },
66                 currentRingerMode = RingerMode(state.ringerModeInternal),
67                 isMuted = it.level == 0 || it.muted,
68                 level = it.level,
69                 levelMax = it.levelMax,
70                 isSingleVolume = audioSystemRepository.isSingleVolume,
71             )
72         }
73     }
74 
75     fun setRingerMode(ringerMode: RingerMode) {
76         controller.setRingerMode(ringerMode.value, false)
77     }
78 
79     fun scheduleTouchFeedback() {
80         controller.scheduleTouchFeedback()
81     }
82 
83     suspend fun getToastCount(): Int {
84         return ringerFeedbackRepository.getToastCount()
85     }
86 
87     suspend fun updateToastCount(toastCount: Int) {
88         ringerFeedbackRepository.updateToastCount(toastCount)
89     }
90 }
91