android.provider.DocumentsContract.Document Java Examples

The following examples show how to use android.provider.DocumentsContract.Document. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LocalStorageProvider.java    From Applozic-Android-SDK with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void includeFile(final MatrixCursor result, final File file)
        throws FileNotFoundException {
    final MatrixCursor.RowBuilder row = result.newRow();
    // These columns are required
    row.add(Document.COLUMN_DOCUMENT_ID, file.getAbsolutePath());
    row.add(Document.COLUMN_DISPLAY_NAME, file.getName());
    String mimeType = getDocumentType(file.getAbsolutePath());
    row.add(Document.COLUMN_MIME_TYPE, mimeType);
    int flags = file.canWrite() ? Document.FLAG_SUPPORTS_DELETE | Document.FLAG_SUPPORTS_WRITE
            : 0;
    // We only show thumbnails for image files - expect a call to
    // openDocumentThumbnail for each file that has
    // this flag set
    if (mimeType.startsWith("image/"))
        flags |= Document.FLAG_SUPPORTS_THUMBNAIL;
    row.add(Document.COLUMN_FLAGS, flags);
    // COLUMN_SIZE is required, but can be null
    row.add(Document.COLUMN_SIZE, file.length());
    // These columns are optional
    row.add(Document.COLUMN_LAST_MODIFIED, file.lastModified());
    // Document.COLUMN_ICON can be a resource id identifying a custom icon.
    // The system provides default icons
    // based on mime type
    // Document.COLUMN_SUMMARY is optional additional information about the
    // file
}
 
Example #2
Source File: LocalStorageProvider.java    From Readily with MIT License 6 votes vote down vote up
@Override
public String getDocumentType(final String documentId) throws FileNotFoundException {
    File file = new File(documentId);
    if (file.isDirectory())
        return Document.MIME_TYPE_DIR;
    // From FileProvider.getType(Uri)
    final int lastDot = file.getName().lastIndexOf('.');
    if (lastDot >= 0) {
        final String extension = file.getName().substring(lastDot + 1);
        final String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
        if (mime != null) {
            return mime;
        }
    }
    return "application/octet-stream";
}
 
Example #3
Source File: LocalStorageProvider.java    From Readily with MIT License 6 votes vote down vote up
private void includeFile(final MatrixCursor result, final File file)
        throws FileNotFoundException {
    final MatrixCursor.RowBuilder row = result.newRow();
    // These columns are required
    row.add(Document.COLUMN_DOCUMENT_ID, file.getAbsolutePath());
    row.add(Document.COLUMN_DISPLAY_NAME, file.getName());
    String mimeType = getDocumentType(file.getAbsolutePath());
    row.add(Document.COLUMN_MIME_TYPE, mimeType);
    int flags = file.canWrite() ? Document.FLAG_SUPPORTS_DELETE | Document.FLAG_SUPPORTS_WRITE
            : 0;
    // We only show thumbnails for image files - expect a call to
    // openDocumentThumbnail for each file that has
    // this flag set
    if (mimeType.startsWith("image/"))
        flags |= Document.FLAG_SUPPORTS_THUMBNAIL;
    row.add(Document.COLUMN_FLAGS, flags);
    // COLUMN_SIZE is required, but can be null
    row.add(Document.COLUMN_SIZE, file.length());
    // These columns are optional
    row.add(Document.COLUMN_LAST_MODIFIED, file.lastModified());
    // Document.COLUMN_ICON can be a resource id identifying a custom icon.
    // The system provides default icons
    // based on mime type
    // Document.COLUMN_SUMMARY is optional additional information about the
    // file
}
 
Example #4
Source File: LocalStorageProvider.java    From secrecy with Apache License 2.0 6 votes vote down vote up
@Override
public String getDocumentType(final String documentId) throws FileNotFoundException {
    File file = new File(documentId);
    if (file.isDirectory())
        return Document.MIME_TYPE_DIR;
    // From FileProvider.getType(Uri)
    final int lastDot = file.getName().lastIndexOf('.');
    if (lastDot >= 0) {
        final String extension = file.getName().substring(lastDot + 1);
        final String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
        if (mime != null) {
            return mime;
        }
    }
    return "application/octet-stream";
}
 
