1 /*
2  * Copyright (C) 2020 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 
23 import java.util.concurrent.Executor;
24 
25 import javax.inject.Inject;
26 
27 class ThreadFactoryImpl implements ThreadFactory {
28     @Inject
ThreadFactoryImpl()29     ThreadFactoryImpl() {}
30 
31     @Override
buildLooperOnNewThread(String threadName)32     public Looper buildLooperOnNewThread(String threadName) {
33         HandlerThread handlerThread = new HandlerThread(threadName);
34         handlerThread.start();
35         return handlerThread.getLooper();
36     }
37 
38     @Override
buildHandlerOnNewThread(String threadName)39     public Handler buildHandlerOnNewThread(String threadName) {
40         return new Handler(buildLooperOnNewThread(threadName));
41     }
42 
43     @Override
buildExecutorOnNewThread(String threadName)44     public Executor buildExecutorOnNewThread(String threadName) {
45         return buildDelayableExecutorOnNewThread(threadName);
46     }
47 
48     @Override
buildDelayableExecutorOnNewThread(String threadName)49     public DelayableExecutor buildDelayableExecutorOnNewThread(String threadName) {
50         HandlerThread handlerThread = new HandlerThread(threadName);
51         handlerThread.start();
52         return buildDelayableExecutorOnLooper(handlerThread.getLooper());
53     }
54 
55     @Override
buildDelayableExecutorOnHandler(Handler handler)56     public DelayableExecutor buildDelayableExecutorOnHandler(Handler handler) {
57         return buildDelayableExecutorOnLooper(handler.getLooper());
58     }
59 
60     @Override
buildDelayableExecutorOnLooper(Looper looper)61     public DelayableExecutor buildDelayableExecutorOnLooper(Looper looper) {
62         return new ExecutorImpl(looper);
63     }
64 }
65