1 /*
2  * 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 package com.android.systemui.qs.tiles.impl.uimodenight.domain.interactor
18 
19 import android.app.UiModeManager
20 import android.content.Intent
21 import android.provider.Settings
22 import com.android.systemui.dagger.qualifiers.Background
23 import com.android.systemui.qs.tiles.base.actions.QSTileIntentUserInputHandler
24 import com.android.systemui.qs.tiles.base.interactor.QSTileInput
25 import com.android.systemui.qs.tiles.base.interactor.QSTileUserActionInteractor
26 import com.android.systemui.qs.tiles.impl.uimodenight.domain.model.UiModeNightTileModel
27 import com.android.systemui.qs.tiles.viewmodel.QSTileUserAction
28 import javax.inject.Inject
29 import kotlin.coroutines.CoroutineContext
30 import kotlinx.coroutines.withContext
31 
32 /** Handles ui mode night tile clicks. */
33 class UiModeNightTileUserActionInteractor
34 @Inject
35 constructor(
36     @Background private val backgroundContext: CoroutineContext,
37     private val uiModeManager: UiModeManager,
38     private val qsTileIntentUserActionHandler: QSTileIntentUserInputHandler,
39 ) : QSTileUserActionInteractor<UiModeNightTileModel> {
40 
handleInputnull41     override suspend fun handleInput(input: QSTileInput<UiModeNightTileModel>) =
42         with(input) {
43             when (action) {
44                 is QSTileUserAction.Click -> {
45                     if (!input.data.isPowerSave) {
46                         withContext(backgroundContext) {
47                             uiModeManager.setNightModeActivated(!input.data.isNightMode)
48                         }
49                     }
50                 }
51                 is QSTileUserAction.LongClick -> {
52                     qsTileIntentUserActionHandler.handle(
53                         action.expandable,
54                         Intent(Settings.ACTION_DARK_THEME_SETTINGS)
55                     )
56                 }
57             }
58         }
59 }
60