content: package androidx.core.provider;
  
  import android.os.Handler;
  import android.os.HandlerThread;
  import android.os.Message;
  import java.util.concurrent.Callable;
  import java.util.concurrent.TimeUnit;
  import java.util.concurrent.atomic.AtomicBoolean;
  import java.util.concurrent.atomic.AtomicReference;
  import java.util.concurrent.locks.Condition;
  import java.util.concurrent.locks.ReentrantLock;
  
  @Deprecated
  public class SelfDestructiveThread {
      private static final int MSG_DESTRUCTION = 0;
      private static final int MSG_INVOKE_RUNNABLE = 1;
      private Handler.Callback mCallback = new Handler.Callback() {
          public boolean handleMessage(Message msg) {
              switch (msg.what) {
                  case 0:
                      SelfDestructiveThread.this.onDestruction();
                      return true;
                  case 1:
                      SelfDestructiveThread.this.onInvokeRunnable((Runnable) msg.obj);
                      return true;
                  default:
                      return true;
              }
          }
      };
      private final int mDestructAfterMillisec;
      private int mGeneration;
      private Handler mHandler;
      private final Object mLock = new Object();
      private final int mPriority;
      private HandlerThread mThread;
      private final String mThreadName;
  
      public interface ReplyCallback<T> {
          void onReply(T t);
      }
  
      public SelfDestructiveThread(String threadName, int priority, int destructAfterMillisec) {
          this.mThreadName = threadName;
          this.mPriority = priority;
          this.mDestructAfterMillisec = destructAfterMillisec;
          this.mGeneration = 0;
      }
  
      private void post(Runnable runnable) {
          synchronized (this.mLock) {
              if (this.mThread == null) {
                  HandlerThread handlerThread = new HandlerThread(this.mThreadName, this.mPriority);
                  this.mThread = handlerThread;
                  handlerThread.start();
                  this.mHandler = new Handler(this.mThread.getLooper(), this.mCallback);
                  this.mGeneration++;
              }
              this.mHandler.removeMessages(0);
              Handler handler = this.mHandler;
              handler.sendMessage(handler.obtainMessage(1, runnable));
          }
      }
  
      public int getGeneration() {
          int i;
          synchronized (this.mLock) {
              i = this.mGeneration;
          }
          return i;
      }
  
      public boolean isRunning() {
          boolean z;
          synchronized (this.mLock) {
              z = this.mThread != null;
          }
          return z;
      }
  
      /* access modifiers changed from: package-private */
      public void onDestruction() {
          synchronized (this.mLock) {
              if (!this.mHandler.hasMessages(1)) {
                  this.mThread.quit();
                  this.mThread = null;
                  this.mHandler = null;
              }
          }
      }
  
      /* access modifiers changed from: package-private */
      public void onInvokeRunnable(Runnable runnable) {
          runnable.run();
          synchronized (this.mLock) {
              this.mHandler.removeMessages(0);
              Handler handler = this.mHandler;
              handler.sendMessageDelayed(handler.obtainMessage(0), (long) this.mDestructAfterMillisec);
          }
      }
  
      public <T> void postAndReply(final Callable<T> callable, final ReplyCallback<T> replyCallback) {
          final Handler create = CalleeHandler.create();
          post(new Runnable() {
              public void run() {
                  Object obj;
                  try {
                      obj = callable.call();
                  } catch (Exception e) {
                      obj = null;
                  }
                  final Object obj2 = obj;
                  create.post(new Runnable() {
                      public void run() {
                          replyCallback.onReply(obj2);
                      }
                  });
              }
          });
      }
  
      public <T> T postAndWait(Callable<T> callable, int timeoutMillis) throws InterruptedException {
          final ReentrantLock reentrantLock = new ReentrantLock();
          Condition newCondition = reentrantLock.newCondition();
          final AtomicReference atomicReference = new AtomicReference();
          final AtomicBoolean atomicBoolean = new AtomicBoolean(true);
          final Callable<T> callable2 = callable;
          final Condition condition = newCondition;
          post(new Runnable() {
              public void run() {
                  try {
                      atomicReference.set(callable2.call());
                  } catch (Exception e) {
                  }
                  reentrantLock.lock();
                  try {
                      atomicBoolean.set(false);
                      condition.signal();
                  } finally {
                      reentrantLock.unlock();
                  }
              }
          });
          reentrantLock.lock();
          try {
              if (!atomicBoolean.get()) {
                  return atomicReference.get();
              }
              long nanos = TimeUnit.MILLISECONDS.toNanos((long) timeoutMillis);
              do {
                  try {
                      nanos = newCondition.awaitNanos(nanos);
                  } catch (InterruptedException e) {
                  }
                  if (!atomicBoolean.get()) {
                      T t = atomicReference.get();
                      reentrantLock.unlock();
                      return t;
                  }
              } while (nanos > 0);
              throw new InterruptedException("timeout");
          } finally {
              reentrantLock.unlock();
          }
      }
  }
  