package androidx.core.os; import androidx.annotation.Nullable; public final class CancellationSignal { private boolean mCancelInProgress; private Object mCancellationSignalObj; private boolean mIsCanceled; private OnCancelListener mOnCancelListener; public interface OnCancelListener { void onCancel(); } private void waitForCancelFinishedLocked() { while (this.mCancelInProgress) { try { wait(); } catch (InterruptedException unused) { } } } public void cancel() { OnCancelListener onCancelListener; Object obj; synchronized (this) { if (!this.mIsCanceled) { this.mIsCanceled = true; this.mCancelInProgress = true; onCancelListener = this.mOnCancelListener; obj = this.mCancellationSignalObj; } else { return; } } if (onCancelListener != null) { try { onCancelListener.onCancel(); } catch (Throwable th) { synchronized (this) { this.mCancelInProgress = false; notifyAll(); throw th; } } } if (obj != null) { ((android.os.CancellationSignal) obj).cancel(); } synchronized (this) { this.mCancelInProgress = false; notifyAll(); } } @Nullable public Object getCancellationSignalObject() { Object obj; synchronized (this) { if (this.mCancellationSignalObj == null) { android.os.CancellationSignal cancellationSignal = new android.os.CancellationSignal(); this.mCancellationSignalObj = cancellationSignal; if (this.mIsCanceled) { cancellationSignal.cancel(); } } obj = this.mCancellationSignalObj; } return obj; } public boolean isCanceled() { boolean z2; synchronized (this) { z2 = this.mIsCanceled; } return z2; } public void setOnCancelListener(@Nullable OnCancelListener onCancelListener) { synchronized (this) { waitForCancelFinishedLocked(); if (this.mOnCancelListener != onCancelListener) { this.mOnCancelListener = onCancelListener; if (this.mIsCanceled) { if (onCancelListener != null) { onCancelListener.onCancel(); } } } } } public void throwIfCanceled() { if (isCanceled()) { throw new OperationCanceledException(); } } }