Search in sources :

Example 16 with ContentProviderClient

use of android.content.ContentProviderClient in project platform_frameworks_base by android.

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 17 with ContentProviderClient

use of android.content.ContentProviderClient in project robolectric by robolectric.

the class ShadowContentProviderClientTest method shouldDelegateToContentProvider.

@Test
@Config(minSdk = JELLY_BEAN_MR2)
public void shouldDelegateToContentProvider() throws Exception {
    ContentProviderClient client = contentResolver.acquireContentProviderClient(AUTHORITY);
    client.query(URI, PROJECTION, SELECTION, SELECTION_ARGS, SORT_ORDER);
    verify(provider).query(URI, PROJECTION, SELECTION, SELECTION_ARGS, SORT_ORDER);
    CancellationSignal signal = new CancellationSignal();
    client.query(URI, PROJECTION, SELECTION, SELECTION_ARGS, SORT_ORDER, signal);
    verify(provider).query(URI, PROJECTION, SELECTION, SELECTION_ARGS, SORT_ORDER, signal);
    client.insert(URI, VALUES);
    verify(provider).insert(URI, VALUES);
    client.update(URI, VALUES, SELECTION, SELECTION_ARGS);
    verify(provider).update(URI, VALUES, SELECTION, SELECTION_ARGS);
    client.delete(URI, SELECTION, SELECTION_ARGS);
    verify(provider).delete(URI, SELECTION, SELECTION_ARGS);
    client.getType(URI);
    verify(provider).getType(URI);
    client.openFile(URI, "rw");
    verify(provider).openFile(URI, "rw");
    client.openAssetFile(URI, "r");
    verify(provider).openAssetFile(URI, "r");
    final Bundle opts = new Bundle();
    client.openTypedAssetFileDescriptor(URI, MIME_TYPE, opts);
    verify(provider).openTypedAssetFile(URI, MIME_TYPE, opts);
    client.getStreamTypes(URI, MIME_TYPE);
    verify(provider).getStreamTypes(URI, MIME_TYPE);
    final ArrayList<ContentProviderOperation> ops = new ArrayList<>();
    client.applyBatch(ops);
    verify(provider).applyBatch(ops);
    final ContentValues[] values = { VALUES };
    client.bulkInsert(URI, values);
    verify(provider).bulkInsert(URI, values);
    final String method = "method";
    final String arg = "arg";
    final Bundle extras = new Bundle();
    client.call(method, arg, extras);
    verify(provider).call(method, arg, extras);
}
Also used : ContentValues(android.content.ContentValues) ContentProviderOperation(android.content.ContentProviderOperation) Bundle(android.os.Bundle) ArrayList(java.util.ArrayList) CancellationSignal(android.os.CancellationSignal) ContentProviderClient(android.content.ContentProviderClient) Test(org.junit.Test) Config(org.robolectric.annotation.Config)

Example 18 with ContentProviderClient

use of android.content.ContentProviderClient in project robolectric by robolectric.

the class ShadowContentProviderClientTest method acquireContentProviderClient_isStable.

@Test
public void acquireContentProviderClient_isStable() {
    ContentProviderClient client = contentResolver.acquireContentProviderClient(AUTHORITY);
    assertThat(shadowOf(client).isStable()).isTrue();
}
Also used : ContentProviderClient(android.content.ContentProviderClient) Test(org.junit.Test)

Example 19 with ContentProviderClient

use of android.content.ContentProviderClient in project apps-android-wikipedia by wikimedia.

the class MainPageReadMoreTopicTask method getInterestedHistoryEntry.

private Cursor getInterestedHistoryEntry() {
    Context context = WikipediaApp.getInstance();
    ContentProviderClient client = HistoryEntry.DATABASE_TABLE.acquireClient(context);
    try {
        Uri uri = PageHistoryContract.PageWithImage.URI;
        final String[] projection = null;
        String selection = ":sourceCol != ? and :sourceCol != ? and :sourceCol != ? and :timeSpentCol >= ?".replaceAll(":sourceCol", PageHistoryContract.Page.SOURCE.qualifiedName()).replaceAll(":timeSpentCol", PageHistoryContract.Page.TIME_SPENT.qualifiedName());
        String[] selectionArgs = new String[] { Integer.toString(HistoryEntry.SOURCE_MAIN_PAGE), Integer.toString(HistoryEntry.SOURCE_RANDOM), Integer.toString(HistoryEntry.SOURCE_FEED_MAIN_PAGE), Integer.toString(context.getResources().getInteger(R.integer.article_engagement_threshold_sec)) };
        String order = PageHistoryContract.PageWithImage.ORDER_MRU;
        return client.query(uri, projection, selection, selectionArgs, order);
    } catch (RemoteException e) {
        throw new RuntimeException(e);
    } finally {
        ContentProviderClientCompat.close(client);
    }
}
Also used : Context(android.content.Context) RemoteException(android.os.RemoteException) Uri(android.net.Uri) ContentProviderClient(android.content.ContentProviderClient)

Example 20 with ContentProviderClient

use of android.content.ContentProviderClient in project android by nextcloud.

the class FileDataStorageManager method deleteFileInMediaScan.

public void deleteFileInMediaScan(String path) {
    String mimetypeString = FileStorageUtils.getMimeTypeFromName(path);
    ContentResolver contentResolver = getContentResolver();
    if (contentResolver != null) {
        if (MimeTypeUtil.isImage(mimetypeString)) {
            // Images
            contentResolver.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, MediaStore.Images.Media.DATA + "=?", new String[] { path });
        } else if (MimeTypeUtil.isAudio(mimetypeString)) {
            // Audio
            contentResolver.delete(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, MediaStore.Audio.Media.DATA + "=?", new String[] { path });
        } else if (MimeTypeUtil.isVideo(mimetypeString)) {
            // Video
            contentResolver.delete(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, MediaStore.Video.Media.DATA + "=?", new String[] { path });
        }
    } else {
        ContentProviderClient contentProviderClient = getContentProviderClient();
        try {
            if (MimeTypeUtil.isImage(mimetypeString)) {
                // Images
                contentProviderClient.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, MediaStore.Images.Media.DATA + "=?", new String[] { path });
            } else if (MimeTypeUtil.isAudio(mimetypeString)) {
                // Audio
                contentProviderClient.delete(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, MediaStore.Audio.Media.DATA + "=?", new String[] { path });
            } else if (MimeTypeUtil.isVideo(mimetypeString)) {
                // Video
                contentProviderClient.delete(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, MediaStore.Video.Media.DATA + "=?", new String[] { path });
            }
        } catch (RemoteException e) {
            Log_OC.e(TAG, "Exception deleting media file in MediaStore " + e.getMessage(), e);
        }
    }
}
Also used : RemoteException(android.os.RemoteException) ContentProviderClient(android.content.ContentProviderClient) ContentResolver(android.content.ContentResolver)

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