code: public static void onStop(Service service, Runnable terminatedCallback, Consumer<Throwable> failureCallback, Executor executor) {
          ShutdownListener listener = new ShutdownListener(terminatedCallback, failureCallback);
          service.addListener(listener, executor);
  
          // addListener() will not invoke the callbacks if the service is already in a terminal state. As such, we need to
          // manually check for these states after registering the listener and invoke the appropriate callback. The
          // ShutdownListener will make sure they are not invoked multiple times.
          Service.State state = service.state();
          if (state == Service.State.FAILED) {
              // We don't care (or know) the state from which we came, so we just pass some random one.
              listener.failed(Service.State.FAILED, service.failureCause());
          } else if (state == Service.State.TERMINATED) {
              listener.terminated(Service.State.TERMINATED);
          }
      }
docs: Attaches the given callbacks which will be invoked when the given Service enters a TERMINATED or FAILED state.
  The callbacks are optional and may be invoked synchronously if the Service is already in one of these states.
  
  @param service            The Service to attach to.
  @param terminatedCallback (Optional) A Runnable that will be invoked if the Service enters a TERMINATED state.
  @param failureCallback    (Optional) A Runnable that will be invoked if the Service enters a FAILED state.
  @param executor           An Executor to use for callback invocations.