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.display.domain.interactor
18 
19 import android.view.Display
20 import com.android.systemui.dagger.SysUISingleton
21 import com.android.systemui.display.data.repository.DisplayRepository
22 import com.android.systemui.display.domain.interactor.ConnectedDisplayInteractor.State
23 import javax.inject.Inject
24 import kotlinx.coroutines.flow.Flow
25 import kotlinx.coroutines.flow.distinctUntilChanged
26 import kotlinx.coroutines.flow.map
27 
28 /** Provides information about an external connected display. */
29 interface ConnectedDisplayInteractor {
30     /**
31      * Provides the current external display state.
32      *
33      * The state is:
34      * - [State.CONNECTED] when there is at least one display with [TYPE_EXTERNAL].
35      * - [State.CONNECTED_SECURE] when is at least one display with both [TYPE_EXTERNAL] AND
36      *   [Display.FLAG_SECURE] set
37      */
38     val connectedDisplayState: Flow<State>
39 
40     /** Possible connected display state. */
41     enum class State {
42         DISCONNECTED,
43         CONNECTED,
44         CONNECTED_SECURE,
45     }
46 }
47 
48 @SysUISingleton
49 class ConnectedDisplayInteractorImpl
50 @Inject
51 constructor(
52     displayRepository: DisplayRepository,
53 ) : ConnectedDisplayInteractor {
54 
55     override val connectedDisplayState: Flow<State> =
56         displayRepository.displays
57             .map { displays ->
58                 val externalDisplays =
59                     displays.filter { display -> display.type == Display.TYPE_EXTERNAL }
60 
61                 val secureExternalDisplays =
62                     externalDisplays.filter { it.flags and Display.FLAG_SECURE != 0 }
63 
64                 if (externalDisplays.isEmpty()) {
65                     State.DISCONNECTED
66                 } else if (!secureExternalDisplays.isEmpty()) {
67                     State.CONNECTED_SECURE
68                 } else {
69                     State.CONNECTED
70                 }
71             }
72             .distinctUntilChanged()
73 }
74