@Override public void handleMessage(Message message) { if (message.what == R.id.decode) { //Log.d(TAG, "Got decode message"); decode((byte[]) message.obj, message.arg1, message.arg2); }else if (message.what == R.id.quit) { Looper.myLooper().quit(); } }
@Override protected void onDestroy() { super.onDestroy(); if (mBackgroundHandler != null) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { mBackgroundHandler.getLooper().quitSafely(); } else { mBackgroundHandler.getLooper().quit(); } mBackgroundHandler = null; } }
@Override public void handleMessage(Message message) { if (message.what == R.id.decode) { decode((byte[]) message.obj, message.arg1, message.arg2); } else if (message.what == R.id.quit) { Looper.myLooper().quit(); } }
@Override public boolean performInteraction(@NonNull Context context, @NonNull CoreConfiguration config, @NonNull File reportFile) { Looper.prepare(); ToastSender.sendToast(context, ConfigUtils.getPluginConfiguration(config, ToastConfiguration.class).text(), Toast.LENGTH_LONG); final Looper looper = Looper.myLooper(); if(looper != null) { new Handler(looper).postDelayed(() -> { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { looper.quitSafely(); } else { looper.quit(); } }, TOAST_WAIT_DURATION); Looper.loop(); } return true; } }
@Override public void handleMessage(Message message) { if (message.what == R.id.decode) { //Log.d(TAG, "Got decode message"); decode((byte[]) message.obj, message.arg1, message.arg2); }else if (message.what == R.id.quit) { Looper.myLooper().quit(); } }
@Override public void notifyReportDropped(@NonNull final Context context, @NonNull final CoreConfiguration config) { final LimiterConfiguration limiterConfiguration = ConfigUtils.getPluginConfiguration(config, LimiterConfiguration.class); if (limiterConfiguration.ignoredCrashToast() != null) { final Future<?> future = Executors.newSingleThreadExecutor().submit(() -> { Looper.prepare(); ToastSender.sendToast(context, limiterConfiguration.ignoredCrashToast(), Toast.LENGTH_LONG); final Looper looper = Looper.myLooper(); if (looper != null) { new Handler(looper).postDelayed(() -> { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { looper.quitSafely(); } else { looper.quit(); } }, 4000); Looper.loop(); } }); while (!future.isDone()) { try { future.get(); } catch (InterruptedException ignored) { } catch (ExecutionException e) { //ReportInteraction crashed, so ignore it break; } } } }
@Override public void handleMessage(Message message) { if (!running) { return; } if (message.what == R.id.decode) { decode((byte[]) message.obj, message.arg1, message.arg2); } else if (message.what == R.id.quit) { running = false; Looper.myLooper().quit(); } }
void shutdown() { this.handler.getLooper().quit(); }
@Override public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { if (playbackState == Player.STATE_ENDED || (playbackState == Player.STATE_IDLE && playbackException != null)) { player.release(); Looper.myLooper().quit(); } } }
@Override public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { if (playbackState == Player.STATE_ENDED || (playbackState == Player.STATE_IDLE && playbackException != null)) { player.release(); Looper.myLooper().quit(); } }
@Test(expected = RuntimeException.class) public void shouldThrowRuntimeExceptionIfTryingToQuitMainLooper() throws Exception { Looper.getMainLooper().quit(); }
@Override public void handleMessage(Message message) { if (!running) { return; } switch (message.what) { case R.id.ocr_continuous_decode: // Only request a decode if a request is not already pending. if (!isDecodePending) { isDecodePending = true; ocrContinuousDecode((byte[]) message.obj, message.arg1, message.arg2); } break; case R.id.ocr_decode: ocrDecode((byte[]) message.obj, message.arg1, message.arg2); break; case R.id.quit: running = false; Looper.myLooper().quit(); break; } }
@Override public void onDestroy() { super.onDestroy(); mPlayHandler.removeCallbacksAndMessages(null); mPlayHandler.getLooper().quit(); mPlayHandler = null; mProgress.removeCallbacks(mUpdateProgress); stopAnim(); }
@Test public void shutdown() { executor.shutdown(); verify(handler.getLooper()).quit(); }
@Test public void shouldStopThreadIfLooperIsQuit() throws Exception { handlerThread = new HandlerThread("test1"); handlerThread.start(); Looper looper = handlerThread.getLooper(); assertFalse(shadowOf(looper).quit); looper.quit(); handlerThread.join(); assertFalse(handlerThread.isAlive()); assertTrue(shadowOf(looper).quit); handlerThread = null; }
@Resetter public static synchronized void resetThreadLoopers() { // Blech. We need to keep the main looper because somebody might refer to it in a static // field. The other loopers need to be wrapped in WeakReferences so that they are not prevented from // being garbage collected. if (!isMainThread()) { throw new IllegalStateException("you should only be calling this from the main thread!"); } synchronized (loopingLoopers) { for (Looper looper : loopingLoopers.values()) { synchronized (looper) { if (!shadowOf(looper).quit) { looper.quit(); } else { // Reset the schedulers of all loopers. This prevents un-run tasks queued up in static // background handlers from leaking to subsequent tests. shadowOf(looper).getScheduler().reset(); shadowOf(looper.getQueue()).reset(); } } } } // Because resetStaticState() is called by ParallelUniverse on startup before prepareMainLooper() is // called, this might be null on that occasion. if (mainLooper != null) { shadowOf(mainLooper).reset(); } }
@Test public void threadShouldContinue_whenLooperQuits() throws InterruptedException { QuitThread test = getQuitThread(); assertThat(test.hasContinued).named("beforeJoin").isFalse(); test.looper.quit(); test.join(5000); assertThat(test.hasContinued).named("afterJoin").isTrue(); }
this.mHandler.getLooper().quit(); this.mHandler = null; this.mList = null;
@Test public void shouldNotQueueMessagesIfLooperIsQuit() throws Exception { HandlerThread ht = getHandlerThread(); Looper looper = ht.getLooper(); looper.quit(); assertThat(shadowOf(looper).hasQuit()).named("hasQuit").isTrue(); assertThat(shadowOf(looper).post(new Runnable() { @Override public void run() { } }, 0)).named("post").isFalse(); assertThat(shadowOf(looper).postAtFrontOfQueue(new Runnable() { @Override public void run() { } })).named("postAtFrontOfQueue").isFalse(); assertThat(shadowOf(looper).getScheduler().areAnyRunnable()).named("areAnyRunnable").isFalse(); }
@Test public void shouldThrowawayRunnableQueueIfLooperQuits() throws Exception { HandlerThread ht = getHandlerThread(); Looper looper = ht.getLooper(); shadowOf(looper).pause(); shadowOf(looper).post(new Runnable() { @Override public void run() { } }, 0); looper.quit(); assertThat(shadowOf(looper).hasQuit()).named("hasQuit").isTrue(); assertThat(shadowOf(looper).getScheduler().areAnyRunnable()).named("areAnyRunnable").isFalse(); assertThat(shadowOf(looper.getQueue()).getHead()).named("queue").isNull(); }