• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * 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.settings.display.darkmode
18 
19 import android.app.UiModeManager
20 import android.content.BroadcastReceiver
21 import android.content.Context
22 import android.content.Intent
23 import android.content.IntentFilter
24 import android.content.res.Configuration
25 import android.os.PowerManager
26 import com.android.settingslib.datastore.AbstractKeyedDataObservable
27 import com.android.settingslib.datastore.KeyValueStore
28 import com.android.settingslib.metadata.PreferenceChangeReason
29 
30 /**
31  * Abstract storage for dark mode settings.
32  *
33  * The underlying storage is manipulated by [UiModeManager] but we do not need to worry about the
34  * details.
35  */
36 @Suppress("UNCHECKED_CAST")
37 internal class DarkModeStorage(private val context: Context) :
38     AbstractKeyedDataObservable<String>(), KeyValueStore {
39     private lateinit var broadcastReceiver: BroadcastReceiver
40     private lateinit var darkModeObserver: DarkModeObserver
41 
containsnull42     override fun contains(key: String) = true
43 
44     override fun <T : Any> getValue(key: String, valueType: Class<T>) = context.isDarkMode() as T
45 
46     private fun Context.isDarkMode() =
47         (resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_YES) != 0
48 
49     override fun <T : Any> setValue(key: String, valueType: Class<T>, value: T?) {
50         context.getSystemService(UiModeManager::class.java)?.setNightModeActivated(value as Boolean)
51     }
52 
onFirstObserverAddednull53     override fun onFirstObserverAdded() {
54         broadcastReceiver =
55             object : BroadcastReceiver() {
56                 override fun onReceive(context: Context, intent: Intent) {
57                     notifyChange(PreferenceChangeReason.STATE)
58                 }
59             }
60         context.registerReceiver(
61             broadcastReceiver,
62             IntentFilter(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED),
63         )
64 
65         darkModeObserver = DarkModeObserver(context)
66         darkModeObserver.subscribe { notifyChange(PreferenceChangeReason.VALUE) }
67     }
68 
onLastObserverRemovednull69     override fun onLastObserverRemoved() {
70         context.unregisterReceiver(broadcastReceiver)
71         darkModeObserver.unsubscribe()
72     }
73 }
74