Example #5
Source File: LocalStorageProvider.java    From secrecy with Apache License 2.0 6 votes vote down vote up
private void includeFile(final MatrixCursor result, final File file)
        throws FileNotFoundException {
    final MatrixCursor.RowBuilder row = result.newRow();
    // These columns are required
    row.add(Document.COLUMN_DOCUMENT_ID, file.getAbsolutePath());
    row.add(Document.COLUMN_DISPLAY_NAME, file.getName());
    String mimeType = getDocumentType(file.getAbsolutePath());
    row.add(Document.COLUMN_MIME_TYPE, mimeType);
    int flags = file.canWrite() ? Document.FLAG_SUPPORTS_DELETE | Document.FLAG_SUPPORTS_WRITE
            : 0;
    // We only show thumbnails for image files - expect a call to
    // openDocumentThumbnail for each file that has
    // this flag set
    if (mimeType.startsWith("image/"))
        flags |= Document.FLAG_SUPPORTS_THUMBNAIL;
    row.add(Document.COLUMN_FLAGS, flags);
    // COLUMN_SIZE is required, but can be null
    row.add(Document.COLUMN_SIZE, file.length());
    // These columns are optional
    row.add(Document.COLUMN_LAST_MODIFIED, file.lastModified());
    // Document.COLUMN_ICON can be a resource id identifying a custom icon.
    // The system provides default icons
    // based on mime type
    // Document.COLUMN_SUMMARY is optional additional information about the
    // file
}
 
Example #6
Source File: LocalStorageProvider.java    From filechooser with MIT License 6 votes vote down vote up
@Override
public String getDocumentType(final String documentId) throws FileNotFoundException {
    File file = new File(documentId);
    if (file.isDirectory())
        return Document.MIME_TYPE_DIR;
    // From FileProvider.getType(Uri)
    final int lastDot = file.getName().lastIndexOf('.');
    if (lastDot >= 0) {
        final String extension = file.getName().substring(lastDot + 1);
        final String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
        if (mime != null) {
            return mime;
        }
    }
    return "application/octet-stream";
}
 
Example #7
Source File: LocalStorageProvider.java    From filechooser with MIT License 6 votes vote down vote up
private void includeFile(final MatrixCursor result, final File file)
        throws FileNotFoundException {
    final MatrixCursor.RowBuilder row = result.newRow();
    // These columns are required
    row.add(Document.COLUMN_DOCUMENT_ID, file.getAbsolutePath());
    row.add(Document.COLUMN_DISPLAY_NAME, file.getName());
    String mimeType = getDocumentType(file.getAbsolutePath());
    row.add(Document.COLUMN_MIME_TYPE, mimeType);
    int flags = file.canWrite() ? Document.FLAG_SUPPORTS_DELETE | Document.FLAG_SUPPORTS_WRITE
            : 0;
    // We only show thumbnails for image files - expect a call to
    // openDocumentThumbnail for each file that has
    // this flag set
    if (mimeType.startsWith("image/"))
        flags |= Document.FLAG_SUPPORTS_THUMBNAIL;
    row.add(Document.COLUMN_FLAGS, flags);
    // COLUMN_SIZE is required, but can be null
    row.add(Document.COLUMN_SIZE, file.length());
    // These columns are optional
    row.add(Document.COLUMN_LAST_MODIFIED, file.lastModified());
    // Document.COLUMN_ICON can be a resource id identifying a custom icon.
    // The system provides default icons
    // based on mime type
    // Document.COLUMN_SUMMARY is optional additional information about the
    // file
}
 
