/** * Checks if the current thread is a background thread and, optionally, restricts it with passed * serials. If no serials passed and current thread is the UI thread, then {@link * WrongThreadListener#onBgExpected(String...)} will be called. If the current thread is not UI * and serials list is empty, then this method just returns. Otherwise, if the method was called * not during {@link Task} execution or the task has no serial, then the {@link * WrongThreadListener#onWrongBgSerial(String, String...)} will be called with null for the first * parameter. If task has a serial but passed serials don't contain that, then {@link * WrongThreadListener#onWrongBgSerial(String, String...)} will be called with the task's serial * for the first parameter. * * @param serials (optional) list of allowed serials */ public static void checkBgThread(String... serials) { if (serials.length == 0) { if (Looper.getMainLooper().getThread() == Thread.currentThread()) { wrongThreadListener.onBgExpected(serials); } return; } String current = CURRENT_SERIAL.get(); if (current == null) { wrongThreadListener.onWrongBgSerial(null, serials); return; } for (String serial : serials) { if (serial.equals(current)) { return; } } wrongThreadListener.onWrongBgSerial(current, serials); }
/** * Checks if the current thread is UI thread and notifies {@link * BackgroundExecutor.WrongThreadListener#onUiExpected()} if it doesn't. */ public static void checkUiThread() { if (Looper.getMainLooper().getThread() != Thread.currentThread()) { wrongThreadListener.onUiExpected(); } }