Add support for notifying about child changes in StubProvider.
Change-Id: I256856a2bbefa6d831e6b912583eca86eeb03ba7
diff --git a/packages/DocumentsUI/tests/src/com/android/documentsui/StubProvider.java b/packages/DocumentsUI/tests/src/com/android/documentsui/StubProvider.java
index 0ba8ed1..75effa7 100644
--- a/packages/DocumentsUI/tests/src/com/android/documentsui/StubProvider.java
+++ b/packages/DocumentsUI/tests/src/com/android/documentsui/StubProvider.java
@@ -70,7 +70,7 @@
public boolean onCreate() {
final File cacheDir = getContext().getCacheDir();
removeRecursively(cacheDir);
- final StubDocument document = new StubDocument(cacheDir, Document.MIME_TYPE_DIR);
+ final StubDocument document = new StubDocument(cacheDir, Document.MIME_TYPE_DIR, null);
mRootDocumentId = document.documentId;
mStorage.put(mRootDocumentId, document);
return true;
@@ -95,7 +95,7 @@
if (file == null) {
throw new FileNotFoundException();
}
- includeFile(result, file);
+ includeDocument(result, file);
return result;
}
@@ -129,8 +129,9 @@
}
}
- final StubDocument document = new StubDocument(file, mimeType);
+ final StubDocument document = new StubDocument(file, mimeType, parentDocument);
mStorage.put(document.documentId, document);
+ notifyParentChanged(document.parentId);
return document.documentId;
}
@@ -144,22 +145,25 @@
synchronized (mWriteLock) {
mStorageUsedBytes -= fileSize;
}
- notifyStorageChanged();
+ notifyParentChanged(document.parentId);
}
@Override
public Cursor queryChildDocuments(String parentDocumentId, String[] projection, String sortOrder)
throws FileNotFoundException {
- final MatrixCursor result = new MatrixCursor(projection != null ? projection : DEFAULT_DOCUMENT_PROJECTION);
final StubDocument parentDocument = mStorage.get(parentDocumentId);
if (parentDocument == null || parentDocument.file.isFile()) {
throw new FileNotFoundException();
}
+ final MatrixCursor result = new MatrixCursor(projection != null ? projection : DEFAULT_DOCUMENT_PROJECTION);
+ result.setNotificationUri(getContext().getContentResolver(),
+ DocumentsContract.buildChildDocumentsUri(mAuthority, parentDocumentId));
StubDocument document;
for (File file : parentDocument.file.listFiles()) {
document = mStorage.get(StubDocument.getDocumentIdForFile(file));
- if (document != null)
- includeFile(result, document);
+ if (document != null) {
+ includeDocument(result, document);
+ }
}
return result;
}
@@ -235,7 +239,7 @@
}
finally {
closePipeSilently(readPipe);
- notifyStorageChanged();
+ notifyParentChanged(document.parentId);
}
}
}.start();
@@ -259,11 +263,14 @@
}
}
- private void notifyStorageChanged() {
+ private void notifyParentChanged(String parentId) {
+ getContext().getContentResolver().notifyChange(
+ DocumentsContract.buildChildDocumentsUri(mAuthority, parentId), null, false);
+ // Notify also about possible change in remaining space on the root.
getContext().getContentResolver().notifyChange(DocumentsContract.buildRootsUri(mAuthority), null, false);
}
- private void includeFile(MatrixCursor result, StubDocument document) {
+ private void includeDocument(MatrixCursor result, StubDocument document) {
final RowBuilder row = result.newRow();
row.add(Document.COLUMN_DOCUMENT_ID, document.documentId);
row.add(Document.COLUMN_DISPLAY_NAME, document.file.getName());
@@ -294,11 +301,13 @@
public final File file;
public final String mimeType;
public final String documentId;
+ public final String parentId;
- StubDocument(File file, String mimeType) {
+ StubDocument(File file, String mimeType, StubDocument parent) {
this.file = file;
- this.mimeType = this.mimeType;
+ this.mimeType = mimeType;
this.documentId = getDocumentIdForFile(file);
+ this.parentId = parent != null ? parent.documentId : null;
}
public static String getDocumentIdForFile(File file) {