Example usage for android.os CancellationSignal setOnCancelListener

List of usage examples for android.os CancellationSignal setOnCancelListener

Introduction

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

Prototype

public void setOnCancelListener(OnCancelListener listener) 

Source Link

Document

Sets the cancellation listener to be called when canceled.

Usage

From source file:net.sf.xfd.provider.PublicProvider.java

@Nullable
@Override/* w  w w .  j  a v  a2  s .c  o  m*/
public ParcelFileDescriptor openFile(@NonNull Uri uri, @NonNull String requestedMode, CancellationSignal signal)
        throws FileNotFoundException {
    String path = uri.getPath();

    assertAbsolute(path);

    final int readableMode = ParcelFileDescriptor.parseMode(requestedMode);

    if (signal != null) {
        final Thread theThread = Thread.currentThread();

        signal.setOnCancelListener(theThread::interrupt);
    }

    path = canonString(path);

    if (!path.equals(uri.getPath())) {
        uri = uri.buildUpon().path(path).build();
    }

    try {
        if (!checkAccess(uri, requestedMode)) {
            return null;
        }

        final OS rooted = base.getOS();

        if (rooted == null) {
            throw new FileNotFoundException("Failed to open " + uri.getPath() + ": unable to acquire access");
        }

        int openFlags;

        if ((readableMode & MODE_READ_ONLY) == readableMode) {
            openFlags = OS.O_RDONLY;
        } else if ((readableMode & MODE_WRITE_ONLY) == readableMode) {
            openFlags = OS.O_WRONLY;
        } else {
            openFlags = OS.O_RDWR;
        }

        if (signal == null) {
            openFlags |= NativeBits.O_NONBLOCK;
        }

        //noinspection WrongConstant
        @Fd
        int fd = rooted.open(path, openFlags, 0);

        return ParcelFileDescriptor.adoptFd(fd);
    } catch (IOException e) {
        throw new FileNotFoundException("Unable to open " + uri.getPath() + ": " + e.getMessage());
    } finally {
        if (signal != null) {
            signal.setOnCancelListener(null);
        }

        Thread.interrupted();
    }
}

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

@Override
public ParcelFileDescriptor openDocument(final String documentId, final String mode,
        final CancellationSignal signal) throws FileNotFoundException {

    if (!Utils.isNetworkOn())
        throw new FileNotFoundException();

    // open the file. this might involve talking to the seafile server. this will hang until
    // it is done.
    final Future<ParcelFileDescriptor> future = ConcurrentAsyncTask
            .submit(new Callable<ParcelFileDescriptor>() {

                @Override// www  .  j av  a 2  s.c o m
                public ParcelFileDescriptor call() throws Exception {

                    String path = docIdParser.getPathFromId(documentId);
                    DataManager dm = createDataManager(documentId);
                    String repoId = DocumentIdParser.getRepoIdFromId(documentId);

                    // we can assume that the repo is cached because the client has already seen it
                    SeafRepo repo = dm.getCachedRepoByID(repoId);
                    if (repo == null)
                        throw new FileNotFoundException();

                    File f = getFile(signal, dm, repo, path);

                    // return the file to the client.
                    String parentPath = Utils.getParentPath(path);
                    return makeParcelFileDescriptor(dm, repo.getName(), repoId, parentPath, f, mode);
                }
            });

    if (signal != null) {
        signal.setOnCancelListener(new CancellationSignal.OnCancelListener() {
            @Override
            public void onCancel() {
                Log.d(DEBUG_TAG, "openDocument cancelling download");
                future.cancel(true);
            }
        });
    }

    try {
        return future.get();
    } catch (InterruptedException e) {
        Log.d(DEBUG_TAG, "openDocument cancelled download");
        throw new FileNotFoundException();
    } catch (CancellationException e) {
        Log.d(DEBUG_TAG, "openDocumentThumbnail cancelled download");
        throw new FileNotFoundException();
    } catch (ExecutionException e) {
        Log.d(DEBUG_TAG, "could not open file", e);
        throw new FileNotFoundException();
    }
}

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

@Override
public AssetFileDescriptor openDocumentThumbnail(String documentId, Point sizeHint, CancellationSignal signal)
        throws FileNotFoundException {

    Log.d(DEBUG_TAG, "openDocumentThumbnail(): " + documentId);

    String repoId = DocumentIdParser.getRepoIdFromId(documentId);
    if (repoId.isEmpty()) {
        throw new FileNotFoundException();
    }//from  w w w. java2  s  .  c  o m

    String mimeType = Utils.getFileMimeType(documentId);
    if (!mimeType.startsWith("image/")) {
        throw new FileNotFoundException();
    }

    DataManager dm = createDataManager(documentId);

    String path = DocumentIdParser.getPathFromId(documentId);

    final DisplayImageOptions options = new DisplayImageOptions.Builder().extraForDownloader(dm.getAccount())
            .cacheInMemory(false) // SAF does its own caching
            .cacheOnDisk(true).considerExifParams(true).build();

    final ParcelFileDescriptor[] pair;
    try {
        pair = ParcelFileDescriptor.createReliablePipe();
    } catch (IOException e) {
        throw new FileNotFoundException();
    }

    final String url = dm.getThumbnailLink(repoId, path, sizeHint.x);
    if (url == null)
        throw new FileNotFoundException();

    // do thumbnail download in another thread to avoid possible network access in UI thread
    final Future future = ConcurrentAsyncTask.submit(new Runnable() {

        @Override
        public void run() {
            try {
                FileOutputStream fileStream = new FileOutputStream(pair[1].getFileDescriptor());

                // load the file. this might involve talking to the seafile server. this will hang until
                // it is done.
                Bitmap bmp = ImageLoader.getInstance().loadImageSync(url, options);

                if (bmp != null) {
                    bmp.compress(Bitmap.CompressFormat.PNG, 100, fileStream);
                }
            } finally {
                IOUtils.closeQuietly(pair[1]);
            }
        }
    });

    if (signal != null) {
        signal.setOnCancelListener(new CancellationSignal.OnCancelListener() {
            @Override
            public void onCancel() {
                Log.d(DEBUG_TAG, "openDocumentThumbnail() cancelling download");
                future.cancel(true);
                IOUtils.closeQuietly(pair[1]);
            }
        });
    }

    return new AssetFileDescriptor(pair[0], 0, AssetFileDescriptor.UNKNOWN_LENGTH);
}