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.content.Context; 20 import android.os.Handler; 21 import android.os.Looper; 22 23 import com.android.systemui.dagger.qualifiers.Main; 24 25 import java.util.Optional; 26 import java.util.concurrent.Executor; 27 28 import javax.inject.Named; 29 import javax.inject.Singleton; 30 31 import dagger.Binds; 32 import dagger.Module; 33 import dagger.Provides; 34 35 /** 36 * Dagger Module for classes found within the concurrent package. 37 */ 38 @Module 39 public abstract class GlobalConcurrencyModule { 40 public static final String PRE_HANDLER = "pre_handler"; 41 42 /** 43 * Binds {@link ThreadFactoryImpl} to {@link ThreadFactory}. 44 */ 45 @Binds bindExecutorFactory(ThreadFactoryImpl impl)46 public abstract ThreadFactory bindExecutorFactory(ThreadFactoryImpl impl); 47 48 /** Main Looper */ 49 @Provides 50 @Main provideMainLooper()51 public static Looper provideMainLooper() { 52 return Looper.getMainLooper(); 53 } 54 55 /** 56 * Main Handler. 57 * 58 * Prefer the Main Executor when possible. 59 */ 60 @Provides 61 @Main provideMainHandler(@ain Looper mainLooper)62 public static Handler provideMainHandler(@Main Looper mainLooper) { 63 return new Handler(mainLooper); 64 } 65 66 /** 67 * Provide a Main-Thread Executor. 68 */ 69 @Provides 70 @Singleton 71 @Main provideMainExecutor(Context context)72 public static Executor provideMainExecutor(Context context) { 73 return context.getMainExecutor(); 74 } 75 76 /** 77 * Provide a Main-Thread DelayableExecutor. 78 */ 79 @Provides 80 @Singleton 81 @Main provideMainDelayableExecutor(@ain Looper looper)82 public static DelayableExecutor provideMainDelayableExecutor(@Main Looper looper) { 83 return new ExecutorImpl(looper); 84 } 85 86 87 /** */ 88 @Binds 89 @Singleton provideExecution(ExecutionImpl execution)90 public abstract Execution provideExecution(ExecutionImpl execution); 91 92 /** */ 93 @Provides 94 @Named(PRE_HANDLER) providesUncaughtExceptionHandler()95 public static Optional<Thread.UncaughtExceptionHandler> providesUncaughtExceptionHandler() { 96 return Optional.ofNullable(Thread.getUncaughtExceptionPreHandler()); 97 } 98 } 99