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.ExecutorService; 32 import java.util.concurrent.Executors; 33 34 import javax.inject.Singleton; 35 36 /** 37 * Dagger Module for classes found within the concurrent package. 38 */ 39 @Module 40 public abstract class GlobalConcurrencyModule { 41 public static final String PRE_HANDLER = "pre_handler"; 42 43 /** 44 * Binds {@link ThreadFactoryImpl} to {@link ThreadFactory}. 45 */ 46 @Binds bindExecutorFactory(ThreadFactoryImpl impl)47 public abstract ThreadFactory bindExecutorFactory(ThreadFactoryImpl impl); 48 49 /** Main Looper */ 50 @Provides 51 @Main provideMainLooper()52 public static Looper provideMainLooper() { 53 return Looper.getMainLooper(); 54 } 55 56 /** 57 * Main Handler. 58 * 59 * Prefer the Main Executor when possible. 60 */ 61 @Provides 62 @Main provideMainHandler(@ain Looper mainLooper)63 public static Handler provideMainHandler(@Main Looper mainLooper) { 64 return new Handler(mainLooper); 65 } 66 67 /** 68 * Provide an Executor specifically for running UI operations on a separate thread. 69 * 70 * Keep submitted runnables short and to the point, just as with any other UI code. 71 */ 72 @Provides 73 @Singleton 74 @UiBackground provideUiBackgroundExecutor()75 public static Executor provideUiBackgroundExecutor() { 76 return provideUiBackgroundExecutorService(); 77 } 78 79 /** 80 * Provide an ExecutorService specifically for running UI operations on a separate thread. 81 * 82 * <p>Keep submitted runnables short and to the point, just as with any other UI code. 83 */ 84 @Provides 85 @Singleton 86 @UiBackground provideUiBackgroundExecutorService()87 public static ExecutorService provideUiBackgroundExecutorService() { 88 return Executors.newSingleThreadExecutor(); 89 } 90 91 /** 92 * Provide a Main-Thread Executor. 93 */ 94 @Provides 95 @Singleton 96 @Main provideMainExecutor(Context context)97 public static Executor provideMainExecutor(Context context) { 98 return context.getMainExecutor(); 99 } 100 101 /** 102 * Provide a Main-Thread DelayableExecutor. 103 */ 104 @Provides 105 @Singleton 106 @Main provideMainDelayableExecutor(@ain Looper looper)107 public static DelayableExecutor provideMainDelayableExecutor(@Main Looper looper) { 108 return new ExecutorImpl(looper); 109 } 110 111 /** */ 112 @Binds 113 @Singleton provideExecution(ExecutionImpl execution)114 public abstract Execution provideExecution(ExecutionImpl execution); 115 } 116