private void addCallbacks(
        final Pair<Consumer<T>, ProducerContext> consumerContextPair,
        final ProducerContext producerContext) {
      producerContext.addCallbacks(
          new BaseProducerContextCallbacks() {
            @Override
            public void onCancellationRequested() {
              BaseProducerContext contextToCancel = null;
              List<ProducerContextCallbacks> isPrefetchCallbacks = null;
              List<ProducerContextCallbacks> priorityCallbacks = null;
              List<ProducerContextCallbacks> isIntermediateResultExpectedCallbacks = null;
              final boolean pairWasRemoved;

              synchronized (Multiplexer.this) {
                pairWasRemoved = mConsumerContextPairs.remove(consumerContextPair);
                if (pairWasRemoved) {
                  if (mConsumerContextPairs.isEmpty()) {
                    contextToCancel = mMultiplexProducerContext;
                  } else {
                    isPrefetchCallbacks = updateIsPrefetch();
                    priorityCallbacks = updatePriority();
                    isIntermediateResultExpectedCallbacks = updateIsIntermediateResultExpected();
                  }
                }
              }

              BaseProducerContext.callOnIsPrefetchChanged(isPrefetchCallbacks);
              BaseProducerContext.callOnPriorityChanged(priorityCallbacks);
              BaseProducerContext.callOnIsIntermediateResultExpectedChanged(
                  isIntermediateResultExpectedCallbacks);

              if (contextToCancel != null) {
                if (mKeepCancelledFetchAsLowPriority && !contextToCancel.isPrefetch()) {
                  List<ProducerContextCallbacks> callbacks =
                      contextToCancel.setPriorityNoCallbacks(Priority.LOW);
                  BaseProducerContext.callOnPriorityChanged(callbacks);
                } else {
                  contextToCancel.cancel();
                }
              }
              if (pairWasRemoved) {
                consumerContextPair.first.onCancellation();
              }
            }

            @Override
            public void onIsPrefetchChanged() {
              BaseProducerContext.callOnIsPrefetchChanged(updateIsPrefetch());
            }

            @Override
            public void onIsIntermediateResultExpectedChanged() {
              BaseProducerContext.callOnIsIntermediateResultExpectedChanged(
                  updateIsIntermediateResultExpected());
            }

            @Override
            public void onPriorityChanged() {
              BaseProducerContext.callOnPriorityChanged(updatePriority());
            }
          });
    }
Register callbacks to be called when cancellation of consumer is requested, or if the
prefetch status of the consumer changes.
imagepipeline/src/main/java/com/facebook/imagepipeline/producers/MultiplexProducer.java