Search in sources :

Example 16 with ContentProvider

use of android.content.ContentProvider in project AndroidLife by CaMnter.

the class RemoteContentProvider method insert.

/**
 * 获取插件 ContentProvider 后
 * 解析复合 Uri 协议,获取其中包含的插件 Uri 协议
 *
 * 手动调用 插件 ContentProvider # insert(...)
 *
 * @param uri uri
 * @param values values
 * @return Uri
 */
@Override
public Uri insert(Uri uri, ContentValues values) {
    ContentProvider provider = getContentProvider(uri);
    Uri pluginUri = Uri.parse(uri.getQueryParameter(KEY_URI));
    if (provider != null) {
        return provider.insert(pluginUri, values);
    }
    return uri;
}
Also used : ContentProvider(android.content.ContentProvider) Uri(android.net.Uri)

Example 17 with ContentProvider

use of android.content.ContentProvider in project AndroidLife by CaMnter.

the class RemoteContentProvider method call.

/**
 * 获取插件 ContentProvider 后
 * 解析复合 Uri 协议,获取其中包含的插件 Uri 协议
 *
 * 手动调用 插件 ContentProvider # call(...)
 *
 * @param method method
 * @param arg arg
 * @param extras extras
 * @return Bundle
 */
@Override
public Bundle call(String method, String arg, Bundle extras) {
    Log.d(TAG, "call " + method + " with extras : " + extras);
    if (extras == null || extras.getString(KEY_WRAPPER_URI) == null) {
        return null;
    }
    Uri uri = Uri.parse(extras.getString(KEY_WRAPPER_URI));
    ContentProvider provider = getContentProvider(uri);
    if (provider != null) {
        return provider.call(method, arg, extras);
    }
    return null;
}
Also used : ContentProvider(android.content.ContentProvider) Uri(android.net.Uri)

Example 18 with ContentProvider

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

the class ShadowContentProviderOperationBuilderTest method build.

@Test
public void build() throws Exception {
    Uri uri = Uri.parse("content://authority/path");
    ContentProviderOperation.Builder builder = ContentProviderOperation.newUpdate(uri);
    builder.withSelection("a=?", new String[] { "a" });
    builder.withValue("k1", "v1");
    ContentValues cv = new ContentValues();
    cv.put("k2", "v2");
    builder.withValues(cv);
    ContentProviderOperation op = builder.build();
    assertThat(op).isNotNull();
    assertThat(op.getUri()).isEqualTo(uri);
    final ContentRequest request = new ContentRequest();
    ContentProvider provider = new ContentProvider() {

        @Override
        public boolean onCreate() {
            return true;
        }

        @Override
        public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
            return null;
        }

        @Override
        public String getType(Uri uri) {
            return null;
        }

        @Override
        public Uri insert(Uri uri, ContentValues values) {
            return null;
        }

        @Override
        public int delete(Uri uri, String selection, String[] selectionArgs) {
            return 0;
        }

        @Override
        public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
            request.uri = uri;
            request.values = values;
            request.selection = selection;
            request.selectionArgs = selectionArgs;
            return 0;
        }
    };
    op.apply(provider, null, 0);
    assertThat(request.uri).isEqualTo(uri);
    assertThat(request.selection).isEqualTo("a=?");
    assertThat(request.selectionArgs).isEqualTo(new String[] { "a" });
    assertThat(request.values.containsKey("k1")).isTrue();
    assertThat(request.values.containsKey("k2")).isTrue();
}
Also used : ContentValues(android.content.ContentValues) ContentProviderOperation(android.content.ContentProviderOperation) ContentProvider(android.content.ContentProvider) Uri(android.net.Uri) Test(org.junit.Test)

Example 19 with ContentProvider

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

the class ShadowContentResolverTest method applyBatchForRegisteredProvider.

