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 static com.android.systemui.Dependency.TIME_TICK_HANDLER_NAME; 20 21 import android.os.Handler; 22 import android.os.HandlerThread; 23 import android.os.Looper; 24 import android.os.Process; 25 26 import com.android.systemui.dagger.SysUISingleton; 27 import com.android.systemui.dagger.qualifiers.Background; 28 import com.android.systemui.dagger.qualifiers.BroadcastRunning; 29 import com.android.systemui.dagger.qualifiers.LongRunning; 30 import com.android.systemui.dagger.qualifiers.Main; 31 32 import java.util.concurrent.Executor; 33 34 import javax.inject.Named; 35 36 import dagger.Module; 37 import dagger.Provides; 38 39 /** 40 * Dagger Module for classes found within the concurrent package. 41 */ 42 @Module 43 public abstract class SysUIConcurrencyModule { 44 /** Background Looper */ 45 @Provides 46 @SysUISingleton 47 @Background provideBgLooper()48 public static Looper provideBgLooper() { 49 HandlerThread thread = new HandlerThread("SysUiBg", 50 Process.THREAD_PRIORITY_BACKGROUND); 51 thread.start(); 52 return thread.getLooper(); 53 } 54 55 /** BroadcastRunning Looper (for sending and receiving broadcasts) */ 56 @Provides 57 @SysUISingleton 58 @BroadcastRunning provideBroadcastRunningLooper()59 public static Looper provideBroadcastRunningLooper() { 60 HandlerThread thread = new HandlerThread("BroadcastRunning", 61 Process.THREAD_PRIORITY_BACKGROUND); 62 thread.start(); 63 return thread.getLooper(); 64 } 65 66 /** Long running tasks Looper */ 67 @Provides 68 @SysUISingleton 69 @LongRunning provideLongRunningLooper()70 public static Looper provideLongRunningLooper() { 71 HandlerThread thread = new HandlerThread("SysUiLng", 72 Process.THREAD_PRIORITY_BACKGROUND); 73 thread.start(); 74 return thread.getLooper(); 75 } 76 77 /** 78 * Background Handler. 79 * 80 * Prefer the Background Executor when possible. 81 */ 82 @Provides 83 @Background provideBgHandler(@ackground Looper bgLooper)84 public static Handler provideBgHandler(@Background Looper bgLooper) { 85 return new Handler(bgLooper); 86 } 87 88 /** 89 * Provide a Background-Thread Executor by default. 90 */ 91 @Provides 92 @SysUISingleton provideExecutor(@ackground Looper looper)93 public static Executor provideExecutor(@Background Looper looper) { 94 return new ExecutorImpl(looper); 95 } 96 97 /** 98 * Provide a BroadcastRunning Executor (for sending and receiving broadcasts). 99 */ 100 @Provides 101 @SysUISingleton 102 @BroadcastRunning provideBroadcastRunningExecutor(@roadcastRunning Looper looper)103 public static Executor provideBroadcastRunningExecutor(@BroadcastRunning Looper looper) { 104 return new ExecutorImpl(looper); 105 } 106 107 /** 108 * Provide a Long running Executor. 109 */ 110 @Provides 111 @SysUISingleton 112 @LongRunning provideLongRunningExecutor(@ongRunning Looper looper)113 public static Executor provideLongRunningExecutor(@LongRunning Looper looper) { 114 return new ExecutorImpl(looper); 115 } 116 117 /** 118 * Provide a Long running Executor. 119 */ 120 @Provides 121 @SysUISingleton 122 @LongRunning provideLongRunningDelayableExecutor( @ongRunning Looper looper)123 public static DelayableExecutor provideLongRunningDelayableExecutor( 124 @LongRunning Looper looper) { 125 return new ExecutorImpl(looper); 126 } 127 128 /** 129 * Provide a Background-Thread Executor. 130 */ 131 @Provides 132 @SysUISingleton 133 @Background provideBackgroundExecutor(@ackground Looper looper)134 public static Executor provideBackgroundExecutor(@Background Looper looper) { 135 return new ExecutorImpl(looper); 136 } 137 138 /** 139 * Provide a Background-Thread Executor by default. 140 */ 141 @Provides 142 @SysUISingleton provideDelayableExecutor(@ackground Looper looper)143 public static DelayableExecutor provideDelayableExecutor(@Background Looper looper) { 144 return new ExecutorImpl(looper); 145 } 146 147 /** 148 * Provide a Background-Thread Executor. 149 */ 150 @Provides 151 @SysUISingleton 152 @Background provideBackgroundDelayableExecutor(@ackground Looper looper)153 public static DelayableExecutor provideBackgroundDelayableExecutor(@Background Looper looper) { 154 return new ExecutorImpl(looper); 155 } 156 157 /** 158 * Provide a Background-Thread Executor by default. 159 */ 160 @Provides 161 @SysUISingleton provideRepeatableExecutor(@ackground DelayableExecutor exec)162 public static RepeatableExecutor provideRepeatableExecutor(@Background DelayableExecutor exec) { 163 return new RepeatableExecutorImpl(exec); 164 } 165 166 /** 167 * Provide a Background-Thread Executor. 168 */ 169 @Provides 170 @SysUISingleton 171 @Background provideBackgroundRepeatableExecutor( @ackground DelayableExecutor exec)172 public static RepeatableExecutor provideBackgroundRepeatableExecutor( 173 @Background DelayableExecutor exec) { 174 return new RepeatableExecutorImpl(exec); 175 } 176 177 /** 178 * Provide a Main-Thread Executor. 179 */ 180 @Provides 181 @SysUISingleton 182 @Main provideMainRepeatableExecutor(@ain DelayableExecutor exec)183 public static RepeatableExecutor provideMainRepeatableExecutor(@Main DelayableExecutor exec) { 184 return new RepeatableExecutorImpl(exec); 185 } 186 187 /** */ 188 @Provides 189 @Main providesMainMessageRouter( @ain DelayableExecutor executor)190 public static MessageRouter providesMainMessageRouter( 191 @Main DelayableExecutor executor) { 192 return new MessageRouterImpl(executor); 193 } 194 195 /** */ 196 @Provides 197 @Background providesBackgroundMessageRouter( @ackground DelayableExecutor executor)198 public static MessageRouter providesBackgroundMessageRouter( 199 @Background DelayableExecutor executor) { 200 return new MessageRouterImpl(executor); 201 } 202 203 /** */ 204 @Provides 205 @SysUISingleton 206 @Named(TIME_TICK_HANDLER_NAME) provideTimeTickHandler()207 public static Handler provideTimeTickHandler() { 208 HandlerThread thread = new HandlerThread("TimeTick"); 209 thread.start(); 210 return new Handler(thread.getLooper()); 211 } 212 } 213