• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * 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.wallpaper.picker.di.modules
18 
19 import android.app.WallpaperManager
20 import android.content.ContentResolver
21 import android.content.Context
22 import android.content.pm.PackageManager
23 import android.content.res.Resources
24 import android.os.Handler
25 import android.os.HandlerThread
26 import android.os.Looper
27 import android.os.Process
28 import com.android.wallpaper.module.CreativeHelper
29 import com.android.wallpaper.module.DefaultCreativeHelper
30 import com.android.wallpaper.module.DefaultNetworkStatusNotifier
31 import com.android.wallpaper.module.DefaultPackageStatusNotifier
32 import com.android.wallpaper.module.DefaultWallpaperRefresher
33 import com.android.wallpaper.module.LargeScreenMultiPanesChecker
34 import com.android.wallpaper.module.MultiPanesChecker
35 import com.android.wallpaper.module.NetworkStatusNotifier
36 import com.android.wallpaper.module.PackageStatusNotifier
37 import com.android.wallpaper.module.WallpaperRefresher
38 import com.android.wallpaper.network.Requester
39 import com.android.wallpaper.network.WallpaperRequester
40 import com.android.wallpaper.picker.MyPhotosStarter
41 import com.android.wallpaper.picker.category.client.DefaultWallpaperCategoryClient
42 import com.android.wallpaper.picker.category.client.DefaultWallpaperCategoryClientImpl
43 import com.android.wallpaper.picker.category.client.LiveWallpapersClient
44 import com.android.wallpaper.picker.category.client.LiveWallpapersClientImpl
45 import com.android.wallpaper.picker.category.data.repository.DefaultWallpaperCategoryRepository
46 import com.android.wallpaper.picker.category.data.repository.WallpaperCategoryRepository
47 import com.android.wallpaper.picker.category.domain.interactor.MyPhotosInteractor
48 import com.android.wallpaper.picker.category.domain.interactor.implementations.MyPhotosInteractorImpl
49 import com.android.wallpaper.picker.category.ui.view.MyPhotosStarterImpl
50 import com.android.wallpaper.picker.customization.data.content.WallpaperClient
51 import com.android.wallpaper.picker.customization.data.content.WallpaperClientImpl
52 import com.android.wallpaper.picker.network.data.DefaultNetworkStatusRepository
53 import com.android.wallpaper.picker.network.data.NetworkStatusRepository
54 import com.android.wallpaper.picker.network.domain.DefaultNetworkStatusInteractor
55 import com.android.wallpaper.picker.network.domain.NetworkStatusInteractor
56 import com.android.wallpaper.system.PowerManagerImpl
57 import com.android.wallpaper.system.PowerManagerWrapper
58 import com.android.wallpaper.system.UiModeManagerImpl
59 import com.android.wallpaper.system.UiModeManagerWrapper
60 import com.android.wallpaper.util.WallpaperParser
61 import com.android.wallpaper.util.WallpaperParserImpl
62 import com.android.wallpaper.util.converter.category.CategoryFactory
63 import com.android.wallpaper.util.converter.category.DefaultCategoryFactory
64 import dagger.Binds
65 import dagger.Module
66 import dagger.Provides
67 import dagger.hilt.InstallIn
68 import dagger.hilt.android.qualifiers.ApplicationContext
69 import dagger.hilt.components.SingletonComponent
70 import java.util.concurrent.Executor
71 import javax.inject.Qualifier
72 import javax.inject.Singleton
73 import kotlinx.coroutines.CoroutineDispatcher
74 import kotlinx.coroutines.CoroutineScope
75 import kotlinx.coroutines.Dispatchers
76 
77 /** Qualifier for main thread [CoroutineDispatcher] bound to app lifecycle. */
78 @Qualifier annotation class MainDispatcher
79 
80 /** Qualifier for background thread [CoroutineDispatcher] for long running and blocking tasks. */
81 @Qualifier annotation class BackgroundDispatcher
82 
83 @Module
84 @InstallIn(SingletonComponent::class)
85 abstract class SharedAppModule {
86 
87     @Binds
88     @Singleton
89     abstract fun bindCategoryFactory(impl: DefaultCategoryFactory): CategoryFactory
90 
91     @Binds @Singleton abstract fun bindCreativeHelper(impl: DefaultCreativeHelper): CreativeHelper
92 
93     @Binds
94     @Singleton
95     abstract fun bindLiveWallpapersClient(impl: LiveWallpapersClientImpl): LiveWallpapersClient
96 
97     @Binds
98     @Singleton
99     abstract fun bindMyPhotosInteractor(impl: MyPhotosInteractorImpl): MyPhotosInteractor
100 
101     @Binds
102     @Singleton
103     abstract fun bindNetworkStatusRepository(
104         impl: DefaultNetworkStatusRepository
105     ): NetworkStatusRepository
106 
107     @Binds
108     @Singleton
109     abstract fun bindNetworkStatusInteractor(
110         impl: DefaultNetworkStatusInteractor
111     ): NetworkStatusInteractor
112 
113     @Binds
114     @Singleton
115     abstract fun bindNetworkStatusNotifier(
116         impl: DefaultNetworkStatusNotifier
117     ): NetworkStatusNotifier
118 
119     @Binds @Singleton abstract fun bindRequester(impl: WallpaperRequester): Requester
120 
121     @Binds
122     @Singleton
123     abstract fun bindUiModeManagerWrapper(impl: UiModeManagerImpl): UiModeManagerWrapper
124 
125     @Binds
126     @Singleton
127     abstract fun bindPowerManagerWrapper(impl: PowerManagerImpl): PowerManagerWrapper
128 
129     @Binds
130     @Singleton
131     abstract fun bindWallpaperCategoryClient(
132         impl: DefaultWallpaperCategoryClientImpl
133     ): DefaultWallpaperCategoryClient
134 
135     @Binds
136     @Singleton
137     abstract fun bindPackageNotifier(impl: DefaultPackageStatusNotifier): PackageStatusNotifier
138 
139     @Binds
140     @Singleton
141     abstract fun bindWallpaperCategoryRepository(
142         impl: DefaultWallpaperCategoryRepository
143     ): WallpaperCategoryRepository
144 
145     @Binds @Singleton abstract fun bindWallpaperClient(impl: WallpaperClientImpl): WallpaperClient
146 
147     @Binds @Singleton abstract fun bindWallpaperParser(impl: WallpaperParserImpl): WallpaperParser
148 
149     @Binds
150     @Singleton
151     abstract fun bindWallpaperPickerDelegate2(impl: MyPhotosStarterImpl): MyPhotosStarter
152 
153     @Binds
154     @Singleton
155     abstract fun bindWallpaperRefresher(impl: DefaultWallpaperRefresher): WallpaperRefresher
156 
157     companion object {
158 
159         @Qualifier
160         @MustBeDocumented
161         @Retention(AnnotationRetention.RUNTIME)
162         annotation class BroadcastRunning
163 
164         const val BROADCAST_SLOW_DISPATCH_THRESHOLD = 1000L
165         const val BROADCAST_SLOW_DELIVERY_THRESHOLD = 1000L
166 
167         @Provides
168         @BackgroundDispatcher
169         fun provideBackgroundDispatcher(): CoroutineDispatcher = Dispatchers.IO
170 
171         @Provides
172         @BackgroundDispatcher
173         fun provideBackgroundScope(): CoroutineScope = CoroutineScope(Dispatchers.IO)
174 
175         /** Provide a BroadcastRunning Executor (for sending and receiving broadcasts). */
176         @Provides
177         @Singleton
178         @BroadcastRunning
179         fun provideBroadcastRunningExecutor(@BroadcastRunning looper: Looper?): Executor {
180             val handler = Handler(looper ?: Looper.getMainLooper())
181             return Executor { command -> handler.post(command) }
182         }
183 
184         @Provides
185         @Singleton
186         @BroadcastRunning
187         fun provideBroadcastRunningLooper(): Looper {
188             return HandlerThread("BroadcastRunning", Process.THREAD_PRIORITY_BACKGROUND)
189                 .apply {
190                     start()
191                     looper.setSlowLogThresholdMs(
192                         BROADCAST_SLOW_DISPATCH_THRESHOLD,
193                         BROADCAST_SLOW_DELIVERY_THRESHOLD,
194                     )
195                 }
196                 .looper
197         }
198 
199         @Provides
200         @MainDispatcher
201         fun provideMainDispatcher(): CoroutineDispatcher = Dispatchers.Main
202 
203         @Provides
204         @MainDispatcher
205         fun provideMainScope(): CoroutineScope = CoroutineScope(Dispatchers.Main)
206 
207         @Provides
208         @Singleton
209         fun provideMultiPanesChecker(): MultiPanesChecker {
210             return LargeScreenMultiPanesChecker()
211         }
212 
213         @Provides
214         @Singleton
215         fun providePackageManager(@ApplicationContext appContext: Context): PackageManager {
216             return appContext.packageManager
217         }
218 
219         @Provides
220         @Singleton
221         fun provideContentResolver(@ApplicationContext appContext: Context): ContentResolver {
222             return appContext.contentResolver
223         }
224 
225         @Provides
226         @Singleton
227         fun provideResources(@ApplicationContext context: Context): Resources {
228             return context.resources
229         }
230 
231         @Provides
232         @Singleton
233         fun provideWallpaperManager(@ApplicationContext appContext: Context): WallpaperManager {
234             return WallpaperManager.getInstance(appContext)
235         }
236     }
237 }
238