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.wallpaper.system
18 
19 import android.app.UiModeManager
20 import android.content.Context
21 import android.content.res.Configuration.UI_MODE_NIGHT_MASK
22 import android.content.res.Configuration.UI_MODE_NIGHT_YES
23 import dagger.hilt.android.qualifiers.ApplicationContext
24 import java.util.concurrent.Executor
25 import javax.inject.Inject
26 import javax.inject.Singleton
27 
28 @Singleton
29 class UiModeManagerImpl @Inject constructor(@ApplicationContext private val context: Context) :
30     UiModeManagerWrapper {
31     val uiModeManager = context.getSystemService(Context.UI_MODE_SERVICE) as UiModeManager?
32 
addContrastChangeListenernull33     override fun addContrastChangeListener(
34         executor: Executor,
35         listener: UiModeManager.ContrastChangeListener,
36     ) {
37         uiModeManager?.addContrastChangeListener(executor, listener)
38     }
39 
removeContrastChangeListenernull40     override fun removeContrastChangeListener(listener: UiModeManager.ContrastChangeListener) {
41         uiModeManager?.removeContrastChangeListener(listener)
42     }
43 
getContrastnull44     override fun getContrast(): Float? {
45         return uiModeManager?.contrast
46     }
47 
getIsNightModeActivatednull48     override fun getIsNightModeActivated(): Boolean {
49         return context.resources.configuration.uiMode and UI_MODE_NIGHT_MASK == UI_MODE_NIGHT_YES
50     }
51 
setNightModeActivatednull52     override fun setNightModeActivated(isActive: Boolean) {
53         uiModeManager?.setNightModeActivated(isActive)
54     }
55 }
56