Object getAndroidMainLooperOrNull() { try { return Looper.getMainLooper(); } catch (RuntimeException e) { // Not really a functional Android (e.g. "Stub!" maven dependencies) return null; } }
/** * Returns {@code true} if called on the main thread, {@code false} otherwise. */ public static boolean isOnMainThread() { return Looper.myLooper() == Looper.getMainLooper(); }
@SuppressWarnings("WeakerAccess") public BitmapPreFillRunner( BitmapPool bitmapPool, MemoryCache memoryCache, PreFillQueue allocationOrder) { this( bitmapPool, memoryCache, allocationOrder, DEFAULT_CLOCK, new Handler(Looper.getMainLooper())); }
@Override public void runOnUiThread (Runnable runnable) { if (Looper.myLooper() != Looper.getMainLooper()) { // The current thread is not the UI thread. // Let's post the runnable to the event queue of the UI thread. new Handler(Looper.getMainLooper()).post(runnable); } else { // The current thread is the UI thread already. // Let's execute the runnable immediately. runnable.run(); } }
@Override public void runOnUiThread (Runnable runnable) { if (Looper.myLooper() != Looper.getMainLooper()) { // The current thread is not the UI thread. // Let's post the runnable to the event queue of the UI thread. new Handler(Looper.getMainLooper()).post(runnable); } else { // The current thread is the UI thread already. // Let's execute the runnable immediately. runnable.run(); } }
@Override public void runOnUiThread (Runnable runnable) { if (Looper.myLooper() != Looper.getMainLooper()) { // The current thread is not the UI thread. // Let's post the runnable to the event queue of the UI thread. new Handler(Looper.getMainLooper()).post(runnable); } else { // The current thread is the UI thread already. // Let's execute the runnable immediately. runnable.run(); } }
@Override public void runOnUiThread (Runnable runnable) { if (Looper.myLooper() != Looper.getMainLooper()) { // The current thread is not the UI thread. // Let's post the runnable to the event queue of the UI thread. new Handler(Looper.getMainLooper()).post(runnable); } else { // The current thread is the UI thread already. // Let's execute the runnable immediately. runnable.run(); } }
public RequestManagerRetriever(@Nullable RequestManagerFactory factory) { this.factory = factory != null ? factory : DEFAULT_FACTORY; handler = new Handler(Looper.getMainLooper(), this /* Callback */); }
private Api() { HandlerThread bgThread = new HandlerThread("api_thread"); bgThread.start(); bgHandler = new Handler(bgThread.getLooper()); mainHandler = new Handler(Looper.getMainLooper()); // Do nothing. }
public AndroidWatchExecutor(long initialDelayMillis) { mainHandler = new Handler(Looper.getMainLooper()); HandlerThread handlerThread = new HandlerThread(LEAK_CANARY_THREAD_NAME); handlerThread.start(); backgroundHandler = new Handler(handlerThread.getLooper()); this.initialDelayMillis = initialDelayMillis; maxBackoffFactor = Long.MAX_VALUE / initialDelayMillis; }
/** * 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(); } }
@Override public void execute(@NonNull Retryable retryable) { if (Looper.getMainLooper().getThread() == Thread.currentThread()) { waitForIdle(retryable, 0); } else { postWaitForIdle(retryable, 0); } }
@Before public void setUpAndroid() throws Exception { mainPoster = new EventPostHandler(Looper.getMainLooper()); assertFalse(Looper.getMainLooper().getThread().equals(Thread.currentThread())); }
@Before public void setUp() throws Exception { handler = new Handler(Looper.getMainLooper()); backgroundPoster = new TestBackgroundPoster(eventBus); backgroundPoster.start(); }
@Subscribe public void onEvent(String event) { trackEvent(event); assertNotSame(Looper.getMainLooper(), Looper.myLooper()); }
@After public void tearDown() { Util.setSdkVersionInt(initialSdkVersion); Shadows.shadowOf(Looper.getMainLooper()).runToEndOfTasks(); assertThat(retriever.pendingRequestManagerFragments).isEmpty(); assertThat(retriever.pendingSupportRequestManagerFragments).isEmpty(); }
@Test public void testPostFromMain() throws InterruptedException { eventBus.register(this); postInMainThread("Hello"); waitForEventCount(1, 1000); assertEquals("Hello", lastEvent); assertFalse(lastThread.equals(Thread.currentThread())); assertFalse(lastThread.equals(Looper.getMainLooper().getThread())); }
@Test public void testRecyclesResourceSynchronouslyIfNotAlreadyRecyclingResource() { Resource<?> resource = mockResource(); Shadows.shadowOf(Looper.getMainLooper()).pause(); recycler.recycle(resource); verify(resource).recycle(); }
@Test public void testCreatesNewFragmentIfNoneExists() { for (RetrieverHarness harness : harnesses) { harness.doGet(); Shadows.shadowOf(Looper.getMainLooper()).runToEndOfTasks(); assertTrue(harness.hasFragmentWithTag(RequestManagerRetriever.FRAGMENT_TAG)); } }
@Test public void testCanCallGetInOnAttachToWindowInFragmentInViewPager() { // Robolectric by default runs messages posted to the main looper synchronously, the // framework does not. We post // to the main thread here to work around an issue caused by a recursive method call so we // need (and reasonably // expect) our message to not run immediately Shadows.shadowOf(Looper.getMainLooper()).pause(); Robolectric.buildActivity(Issue117Activity.class).create().start().resume().visible(); }