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