1 /* 2 * Copyright (C) 2020 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.controls.dagger 18 19 import android.content.ContentResolver 20 import android.content.Context 21 import android.database.ContentObserver 22 import android.provider.Settings 23 import com.android.systemui.controls.controller.ControlsController 24 import com.android.systemui.controls.management.ControlsListingController 25 import com.android.systemui.controls.ui.ControlsUiController 26 import com.android.systemui.dagger.SysUISingleton 27 import com.android.systemui.settings.UserTracker 28 import com.android.systemui.statusbar.policy.KeyguardStateController 29 import com.android.systemui.util.settings.SecureSettings 30 import com.android.internal.widget.LockPatternUtils 31 import com.android.internal.widget.LockPatternUtils.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_BOOT 32 import dagger.Lazy 33 import java.util.Optional 34 import javax.inject.Inject 35 36 /** 37 * Pseudo-component to inject into classes outside `com.android.systemui.controls`. 38 * 39 * If `featureEnabled` is false, all the optionals should be empty. The controllers will only be 40 * instantiated if `featureEnabled` is true. Can also be queried for the availability of controls. 41 */ 42 @SysUISingleton 43 class ControlsComponent @Inject constructor( 44 @ControlsFeatureEnabled private val featureEnabled: Boolean, 45 private val context: Context, 46 private val lazyControlsController: Lazy<ControlsController>, 47 private val lazyControlsUiController: Lazy<ControlsUiController>, 48 private val lazyControlsListingController: Lazy<ControlsListingController>, 49 private val lockPatternUtils: LockPatternUtils, 50 private val keyguardStateController: KeyguardStateController, 51 private val userTracker: UserTracker, 52 private val secureSettings: SecureSettings 53 ) { 54 private val contentResolver: ContentResolver 55 get() = context.contentResolver 56 57 private var canShowWhileLockedSetting = false 58 59 val showWhileLockedObserver = object : ContentObserver(null) { onChangenull60 override fun onChange(selfChange: Boolean) { 61 updateShowWhileLocked() 62 } 63 } 64 <lambda>null65 init { 66 if (featureEnabled) { 67 secureSettings.registerContentObserver( 68 Settings.Secure.getUriFor(Settings.Secure.LOCKSCREEN_SHOW_CONTROLS), 69 false, /* notifyForDescendants */ 70 showWhileLockedObserver 71 ) 72 updateShowWhileLocked() 73 } 74 } 75 getControlsControllernull76 fun getControlsController(): Optional<ControlsController> { 77 return if (featureEnabled) Optional.of(lazyControlsController.get()) else Optional.empty() 78 } 79 getControlsUiControllernull80 fun getControlsUiController(): Optional<ControlsUiController> { 81 return if (featureEnabled) Optional.of(lazyControlsUiController.get()) else Optional.empty() 82 } 83 getControlsListingControllernull84 fun getControlsListingController(): Optional<ControlsListingController> { 85 return if (featureEnabled) { 86 Optional.of(lazyControlsListingController.get()) 87 } else { 88 Optional.empty() 89 } 90 } 91 92 /** 93 * @return true if controls are feature-enabled and the user has the setting enabled 94 */ isEnablednull95 fun isEnabled() = featureEnabled 96 97 /** 98 * Returns one of 3 states: 99 * * AVAILABLE - Controls can be made visible 100 * * AVAILABLE_AFTER_UNLOCK - Controls can be made visible only after device unlock 101 * * UNAVAILABLE - Controls are not enabled 102 */ 103 fun getVisibility(): Visibility { 104 if (!isEnabled()) return Visibility.UNAVAILABLE 105 if (lockPatternUtils.getStrongAuthForUser(userTracker.userHandle.identifier) 106 == STRONG_AUTH_REQUIRED_AFTER_BOOT) { 107 return Visibility.AVAILABLE_AFTER_UNLOCK 108 } 109 if (!canShowWhileLockedSetting && !keyguardStateController.isUnlocked()) { 110 return Visibility.AVAILABLE_AFTER_UNLOCK 111 } 112 113 return Visibility.AVAILABLE 114 } 115 updateShowWhileLockednull116 private fun updateShowWhileLocked() { 117 canShowWhileLockedSetting = secureSettings.getInt( 118 Settings.Secure.LOCKSCREEN_SHOW_CONTROLS, 0) != 0 119 } 120 121 enum class Visibility { 122 AVAILABLE, AVAILABLE_AFTER_UNLOCK, UNAVAILABLE 123 } 124 } 125