FileDocCategorySizeDatePackage
DocumentsContractApi19.javaAPI DocAndroid 5.1 API6743Thu Mar 12 22:22:56 GMT 2015android.support.v4.provider

DocumentsContractApi19

public class DocumentsContractApi19 extends Object

Fields Summary
private static final String
TAG
Constructors Summary
Methods Summary
public static booleancanRead(android.content.Context context, android.net.Uri self)

        // Ignore if grant doesn't allow read
        if (context.checkCallingOrSelfUriPermission(self, Intent.FLAG_GRANT_READ_URI_PERMISSION)
                != PackageManager.PERMISSION_GRANTED) {
            return false;
        }

        // Ignore documents without MIME
        if (TextUtils.isEmpty(getRawType(context, self))) {
            return false;
        }

        return true;
    
public static booleancanWrite(android.content.Context context, android.net.Uri self)

        // Ignore if grant doesn't allow write
        if (context.checkCallingOrSelfUriPermission(self, Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
                != PackageManager.PERMISSION_GRANTED) {
            return false;
        }

        final String type = getRawType(context, self);
        final int flags = queryForInt(context, self, DocumentsContract.Document.COLUMN_FLAGS, 0);

        // Ignore documents without MIME
        if (TextUtils.isEmpty(type)) {
            return false;
        }

        // Deletable documents considered writable
        if ((flags & DocumentsContract.Document.FLAG_SUPPORTS_DELETE) != 0) {
            return true;
        }

        if (DocumentsContract.Document.MIME_TYPE_DIR.equals(type)
                && (flags & DocumentsContract.Document.FLAG_DIR_SUPPORTS_CREATE) != 0) {
            // Directories that allow create considered writable
            return true;
        } else if (!TextUtils.isEmpty(type)
                && (flags & DocumentsContract.Document.FLAG_SUPPORTS_WRITE) != 0) {
            // Writable normal files considered writable
            return true;
        }

        return false;
    
private static voidcloseQuietly(java.lang.AutoCloseable closeable)

        if (closeable != null) {
            try {
                closeable.close();
            } catch (RuntimeException rethrown) {
                throw rethrown;
            } catch (Exception ignored) {
            }
        }
    
public static booleandelete(android.content.Context context, android.net.Uri self)

        return DocumentsContract.deleteDocument(context.getContentResolver(), self);
    
public static booleanexists(android.content.Context context, android.net.Uri self)

        final ContentResolver resolver = context.getContentResolver();

        Cursor c = null;
        try {
            c = resolver.query(self, new String[] {
                    DocumentsContract.Document.COLUMN_DOCUMENT_ID }, null, null, null);
            return c.getCount() > 0;
        } catch (Exception e) {
            Log.w(TAG, "Failed query: " + e);
            return false;
        } finally {
            closeQuietly(c);
        }
    
public static java.lang.StringgetName(android.content.Context context, android.net.Uri self)

        return queryForString(context, self, DocumentsContract.Document.COLUMN_DISPLAY_NAME, null);
    
private static java.lang.StringgetRawType(android.content.Context context, android.net.Uri self)

        return queryForString(context, self, DocumentsContract.Document.COLUMN_MIME_TYPE, null);
    
public static java.lang.StringgetType(android.content.Context context, android.net.Uri self)

        final String rawType = getRawType(context, self);
        if (DocumentsContract.Document.MIME_TYPE_DIR.equals(rawType)) {
            return null;
        } else {
            return rawType;
        }
    
public static booleanisDirectory(android.content.Context context, android.net.Uri self)

        return DocumentsContract.Document.MIME_TYPE_DIR.equals(getRawType(context, self));
    
public static booleanisDocumentUri(android.content.Context context, android.net.Uri self)


           
        return DocumentsContract.isDocumentUri(context, self);
    
public static booleanisFile(android.content.Context context, android.net.Uri self)

        final String type = getRawType(context, self);
        if (DocumentsContract.Document.MIME_TYPE_DIR.equals(type) || TextUtils.isEmpty(type)) {
            return false;
        } else {
            return true;
        }
    
public static longlastModified(android.content.Context context, android.net.Uri self)

        return queryForLong(context, self, DocumentsContract.Document.COLUMN_LAST_MODIFIED, 0);
    
public static longlength(android.content.Context context, android.net.Uri self)

        return queryForLong(context, self, DocumentsContract.Document.COLUMN_SIZE, 0);
    
private static intqueryForInt(android.content.Context context, android.net.Uri self, java.lang.String column, int defaultValue)

        return (int) queryForLong(context, self, column, defaultValue);
    
private static longqueryForLong(android.content.Context context, android.net.Uri self, java.lang.String column, long defaultValue)

        final ContentResolver resolver = context.getContentResolver();

        Cursor c = null;
        try {
            c = resolver.query(self, new String[] { column }, null, null, null);
            if (c.moveToFirst() && !c.isNull(0)) {
                return c.getLong(0);
            } else {
                return defaultValue;
            }
        } catch (Exception e) {
            Log.w(TAG, "Failed query: " + e);
            return defaultValue;
        } finally {
            closeQuietly(c);
        }
    
private static java.lang.StringqueryForString(android.content.Context context, android.net.Uri self, java.lang.String column, java.lang.String defaultValue)

        final ContentResolver resolver = context.getContentResolver();

        Cursor c = null;
        try {
            c = resolver.query(self, new String[] { column }, null, null, null);
            if (c.moveToFirst() && !c.isNull(0)) {
                return c.getString(0);
            } else {
                return defaultValue;
            }
        } catch (Exception e) {
            Log.w(TAG, "Failed query: " + e);
            return defaultValue;
        } finally {
            closeQuietly(c);
        }