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.systemui.qs.tiles.impl.sensorprivacy.domain
18 
19 import android.content.Intent
20 import android.hardware.SensorPrivacyManager.Sensors.Sensor
21 import android.hardware.SensorPrivacyManager.Sources.QS_TILE
22 import android.provider.Settings
23 import android.safetycenter.SafetyCenterManager
24 import com.android.systemui.keyguard.domain.interactor.KeyguardInteractor
25 import com.android.systemui.plugins.ActivityStarter
26 import com.android.systemui.qs.tiles.base.actions.QSTileIntentUserInputHandler
27 import com.android.systemui.qs.tiles.base.interactor.QSTileInput
28 import com.android.systemui.qs.tiles.base.interactor.QSTileUserActionInteractor
29 import com.android.systemui.qs.tiles.impl.sensorprivacy.domain.model.SensorPrivacyToggleTileModel
30 import com.android.systemui.qs.tiles.viewmodel.QSTileUserAction
31 import com.android.systemui.statusbar.policy.IndividualSensorPrivacyController
32 import dagger.assisted.Assisted
33 import dagger.assisted.AssistedFactory
34 import dagger.assisted.AssistedInject
35 
36 /** Handles sensor privacy toggle tile clicks and long clicks. */
37 class SensorPrivacyToggleTileUserActionInteractor
38 @AssistedInject
39 constructor(
40     private val qsTileIntentUserActionHandler: QSTileIntentUserInputHandler,
41     private val keyguardInteractor: KeyguardInteractor,
42     private val activityStarter: ActivityStarter,
43     private val sensorPrivacyController: IndividualSensorPrivacyController,
44     private val safetyCenterManager: SafetyCenterManager,
45     @Assisted @Sensor private val sensorId: Int,
46 ) : QSTileUserActionInteractor<SensorPrivacyToggleTileModel> {
47     @AssistedFactory
48     interface Factory {
createnull49         fun create(@Sensor id: Int): SensorPrivacyToggleTileUserActionInteractor
50     }
51 
52     // should only be initialized in code known to run in background thread
53     private lateinit var longClickIntent: Intent
54 
55     override suspend fun handleInput(input: QSTileInput<SensorPrivacyToggleTileModel>) =
56         with(input) {
57             when (action) {
58                 is QSTileUserAction.Click -> {
59                     val blocked = input.data.isBlocked
60                     if (
61                         sensorPrivacyController.requiresAuthentication() &&
62                             keyguardInteractor.isKeyguardDismissible.value &&
63                             keyguardInteractor.isKeyguardShowing()
64                     ) {
65                         activityStarter.postQSRunnableDismissingKeyguard {
66                             sensorPrivacyController.setSensorBlocked(QS_TILE, sensorId, !blocked)
67                         }
68                         return
69                     }
70                     sensorPrivacyController.setSensorBlocked(QS_TILE, sensorId, !blocked)
71                 }
72                 is QSTileUserAction.LongClick -> {
73                     if (!::longClickIntent.isInitialized) {
74                         longClickIntent =
75                             Intent(
76                                 if (safetyCenterManager.isSafetyCenterEnabled) {
77                                     Settings.ACTION_PRIVACY_CONTROLS
78                                 } else {
79                                     Settings.ACTION_PRIVACY_SETTINGS
80                                 }
81                             )
82                     }
83                     qsTileIntentUserActionHandler.handle(action.expandable, longClickIntent)
84                 }
85             }
86         }
87 }
88