Example #8
Source File: LocalStorageProvider.java    From Effects-Pro with MIT License 6 votes vote down vote up
@Override
public String getDocumentType(final String documentId) throws FileNotFoundException {
	File file = new File(documentId);
	if (file.isDirectory())
		return Document.MIME_TYPE_DIR;
	// From FileProvider.getType(Uri)
	final int lastDot = file.getName().lastIndexOf('.');
	if (lastDot >= 0) {
		final String extension = file.getName().substring(lastDot + 1);
		final String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
		if (mime != null) {
			return mime;
		}
	}
	return "application/octet-stream";
}
 
Example #9
Source File: LocalStorageProvider.java    From Effects-Pro with MIT License 6 votes vote down vote up
private void includeFile(final MatrixCursor result, final File file)
		throws FileNotFoundException {
	final MatrixCursor.RowBuilder row = result.newRow();
	// These columns are required
	row.add(Document.COLUMN_DOCUMENT_ID, file.getAbsolutePath());
	row.add(Document.COLUMN_DISPLAY_NAME, file.getName());
	String mimeType = getDocumentType(file.getAbsolutePath());
	row.add(Document.COLUMN_MIME_TYPE, mimeType);
	int flags = file.canWrite() ? Document.FLAG_SUPPORTS_DELETE | Document.FLAG_SUPPORTS_WRITE
			: 0;
	// We only show thumbnails for image files - expect a call to
	// openDocumentThumbnail for each file that has
	// this flag set
	if (mimeType.startsWith("image/"))
		flags |= Document.FLAG_SUPPORTS_THUMBNAIL;
	row.add(Document.COLUMN_FLAGS, flags);
	// COLUMN_SIZE is required, but can be null
	row.add(Document.COLUMN_SIZE, file.length());
	// These columns are optional
	row.add(Document.COLUMN_LAST_MODIFIED, file.lastModified());
	// Document.COLUMN_ICON can be a resource id identifying a custom icon.
	// The system provides default icons
	// based on mime type
	// Document.COLUMN_SUMMARY is optional additional information about the
	// file
}
 
Example #10
Source File: LocalStorageProvider.java    From droid-stealth with GNU General Public License v2.0 6 votes vote down vote up
@Override
public String getDocumentType(final String documentId) throws FileNotFoundException {
    File file = new File(documentId);
    if (file.isDirectory())
        return Document.MIME_TYPE_DIR;
    // From FileProvider.getType(Uri)
    final int lastDot = file.getName().lastIndexOf('.');
    if (lastDot >= 0) {
        final String extension = file.getName().substring(lastDot + 1);
        final String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
        if (mime != null) {
            return mime;
        }
    }
    return "application/octet-stream";
}
 
Example #11
Source File: LocalStorageProvider.java    From droid-stealth with GNU General Public License v2.0 6 votes vote down vote up
private void includeFile(final MatrixCursor result, final File file)
        throws FileNotFoundException {
    final MatrixCursor.RowBuilder row = result.newRow();
    // These columns are required
    row.add(Document.COLUMN_DOCUMENT_ID, file.getAbsolutePath());
    row.add(Document.COLUMN_DISPLAY_NAME, file.getName());
    String mimeType = getDocumentType(file.getAbsolutePath());
    row.add(Document.COLUMN_MIME_TYPE, mimeType);
    int flags = file.canWrite() ? Document.FLAG_SUPPORTS_DELETE | Document.FLAG_SUPPORTS_WRITE
            : 0;
    // We only show thumbnails for image files - expect a call to
    // openDocumentThumbnail for each file that has
    // this flag set
    if (mimeType.startsWith("image/"))
        flags |= Document.FLAG_SUPPORTS_THUMBNAIL;
    row.add(Document.COLUMN_FLAGS, flags);
    // COLUMN_SIZE is required, but can be null
    row.add(Document.COLUMN_SIZE, file.length());
    // These columns are optional
    row.add(Document.COLUMN_LAST_MODIFIED, file.lastModified());
    // Document.COLUMN_ICON can be a resource id identifying a custom icon.
    // The system provides default icons
    // based on mime type
    // Document.COLUMN_SUMMARY is optional additional information about the
    // file
}
 
