summaryrefslogtreecommitdiffstats
path: root/packages/DocumentsUI
diff options
context:
space:
mode:
authorTomasz Mikolajewski <mtomasz@google.com>2015-04-10 17:28:53 +0900
committerTomasz Mikolajewski <mtomasz@google.com>2015-04-13 09:37:54 +0900
commit30b669465d344c02c7c3f8df2093793fc33456eb (patch)
tree8da9c32af7289453c4d61f9fadb9a338a50f3cec /packages/DocumentsUI
parent9e3cb0461ce56146e2ed67082431c052dc4fee1d (diff)
downloadframeworks_base-30b669465d344c02c7c3f8df2093793fc33456eb.zip
frameworks_base-30b669465d344c02c7c3f8df2093793fc33456eb.tar.gz
frameworks_base-30b669465d344c02c7c3f8df2093793fc33456eb.tar.bz2
Simulate running out of space in StubProvider.
Change-Id: I0a069c07bcf8784aeb5c5993d90d0a6db6e2afb1
Diffstat (limited to 'packages/DocumentsUI')
-rw-r--r--packages/DocumentsUI/tests/src/com/android/documentsui/StubProvider.java81
1 files changed, 76 insertions, 5 deletions
diff --git a/packages/DocumentsUI/tests/src/com/android/documentsui/StubProvider.java b/packages/DocumentsUI/tests/src/com/android/documentsui/StubProvider.java
index 6044781..79e7d7d 100644
--- a/packages/DocumentsUI/tests/src/com/android/documentsui/StubProvider.java
+++ b/packages/DocumentsUI/tests/src/com/android/documentsui/StubProvider.java
@@ -30,6 +30,8 @@ import android.provider.DocumentsContract.Root;
import android.provider.DocumentsContract.Root;
import android.provider.DocumentsProvider;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
@@ -52,6 +54,7 @@ public class StubProvider extends DocumentsProvider {
private String mRootDocumentId;
private HashMap<String, File> mStorage = new HashMap<String, File>();
private int mStorageUsedBytes;
+ private Object mWriteLock = new Object();
@Override
public boolean onCreate() {
@@ -147,11 +150,11 @@ public class StubProvider extends DocumentsProvider {
@Override
public ParcelFileDescriptor openDocument(String docId, String mode, CancellationSignal signal)
throws FileNotFoundException {
- final File file = mStorage.get(docId);
- if (file == null || !file.isFile())
- throw new FileNotFoundException();
- // TODO: Simulate running out of storage.
- return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE);
+ // TODO: Add support for reading files.
+ if ("w".equals(mode)) {
+ return startWrite(docId);
+ }
+ throw new FileNotFoundException();
}
@Override
@@ -160,6 +163,74 @@ public class StubProvider extends DocumentsProvider {
throw new FileNotFoundException();
}
+ private ParcelFileDescriptor startWrite(String docId)
+ throws FileNotFoundException {
+ final File file = mStorage.get(docId);
+ if (file == null || !file.isFile())
+ throw new FileNotFoundException();
+ ParcelFileDescriptor[] pipe;
+ try {
+ pipe = ParcelFileDescriptor.createReliablePipe();
+ }
+ catch (IOException exception) {
+ throw new FileNotFoundException();
+ }
+ final ParcelFileDescriptor readPipe = pipe[0];
+ final ParcelFileDescriptor writePipe = pipe[1];
+
+ new Thread() {
+ @Override
+ public void run() {
+ try {
+ final FileInputStream inputStream = new FileInputStream(readPipe.getFileDescriptor());
+ final FileOutputStream outputStream = new FileOutputStream(file);
+ byte[] buffer = new byte[32 * 1024];
+ int bytesToRead;
+ int bytesRead = 0;
+ while (bytesRead != -1) {
+ synchronized (mWriteLock) {
+ bytesToRead = Math.min(STORAGE_SIZE - mStorageUsedBytes, buffer.length);
+ if (bytesToRead == 0) {
+ closePipeWithErrorSilently(readPipe, "Not enough space.");
+ break;
+ }
+ bytesRead = inputStream.read(buffer, 0, bytesToRead);
+ if (bytesRead == -1) {
+ break;
+ }
+ outputStream.write(buffer, 0, bytesRead);
+ mStorageUsedBytes += bytesRead;
+ }
+ }
+ }
+ catch (IOException e) {
+ closePipeWithErrorSilently(readPipe, e.getMessage());
+ }
+ finally {
+ closePipeSilently(readPipe);
+ }
+ }
+ }.start();
+
+ return writePipe;
+ }
+
+ private void closePipeWithErrorSilently(ParcelFileDescriptor pipe, String error) {
+ try {
+ pipe.closeWithError(error);
+ }
+ catch (IOException ignore) {
+ }
+ }
+
+ private void closePipeSilently(ParcelFileDescriptor pipe) {
+ try {
+ pipe.close();
+ }
+ catch (IOException ignore) {
+ }
+ }
+
private void includeFile(MatrixCursor result, File file) {
final RowBuilder row = result.newRow();
row.add(Document.COLUMN_DOCUMENT_ID, getDocumentIdForFile(file));