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 import com.android.systemui.dagger.qualifiers.UiBackground; 25 26 import dagger.Binds; 27 import dagger.Module; 28 import dagger.Provides; 29 30 import java.util.concurrent.Executor; 31 import java.util.concurrent.Executors; 32 33 import javax.inject.Singleton; 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 * @deprecated Use @Main Handler. 68 */ 69 @Deprecated 70 @Provides provideHandler()71 public static Handler provideHandler() { 72 return new Handler(); 73 } 74 75 /** 76 * Provide an Executor specifically for running UI operations on a separate thread. 77 * 78 * Keep submitted runnables short and to the point, just as with any other UI code. 79 */ 80 @Provides 81 @Singleton 82 @UiBackground provideUiBackgroundExecutor()83 public static Executor provideUiBackgroundExecutor() { 84 return Executors.newSingleThreadExecutor(); 85 } 86 87 /** 88 * Provide a Main-Thread Executor. 89 */ 90 @Provides 91 @Singleton 92 @Main provideMainExecutor(Context context)93 public static Executor provideMainExecutor(Context context) { 94 return context.getMainExecutor(); 95 } 96 97 /** 98 * Provide a Main-Thread DelayableExecutor. 99 */ 100 @Provides 101 @Singleton 102 @Main provideMainDelayableExecutor(@ain Looper looper)103 public static DelayableExecutor provideMainDelayableExecutor(@Main Looper looper) { 104 return new ExecutorImpl(looper); 105 } 106 107 /** */ 108 @Binds 109 @Singleton provideExecution(ExecutionImpl execution)110 public abstract Execution provideExecution(ExecutionImpl execution); 111 } 112