• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 package com.android.systemui.lowlightclock
17 
18 import android.content.res.Resources
19 import android.database.ContentObserver
20 import android.os.UserHandle
21 import android.provider.Settings
22 import android.util.Log
23 import com.android.internal.R
24 import com.android.systemui.dagger.qualifiers.Background
25 import com.android.systemui.dagger.qualifiers.Main
26 import com.android.systemui.shared.condition.Condition
27 import com.android.systemui.util.settings.SecureSettings
28 import javax.inject.Inject
29 import kotlinx.coroutines.CoroutineScope
30 
31 /** Condition for monitoring if the screensaver setting is enabled. */
32 class ScreenSaverEnabledCondition
33 @Inject
34 constructor(
35     @Background scope: CoroutineScope,
36     @Main resources: Resources,
37     private val secureSettings: SecureSettings,
38 ) : Condition(scope) {
39     private val screenSaverEnabledByDefaultConfig =
40         resources.getBoolean(R.bool.config_dreamsEnabledByDefault)
41 
42     private val screenSaverSettingObserver: ContentObserver =
43         object : ContentObserver(null) {
onChangenull44             override fun onChange(selfChange: Boolean) {
45                 updateScreenSaverEnabledSetting()
46             }
47         }
48 
startnull49     public override suspend fun start() {
50         secureSettings.registerContentObserverForUserSync(
51             Settings.Secure.SCREENSAVER_ENABLED,
52             screenSaverSettingObserver,
53             UserHandle.USER_CURRENT,
54         )
55         updateScreenSaverEnabledSetting()
56     }
57 
stopnull58     override fun stop() {
59         secureSettings.unregisterContentObserverSync(screenSaverSettingObserver)
60     }
61 
62     override val startStrategy: Int
63         get() = START_EAGERLY
64 
updateScreenSaverEnabledSettingnull65     private fun updateScreenSaverEnabledSetting() {
66         val enabled =
67             secureSettings.getIntForUser(
68                 Settings.Secure.SCREENSAVER_ENABLED,
69                 if (screenSaverEnabledByDefaultConfig) 1 else 0,
70                 UserHandle.USER_CURRENT,
71             ) != 0
72         if (!enabled) {
73             Log.i(TAG, "Disabling low-light clock because screen saver has been disabled")
74         }
75         updateCondition(enabled)
76     }
77 
78     companion object {
79         private val TAG: String = ScreenSaverEnabledCondition::class.java.simpleName
80     }
81 }
82