1 /* 2 * Copyright (C) 2019 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 */ 16 17 package com.android.systemui.util.concurrency; 18 19 import android.os.Handler; 20 import android.os.HandlerThread; 21 import android.os.Looper; 22 import android.os.Process; 23 24 import com.android.systemui.dagger.SysUISingleton; 25 import com.android.systemui.dagger.qualifiers.Background; 26 import com.android.systemui.dagger.qualifiers.LongRunning; 27 import com.android.systemui.dagger.qualifiers.Main; 28 import com.android.systemui.dagger.qualifiers.UiBackground; 29 30 import java.util.concurrent.Executor; 31 import java.util.concurrent.Executors; 32 33 import dagger.Module; 34 import dagger.Provides; 35 36 /** 37 * Dagger Module for classes found within the concurrent package. 38 */ 39 @Module 40 public abstract class SysUIConcurrencyModule { 41 /** Background Looper */ 42 @Provides 43 @SysUISingleton 44 @Background provideBgLooper()45 public static Looper provideBgLooper() { 46 HandlerThread thread = new HandlerThread("SysUiBg", 47 Process.THREAD_PRIORITY_BACKGROUND); 48 thread.start(); 49 return thread.getLooper(); 50 } 51 52 /** Long running tasks Looper */ 53 @Provides 54 @SysUISingleton 55 @LongRunning provideLongRunningLooper()56 public static Looper provideLongRunningLooper() { 57 HandlerThread thread = new HandlerThread("SysUiLng", 58 Process.THREAD_PRIORITY_BACKGROUND); 59 thread.start(); 60 return thread.getLooper(); 61 } 62 63 /** 64 * Background Handler. 65 * 66 * Prefer the Background Executor when possible. 67 */ 68 @Provides 69 @Background provideBgHandler(@ackground Looper bgLooper)70 public static Handler provideBgHandler(@Background Looper bgLooper) { 71 return new Handler(bgLooper); 72 } 73 74 /** 75 * Provide a Background-Thread Executor by default. 76 */ 77 @Provides 78 @SysUISingleton provideExecutor(@ackground Looper looper)79 public static Executor provideExecutor(@Background Looper looper) { 80 return new ExecutorImpl(looper); 81 } 82 83 /** 84 * Provide a Long running Executor by default. 85 */ 86 @Provides 87 @SysUISingleton 88 @LongRunning provideLongRunningExecutor(@ongRunning Looper looper)89 public static Executor provideLongRunningExecutor(@LongRunning Looper looper) { 90 return new ExecutorImpl(looper); 91 } 92 93 /** 94 * Provide a Background-Thread Executor. 95 */ 96 @Provides 97 @SysUISingleton 98 @Background provideBackgroundExecutor(@ackground Looper looper)99 public static Executor provideBackgroundExecutor(@Background Looper looper) { 100 return new ExecutorImpl(looper); 101 } 102 103 /** 104 * Provide a Background-Thread Executor by default. 105 */ 106 @Provides 107 @SysUISingleton provideDelayableExecutor(@ackground Looper looper)108 public static DelayableExecutor provideDelayableExecutor(@Background Looper looper) { 109 return new ExecutorImpl(looper); 110 } 111 112 /** 113 * Provide a Background-Thread Executor. 114 */ 115 @Provides 116 @SysUISingleton 117 @Background provideBackgroundDelayableExecutor(@ackground Looper looper)118 public static DelayableExecutor provideBackgroundDelayableExecutor(@Background Looper looper) { 119 return new ExecutorImpl(looper); 120 } 121 122 /** 123 * Provide a Background-Thread Executor by default. 124 */ 125 @Provides 126 @SysUISingleton provideRepeatableExecutor(@ackground DelayableExecutor exec)127 public static RepeatableExecutor provideRepeatableExecutor(@Background DelayableExecutor exec) { 128 return new RepeatableExecutorImpl(exec); 129 } 130 131 /** 132 * Provide a Background-Thread Executor. 133 */ 134 @Provides 135 @SysUISingleton 136 @Background provideBackgroundRepeatableExecutor( @ackground DelayableExecutor exec)137 public static RepeatableExecutor provideBackgroundRepeatableExecutor( 138 @Background DelayableExecutor exec) { 139 return new RepeatableExecutorImpl(exec); 140 } 141 142 /** 143 * Provide a Main-Thread Executor. 144 */ 145 @Provides 146 @SysUISingleton 147 @Main provideMainRepeatableExecutor(@ain DelayableExecutor exec)148 public static RepeatableExecutor provideMainRepeatableExecutor(@Main DelayableExecutor exec) { 149 return new RepeatableExecutorImpl(exec); 150 } 151 152 /** 153 * Provide an Executor specifically for running UI operations on a separate thread. 154 * 155 * Keep submitted runnables short and to the point, just as with any other UI code. 156 */ 157 @Provides 158 @SysUISingleton 159 @UiBackground provideUiBackgroundExecutor()160 public static Executor provideUiBackgroundExecutor() { 161 return Executors.newSingleThreadExecutor(); 162 } 163 164 /** */ 165 @Provides 166 @Main providesMainMessageRouter( @ain DelayableExecutor executor)167 public static MessageRouter providesMainMessageRouter( 168 @Main DelayableExecutor executor) { 169 return new MessageRouterImpl(executor); 170 } 171 172 /** */ 173 @Provides 174 @Background providesBackgroundMessageRouter( @ackground DelayableExecutor executor)175 public static MessageRouter providesBackgroundMessageRouter( 176 @Background DelayableExecutor executor) { 177 return new MessageRouterImpl(executor); 178 } 179 } 180