Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ac794ba9 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Provide @ShellMainThread dispatcher as MainCoroutineDispatcher" into main

parents 033df436 4688fd89
Loading
Loading
Loading
Loading
+21 −5
Original line number Diff line number Diff line
@@ -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
@@ -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