diff --git a/packages/DocumentsUI/tests/AndroidManifest.xml b/packages/DocumentsUI/tests/AndroidManifest.xml index 81a2889a8ee92..a3124275a6ade 100644 --- a/packages/DocumentsUI/tests/AndroidManifest.xml +++ b/packages/DocumentsUI/tests/AndroidManifest.xml @@ -4,6 +4,17 @@ + + + + + mStorage = new HashMap(); + private int mStorageUsedBytes; + + @Override + public boolean onCreate() { + final File cacheDir = getContext().getCacheDir(); + removeRecursively(cacheDir); + mRootDocumentId = getDocumentIdForFile(cacheDir); + mStorage.put(mRootDocumentId, cacheDir); + return true; + } + + @Override + public Cursor queryRoots(String[] projection) throws FileNotFoundException { + final MatrixCursor result = new MatrixCursor(projection != null ? projection : DEFAULT_ROOT_PROJECTION); + final RowBuilder row = result.newRow(); + row.add(Root.COLUMN_ROOT_ID, MY_ROOT_ID); + row.add(Root.COLUMN_FLAGS, Root.FLAG_SUPPORTS_CREATE); + row.add(Root.COLUMN_TITLE, "Foobar SD 4GB"); + row.add(Root.COLUMN_DOCUMENT_ID, mRootDocumentId); + row.add(Root.COLUMN_AVAILABLE_BYTES, STORAGE_SIZE - mStorageUsedBytes); + return result; + } + + @Override + public Cursor queryDocument(String documentId, String[] projection) throws FileNotFoundException { + final MatrixCursor result = new MatrixCursor(projection != null ? projection : DEFAULT_DOCUMENT_PROJECTION); + final File file = mStorage.get(documentId); + if (file == null) { + throw new FileNotFoundException(); + } + includeFile(result, file); + return result; + } + + @Override + public String createDocument(String parentDocumentId, String mimeType, String displayName) + throws FileNotFoundException { + final File parentFile = mStorage.get(parentDocumentId); + if (parentFile == null || !parentFile.isDirectory()) { + throw new FileNotFoundException(); + } + final File file = new File(parentFile, displayName); + if (mimeType.equals(Document.MIME_TYPE_DIR)) { + if (!file.mkdirs()) { + throw new FileNotFoundException(); + } + } else { + try { + if (!file.createNewFile()) { + throw new FileNotFoundException(); + } + } + catch (IOException e) { + throw new FileNotFoundException(); + } + } + + final String documentId = getDocumentIdForFile(file); + mStorage.put(documentId, file); + return documentId; + } + + @Override + public Cursor queryChildDocuments(String parentDocumentId, String[] projection, String sortOrder) + throws FileNotFoundException { + final MatrixCursor result = new MatrixCursor(projection != null ? projection : DEFAULT_DOCUMENT_PROJECTION); + final File parentFile = mStorage.get(parentDocumentId); + if (parentFile == null || parentFile.isFile()) { + throw new FileNotFoundException(); + } + for (File file : parentFile.listFiles()) { + includeFile(result, file); + } + return result; + } + + @Override + public Cursor queryRecentDocuments(String rootId, String[] projection) + throws FileNotFoundException { + throw new FileNotFoundException(); + } + + @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); + } + + @Override + public AssetFileDescriptor openDocumentThumbnail( + String docId, Point sizeHint, CancellationSignal signal) throws FileNotFoundException { + throw new FileNotFoundException(); + } + + private void includeFile(MatrixCursor result, File file) { + final RowBuilder row = result.newRow(); + row.add(Document.COLUMN_DOCUMENT_ID, getDocumentIdForFile(file)); + row.add(Document.COLUMN_DISPLAY_NAME, file.getName()); + row.add(Document.COLUMN_SIZE, file.length()); + // TODO: Provide real mime type for files. + row.add(Document.COLUMN_MIME_TYPE, file.isDirectory() ? Document.MIME_TYPE_DIR : "application/octet-stream"); + int flags = 0; + // TODO: Add support for renaming and deleting. + if (file.isDirectory()) { + flags |= Document.FLAG_DIR_SUPPORTS_CREATE; + } else { + flags |= Document.FLAG_SUPPORTS_WRITE; + } + row.add(Document.COLUMN_FLAGS, flags); + row.add(Document.COLUMN_LAST_MODIFIED, file.lastModified()); + } + + private String getDocumentIdForFile(File file) { + return file.getAbsolutePath(); + } + + private void removeRecursively(File file) { + for (File childFile : file.listFiles()) { + if (childFile.isDirectory()) { + removeRecursively(childFile); + } + childFile.delete(); + } + } +}