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