Loading libs/WindowManager/Shell/src/com/android/wm/shell/dagger/WMShellCoroutinesModule.kt +21 −5 Original line number Diff line number Diff line Loading @@ -16,6 +16,7 @@ package com.android.wm.shell.dagger import android.os.Handler import com.android.wm.shell.common.ShellExecutor import com.android.wm.shell.shared.annotations.ShellBackgroundThread import com.android.wm.shell.shared.annotations.ShellMainThread Loading @@ -24,22 +25,37 @@ import dagger.Provides import kotlin.coroutines.CoroutineContext import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.MainCoroutineDispatcher import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.android.asCoroutineDispatcher import kotlinx.coroutines.asCoroutineDispatcher /** Providers for various WmShell-specific coroutines-related constructs. */ /** * Providers for various WmShell-specific coroutines-related constructs. * * Providers of [MainCoroutineDispatcher] intentionally creates the dispatcher with a [Handler] * backing it instead of a [ShellExecutor] because [ShellExecutor.asCoroutineDispatcher] will * create a [CoroutineDispatcher] whose [CoroutineDispatcher.isDispatchNeeded] is effectively never * dispatching. This is because even if dispatched, the backing [ShellExecutor.execute] always runs * the [Runnable] immediately if called from the same thread, whereas * [Handler.asCoroutineDispatcher] will create a [MainCoroutineDispatcher] that correctly * dispatches (queues) when [CoroutineDispatcher.isDispatchNeeded] is true using [Handler.post]. * For callers that do need a non-dispatching version, [MainCoroutineDispatcher.immediate] is * available. */ @Module class WMShellCoroutinesModule { @Provides @ShellMainThread fun provideMainDispatcher(@ShellMainThread mainExecutor: ShellExecutor): CoroutineDispatcher = mainExecutor.asCoroutineDispatcher() fun provideMainDispatcher( @ShellMainThread mainHandler: Handler ): MainCoroutineDispatcher = mainHandler.asCoroutineDispatcher() @Provides @ShellBackgroundThread fun provideBackgroundDispatcher( @ShellBackgroundThread backgroundExecutor: ShellExecutor ): CoroutineDispatcher = backgroundExecutor.asCoroutineDispatcher() @ShellBackgroundThread backgroundHandler: Handler ): MainCoroutineDispatcher = backgroundHandler.asCoroutineDispatcher() @Provides @WMSingleton Loading Loading
libs/WindowManager/Shell/src/com/android/wm/shell/dagger/WMShellCoroutinesModule.kt +21 −5 Original line number Diff line number Diff line Loading @@ -16,6 +16,7 @@ package com.android.wm.shell.dagger import android.os.Handler import com.android.wm.shell.common.ShellExecutor import com.android.wm.shell.shared.annotations.ShellBackgroundThread import com.android.wm.shell.shared.annotations.ShellMainThread Loading @@ -24,22 +25,37 @@ import dagger.Provides import kotlin.coroutines.CoroutineContext import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.MainCoroutineDispatcher import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.android.asCoroutineDispatcher import kotlinx.coroutines.asCoroutineDispatcher /** Providers for various WmShell-specific coroutines-related constructs. */ /** * Providers for various WmShell-specific coroutines-related constructs. * * Providers of [MainCoroutineDispatcher] intentionally creates the dispatcher with a [Handler] * backing it instead of a [ShellExecutor] because [ShellExecutor.asCoroutineDispatcher] will * create a [CoroutineDispatcher] whose [CoroutineDispatcher.isDispatchNeeded] is effectively never * dispatching. This is because even if dispatched, the backing [ShellExecutor.execute] always runs * the [Runnable] immediately if called from the same thread, whereas * [Handler.asCoroutineDispatcher] will create a [MainCoroutineDispatcher] that correctly * dispatches (queues) when [CoroutineDispatcher.isDispatchNeeded] is true using [Handler.post]. * For callers that do need a non-dispatching version, [MainCoroutineDispatcher.immediate] is * available. */ @Module class WMShellCoroutinesModule { @Provides @ShellMainThread fun provideMainDispatcher(@ShellMainThread mainExecutor: ShellExecutor): CoroutineDispatcher = mainExecutor.asCoroutineDispatcher() fun provideMainDispatcher( @ShellMainThread mainHandler: Handler ): MainCoroutineDispatcher = mainHandler.asCoroutineDispatcher() @Provides @ShellBackgroundThread fun provideBackgroundDispatcher( @ShellBackgroundThread backgroundExecutor: ShellExecutor ): CoroutineDispatcher = backgroundExecutor.asCoroutineDispatcher() @ShellBackgroundThread backgroundHandler: Handler ): MainCoroutineDispatcher = backgroundHandler.asCoroutineDispatcher() @Provides @WMSingleton Loading