use of android.database.MatrixCursor in project Android-Developers-Samples by johnjohndoe.
the class MyCloudProvider method querySearchDocuments.
// END_INCLUDE(query_recent_documents)
// BEGIN_INCLUDE(query_search_documents)
@Override
public Cursor querySearchDocuments(String rootId, String query, String[] projection) throws FileNotFoundException {
Log.v(TAG, "querySearchDocuments");
// Create a cursor with the requested projection, or the default projection.
final MatrixCursor result = new MatrixCursor(resolveDocumentProjection(projection));
final File parent = getFileForDocId(rootId);
// This example implementation searches file names for the query and doesn't rank search
// results, so we can stop as soon as we find a sufficient number of matches. Other
// implementations might use other data about files, rather than the file name, to
// produce a match; it might also require a network call to query a remote server.
// Iterate through all files in the file structure under the root until we reach the
// desired number of matches.
final LinkedList<File> pending = new LinkedList<File>();
// Start by adding the parent to the list of files to be processed
pending.add(parent);
// Do while we still have unexamined files, and fewer than the max search results
while (!pending.isEmpty() && result.getCount() < MAX_SEARCH_RESULTS) {
// Take a file from the list of unprocessed files
final File file = pending.removeFirst();
if (file.isDirectory()) {
// If it's a directory, add all its children to the unprocessed list
Collections.addAll(pending, file.listFiles());
} else {
// If it's a file and it matches, add it to the result cursor.
if (file.getName().toLowerCase().contains(query)) {
includeFile(result, null, file);
}
}
}
return result;
}
use of android.database.MatrixCursor in project Android-Developers-Samples by johnjohndoe.
the class MyCloudProvider method queryChildDocuments.
// END_INCLUDE(query_document)
// BEGIN_INCLUDE(query_child_documents)
@Override
public Cursor queryChildDocuments(String parentDocumentId, String[] projection, String sortOrder) throws FileNotFoundException {
Log.v(TAG, "queryChildDocuments, parentDocumentId: " + parentDocumentId + " sortOrder: " + sortOrder);
final MatrixCursor result = new MatrixCursor(resolveDocumentProjection(projection));
final File parent = getFileForDocId(parentDocumentId);
for (File file : parent.listFiles()) {
includeFile(result, null, file);
}
return result;
}
use of android.database.MatrixCursor in project Android-Developers-Samples by johnjohndoe.
the class MyCloudProvider method includeFile.
/**
* Add a representation of a file to a cursor.
*
* @param result the cursor to modify
* @param docId the document ID representing the desired file (may be null if given file)
* @param file the File object representing the desired file (may be null if given docID)
* @throws java.io.FileNotFoundException
*/
private void includeFile(MatrixCursor result, String docId, File file) throws FileNotFoundException {
if (docId == null) {
docId = getDocIdForFile(file);
} else {
file = getFileForDocId(docId);
}
int flags = 0;
if (file.isDirectory()) {
// Add FLAG_DIR_SUPPORTS_CREATE if the file is a writable directory.
if (file.isDirectory() && file.canWrite()) {
flags |= Document.FLAG_DIR_SUPPORTS_CREATE;
}
} else if (file.canWrite()) {
// If the file is writable set FLAG_SUPPORTS_WRITE and
// FLAG_SUPPORTS_DELETE
flags |= Document.FLAG_SUPPORTS_WRITE;
flags |= Document.FLAG_SUPPORTS_DELETE;
}
final String displayName = file.getName();
final String mimeType = getTypeForFile(file);
if (mimeType.startsWith("image/")) {
// Allow the image to be represented by a thumbnail rather than an icon
flags |= Document.FLAG_SUPPORTS_THUMBNAIL;
}
final MatrixCursor.RowBuilder row = result.newRow();
row.add(Document.COLUMN_DOCUMENT_ID, docId);
row.add(Document.COLUMN_DISPLAY_NAME, displayName);
row.add(Document.COLUMN_SIZE, file.length());
row.add(Document.COLUMN_MIME_TYPE, mimeType);
row.add(Document.COLUMN_LAST_MODIFIED, file.lastModified());
row.add(Document.COLUMN_FLAGS, flags);
// Add a custom icon
row.add(Document.COLUMN_ICON, R.drawable.ic_launcher);
}
use of android.database.MatrixCursor in project Android-Developers-Samples by johnjohndoe.
the class MyCloudProvider method queryRecentDocuments.
// END_INCLUDE(query_roots)
// BEGIN_INCLUDE(query_recent_documents)
@Override
public Cursor queryRecentDocuments(String rootId, String[] projection) throws FileNotFoundException {
Log.v(TAG, "queryRecentDocuments");
// This example implementation walks a local file structure to find the most recently
// modified files. Other implementations might include making a network call to query a
// server.
// Create a cursor with the requested projection, or the default projection.
final MatrixCursor result = new MatrixCursor(resolveDocumentProjection(projection));
final File parent = getFileForDocId(rootId);
// Create a queue to store the most recent documents, which orders by last modified.
PriorityQueue<File> lastModifiedFiles = new PriorityQueue<File>(5, new Comparator<File>() {
public int compare(File i, File j) {
return Long.compare(i.lastModified(), j.lastModified());
}
});
// Iterate through all files and directories in the file structure under the root. If
// the file is more recent than the least recently modified, add it to the queue,
// limiting the number of results.
final LinkedList<File> pending = new LinkedList<File>();
// Start by adding the parent to the list of files to be processed
pending.add(parent);
// Do while we still have unexamined files
while (!pending.isEmpty()) {
// Take a file from the list of unprocessed files
final File file = pending.removeFirst();
if (file.isDirectory()) {
// If it's a directory, add all its children to the unprocessed list
Collections.addAll(pending, file.listFiles());
} else {
// If it's a file, add it to the ordered queue.
lastModifiedFiles.add(file);
}
}
// Add the most recent files to the cursor, not exceeding the max number of results.
for (int i = 0; i < Math.min(MAX_LAST_MODIFIED + 1, lastModifiedFiles.size()); i++) {
final File file = lastModifiedFiles.remove();
includeFile(result, null, file);
}
return result;
}
use of android.database.MatrixCursor in project Android-Developers-Samples by johnjohndoe.
the class MyCloudProvider method queryDocument.
// END_INCLUDE(open_document_thumbnail)
// BEGIN_INCLUDE(query_document)
@Override
public Cursor queryDocument(String documentId, String[] projection) throws FileNotFoundException {
Log.v(TAG, "queryDocument");
// Create a cursor with the requested projection, or the default projection.
final MatrixCursor result = new MatrixCursor(resolveDocumentProjection(projection));
includeFile(result, documentId, null);
return result;
}
Aggregations