1 package com.android.systemui.util.kotlin
2 
3 import com.android.systemui.dagger.SysUISingleton
4 import com.android.systemui.dagger.qualifiers.Application
5 import com.android.systemui.dagger.qualifiers.Background
6 import com.android.systemui.dagger.qualifiers.Main
7 import dagger.Module
8 import dagger.Provides
9 import kotlinx.coroutines.CoroutineDispatcher
10 import kotlinx.coroutines.CoroutineScope
11 import kotlinx.coroutines.Dispatchers
12 
13 /** Providers for various coroutines-related constructs. */
14 @Module
15 object CoroutinesModule {
16     @Provides
17     @SysUISingleton
18     @Application
19     fun applicationScope(
20         @Main dispatcher: CoroutineDispatcher,
21     ): CoroutineScope = CoroutineScope(dispatcher)
22 
23     @Provides
24     @SysUISingleton
25     @Main
26     fun mainDispatcher(): CoroutineDispatcher = Dispatchers.Main.immediate
27 
28     /**
29      * Provide a [CoroutineDispatcher] backed by a thread pool containing at most X threads, where
30      * X is the number of CPU cores available.
31      *
32      * Because there are multiple threads at play, there is no serialization order guarantee. You
33      * should use a [kotlinx.coroutines.channels.Channel] for serialization if necessary.
34      *
35      * @see Dispatchers.Default
36      */
37     @Provides
38     @SysUISingleton
39     @Background
40     fun bgDispatcher(): CoroutineDispatcher = Dispatchers.IO
41 }
42