1 package com.android.onboarding.tasks
2 
3 import android.content.Context
4 import java.util.concurrent.ConcurrentHashMap
5 import kotlinx.coroutines.CoroutineScope
6 import kotlinx.coroutines.Dispatchers
7 import kotlinx.coroutines.SupervisorJob
8 import kotlinx.coroutines.cancel
9 @Deprecated("We will remove this class very soon after we fix the build error in cl/619298504.")
10 class DefaultOnboardingTaskManager
11 private constructor(appContext: Context, coroutineScope: CoroutineScope) :
12   AbstractOnboardingTaskManager(appContext, coroutineScope) {
13 
14   override val componentName = "DefaultOnboardingTaskManager"
15 
initializeContractAndTaskMapnull16   override fun initializeContractAndTaskMap() =
17     ConcurrentHashMap<Class<out OnboardingTaskContract<*, *>>, Class<out OnboardingTask<*, *, *>>>()
18 
19   /**
20    * Gets an instance of [DefaultOnboardingTaskManager], creating one if it does not exist.
21    *
22    * @return The singleton instance of [DefaultOnboardingTaskManager].
23    */
24   companion object {
25 
26     const val TAG: String = "DefaultOTM"
27     private var instance: DefaultOnboardingTaskManager? = null
28 
29     /**
30      * Gets an instance of [DefaultOnboardingTaskManager], creating one if it does not exist. A
31      * default coroutine scope with [Dispatchers.Default] and [SupervisorJob] will be used for this
32      * function.
33      *
34      * @param appContext The application context.
35      * @return The singleton instance of [DefaultOnboardingTaskManager].
36      */
37     fun getInstance(appContext: Context): DefaultOnboardingTaskManager {
38       return getInstance(appContext, CoroutineScope(Dispatchers.Default + SupervisorJob()))
39     }
40 
41     /**
42      * Gets an instance of [DefaultOnboardingTaskManager], creating one if it does not exist. You
43      * can customize the coroutine scope by providing your own [CoroutineScope] instance. If not
44      * provided, a default coroutine scope with [Dispatchers.Default] and [SupervisorJob] will be
45      * used.
46      *
47      * @param appContext The application context.
48      * @param coroutineScope The optional [CoroutineScope] for custom coroutine handling.
49      * @return The singleton instance of [DefaultOnboardingTaskManager].
50      */
51     fun getInstance(
52       appContext: Context,
53       coroutineScope: CoroutineScope? = null,
54     ): DefaultOnboardingTaskManager {
55       return instance
56         ?: synchronized(this) {
57           instance
58             ?: DefaultOnboardingTaskManager(
59                 appContext,
60                 coroutineScope ?: CoroutineScope(Dispatchers.Default + SupervisorJob()),
61               )
62               .also { instance = it }
63         }
64     }
65 
66     fun release() {
67       instance?.coroutineScope?.cancel()
68       instance = null
69     }
70   }
71 }
72