• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
17 
18 package com.android.systemui.keyguard.data.quickaffordance
19 
20 import android.content.Context
21 import android.media.AudioManager
22 import androidx.lifecycle.Observer
23 import com.android.systemui.CoreStartable
24 import com.android.systemui.dagger.SysUISingleton
25 import com.android.systemui.dagger.qualifiers.Application
26 import com.android.systemui.dagger.qualifiers.Background
27 import com.android.systemui.keyguard.data.repository.KeyguardQuickAffordanceRepository
28 import com.android.systemui.settings.UserFileManager
29 import com.android.systemui.settings.UserTracker
30 import com.android.systemui.util.RingerModeTracker
31 import kotlinx.coroutines.CoroutineDispatcher
32 import kotlinx.coroutines.CoroutineScope
33 import kotlinx.coroutines.flow.launchIn
34 import kotlinx.coroutines.flow.map
35 import com.android.app.tracing.coroutines.launchTraced as launch
36 import javax.inject.Inject
37 
38 /**
39  * Store previous non-silent Ringer Mode into shared prefs to be used for Mute Lockscreen Shortcut
40  */
41 @SysUISingleton
42 class MuteQuickAffordanceCoreStartable @Inject constructor(
43     private val userTracker: UserTracker,
44     private val ringerModeTracker: RingerModeTracker,
45     private val userFileManager: UserFileManager,
46     private val keyguardQuickAffordanceRepository: KeyguardQuickAffordanceRepository,
47     @Application private val coroutineScope: CoroutineScope,
48     @Background private val backgroundDispatcher: CoroutineDispatcher,
49 ) : CoreStartable {
50 
51     private val observer = Observer(this::updateLastNonSilentRingerMode)
52 
53     override fun start() {
54         // only listen to ringerModeInternal changes when Mute is one of the selected affordances
55         keyguardQuickAffordanceRepository
56             .selections
57             .map { selections ->
58                 // determines if Mute is selected in any lockscreen shortcut position
59                 val muteSelected: Boolean = selections.values.any { configList ->
60                     configList.any { config ->
61                         config.key == BuiltInKeyguardQuickAffordanceKeys.MUTE
62                     }
63                 }
64                 if (muteSelected) {
65                     ringerModeTracker.ringerModeInternal.observeForever(observer)
66                 } else {
67                     ringerModeTracker.ringerModeInternal.removeObserver(observer)
68                 }
69             }
70             .launchIn(coroutineScope)
71     }
72 
73     private fun updateLastNonSilentRingerMode(lastRingerMode: Int) {
74         coroutineScope.launch(context = backgroundDispatcher) {
75             if (AudioManager.RINGER_MODE_SILENT != lastRingerMode) {
76                 userFileManager.getSharedPreferences(
77                         MuteQuickAffordanceConfig.MUTE_QUICK_AFFORDANCE_PREFS_FILE_NAME,
78                         Context.MODE_PRIVATE,
79                         userTracker.userId
80                 )
81                 .edit()
82                 .putInt(MuteQuickAffordanceConfig.LAST_NON_SILENT_RINGER_MODE_KEY, lastRingerMode)
83                 .apply()
84             }
85         }
86     }
87 }