diff options
Diffstat (limited to 'platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/utils')
-rw-r--r-- | platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/utils/ThreadUtils.java | 41 |
1 files changed, 36 insertions, 5 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/utils/ThreadUtils.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/utils/ThreadUtils.java index 7dfd5ddd98..6c4018622c 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/utils/ThreadUtils.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/utils/ThreadUtils.java @@ -1,17 +1,48 @@ package com.mapbox.mapboxsdk.utils; +import android.content.Context; +import android.content.pm.ApplicationInfo; import android.os.Looper; +import android.support.annotation.NonNull; import com.mapbox.mapboxsdk.exceptions.CalledFromWorkerThreadException; +/** + * Utility class to verify if execution is running on the main thread. + * <p> + * Verification only runs for debug builds. + * </p> + */ public class ThreadUtils { + private static Boolean debug; + /** - * Validates if execution is occuring on the main thread. + * Initialises the thread utils, verifies debug state of the consuming app. + * + * @param context Context hosting the Mapbox Maps SDK for Android + * @return this */ - public static void checkThread(String origin) { - if (Looper.myLooper() != Looper.getMainLooper()) { - throw new CalledFromWorkerThreadException( - String.format("%s interactions should happen on the UI thread.",origin)); + public static ThreadUtils init(@NonNull Context context) { + debug = (0 != (context.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE)); + return null; + } + + /** + * Validates if execution is running on the main thread. + * + * @param origin the origin of the execution + */ + public static void checkThread(@NonNull String origin) { + if (debug == null) { + throw new IllegalStateException("ThreadUtils isn't correctly initialised"); + } + + if (debug) { + if (Looper.myLooper() != Looper.getMainLooper()) { + throw new CalledFromWorkerThreadException( + String.format("%s interactions should happen on the UI thread.", origin) + ); + } } } } |