• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
18 package com.android.systemui.keyboard.backlight.ui.viewmodel
19 
20 import android.view.accessibility.AccessibilityManager
21 import com.android.systemui.dagger.SysUISingleton
22 import com.android.systemui.keyboard.backlight.domain.interactor.KeyboardBacklightInteractor
23 import com.android.systemui.statusbar.policy.AccessibilityManagerWrapper
24 import javax.inject.Inject
25 import kotlinx.coroutines.delay
26 import kotlinx.coroutines.flow.Flow
27 import kotlinx.coroutines.flow.filterNotNull
28 import kotlinx.coroutines.flow.flatMapLatest
29 import kotlinx.coroutines.flow.flow
30 import kotlinx.coroutines.flow.map
31 
32 /**
33  * Responsible for dialog visibility and content - emits [BacklightDialogContentViewModel] if dialog
34  * should be shown and hidden otherwise
35  */
36 @SysUISingleton
37 class BacklightDialogViewModel
38 @Inject
39 constructor(
40     interactor: KeyboardBacklightInteractor,
41     private val accessibilityManagerWrapper: AccessibilityManagerWrapper,
42 ) {
43 
44     private val timeoutMillis: Long
45         get() =
46             accessibilityManagerWrapper
47                 .getRecommendedTimeoutMillis(
48                     DEFAULT_DIALOG_TIMEOUT_MILLIS,
49                     AccessibilityManager.FLAG_CONTENT_ICONS
50                 )
51                 .toLong()
52 
53     val dialogContent: Flow<BacklightDialogContentViewModel?> =
54         interactor.backlight
55             .filterNotNull()
<lambda>null56             .map { BacklightDialogContentViewModel(it.level, it.maxLevel) }
57             .timeout(timeoutMillis, emitAfterTimeout = null)
58 
timeoutnull59     private fun <T> Flow<T>.timeout(timeoutMillis: Long, emitAfterTimeout: T): Flow<T> {
60         return flatMapLatest {
61             flow {
62                 emit(it)
63                 delay(timeoutMillis)
64                 emit(emitAfterTimeout)
65             }
66         }
67     }
68 
69     private companion object {
70         const val DEFAULT_DIALOG_TIMEOUT_MILLIS = 3000
71     }
72 }
73