Search in sources :

Example 6 with ICancellationSignal

use of android.os.ICancellationSignal in project android_frameworks_base by DirtyUnicorns.

the class ContentProviderProxy method createCancellationSignal.

public ICancellationSignal createCancellationSignal() throws RemoteException {
    Parcel data = Parcel.obtain();
    Parcel reply = Parcel.obtain();
    try {
        data.writeInterfaceToken(IContentProvider.descriptor);
        mRemote.transact(IContentProvider.CREATE_CANCELATION_SIGNAL_TRANSACTION, data, reply, 0);
        DatabaseUtils.readExceptionFromParcel(reply);
        ICancellationSignal cancellationSignal = ICancellationSignal.Stub.asInterface(reply.readStrongBinder());
        return cancellationSignal;
    } finally {
        data.recycle();
        reply.recycle();
    }
}
Also used : Parcel(android.os.Parcel) ICancellationSignal(android.os.ICancellationSignal)

Example 7 with ICancellationSignal

use of android.os.ICancellationSignal in project android_frameworks_base by AOSPA.

the class ContentResolver method openAssetFileDescriptor.

/**
     * Open a raw file descriptor to access data under a URI.  This
     * interacts with the underlying {@link ContentProvider#openAssetFile}
     * method of the provider associated with the given URI, to retrieve any file stored there.
     *
     * <h5>Accepts the following URI schemes:</h5>
     * <ul>
     * <li>content ({@link #SCHEME_CONTENT})</li>
     * <li>android.resource ({@link #SCHEME_ANDROID_RESOURCE})</li>
     * <li>file ({@link #SCHEME_FILE})</li>
     * </ul>
     * <h5>The android.resource ({@link #SCHEME_ANDROID_RESOURCE}) Scheme</h5>
     * <p>
     * A Uri object can be used to reference a resource in an APK file.  The
     * Uri should be one of the following formats:
     * <ul>
     * <li><code>android.resource://package_name/id_number</code><br/>
     * <code>package_name</code> is your package name as listed in your AndroidManifest.xml.
     * For example <code>com.example.myapp</code><br/>
     * <code>id_number</code> is the int form of the ID.<br/>
     * The easiest way to construct this form is
     * <pre>Uri uri = Uri.parse("android.resource://com.example.myapp/" + R.raw.my_resource");</pre>
     * </li>
     * <li><code>android.resource://package_name/type/name</code><br/>
     * <code>package_name</code> is your package name as listed in your AndroidManifest.xml.
     * For example <code>com.example.myapp</code><br/>
     * <code>type</code> is the string form of the resource type.  For example, <code>raw</code>
     * or <code>drawable</code>.
     * <code>name</code> is the string form of the resource name.  That is, whatever the file
     * name was in your res directory, without the type extension.
     * The easiest way to construct this form is
     * <pre>Uri uri = Uri.parse("android.resource://com.example.myapp/raw/my_resource");</pre>
     * </li>
     * </ul>
     *
     * <p>Note that if this function is called for read-only input (mode is "r")
     * on a content: URI, it will instead call {@link #openTypedAssetFileDescriptor}
     * for you with a MIME type of "*&#47;*".  This allows such callers to benefit
     * from any built-in data conversion that a provider implements.
     *
     * @param uri The desired URI to open.
     * @param mode The file mode to use, as per {@link ContentProvider#openAssetFile
     * ContentProvider.openAssetFile}.
     * @param cancellationSignal A signal to cancel the operation in progress, or null if
     *            none. If the operation is canceled, then
     *            {@link OperationCanceledException} will be thrown.
     * @return Returns a new ParcelFileDescriptor pointing to the file.  You
     * own this descriptor and are responsible for closing it when done.
     * @throws FileNotFoundException Throws FileNotFoundException of no
     * file exists under the URI or the mode is invalid.
     */
