@Override public Looper getLooper() { return handler.getLooper(); }
@Override public Looper getApplicationLooper() { return eventHandler.getLooper(); }
@Override public Looper getLooper() { return handler.getLooper(); }
@Override public Looper getApplicationLooper() { return handler.getLooper(); }
/** * Checks whether the current thread is the same thread that the {@link Handler} is associated * with. * @return true if the current thread is the same thread that the {@link Handler} is associated * with; otherwise false. */ public static boolean checkThreadAccess(Handler handler) { return Looper.myLooper() == handler.getLooper(); }
private void postOrRun(Handler handler, Runnable runnable) { if (handler.getLooper() == Looper.myLooper()) { runnable.run(); } else { handler.post(runnable); } }
@Override public void execute(@NonNull Runnable command) { // If we're already on the same thread then we can execute this synchronously if (Looper.myLooper() == handler.getLooper()) { command.run(); } else { handler.post(command); } } }
@Override public Looper getApplicationLooper() { return handler.getLooper(); }
@Override public Looper getLooper() { return handler.getLooper(); }
private void sendMessageToTarget(PlayerMessage message) throws ExoPlaybackException { if (message.getHandler().getLooper() == handler.getLooper()) { deliverMessage(message); if (playbackInfo.playbackState == Player.STATE_READY || playbackInfo.playbackState == Player.STATE_BUFFERING) { // The message may have caused something to change that now requires us to do work. handler.sendEmptyMessage(MSG_DO_SOME_WORK); } } else { handler.obtainMessage(MSG_SEND_MESSAGE_TO_TARGET_THREAD, message).sendToTarget(); } }
@Override public void onDestroy() { super.onDestroy(); mPlayHandler.removeCallbacksAndMessages(null); mPlayHandler.getLooper().quit(); mPlayHandler = null; mProgress.removeCallbacks(mUpdateProgress); stopAnim(); }
private void unschedule() { Handler target = realMessage.getTarget(); if (target != null && scheduledRunnable != null) { shadowOf(target.getLooper()).getScheduler().remove(scheduledRunnable); scheduledRunnable = null; } }
@Test public void testRemoveCallbacks() throws Exception { Handler handler = new Handler(); ShadowLooper shadowLooper = shadowOf(handler.getLooper()); shadowLooper.pause(); handler.post(scratchRunnable); handler.removeCallbacks(scratchRunnable); shadowLooper.unPause(); assertThat(scratchRunnable.wasRun).isFalse(); }
/** * Runs the provided {@link Runnable} on the main thread, blocking until execution completes or * until timeout milliseconds have passed. * * @param timeoutMs the maximum time to wait in milliseconds. * @param runnable The {@link Runnable} to run. */ public void runOnMainThread(int timeoutMs, final Runnable runnable) { if (Looper.myLooper() == handler.getLooper()) { runnable.run(); } else { final ConditionVariable finishedCondition = new ConditionVariable(); handler.post( () -> { runnable.run(); finishedCondition.open(); }); assertThat(finishedCondition.block(timeoutMs)).isTrue(); } }
private void runOnUiThread(Runnable action) { // This is meant to emulate the behavior of Activity.runOnUiThread(); shadowOf(handler.getLooper()).getScheduler().post(action); } }
private void runOnUiThread(Runnable action) { // This is meant to emulate the behavior of Activity.runOnUiThread(); shadowOf(handler.getLooper()).getScheduler().post(action); } }
@Test public void sendBroadcast_shouldSendIntentUsingHandlerIfOneIsProvided() { HandlerThread handlerThread = new HandlerThread("test"); handlerThread.start(); Handler handler = new Handler(handlerThread.getLooper()); assertNotSame(handler.getLooper(), Looper.getMainLooper()); BroadcastReceiver receiver = broadcastReceiver("Larry"); contextWrapper.registerReceiver(receiver, intentFilter("foo", "baz"), null, handler); assertThat(shadowOf(handler.getLooper()).getScheduler().size()).isEqualTo(0); contextWrapper.sendBroadcast(new Intent("foo")); assertThat(shadowOf(handler.getLooper()).getScheduler().size()).isEqualTo(1); shadowOf(handlerThread.getLooper()).idle(); assertThat(shadowOf(handler.getLooper()).getScheduler().size()).isEqualTo(0); assertThat(transcript).containsExactly("Larry notified of foo"); }
@Test public void testReleasesResourceIfCancelledOnReady() { Looper looper = harness.mainHandler.getLooper(); Shadows.shadowOf(looper).pause(); final EngineJob<Object> job = harness.getJob(); job.start(harness.decodeJob); job.cancel(); job.onResourceReady(harness.resource, harness.dataSource); verify(harness.resource).recycle(); }
@Test public void testCreationWithInputs() { ComponentTree componentTree = mComponentTreeBuilder .layoutThreadLooper(mLooper) .build(); assertSameAsInternalState(componentTree, mRoot, "mRoot"); assertEqualToInternalState(componentTree, true, "mIsLayoutDiffingEnabled"); assertThat(componentTree.isIncrementalMountEnabled()).isTrue(); assertThat(mContext.getLogger()).isEqualTo(mComponentsLogger); assertThat(mContext.getLogTag()).isEqualTo(mLogTag); Handler handler = getInternalState(componentTree, "mLayoutThreadHandler"); assertThat(mLooper).isSameAs(handler.getLooper()); }