Example usage for android.provider DocumentsContract buildDocumentUri

List of usage examples for android.provider DocumentsContract buildDocumentUri

Introduction

In this page you can find the example usage for android.provider DocumentsContract buildDocumentUri.

Prototype

public static Uri buildDocumentUri(String authority, String documentId) 

Source Link

Document

Build URI representing the target Document#COLUMN_DOCUMENT_ID in a document provider.

Usage

From source file:cn.edu.wyu.documentviewer.DirectoryResult.java

@Override
public final DirectoryResult loadInBackground() {
    synchronized (this) {
        if (isLoadInBackgroundCanceled()) {
            throw new OperationCanceledException();
        }/*from   w ww  .  j  ava2 s .com*/
        mSignal = new CancellationSignal();
    }

    final ContentResolver resolver = getContext().getContentResolver();
    final String authority = mUri.getAuthority();

    final DirectoryResult result = new DirectoryResult();

    int userMode = State.MODE_UNKNOWN;

    // Use default document when searching
    if (mType == DirectoryFragment.TYPE_SEARCH) {
        final Uri docUri = DocumentsContract.buildDocumentUri(mRoot.authority, mRoot.documentId);
        try {
            mDoc = DocumentInfo.fromUri(resolver, docUri);
        } catch (FileNotFoundException e) {
            Log.w(TAG, "Failed to query", e);
            result.exception = e;
            return result;
        }
    }

    // Pick up any custom modes requested by user
    Cursor cursor = null;
    try {
        final Uri stateUri = RecentsProvider.buildState(mRoot.authority, mRoot.rootId, mDoc.documentId);
        cursor = resolver.query(stateUri, null, null, null, null);
        if (cursor.moveToFirst()) {
            userMode = getCursorInt(cursor, StateColumns.MODE);
        }
    } finally {
        IOUtils.closeQuietly(cursor);
    }

    if (userMode != State.MODE_UNKNOWN) {
        result.mode = userMode;
    } else {
        if ((mDoc.flags & Document.FLAG_DIR_PREFERS_GRID) != 0) {
            result.mode = State.MODE_GRID;
        } else {
            result.mode = State.MODE_LIST;
        }
    }

    if (mUserSortOrder != State.SORT_ORDER_UNKNOWN) {
        result.sortOrder = mUserSortOrder;
    } else {
        if ((mDoc.flags & Document.FLAG_DIR_PREFERS_LAST_MODIFIED) != 0) {
            result.sortOrder = State.SORT_ORDER_LAST_MODIFIED;
        } else {
            result.sortOrder = State.SORT_ORDER_DISPLAY_NAME;
        }
    }

    // Search always uses ranking from provider
    if (mType == DirectoryFragment.TYPE_SEARCH) {
        result.sortOrder = State.SORT_ORDER_UNKNOWN;
    }

    Log.d(TAG, "userMode=" + userMode + ", userSortOrder=" + mUserSortOrder + " --> mode=" + result.mode
            + ", sortOrder=" + result.sortOrder);

    ContentProviderClient client = null;
    try {
        client = DocumentsApplication.acquireUnstableProviderOrThrow(resolver, authority);

        cursor = client.query(mUri, null, null, null, getQuerySortOrder(result.sortOrder), mSignal);
        cursor.registerContentObserver(mObserver);

        cursor = new RootCursorWrapper(mUri.getAuthority(), mRoot.rootId, cursor, -1);

        if (mType == DirectoryFragment.TYPE_SEARCH) {
            // Filter directories out of search results, for now
            cursor = new FilteringCursorWrapper(cursor, null, SEARCH_REJECT_MIMES);
        } else {
            // Normal directories should have sorting applied
            cursor = new SortingCursorWrapper(cursor, result.sortOrder);
        }

        result.client = client;
        result.cursor = cursor;
    } catch (Exception e) {
        Log.w(TAG, "Failed to query", e);
        result.exception = e;
        ContentProviderClient.releaseQuietly(client);
    } finally {
        synchronized (this) {
            mSignal = null;
        }
    }

    return result;
}

From source file:cn.edu.wyu.documentviewer.model.DocumentInfo.java

private void deriveFields() {
    derivedUri = DocumentsContract.buildDocumentUri(authority, documentId);
}

From source file:net.sf.xfd.provider.PublicProvider.java

@Nullable
@Override// w  w w  . j  a  va 2s  . co  m
public Cursor query(@NonNull Uri uri, String[] projection, String selection, String[] selectionArgs,
        String sortOrder) {
    String path = uri.getPath();

    if (TextUtils.isEmpty(uri.getPath())) {
        path = "/";
    }

    try {
        assertAbsolute(path);
    } catch (FileNotFoundException e) {
        return null;
    }

    path = canonString(path);

    if (!path.equals(uri.getPath())) {
        uri = uri.buildUpon().path(path).build();
    }

    if (!checkAccess(uri, "r")) {
        return null;
    }

    if (projection == null) {
        projection = COMMON_PROJECTION;
    }

    final OS os = base.getOS();
    if (os == null) {
        return null;
    }

    try {
        final MatrixCursor cursor = new MatrixCursor(projection, 1);

        final Object[] row = new Object[projection.length];

        final Stat stat = new Stat();
        final String name = extractName(path);
        final String mime = base.getTypeFast(path, name, stat);

        for (int i = 0; i < projection.length; ++i) {
            String col = projection[i];

            switch (col) {
            case BaseColumns._ID:
                row[i] = stat.st_ino;
                break;
            case COLUMN_DISPLAY_NAME:
                row[i] = name;
                break;
            case COLUMN_SIZE:
                row[i] = stat.st_size;
                break;
            case COLUMN_MIME_TYPE:
                row[i] = mime;
                break;
            default:
                row[i] = null;
            }
        }

        cursor.addRow(row);

        final Context context = getContext();
        assert context != null;

        final String packageName = context.getPackageName();

        cursor.setNotificationUri(context.getContentResolver(),
                DocumentsContract.buildDocumentUri(packageName + FileProvider.AUTHORITY_SUFFIX, path));

        return cursor;
    } catch (IOException e) {
        e.printStackTrace();

        return null;
    }
}

