1 /*
2  * 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 package com.android.intentresolver.inject
17 
18 import android.app.ActivityManager
19 import android.app.admin.DevicePolicyManager
20 import android.app.prediction.AppPredictionManager
21 import android.content.ClipboardManager
22 import android.content.ContentInterface
23 import android.content.ContentResolver
24 import android.content.Context
25 import android.content.pm.LauncherApps
26 import android.content.pm.ShortcutManager
27 import android.os.UserManager
28 import android.view.WindowManager
29 import androidx.core.content.getSystemService
30 import com.android.intentresolver.data.repository.UserScopedService
31 import com.android.intentresolver.data.repository.UserScopedServiceImpl
32 import dagger.Binds
33 import dagger.Module
34 import dagger.Provides
35 import dagger.hilt.InstallIn
36 import dagger.hilt.android.qualifiers.ApplicationContext
37 import dagger.hilt.components.SingletonComponent
38 
requireSystemServicenull39 inline fun <reified T> Context.requireSystemService(): T {
40     return checkNotNull(getSystemService())
41 }
42 
43 @Module
44 @InstallIn(SingletonComponent::class)
45 class ActivityManagerModule {
46     @Provides
activityManagernull47     fun activityManager(@ApplicationContext ctx: Context): ActivityManager =
48         ctx.requireSystemService()
49 }
50 
51 @Module
52 @InstallIn(SingletonComponent::class)
53 class ClipboardManagerModule {
54     @Provides
55     fun clipboardManager(@ApplicationContext ctx: Context): ClipboardManager =
56         ctx.requireSystemService()
57 }
58 
59 @Module
60 @InstallIn(SingletonComponent::class)
61 interface ContentResolverModule {
bindContentInterfacenull62     @Binds fun bindContentInterface(cr: ContentResolver): ContentInterface
63 
64     companion object {
65         @Provides
66         fun contentResolver(@ApplicationContext ctx: Context) = requireNotNull(ctx.contentResolver)
67     }
68 }
69 
70 @Module
71 @InstallIn(SingletonComponent::class)
72 class DevicePolicyManagerModule {
73     @Provides
devicePolicyManagernull74     fun devicePolicyManager(@ApplicationContext ctx: Context): DevicePolicyManager =
75         ctx.requireSystemService()
76 }
77 
78 @Module
79 @InstallIn(SingletonComponent::class)
80 class LauncherAppsModule {
81     @Provides
82     fun launcherApps(@ApplicationContext ctx: Context): LauncherApps = ctx.requireSystemService()
83 }
84 
85 @Module
86 @InstallIn(SingletonComponent::class)
87 class PackageManagerModule {
88     @Provides
packageManagernull89     fun packageManager(@ApplicationContext ctx: Context) = requireNotNull(ctx.packageManager)
90 }
91 
92 @Module
93 @InstallIn(SingletonComponent::class)
94 class PredictionManagerModule {
95     @Provides
96     fun scopedPredictionManager(
97         @ApplicationContext ctx: Context,
98     ): UserScopedService<AppPredictionManager> {
99         return UserScopedServiceImpl(ctx, AppPredictionManager::class)
100     }
101 }
102 
103 @Module
104 @InstallIn(SingletonComponent::class)
105 class ShortcutManagerModule {
106     @Provides
shortcutManagernull107     fun shortcutManager(@ApplicationContext ctx: Context): ShortcutManager {
108         return ctx.requireSystemService()
109     }
110 
111     @Provides
scopedShortcutManagernull112     fun scopedShortcutManager(
113         @ApplicationContext ctx: Context,
114     ): UserScopedService<ShortcutManager> {
115         return UserScopedServiceImpl(ctx, ShortcutManager::class)
116     }
117 }
118 
119 @Module
120 @InstallIn(SingletonComponent::class)
121 class UserManagerModule {
122     @Provides
userManagernull123     fun userManager(@ApplicationContext ctx: Context): UserManager = ctx.requireSystemService()
124 
125     @Provides
126     fun scopedUserManager(@ApplicationContext ctx: Context): UserScopedService<UserManager> {
127         return UserScopedServiceImpl(ctx, UserManager::class)
128     }
129 }
130 
131 @Module
132 @InstallIn(SingletonComponent::class)
133 class WindowManagerModule {
134     @Provides
windowManagernull135     fun windowManager(@ApplicationContext ctx: Context): WindowManager = ctx.requireSystemService()
136 }
137