1 /* 2 * Copyright (C) 2025 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.dagger 18 19 import android.content.Context 20 import android.view.Display 21 import com.android.app.displaylib.DisplayRepository 22 import com.android.systemui.coroutines.newTracingContext 23 import com.android.systemui.dagger.qualifiers.Application 24 import com.android.systemui.dagger.qualifiers.Background 25 import com.android.systemui.display.dagger.SystemUIDisplaySubcomponent.DisplayAware 26 import com.android.systemui.display.dagger.SystemUIDisplaySubcomponent.DisplayId 27 import com.android.systemui.display.dagger.SystemUIDisplaySubcomponent.PerDisplaySingleton 28 import dagger.Module 29 import dagger.Provides 30 import kotlinx.coroutines.CoroutineDispatcher 31 import kotlinx.coroutines.CoroutineScope 32 33 /** Module providing common dependencies for per-display singletons. */ 34 @Module 35 class PerDisplayCommonModule { 36 37 @Provides 38 @PerDisplaySingleton provideDisplaynull39 fun provideDisplay(@DisplayId displayId: Int, displayRepository: DisplayRepository): Display { 40 return displayRepository.getDisplay(displayId) 41 ?: error("Couldn't get the display with id=$displayId") 42 } 43 44 @Provides 45 @PerDisplaySingleton 46 @DisplayAware provideDisplayContextnull47 fun provideDisplayContext( 48 display: Display, 49 @Application context: Context, 50 ): Context { 51 return context.createDisplayContext(display) 52 } 53 54 @Provides 55 @PerDisplaySingleton 56 @DisplayAware provideDisplayCoroutineScopenull57 fun provideDisplayCoroutineScope( 58 @Background backgroundDispatcher: CoroutineDispatcher, 59 @DisplayId displayId: Int, 60 ): CoroutineScope { 61 return CoroutineScope( 62 backgroundDispatcher + newTracingContext("DisplayScope(id=$displayId)") 63 ) 64 } 65 } 66