ThreadUtil.java revision a3eccfee788c3ac3c831a443b085b141b39bb63d
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 17package com.android.server.telecom; 18 19import android.os.Looper; 20import android.telecom.Log; 21 22/** 23 * Helper methods to deal with threading related tasks. 24 */ 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()) { 41 Log.wtf(TAG, new IllegalStateException(), "Must be on the main thread!"); 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()) { 51 Log.wtf(TAG, new IllegalStateException(), "Must not be on the main thread!"); 52 } 53 } 54} 55