@Nullable
public final AssetFileDescriptor openAssetFileDescriptor(@NonNull Uri uri, @NonNull String mode, @Nullable CancellationSignal cancellationSignal) throws FileNotFoundException {
    Preconditions.checkNotNull(uri, "uri");
    Preconditions.checkNotNull(mode, "mode");
    String scheme = uri.getScheme();
    if (SCHEME_ANDROID_RESOURCE.equals(scheme)) {
        if (!"r".equals(mode)) {
            throw new FileNotFoundException("Can't write resources: " + uri);
        }
        OpenResourceIdResult r = getResourceId(uri);
        try {
            return r.r.openRawResourceFd(r.id);
        } catch (Resources.NotFoundException ex) {
            throw new FileNotFoundException("Resource does not exist: " + uri);
        }
    } else if (SCHEME_FILE.equals(scheme)) {
        ParcelFileDescriptor pfd = ParcelFileDescriptor.open(new File(uri.getPath()), ParcelFileDescriptor.parseMode(mode));
        return new AssetFileDescriptor(pfd, 0, -1);
    } else {
        if ("r".equals(mode)) {
            return openTypedAssetFileDescriptor(uri, "*/*", null, cancellationSignal);
        } else {
            IContentProvider unstableProvider = acquireUnstableProvider(uri);
            if (unstableProvider == null) {
                throw new FileNotFoundException("No content provider: " + uri);
            }
            IContentProvider stableProvider = null;
            AssetFileDescriptor fd = null;
            try {
                ICancellationSignal remoteCancellationSignal = null;
                if (cancellationSignal != null) {
                    cancellationSignal.throwIfCanceled();
                    remoteCancellationSignal = unstableProvider.createCancellationSignal();
                    cancellationSignal.setRemote(remoteCancellationSignal);
                }
                try {
                    fd = unstableProvider.openAssetFile(mPackageName, uri, mode, remoteCancellationSignal);
                    if (fd == null) {
                        // The provider will be released by the finally{} clause
                        return null;
                    }
                } catch (DeadObjectException e) {
                    // The remote process has died...  but we only hold an unstable
                    // reference though, so we might recover!!!  Let's try!!!!
                    // This is exciting!!1!!1!!!!1
                    unstableProviderDied(unstableProvider);
                    stableProvider = acquireProvider(uri);
                    if (stableProvider == null) {
                        throw new FileNotFoundException("No content provider: " + uri);
                    }
                    fd = stableProvider.openAssetFile(mPackageName, uri, mode, remoteCancellationSignal);
                    if (fd == null) {
                        // The provider will be released by the finally{} clause
                        return null;
                    }
                }
                if (stableProvider == null) {
                    stableProvider = acquireProvider(uri);
                }
                releaseUnstableProvider(unstableProvider);
                unstableProvider = null;
                ParcelFileDescriptor pfd = new ParcelFileDescriptorInner(fd.getParcelFileDescriptor(), stableProvider);
                // Success!  Don't release the provider when exiting, let
                // ParcelFileDescriptorInner do that when it is closed.
                stableProvider = null;
                return new AssetFileDescriptor(pfd, fd.getStartOffset(), fd.getDeclaredLength());
            } catch (RemoteException e) {
                // Whatever, whatever, we'll go away.
                throw new FileNotFoundException("Failed opening content provider: " + uri);
            } catch (FileNotFoundException e) {
                throw e;
            } finally {
                if (cancellationSignal != null) {
                    cancellationSignal.setRemote(null);
                }
                if (stableProvider != null) {
                    releaseProvider(stableProvider);
                }
                if (unstableProvider != null) {
                    releaseUnstableProvider(unstableProvider);
                }
            }
        }
    }
}
Also used : AssetFileDescriptor(android.content.res.AssetFileDescriptor) FileNotFoundException(java.io.FileNotFoundException) ParcelFileDescriptor(android.os.ParcelFileDescriptor) ICancellationSignal(android.os.ICancellationSignal) DeadObjectException(android.os.DeadObjectException) Resources(android.content.res.Resources) RemoteException(android.os.RemoteException) File(java.io.File) Nullable(android.annotation.Nullable)

Example 8 with ICancellationSignal

use of android.os.ICancellationSignal in project platform_frameworks_base by android.

the class ContentProviderProxy method createCancellationSignal.

public ICancellationSignal createCancellationSignal() throws RemoteException {
    Parcel data = Parcel.obtain();
    Parcel reply = Parcel.obtain();
    try {
        data.writeInterfaceToken(IContentProvider.descriptor);
        mRemote.transact(IContentProvider.CREATE_CANCELATION_SIGNAL_TRANSACTION, data, reply, 0);
        DatabaseUtils.readExceptionFromParcel(reply);
        ICancellationSignal cancellationSignal = ICancellationSignal.Stub.asInterface(reply.readStrongBinder());
        return cancellationSignal;
    } finally {
        data.recycle();
        reply.recycle();
    }
}
Also used : Parcel(android.os.Parcel) ICancellationSignal(android.os.ICancellationSignal)

Example 9 with ICancellationSignal

use of android.os.ICancellationSignal in project platform_frameworks_base by android.

the class CancellationSignal method cancel.

/**
     * 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.
     */
public void cancel() {
    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();
        }
    }
}
Also used : ICancellationSignal(android.os.ICancellationSignal)

Example 10 with ICancellationSignal

use of android.os.ICancellationSignal in project android_frameworks_base by crdroidandroid.

the class CancellationSignal method cancel.

/**
     * 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.
     */
public void cancel() {
    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();
        }
    }
}
Also used : ICancellationSignal(android.os.ICancellationSignal)

Aggregations

ICancellationSignal (android.os.ICancellationSignal)49 DeadObjectException (android.os.DeadObjectException)37 Nullable (android.annotation.Nullable)35 RemoteException (android.os.RemoteException)16 Cursor (android.database.Cursor)11 AssetFileDescriptor (android.content.res.AssetFileDescriptor)10 ParcelFileDescriptor (android.os.ParcelFileDescriptor)10 FileNotFoundException (java.io.FileNotFoundException)10 Parcel (android.os.Parcel)6 Resources (android.content.res.Resources)5 File (java.io.File)5