Search in sources :

Example 26 with ContentProviderClient

use of android.content.ContentProviderClient in project android_frameworks_base by AOSPA.

the class DocumentInfo method updateFromUri.

public void updateFromUri(ContentResolver resolver, Uri uri) throws FileNotFoundException {
    ContentProviderClient client = null;
    Cursor cursor = null;
    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);
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) Cursor(android.database.Cursor) ContentProviderClient(android.content.ContentProviderClient)

Example 27 with ContentProviderClient

use of android.content.ContentProviderClient in project android_frameworks_base by AOSPA.

the class DirectoryLoader method loadInBackground.

@Override
public final DirectoryResult loadInBackground() {
    synchronized (this) {
        if (isLoadInBackgroundCanceled()) {
            throw new OperationCanceledException();
        }
        mSignal = new CancellationSignal();
    }
    final ContentResolver resolver = getContext().getContentResolver();
    final String authority = mUri.getAuthority();
    final DirectoryResult result = new DirectoryResult();
    result.doc = mDoc;
    // Use default document when searching
    if (mSearchMode) {
        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;
        }
    }
    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 (mSearchMode) {
        result.sortOrder = State.SORT_ORDER_UNKNOWN;
    }
    if (DEBUG)
        Log.d(TAG, "userSortOrder=" + mUserSortOrder + ", sortOrder=" + result.sortOrder);
    ContentProviderClient client = null;
    Cursor cursor = null;
    try {
        client = DocumentsApplication.acquireUnstableProviderOrThrow(resolver, authority);
        cursor = client.query(mUri, null, null, null, getQuerySortOrder(result.sortOrder), mSignal);
        if (cursor == null) {
            throw new RemoteException("Provider returned null");
        }
        cursor.registerContentObserver(mObserver);
        cursor = new RootCursorWrapper(mUri.getAuthority(), mRoot.rootId, cursor, -1);
        if (mSearchMode) {
            // Filter directories out of search results, for now
            cursor = new FilteringCursorWrapper(cursor, null, SEARCH_REJECT_MIMES);
        }
        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;
}
Also used : OperationCanceledException(android.os.OperationCanceledException) FileNotFoundException(java.io.FileNotFoundException) Cursor(android.database.Cursor) Uri(android.net.Uri) OperationCanceledException(android.os.OperationCanceledException) RemoteException(android.os.RemoteException) FileNotFoundException(java.io.FileNotFoundException) ContentResolver(android.content.ContentResolver) RemoteException(android.os.RemoteException) CancellationSignal(android.os.CancellationSignal) ContentProviderClient(android.content.ContentProviderClient)

Example 28 with ContentProviderClient

use of android.content.ContentProviderClient in project android_frameworks_base by AOSPA.

the class DocumentClipper method getDocumentsFromClipData.

/**
     * Returns a list of Documents as decoded in clipData.
     * This should be run from inside an AsyncTask.
     */
public List<DocumentInfo> getDocumentsFromClipData(ClipData clipData) {
    assert (clipData != null);
    final List<DocumentInfo> srcDocs = new ArrayList<>();
    int count = clipData.getItemCount();
    if (count == 0) {
        return srcDocs;
    }
    ContentResolver resolver = mContext.getContentResolver();
    for (int i = 0; i < count; ++i) {
        ClipData.Item item = clipData.getItemAt(i);
        Uri itemUri = item.getUri();
        if (itemUri != null && DocumentsContract.isDocumentUri(mContext, itemUri)) {
            ContentProviderClient client = null;
            Cursor cursor = null;
            try {
                client = DocumentsApplication.acquireUnstableProviderOrThrow(resolver, itemUri.getAuthority());
                cursor = client.query(itemUri, null, null, null, null);
                cursor.moveToPosition(0);
                srcDocs.add(DocumentInfo.fromCursor(cursor, itemUri.getAuthority()));
            } catch (Exception e) {
                Log.e(TAG, e.getMessage());
            } finally {
                IoUtils.closeQuietly(cursor);
                ContentProviderClient.releaseQuietly(client);
            }
        }
    }
    return srcDocs;
}
Also used : ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) ClipData(android.content.ClipData) Uri(android.net.Uri) ContentProviderClient(android.content.ContentProviderClient) DocumentInfo(com.android.documentsui.model.DocumentInfo) ContentResolver(android.content.ContentResolver)

Example 29 with ContentProviderClient

use of android.content.ContentProviderClient in project DroidPlugin by DroidPluginTeam.

the class ContentProviderCompat method crazyAcquireContentProvider.

public static ContentProviderClient crazyAcquireContentProvider(Context context, String name) {
    ContentProviderClient client = acquireContentProviderClient(context, name);
    if (client == null) {
        int retry = 0;
        while (retry < 5 && client == null) {
            SystemClock.sleep(100);
            retry++;
            client = acquireContentProviderClient(context, name);
        }
    }
    return client;
}
Also used : ContentProviderClient(android.content.ContentProviderClient)

Example 30 with ContentProviderClient

use of android.content.ContentProviderClient in project DroidPlugin by DroidPluginTeam.

the class ContentProviderCompat method call.

public static Bundle call(Context context, Uri uri, String method, String arg, Bundle extras) {
    if (VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
        return context.getContentResolver().call(uri, method, arg, extras);
    }
    ContentProviderClient client = crazyAcquireContentProvider(context, uri);
    Bundle res = null;
    try {
        res = client.call(method, arg, extras);
    } catch (RemoteException e) {
        e.printStackTrace();
    } finally {
        releaseQuietly(client);
    }
    return res;
}
Also used : Bundle(android.os.Bundle) RemoteException(android.os.RemoteException) ContentProviderClient(android.content.ContentProviderClient)

Aggregations

ContentProviderClient (android.content.ContentProviderClient)93 RemoteException (android.os.RemoteException)30 Cursor (android.database.Cursor)28 Uri (android.net.Uri)24 ContentResolver (android.content.ContentResolver)18 Bundle (android.os.Bundle)18 FileNotFoundException (java.io.FileNotFoundException)15 ArrayList (java.util.ArrayList)13 IOException (java.io.IOException)9 Test (org.junit.Test)9 OperationCanceledException (android.os.OperationCanceledException)8 CancellationSignal (android.os.CancellationSignal)7 IContentProvider (android.content.IContentProvider)6 ActivityManager (android.app.ActivityManager)5 AlertDialog (android.app.AlertDialog)5 ClipData (android.content.ClipData)5 ComponentName (android.content.ComponentName)5 Intent (android.content.Intent)5 ServiceConnection (android.content.ServiceConnection)5 UriPermission (android.content.UriPermission)5