Example usage for android.content ContentProviderClient releaseQuietly

List of usage examples for android.content ContentProviderClient releaseQuietly

Introduction

In this page you can find the example usage for android.content ContentProviderClient releaseQuietly.

Prototype

public static void releaseQuietly(ContentProviderClient client) 

Source Link

Usage

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

@Override
public void close() {
    IOUtils.closeQuietly(cursor);
    ContentProviderClient.releaseQuietly(client);
    cursor = null;
    client = null;
}

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

@Override
public final DirectoryResult loadInBackground() {
    synchronized (this) {
        if (isLoadInBackgroundCanceled()) {
            throw new OperationCanceledException();
        }/*from   w  w w .j  a v a 2 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

public void updateFromUri(ContentResolver resolver, Uri uri) throws FileNotFoundException {
    ContentProviderClient client = null;
    Cursor cursor = null;// w  ww . j  ava2 s.c  om
    try {
        client = DocumentsApplication.acquireUnstableProviderOrThrow(resolver, uri.getAuthority());
        cursor = client.query(uri, null, null, null, null);
        if (!cursor.moveToFirst()) {
            throw new FileNotFoundException("Missing details for " + uri);
        }
        updateFromCursor(cursor, uri.getAuthority());
    } catch (Throwable t) {
        throw asFileNotFoundException(t);
    } finally {
        IOUtils.closeQuietly(cursor);
        ContentProviderClient.releaseQuietly(client);
    }
}

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

/**
 * Bring up requested provider and query for all active roots.
 *//*  w  w w . java2 s .  com*/
private Collection<RootInfo> loadRootsForAuthority(ContentResolver resolver, String authority) {
    if (DEBUG)
        Log.d(TAG, "Loading roots for " + authority);

    synchronized (mObservedAuthorities) {
        if (mObservedAuthorities.add(authority)) {
            // Watch for any future updates
            final Uri rootsUri = DocumentsContract.buildRootsUri(authority);
            mContext.getContentResolver().registerContentObserver(rootsUri, true, mObserver);
        }
    }

    final List<RootInfo> roots = Lists.newArrayList();
    final Uri rootsUri = DocumentsContract.buildRootsUri(authority);

    ContentProviderClient client = null;
    Cursor cursor = null;
    try {
        client = DocumentsApplication.acquireUnstableProviderOrThrow(resolver, authority);
        cursor = client.query(rootsUri, null, null, null, null);
        while (cursor.moveToNext()) {
            final RootInfo root = RootInfo.fromRootsCursor(authority, cursor);
            roots.add(root);
        }
    } catch (Exception e) {
        Log.w(TAG, "Failed to load some roots from " + authority + ": " + e);
    } finally {
        IOUtils.closeQuietly(cursor);
        ContentProviderClient.releaseQuietly(client);
    }
    return roots;
}