blob: 5e0190e8dfcb344058753d02ca8009b1e30a9d91 [file] [log] [blame]
Eric Erfanianccca3152017-02-22 16:32:36 -08001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
15 */
Eric Erfanian91ce7d22017-06-05 13:35:02 -070016package com.android.dialer.common.concurrent;
Eric Erfanianccca3152017-02-22 16:32:36 -080017
zachh6a4cebd2017-10-24 17:10:06 -070018import android.os.AsyncTask;
19import com.android.dialer.common.LogUtil;
20import com.android.dialer.common.concurrent.Annotations.NonUiParallel;
21import com.android.dialer.common.concurrent.Annotations.NonUiSerial;
Zachary Heidepriem922cbed2017-11-11 18:49:27 -080022import com.android.dialer.common.concurrent.Annotations.Ui;
zachh6a4cebd2017-10-24 17:10:06 -070023import com.android.dialer.common.concurrent.Annotations.UiParallel;
24import com.android.dialer.common.concurrent.Annotations.UiSerial;
Zachary Heidepriem922cbed2017-11-11 18:49:27 -080025import com.google.common.util.concurrent.ListeningExecutorService;
Eric Erfanian91ce7d22017-06-05 13:35:02 -070026import dagger.Binds;
27import dagger.Module;
zachh6a4cebd2017-10-24 17:10:06 -070028import dagger.Provides;
29import java.util.concurrent.Executor;
30import java.util.concurrent.ExecutorService;
31import java.util.concurrent.Executors;
32import java.util.concurrent.ScheduledExecutorService;
33import java.util.concurrent.ThreadFactory;
34import javax.inject.Singleton;
Eric Erfanianccca3152017-02-22 16:32:36 -080035
zachh6a4cebd2017-10-24 17:10:06 -070036/** Module which provides concurrency bindings. */
Eric Erfanian91ce7d22017-06-05 13:35:02 -070037@Module
38public abstract class DialerExecutorModule {
Eric Erfanianccca3152017-02-22 16:32:36 -080039
Eric Erfanian91ce7d22017-06-05 13:35:02 -070040 @Binds
41 abstract DialerExecutorFactory bindDialerExecutorFactory(
42 DefaultDialerExecutorFactory defaultDialerExecutorFactory);
zachh6a4cebd2017-10-24 17:10:06 -070043
44 @Provides
45 @Singleton
Zachary Heidepriem922cbed2017-11-11 18:49:27 -080046 @Ui
47 static ListeningExecutorService provideUiThreadExecutorService() {
48 return new UiThreadExecutor();
49 }
50
51 @Provides
52 @Singleton
zachh6a4cebd2017-10-24 17:10:06 -070053 @NonUiParallel
54 static ExecutorService provideNonUiThreadPool() {
55 return Executors.newFixedThreadPool(
56 5,
57 new ThreadFactory() {
58 @Override
59 public Thread newThread(Runnable runnable) {
60 LogUtil.i("DialerExecutorModule.newThread", "creating low priority thread");
61 Thread thread = new Thread(runnable, "DialerExecutors-LowPriority");
62 // Java thread priority 4 corresponds to Process.THREAD_PRIORITY_BACKGROUND (10)
63 thread.setPriority(4);
64 return thread;
65 }
66 });
67 }
68
69 @Provides
70 @Singleton
71 @NonUiSerial
72 static ScheduledExecutorService provideNonUiSerialExecutorService() {
73 return Executors.newSingleThreadScheduledExecutor(
74 new ThreadFactory() {
75 @Override
76 public Thread newThread(Runnable runnable) {
77 LogUtil.i("NonUiTaskBuilder.newThread", "creating serial thread");
78 Thread thread = new Thread(runnable, "DialerExecutors-LowPriority-Serial");
79 // Java thread priority 4 corresponds to Process.THREAD_PRIORITY_BACKGROUND (10)
80 thread.setPriority(4);
81 return thread;
82 }
83 });
84 }
85
86 @Provides
87 @UiParallel
88 static Executor provideUiThreadPool() {
89 return AsyncTask.THREAD_POOL_EXECUTOR;
90 }
91
92 @Provides
93 @Singleton
94 @UiSerial
95 static ScheduledExecutorService provideUiSerialExecutorService() {
96 return Executors.newSingleThreadScheduledExecutor(
97 new ThreadFactory() {
98 @Override
99 public Thread newThread(Runnable runnable) {
100 LogUtil.i("DialerExecutorModule.newThread", "creating serial thread");
101 Thread thread = new Thread(runnable, "DialerExecutors-HighPriority-Serial");
102 // Java thread priority 5 corresponds to Process.THREAD_PRIORITY_DEFAULT (0)
103 thread.setPriority(5);
104 return thread;
105 }
106 });
107 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800108}