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.lowlightclock 18 19 import android.content.Intent 20 import android.content.IntentFilter 21 import android.os.UserManager 22 import com.android.systemui.broadcast.BroadcastDispatcher 23 import com.android.systemui.dagger.qualifiers.Background 24 import com.android.systemui.shared.condition.Condition 25 import javax.inject.Inject 26 import kotlinx.coroutines.CoroutineScope 27 import kotlinx.coroutines.Job 28 import kotlinx.coroutines.flow.cancellable 29 import kotlinx.coroutines.flow.distinctUntilChanged 30 import kotlinx.coroutines.flow.map 31 import kotlinx.coroutines.launch 32 33 class DirectBootCondition 34 @Inject 35 constructor( 36 broadcastDispatcher: BroadcastDispatcher, 37 private val userManager: UserManager, 38 @Background private val coroutineScope: CoroutineScope, 39 ) : Condition(coroutineScope) { 40 private var job: Job? = null 41 private val directBootFlow = 42 broadcastDispatcher 43 .broadcastFlow(IntentFilter(Intent.ACTION_USER_UNLOCKED)) <lambda>null44 .map { !userManager.isUserUnlocked } 45 .cancellable() 46 .distinctUntilChanged() 47 startnull48 override suspend fun start() { 49 job = coroutineScope.launch { directBootFlow.collect { updateCondition(it) } } 50 updateCondition(!userManager.isUserUnlocked) 51 } 52 stopnull53 override fun stop() { 54 job?.cancel() 55 } 56 57 override val startStrategy: Int 58 get() = START_EAGERLY 59 } 60