content: package androidx.activity;
  
  import androidx.lifecycle.Lifecycle;
  import androidx.lifecycle.LifecycleEventObserver;
  import androidx.lifecycle.LifecycleOwner;
  import java.util.ArrayDeque;
  import java.util.Iterator;
  
  public final class OnBackPressedDispatcher {
      private final Runnable mFallbackOnBackPressed;
      final ArrayDeque<OnBackPressedCallback> mOnBackPressedCallbacks;
  
      private class LifecycleOnBackPressedCancellable implements LifecycleEventObserver, Cancellable {
          private Cancellable mCurrentCancellable;
          private final Lifecycle mLifecycle;
          private final OnBackPressedCallback mOnBackPressedCallback;
  
          LifecycleOnBackPressedCancellable(Lifecycle lifecycle, OnBackPressedCallback onBackPressedCallback) {
              this.mLifecycle = lifecycle;
              this.mOnBackPressedCallback = onBackPressedCallback;
              lifecycle.addObserver(this);
          }
  
          public void cancel() {
              this.mLifecycle.removeObserver(this);
              this.mOnBackPressedCallback.removeCancellable(this);
              Cancellable cancellable = this.mCurrentCancellable;
              if (cancellable != null) {
                  cancellable.cancel();
                  this.mCurrentCancellable = null;
              }
          }
  
          public void onStateChanged(LifecycleOwner source, Lifecycle.Event event) {
              if (event == Lifecycle.Event.ON_START) {
                  this.mCurrentCancellable = OnBackPressedDispatcher.this.addCancellableCallback(this.mOnBackPressedCallback);
              } else if (event == Lifecycle.Event.ON_STOP) {
                  Cancellable cancellable = this.mCurrentCancellable;
                  if (cancellable != null) {
                      cancellable.cancel();
                  }
              } else if (event == Lifecycle.Event.ON_DESTROY) {
                  cancel();
              }
          }
      }
  
      private class OnBackPressedCancellable implements Cancellable {
          private final OnBackPressedCallback mOnBackPressedCallback;
  
          OnBackPressedCancellable(OnBackPressedCallback onBackPressedCallback) {
              this.mOnBackPressedCallback = onBackPressedCallback;
          }
  
          public void cancel() {
              OnBackPressedDispatcher.this.mOnBackPressedCallbacks.remove(this.mOnBackPressedCallback);
              this.mOnBackPressedCallback.removeCancellable(this);
          }
      }
  
      public OnBackPressedDispatcher() {
          this((Runnable) null);
      }
  
      public OnBackPressedDispatcher(Runnable fallbackOnBackPressed) {
          this.mOnBackPressedCallbacks = new ArrayDeque<>();
          this.mFallbackOnBackPressed = fallbackOnBackPressed;
      }
  
      public void addCallback(OnBackPressedCallback onBackPressedCallback) {
          addCancellableCallback(onBackPressedCallback);
      }
  
      public void addCallback(LifecycleOwner owner, OnBackPressedCallback onBackPressedCallback) {
          Lifecycle lifecycle = owner.getLifecycle();
          if (lifecycle.getCurrentState() != Lifecycle.State.DESTROYED) {
              onBackPressedCallback.addCancellable(new LifecycleOnBackPressedCancellable(lifecycle, onBackPressedCallback));
          }
      }
  
      /* access modifiers changed from: package-private */
      public Cancellable addCancellableCallback(OnBackPressedCallback onBackPressedCallback) {
          this.mOnBackPressedCallbacks.add(onBackPressedCallback);
          OnBackPressedCancellable onBackPressedCancellable = new OnBackPressedCancellable(onBackPressedCallback);
          onBackPressedCallback.addCancellable(onBackPressedCancellable);
          return onBackPressedCancellable;
      }
  
      public boolean hasEnabledCallbacks() {
          Iterator<OnBackPressedCallback> descendingIterator = this.mOnBackPressedCallbacks.descendingIterator();
          while (descendingIterator.hasNext()) {
              if (descendingIterator.next().isEnabled()) {
                  return true;
              }
          }
          return false;
      }
  
      public void onBackPressed() {
          Iterator<OnBackPressedCallback> descendingIterator = this.mOnBackPressedCallbacks.descendingIterator();
          while (descendingIterator.hasNext()) {
              OnBackPressedCallback next = descendingIterator.next();
              if (next.isEnabled()) {
                  next.handleOnBackPressed();
                  return;
              }
          }
          Runnable runnable = this.mFallbackOnBackPressed;
          if (runnable != null) {
              runnable.run();
          }
      }
  }
  