Example #12
Source File: LocalStorageProvider.java    From qiniu-lab-android with MIT License 6 votes vote down vote up
@Override
public String getDocumentType(final String documentId) throws FileNotFoundException {
    File file = new File(documentId);
    if (file.isDirectory())
        return Document.MIME_TYPE_DIR;
    // From FileProvider.getType(Uri)
    final int lastDot = file.getName().lastIndexOf('.');
    if (lastDot >= 0) {
        final String extension = file.getName().substring(lastDot + 1);
        final String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
        if (mime != null) {
            return mime;
        }
    }
    return "application/octet-stream";
}
 
Example #13
Source File: LocalStorageProvider.java    From qiniu-lab-android with MIT License 6 votes vote down vote up
private void includeFile(final MatrixCursor result, final File file)
        throws FileNotFoundException {
    final MatrixCursor.RowBuilder row = result.newRow();
    // These columns are required
    row.add(Document.COLUMN_DOCUMENT_ID, file.getAbsolutePath());
    row.add(Document.COLUMN_DISPLAY_NAME, file.getName());
    String mimeType = getDocumentType(file.getAbsolutePath());
    row.add(Document.COLUMN_MIME_TYPE, mimeType);
    int flags = file.canWrite() ? Document.FLAG_SUPPORTS_DELETE | Document.FLAG_SUPPORTS_WRITE
            : 0;
    // We only show thumbnails for image files - expect a call to
    // openDocumentThumbnail for each file that has
    // this flag set
    if (mimeType.startsWith("image/"))
        flags |= Document.FLAG_SUPPORTS_THUMBNAIL;
    row.add(Document.COLUMN_FLAGS, flags);
    // COLUMN_SIZE is required, but can be null
    row.add(Document.COLUMN_SIZE, file.length());
    // These columns are optional
    row.add(Document.COLUMN_LAST_MODIFIED, file.lastModified());
    // Document.COLUMN_ICON can be a resource id identifying a custom icon.
    // The system provides default icons
    // based on mime type
    // Document.COLUMN_SUMMARY is optional additional information about the
    // file
}
 
Example #14
Source File: LocalStorageProvider.java    From Applozic-Android-SDK with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public String getDocumentType(final String documentId) throws FileNotFoundException {
    File file = new File(documentId);
    if (file.isDirectory())
        return Document.MIME_TYPE_DIR;
    // From FileProvider.getType(Uri)
    final int lastDot = file.getName().lastIndexOf('.');
    if (lastDot >= 0) {
        final String extension = file.getName().substring(lastDot + 1);
        final String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
        if (mime != null) {
            return mime;
        }
    }
    return "application/octet-stream";
}
 
Example #15
Source File: LocalStorageProvider.java    From droidddle with Apache License 2.0 6 votes vote down vote up
@Override
public String getDocumentType(final String documentId) throws FileNotFoundException {
    File file = new File(documentId);
    if (file.isDirectory())
        return Document.MIME_TYPE_DIR;
    // From FileProvider.getType(Uri)
    final int lastDot = file.getName().lastIndexOf('.');
    if (lastDot >= 0) {
        final String extension = file.getName().substring(lastDot + 1);
        final String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
        if (mime != null) {
            return mime;
        }
    }
    return "application/octet-stream";
}
 
