blob: 98738ed37f69623c65460ed9f4abaf3f59862770 [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;
zachh0c0c0a32017-12-06 18:06:06 -080020import com.android.dialer.common.concurrent.Annotations.BackgroundExecutor;
21import com.android.dialer.common.concurrent.Annotations.LightweightExecutor;
zachh6a4cebd2017-10-24 17:10:06 -070022import com.android.dialer.common.concurrent.Annotations.NonUiParallel;
23import com.android.dialer.common.concurrent.Annotations.NonUiSerial;
Zachary Heidepriem922cbed2017-11-11 18:49:27 -080024import com.android.dialer.common.concurrent.Annotations.Ui;
zachh6a4cebd2017-10-24 17:10:06 -070025import com.android.dialer.common.concurrent.Annotations.UiParallel;
26import com.android.dialer.common.concurrent.Annotations.UiSerial;
Zachary Heidepriem922cbed2017-11-11 18:49:27 -080027import com.google.common.util.concurrent.ListeningExecutorService;
zachh0c0c0a32017-12-06 18:06:06 -080028import com.google.common.util.concurrent.MoreExecutors;
Eric Erfanian91ce7d22017-06-05 13:35:02 -070029import dagger.Binds;
30import dagger.Module;
zachh6a4cebd2017-10-24 17:10:06 -070031import dagger.Provides;
zachh6a4cebd2017-10-24 17:10:06 -070032import java.util.concurrent.ExecutorService;
33import java.util.concurrent.Executors;
34import java.util.concurrent.ScheduledExecutorService;
35import java.util.concurrent.ThreadFactory;
36import javax.inject.Singleton;
Eric Erfanianccca3152017-02-22 16:32:36 -080037
zachh6a4cebd2017-10-24 17:10:06 -070038/** Module which provides concurrency bindings. */
Eric Erfanian91ce7d22017-06-05 13:35:02 -070039@Module
40public abstract class DialerExecutorModule {
Eric Erfanianccca3152017-02-22 16:32:36 -080041
Eric Erfanian91ce7d22017-06-05 13:35:02 -070042 @Binds
43 abstract DialerExecutorFactory bindDialerExecutorFactory(
44 DefaultDialerExecutorFactory defaultDialerExecutorFactory);
zachh6a4cebd2017-10-24 17:10:06 -070045
46 @Provides
47 @Singleton
Zachary Heidepriem922cbed2017-11-11 18:49:27 -080048 @Ui
49 static ListeningExecutorService provideUiThreadExecutorService() {
50 return new UiThreadExecutor();
51 }
52
53 @Provides
54 @Singleton
zachh6a4cebd2017-10-24 17:10:06 -070055 @NonUiParallel
56 static ExecutorService provideNonUiThreadPool() {
57 return Executors.newFixedThreadPool(
58 5,
59 new ThreadFactory() {
60 @Override
61 public Thread newThread(Runnable runnable) {
62 LogUtil.i("DialerExecutorModule.newThread", "creating low priority thread");
63 Thread thread = new Thread(runnable, "DialerExecutors-LowPriority");
64 // Java thread priority 4 corresponds to Process.THREAD_PRIORITY_BACKGROUND (10)
65 thread.setPriority(4);
66 return thread;
67 }
68 });
69 }
70
71 @Provides
72 @Singleton
73 @NonUiSerial
74 static ScheduledExecutorService provideNonUiSerialExecutorService() {
75 return Executors.newSingleThreadScheduledExecutor(
76 new ThreadFactory() {
77 @Override
78 public Thread newThread(Runnable runnable) {
79 LogUtil.i("NonUiTaskBuilder.newThread", "creating serial thread");
80 Thread thread = new Thread(runnable, "DialerExecutors-LowPriority-Serial");
81 // Java thread priority 4 corresponds to Process.THREAD_PRIORITY_BACKGROUND (10)
82 thread.setPriority(4);
83 return thread;
84 }
85 });
86 }
87
88 @Provides
89 @UiParallel
zachh0c0c0a32017-12-06 18:06:06 -080090 static ExecutorService provideUiThreadPool() {
91 return (ExecutorService) AsyncTask.THREAD_POOL_EXECUTOR;
zachh6a4cebd2017-10-24 17:10:06 -070092 }
93
94 @Provides
95 @Singleton
96 @UiSerial
97 static ScheduledExecutorService provideUiSerialExecutorService() {
98 return Executors.newSingleThreadScheduledExecutor(
99 new ThreadFactory() {
100 @Override
101 public Thread newThread(Runnable runnable) {
102 LogUtil.i("DialerExecutorModule.newThread", "creating serial thread");
103 Thread thread = new Thread(runnable, "DialerExecutors-HighPriority-Serial");
104 // Java thread priority 5 corresponds to Process.THREAD_PRIORITY_DEFAULT (0)
105 thread.setPriority(5);
106 return thread;
107 }
108 });
109 }
zachh0c0c0a32017-12-06 18:06:06 -0800110
111 @Provides
112 @Singleton
113 @LightweightExecutor
114 static ListeningExecutorService provideLightweightExecutor(@UiParallel ExecutorService delegate) {
115 return MoreExecutors.listeningDecorator(delegate);
116 }
117
118 @Provides
119 @Singleton
120 @BackgroundExecutor
121 static ListeningExecutorService provideBackgroundExecutor(
122 @NonUiParallel ExecutorService delegate) {
123 return MoreExecutors.listeningDecorator(delegate);
124 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800125}