1 /*
2  *  * Copyright (C) 2021 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.modules.utils;
18 
19 import android.annotation.NonNull;
20 import android.os.Handler;
21 import android.os.HandlerThread;
22 
23 import com.android.internal.annotations.GuardedBy;
24 
25 import java.util.concurrent.Executor;
26 
27 /**
28  * Thread for asynchronous event processing. This thread is configured as
29  * {@link android.os.Process#THREAD_PRIORITY_BACKGROUND}, which means fewer CPU
30  * resources will be dedicated to it, and it will "have less chance of impacting
31  * the responsiveness of the user interface."
32  * <p>
33  * This thread is best suited for tasks that the user is not actively waiting
34  * for, or for tasks that the user expects to be executed eventually.
35  *
36  * @see com.android.internal.os.BackgroundThread
37  */
38 public final class BackgroundThread extends HandlerThread {
39     private static final Object sLock = new Object();
40 
41     @GuardedBy("sLock")
42     private static BackgroundThread sInstance;
43     @GuardedBy("sLock")
44     private static Handler sHandler;
45     @GuardedBy("sLock")
46     private static HandlerExecutor sHandlerExecutor;
47 
BackgroundThread()48     private BackgroundThread() {
49         super(BackgroundThread.class.getName(), android.os.Process.THREAD_PRIORITY_BACKGROUND);
50     }
51 
52     @GuardedBy("sLock")
ensureThreadLocked()53     private static void ensureThreadLocked() {
54         if (sInstance == null) {
55             sInstance = new BackgroundThread();
56             sInstance.start();
57             sHandler = new Handler(sInstance.getLooper());
58             sHandlerExecutor = new HandlerExecutor(sHandler);
59         }
60     }
61 
62     /**
63      * Get the singleton instance of this class.
64      *
65      * @return the singleton instance of this class
66      */
67     @NonNull
get()68     public static BackgroundThread get() {
69         synchronized (sLock) {
70             ensureThreadLocked();
71             return sInstance;
72         }
73     }
74 
75     /**
76      * Get the singleton {@link Handler} for this class.
77      *
78      * @return the singleton {@link Handler} for this class.
79      */
80     @NonNull
getHandler()81     public static Handler getHandler() {
82         synchronized (sLock) {
83             ensureThreadLocked();
84             return sHandler;
85         }
86     }
87 
88     /**
89      * Get the singleton {@link Executor} for this class.
90      *
91      * @return the singleton {@link Executor} for this class.
92      */
93     @NonNull
getExecutor()94     public static Executor getExecutor() {
95         synchronized (sLock) {
96             ensureThreadLocked();
97             return sHandlerExecutor;
98         }
99     }
100 }
101