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