summaryrefslogtreecommitdiffstats
path: root/src/com/android/documentsui/ThreadHelper.java
diff options
context:
space:
mode:
authorXin Li <delphij@google.com>2021-10-06 22:53:54 +0000
committerXin Li <delphij@google.com>2021-10-06 22:53:54 +0000
commit8c348c41a50cb3fe83e9d2403cc34db5d5ddcb70 (patch)
tree2ba30a68b1e5a2246c7e071219139cea61cab0e6 /src/com/android/documentsui/ThreadHelper.java
parent4bc189377da5f3cb27fb439d677cb89f3390a7a6 (diff)
parent84af7ecf6ae45a2ab3aba9b0019baa191b430af5 (diff)
downloadplatform_packages_apps_DocumentsUI-master.tar.gz
platform_packages_apps_DocumentsUI-master.tar.bz2
platform_packages_apps_DocumentsUI-master.zip
Merge Android 12HEADmaster
Bug: 202323961 Merged-In: I64e7be8bd815a3f3bf84277ffb9ea801a5dceb24 Change-Id: Ib2ecaa196b974cec584f6ae5c1e8b4092818d73a
Diffstat (limited to 'src/com/android/documentsui/ThreadHelper.java')
-rw-r--r--src/com/android/documentsui/ThreadHelper.java58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/com/android/documentsui/ThreadHelper.java b/src/com/android/documentsui/ThreadHelper.java
new file mode 100644
index 000000000..bb123b69b
--- /dev/null
+++ b/src/com/android/documentsui/ThreadHelper.java
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.documentsui;
+
+import android.os.Looper;
+
+/** Class for handler/thread utils. */
+public final class ThreadHelper {
+ private ThreadHelper() {
+ }
+
+ static final String MUST_NOT_ON_MAIN_THREAD_MSG =
+ "This method should not be called on main thread.";
+ static final String MUST_ON_MAIN_THREAD_MSG =
+ "This method should only be called on main thread.";
+
+ /** Verifies that current thread is not the UI thread. */
+ public static void assertNotOnMainThread() {
+ if (Looper.getMainLooper().getThread() == Thread.currentThread()) {
+ fatalAssert(MUST_NOT_ON_MAIN_THREAD_MSG);
+ }
+ }
+
+ /** Verifies that current thread is the UI thread. */
+ public static void assertOnMainThread() {
+ if (Looper.getMainLooper().getThread() != Thread.currentThread()) {
+ fatalAssert(MUST_ON_MAIN_THREAD_MSG);
+ }
+ }
+
+ /**
+ * Exceptions thrown in background threads are silently swallowed on Android. Use the
+ * uncaught exception handler of the UI thread to force the app to crash.
+ */
+ public static void fatalAssert(final String message) {
+ crashMainThread(new AssertionError(message));
+ }
+
+ private static void crashMainThread(Throwable t) {
+ Thread.UncaughtExceptionHandler uiThreadExceptionHandler =
+ Looper.getMainLooper().getThread().getUncaughtExceptionHandler();
+ uiThreadExceptionHandler.uncaughtException(Thread.currentThread(), t);
+ }
+}