Example #16
Source File: DocumentsProvider.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * Return a list of streamable MIME types matching the filter, which can be passed to
 * {@link #openTypedDocument(String, String, Bundle, CancellationSignal)}.
 *
 * <p>The default implementation returns a MIME type provided by
 * {@link #queryDocument(String, String[])} as long as it matches the filter and the document
 * does not have the {@link Document#FLAG_VIRTUAL_DOCUMENT} flag set.
 *
 * <p>Virtual documents must have at least one streamable format.
 *
 * @see #getStreamTypes(Uri, String)
 * @see #openTypedDocument(String, String, Bundle, CancellationSignal)
 */
public String[] getDocumentStreamTypes(String documentId, String mimeTypeFilter) {
    Cursor cursor = null;
    try {
        cursor = queryDocument(documentId, null);
        if (cursor.moveToFirst()) {
            final String mimeType =
                cursor.getString(cursor.getColumnIndexOrThrow(Document.COLUMN_MIME_TYPE));
            final long flags =
                cursor.getLong(cursor.getColumnIndexOrThrow(Document.COLUMN_FLAGS));
            if ((flags & Document.FLAG_VIRTUAL_DOCUMENT) == 0 && mimeType != null &&
                    mimeTypeMatches(mimeTypeFilter, mimeType)) {
                return new String[] { mimeType };
            }
        }
    } catch (FileNotFoundException e) {
        return null;
    } finally {
        IoUtils.closeQuietly(cursor);
    }

    // No streamable MIME types.
    return null;
}
 
Example #17
Source File: AppStorageProvider.java    From JPPF with Apache License 2.0 6 votes vote down vote up
/**
 * Add a row in the specified cursor for the specified file.
 * @param child the file for which to add a row.
 * @param cursor the cursor to add a row to.
 */
private void createRowForChild(File child, MatrixCursor cursor) {
  MatrixCursor.RowBuilder row = cursor.newRow();
  String type = getContext().getContentResolver().getType(Uri.fromFile(child));
  Log.v(LOG_TAG, "createRowForChild(child=" + child + ") type=" + type);
  if (type == null) type = child.isDirectory() ? Document.MIME_TYPE_DIR : "application/octet-stream";
  int flags = child.isDirectory()
    ? /*Document.FLAG_DIR_PREFERS_GRID |*/ Document.FLAG_DIR_PREFERS_LAST_MODIFIED | Document.FLAG_DIR_SUPPORTS_CREATE
    : /*Document.FLAG_SUPPORTS_WRITE*/ 0;
  row.add(Document.COLUMN_FLAGS, flags);
  row.add(Document.COLUMN_DISPLAY_NAME, child.getName());
  row.add(Document.COLUMN_DOCUMENT_ID, getDocumentId(child));
  row.add(Document.COLUMN_MIME_TYPE, type);
  row.add(Document.COLUMN_SIZE, child.isDirectory() ? child.getTotalSpace() - child.getFreeSpace() : child.length());
  row.add(Document.COLUMN_LAST_MODIFIED, null);
}
 
Example #18
Source File: AdapterDocuments.java    From microMathematics with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void createFolder(String new_name)
{
    try
    {
        Uri new_uri = DocumentsContract.createDocument(ctx.getContentResolver(), uri, Document.MIME_TYPE_DIR,
                new_name);
        if (new_uri != null)
        {
            notifyRefr(new_name);
            return;
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    notify(ctx.getString(R.string.fman_create_folder_error, new_name), CommanderIf.OPERATION_FAILED);
}
 
Example #19
Source File: SambaDocumentsProvider.java    From samba-documents-provider with GNU General Public License v3.0 6 votes vote down vote up
private void recursiveDeleteFolder(DocumentMetadata metadata) throws IOException {
  // Fetch the latest children just in case our cache is out of date.
  metadata.loadChildren(mClient);
  for (DocumentMetadata child : metadata.getChildren().values()) {
    if (Document.MIME_TYPE_DIR.equals(child.getMimeType())) {
      recursiveDeleteFolder(child);
    } else {
      deleteFile(child);
    }
  }

  final Uri uri = metadata.getUri();
  mClient.rmdir(uri.toString());
  mCache.remove(uri);

  revokeDocumentPermission(toDocumentId(uri));
}
 
Example #20
Source File: LocalStorageProvider.java    From droidddle with Apache License 2.0 6 votes vote down vote up
private void includeFile(final MatrixCursor result, final File file) throws FileNotFoundException {
    final MatrixCursor.RowBuilder row = result.newRow();
    // These columns are required
    row.add(Document.COLUMN_DOCUMENT_ID, file.getAbsolutePath());
    row.add(Document.COLUMN_DISPLAY_NAME, file.getName());
    String mimeType = getDocumentType(file.getAbsolutePath());
    row.add(Document.COLUMN_MIME_TYPE, mimeType);
    int flags = file.canWrite() ? Document.FLAG_SUPPORTS_DELETE | Document.FLAG_SUPPORTS_WRITE : 0;
    // We only show thumbnails for image files - expect a call to
    // openDocumentThumbnail for each file that has
    // this flag set
    if (mimeType.startsWith("image/"))
        flags |= Document.FLAG_SUPPORTS_THUMBNAIL;
    row.add(Document.COLUMN_FLAGS, flags);
    // COLUMN_SIZE is required, but can be null
    row.add(Document.COLUMN_SIZE, file.length());
    // These columns are optional
    row.add(Document.COLUMN_LAST_MODIFIED, file.lastModified());
    // Document.COLUMN_ICON can be a resource id identifying a custom icon.
    // The system provides default icons
    // based on mime type
    // Document.COLUMN_SUMMARY is optional additional information about the
    // file
}
 
Example #21
Source File: MyCloudProvider.java    From android-StorageProvider with Apache License 2.0 5 votes vote down vote up
/**
 * Get a file's MIME type
 *
 * @param file the File object whose type we want
 * @return the MIME type of the file
 */
private static String getTypeForFile(File file) {
    if (file.isDirectory()) {
        return Document.MIME_TYPE_DIR;
    } else {
        return getTypeForName(file.getName());
    }
}
 
Example #22
Source File: DocumentsProvider.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Return concrete MIME type of the requested document. Must match the value
 * of {@link Document#COLUMN_MIME_TYPE} for this document. The default
 * implementation queries {@link #queryDocument(String, String[])}, so
 * providers may choose to override this as an optimization.
 * <p>
 * @throws AuthenticationRequiredException If authentication is required from
 *            the user (such as login credentials), but it is not guaranteed
 *            that the client will handle this properly.
 */
public String getDocumentType(String documentId) throws FileNotFoundException {
    final Cursor cursor = queryDocument(documentId, null);
    try {
        if (cursor.moveToFirst()) {
            return cursor.getString(cursor.getColumnIndexOrThrow(Document.COLUMN_MIME_TYPE));
        } else {
            return null;
        }
    } finally {
        IoUtils.closeQuietly(cursor);
    }
}
 
Example #23
Source File: MyCloudProvider.java    From storage-samples with Apache License 2.0 5 votes vote down vote up
/**
 * Get a file's MIME type
 *
 * @param file the File object whose type we want
 * @return the MIME type of the file
 */
private static String getTypeForFile(File file) {
    if (file.isDirectory()) {
        return Document.MIME_TYPE_DIR;
    } else {
        return getTypeForName(file.getName());
    }
}
 
Example #24
Source File: SambaDocumentsProvider.java    From samba-documents-provider with GNU General Public License v3.0 5 votes vote down vote up
private Object[] getDocumentValues(
    String[] projection, DocumentMetadata metadata) {
  Object[] row = new Object[projection.length];
  for (int i = 0; i < projection.length; ++i) {
    switch (projection[i]) {
      case Document.COLUMN_DOCUMENT_ID:
        row[i] = toDocumentId(metadata.getUri());
        break;
      case Document.COLUMN_DISPLAY_NAME:
        row[i] = metadata.getDisplayName();
        break;
      case Document.COLUMN_FLAGS:
        // Always assume it can write to it until the file operation fails. Windows 10 also does
        // the same thing.
        int flag = metadata.canCreateDocument() ? Document.FLAG_DIR_SUPPORTS_CREATE : 0;
        flag |= Document.FLAG_SUPPORTS_WRITE;
        flag |= Document.FLAG_SUPPORTS_DELETE;
        flag |= Document.FLAG_SUPPORTS_RENAME;
        flag |= Document.FLAG_SUPPORTS_REMOVE;
        flag |= Document.FLAG_SUPPORTS_MOVE;
        row[i] = flag;
        break;
      case Document.COLUMN_MIME_TYPE:
        row[i] = metadata.getMimeType();
        break;
      case Document.COLUMN_SIZE:
        row[i] = metadata.getSize();
        break;
      case Document.COLUMN_LAST_MODIFIED:
        row[i] = metadata.getLastModified();
        break;
      case Document.COLUMN_ICON:
        row[i] = metadata.getIconResourceId();
        break;
    }
  }
  return row;
}
 
Example #25
Source File: DirectorySelectionFragment.java    From android-DirectorySelection with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a directory under the directory represented as the uri in the argument.
 *
 * @param uri The uri of the directory under which a new directory is created.
 * @param directoryName The directory name of a new directory.
 */
//VisibileForTesting
void createDirectory(Uri uri, String directoryName) {
    ContentResolver contentResolver = getActivity().getContentResolver();
    Uri docUri = DocumentsContract.buildDocumentUriUsingTree(uri,
            DocumentsContract.getTreeDocumentId(uri));
    Uri directoryUri = null;
    try {
        directoryUri = DocumentsContract
                .createDocument(contentResolver, docUri, Document.MIME_TYPE_DIR, directoryName);
    } catch (IOException e) {
        Log.w(TAG, "IOException", e);
    }
    if (directoryUri != null) {
        Log.i(TAG, String.format(
                "Created directory : %s, Document Uri : %s, Created directory Uri : %s",
                directoryName, docUri, directoryUri));
        Toast.makeText(getActivity(), String.format("Created a directory [%s]",
                directoryName), Toast.LENGTH_SHORT).show();
    } else {
        Log.w(TAG, String.format("Failed to create a directory : %s, Uri %s", directoryName,
                docUri));
        Toast.makeText(getActivity(), String.format("Failed to created a directory [%s] : ",
                directoryName), Toast.LENGTH_SHORT).show();
    }

}
 
Example #26
Source File: DocumentMetadata.java    From samba-documents-provider with GNU General Public License v3.0 5 votes vote down vote up
public String getMimeType() {
  switch (mEntry.getType()) {
    case DirectoryEntry.FILE_SHARE:
    case DirectoryEntry.WORKGROUP:
    case DirectoryEntry.SERVER:
    case DirectoryEntry.DIR:
      return Document.MIME_TYPE_DIR;

    case DirectoryEntry.LINK:
    case DirectoryEntry.COMMS_SHARE:
    case DirectoryEntry.IPC_SHARE:
    case DirectoryEntry.PRINTER_SHARE:
      throw new UnsupportedOperationException(
          "Unsupported type of Samba directory entry " + mEntry.getType());

    case DirectoryEntry.FILE:
      final String ext = getExtension(mEntry.getName());
      if (ext == null) {
        return GENERIC_MIME_TYPE;
      }

      final String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext);
      return (mimeType == null) ? GENERIC_MIME_TYPE : mimeType;
  }

  throw new IllegalStateException("Should never reach here.");
}
 
Example #27
Source File: ScienceJournalDocsProvider.java    From science-journal with Apache License 2.0 5 votes vote down vote up
private void addExperimentToRow(MatrixCursor.RowBuilder row, ExperimentOverviewPojo overview) {
  row.add(
      Document.COLUMN_DISPLAY_NAME,
      Experiment.getDisplayTitle(getContext(), overview.getTitle()));
  row.add(Document.COLUMN_MIME_TYPE, Document.MIME_TYPE_DIR);
  row.add(Document.COLUMN_LAST_MODIFIED, overview.getLastUsedTimeMs());
  row.add(Document.COLUMN_SIZE, null);
}
 
Example #28
Source File: DirectorySelectionFragment.java    From android-DirectorySelection with Apache License 2.0 4 votes vote down vote up
/**
 * Updates the current directory of the uri passed as an argument and its children directories.
 * And updates the {@link #mRecyclerView} depending on the contents of the children.
 *
 * @param uri The uri of the current directory.
 */
//VisibileForTesting
void updateDirectoryEntries(Uri uri) {
    ContentResolver contentResolver = getActivity().getContentResolver();
    Uri docUri = DocumentsContract.buildDocumentUriUsingTree(uri,
            DocumentsContract.getTreeDocumentId(uri));
    Uri childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(uri,
            DocumentsContract.getTreeDocumentId(uri));

    Cursor docCursor = contentResolver.query(docUri, new String[]{
            Document.COLUMN_DISPLAY_NAME, Document.COLUMN_MIME_TYPE}, null, null, null);
    try {
        while (docCursor.moveToNext()) {
            Log.d(TAG, "found doc =" + docCursor.getString(0) + ", mime=" + docCursor
                    .getString(1));
            mCurrentDirectoryUri = uri;
            mCurrentDirectoryTextView.setText(docCursor.getString(0));
            mCreateDirectoryButton.setEnabled(true);
        }
    } finally {
        closeQuietly(docCursor);
    }

    Cursor childCursor = contentResolver.query(childrenUri, new String[]{
            Document.COLUMN_DISPLAY_NAME, Document.COLUMN_MIME_TYPE}, null, null, null);
    try {
        List<DirectoryEntry> directoryEntries = new ArrayList<>();
        while (childCursor.moveToNext()) {
            Log.d(TAG, "found child=" + childCursor.getString(0) + ", mime=" + childCursor
                    .getString(1));
            DirectoryEntry entry = new DirectoryEntry();
            entry.fileName = childCursor.getString(0);
            entry.mimeType = childCursor.getString(1);
            directoryEntries.add(entry);
        }
        mAdapter.setDirectoryEntries(directoryEntries);
        mAdapter.notifyDataSetChanged();
    } finally {
        closeQuietly(childCursor);
    }
}
 
Example #29
Source File: MyCloudProvider.java    From android-StorageProvider with Apache License 2.0 4 votes vote down vote up
/**
 * 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()) {
        // Request the folder to lay out as a grid rather than a list. This also allows a larger
        // thumbnail to be displayed for each image.
        //            flags |= Document.FLAG_DIR_PREFERS_GRID;

        // 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);
}
 
Example #30
Source File: ScienceJournalDocsProvider.java    From science-journal with Apache License 2.0 4 votes vote down vote up
@Override
public Cursor queryDocument(String documentId, String[] projection) throws FileNotFoundException {
  AppAccount appAccount = getAppAccountFromDocumentId(documentId);

  // Create a cursor with the requested projection, or the default projection.
  final MatrixCursor result = new MatrixCursor(resolveDocumentProjection(projection));
  MatrixCursor.RowBuilder row = result.newRow();
  row.add(Document.COLUMN_DOCUMENT_ID, documentId);

  if (TextUtils.equals(documentId, ROOT_DIRECTORY_ID)) {
    row.add(
        Document.COLUMN_DISPLAY_NAME, getContext().getResources().getString(R.string.app_name));
    row.add(Document.COLUMN_MIME_TYPE, Document.MIME_TYPE_DIR);
    row.add(Document.COLUMN_LAST_MODIFIED, null); // Not sure
    row.add(Document.COLUMN_SIZE, null);

  } else if (WhistlePunkApplication.getAppServices(getContext())
      .getAccountsProvider().isAppAccount(documentId)) {
    // It is an app account
    addAccountToRow(row, appAccount.getAccountName());

  } else if (!documentId.contains("/")) {
    // It is an experiment directory
    // TODO: Use notifyChange to load data off-thread and update only when it is available.
    List<ExperimentOverviewPojo> overviews =
        AppSingleton.getInstance(getContext())
            .getDataController(appAccount)
            .blockingGetExperimentOverviews(true);
    for (ExperimentOverviewPojo overview : overviews) {
      if (TextUtils.equals(overview.getExperimentId(), documentId)) {
        addExperimentToRow(row, overview);
        break;
      }
    }

  } else {
    // It is a file
    File file =
        new File(
            FileMetadataUtil.getInstance().getExperimentsRootDirectory(appAccount)
                + "/"
                + documentId);
    addAssetToRow(row, file);
  }
  return result;
}