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 18 import android.os.Handler; 19 import android.os.Looper; 20 21 import androidx.annotation.NonNull; 22 23 import com.google.common.util.concurrent.ListenableFuture; 24 import com.google.common.util.concurrent.ListeningExecutorService; 25 import com.google.common.util.concurrent.MoreExecutors; 26 27 import java.util.concurrent.Callable; 28 import java.util.concurrent.Executors; 29 30 public class ThreadUtils { 31 32 private static volatile Thread sMainThread; 33 private static volatile Handler sMainThreadHandler; 34 private static volatile ListeningExecutorService sListeningService; 35 36 /** 37 * Returns true if the current thread is the UI thread. 38 */ isMainThread()39 public static boolean isMainThread() { 40 if (sMainThread == null) { 41 sMainThread = Looper.getMainLooper().getThread(); 42 } 43 return Thread.currentThread() == sMainThread; 44 } 45 46 /** 47 * Returns a shared UI thread handler. 48 */ 49 @NonNull getUiThreadHandler()50 public static Handler getUiThreadHandler() { 51 if (sMainThreadHandler == null) { 52 sMainThreadHandler = new Handler(Looper.getMainLooper()); 53 } 54 55 return sMainThreadHandler; 56 } 57 58 /** 59 * Checks that the current thread is the UI thread. Otherwise throws an exception. 60 */ ensureMainThread()61 public static void ensureMainThread() { 62 if (!isMainThread()) { 63 throw new RuntimeException("Must be called on the UI thread"); 64 } 65 } 66 67 /** 68 * Posts runnable in background using shared background thread pool. 69 * 70 * @return A future of the task that can be monitored for updates or cancelled. 71 */ 72 @SuppressWarnings("rawtypes") 73 @NonNull postOnBackgroundThread(@onNull Runnable runnable)74 public static ListenableFuture postOnBackgroundThread(@NonNull Runnable runnable) { 75 return getBackgroundExecutor().submit(runnable); 76 } 77 78 /** 79 * Posts callable in background using shared background thread pool. 80 * 81 * @return A future of the task that can be monitored for updates or cancelled. 82 */ 83 @NonNull postOnBackgroundThread(@onNull Callable<T> callable)84 public static <T> ListenableFuture<T> postOnBackgroundThread(@NonNull Callable<T> callable) { 85 return getBackgroundExecutor().submit(callable); 86 } 87 88 /** 89 * Posts the runnable on the main thread. 90 * 91 * @deprecated moving work to the main thread should be done via the main executor provided to 92 * {@link com.google.common.util.concurrent.FutureCallback} via 93 * {@link android.content.Context#getMainExecutor()} or by calling an SDK method such as 94 * {@link android.app.Activity#runOnUiThread(Runnable)} or 95 * {@link android.content.Context#getMainThreadHandler()} where appropriate. 96 */ 97 @Deprecated postOnMainThread(@onNull Runnable runnable)98 public static void postOnMainThread(@NonNull Runnable runnable) { 99 getUiThreadHandler().post(runnable); 100 } 101 102 /** 103 * Provides a shared {@link ListeningExecutorService} created using a fixed thread pool executor 104 */ 105 @NonNull getBackgroundExecutor()106 public static synchronized ListeningExecutorService getBackgroundExecutor() { 107 if (sListeningService == null) { 108 sListeningService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool( 109 Runtime.getRuntime().availableProcessors())); 110 } 111 return sListeningService; 112 } 113 } 114