1 /* <lambda>null2 * 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.shade.display 18 19 import android.view.Display 20 import android.view.Display.DEFAULT_DISPLAY 21 import com.android.systemui.dagger.SysUISingleton 22 import com.android.systemui.dagger.qualifiers.Background 23 import com.android.systemui.display.data.repository.DisplayRepository 24 import javax.inject.Inject 25 import kotlinx.coroutines.CoroutineScope 26 import kotlinx.coroutines.flow.SharingStarted 27 import kotlinx.coroutines.flow.StateFlow 28 import kotlinx.coroutines.flow.map 29 import kotlinx.coroutines.flow.stateIn 30 31 /** 32 * Returns an external display if one exists, otherwise the default display. 33 * 34 * If there are multiple external displays, the one with minimum display ID is returned. 35 */ 36 @SysUISingleton 37 class AnyExternalShadeDisplayPolicy 38 @Inject 39 constructor(displayRepository: DisplayRepository, @Background bgScope: CoroutineScope) : 40 ShadeDisplayPolicy { 41 override val name: String 42 get() = "any_external_display" 43 44 override val displayId: StateFlow<Int> = 45 displayRepository.displays 46 .map { displays -> 47 displays 48 .filter { it.displayId != DEFAULT_DISPLAY && it.type in ALLOWED_DISPLAY_TYPES } 49 .minOfOrNull { it.displayId } ?: DEFAULT_DISPLAY 50 } 51 .stateIn(bgScope, SharingStarted.WhileSubscribed(), DEFAULT_DISPLAY) 52 53 private companion object { 54 val ALLOWED_DISPLAY_TYPES = 55 setOf(Display.TYPE_EXTERNAL, Display.TYPE_OVERLAY, Display.TYPE_WIFI) 56 } 57 } 58