1 package com.android.keyguard.dagger
2 
3 import android.content.Context
4 import android.content.res.Resources
5 import android.view.Display
6 import com.android.systemui.dagger.qualifiers.DisplaySpecific
7 import com.android.systemui.util.kotlin.getOrNull
8 import dagger.BindsOptionalOf
9 import dagger.Module
10 import dagger.Provides
11 import java.util.Optional
12 
13 /**
14  * Binds display specific context and resources.
15  *
16  * When a [Display] is available in the scope, binds a [DisplaySpecific] [Context] and [Resources].
17  * When not available, the default display context and resources are used.
18  */
19 @Module
20 abstract class KeyguardDisplayModule {
21 
optionalDisplaynull22     @BindsOptionalOf abstract fun optionalDisplay(): Display
23 
24     companion object {
25         @Provides
26         @DisplaySpecific
27         fun getDisplayContext(context: Context, optionalDisplay: Optional<Display>): Context {
28             val display = optionalDisplay.getOrNull() ?: return context
29             return if (context.displayId == display.displayId) {
30                 context
31             } else {
32                 context.createDisplayContext(display)
33             }
34         }
35 
36         @Provides
37         @DisplaySpecific
38         fun getDisplayResources(@DisplaySpecific context: Context): Resources {
39             return context.resources
40         }
41     }
42 }
43