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.car.customization.tool.di
18 
19 import android.accessibilityservice.AccessibilityService
20 import android.content.Context
21 import android.content.om.OverlayManager
22 import android.content.pm.PackageManager
23 import android.hardware.display.DisplayManager
24 import android.view.ContextThemeWrapper
25 import android.view.Display
26 import android.view.LayoutInflater
27 import android.view.WindowManager
28 import com.android.car.customization.tool.R
29 import dagger.Module
30 import dagger.Provides
31 import javax.inject.Qualifier
32 import javax.inject.Singleton
33 
34 /**
35  * Qualifier used to provide a UI valid [Context].
36  */
37 @Qualifier
38 @Retention(AnnotationRetention.BINARY)
39 internal annotation class UIContext
40 
41 @Module
42 internal class ServiceModule {
43 
44     /**
45      * Provides a [Context] configured for UI operations since the standard [Context] provided by
46      * an [AccessibilityService] is not.
47      */
48     @Provides
49     @UIContext
50     @Singleton
provideUiContextnull51     fun provideUiContext(service: AccessibilityService): Context {
52         val displayManager = service.getSystemService(DisplayManager::class.java) as DisplayManager
53         val primaryDisplay = displayManager.getDisplay(Display.DEFAULT_DISPLAY)
54         val baseContext = service.createWindowContext(
55             primaryDisplay,
56             WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY,
57             /* options= */ null
58         )
59         return ContextThemeWrapper(baseContext, R.style.BaseTheme)
60     }
61 
62     @Provides
provideWindowManagernull63     fun provideWindowManager(@UIContext context: Context): WindowManager =
64         context.getSystemService(AccessibilityService.WINDOW_SERVICE) as WindowManager
65 
66     @Provides
67     fun provideLayoutInflater(@UIContext context: Context): LayoutInflater =
68         context.getSystemService(AccessibilityService.LAYOUT_INFLATER_SERVICE) as LayoutInflater
69 
70     @Provides
71     fun providePackageManager(@UIContext context: Context): PackageManager =
72         context.packageManager
73 
74     @Provides
75     @Singleton
76     fun provideOverlayManager(@UIContext context: Context): OverlayManager =
77         context.getSystemService(android.content.om.OverlayManager::class.java) as OverlayManager
78 }
79