Example usage for android.os CancellationSignal isCanceled

List of usage examples for android.os CancellationSignal isCanceled

Introduction

In this page you can find the example usage for android.os CancellationSignal isCanceled.

Prototype

public boolean isCanceled() 

Source Link

Document

Returns true if the operation has been canceled.

Usage

From source file:net.sf.fdshare.RootFileProvider.java

/**
 * {@inheritDoc}//w ww . j  ava 2s . com
 *
 * Note, that cancellation is inherently racy. The caller must close whatever Cursor may be returned regardless.
 */
@Override
@SuppressLint("NewApi")
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder,
        CancellationSignal signal) {
    Cursor result = null;

    try (Closeable cancellation = new ThreadInterrupter(signal)) {
        result = super.query(uri, projection, selection, selectionArgs, sortOrder);
        return result;
    } catch (IOException ioe) {
        return null;
    } finally {
        if (signal != null && signal.isCanceled()) {
            if (result != null) {
                result.close();
            }

            // override whatever exception have resulted from interruption
            signal.throwIfCanceled();
        }
    }
}

From source file:com.seafile.seadroid2.provider.SeafileProvider.java

/**
 * Load a file from the Seafile server./*from   w w  w  .j  a v  a 2 s.co  m*/
 *
 * This might take a while, therefore we have to listen to the CancellationSignal and abort
 * if it says so.
 *
 * @param signal CancellationSignal
 * @param dm DataManager object belonging to the seafile account, where the file lies
 * @param repo The repository where the file lies
 * @param path File path
 * @return
 * @throws FileNotFoundException
 */
private static File getFile(final CancellationSignal signal, DataManager dm, SeafRepo repo, String path)
        throws FileNotFoundException {

    try {
        // fetch the file from the Seafile server.
        File f = dm.getFile(repo.getName(), repo.getID(), path, new ProgressMonitor() {
            @Override
            public void onProgressNotify(long total, boolean updateTotal) {
            }

            @Override
            public boolean isCancelled() {
                // cancel the download if the client has lost interest.
                if (signal != null)
                    return signal.isCanceled();
                else
                    return false;
            }
        });

        if (f == null) {
            throw new FileNotFoundException();
        }

        if (f.isDirectory()) {
            throw new FileNotFoundException();
        }

        return f;

    } catch (SeafException e) {
        throw new FileNotFoundException();
    }
}

From source file:net.sf.fdshare.RootFileProvider.java

/**
 * {@inheritDoc}//from ww w. j  av  a 2 s .  co m
 *
 * Note, that cancellation is inherently racy. The caller must close whatever descriptor may be returned regardless.
 */
@Override
@SuppressLint("NewApi")
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public ParcelFileDescriptor openFile(Uri uri, String mode, CancellationSignal signal)
        throws FileNotFoundException {
    ParcelFileDescriptor result = null;

    try (Closeable cancellation = new ThreadInterrupter(signal)) {
        result = super.openFile(uri, mode);
        return result;
    } catch (IOException ioe) {
        throw new FileNotFoundException(ioe.getMessage());
    } finally {
        if (signal != null && signal.isCanceled()) {
            if (result != null) {
                try {
                    result.close();
                } catch (IOException ignored) {
                }
            }

            // override whatever exception have resulted from interruption
            signal.throwIfCanceled();
        }
    }
}

From source file:org.alfresco.mobile.android.application.providers.storage.StorageAccessDocumentsProvider.java

public static boolean copyFile(InputStream src, long size, File dest, CancellationSignal signal) {
    IOUtils.ensureOrCreatePathAndFile(dest);
    OutputStream os = null;/*from w  w  w .ja  va2s.  c  o  m*/
    boolean copied = true;
    int downloaded = 0;

    try {
        os = new BufferedOutputStream(new FileOutputStream(dest));

        byte[] buffer = new byte[MAX_BUFFER_SIZE];

        while (size - downloaded > 0) {
            if (size - downloaded < MAX_BUFFER_SIZE) {
                buffer = new byte[(int) (size - downloaded)];
            }

            int read = src.read(buffer);
            if (read == -1) {
                break;
            }

            os.write(buffer, 0, read);
            downloaded += read;
            if (signal != null && signal.isCanceled()) {
                signal.throwIfCanceled();
            }
        }
    } catch (Exception e) {
        Log.e(TAG, Log.getStackTraceString(e));
        copied = false;
    } finally {
        IOUtils.closeStream(src);
        IOUtils.closeStream(os);
    }
    return copied;
}