Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1 | /* |
| 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 Erfanian | 91ce7d2 | 2017-06-05 13:35:02 -0700 | [diff] [blame] | 16 | package com.android.dialer.common.concurrent; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 17 | |
zachh | 6a4cebd | 2017-10-24 17:10:06 -0700 | [diff] [blame] | 18 | import android.os.AsyncTask; |
| 19 | import com.android.dialer.common.LogUtil; |
| 20 | import com.android.dialer.common.concurrent.Annotations.NonUiParallel; |
| 21 | import com.android.dialer.common.concurrent.Annotations.NonUiSerial; |
Zachary Heidepriem | 922cbed | 2017-11-11 18:49:27 -0800 | [diff] [blame^] | 22 | import com.android.dialer.common.concurrent.Annotations.Ui; |
zachh | 6a4cebd | 2017-10-24 17:10:06 -0700 | [diff] [blame] | 23 | import com.android.dialer.common.concurrent.Annotations.UiParallel; |
| 24 | import com.android.dialer.common.concurrent.Annotations.UiSerial; |
Zachary Heidepriem | 922cbed | 2017-11-11 18:49:27 -0800 | [diff] [blame^] | 25 | import com.google.common.util.concurrent.ListeningExecutorService; |
Eric Erfanian | 91ce7d2 | 2017-06-05 13:35:02 -0700 | [diff] [blame] | 26 | import dagger.Binds; |
| 27 | import dagger.Module; |
zachh | 6a4cebd | 2017-10-24 17:10:06 -0700 | [diff] [blame] | 28 | import dagger.Provides; |
| 29 | import java.util.concurrent.Executor; |
| 30 | import java.util.concurrent.ExecutorService; |
| 31 | import java.util.concurrent.Executors; |
| 32 | import java.util.concurrent.ScheduledExecutorService; |
| 33 | import java.util.concurrent.ThreadFactory; |
| 34 | import javax.inject.Singleton; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 35 | |
zachh | 6a4cebd | 2017-10-24 17:10:06 -0700 | [diff] [blame] | 36 | /** Module which provides concurrency bindings. */ |
Eric Erfanian | 91ce7d2 | 2017-06-05 13:35:02 -0700 | [diff] [blame] | 37 | @Module |
| 38 | public abstract class DialerExecutorModule { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 39 | |
Eric Erfanian | 91ce7d2 | 2017-06-05 13:35:02 -0700 | [diff] [blame] | 40 | @Binds |
| 41 | abstract DialerExecutorFactory bindDialerExecutorFactory( |
| 42 | DefaultDialerExecutorFactory defaultDialerExecutorFactory); |
zachh | 6a4cebd | 2017-10-24 17:10:06 -0700 | [diff] [blame] | 43 | |
| 44 | @Provides |
| 45 | @Singleton |
Zachary Heidepriem | 922cbed | 2017-11-11 18:49:27 -0800 | [diff] [blame^] | 46 | @Ui |
| 47 | static ListeningExecutorService provideUiThreadExecutorService() { |
| 48 | return new UiThreadExecutor(); |
| 49 | } |
| 50 | |
| 51 | @Provides |
| 52 | @Singleton |
zachh | 6a4cebd | 2017-10-24 17:10:06 -0700 | [diff] [blame] | 53 | @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 Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 108 | } |