use of android.os.CancellationSignal in project platform_frameworks_base by android.
the class MtpManagerTest method testEventObjectAdded.
public void testEventObjectAdded() throws Exception {
while (true) {
getInstrumentation().show("Please take a photo by using connected MTP device.");
final CancellationSignal signal = new CancellationSignal();
MtpEvent event = mManager.readEvent(mUsbDevice.getDeviceId(), signal);
if (event.getEventCode() != MtpConstants.EVENT_OBJECT_ADDED) {
continue;
}
assertTrue(event.getObjectHandle() != 0);
break;
}
}
use of android.os.CancellationSignal in project platform_frameworks_base by android.
the class CursorLoader method loadInBackground.
/* Runs on a worker thread */
@Override
public Cursor loadInBackground() {
synchronized (this) {
if (isLoadInBackgroundCanceled()) {
throw new OperationCanceledException();
}
mCancellationSignal = new CancellationSignal();
}
try {
Cursor cursor = getContext().getContentResolver().query(mUri, mProjection, mSelection, mSelectionArgs, mSortOrder, mCancellationSignal);
if (cursor != null) {
try {
// Ensure the cursor window is filled.
cursor.getCount();
cursor.registerContentObserver(mObserver);
} catch (RuntimeException ex) {
cursor.close();
throw ex;
}
}
return cursor;
} finally {
synchronized (this) {
mCancellationSignal = null;
}
}
}
use of android.os.CancellationSignal in project robolectric by robolectric.
the class ShadowContentProviderClientTest method shouldDelegateToContentProvider.
@Test
@Config(minSdk = JELLY_BEAN_MR2)
public void shouldDelegateToContentProvider() throws Exception {
ContentProviderClient client = contentResolver.acquireContentProviderClient(AUTHORITY);
client.query(URI, PROJECTION, SELECTION, SELECTION_ARGS, SORT_ORDER);
verify(provider).query(URI, PROJECTION, SELECTION, SELECTION_ARGS, SORT_ORDER);
CancellationSignal signal = new CancellationSignal();
client.query(URI, PROJECTION, SELECTION, SELECTION_ARGS, SORT_ORDER, signal);
verify(provider).query(URI, PROJECTION, SELECTION, SELECTION_ARGS, SORT_ORDER, signal);
client.insert(URI, VALUES);
verify(provider).insert(URI, VALUES);
client.update(URI, VALUES, SELECTION, SELECTION_ARGS);
verify(provider).update(URI, VALUES, SELECTION, SELECTION_ARGS);
client.delete(URI, SELECTION, SELECTION_ARGS);
verify(provider).delete(URI, SELECTION, SELECTION_ARGS);
client.getType(URI);
verify(provider).getType(URI);
client.openFile(URI, "rw");
verify(provider).openFile(URI, "rw");
client.openAssetFile(URI, "r");
verify(provider).openAssetFile(URI, "r");
final Bundle opts = new Bundle();
client.openTypedAssetFileDescriptor(URI, MIME_TYPE, opts);
verify(provider).openTypedAssetFile(URI, MIME_TYPE, opts);
client.getStreamTypes(URI, MIME_TYPE);
verify(provider).getStreamTypes(URI, MIME_TYPE);
final ArrayList<ContentProviderOperation> ops = new ArrayList<>();
client.applyBatch(ops);
verify(provider).applyBatch(ops);
final ContentValues[] values = { VALUES };
client.bulkInsert(URI, values);
verify(provider).bulkInsert(URI, values);
final String method = "method";
final String arg = "arg";
final Bundle extras = new Bundle();
client.call(method, arg, extras);
verify(provider).call(method, arg, extras);
}
use of android.os.CancellationSignal in project platform_frameworks_base by android.
the class PrinterDiscoverySession method requestCustomPrinterIcon.
/**
* Request the custom icon for a printer.
*
* @param printerId The printer to icon belongs to.
* @see android.print.PrinterInfo.Builder#setHasCustomPrinterIcon()
*/
void requestCustomPrinterIcon(@NonNull PrinterId printerId) {
if (!mIsDestroyed && mObserver != null) {
CustomPrinterIconCallback callback = new CustomPrinterIconCallback(printerId, mObserver);
onRequestCustomPrinterIcon(printerId, new CancellationSignal(), callback);
}
}
use of android.os.CancellationSignal in project android_frameworks_base by crdroidandroid.
the class CopyJob method copyFileHelper.
/**
* Handles copying a single file.
*
* @param src Info of the file to copy from.
* @param dest Info of the *file* to copy to. Must be created beforehand.
* @param destParent Info of the parent of the destination.
* @param mimeType Mime type for the target. Can be different than source for virtual files.
* @throws ResourceException
*/
private void copyFileHelper(DocumentInfo src, DocumentInfo dest, DocumentInfo destParent, String mimeType) throws ResourceException {
CancellationSignal canceller = new CancellationSignal();
AssetFileDescriptor srcFileAsAsset = null;
ParcelFileDescriptor srcFile = null;
ParcelFileDescriptor dstFile = null;
InputStream in = null;
ParcelFileDescriptor.AutoCloseOutputStream out = null;
boolean success = false;
try {
// as such format.
if (src.isVirtualDocument()) {
try {
srcFileAsAsset = getClient(src).openTypedAssetFileDescriptor(src.derivedUri, mimeType, null, canceller);
} catch (FileNotFoundException | RemoteException | RuntimeException e) {
throw new ResourceException("Failed to open a file as asset for %s due to an " + "exception.", src.derivedUri, e);
}
srcFile = srcFileAsAsset.getParcelFileDescriptor();
try {
in = new AssetFileDescriptor.AutoCloseInputStream(srcFileAsAsset);
} catch (IOException e) {
throw new ResourceException("Failed to open a file input stream for %s due " + "an exception.", src.derivedUri, e);
}
} else {
try {
srcFile = getClient(src).openFile(src.derivedUri, "r", canceller);
} catch (FileNotFoundException | RemoteException | RuntimeException e) {
throw new ResourceException("Failed to open a file for %s due to an exception.", src.derivedUri, e);
}
in = new ParcelFileDescriptor.AutoCloseInputStream(srcFile);
}
try {
dstFile = getClient(dest).openFile(dest.derivedUri, "w", canceller);
} catch (FileNotFoundException | RemoteException | RuntimeException e) {
throw new ResourceException("Failed to open the destination file %s for writing " + "due to an exception.", dest.derivedUri, e);
}
out = new ParcelFileDescriptor.AutoCloseOutputStream(dstFile);
byte[] buffer = new byte[32 * 1024];
int len;
try {
while ((len = in.read(buffer)) != -1) {
if (isCanceled()) {
if (DEBUG)
Log.d(TAG, "Canceled copy mid-copy of: " + src.derivedUri);
return;
}
out.write(buffer, 0, len);
makeCopyProgress(len);
}
// Need to invoke IoUtils.close explicitly to avoid from ignoring errors at flush.
IoUtils.close(dstFile.getFileDescriptor());
srcFile.checkError();
} catch (IOException e) {
throw new ResourceException("Failed to copy bytes from %s to %s due to an IO exception.", src.derivedUri, dest.derivedUri, e);
}
if (src.isVirtualDocument()) {
convertedFiles.add(src);
}
success = true;
} finally {
if (!success) {
if (dstFile != null) {
try {
dstFile.closeWithError("Error copying bytes.");
} catch (IOException closeError) {
Log.w(TAG, "Error closing destination.", closeError);
}
}
if (DEBUG)
Log.d(TAG, "Cleaning up failed operation leftovers.");
canceller.cancel();
try {
deleteDocument(dest, destParent);
} catch (ResourceException e) {
Log.w(TAG, "Failed to cleanup after copy error: " + src.derivedUri, e);
}
}
// This also ensures the file descriptors are closed.
IoUtils.closeQuietly(in);
IoUtils.closeQuietly(out);
}
}
Aggregations