@SuppressWarnings("serial")
@Test
public void applyBatchForRegisteredProvider() throws RemoteException, OperationApplicationException {
    final List<String> operations = new ArrayList<>();
    ShadowContentResolver.registerProviderInternal("registeredProvider", new ContentProvider() {

        @Override
        public boolean onCreate() {
            return true;
        }

        @Override
        public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
            operations.add("query");
            MatrixCursor cursor = new MatrixCursor(new String[] { "a" });
            cursor.addRow(new Object[] { "b" });
            return cursor;
        }

        @Override
        public String getType(Uri uri) {
            return null;
        }

        @Override
        public Uri insert(Uri uri, ContentValues values) {
            operations.add("insert");
            return ContentUris.withAppendedId(uri, 1);
        }

        @Override
        public int delete(Uri uri, String selection, String[] selectionArgs) {
            operations.add("delete");
            return 0;
        }

        @Override
        public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
            operations.add("update");
            return 0;
        }
    });
    final Uri uri = Uri.parse("content://registeredProvider/path");
    List<ContentProviderOperation> contentProviderOperations = Arrays.asList(ContentProviderOperation.newInsert(uri).withValue("a", "b").build(), ContentProviderOperation.newUpdate(uri).withValue("a", "b").build(), ContentProviderOperation.newDelete(uri).build(), ContentProviderOperation.newAssertQuery(uri).withValue("a", "b").build());
    contentResolver.applyBatch("registeredProvider", new ArrayList<>(contentProviderOperations));
    assertThat(operations).containsExactly("insert", "update", "delete", "query");
}
Also used : ContentValues(android.content.ContentValues) ContentProviderOperation(android.content.ContentProviderOperation) ContentProvider(android.content.ContentProvider) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) MatrixCursor(android.database.MatrixCursor) BaseCursor(org.robolectric.fakes.BaseCursor) Uri(android.net.Uri) MatrixCursor(android.database.MatrixCursor) Test(org.junit.Test)

Example 20 with ContentProvider

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

the class ShadowContentResolverTest method getType_shouldReturnProviderValue.

@Test
public void getType_shouldReturnProviderValue() {
    ShadowContentResolver.registerProviderInternal(AUTHORITY, new ContentProvider() {

        @Override
        public boolean onCreate() {
            return false;
        }

        @Override
        public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
            return new BaseCursor();
        }

        @Override
        public Uri insert(Uri uri, ContentValues values) {
            return null;
        }

        @Override
        public int delete(Uri uri, String selection, String[] selectionArgs) {
            return -1;
        }

        @Override
        public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
            return -1;
        }

        @Override
        public String getType(Uri uri) {
            return "mytype";
        }
    });
    final Uri uri = Uri.parse("content://" + AUTHORITY + "/some/path");
    assertThat(contentResolver.getType(uri)).isEqualTo("mytype");
}
Also used : ContentValues(android.content.ContentValues) BaseCursor(org.robolectric.fakes.BaseCursor) ContentProvider(android.content.ContentProvider) Cursor(android.database.Cursor) MatrixCursor(android.database.MatrixCursor) BaseCursor(org.robolectric.fakes.BaseCursor) Uri(android.net.Uri) Test(org.junit.Test)

Aggregations

ContentProvider (android.content.ContentProvider)53 Uri (android.net.Uri)28 IContentProvider (android.content.IContentProvider)16 Test (org.junit.Test)12 Implementation (org.robolectric.annotation.Implementation)11 ContentValues (android.content.ContentValues)8 Context (android.content.Context)7 ApplicationInfo (android.content.pm.ApplicationInfo)7 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)7 RemoteException (android.os.RemoteException)7 AndroidRuntimeException (android.util.AndroidRuntimeException)7 ComponentName (android.content.ComponentName)6 IBinder (android.os.IBinder)6 BaseCursor (org.robolectric.fakes.BaseCursor)6 ContentProviderOperation (android.content.ContentProviderOperation)4 OperationApplicationException (android.content.OperationApplicationException)4 ProviderInfo (android.content.pm.ProviderInfo)3 Cursor (android.database.Cursor)3 MatrixCursor (android.database.MatrixCursor)3 ContentProviderResult (android.content.ContentProviderResult)2