Improved dagger support for executor services.

By always creating executor services via dagger, we can now bind special versions during espresso tests that can implement idling resources. We should be using idling resources during espresso tests for threads that we create ourselves, because espresso does not know about them.

Hopefully this reduces some of the flakiness of espresso tests that we have today.

This required converting all existing calls to DialerExecutors to pass a context used to fetch the component, and also required creating new application classes for espresso tests.

Test: temporarily added a task which just slept to DialtactsActivity and verified that its integration test failed due to idling resource timeout
PiperOrigin-RevId: 173334773
Change-Id: I876a93022d235d62cfc377bf5b06687e21a34758
diff --git a/java/com/android/dialer/common/concurrent/DialerExecutorModule.java b/java/com/android/dialer/common/concurrent/DialerExecutorModule.java
index 281f88c..68910fb 100644
--- a/java/com/android/dialer/common/concurrent/DialerExecutorModule.java
+++ b/java/com/android/dialer/common/concurrent/DialerExecutorModule.java
@@ -15,14 +15,85 @@
  */
 package com.android.dialer.common.concurrent;
 
+import android.os.AsyncTask;
+import com.android.dialer.common.LogUtil;
+import com.android.dialer.common.concurrent.Annotations.NonUiParallel;
+import com.android.dialer.common.concurrent.Annotations.NonUiSerial;
+import com.android.dialer.common.concurrent.Annotations.UiParallel;
+import com.android.dialer.common.concurrent.Annotations.UiSerial;
 import dagger.Binds;
 import dagger.Module;
+import dagger.Provides;
+import java.util.concurrent.Executor;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ThreadFactory;
+import javax.inject.Singleton;
 
-/** Module which binds the production {@link DialerExecutorFactory}. */
+/** Module which provides concurrency bindings. */
 @Module
 public abstract class DialerExecutorModule {
 
   @Binds
   abstract DialerExecutorFactory bindDialerExecutorFactory(
       DefaultDialerExecutorFactory defaultDialerExecutorFactory);
+
+  @Provides
+  @Singleton
+  @NonUiParallel
+  static ExecutorService provideNonUiThreadPool() {
+    return Executors.newFixedThreadPool(
+        5,
+        new ThreadFactory() {
+          @Override
+          public Thread newThread(Runnable runnable) {
+            LogUtil.i("DialerExecutorModule.newThread", "creating low priority thread");
+            Thread thread = new Thread(runnable, "DialerExecutors-LowPriority");
+            // Java thread priority 4 corresponds to Process.THREAD_PRIORITY_BACKGROUND (10)
+            thread.setPriority(4);
+            return thread;
+          }
+        });
+  }
+
+  @Provides
+  @Singleton
+  @NonUiSerial
+  static ScheduledExecutorService provideNonUiSerialExecutorService() {
+    return Executors.newSingleThreadScheduledExecutor(
+        new ThreadFactory() {
+          @Override
+          public Thread newThread(Runnable runnable) {
+            LogUtil.i("NonUiTaskBuilder.newThread", "creating serial thread");
+            Thread thread = new Thread(runnable, "DialerExecutors-LowPriority-Serial");
+            // Java thread priority 4 corresponds to Process.THREAD_PRIORITY_BACKGROUND (10)
+            thread.setPriority(4);
+            return thread;
+          }
+        });
+  }
+
+  @Provides
+  @UiParallel
+  static Executor provideUiThreadPool() {
+    return AsyncTask.THREAD_POOL_EXECUTOR;
+  }
+
+  @Provides
+  @Singleton
+  @UiSerial
+  static ScheduledExecutorService provideUiSerialExecutorService() {
+    return Executors.newSingleThreadScheduledExecutor(
+        new ThreadFactory() {
+          @Override
+          public Thread newThread(Runnable runnable) {
+            LogUtil.i("DialerExecutorModule.newThread", "creating serial thread");
+            Thread thread = new Thread(runnable, "DialerExecutors-HighPriority-Serial");
+            // Java thread priority 5 corresponds to Process.THREAD_PRIORITY_DEFAULT (0)
+            thread.setPriority(5);
+            return thread;
+          }
+        });
+  }
 }