blob: 571d52980a7de2f2195939c992a6ed903d0576a0 [file] [log] [blame]
Evan Charlton0958f532014-01-10 16:58:02 -08001/*
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 Gunn7cc70b42014-09-12 22:17:27 -070017package com.android.server.telecom;
Evan Charlton0958f532014-01-10 16:58:02 -080018
19import android.os.Looper;
Brad Ebingera3eccfe2016-10-05 15:45:22 -070020import android.telecom.Log;
Evan Charlton0958f532014-01-10 16:58:02 -080021
22/**
Sailesh Nepald2dbf122014-03-17 21:19:42 -070023 * Helper methods to deal with threading related tasks.
Evan Charlton0958f532014-01-10 16:58:02 -080024 */
25public 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 Nepalf1c191d2014-03-07 18:17:39 -080041 Log.wtf(TAG, new IllegalStateException(), "Must be on the main thread!");
Evan Charlton0958f532014-01-10 16:58:02 -080042 }
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 Nepalf1c191d2014-03-07 18:17:39 -080051 Log.wtf(TAG, new IllegalStateException(), "Must not be on the main thread!");
Evan Charlton0958f532014-01-10 16:58:02 -080052 }
53 }
54}