From source file:org.alfresco.mobile.android.application.providers.storage.StorageAccessDocumentsProvider.java

@Override
public Cursor queryDocument(String documentId, String[] projection) throws FileNotFoundException {
    // Log.d(TAG, "Query Document : " + documentId);
    final DocumentFolderCursor docsCursor = new DocumentFolderCursor(resolveDocumentProjection(projection));
    Uri uri = DocumentsContract.buildDocumentUri(mAuthority, documentId);

    try {/*from w  w  w.j a  v  a  2  s .c om*/
        EncodedQueryUri cUri = new EncodedQueryUri(documentId);
        // checkSession(cUri);

        if (cUri.id != null) {
            if (nodesIndex.containsKey(cUri.id)) {
                addNodeRow(docsCursor, nodesIndex.get(cUri.id));
            } else if (pathIndex.containsKey(cUri.id)) {
                addNodeRow(docsCursor, pathIndex.get(cUri.id));
            } else if (siteIndex.containsKey(cUri.id)) {
                addSiteRow(docsCursor, siteIndex.get(cUri.id));
            } else if (IMPORT_FOLDER_LIST.contains(cUri.id)) {
                addRootMenuRow(docsCursor, Integer.parseInt(cUri.id));
            }
        } else {
            // Log.d(TAG, "Default Row " + documentId);
            DocumentFolderCursor.RowBuilder row = docsCursor.newRow();
            row.add(Document.COLUMN_DOCUMENT_ID,
                    EncodedQueryUri.encodeItem(PREFIX_ACCOUNT, cUri.accountId, cUri.id));
            row.add(Document.COLUMN_DISPLAY_NAME, cUri.id);
            row.add(Document.COLUMN_SIZE, null);
            row.add(Document.COLUMN_LAST_MODIFIED, null);
            row.add(Document.COLUMN_MIME_TYPE, Document.MIME_TYPE_DIR);
            row.add(Document.COLUMN_ICON, null);
        }
    } catch (Exception e) {
        docsCursor.setErrorInformation("Error : " + e.getMessage());
        docsCursor.setNotificationUri(getContext().getContentResolver(), uri);
        getContext().getContentResolver().notifyChange(uri, null);
        Log.d(TAG, Log.getStackTraceString(e));
    }

    return docsCursor;
}

From source file:net.sf.xfd.provider.PublicProvider.java

@Override
public Uri canonicalize(@NonNull Uri uri) {
    try {//from   w ww.j a  v a  2  s . c  o m
        base.assertAbsolute(uri);

        String grantMode = uri.getQueryParameter(URI_ARG_MODE);
        if (TextUtils.isEmpty(grantMode)) {
            grantMode = "r";
        }

        verifyMac(uri, grantMode, grantMode);

        final int flags = ParcelFileDescriptor.parseMode(grantMode);

        final Context context = getContext();
        assert context != null;

        final String packageName = context.getPackageName();

        final Uri canon = DocumentsContract.buildDocumentUri(packageName + FileProvider.AUTHORITY_SUFFIX,
                canonString(uri.getPath()));

        final int callerUid = Binder.getCallingUid();

        if (callerUid != Process.myUid()) {
            int grantFlags = Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION;

            if ((flags & ParcelFileDescriptor.MODE_READ_ONLY) == flags) {
                grantFlags |= Intent.FLAG_GRANT_READ_URI_PERMISSION;
            } else if ((flags & ParcelFileDescriptor.MODE_WRITE_ONLY) == flags)
                grantFlags |= Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
            else {
                grantFlags |= Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
                grantFlags |= Intent.FLAG_GRANT_READ_URI_PERMISSION;
            }

            final String[] packages;

            final String caller = getCallingPackage();
            if (caller != null) {
                packages = new String[] { caller };
            } else {
                final PackageManager pm = context.getPackageManager();
                packages = pm.getPackagesForUid(callerUid);
            }

            if (packages != null) {
                for (String pkg : packages) {
                    context.grantUriPermission(pkg, canon, grantFlags);
                }
            }
        }

        return canon;
    } catch (FileNotFoundException e) {
        return null;
    }
}

From source file:org.alfresco.mobile.android.application.providers.storage.StorageAccessDocumentsProvider.java

@Override
public void deleteDocument(final String documentId) throws FileNotFoundException {
    // Log.v(TAG, "deleteDocument");
    final Uri uri = DocumentsContract.buildDocumentUri(mAuthority, documentId);

    final EncodedQueryUri cUri = new EncodedQueryUri(documentId);

    Boolean active = mLoadingUris.get(uri);

    if (active != null && !active) {
        // loading request is finished and refreshed
        mLoadingUris.remove(uri);/*w  w  w  . j  av  a 2  s  .c  o  m*/
    }

    if (active == null && session != null) {
        mLoadingUris.put(uri, Boolean.TRUE);

        new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... params) {
                checkSession(cUri);
                Node currentNode = retrieveNode(cUri.id);
                session.getServiceRegistry().getDocumentFolderService().deleteNode(currentNode);
                return null;
            }

            protected void onPostExecute(Void noResult) {
                mLoadingUris.put(uri, Boolean.FALSE);
                getContext().getContentResolver().notifyChange(uri, null);
            }
        }.execute();
    }
}