FileDocCategorySizeDatePackage
CancellationSignal.javaAPI DocAndroid 5.1 API6117Thu Mar 12 22:22:10 GMT 2015android.os

CancellationSignal

public final class CancellationSignal extends Object
Provides the ability to cancel an operation in progress.

Fields Summary
private boolean
mIsCanceled
private OnCancelListener
mOnCancelListener
private android.os.ICancellationSignal
mRemote
private boolean
mCancelInProgress
Constructors Summary
public CancellationSignal()
Creates a cancellation signal, initially not canceled.

    
Methods Summary
public voidcancel()
Cancels the operation and signals the cancellation listener. If the operation has not yet started, then it will be canceled as soon as it does.

        final OnCancelListener listener;
        final ICancellationSignal remote;
        synchronized (this) {
            if (mIsCanceled) {
                return;
            }
            mIsCanceled = true;
            mCancelInProgress = true;
            listener = mOnCancelListener;
            remote = mRemote;
        }

        try {
            if (listener != null) {
                listener.onCancel();
            }
            if (remote != null) {
                try {
                    remote.cancel();
                } catch (RemoteException ex) {
                }
            }
        } finally {
            synchronized (this) {
                mCancelInProgress = false;
                notifyAll();
            }
        }
    
public static android.os.ICancellationSignalcreateTransport()
Creates a transport that can be returned back to the caller of a Binder function and subsequently used to dispatch a cancellation signal.

return
The new cancellation signal transport.
hide

        return new Transport();
    
public static android.os.CancellationSignalfromTransport(android.os.ICancellationSignal transport)
Given a locally created transport, returns its associated cancellation signal.

param
transport The locally created transport, or null if none.
return
The associated cancellation signal, or null if none.
hide

        if (transport instanceof Transport) {
            return ((Transport)transport).mCancellationSignal;
        }
        return null;
    
public booleanisCanceled()
Returns true if the operation has been canceled.

return
True if the operation has been canceled.

        synchronized (this) {
            return mIsCanceled;
        }
    
public voidsetOnCancelListener(android.os.CancellationSignal$OnCancelListener listener)
Sets the cancellation listener to be called when canceled. This method is intended to be used by the recipient of a cancellation signal such as a database or a content provider to handle cancellation requests while performing a long-running operation. This method is not intended to be used by applications themselves. If {@link CancellationSignal#cancel} has already been called, then the provided listener is invoked immediately. This method is guaranteed that the listener will not be called after it has been removed.

param
listener The cancellation listener, or null to remove the current listener.

        synchronized (this) {
            waitForCancelFinishedLocked();

            if (mOnCancelListener == listener) {
                return;
            }
            mOnCancelListener = listener;
            if (!mIsCanceled || listener == null) {
                return;
            }
        }
        listener.onCancel();
    
public voidsetRemote(android.os.ICancellationSignal remote)
Sets the remote transport. If {@link CancellationSignal#cancel} has already been called, then the provided remote transport is canceled immediately. This method is guaranteed that the remote transport will not be called after it has been removed.

param
remote The remote transport, or null to remove.
hide

        synchronized (this) {
            waitForCancelFinishedLocked();

            if (mRemote == remote) {
                return;
            }
            mRemote = remote;
            if (!mIsCanceled || remote == null) {
                return;
            }
        }
        try {
            remote.cancel();
        } catch (RemoteException ex) {
        }
    
public voidthrowIfCanceled()
Throws {@link OperationCanceledException} if the operation has been canceled.

throws
OperationCanceledException if the operation has been canceled.

        if (isCanceled()) {
            throw new OperationCanceledException();
        }
    
private voidwaitForCancelFinishedLocked()

        while (mCancelInProgress) {
            try {
                wait();
            } catch (InterruptedException ex) {
            }
        }