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.location.domain.interactor
18 
19 import android.content.Intent
20 import android.provider.Settings
21 import com.android.systemui.dagger.qualifiers.Application
22 import com.android.systemui.dagger.qualifiers.Background
23 import com.android.systemui.plugins.ActivityStarter
24 import com.android.systemui.qs.tiles.base.actions.QSTileIntentUserInputHandler
25 import com.android.systemui.qs.tiles.base.interactor.QSTileInput
26 import com.android.systemui.qs.tiles.base.interactor.QSTileUserActionInteractor
27 import com.android.systemui.qs.tiles.impl.location.domain.model.LocationTileModel
28 import com.android.systemui.qs.tiles.viewmodel.QSTileUserAction
29 import com.android.systemui.statusbar.policy.KeyguardStateController
30 import com.android.systemui.statusbar.policy.LocationController
31 import javax.inject.Inject
32 import kotlin.coroutines.CoroutineContext
33 import kotlinx.coroutines.CoroutineScope
34 import kotlinx.coroutines.launch
35 import kotlinx.coroutines.withContext
36 
37 /** Handles location tile clicks. */
38 class LocationTileUserActionInteractor
39 @Inject
40 constructor(
41     @Background private val coroutineContext: CoroutineContext,
42     @Application private val applicationScope: CoroutineScope,
43     private val locationController: LocationController,
44     private val qsTileIntentUserActionHandler: QSTileIntentUserInputHandler,
45     private val activityStarter: ActivityStarter,
46     private val keyguardController: KeyguardStateController,
47 ) : QSTileUserActionInteractor<LocationTileModel> {
handleInputnull48     override suspend fun handleInput(input: QSTileInput<LocationTileModel>): Unit =
49         with(input) {
50             when (action) {
51                 is QSTileUserAction.Click -> {
52                     val wasEnabled: Boolean = input.data.isEnabled
53                     if (keyguardController.isMethodSecure() && keyguardController.isShowing()) {
54                         activityStarter.postQSRunnableDismissingKeyguard {
55                             CoroutineScope(applicationScope.coroutineContext).launch {
56                                 locationController.setLocationEnabled(!wasEnabled)
57                             }
58                         }
59                     } else {
60                         withContext(coroutineContext) {
61                             locationController.setLocationEnabled(!wasEnabled)
62                         }
63                     }
64                 }
65                 is QSTileUserAction.LongClick -> {
66                     qsTileIntentUserActionHandler.handle(
67                         action.expandable,
68                         Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)
69                     )
70                 }
71             }
72         }
73 }
74