Evan Charlton | 0958f53 | 2014-01-10 16:58:02 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014, 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 | |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 17 | package com.android.server.telecom; |
Evan Charlton | 0958f53 | 2014-01-10 16:58:02 -0800 | [diff] [blame] | 18 | |
| 19 | import android.os.Looper; |
Brad Ebinger | a3eccfe | 2016-10-05 15:45:22 -0700 | [diff] [blame] | 20 | import android.telecom.Log; |
Evan Charlton | 0958f53 | 2014-01-10 16:58:02 -0800 | [diff] [blame] | 21 | |
| 22 | /** |
Sailesh Nepal | d2dbf12 | 2014-03-17 21:19:42 -0700 | [diff] [blame] | 23 | * Helper methods to deal with threading related tasks. |
Evan Charlton | 0958f53 | 2014-01-10 16:58:02 -0800 | [diff] [blame] | 24 | */ |
| 25 | public final class ThreadUtil { |
| 26 | private static final String TAG = ThreadUtil.class.getSimpleName(); |
| 27 | |
| 28 | private ThreadUtil() {} |
| 29 | |
| 30 | /** @return whether this method is being called from the main (UI) thread. */ |
| 31 | public static boolean isOnMainThread() { |
| 32 | return Looper.getMainLooper() == Looper.myLooper(); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Checks that this is being executed on the main thread. If not, a message is logged at |
| 37 | * WTF-level priority. |
| 38 | */ |
| 39 | public static void checkOnMainThread() { |
| 40 | if (!isOnMainThread()) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 41 | Log.wtf(TAG, new IllegalStateException(), "Must be on the main thread!"); |
Evan Charlton | 0958f53 | 2014-01-10 16:58:02 -0800 | [diff] [blame] | 42 | } |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Checks that this is not being executed on the main thread. If so, a message is logged at |
| 47 | * WTF-level priority. |
| 48 | */ |
| 49 | public static void checkNotOnMainThread() { |
| 50 | if (isOnMainThread()) { |
Sailesh Nepal | f1c191d | 2014-03-07 18:17:39 -0800 | [diff] [blame] | 51 | Log.wtf(TAG, new IllegalStateException(), "Must not be on the main thread!"); |
Evan Charlton | 0958f53 | 2014-01-10 16:58:02 -0800 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | } |