Juan Lang | 5efe8f0 | 2017-05-04 16:59:46 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | package com.android.settingslib.utils; |
| 17 | |
Fan Zhang | d30c05c | 2017-07-21 16:59:57 -0700 | [diff] [blame] | 18 | import android.os.Handler; |
Juan Lang | 5efe8f0 | 2017-05-04 16:59:46 -0700 | [diff] [blame] | 19 | import android.os.Looper; |
| 20 | |
Fan Zhang | dadfd50 | 2017-07-26 11:00:51 -0700 | [diff] [blame] | 21 | import java.util.concurrent.ExecutorService; |
| 22 | import java.util.concurrent.Executors; |
| 23 | |
Juan Lang | 5efe8f0 | 2017-05-04 16:59:46 -0700 | [diff] [blame] | 24 | public class ThreadUtils { |
Fan Zhang | d30c05c | 2017-07-21 16:59:57 -0700 | [diff] [blame] | 25 | |
Juan Lang | 5efe8f0 | 2017-05-04 16:59:46 -0700 | [diff] [blame] | 26 | private static volatile Thread sMainThread; |
Fan Zhang | d30c05c | 2017-07-21 16:59:57 -0700 | [diff] [blame] | 27 | private static volatile Handler sMainThreadHandler; |
Fan Zhang | dadfd50 | 2017-07-26 11:00:51 -0700 | [diff] [blame] | 28 | private static volatile ExecutorService sSingleThreadExecutor; |
Juan Lang | 5efe8f0 | 2017-05-04 16:59:46 -0700 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Returns true if the current thread is the UI thread. |
| 32 | */ |
| 33 | public static boolean isMainThread() { |
| 34 | if (sMainThread == null) { |
| 35 | sMainThread = Looper.getMainLooper().getThread(); |
| 36 | } |
| 37 | return Thread.currentThread() == sMainThread; |
| 38 | } |
| 39 | |
| 40 | /** |
Fan Zhang | d30c05c | 2017-07-21 16:59:57 -0700 | [diff] [blame] | 41 | * Returns a shared UI thread handler. |
| 42 | */ |
| 43 | public static Handler getUiThreadHandler() { |
| 44 | if (sMainThreadHandler == null) { |
| 45 | sMainThreadHandler = new Handler(Looper.getMainLooper()); |
| 46 | } |
| 47 | |
| 48 | return sMainThreadHandler; |
| 49 | } |
| 50 | |
| 51 | /** |
Juan Lang | 5efe8f0 | 2017-05-04 16:59:46 -0700 | [diff] [blame] | 52 | * Checks that the current thread is the UI thread. Otherwise throws an exception. |
| 53 | */ |
| 54 | public static void ensureMainThread() { |
| 55 | if (!isMainThread()) { |
| 56 | throw new RuntimeException("Must be called on the UI thread"); |
| 57 | } |
| 58 | } |
| 59 | |
Fan Zhang | d30c05c | 2017-07-21 16:59:57 -0700 | [diff] [blame] | 60 | /** |
Fan Zhang | dadfd50 | 2017-07-26 11:00:51 -0700 | [diff] [blame] | 61 | * Posts runnable in background using shared background thread pool. |
| 62 | */ |
| 63 | public static void postOnBackgroundThread(Runnable runnable) { |
| 64 | if (sSingleThreadExecutor == null) { |
| 65 | sSingleThreadExecutor = Executors.newSingleThreadExecutor(); |
| 66 | } |
| 67 | sSingleThreadExecutor.execute(runnable); |
| 68 | } |
| 69 | |
| 70 | /** |
Fan Zhang | d30c05c | 2017-07-21 16:59:57 -0700 | [diff] [blame] | 71 | * Posts the runnable on the main thread. |
| 72 | */ |
| 73 | public static void postOnMainThread(Runnable runnable) { |
| 74 | getUiThreadHandler().post(runnable); |
| 75 | } |
| 76 | |
Juan Lang | 5efe8f0 | 2017-05-04 16:59:46 -0700 | [diff] [blame] | 77 | } |