1 /*
2  * Copyright (C) 2017 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.dialer.common.concurrent;
18 
19 import android.os.Handler;
20 import android.os.Looper;
21 
22 /** Application-wide utility methods for working with threads. */
23 public class ThreadUtil {
24   private static volatile Handler mainThreadHandler;
25 
26   /** Posts a runnable to the UI thread. */
postOnUiThread(Runnable runnable)27   public static void postOnUiThread(Runnable runnable) {
28     getUiThreadHandler().post(runnable);
29   }
30 
31   /** Posts a runnable to the UI thread, to be run after the specified amount of time elapses. */
postDelayedOnUiThread(Runnable runnable, long delayMillis)32   public static void postDelayedOnUiThread(Runnable runnable, long delayMillis) {
33     getUiThreadHandler().postDelayed(runnable, delayMillis);
34   }
35 
36   /** Gets a handler which uses the main looper. */
getUiThreadHandler()37   public static Handler getUiThreadHandler() {
38     if (mainThreadHandler == null) {
39       mainThreadHandler = new Handler(Looper.getMainLooper());
40     }
41     return mainThreadHandler;
42   }
43 }
44