content: package androidx.core.view;
  
  import android.view.View;
  import android.view.ViewTreeObserver;
  
  public final class OneShotPreDrawListener implements ViewTreeObserver.OnPreDrawListener, View.OnAttachStateChangeListener {
      private final Runnable mRunnable;
      private final View mView;
      private ViewTreeObserver mViewTreeObserver;
  
      private OneShotPreDrawListener(View view, Runnable runnable) {
          this.mView = view;
          this.mViewTreeObserver = view.getViewTreeObserver();
          this.mRunnable = runnable;
      }
  
      public static OneShotPreDrawListener add(View view, Runnable runnable) {
          if (view == null) {
              throw new NullPointerException("view == null");
          } else if (runnable != null) {
              OneShotPreDrawListener oneShotPreDrawListener = new OneShotPreDrawListener(view, runnable);
              view.getViewTreeObserver().addOnPreDrawListener(oneShotPreDrawListener);
              view.addOnAttachStateChangeListener(oneShotPreDrawListener);
              return oneShotPreDrawListener;
          } else {
              throw new NullPointerException("runnable == null");
          }
      }
  
      public boolean onPreDraw() {
          removeListener();
          this.mRunnable.run();
          return true;
      }
  
      public void onViewAttachedToWindow(View v) {
          this.mViewTreeObserver = v.getViewTreeObserver();
      }
  
      public void onViewDetachedFromWindow(View v) {
          removeListener();
      }
  
      public void removeListener() {
          if (this.mViewTreeObserver.isAlive()) {
              this.mViewTreeObserver.removeOnPreDrawListener(this);
          } else {
              this.mView.getViewTreeObserver().removeOnPreDrawListener(this);
          }
          this.mView.removeOnAttachStateChangeListener(this);
      }
  }
  